composite.el 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762
  1. ;;; composite.el --- support character composition
  2. ;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
  3. ;; 2008, 2009, 2010, 2011
  4. ;; National Institute of Advanced Industrial Science and Technology (AIST)
  5. ;; Registration Number H14PRO021
  6. ;; Author: Kenichi HANDA <handa@etl.go.jp>
  7. ;; (according to ack.texi)
  8. ;; Keywords: mule, multilingual, character composition
  9. ;; Package: emacs
  10. ;; This file is part of GNU Emacs.
  11. ;; GNU Emacs is free software: you can redistribute it and/or modify
  12. ;; it under the terms of the GNU General Public License as published by
  13. ;; the Free Software Foundation, either version 3 of the License, or
  14. ;; (at your option) any later version.
  15. ;; GNU Emacs is distributed in the hope that it will be useful,
  16. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. ;; GNU General Public License for more details.
  19. ;; You should have received a copy of the GNU General Public License
  20. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  21. ;;; Commentary:
  22. ;;; Code:
  23. (eval-when-compile (require 'cl))
  24. (defconst reference-point-alist
  25. '((tl . 0) (tc . 1) (tr . 2)
  26. (Bl . 3) (Bc . 4) (Br . 5)
  27. (bl . 6) (bc . 7) (br . 8)
  28. (cl . 9) (cc . 10) (cr . 11)
  29. (top-left . 0) (top-center . 1) (top-right . 2)
  30. (base-left . 3) (base-center . 4) (base-right . 5)
  31. (bottom-left . 6) (bottom-center . 7) (bottom-right . 8)
  32. (center-left . 9) (center-center . 10) (center-right . 11)
  33. ;; For backward compatibility...
  34. (ml . 3) (mc . 10) (mr . 5)
  35. (mid-left . 3) (mid-center . 10) (mid-right . 5))
  36. "Alist of symbols vs integer codes of glyph reference points.
  37. A glyph reference point symbol is to be used to specify a composition
  38. rule in COMPONENTS argument to such functions as `compose-region'.
  39. The meaning of glyph reference point codes is as follows:
  40. 0----1----2 <---- ascent 0:tl or top-left
  41. | | 1:tc or top-center
  42. | | 2:tr or top-right
  43. | | 3:Bl or base-left 9:cl or center-left
  44. 9 10 11 <---- center 4:Bc or base-center 10:cc or center-center
  45. | | 5:Br or base-right 11:cr or center-right
  46. --3----4----5-- <-- baseline 6:bl or bottom-left
  47. | | 7:bc or bottom-center
  48. 6----7----8 <---- descent 8:br or bottom-right
  49. Glyph reference point symbols are to be used to specify composition
  50. rule of the form \(GLOBAL-REF-POINT . NEW-REF-POINT), where
  51. GLOBAL-REF-POINT is a reference point in the overall glyphs already
  52. composed, and NEW-REF-POINT is a reference point in the new glyph to
  53. be added.
  54. For instance, if GLOBAL-REF-POINT is `br' (bottom-right) and
  55. NEW-REF-POINT is `tc' (top-center), the overall glyph is updated as
  56. follows (the point `*' corresponds to both reference points):
  57. +-------+--+ <--- new ascent
  58. | | |
  59. | global| |
  60. | glyph | |
  61. -- | | |-- <--- baseline \(doesn't change)
  62. +----+--*--+
  63. | | new |
  64. | |glyph|
  65. +----+-----+ <--- new descent
  66. A composition rule may have the form \(GLOBAL-REF-POINT
  67. NEW-REF-POINT XOFF YOFF), where XOFF and YOFF specify how much
  68. to shift NEW-REF-POINT from GLOBAL-REF-POINT. In this case, XOFF
  69. and YOFF are integers in the range -100..100 representing the
  70. shifting percentage against the font size.")
  71. ;;;###autoload
  72. (defun encode-composition-rule (rule)
  73. "Encode composition rule RULE into an integer value.
  74. RULE is a cons of global and new reference point symbols
  75. \(see `reference-point-alist')."
  76. ;; This must be compatible with C macro COMPOSITION_ENCODE_RULE
  77. ;; defined in composite.h.
  78. (if (and (integerp rule) (< rule 144))
  79. ;; Already encoded.
  80. rule
  81. (if (consp rule)
  82. (let ((gref (car rule))
  83. (nref (cdr rule))
  84. xoff yoff)
  85. (if (consp nref) ; (GREF NREF XOFF YOFF)
  86. (progn
  87. (setq xoff (nth 1 nref)
  88. yoff (nth 2 nref)
  89. nref (car nref))
  90. (or (and (>= xoff -100) (<= xoff 100)
  91. (>= yoff -100) (<= yoff 100))
  92. (error "Invalid composition rule: %s" rule))
  93. (setq xoff (+ xoff 128) yoff (+ yoff 128)))
  94. ;; (GREF . NREF)
  95. (setq xoff 0 yoff 0))
  96. (or (integerp gref)
  97. (setq gref (cdr (assq gref reference-point-alist))))
  98. (or (integerp nref)
  99. (setq nref (cdr (assq nref reference-point-alist))))
  100. (or (and (>= gref 0) (< gref 12) (>= nref 0) (< nref 12))
  101. (error "Invalid composition rule: %S" rule))
  102. (logior (lsh xoff 16) (lsh yoff 8) (+ (* gref 12) nref)))
  103. (error "Invalid composition rule: %S" rule))))
  104. ;; Decode encoded composition rule RULE-CODE. The value is a cons of
  105. ;; global and new reference point symbols.
  106. ;; This must be compatible with C macro COMPOSITION_DECODE_RULE
  107. ;; defined in composite.h.
  108. (defun decode-composition-rule (rule-code)
  109. (or (and (natnump rule-code) (< rule-code #x1000000))
  110. (error "Invalid encoded composition rule: %S" rule-code))
  111. (let ((xoff (lsh rule-code -16))
  112. (yoff (logand (lsh rule-code -8) #xFF))
  113. gref nref)
  114. (setq rule-code (logand rule-code #xFF)
  115. gref (car (rassq (/ rule-code 12) reference-point-alist))
  116. nref (car (rassq (% rule-code 12) reference-point-alist)))
  117. (or (and gref (symbolp gref) nref (symbolp nref))
  118. (error "Invalid composition rule code: %S" rule-code))
  119. (if (and (= xoff 0) (= yoff 0))
  120. (cons gref nref)
  121. (setq xoff (- xoff 128) yoff (- yoff 128))
  122. (list gref xoff yoff nref))))
  123. ;; Encode composition rules in composition components COMPONENTS. The
  124. ;; value is a copy of COMPONENTS, where composition rules (cons of
  125. ;; global and new glyph reference point symbols) are replaced with
  126. ;; encoded composition rules. Optional 2nd argument NOCOPY non-nil
  127. ;; means don't make a copy but modify COMPONENTS directly.
  128. (defun encode-composition-components (components &optional nocopy)
  129. (or nocopy
  130. (setq components (copy-sequence components)))
  131. (if (vectorp components)
  132. (let ((len (length components))
  133. (i 1))
  134. (while (< i len)
  135. (aset components i
  136. (encode-composition-rule (aref components i)))
  137. (setq i (+ i 2))))
  138. (let ((tail (cdr components)))
  139. (while tail
  140. (setcar tail
  141. (encode-composition-rule (car tail)))
  142. (setq tail (nthcdr 2 tail)))))
  143. components)
  144. ;; Decode composition rule codes in composition components COMPONENTS.
  145. ;; The value is a copy of COMPONENTS, where composition rule codes are
  146. ;; replaced with composition rules (cons of global and new glyph
  147. ;; reference point symbols). Optional 2nd argument NOCOPY non-nil
  148. ;; means don't make a copy but modify COMPONENTS directly.
  149. ;; It is assumed that COMPONENTS is a vector and is for rule-base
  150. ;; composition, thus (2N+1)th elements are rule codes.
  151. (defun decode-composition-components (components &optional nocopy)
  152. (or nocopy
  153. (setq components (copy-sequence components)))
  154. (let ((len (length components))
  155. (i 1))
  156. (while (< i len)
  157. (aset components i
  158. (decode-composition-rule (aref components i)))
  159. (setq i (+ i 2))))
  160. components)
  161. (defun compose-region (start end &optional components modification-func)
  162. "Compose characters in the current region.
  163. Characters are composed relatively, i.e. composed by overstriking
  164. or stacking depending on ascent, descent and other metrics of
  165. glyphs.
  166. For instance, if the region has three characters \"XYZ\", X is
  167. regarded as BASE glyph, and Y is displayed:
  168. (1) above BASE if Y's descent value is not positive
  169. (2) below BASE if Y's ascent value is not positive
  170. (3) on BASE (i.e. at the BASE position) otherwise
  171. and Z is displayed with the same rule while regarding the whole
  172. XY glyphs as BASE.
  173. When called from a program, expects these four arguments.
  174. First two arguments START and END are positions (integers or markers)
  175. specifying the region.
  176. Optional 3rd argument COMPONENTS, if non-nil, is a character, a string
  177. or a vector or list of integers and rules.
  178. If it is a character, it is an alternate character to display instead
  179. of the text in the region.
  180. If it is a string, the elements are alternate characters. In
  181. this case, TAB element has a special meaning. If the first
  182. character is TAB, the glyphs are displayed with left padding space
  183. so that no pixel overlaps with the previous column. If the last
  184. character is TAB, the glyphs are displayed with right padding
  185. space so that no pixel overlaps with the following column.
  186. If it is a vector or list, it is a sequence of alternate characters and
  187. composition rules, where (2N)th elements are characters and (2N+1)th
  188. elements are composition rules to specify how to compose (2N+2)th
  189. elements with previously composed N glyphs.
  190. A composition rule is a cons of global and new glyph reference point
  191. symbols. See the documentation of `reference-point-alist' for more
  192. details.
  193. Optional 4th argument MODIFICATION-FUNC is a function to call to
  194. adjust the composition when it gets invalid because of a change of
  195. text in the composition."
  196. (interactive "r")
  197. (let ((modified-p (buffer-modified-p))
  198. (inhibit-read-only t))
  199. (if (or (vectorp components) (listp components))
  200. (setq components (encode-composition-components components)))
  201. (compose-region-internal start end components modification-func)
  202. (restore-buffer-modified-p modified-p)))
  203. (defun decompose-region (start end)
  204. "Decompose text in the current region.
  205. When called from a program, expects two arguments,
  206. positions (integers or markers) specifying the region."
  207. (interactive "r")
  208. (let ((modified-p (buffer-modified-p))
  209. (inhibit-read-only t))
  210. (remove-text-properties start end '(composition nil))
  211. (restore-buffer-modified-p modified-p)))
  212. (defun compose-string (string &optional start end components modification-func)
  213. "Compose characters in string STRING.
  214. The return value is STRING with the `composition' property put on all
  215. the characters in it.
  216. Optional 2nd and 3rd arguments START and END specify the range of
  217. STRING to be composed. They default to the beginning and the end of
  218. STRING respectively.
  219. Optional 4th argument COMPONENTS, if non-nil, is a character or a
  220. sequence (vector, list, or string) of integers. See the function
  221. `compose-region' for more detail.
  222. Optional 5th argument MODIFICATION-FUNC is a function to call to
  223. adjust the composition when it gets invalid because of a change of
  224. text in the composition."
  225. (if (or (vectorp components) (listp components))
  226. (setq components (encode-composition-components components)))
  227. (or start (setq start 0))
  228. (or end (setq end (length string)))
  229. (compose-string-internal string start end components modification-func)
  230. string)
  231. (defun decompose-string (string)
  232. "Return STRING where `composition' property is removed."
  233. (remove-text-properties 0 (length string) '(composition nil) string)
  234. string)
  235. (defun compose-chars (&rest args)
  236. "Return a string from arguments in which all characters are composed.
  237. For relative composition, arguments are characters.
  238. For rule-based composition, Mth \(where M is odd) arguments are
  239. characters, and Nth \(where N is even) arguments are composition rules.
  240. A composition rule is a cons of glyph reference points of the form
  241. \(GLOBAL-REF-POINT . NEW-REF-POINT). See the documentation of
  242. `reference-point-alist' for more detail."
  243. (let (str components)
  244. (if (consp (car (cdr args)))
  245. ;; Rule-base composition.
  246. (let ((tail (encode-composition-components args 'nocopy)))
  247. (while tail
  248. (setq str (cons (car tail) str))
  249. (setq tail (nthcdr 2 tail)))
  250. (setq str (concat (nreverse str))
  251. components args))
  252. ;; Relative composition.
  253. (setq str (concat args)))
  254. (compose-string-internal str 0 (length str) components)))
  255. (defun find-composition (pos &optional limit string detail-p)
  256. "Return information about a composition at or near buffer position POS.
  257. If the character at POS has `composition' property, the value is a list
  258. \(FROM TO VALID-P).
  259. FROM and TO specify the range of text that has the same `composition'
  260. property, VALID-P is t if this composition is valid, and nil if not.
  261. If there's no composition at POS, and the optional 2nd argument LIMIT
  262. is non-nil, search for a composition toward the position given by LIMIT.
  263. If no composition is found, return nil.
  264. Optional 3rd argument STRING, if non-nil, is a string to look for a
  265. composition in; nil means the current buffer.
  266. If a valid composition is found and the optional 4th argument DETAIL-P
  267. is non-nil, the return value is a list of the form
  268. (FROM TO COMPONENTS RELATIVE-P MOD-FUNC WIDTH)
  269. COMPONENTS is a vector of integers, the meaning depends on RELATIVE-P.
  270. RELATIVE-P is t if the composition method is relative, else nil.
  271. If RELATIVE-P is t, COMPONENTS is a vector of characters to be
  272. composed. If RELATIVE-P is nil, COMPONENTS is a vector of characters
  273. and composition rules as described in `compose-region'.
  274. MOD-FUNC is a modification function of the composition.
  275. WIDTH is a number of columns the composition occupies on the screen.
  276. When Automatic Composition mode is on, this function also finds a
  277. chunk of text that is automatically composed. If such a chunk is
  278. found closer to POS than the position that has `composition'
  279. property, the value is a list of FROM, TO, and a glyph-string
  280. that specifies how the chunk is to be composed. See the function
  281. `composition-get-gstring' for the format of the glyph-string."
  282. (let ((result (find-composition-internal pos limit string detail-p)))
  283. (if (and detail-p (> (length result) 3) (nth 2 result) (not (nth 3 result)))
  284. ;; This is a valid rule-base composition.
  285. (decode-composition-components (nth 2 result) 'nocopy))
  286. result))
  287. (defun compose-chars-after (pos &optional limit object)
  288. "Compose characters in current buffer after position POS.
  289. It looks up the char-table `composition-function-table' (which
  290. see) by a character at POS, and compose characters after POS
  291. according to the contents of `composition-function-table'.
  292. Optional 2nd arg LIMIT, if non-nil, limits characters to compose.
  293. Optional 3rd arg OBJECT, if non-nil, is a string that contains the
  294. text to compose. In that case, POS and LIMIT index into the string.
  295. This function is the default value of `compose-chars-after-function'."
  296. (let ((tail (aref composition-function-table (char-after pos)))
  297. (font-obj (and (display-multi-font-p)
  298. (and (not (stringp object))
  299. (font-at pos (selected-window)))))
  300. pattern func result)
  301. (or limit
  302. (setq limit (if (stringp object) (length object) (point-max))))
  303. (when (and font-obj tail)
  304. (save-match-data
  305. (save-excursion
  306. (while tail
  307. (if (functionp (car tail))
  308. (setq pattern nil func (car tail))
  309. (setq pattern (car (car tail))
  310. func (cdr (car tail))))
  311. (goto-char pos)
  312. (if pattern
  313. (if (and (if (stringp object)
  314. (eq (string-match pattern object) 0)
  315. (looking-at pattern))
  316. (<= (match-end 0) limit))
  317. (setq result
  318. (funcall func pos (match-end 0) font-obj object)))
  319. (setq result (funcall func pos limit font-obj object)))
  320. (if result (setq tail nil))))))
  321. result))
  322. (defun compose-last-chars (args)
  323. "Compose last characters.
  324. The argument is a parameterized event of the form
  325. \(compose-last-chars N COMPONENTS),
  326. where N is the number of characters before point to compose,
  327. COMPONENTS, if non-nil, is the same as the argument to `compose-region'
  328. \(which see). If it is nil, `compose-chars-after' is called,
  329. and that function finds a proper rule to compose the target characters.
  330. This function is intended to be used from input methods.
  331. The global keymap binds special event `compose-last-chars' to this
  332. function. Input method may generate an event (compose-last-chars N COMPONENTS)
  333. after a sequence of character events."
  334. (interactive "e")
  335. (let ((chars (nth 1 args)))
  336. (if (and (numberp chars)
  337. (>= (- (point) (point-min)) chars))
  338. (if (nth 2 args)
  339. (compose-region (- (point) chars) (point) (nth 2 args))
  340. (compose-chars-after (- (point) chars) (point))))))
  341. (global-set-key [compose-last-chars] 'compose-last-chars)
  342. ;;; Automatic character composition.
  343. ;; These macros must match with C macros LGSTRING_XXX and LGLYPH_XXX in font.h
  344. (defsubst lgstring-header (gstring) (aref gstring 0))
  345. (defsubst lgstring-set-header (gstring header) (aset gstring 0 header))
  346. (defsubst lgstring-font (gstring) (aref (lgstring-header gstring) 0))
  347. (defsubst lgstring-char (gstring i) (aref (lgstring-header gstring) (1+ i)))
  348. (defsubst lgstring-char-len (gstring) (1- (length (lgstring-header gstring))))
  349. (defsubst lgstring-shaped-p (gstring) (aref gstring 1))
  350. (defsubst lgstring-set-id (gstring id) (aset gstring 1 id))
  351. (defsubst lgstring-glyph (gstring i) (aref gstring (+ i 2)))
  352. (defsubst lgstring-glyph-len (gstring) (- (length gstring) 2))
  353. (defsubst lgstring-set-glyph (gstring i glyph) (aset gstring (+ i 2) glyph))
  354. (defsubst lglyph-from (glyph) (aref glyph 0))
  355. (defsubst lglyph-to (glyph) (aref glyph 1))
  356. (defsubst lglyph-char (glyph) (aref glyph 2))
  357. (defsubst lglyph-code (glyph) (aref glyph 3))
  358. (defsubst lglyph-width (glyph) (aref glyph 4))
  359. (defsubst lglyph-lbearing (glyph) (aref glyph 5))
  360. (defsubst lglyph-rbearing (glyph) (aref glyph 6))
  361. (defsubst lglyph-ascent (glyph) (aref glyph 7))
  362. (defsubst lglyph-descent (glyph) (aref glyph 8))
  363. (defsubst lglyph-adjustment (glyph) (aref glyph 9))
  364. (defsubst lglyph-set-from-to (glyph from to)
  365. (progn (aset glyph 0 from) (aset glyph 1 to)))
  366. (defsubst lglyph-set-char (glyph char) (aset glyph 2 char))
  367. (defsubst lglyph-set-code (glyph code) (aset glyph 3 code))
  368. (defsubst lglyph-set-width (glyph width) (aset glyph 4 width))
  369. (defsubst lglyph-set-adjustment (glyph &optional xoff yoff wadjust)
  370. (aset glyph 9 (vector (or xoff 0) (or yoff 0) (or wadjust 0))))
  371. (defsubst lglyph-copy (glyph) (copy-sequence glyph))
  372. (defun lgstring-insert-glyph (gstring idx glyph)
  373. (let ((nglyphs (lgstring-glyph-len gstring))
  374. (i idx))
  375. (while (and (< i nglyphs) (lgstring-glyph gstring i))
  376. (setq i (1+ i)))
  377. (if (= i nglyphs)
  378. (setq gstring (vconcat gstring (vector glyph)))
  379. (if (< (1+ i) nglyphs)
  380. (lgstring-set-glyph gstring (1+ i) nil)))
  381. (while (> i idx)
  382. (lgstring-set-glyph gstring i (lgstring-glyph gstring (1- i)))
  383. (setq i (1- i)))
  384. (lgstring-set-glyph gstring i glyph)
  385. gstring))
  386. (defun compose-glyph-string (gstring from to)
  387. (let ((glyph (lgstring-glyph gstring from))
  388. from-pos to-pos)
  389. (setq from-pos (lglyph-from glyph)
  390. to-pos (lglyph-to (lgstring-glyph gstring (1- to))))
  391. (lglyph-set-from-to glyph from-pos to-pos)
  392. (setq from (1+ from))
  393. (while (and (< from to)
  394. (setq glyph (lgstring-glyph gstring from)))
  395. (lglyph-set-from-to glyph from-pos to-pos)
  396. (let ((xoff (if (<= (lglyph-rbearing glyph) 0) 0
  397. (- (lglyph-width glyph)))))
  398. (lglyph-set-adjustment glyph xoff 0 0))
  399. (setq from (1+ from)))
  400. gstring))
  401. (defun compose-glyph-string-relative (gstring from to &optional gap)
  402. (let ((font-object (lgstring-font gstring))
  403. (glyph (lgstring-glyph gstring from))
  404. from-pos to-pos
  405. ascent descent)
  406. (if gap
  407. (setq gap (floor (* (font-get font-object :size) gap)))
  408. (setq gap 0))
  409. (setq from-pos (lglyph-from glyph)
  410. to-pos (lglyph-to (lgstring-glyph gstring (1- to)))
  411. ascent (lglyph-ascent glyph)
  412. descent (lglyph-descent glyph))
  413. (lglyph-set-from-to glyph from-pos to-pos)
  414. (setq from (1+ from))
  415. (while (< from to)
  416. (setq glyph (lgstring-glyph gstring from))
  417. (lglyph-set-from-to glyph from-pos to-pos)
  418. (let ((this-ascent (lglyph-ascent glyph))
  419. (this-descent (lglyph-descent glyph))
  420. xoff yoff)
  421. (setq xoff (if (<= (lglyph-rbearing glyph) 0) 0
  422. (- (lglyph-width glyph))))
  423. (if (> this-ascent 0)
  424. (if (< this-descent 0)
  425. (setq yoff (- 0 ascent gap this-descent)
  426. ascent (+ ascent gap this-ascent this-descent))
  427. (setq yoff 0))
  428. (setq yoff (+ descent gap this-ascent)
  429. descent (+ descent gap this-ascent this-descent)))
  430. (if (or (/= xoff 0) (/= yoff 0))
  431. (lglyph-set-adjustment glyph xoff yoff 0)))
  432. (setq from (1+ from)))
  433. gstring))
  434. (defun compose-gstring-for-graphic (gstring)
  435. "Compose glyph-string GSTRING for graphic display.
  436. Combining characters are composed with the preceding base
  437. character. If the preceding character is not a base character,
  438. each combining character is composed as a spacing character by
  439. a padding space before and/or after the character.
  440. All non-spacing characters have this function in
  441. `composition-function-table' unless overwritten."
  442. (let ((nchars (lgstring-char-len gstring))
  443. (nglyphs (lgstring-glyph-len gstring))
  444. (glyph (lgstring-glyph gstring 0)))
  445. (cond
  446. ;; A non-spacing character not following a proper base character.
  447. ((= nchars 1)
  448. (let ((lbearing (lglyph-lbearing glyph))
  449. (rbearing (lglyph-rbearing glyph))
  450. (width (lglyph-width glyph))
  451. xoff)
  452. (if (< lbearing 0)
  453. (setq xoff (- lbearing))
  454. (setq xoff 0 lbearing 0))
  455. (if (< rbearing width)
  456. (setq rbearing width))
  457. (lglyph-set-adjustment glyph xoff 0 (- rbearing lbearing))
  458. gstring))
  459. ;; This sequence doesn't start with a proper base character.
  460. ((memq (get-char-code-property (lgstring-char gstring 0)
  461. 'general-category)
  462. '(Mn Mc Me Zs Zl Zp Cc Cf Cs))
  463. nil)
  464. ;; A base character and the following non-spacing characters.
  465. (t
  466. (let ((gstr (font-shape-gstring gstring)))
  467. (if (and gstr
  468. (> (lglyph-to (lgstring-glyph gstr 0)) 0))
  469. gstr
  470. ;; The shaper of the font couldn't shape the gstring.
  471. ;; Shape them according to canonical-combining-class.
  472. (lgstring-set-id gstring nil)
  473. (let* ((width (lglyph-width glyph))
  474. (ascent (lglyph-ascent glyph))
  475. (descent (lglyph-descent glyph))
  476. (rbearing (lglyph-rbearing glyph))
  477. (lbearing (lglyph-lbearing glyph))
  478. (center (/ (+ lbearing rbearing) 2))
  479. (gap (round (* (font-get (lgstring-font gstring) :size) 0.1))))
  480. (dotimes (i nchars)
  481. (setq glyph (lgstring-glyph gstring i))
  482. (when (> i 0)
  483. (let* ((class (get-char-code-property
  484. (lglyph-char glyph) 'canonical-combining-class))
  485. (lb (lglyph-lbearing glyph))
  486. (rb (lglyph-rbearing glyph))
  487. (as (lglyph-ascent glyph))
  488. (de (lglyph-descent glyph))
  489. (ce (/ (+ lb rb) 2))
  490. xoff yoff)
  491. (when (and class (>= class 200) (<= class 240))
  492. (setq xoff 0 yoff 0)
  493. (cond
  494. ((= class 200)
  495. (setq xoff (- lbearing ce)
  496. yoff (if (> as 0) 0 (+ descent as))))
  497. ((= class 202)
  498. (if (> as 0) (setq as 0))
  499. (setq xoff (- center ce)
  500. yoff (if (> as 0) 0 (+ descent as))))
  501. ((= class 204)
  502. (if (> as 0) (setq as 0))
  503. (setq xoff (- rbearing ce)
  504. yoff (if (> as 0) 0 (+ descent as))))
  505. ((= class 208)
  506. (setq xoff (- lbearing rb)))
  507. ((= class 210)
  508. (setq xoff (- rbearing lb)))
  509. ((= class 212)
  510. (setq xoff (- lbearing ce)
  511. yoff (if (>= de 0) 0 (- (- ascent) de))))
  512. ((= class 214)
  513. (setq xoff (- center ce)
  514. yoff (if (>= de 0) 0 (- (- ascent) de))))
  515. ((= class 216)
  516. (setq xoff (- rbearing ce)
  517. yoff (if (>= de 0) 0 (- (- ascent) de))))
  518. ((= class 218)
  519. (setq xoff (- lbearing ce)
  520. yoff (if (> as 0) 0 (+ descent as gap))))
  521. ((= class 220)
  522. (setq xoff (- center ce)
  523. yoff (if (> as 0) 0 (+ descent as gap))))
  524. ((= class 222)
  525. (setq xoff (- rbearing ce)
  526. yoff (if (> as 0) 0 (+ descent as gap))))
  527. ((= class 224)
  528. (setq xoff (- lbearing rb)))
  529. ((= class 226)
  530. (setq xoff (- rbearing lb)))
  531. ((= class 228)
  532. (setq xoff (- lbearing ce)
  533. yoff (if (>= de 0) 0 (- (- ascent) de gap))))
  534. ((= class 230)
  535. (setq xoff (- center ce)
  536. yoff (if (>= de 0) 0 (- (- ascent) de gap))))
  537. ((= class 232)
  538. (setq xoff (- rbearing ce)
  539. yoff (if (>= de 0) 0 (- (+ ascent de) gap)))))
  540. (lglyph-set-adjustment glyph (- xoff width) yoff)
  541. (setq lb (+ lb xoff)
  542. rb (+ lb xoff)
  543. as (- as yoff)
  544. de (+ de yoff)))
  545. (if (< ascent as)
  546. (setq ascent as))
  547. (if (< descent de)
  548. (setq descent de))))))
  549. (let ((i 0))
  550. (while (and (< i nglyphs) (setq glyph (lgstring-glyph gstring i)))
  551. (lglyph-set-from-to glyph 0 (1- nchars))
  552. (setq i (1+ i))))
  553. gstring))))))
  554. (let ((elt `([,(purecopy "\\c.\\c^+") 1 compose-gstring-for-graphic]
  555. [nil 0 compose-gstring-for-graphic])))
  556. (map-char-table
  557. #'(lambda (key val)
  558. (if (memq val '(Mn Mc Me))
  559. (set-char-table-range composition-function-table key elt)))
  560. unicode-category-table))
  561. (defun compose-gstring-for-terminal (gstring)
  562. "Compose glyph string GSTRING for terminal display.
  563. Non-spacing characters are composed with the preceding base
  564. character. If the preceding character is not a base character,
  565. each non-spacing character is composed as a spacing character by
  566. prepending a space before it."
  567. (let ((nglyphs (lgstring-glyph-len gstring))
  568. (i 0)
  569. (coding (lgstring-font gstring))
  570. glyph)
  571. (while (and (< i nglyphs)
  572. (setq glyph (lgstring-glyph gstring i)))
  573. (if (not (char-charset (lglyph-char glyph) coding))
  574. (progn
  575. ;; As the terminal doesn't support this glyph, return a
  576. ;; gstring in which each glyph is its own grapheme-cluster
  577. ;; of width 1..
  578. (setq i 0)
  579. (while (and (< i nglyphs)
  580. (setq glyph (lgstring-glyph gstring i)))
  581. (if (< (lglyph-width glyph) 1)
  582. (lglyph-set-width glyph 1))
  583. (lglyph-set-from-to glyph i i)
  584. (setq i (1+ i))))
  585. (if (= (lglyph-width glyph) 0)
  586. (if (eq (get-char-code-property (lglyph-char glyph)
  587. 'general-category)
  588. 'Cf)
  589. (progn
  590. ;; Compose by replacing with a space.
  591. (lglyph-set-char glyph 32)
  592. (lglyph-set-width glyph 1)
  593. (setq i (1+ i)))
  594. ;; Compose by prepending a space.
  595. (setq gstring (lgstring-insert-glyph gstring i
  596. (lglyph-copy glyph))
  597. nglyphs (lgstring-glyph-len gstring))
  598. (setq glyph (lgstring-glyph gstring i))
  599. (lglyph-set-char glyph 32)
  600. (lglyph-set-width glyph 1)
  601. (setq i (+ 2)))
  602. (let ((from (lglyph-from glyph))
  603. (to (lglyph-to glyph))
  604. (j (1+ i)))
  605. (while (and (< j nglyphs)
  606. (setq glyph (lgstring-glyph gstring j))
  607. (char-charset (lglyph-char glyph) coding)
  608. (= (lglyph-width glyph) 0))
  609. (setq to (lglyph-to glyph)
  610. j (1+ j)))
  611. (while (< i j)
  612. (setq glyph (lgstring-glyph gstring i))
  613. (lglyph-set-from-to glyph from to)
  614. (setq i (1+ i)))))))
  615. gstring))
  616. (defun auto-compose-chars (func from to font-object string)
  617. "Compose the characters at FROM by FUNC.
  618. FUNC is called with one argument GSTRING which is built for characters
  619. in the region FROM (inclusive) and TO (exclusive).
  620. If the character are composed on a graphic display, FONT-OBJECT
  621. is a font to use. Otherwise, FONT-OBJECT is nil, and the function
  622. `compose-gstring-for-terminal' is used instead of FUNC.
  623. If STRING is non-nil, it is a string, and FROM and TO are indices
  624. into the string. In that case, compose characters in the string.
  625. The value is a gstring containing information for shaping the characters.
  626. This function is the default value of `auto-composition-function' (which see)."
  627. (let ((gstring (composition-get-gstring from to font-object string)))
  628. (if (lgstring-shaped-p gstring)
  629. gstring
  630. (or (fontp font-object 'font-object)
  631. (setq func 'compose-gstring-for-terminal))
  632. (funcall func gstring))))
  633. (put 'auto-composition-mode 'permanent-local t)
  634. (make-variable-buffer-local 'auto-composition-function)
  635. (setq-default auto-composition-function 'auto-compose-chars)
  636. ;;;###autoload
  637. (define-minor-mode auto-composition-mode
  638. "Toggle Auto Composition mode.
  639. With a prefix argument ARG, enable Auto Composition mode if ARG
  640. is positive, and disable it otherwise. If called from Lisp,
  641. enable the mode if ARG is omitted or nil.
  642. When Auto Composition mode is enabled, text characters are
  643. automatically composed by functions registered in
  644. `composition-function-table'.
  645. You can use `global-auto-composition-mode' to turn on
  646. Auto Composition mode in all buffers (this is the default)."
  647. ;; It's defined in C, this stops the d-m-m macro defining it again.
  648. :variable auto-composition-mode)
  649. ;; It's not defined with DEFVAR_PER_BUFFER though.
  650. (make-variable-buffer-local 'auto-composition-mode)
  651. ;;;###autoload
  652. (define-minor-mode global-auto-composition-mode
  653. "Toggle Auto Composition mode in all buffers.
  654. With a prefix argument ARG, enable it if ARG is positive, and
  655. disable it otherwise. If called from Lisp, enable it if ARG is
  656. omitted or nil.
  657. For more information on Auto Composition mode, see
  658. `auto-composition-mode' ."
  659. :variable (default-value 'auto-composition-mode))
  660. (defalias 'toggle-auto-composition 'auto-composition-mode)
  661. ;;; composite.el ends here