Fixing user-spec for fonts instead of using name.
[emacs.git] / src / composite.c
bloba24abd0fe8fd46a61a3fdfcd40aedb95b478a1bf
1 /* Composite sequence support.
2 Copyright (C) 2001, 2002, 2003, 2004, 2005,
3 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
4 Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
5 National Institute of Advanced Industrial Science and Technology (AIST)
6 Registration Number H14PRO021
7 Copyright (C) 2003, 2006
8 National Institute of Advanced Industrial Science and Technology (AIST)
9 Registration Number H13PRO009
11 This file is part of GNU Emacs.
13 GNU Emacs is free software: you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation, either version 3 of the License, or
16 (at your option) any later version.
18 GNU Emacs is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
26 #include <config.h>
27 #include <setjmp.h>
28 #include "lisp.h"
29 #include "buffer.h"
30 #include "character.h"
31 #include "coding.h"
32 #include "intervals.h"
33 #include "window.h"
34 #include "frame.h"
35 #include "dispextern.h"
36 #include "font.h"
37 #include "termhooks.h"
40 /* Emacs uses special text property `composition' to support character
41 composition. A sequence of characters that have the same (i.e. eq)
42 `composition' property value is treated as a single composite
43 sequence (we call it just `composition' here after). Characters in
44 a composition are all composed somehow on the screen.
46 The property value has this form when the composition is made:
47 ((LENGTH . COMPONENTS) . MODIFICATION-FUNC)
48 then turns to this form:
49 (COMPOSITION-ID . (LENGTH COMPONENTS-VEC . MODIFICATION-FUNC))
50 when the composition is registered in composition_hash_table and
51 composition_table. These rather peculiar structures were designed
52 to make it easy to distinguish them quickly (we can do that by
53 checking only the first element) and to extract LENGTH (from the
54 former form) and COMPOSITION-ID (from the latter form).
56 We register a composition when it is displayed, or when the width
57 is required (for instance, to calculate columns).
59 LENGTH -- Length of the composition. This information is used to
60 check the validity of the composition.
62 COMPONENTS -- Character, string, vector, list, or nil.
64 If it is nil, characters in the text are composed relatively
65 according to their metrics in font glyphs.
67 If it is a character or a string, the character or characters
68 in the string are composed relatively.
70 If it is a vector or list of integers, the element is a
71 character or an encoded composition rule. The characters are
72 composed according to the rules. (2N)th elements are
73 characters to be composed and (2N+1)th elements are
74 composition rules to tell how to compose (2N+2)th element with
75 the previously composed 2N glyphs.
77 COMPONENTS-VEC -- Vector of integers. In relative composition, the
78 elements are characters to be composed. In rule-base
79 composition, the elements are characters or encoded
80 composition rules.
82 MODIFICATION-FUNC -- If non nil, it is a function to call when the
83 composition gets invalid after a modification in a buffer. If
84 it is nil, a function in `composition-function-table' of the
85 first character in the sequence is called.
87 COMPOSITION-ID --Identification number of the composition. It is
88 used as an index to composition_table for the composition.
90 When Emacs has to display a composition or has to know its
91 displaying width, the function get_composition_id is called. It
92 returns COMPOSITION-ID so that the caller can access the
93 information about the composition through composition_table. If a
94 COMPOSITION-ID has not yet been assigned to the composition,
95 get_composition_id checks the validity of `composition' property,
96 and, if valid, assigns a new ID, registers the information in
97 composition_hash_table and composition_table, and changes the form
98 of the property value. If the property is invalid, return -1
99 without changing the property value.
101 We use two tables to keep information about composition;
102 composition_hash_table and composition_table.
104 The former is a hash table in which keys are COMPONENTS-VECs and
105 values are the corresponding COMPOSITION-IDs. This hash table is
106 weak, but as each key (COMPONENTS-VEC) is also kept as a value of the
107 `composition' property, it won't be collected as garbage until all
108 bits of text that have the same COMPONENTS-VEC are deleted.
110 The latter is a table of pointers to `struct composition' indexed
111 by COMPOSITION-ID. This structure keeps the other information (see
112 composite.h).
114 In general, a text property holds information about individual
115 characters. But, a `composition' property holds information about
116 a sequence of characters (in this sense, it is like the `intangible'
117 property). That means that we should not share the property value
118 in adjacent compositions -- we can't distinguish them if they have the
119 same property. So, after any changes, we call
120 `update_compositions' and change a property of one of adjacent
121 compositions to a copy of it. This function also runs a proper
122 composition modification function to make a composition that gets
123 invalid by the change valid again.
125 As the value of the `composition' property holds information about a
126 specific range of text, the value gets invalid if we change the
127 text in the range. We treat the `composition' property as always
128 rear-nonsticky (currently by setting default-text-properties to
129 (rear-nonsticky (composition))) and we never make properties of
130 adjacent compositions identical. Thus, any such changes make the
131 range just shorter. So, we can check the validity of the `composition'
132 property by comparing LENGTH information with the actual length of
133 the composition.
138 Lisp_Object Qcomposition;
140 /* Table of pointers to the structure `composition' indexed by
141 COMPOSITION-ID. This structure is for storing information about
142 each composition except for COMPONENTS-VEC. */
143 struct composition **composition_table;
145 /* The current size of `composition_table'. */
146 static int composition_table_size;
148 /* Number of compositions currently made. */
149 int n_compositions;
151 /* Hash table for compositions. The key is COMPONENTS-VEC of
152 `composition' property. The value is the corresponding
153 COMPOSITION-ID. */
154 Lisp_Object composition_hash_table;
156 /* Function to call to adjust composition. */
157 Lisp_Object Vcompose_chars_after_function;
159 Lisp_Object Qauto_composed;
160 Lisp_Object Vauto_composition_function;
161 Lisp_Object Qauto_composition_function;
162 Lisp_Object Vcomposition_function_table;
164 /* Maxinum number of characters to lookback to check
165 auto-composition. */
166 #define MAX_AUTO_COMPOSITION_LOOKBACK 3
168 EXFUN (Fremove_list_of_text_properties, 4);
170 /* Temporary variable used in macros COMPOSITION_XXX. */
171 Lisp_Object composition_temp;
174 /* Return COMPOSITION-ID of a composition at buffer position
175 CHARPOS/BYTEPOS and length NCHARS. The `composition' property of
176 the sequence is PROP. STRING, if non-nil, is a string that
177 contains the composition instead of the current buffer.
179 If the composition is invalid, return -1. */
182 get_composition_id (charpos, bytepos, nchars, prop, string)
183 int charpos, bytepos, nchars;
184 Lisp_Object prop, string;
186 Lisp_Object id, length, components, key, *key_contents;
187 int glyph_len;
188 struct Lisp_Hash_Table *hash_table = XHASH_TABLE (composition_hash_table);
189 int hash_index;
190 unsigned hash_code;
191 struct composition *cmp;
192 int i, ch;
194 /* PROP should be
195 Form-A: ((LENGTH . COMPONENTS) . MODIFICATION-FUNC)
197 Form-B: (COMPOSITION-ID . (LENGTH COMPONENTS-VEC . MODIFICATION-FUNC))
199 if (nchars == 0 || !CONSP (prop))
200 goto invalid_composition;
202 id = XCAR (prop);
203 if (INTEGERP (id))
205 /* PROP should be Form-B. */
206 if (XINT (id) < 0 || XINT (id) >= n_compositions)
207 goto invalid_composition;
208 return XINT (id);
211 /* PROP should be Form-A.
212 Thus, ID should be (LENGTH . COMPONENTS). */
213 if (!CONSP (id))
214 goto invalid_composition;
215 length = XCAR (id);
216 if (!INTEGERP (length) || XINT (length) != nchars)
217 goto invalid_composition;
219 components = XCDR (id);
221 /* Check if the same composition has already been registered or not
222 by consulting composition_hash_table. The key for this table is
223 COMPONENTS (converted to a vector COMPONENTS-VEC) or, if it is
224 nil, vector of characters in the composition range. */
225 if (INTEGERP (components))
226 key = Fmake_vector (make_number (1), components);
227 else if (STRINGP (components) || CONSP (components))
228 key = Fvconcat (1, &components);
229 else if (VECTORP (components))
230 key = components;
231 else if (NILP (components))
233 key = Fmake_vector (make_number (nchars), Qnil);
234 if (STRINGP (string))
235 for (i = 0; i < nchars; i++)
237 FETCH_STRING_CHAR_ADVANCE (ch, string, charpos, bytepos);
238 XVECTOR (key)->contents[i] = make_number (ch);
240 else
241 for (i = 0; i < nchars; i++)
243 FETCH_CHAR_ADVANCE (ch, charpos, bytepos);
244 XVECTOR (key)->contents[i] = make_number (ch);
247 else
248 goto invalid_composition;
250 hash_index = hash_lookup (hash_table, key, &hash_code);
251 if (hash_index >= 0)
253 /* We have already registered the same composition. Change PROP
254 from Form-A above to Form-B while replacing COMPONENTS with
255 COMPONENTS-VEC stored in the hash table. We can directly
256 modify the cons cell of PROP because it is not shared. */
257 key = HASH_KEY (hash_table, hash_index);
258 id = HASH_VALUE (hash_table, hash_index);
259 XSETCAR (prop, id);
260 XSETCDR (prop, Fcons (make_number (nchars), Fcons (key, XCDR (prop))));
261 return XINT (id);
264 /* This composition is a new one. We must register it. */
266 /* Check if we have sufficient memory to store this information. */
267 if (composition_table_size == 0)
269 composition_table_size = 256;
270 composition_table
271 = (struct composition **) xmalloc (sizeof (composition_table[0])
272 * composition_table_size);
274 else if (composition_table_size <= n_compositions)
276 composition_table_size += 256;
277 composition_table
278 = (struct composition **) xrealloc (composition_table,
279 sizeof (composition_table[0])
280 * composition_table_size);
283 key_contents = XVECTOR (key)->contents;
285 /* Check if the contents of COMPONENTS are valid if COMPONENTS is a
286 vector or a list. It should be a sequence of:
287 char1 rule1 char2 rule2 char3 ... ruleN charN+1 */
289 if (VECTORP (components)
290 && ASIZE (components) >= 2
291 && VECTORP (AREF (components, 0)))
293 /* COMPONENTS is a glyph-string. */
294 int len = ASIZE (key);
296 for (i = 1; i < len; i++)
297 if (! VECTORP (AREF (key, i)))
298 goto invalid_composition;
300 else if (VECTORP (components) || CONSP (components))
302 int len = XVECTOR (key)->size;
304 /* The number of elements should be odd. */
305 if ((len % 2) == 0)
306 goto invalid_composition;
307 /* All elements should be integers (character or encoded
308 composition rule). */
309 for (i = 0; i < len; i++)
311 if (!INTEGERP (key_contents[i]))
312 goto invalid_composition;
316 /* Change PROP from Form-A above to Form-B. We can directly modify
317 the cons cell of PROP because it is not shared. */
318 XSETFASTINT (id, n_compositions);
319 XSETCAR (prop, id);
320 XSETCDR (prop, Fcons (make_number (nchars), Fcons (key, XCDR (prop))));
322 /* Register the composition in composition_hash_table. */
323 hash_index = hash_put (hash_table, key, id, hash_code);
325 /* Register the composition in composition_table. */
326 cmp = (struct composition *) xmalloc (sizeof (struct composition));
328 cmp->method = (NILP (components)
329 ? COMPOSITION_RELATIVE
330 : ((INTEGERP (components) || STRINGP (components))
331 ? COMPOSITION_WITH_ALTCHARS
332 : COMPOSITION_WITH_RULE_ALTCHARS));
333 cmp->hash_index = hash_index;
334 glyph_len = (cmp->method == COMPOSITION_WITH_RULE_ALTCHARS
335 ? (XVECTOR (key)->size + 1) / 2
336 : XVECTOR (key)->size);
337 cmp->glyph_len = glyph_len;
338 cmp->offsets = (short *) xmalloc (sizeof (short) * glyph_len * 2);
339 cmp->font = NULL;
341 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
343 /* Relative composition. */
344 cmp->width = 0;
345 for (i = 0; i < glyph_len; i++)
347 int this_width;
348 ch = XINT (key_contents[i]);
349 this_width = (ch == '\t' ? 1 : CHAR_WIDTH (ch));
350 if (cmp->width < this_width)
351 cmp->width = this_width;
354 else
356 /* Rule-base composition. */
357 float leftmost = 0.0, rightmost;
359 ch = XINT (key_contents[0]);
360 rightmost = ch != '\t' ? CHAR_WIDTH (ch) : 1;
362 for (i = 1; i < glyph_len; i += 2)
364 int rule, gref, nref, xoff, yoff;
365 int this_width;
366 float this_left;
368 rule = XINT (key_contents[i]);
369 ch = XINT (key_contents[i + 1]);
370 this_width = ch != '\t' ? CHAR_WIDTH (ch) : 1;
372 /* A composition rule is specified by an integer value
373 that encodes global and new reference points (GREF and
374 NREF). GREF and NREF are specified by numbers as
375 below:
376 0---1---2 -- ascent
380 9--10--11 -- center
382 ---3---4---5--- baseline
384 6---7---8 -- descent
386 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
387 this_left = (leftmost
388 + (gref % 3) * (rightmost - leftmost) / 2.0
389 - (nref % 3) * this_width / 2.0);
391 if (this_left < leftmost)
392 leftmost = this_left;
393 if (this_left + this_width > rightmost)
394 rightmost = this_left + this_width;
397 cmp->width = rightmost - leftmost;
398 if (cmp->width < (rightmost - leftmost))
399 /* To get a ceiling integer value. */
400 cmp->width++;
403 composition_table[n_compositions] = cmp;
405 return n_compositions++;
407 invalid_composition:
408 /* Would it be better to remove this `composition' property? */
409 return -1;
413 /* Find a static composition at or nearest to position POS of OBJECT
414 (buffer or string).
416 OBJECT defaults to the current buffer. If there's a composition at
417 POS, set *START and *END to the start and end of the sequence,
418 *PROP to the `composition' property, and return 1.
420 If there's no composition at POS and LIMIT is negative, return 0.
422 Otherwise, search for a composition forward (LIMIT > POS) or
423 backward (LIMIT < POS). In this case, LIMIT bounds the search.
425 If a composition is found, set *START, *END, and *PROP as above,
426 and return 1, else return 0.
428 This doesn't check the validity of composition. */
431 find_composition (pos, limit, start, end, prop, object)
432 int pos, limit;
433 EMACS_INT *start, *end;
434 Lisp_Object *prop, object;
436 Lisp_Object val;
438 if (get_property_and_range (pos, Qcomposition, prop, start, end, object))
439 return 1;
441 if (limit < 0 || limit == pos)
442 return 0;
444 if (limit > pos) /* search forward */
446 val = Fnext_single_property_change (make_number (pos), Qcomposition,
447 object, make_number (limit));
448 pos = XINT (val);
449 if (pos == limit)
450 return 0;
452 else /* search backward */
454 if (get_property_and_range (pos - 1, Qcomposition, prop, start, end,
455 object))
456 return 1;
457 val = Fprevious_single_property_change (make_number (pos), Qcomposition,
458 object, make_number (limit));
459 pos = XINT (val);
460 if (pos == limit)
461 return 0;
462 pos--;
464 get_property_and_range (pos, Qcomposition, prop, start, end, object);
465 return 1;
468 /* Run a proper function to adjust the composition sitting between
469 FROM and TO with property PROP. */
471 static void
472 run_composition_function (from, to, prop)
473 int from, to;
474 Lisp_Object prop;
476 Lisp_Object func;
477 EMACS_INT start, end;
479 func = COMPOSITION_MODIFICATION_FUNC (prop);
480 /* If an invalid composition precedes or follows, try to make them
481 valid too. */
482 if (from > BEGV
483 && find_composition (from - 1, -1, &start, &end, &prop, Qnil)
484 && !COMPOSITION_VALID_P (start, end, prop))
485 from = start;
486 if (to < ZV
487 && find_composition (to, -1, &start, &end, &prop, Qnil)
488 && !COMPOSITION_VALID_P (start, end, prop))
489 to = end;
490 if (!NILP (Ffboundp (func)))
491 call2 (func, make_number (from), make_number (to));
494 /* Make invalid compositions adjacent to or inside FROM and TO valid.
495 CHECK_MASK is bitwise `or' of mask bits defined by macros
496 CHECK_XXX (see the comment in composite.h).
498 It also resets the text-property `auto-composed' to a proper region
499 so that automatic character composition works correctly later while
500 displaying the region.
502 This function is called when a buffer text is changed. If the
503 change is deletion, FROM == TO. Otherwise, FROM < TO. */
505 void
506 update_compositions (from, to, check_mask)
507 EMACS_INT from, to;
508 int check_mask;
510 Lisp_Object prop;
511 EMACS_INT start, end;
512 /* The beginning and end of the region to set the property
513 `auto-composed' to nil. */
514 EMACS_INT min_pos = from, max_pos = to;
516 if (inhibit_modification_hooks)
517 return;
519 /* If FROM and TO are not in a valid range, do nothing. */
520 if (! (BEGV <= from && from <= to && to <= ZV))
521 return;
523 if (check_mask & CHECK_HEAD)
525 /* FROM should be at composition boundary. But, insertion or
526 deletion will make two compositions adjacent and
527 indistinguishable when they have same (eq) property. To
528 avoid it, in such a case, we change the property of the
529 latter to the copy of it. */
530 if (from > BEGV
531 && find_composition (from - 1, -1, &start, &end, &prop, Qnil)
532 && COMPOSITION_VALID_P (start, end, prop))
534 min_pos = start;
535 if (end > to)
536 max_pos = end;
537 if (from < end)
538 Fput_text_property (make_number (from), make_number (end),
539 Qcomposition,
540 Fcons (XCAR (prop), XCDR (prop)), Qnil);
541 run_composition_function (start, end, prop);
542 from = end;
544 else if (from < ZV
545 && find_composition (from, -1, &start, &from, &prop, Qnil)
546 && COMPOSITION_VALID_P (start, from, prop))
548 if (from > to)
549 max_pos = from;
550 run_composition_function (start, from, prop);
554 if (check_mask & CHECK_INSIDE)
556 /* In this case, we are sure that (check & CHECK_TAIL) is also
557 nonzero. Thus, here we should check only compositions before
558 (to - 1). */
559 while (from < to - 1
560 && find_composition (from, to, &start, &from, &prop, Qnil)
561 && COMPOSITION_VALID_P (start, from, prop)
562 && from < to - 1)
563 run_composition_function (start, from, prop);
566 if (check_mask & CHECK_TAIL)
568 if (from < to
569 && find_composition (to - 1, -1, &start, &end, &prop, Qnil)
570 && COMPOSITION_VALID_P (start, end, prop))
572 /* TO should be also at composition boundary. But,
573 insertion or deletion will make two compositions adjacent
574 and indistinguishable when they have same (eq) property.
575 To avoid it, in such a case, we change the property of
576 the former to the copy of it. */
577 if (to < end)
579 Fput_text_property (make_number (start), make_number (to),
580 Qcomposition,
581 Fcons (XCAR (prop), XCDR (prop)), Qnil);
582 max_pos = end;
584 run_composition_function (start, end, prop);
586 else if (to < ZV
587 && find_composition (to, -1, &start, &end, &prop, Qnil)
588 && COMPOSITION_VALID_P (start, end, prop))
590 run_composition_function (start, end, prop);
591 max_pos = end;
594 if (min_pos < max_pos)
596 int count = SPECPDL_INDEX ();
598 specbind (Qinhibit_read_only, Qt);
599 specbind (Qinhibit_modification_hooks, Qt);
600 specbind (Qinhibit_point_motion_hooks, Qt);
601 Fremove_list_of_text_properties (make_number (min_pos),
602 make_number (max_pos),
603 Fcons (Qauto_composed, Qnil), Qnil);
604 unbind_to (count, Qnil);
609 /* Modify composition property values in LIST destructively. LIST is
610 a list as returned from text_property_list. Change values to the
611 top-level copies of them so that none of them are `eq'. */
613 void
614 make_composition_value_copy (list)
615 Lisp_Object list;
617 Lisp_Object plist, val;
619 for (; CONSP (list); list = XCDR (list))
621 plist = XCAR (XCDR (XCDR (XCAR (list))));
622 while (CONSP (plist) && CONSP (XCDR (plist)))
624 if (EQ (XCAR (plist), Qcomposition)
625 && (val = XCAR (XCDR (plist)), CONSP (val)))
626 XSETCAR (XCDR (plist), Fcons (XCAR (val), XCDR (val)));
627 plist = XCDR (XCDR (plist));
633 /* Make text in the region between START and END a composition that
634 has COMPONENTS and MODIFICATION-FUNC.
636 If STRING is non-nil, then operate on characters contained between
637 indices START and END in STRING. */
639 void
640 compose_text (start, end, components, modification_func, string)
641 int start, end;
642 Lisp_Object components, modification_func, string;
644 Lisp_Object prop;
646 prop = Fcons (Fcons (make_number (end - start), components),
647 modification_func);
648 Fput_text_property (make_number (start), make_number (end),
649 Qcomposition, prop, string);
653 static Lisp_Object autocmp_chars P_ ((Lisp_Object, EMACS_INT, EMACS_INT,
654 EMACS_INT, struct window *,
655 struct face *, Lisp_Object));
658 /* Lisp glyph-string handlers */
660 /* Hash table for automatic composition. The key is a header of a
661 lgstring (Lispy glyph-string), and the value is a body of a
662 lgstring. */
664 static Lisp_Object gstring_hash_table;
666 static Lisp_Object gstring_lookup_cache P_ ((Lisp_Object));
668 static Lisp_Object
669 gstring_lookup_cache (header)
670 Lisp_Object header;
672 struct Lisp_Hash_Table *h = XHASH_TABLE (gstring_hash_table);
673 int i = hash_lookup (h, header, NULL);
675 return (i >= 0 ? HASH_VALUE (h, i) : Qnil);
678 Lisp_Object
679 composition_gstring_put_cache (gstring, len)
680 Lisp_Object gstring;
681 int len;
683 struct Lisp_Hash_Table *h = XHASH_TABLE (gstring_hash_table);
684 unsigned hash;
685 Lisp_Object header, copy;
686 int i;
688 header = LGSTRING_HEADER (gstring);
689 hash = h->hashfn (h, header);
690 if (len < 0)
692 len = LGSTRING_GLYPH_LEN (gstring);
693 for (i = 0; i < len; i++)
694 if (NILP (LGSTRING_GLYPH (gstring, i)))
695 break;
696 len = i;
699 copy = Fmake_vector (make_number (len + 2), Qnil);
700 LGSTRING_SET_HEADER (copy, Fcopy_sequence (header));
701 for (i = 0; i < len; i++)
702 LGSTRING_SET_GLYPH (copy, i, Fcopy_sequence (LGSTRING_GLYPH (gstring, i)));
703 i = hash_put (h, LGSTRING_HEADER (copy), copy, hash);
704 LGSTRING_SET_ID (copy, make_number (i));
705 return copy;
708 Lisp_Object
709 composition_gstring_from_id (id)
710 int id;
712 struct Lisp_Hash_Table *h = XHASH_TABLE (gstring_hash_table);
714 return HASH_VALUE (h, id);
717 static Lisp_Object fill_gstring_header P_ ((Lisp_Object, Lisp_Object,
718 Lisp_Object, Lisp_Object,
719 Lisp_Object));
722 composition_gstring_p (gstring)
723 Lisp_Object gstring;
725 Lisp_Object header;
726 int i;
728 if (! VECTORP (gstring) || ASIZE (gstring) < 2)
729 return 0;
730 header = LGSTRING_HEADER (gstring);
731 if (! VECTORP (header) || ASIZE (header) < 2)
732 return 0;
733 if (! NILP (LGSTRING_FONT (gstring))
734 && (! FONT_OBJECT_P (LGSTRING_FONT (gstring))
735 && ! CODING_SYSTEM_P (LGSTRING_FONT (gstring))))
736 return 0;
737 for (i = 1; i < ASIZE (LGSTRING_HEADER (gstring)); i++)
738 if (! NATNUMP (AREF (LGSTRING_HEADER (gstring), i)))
739 return 0;
740 if (! NILP (LGSTRING_ID (gstring)) && ! NATNUMP (LGSTRING_ID (gstring)))
741 return 0;
742 for (i = 0; i < LGSTRING_GLYPH_LEN (gstring); i++)
744 Lisp_Object glyph = LGSTRING_GLYPH (gstring, i);
745 if (NILP (glyph))
746 break;
747 if (! VECTORP (glyph) || ASIZE (glyph) != LGLYPH_SIZE)
748 return 0;
750 return 1;
754 composition_gstring_width (gstring, from, to, metrics)
755 Lisp_Object gstring;
756 int from, to;
757 struct font_metrics *metrics;
759 Lisp_Object *glyph;
760 int width = 0;
762 if (metrics)
764 Lisp_Object font_object = LGSTRING_FONT (gstring);
766 if (FONT_OBJECT_P (font_object))
768 struct font *font = XFONT_OBJECT (font_object);
770 metrics->ascent = font->ascent;
771 metrics->descent = font->descent;
773 else
775 metrics->ascent = 1;
776 metrics->descent = 0;
778 metrics->width = metrics->lbearing = metrics->rbearing = 0;
780 for (glyph = &LGSTRING_GLYPH (gstring, from); from < to; from++, glyph++)
782 int x;
784 if (NILP (LGLYPH_ADJUSTMENT (*glyph)))
785 width += LGLYPH_WIDTH (*glyph);
786 else
787 width += LGLYPH_WADJUST (*glyph);
788 if (metrics)
790 x = metrics->width + LGLYPH_LBEARING (*glyph) + LGLYPH_XOFF (*glyph);
791 if (metrics->lbearing > x)
792 metrics->lbearing = x;
793 x = metrics->width + LGLYPH_RBEARING (*glyph) + LGLYPH_XOFF (*glyph);
794 if (metrics->rbearing < x)
795 metrics->rbearing = x;
796 metrics->width = width;
797 x = LGLYPH_ASCENT (*glyph) - LGLYPH_YOFF (*glyph);
798 if (metrics->ascent < x)
799 metrics->ascent = x;
800 x = LGLYPH_DESCENT (*glyph) + LGLYPH_YOFF (*glyph);
801 if (metrics->descent < x)
802 metrics->descent = x;
805 return width;
809 static Lisp_Object gstring_work;
810 static Lisp_Object gstring_work_headers;
812 static Lisp_Object
813 fill_gstring_header (header, start, end, font_object, string)
814 Lisp_Object header, start, end, font_object, string;
816 EMACS_INT from, to, from_byte;
817 EMACS_INT len, i;
819 if (NILP (string))
821 if (NILP (current_buffer->enable_multibyte_characters))
822 error ("Attempt to shape unibyte text");
823 validate_region (&start, &end);
824 from = XFASTINT (start);
825 to = XFASTINT (end);
826 from_byte = CHAR_TO_BYTE (from);
828 else
830 CHECK_STRING (string);
831 if (! STRING_MULTIBYTE (string))
832 error ("Attempt to shape unibyte text");
833 /* FROM and TO are checked by the caller. */
834 from = XINT (start);
835 to = XINT (end);
836 if (from < 0 || from > to || to > SCHARS (string))
837 args_out_of_range_3 (string, start, end);
838 from_byte = string_char_to_byte (string, from);
841 len = to - from;
842 if (len == 0)
843 error ("Attempt to shape zero-length text");
844 if (VECTORP (header))
846 if (ASIZE (header) != len + 1)
847 args_out_of_range (header, make_number (len + 1));
849 else
851 if (len <= 8)
852 header = AREF (gstring_work_headers, len - 1);
853 else
854 header = Fmake_vector (make_number (len + 1), Qnil);
857 ASET (header, 0, font_object);
858 for (i = 0; i < len; i++)
860 int c;
862 if (NILP (string))
863 FETCH_CHAR_ADVANCE_NO_CHECK (c, from, from_byte);
864 else
865 FETCH_STRING_CHAR_ADVANCE_NO_CHECK (c, string, from, from_byte);
866 ASET (header, i + 1, make_number (c));
868 return header;
871 extern void font_fill_lglyph_metrics P_ ((Lisp_Object, Lisp_Object));
873 static void
874 fill_gstring_body (gstring)
875 Lisp_Object gstring;
877 Lisp_Object font_object = LGSTRING_FONT (gstring);
878 Lisp_Object header = AREF (gstring, 0);
879 EMACS_INT len = LGSTRING_CHAR_LEN (gstring);
880 EMACS_INT i;
882 for (i = 0; i < len; i++)
884 Lisp_Object g = LGSTRING_GLYPH (gstring, i);
885 EMACS_INT c = XINT (AREF (header, i + 1));
887 if (NILP (g))
889 g = LGLYPH_NEW ();
890 LGSTRING_SET_GLYPH (gstring, i, g);
892 LGLYPH_SET_FROM (g, i);
893 LGLYPH_SET_TO (g, i);
894 LGLYPH_SET_CHAR (g, c);
895 if (FONT_OBJECT_P (font_object))
897 font_fill_lglyph_metrics (g, font_object);
899 else
901 int width = XFASTINT (CHAR_TABLE_REF (Vchar_width_table, c));
903 LGLYPH_SET_CODE (g, c);
904 LGLYPH_SET_LBEARING (g, 0);
905 LGLYPH_SET_RBEARING (g, width);
906 LGLYPH_SET_WIDTH (g, width);
907 LGLYPH_SET_ASCENT (g, 1);
908 LGLYPH_SET_DESCENT (g, 0);
910 LGLYPH_SET_ADJUSTMENT (g, Qnil);
912 if (i < LGSTRING_GLYPH_LEN (gstring))
913 LGSTRING_SET_GLYPH (gstring, i, Qnil);
917 /* Try to compose the characters at CHARPOS according to CFT_ELEMENT
918 which is an element of composition-function-table (which see).
919 LIMIT limits the characters to compose. STRING, if not nil, is a
920 target string. WIN is a window where the characters are being
921 displayed. */
923 static Lisp_Object
924 autocmp_chars (cft_element, charpos, bytepos, limit, win, face, string)
925 Lisp_Object cft_element;
926 EMACS_INT charpos, bytepos, limit;
927 struct window *win;
928 struct face *face;
929 Lisp_Object string;
931 int count = SPECPDL_INDEX ();
932 FRAME_PTR f = XFRAME (win->frame);
933 Lisp_Object pos = make_number (charpos);
934 EMACS_INT pt = PT, pt_byte = PT_BYTE;
935 int lookback;
937 record_unwind_save_match_data ();
938 for (lookback = -1; CONSP (cft_element); cft_element = XCDR (cft_element))
940 Lisp_Object elt = XCAR (cft_element);
941 Lisp_Object re;
942 Lisp_Object font_object = Qnil, gstring;
943 EMACS_INT len, to;
945 if (! VECTORP (elt) || ASIZE (elt) != 3)
946 continue;
947 if (lookback < 0)
949 lookback = XFASTINT (AREF (elt, 1));
950 if (limit > charpos + MAX_COMPOSITION_COMPONENTS)
951 limit = charpos + MAX_COMPOSITION_COMPONENTS;
953 else if (lookback != XFASTINT (AREF (elt, 1)))
954 break;
955 re = AREF (elt, 0);
956 if (NILP (re))
957 len = 1;
958 else if ((len = fast_looking_at (re, charpos, bytepos, limit, -1, string))
959 > 0)
961 if (NILP (string))
962 len = BYTE_TO_CHAR (bytepos + len) - charpos;
963 else
964 len = string_byte_to_char (string, bytepos + len) - charpos;
966 if (len > 0)
968 limit = to = charpos + len;
969 #ifdef HAVE_WINDOW_SYSTEM
970 if (FRAME_WINDOW_P (f))
972 font_object = font_range (charpos, &to, win, face, string);
973 if (! FONT_OBJECT_P (font_object)
974 || (! NILP (re)
975 && to < limit
976 && (fast_looking_at (re, charpos, bytepos, to, -1, string) <= 0)))
978 if (NILP (string))
979 TEMP_SET_PT_BOTH (pt, pt_byte);
980 return unbind_to (count, Qnil);
983 else
984 #endif /* not HAVE_WINDOW_SYSTEM */
985 font_object = win->frame;
986 gstring = Fcomposition_get_gstring (pos, make_number (to),
987 font_object, string);
988 if (NILP (LGSTRING_ID (gstring)))
990 Lisp_Object args[6];
992 args[0] = Vauto_composition_function;
993 args[1] = AREF (elt, 2);
994 args[2] = pos;
995 args[3] = make_number (to);
996 args[4] = font_object;
997 args[5] = string;
998 gstring = safe_call (6, args);
1000 if (NILP (string))
1001 TEMP_SET_PT_BOTH (pt, pt_byte);
1002 return unbind_to (count, gstring);
1005 if (NILP (string))
1006 TEMP_SET_PT_BOTH (pt, pt_byte);
1007 return unbind_to (count, Qnil);
1011 /* Update cmp_it->stop_pos to the next position after CHARPOS (and
1012 BYTEPOS) where character composition may happen. If BYTEPOS is
1013 negative, compoute it. If it is a static composition, set
1014 cmp_it->ch to -1. Otherwise, set cmp_it->ch to the character that
1015 triggers a automatic composition. */
1017 void
1018 composition_compute_stop_pos (cmp_it, charpos, bytepos, endpos, string)
1019 struct composition_it *cmp_it;
1020 EMACS_INT charpos, bytepos, endpos;
1021 Lisp_Object string;
1023 EMACS_INT start, end, c;
1024 Lisp_Object prop, val;
1025 /* This is from forward_to_next_line_start in xdisp.c. */
1026 const int MAX_NEWLINE_DISTANCE = 500;
1028 if (endpos > charpos + MAX_NEWLINE_DISTANCE)
1029 endpos = charpos + MAX_NEWLINE_DISTANCE;
1030 cmp_it->stop_pos = endpos;
1031 cmp_it->id = -1;
1032 cmp_it->ch = -2;
1033 if (find_composition (charpos, endpos, &start, &end, &prop, string)
1034 && COMPOSITION_VALID_P (start, end, prop))
1036 cmp_it->stop_pos = endpos = start;
1037 cmp_it->ch = -1;
1039 if (NILP (string) && PT > charpos && PT < endpos)
1040 cmp_it->stop_pos = PT;
1041 if (NILP (current_buffer->enable_multibyte_characters)
1042 || ! FUNCTIONP (Vauto_composition_function))
1043 return;
1044 if (bytepos < 0)
1046 if (STRINGP (string))
1047 bytepos = string_char_to_byte (string, charpos);
1048 else
1049 bytepos = CHAR_TO_BYTE (charpos);
1052 start = charpos;
1053 while (charpos < endpos)
1055 if (STRINGP (string))
1056 FETCH_STRING_CHAR_ADVANCE (c, string, charpos, bytepos);
1057 else
1058 FETCH_CHAR_ADVANCE (c, charpos, bytepos);
1059 if (c == '\n')
1061 cmp_it->ch = -2;
1062 break;
1064 val = CHAR_TABLE_REF (Vcomposition_function_table, c);
1065 if (! NILP (val))
1067 Lisp_Object elt;
1069 for (; CONSP (val); val = XCDR (val))
1071 elt = XCAR (val);
1072 if (VECTORP (elt) && ASIZE (elt) == 3 && NATNUMP (AREF (elt, 1))
1073 && charpos - 1 - XFASTINT (AREF (elt, 1)) >= start)
1074 break;
1076 if (CONSP (val))
1078 cmp_it->lookback = XFASTINT (AREF (elt, 1));
1079 cmp_it->stop_pos = charpos - 1 - cmp_it->lookback;
1080 cmp_it->ch = c;
1081 return;
1085 cmp_it->stop_pos = charpos;
1088 /* Check if the character at CHARPOS (and BYTEPOS) is composed
1089 (possibly with the following characters) on window W. ENDPOS limits
1090 characters to be composed. FACE, in non-NULL, is a base face of
1091 the character. If STRING is not nil, it is a string containing the
1092 character to check, and CHARPOS and BYTEPOS are indices in the
1093 string. In that case, FACE must not be NULL.
1095 If the character is composed, setup members of CMP_IT (id, nglyphs,
1096 and from), and return 1. Otherwise, update CMP_IT->stop_pos, and
1097 return 0. */
1100 composition_reseat_it (cmp_it, charpos, bytepos, endpos, w, face, string)
1101 struct composition_it *cmp_it;
1102 EMACS_INT charpos, bytepos, endpos;
1103 struct window *w;
1104 struct face *face;
1105 Lisp_Object string;
1107 if (charpos < PT && PT < endpos)
1108 endpos = PT;
1110 if (cmp_it->ch == -2)
1112 composition_compute_stop_pos (cmp_it, charpos, bytepos, endpos, string);
1113 if (cmp_it->ch == -2)
1114 return 0;
1117 if (cmp_it->ch < 0)
1119 /* We are looking at a static composition. */
1120 EMACS_INT start, end;
1121 Lisp_Object prop;
1123 find_composition (charpos, -1, &start, &end, &prop, string);
1124 cmp_it->id = get_composition_id (charpos, bytepos, end - start,
1125 prop, string);
1126 if (cmp_it->id < 0)
1127 goto no_composition;
1128 cmp_it->nchars = end - start;
1129 cmp_it->nglyphs = composition_table[cmp_it->id]->glyph_len;
1131 else if (w)
1133 Lisp_Object val, elt;
1134 int i;
1136 val = CHAR_TABLE_REF (Vcomposition_function_table, cmp_it->ch);
1137 for (; CONSP (val); val = XCDR (val))
1139 elt = XCAR (val);
1140 if (cmp_it->lookback == XFASTINT (AREF (elt, 1)))
1141 break;
1143 if (NILP (val))
1144 goto no_composition;
1146 val = autocmp_chars (val, charpos, bytepos, endpos, w, face, string);
1147 if (! composition_gstring_p (val))
1148 goto no_composition;
1149 if (NILP (LGSTRING_ID (val)))
1150 val = composition_gstring_put_cache (val, -1);
1151 cmp_it->id = XINT (LGSTRING_ID (val));
1152 for (i = 0; i < LGSTRING_GLYPH_LEN (val); i++)
1153 if (NILP (LGSTRING_GLYPH (val, i)))
1154 break;
1155 cmp_it->nglyphs = i;
1157 else
1158 goto no_composition;
1159 cmp_it->from = 0;
1160 return 1;
1162 no_composition:
1163 charpos++;
1164 if (STRINGP (string))
1165 bytepos += MULTIBYTE_LENGTH_NO_CHECK (SDATA (string) + bytepos);
1166 else
1167 INC_POS (bytepos);
1168 composition_compute_stop_pos (cmp_it, charpos, bytepos, endpos, string);
1169 return 0;
1173 composition_update_it (cmp_it, charpos, bytepos, string)
1174 struct composition_it *cmp_it;
1175 EMACS_INT charpos, bytepos;
1176 Lisp_Object string;
1178 int i, c;
1180 if (cmp_it->ch < 0)
1182 struct composition *cmp = composition_table[cmp_it->id];
1184 cmp_it->to = cmp_it->nglyphs;
1185 if (cmp_it->nglyphs == 0)
1186 c = -1;
1187 else
1189 for (i = 0; i < cmp->glyph_len; i++)
1190 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
1191 break;
1192 if (c == '\t')
1193 c = ' ';
1195 cmp_it->width = cmp->width;
1197 else
1199 Lisp_Object gstring = composition_gstring_from_id (cmp_it->id);
1201 if (cmp_it->nglyphs == 0)
1203 c = -1;
1204 cmp_it->nchars = LGSTRING_CHAR_LEN (gstring);
1205 cmp_it->width = 0;
1207 else
1209 Lisp_Object glyph = LGSTRING_GLYPH (gstring, cmp_it->from);
1210 int from = LGLYPH_FROM (glyph);
1212 c = XINT (LGSTRING_CHAR (gstring, from));
1213 cmp_it->nchars = LGLYPH_TO (glyph) - from + 1;
1214 cmp_it->width = (LGLYPH_WIDTH (glyph) > 0
1215 ? CHAR_WIDTH (LGLYPH_CHAR (glyph)) : 0);
1216 for (cmp_it->to = cmp_it->from + 1; cmp_it->to < cmp_it->nglyphs;
1217 cmp_it->to++)
1219 glyph = LGSTRING_GLYPH (gstring, cmp_it->to);
1220 if (LGLYPH_FROM (glyph) != from)
1221 break;
1222 if (LGLYPH_WIDTH (glyph) > 0)
1223 cmp_it->width += CHAR_WIDTH (LGLYPH_CHAR (glyph));
1228 charpos += cmp_it->nchars;
1229 if (STRINGP (string))
1230 cmp_it->nbytes = string_char_to_byte (string, charpos) - bytepos;
1231 else
1232 cmp_it->nbytes = CHAR_TO_BYTE (charpos) - bytepos;
1233 return c;
1237 struct position_record
1239 EMACS_INT pos, pos_byte;
1240 unsigned char *p;
1243 /* Update the members of POSTION to the next character boundary. */
1244 #define FORWARD_CHAR(POSITION, STOP) \
1245 do { \
1246 (POSITION).pos++; \
1247 if ((POSITION).pos == (STOP)) \
1249 (POSITION).p = GAP_END_ADDR; \
1250 (POSITION).pos_byte = GPT_BYTE; \
1252 else \
1254 (POSITION).pos_byte += BYTES_BY_CHAR_HEAD (*((POSITION).p)); \
1255 (POSITION).p += BYTES_BY_CHAR_HEAD (*((POSITION).p)); \
1257 } while (0)
1259 /* Update the members of POSTION to the previous character boundary. */
1260 #define BACKWARD_CHAR(POSITION, STOP) \
1261 do { \
1262 if ((POSITION).pos == STOP) \
1263 (POSITION).p = GPT_ADDR; \
1264 do { \
1265 (POSITION).pos_byte--; \
1266 (POSITION).p--; \
1267 } while (! CHAR_HEAD_P (*((POSITION).p))); \
1268 (POSITION).pos--; \
1269 } while (0)
1271 static Lisp_Object _work_val;
1272 static int _work_char;
1274 /* 1 iff the character C is composable. */
1275 #define CHAR_COMPOSABLE_P(C) \
1276 ((C) == 0x200C || (C) == 0x200D \
1277 || (_work_val = CHAR_TABLE_REF (Vunicode_category_table, (C)), \
1278 (SYMBOLP (_work_val) \
1279 && (_work_char = SDATA (SYMBOL_NAME (_work_val))[0]) != 'C' \
1280 && _work_char != 'Z')))
1282 /* This is like find_composition, but find an automatic composition
1283 instead. If found, set *GSTRING to the glyph-string representing
1284 the composition, and return 1. Otherwise, return 0. */
1286 static int
1287 find_automatic_composition (pos, limit, start, end, gstring, string)
1288 EMACS_INT pos, limit, *start, *end;
1289 Lisp_Object *gstring, string;
1291 EMACS_INT head, tail, stop;
1292 /* Limit to check a composition after POS. */
1293 EMACS_INT fore_check_limit;
1294 struct position_record orig, cur, check, prev;
1295 Lisp_Object check_val, val, elt;
1296 int check_lookback;
1297 int c;
1298 Lisp_Object window;
1299 struct window *w;
1301 window = Fget_buffer_window (Fcurrent_buffer (), Qnil);
1302 if (NILP (window))
1303 return 0;
1304 w = XWINDOW (window);
1306 orig.pos = pos;
1307 if (NILP (string))
1309 head = BEGV, tail = ZV, stop = GPT;
1310 orig.pos_byte = CHAR_TO_BYTE (orig.pos);
1311 orig.p = BYTE_POS_ADDR (orig.pos_byte);
1313 else
1315 head = 0, tail = SCHARS (string), stop = -1;
1316 orig.pos_byte = string_char_to_byte (string, orig.pos);
1317 orig.p = SDATA (string) + orig.pos_byte;
1319 if (limit < pos)
1320 fore_check_limit = min (tail, pos + MAX_AUTO_COMPOSITION_LOOKBACK);
1321 else
1322 fore_check_limit = min (tail, limit + MAX_AUTO_COMPOSITION_LOOKBACK);
1323 cur = orig;
1325 retry:
1326 check_val = Qnil;
1327 /* At first, check if POS is composable. */
1328 c = STRING_CHAR (cur.p);
1329 if (! CHAR_COMPOSABLE_P (c))
1331 if (limit < 0)
1332 return 0;
1333 if (limit >= cur.pos)
1334 goto search_forward;
1336 else
1338 val = CHAR_TABLE_REF (Vcomposition_function_table, c);
1339 if (! NILP (val))
1340 check_val = val, check = cur;
1341 else
1342 while (cur.pos + 1 < fore_check_limit)
1344 EMACS_INT b, e;
1346 FORWARD_CHAR (cur, stop);
1347 if (get_property_and_range (cur.pos, Qcomposition, &val, &b, &e,
1348 Qnil)
1349 && COMPOSITION_VALID_P (b, e, val))
1351 fore_check_limit = cur.pos;
1352 break;
1354 c = STRING_CHAR (cur.p);
1355 if (! CHAR_COMPOSABLE_P (c))
1356 break;
1357 val = CHAR_TABLE_REF (Vcomposition_function_table, c);
1358 if (NILP (val))
1359 continue;
1360 check_val = val, check = cur;
1361 break;
1363 cur = orig;
1365 /* Rewind back to the position where we can safely search forward
1366 for compositions. */
1367 while (cur.pos > head)
1369 EMACS_INT b, e;
1371 BACKWARD_CHAR (cur, stop);
1372 if (get_property_and_range (cur.pos, Qcomposition, &val, &b, &e, Qnil)
1373 && COMPOSITION_VALID_P (b, e, val))
1374 break;
1375 c = STRING_CHAR (cur.p);
1376 if (! CHAR_COMPOSABLE_P (c))
1377 break;
1378 val = CHAR_TABLE_REF (Vcomposition_function_table, c);
1379 if (! NILP (val))
1380 check_val = val, check = cur;
1382 prev = cur;
1383 /* Now search forward. */
1384 search_forward:
1385 *gstring = Qnil;
1386 if (! NILP (check_val) || limit >= orig.pos)
1388 if (NILP (check_val))
1389 cur = orig;
1390 else
1391 cur = check;
1392 while (cur.pos < fore_check_limit)
1394 int need_adjustment = 0;
1396 if (NILP (check_val))
1398 c = STRING_CHAR (cur.p);
1399 check_val = CHAR_TABLE_REF (Vcomposition_function_table, c);
1401 for (; CONSP (check_val); check_val = XCDR (check_val))
1403 elt = XCAR (check_val);
1404 if (VECTORP (elt) && ASIZE (elt) == 3 && NATNUMP (AREF (elt, 1))
1405 && cur.pos - XFASTINT (AREF (elt, 1)) >= head)
1407 check.pos = cur.pos - XFASTINT (AREF (elt, 1));
1408 if (check.pos == cur.pos)
1409 check.pos_byte = cur.pos_byte;
1410 else
1411 check.pos_byte = CHAR_TO_BYTE (check.pos);
1412 val = autocmp_chars (check_val, check.pos, check.pos_byte,
1413 tail, w, NULL, string);
1414 need_adjustment = 1;
1415 if (! NILP (val))
1417 *gstring = val;
1418 *start = check.pos;
1419 *end = check.pos + LGSTRING_CHAR_LEN (*gstring);
1420 if (*start <= orig.pos ? *end > orig.pos
1421 : limit >= orig.pos)
1422 return 1;
1423 cur.pos = *end;
1424 cur.pos_byte = CHAR_TO_BYTE (cur.pos);
1425 break;
1429 if (need_adjustment)
1431 /* As we have called Lisp, there's a possibilily that
1432 buffer/string is relocated. */
1433 if (NILP (string))
1434 cur.p = BYTE_POS_ADDR (cur.pos_byte);
1435 else
1436 cur.p = SDATA (string) + cur.pos_byte;
1438 if (! CONSP (check_val))
1439 FORWARD_CHAR (cur, stop);
1440 check_val = Qnil;
1443 if (! NILP (*gstring))
1444 return (limit >= 0 || (*start <= orig.pos && *end > orig.pos));
1445 if (limit >= 0 && limit < orig.pos && prev.pos > head)
1447 cur = prev;
1448 BACKWARD_CHAR (cur, stop);
1449 orig = cur;
1450 fore_check_limit = orig.pos;
1451 goto retry;
1453 return 0;
1456 /* Return the adjusted point provided that point is moved from LAST_PT
1457 to NEW_PT. */
1460 composition_adjust_point (last_pt, new_pt)
1461 EMACS_INT last_pt, new_pt;
1463 EMACS_INT charpos, bytepos, startpos, beg, end, pos;
1464 Lisp_Object val;
1465 int i;
1467 if (new_pt == BEGV || new_pt == ZV)
1468 return new_pt;
1470 /* At first check the static composition. */
1471 if (get_property_and_range (new_pt, Qcomposition, &val, &beg, &end, Qnil)
1472 && COMPOSITION_VALID_P (beg, end, val))
1474 if (beg < new_pt /* && end > new_pt <- It's always the case. */
1475 && (last_pt <= beg || last_pt >= end))
1476 return (new_pt < last_pt ? beg : end);
1477 return new_pt;
1480 if (NILP (current_buffer->enable_multibyte_characters)
1481 || ! FUNCTIONP (Vauto_composition_function))
1482 return new_pt;
1484 /* Next check the automatic composition. */
1485 if (! find_automatic_composition (new_pt, (EMACS_INT) -1, &beg, &end, &val,
1486 Qnil)
1487 || beg == new_pt)
1488 return new_pt;
1489 for (i = 0; i < LGSTRING_GLYPH_LEN (val); i++)
1491 Lisp_Object glyph = LGSTRING_GLYPH (val, i);
1493 if (NILP (glyph))
1494 break;
1495 if (beg + LGLYPH_FROM (glyph) == new_pt)
1496 return new_pt;
1497 if (beg + LGLYPH_TO (glyph) >= new_pt)
1498 return (new_pt < last_pt
1499 ? beg + LGLYPH_FROM (glyph)
1500 : beg + LGLYPH_TO (glyph) + 1);
1502 return new_pt;
1505 DEFUN ("composition-get-gstring", Fcomposition_get_gstring,
1506 Scomposition_get_gstring, 4, 4, 0,
1507 doc: /* Return a glyph-string for characters between FROM and TO.
1508 If the glyph string is for graphic display, FONT-OBJECT must be
1509 a font-object to use for those characters.
1510 Otherwise (for terminal display), FONT-OBJECT must be a terminal ID, a
1511 frame, or nil for the selected frame's terminal device.
1513 If the optional 4th argument STRING is not nil, it is a string
1514 containing the target characters between indices FROM and TO.
1516 A glyph-string is a vector containing information about how to display
1517 a specific character sequence. The format is:
1518 [HEADER ID GLYPH ...]
1520 HEADER is a vector of this form:
1521 [FONT-OBJECT CHAR ...]
1522 where
1523 FONT-OBJECT is a font-object for all glyphs in the glyph-string,
1524 or the terminal coding system of the specified terminal.
1525 CHARs are characters to be composed by GLYPHs.
1527 ID is an identification number of the glyph-string. It may be nil if
1528 not yet shaped.
1530 GLYPH is a vector whose elements have this form:
1531 [ FROM-IDX TO-IDX C CODE WIDTH LBEARING RBEARING ASCENT DESCENT
1532 [ [X-OFF Y-OFF WADJUST] | nil] ]
1533 where
1534 FROM-IDX and TO-IDX are used internally and should not be touched.
1535 C is the character of the glyph.
1536 CODE is the glyph-code of C in FONT-OBJECT.
1537 WIDTH thru DESCENT are the metrics (in pixels) of the glyph.
1538 X-OFF and Y-OFF are offsets to the base position for the glyph.
1539 WADJUST is the adjustment to the normal width of the glyph.
1541 If GLYPH is nil, the remaining elements of the glyph-string vector
1542 should be ignored. */)
1543 (from, to, font_object, string)
1544 Lisp_Object font_object, from, to, string;
1546 Lisp_Object gstring, header;
1547 EMACS_INT frompos, topos;
1549 CHECK_NATNUM (from);
1550 CHECK_NATNUM (to);
1551 if (XINT (to) > XINT (from) + MAX_COMPOSITION_COMPONENTS)
1552 to = make_number (XINT (from) + MAX_COMPOSITION_COMPONENTS);
1553 if (! FONT_OBJECT_P (font_object))
1555 struct coding_system *coding;
1556 struct terminal *terminal = get_terminal (font_object, 1);
1558 coding = ((TERMINAL_TERMINAL_CODING (terminal)->common_flags
1559 & CODING_REQUIRE_ENCODING_MASK)
1560 ? TERMINAL_TERMINAL_CODING (terminal) : &safe_terminal_coding);
1561 font_object = CODING_ID_NAME (coding->id);
1564 header = fill_gstring_header (Qnil, from, to, font_object, string);
1565 gstring = gstring_lookup_cache (header);
1566 if (! NILP (gstring))
1567 return gstring;
1569 frompos = XINT (from);
1570 topos = XINT (to);
1571 if (LGSTRING_GLYPH_LEN (gstring_work) < topos - frompos)
1572 gstring_work = Fmake_vector (make_number (topos - frompos + 2), Qnil);
1573 LGSTRING_SET_HEADER (gstring_work, header);
1574 LGSTRING_SET_ID (gstring_work, Qnil);
1575 fill_gstring_body (gstring_work);
1576 return gstring_work;
1580 /* Emacs Lisp APIs. */
1582 DEFUN ("compose-region-internal", Fcompose_region_internal,
1583 Scompose_region_internal, 2, 4, 0,
1584 doc: /* Internal use only.
1586 Compose text in the region between START and END.
1587 Optional 3rd and 4th arguments are COMPONENTS and MODIFICATION-FUNC
1588 for the composition. See `compose-region' for more details. */)
1589 (start, end, components, modification_func)
1590 Lisp_Object start, end, components, modification_func;
1592 validate_region (&start, &end);
1593 if (!NILP (components)
1594 && !INTEGERP (components)
1595 && !CONSP (components)
1596 && !STRINGP (components))
1597 CHECK_VECTOR (components);
1599 compose_text (XINT (start), XINT (end), components, modification_func, Qnil);
1600 return Qnil;
1603 DEFUN ("compose-string-internal", Fcompose_string_internal,
1604 Scompose_string_internal, 3, 5, 0,
1605 doc: /* Internal use only.
1607 Compose text between indices START and END of STRING.
1608 Optional 4th and 5th arguments are COMPONENTS and MODIFICATION-FUNC
1609 for the composition. See `compose-string' for more details. */)
1610 (string, start, end, components, modification_func)
1611 Lisp_Object string, start, end, components, modification_func;
1613 CHECK_STRING (string);
1614 CHECK_NUMBER (start);
1615 CHECK_NUMBER (end);
1617 if (XINT (start) < 0 ||
1618 XINT (start) > XINT (end)
1619 || XINT (end) > SCHARS (string))
1620 args_out_of_range (start, end);
1622 compose_text (XINT (start), XINT (end), components, modification_func, string);
1623 return string;
1626 DEFUN ("find-composition-internal", Ffind_composition_internal,
1627 Sfind_composition_internal, 4, 4, 0,
1628 doc: /* Internal use only.
1630 Return information about composition at or nearest to position POS.
1631 See `find-composition' for more details. */)
1632 (pos, limit, string, detail_p)
1633 Lisp_Object pos, limit, string, detail_p;
1635 Lisp_Object prop, tail, gstring;
1636 EMACS_INT start, end, from, to;
1637 int id;
1639 CHECK_NUMBER_COERCE_MARKER (pos);
1640 from = XINT (pos);
1641 if (!NILP (limit))
1643 CHECK_NUMBER_COERCE_MARKER (limit);
1644 to = XINT (limit);
1646 else
1647 to = -1;
1649 if (!NILP (string))
1651 CHECK_STRING (string);
1652 if (XINT (pos) < 0 || XINT (pos) > SCHARS (string))
1653 args_out_of_range (string, pos);
1655 else
1657 if (XINT (pos) < BEGV || XINT (pos) > ZV)
1658 args_out_of_range (Fcurrent_buffer (), pos);
1661 if (!find_composition (from, to, &start, &end, &prop, string))
1663 if (!NILP (current_buffer->enable_multibyte_characters)
1664 && FUNCTIONP (Vauto_composition_function)
1665 && find_automatic_composition (from, to, &start, &end, &gstring,
1666 string))
1667 return list3 (make_number (start), make_number (end), gstring);
1668 return Qnil;
1670 if ((end <= XINT (pos) || start > XINT (pos)))
1672 EMACS_INT s, e;
1674 if (find_automatic_composition (from, to, &s, &e, &gstring, string)
1675 && (e <= XINT (pos) ? e > end : s < start))
1676 return list3 (make_number (start), make_number (end), gstring);
1678 if (!COMPOSITION_VALID_P (start, end, prop))
1679 return Fcons (make_number (start), Fcons (make_number (end),
1680 Fcons (Qnil, Qnil)));
1681 if (NILP (detail_p))
1682 return Fcons (make_number (start), Fcons (make_number (end),
1683 Fcons (Qt, Qnil)));
1685 if (COMPOSITION_REGISTERD_P (prop))
1686 id = COMPOSITION_ID (prop);
1687 else
1689 int start_byte = (NILP (string)
1690 ? CHAR_TO_BYTE (start)
1691 : string_char_to_byte (string, start));
1692 id = get_composition_id (start, start_byte, end - start, prop, string);
1695 if (id >= 0)
1697 Lisp_Object components, relative_p, mod_func;
1698 enum composition_method method = COMPOSITION_METHOD (prop);
1699 int width = composition_table[id]->width;
1701 components = Fcopy_sequence (COMPOSITION_COMPONENTS (prop));
1702 relative_p = (method == COMPOSITION_WITH_RULE_ALTCHARS
1703 ? Qnil : Qt);
1704 mod_func = COMPOSITION_MODIFICATION_FUNC (prop);
1705 tail = Fcons (components,
1706 Fcons (relative_p,
1707 Fcons (mod_func,
1708 Fcons (make_number (width), Qnil))));
1710 else
1711 tail = Qnil;
1713 return Fcons (make_number (start), Fcons (make_number (end), tail));
1717 void
1718 syms_of_composite ()
1720 int i;
1722 Qcomposition = intern_c_string ("composition");
1723 staticpro (&Qcomposition);
1725 /* Make a hash table for static composition. */
1727 Lisp_Object args[6];
1728 extern Lisp_Object QCsize;
1730 args[0] = QCtest;
1731 args[1] = Qequal;
1732 args[2] = QCweakness;
1733 /* We used to make the hash table weak so that unreferenced
1734 compositions can be garbage-collected. But, usually once
1735 created compositions are repeatedly used in an Emacs session,
1736 and thus it's not worth to save memory in such a way. So, we
1737 make the table not weak. */
1738 args[3] = Qnil;
1739 args[4] = QCsize;
1740 args[5] = make_number (311);
1741 composition_hash_table = Fmake_hash_table (6, args);
1742 staticpro (&composition_hash_table);
1745 /* Make a hash table for glyph-string. */
1747 Lisp_Object args[6];
1748 extern Lisp_Object QCsize;
1750 args[0] = QCtest;
1751 args[1] = Qequal;
1752 args[2] = QCweakness;
1753 args[3] = Qnil;
1754 args[4] = QCsize;
1755 args[5] = make_number (311);
1756 gstring_hash_table = Fmake_hash_table (6, args);
1757 staticpro (&gstring_hash_table);
1760 staticpro (&gstring_work_headers);
1761 gstring_work_headers = Fmake_vector (make_number (8), Qnil);
1762 for (i = 0; i < 8; i++)
1763 ASET (gstring_work_headers, i, Fmake_vector (make_number (i + 2), Qnil));
1764 staticpro (&gstring_work);
1765 gstring_work = Fmake_vector (make_number (10), Qnil);
1767 /* Text property `composition' should be nonsticky by default. */
1768 Vtext_property_default_nonsticky
1769 = Fcons (Fcons (Qcomposition, Qt), Vtext_property_default_nonsticky);
1771 DEFVAR_LISP ("compose-chars-after-function", &Vcompose_chars_after_function,
1772 doc: /* Function to adjust composition of buffer text.
1774 This function is called with three arguments: FROM, TO, and OBJECT.
1775 FROM and TO specify the range of text whose composition should be
1776 adjusted. OBJECT, if non-nil, is a string that contains the text.
1778 This function is called after a text with `composition' property is
1779 inserted or deleted to keep `composition' property of buffer text
1780 valid.
1782 The default value is the function `compose-chars-after'. */);
1783 Vcompose_chars_after_function = intern_c_string ("compose-chars-after");
1785 Qauto_composed = intern_c_string ("auto-composed");
1786 staticpro (&Qauto_composed);
1788 Qauto_composition_function = intern_c_string ("auto-composition-function");
1789 staticpro (&Qauto_composition_function);
1791 DEFVAR_LISP ("auto-composition-function", &Vauto_composition_function,
1792 doc: /* Function to call to compose characters automatically.
1793 This function is called from the display routine with four arguments:
1794 FROM, TO, WINDOW, and STRING.
1796 If STRING is nil, the function must compose characters in the region
1797 between FROM and TO in the current buffer.
1799 Otherwise, STRING is a string, and FROM and TO are indices into the
1800 string. In this case, the function must compose characters in the
1801 string. */);
1802 Vauto_composition_function = Qnil;
1804 DEFVAR_LISP ("composition-function-table", &Vcomposition_function_table,
1805 doc: /* Char-table of functions for automatic character composition.
1806 For each character that has to be composed automatically with
1807 preceding and/or following characters, this char-table contains
1808 a function to call to compose that character.
1810 The element at index C in the table, if non-nil, is a list of
1811 this form: ([PATTERN PREV-CHARS FUNC] ...)
1813 PATTERN is a regular expression which C and the surrounding
1814 characters must match.
1816 PREV-CHARS is a non-negative integer (less than 4) specifying how many
1817 characters before C to check the matching with PATTERN. If it is 0,
1818 PATTERN must match C and the following characters. If it is 1,
1819 PATTERN must match a character before C and the following characters.
1821 If PREV-CHARS is 0, PATTERN can be nil, which means that the
1822 single character C should be composed.
1824 FUNC is a function to return a glyph-string representing a
1825 composition of the characters that match PATTERN. It is
1826 called with one argument GSTRING.
1828 GSTRING is a template of a glyph-string to return. It is already
1829 filled with a proper header for the characters to compose, and
1830 glyphs corresponding to those characters one by one. The
1831 function must return a new glyph-string with the same header as
1832 GSTRING, or modify GSTRING itself and return it.
1834 See also the documentation of `auto-composition-mode'. */);
1835 Vcomposition_function_table = Fmake_char_table (Qnil, Qnil);
1837 defsubr (&Scompose_region_internal);
1838 defsubr (&Scompose_string_internal);
1839 defsubr (&Sfind_composition_internal);
1840 defsubr (&Scomposition_get_gstring);
1843 /* arch-tag: 79cefaf8-ca48-4eed-97e5-d5afb290d272
1844 (do not change this comment) */