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