Auto-commit of generated files.
[emacs.git] / lisp / composite.el
blob3c25b8b60af16b45edeaadad537ab7bc83d2f03b
1 ;;; composite.el --- support character composition
3 ;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
4 ;; 2008, 2009, 2010, 2011
5 ;; National Institute of Advanced Industrial Science and Technology (AIST)
6 ;; Registration Number H14PRO021
8 ;; Author: Kenichi HANDA <handa@etl.go.jp>
9 ;; (according to ack.texi)
10 ;; Keywords: mule, multilingual, character composition
11 ;; Package: emacs
13 ;; This file is part of GNU Emacs.
15 ;; GNU Emacs is free software: you can redistribute it and/or modify
16 ;; it under the terms of the GNU General Public License as published by
17 ;; the Free Software Foundation, either version 3 of the License, or
18 ;; (at your option) any later version.
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.
25 ;; You should have received a copy of the GNU General Public License
26 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
28 ;;; Commentary:
30 ;;; Code:
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.
45 A glyph reference point symbol is to be used to specify a composition
46 rule in COMPONENTS argument to such functions as `compose-region'.
48 The meaning of glyph reference point codes is as follows:
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
60 Glyph reference point symbols are to be used to specify a composition
61 rule of the form (GLOBAL-REF-POINT . NEW-REF-POINT), where
62 GLOBAL-REF-POINT is a reference point in the overall glyphs already
63 composed, and NEW-REF-POINT is a reference point in the new glyph to
64 be added.
66 For instance, if GLOBAL-REF-POINT is `br' (bottom-right) and
67 NEW-REF-POINT is `tc' (top-center), the overall glyph is updated as
68 follows (the point `*' corresponds to both reference points):
70 +-------+--+ <--- new ascent
71 | | |
72 | global| |
73 | glyph | |
74 -- | | |-- <--- baseline (doesn't change)
75 +----+--*--+
76 | | new |
77 | |glyph|
78 +----+-----+ <--- new descent
80 A composition rule may have the form (GLOBAL-REF-POINT
81 NEW-REF-POINT XOFF YOFF), where XOFF and YOFF specify how much
82 to shift NEW-REF-POINT from GLOBAL-REF-POINT. In this case, XOFF
83 and YOFF are integers in the range -100..100 representing the
84 shifting percentage against the font size.")
87 ;;;###autoload
88 (defun encode-composition-rule (rule)
89 "Encode composition rule RULE into an integer value.
90 RULE is a cons of global and new reference point symbols
91 \(see `reference-point-alist')."
93 ;; This must be compatible with C macro COMPOSITION_ENCODE_RULE
94 ;; defined in composite.h.
96 (if (and (integerp rule) (< rule 144))
97 ;; Already encoded.
98 rule
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))
110 (error "Invalid composition rule: %s" rule))
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))))
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.
128 (defun decode-composition-rule (rule-code)
129 (or (and (natnump rule-code) (< rule-code #x1000000))
130 (error "Invalid encoded composition rule: %S" rule-code))
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)))
137 (or (and gref (symbolp gref) nref (symbolp nref))
138 (error "Invalid composition rule code: %S" rule-code))
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))))
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.
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)
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.
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)
186 (defun compose-region (start end &optional components modification-func)
187 "Compose characters in the current region.
189 Characters are composed relatively, i.e. composed by overstriking
190 or stacking depending on ascent, descent and other metrics of
191 glyphs.
193 For instance, if the region has three characters \"XYZ\", X is
194 regarded 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
198 and Z is displayed with the same rule while regarding the whole
199 XY glyphs as BASE.
201 When called from a program, expects these four arguments.
203 First two arguments START and END are positions (integers or markers)
204 specifying the region.
206 Optional 3rd argument COMPONENTS, if non-nil, is a character, a string
207 or a vector or list of integers and rules.
209 If it is a character, it is an alternate character to display instead
210 of the text in the region.
212 If it is a string, the elements are alternate characters. In
213 this case, TAB element has a special meaning. If the first
214 character is TAB, the glyphs are displayed with left padding space
215 so that no pixel overlaps with the previous column. If the last
216 character is TAB, the glyphs are displayed with right padding
217 space so that no pixel overlaps with the following column.
219 If it is a vector or list, it is a sequence of alternate characters and
220 composition rules, where (2N)th elements are characters and (2N+1)th
221 elements are composition rules to specify how to compose (2N+2)th
222 elements with previously composed N glyphs.
224 A composition rule is a cons of global and new glyph reference point
225 symbols. See the documentation of `reference-point-alist' for more
226 details.
228 Optional 4th argument MODIFICATION-FUNC is a function to call to
229 adjust the composition when it gets invalid because of a change of
230 text in the composition."
231 (interactive "r")
232 (let ((modified-p (buffer-modified-p))
233 (inhibit-read-only t))
234 (if (or (vectorp components) (listp components))
235 (setq components (encode-composition-components components)))
236 (compose-region-internal start end components modification-func)
237 (restore-buffer-modified-p modified-p)))
239 (defun decompose-region (start end)
240 "Decompose text in the current region.
242 When called from a program, expects two arguments,
243 positions (integers or markers) specifying the region."
244 (interactive "r")
245 (let ((modified-p (buffer-modified-p))
246 (inhibit-read-only t))
247 (remove-text-properties start end '(composition nil))
248 (restore-buffer-modified-p modified-p)))
250 (defun compose-string (string &optional start end components modification-func)
251 "Compose characters in string STRING.
253 The return value is STRING with the `composition' property put on all
254 the characters in it.
256 Optional 2nd and 3rd arguments START and END specify the range of
257 STRING to be composed. They default to the beginning and the end of
258 STRING respectively.
260 Optional 4th argument COMPONENTS, if non-nil, is a character or a
261 sequence (vector, list, or string) of integers. See the function
262 `compose-region' for more detail.
264 Optional 5th argument MODIFICATION-FUNC is a function to call to
265 adjust the composition when it gets invalid because of a change of
266 text in the composition."
267 (if (or (vectorp components) (listp components))
268 (setq components (encode-composition-components components)))
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)
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)
279 (defun compose-chars (&rest args)
280 "Return a string from arguments in which all characters are composed.
281 For relative composition, arguments are characters.
282 For rule-based composition, Mth (where M is odd) arguments are
283 characters, and Nth (where N is even) arguments are composition rules.
284 A 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.
290 (let ((tail (encode-composition-components args 'nocopy)))
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)))
300 (defun find-composition (pos &optional limit string detail-p)
301 "Return information about a composition at or near buffer position POS.
303 If the character at POS has `composition' property, the value is a list
304 \(FROM TO VALID-P).
306 FROM and TO specify the range of text that has the same `composition'
307 property, VALID-P is t if this composition is valid, and nil if not.
309 If there's no composition at POS, and the optional 2nd argument LIMIT
310 is non-nil, search for a composition toward the position given by LIMIT.
312 If no composition is found, return nil.
314 Optional 3rd argument STRING, if non-nil, is a string to look for a
315 composition in; nil means the current buffer.
317 If a valid composition is found and the optional 4th argument DETAIL-P
318 is non-nil, the return value is a list of the form
320 (FROM TO COMPONENTS RELATIVE-P MOD-FUNC WIDTH)
322 COMPONENTS is a vector of integers, the meaning depends on RELATIVE-P.
324 RELATIVE-P is t if the composition method is relative, else nil.
326 If RELATIVE-P is t, COMPONENTS is a vector of characters to be
327 composed. If RELATIVE-P is nil, COMPONENTS is a vector of characters
328 and composition rules as described in `compose-region'.
330 MOD-FUNC is a modification function of the composition.
332 WIDTH is a number of columns the composition occupies on the screen.
334 When Automatic Composition mode is on, this function also finds a
335 chunk of text that is automatically composed. If such a chunk is
336 found closer to POS than the position that has `composition'
337 property, the value is a list of FROM, TO, and a glyph-string
338 that specifies how the chunk is to be composed. See the function
339 `composition-get-gstring' for the format of the glyph-string."
340 (let ((result (find-composition-internal pos limit string detail-p)))
341 (if (and detail-p (> (length result) 3) (nth 2 result) (not (nth 3 result)))
342 ;; This is a valid rule-base composition.
343 (decode-composition-components (nth 2 result) 'nocopy))
344 result))
347 (defun compose-chars-after (pos &optional limit object)
348 "Compose characters in current buffer after position POS.
350 It looks up the char-table `composition-function-table' (which
351 see) by a character at POS, and compose characters after POS
352 according to the contents of `composition-function-table'.
354 Optional 2nd arg LIMIT, if non-nil, limits characters to compose.
356 Optional 3rd arg OBJECT, if non-nil, is a string that contains the
357 text to compose. In that case, POS and LIMIT index into the string.
359 This function is the default value of `compose-chars-after-function'."
360 (let ((tail (aref composition-function-table (char-after pos)))
361 (font-obj (and (display-multi-font-p)
362 (and (not (stringp object))
363 (font-at pos (selected-window)))))
364 pattern func result)
365 (or limit
366 (setq limit (if (stringp object) (length object) (point-max))))
367 (when (and font-obj tail)
368 (save-match-data
369 (save-excursion
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))))
375 (goto-char pos)
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))
387 (defun compose-last-chars (args)
388 "Compose last characters.
389 The argument is a parameterized event of the form
390 (compose-last-chars N COMPONENTS),
391 where N is the number of characters before point to compose,
392 COMPONENTS, if non-nil, is the same as the argument to `compose-region'
393 \(which see). If it is nil, `compose-chars-after' is called,
394 and that function finds a proper rule to compose the target characters.
395 This function is intended to be used from input methods.
396 The global keymap binds special event `compose-last-chars' to this
397 function. Input method may generate an event (compose-last-chars N COMPONENTS)
398 after a sequence of character events."
399 (interactive "e")
400 (let ((chars (nth 1 args)))
401 (if (and (numberp chars)
402 (>= (- (point) (point-min)) chars))
403 (if (nth 2 args)
404 (compose-region (- (point) chars) (point) (nth 2 args))
405 (compose-chars-after (- (point) chars) (point))))))
407 (global-set-key [compose-last-chars] 'compose-last-chars)
410 ;;; Automatic character composition.
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))
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))
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))
438 (defsubst lglyph-set-code (glyph code) (aset glyph 3 code))
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))))
443 (defsubst lglyph-copy (glyph) (copy-sequence glyph))
445 (defun lgstring-insert-glyph (gstring idx glyph)
446 (let ((nglyphs (lgstring-glyph-len gstring))
447 (i idx))
448 (while (and (< i nglyphs) (lgstring-glyph gstring i))
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))
460 (defun compose-glyph-string (gstring from to)
461 (let ((glyph (lgstring-glyph gstring from))
462 from-pos to-pos)
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))
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
480 ascent descent)
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))
495 xoff yoff)
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))
510 (defun compose-gstring-for-graphic (gstring)
511 "Compose glyph-string GSTRING for graphic display.
512 Combining characters are composed with the preceding base
513 character. If the preceding character is not a base character,
514 each combining character is composed as a spacing character by
515 a padding space before and/or after the character.
517 All non-spacing characters have this function in
518 `composition-function-table' unless overwritten."
519 (let ((nchars (lgstring-char-len gstring))
520 (nglyphs (lgstring-glyph-len gstring))
521 (glyph (lgstring-glyph gstring 0)))
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))
528 xoff)
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))
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)
543 ;; A base character and the following non-spacing characters.
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))
558 (gap (round (* (font-get (lgstring-font gstring) :size) 0.1))))
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)
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))))))
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))))))
634 (let ((elt `([,(purecopy "\\c.\\c^+") 1 compose-gstring-for-graphic]
635 [nil 0 compose-gstring-for-graphic])))
636 (map-char-table
637 #'(lambda (key val)
638 (if (memq val '(Mn Mc Me))
639 (set-char-table-range composition-function-table key elt)))
640 unicode-category-table))
642 (defun compose-gstring-for-terminal (gstring)
643 "Compose glyph-string GSTRING for terminal display.
644 Non-spacing characters are composed with the preceding base
645 character. If the preceding character is not a base character,
646 each non-spacing character is composed as a spacing character by
647 prepending a space before it."
648 (let ((nglyphs (lgstring-glyph-len gstring))
649 (i 0)
650 (coding (lgstring-font gstring))
651 glyph)
652 (while (and (< i nglyphs)
653 (setq glyph (lgstring-glyph gstring i)))
654 (if (not (char-charset (lglyph-char glyph) coding))
655 (progn
656 ;; As the terminal doesn't support this glyph, return a
657 ;; gstring in which each glyph is its own grapheme-cluster
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)
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)))
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)))))))
696 gstring))
699 (defun auto-compose-chars (func from to font-object string)
700 "Compose the characters at FROM by FUNC.
701 FUNC is called with one argument GSTRING which is built for characters
702 in the region FROM (inclusive) and TO (exclusive).
704 If the character are composed on a graphic display, FONT-OBJECT
705 is a font to use. Otherwise, FONT-OBJECT is nil, and the function
706 `compose-gstring-for-terminal' is used instead of FUNC.
708 If STRING is non-nil, it is a string, and FROM and TO are indices
709 into the string. In that case, compose characters in the string.
711 The value is a gstring containing information for shaping the characters.
713 This function is the default value of `auto-composition-function' (which see)."
714 (let ((gstring (composition-get-gstring from to font-object string)))
715 (if (lgstring-shaped-p gstring)
716 gstring
717 (or (fontp font-object 'font-object)
718 (setq func 'compose-gstring-for-terminal))
719 (funcall func gstring))))
721 (put 'auto-composition-mode 'permanent-local t)
723 (make-variable-buffer-local 'auto-composition-function)
724 (setq-default auto-composition-function 'auto-compose-chars)
726 ;;;###autoload
727 (define-minor-mode auto-composition-mode
728 "Toggle Auto Composition mode.
729 With a prefix argument ARG, enable Auto Composition mode if ARG
730 is positive, and disable it otherwise. If called from Lisp,
731 enable the mode if ARG is omitted or nil.
733 When Auto Composition mode is enabled, text characters are
734 automatically composed by functions registered in
735 `composition-function-table'.
737 You can use `global-auto-composition-mode' to turn on
738 Auto 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)
744 ;;;###autoload
745 (define-minor-mode global-auto-composition-mode
746 "Toggle Auto Composition mode in all buffers.
747 With a prefix argument ARG, enable it if ARG is positive, and
748 disable it otherwise. If called from Lisp, enable it if ARG is
749 omitted or nil.
751 For more information on Auto Composition mode, see
752 `auto-composition-mode' ."
753 :variable (default-value 'auto-composition-mode))
755 (defalias 'toggle-auto-composition 'auto-composition-mode)
759 ;;; composite.el ends here