diff options
| author | Kenichi Handa | 1999-12-15 00:08:01 +0000 |
|---|---|---|
| committer | Kenichi Handa | 1999-12-15 00:08:01 +0000 |
| commit | ca4c9455f8d4b719574e4dc6a6e8a4fc06dd408c (patch) | |
| tree | 65a0ebbfe621f34bb71725d8bed6749068d5bd0a | |
| parent | ec6d2bb84babd6588b3da8d205ff7bea64e812d7 (diff) | |
| download | emacs-ca4c9455f8d4b719574e4dc6a6e8a4fc06dd408c.tar.gz emacs-ca4c9455f8d4b719574e4dc6a6e8a4fc06dd408c.zip | |
New file
| -rw-r--r-- | src/composite.c | 707 | ||||
| -rw-r--r-- | src/composite.h | 207 |
2 files changed, 914 insertions, 0 deletions
diff --git a/src/composite.c b/src/composite.c new file mode 100644 index 00000000000..335fbc103cf --- /dev/null +++ b/src/composite.c | |||
| @@ -0,0 +1,707 @@ | |||
| 1 | /* Composite sequence support. | ||
| 2 | Copyright (C) 1999 Electrotechnical Laboratory, JAPAN. | ||
| 3 | Licensed to the Free Software Foundation. | ||
| 4 | |||
| 5 | This file is part of GNU Emacs. | ||
| 6 | |||
| 7 | GNU Emacs is free software; you can redistribute it and/or modify | ||
| 8 | it under the terms of the GNU General Public License as published by | ||
| 9 | the Free Software Foundation; either version 2, or (at your option) | ||
| 10 | any later version. | ||
| 11 | |||
| 12 | GNU Emacs is distributed in the hope that it will be useful, | ||
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 15 | GNU General Public License for more details. | ||
| 16 | |||
| 17 | You should have received a copy of the GNU General Public License | ||
| 18 | along with GNU Emacs; see the file COPYING. If not, write to | ||
| 19 | the Free Software Foundation, Inc., 59 Temple Place - Suite 330, | ||
| 20 | Boston, MA 02111-1307, USA. */ | ||
| 21 | |||
| 22 | #include <config.h> | ||
| 23 | #include "lisp.h" | ||
| 24 | #include "buffer.h" | ||
| 25 | #include "charset.h" | ||
| 26 | #include "intervals.h" | ||
| 27 | |||
| 28 | /* Emacs uses special text property `composition' to support character | ||
| 29 | composition. A sequence of characters that have the same (i.e. eq) | ||
| 30 | `composition' property value is treated as a single composite | ||
| 31 | sequence (we call it just `composition' here after). Characters in | ||
| 32 | a composition are all composed somehow on the screen. | ||
| 33 | |||
| 34 | The property value has this form when the composition is made: | ||
| 35 | ((LENGTH . COMPONENTS) . MODIFICATION-FUNC) | ||
| 36 | then turns to this form: | ||
| 37 | (COMPOSITION-ID . (LENGTH COMPONENTS-VEC . MODIFICATION-FUNC)) | ||
| 38 | when the composition is registered in composition_hash_table and | ||
| 39 | composition_table. These rather peculiar structures were designed | ||
| 40 | to make it easy to distinguish them quickly (we can do that by | ||
| 41 | checking only the first element) and to extract LENGTH (from the | ||
| 42 | former form) and COMPOSITION-ID (from the latter form). | ||
| 43 | |||
| 44 | We register a composition when it is displayed, or when the width | ||
| 45 | is required (for instance, to calculate columns). | ||
| 46 | |||
| 47 | LENGTH -- Length of the composition. This information is used to | ||
| 48 | check the validity of the composition. | ||
| 49 | |||
| 50 | COMPONENTS -- Character, string, vector, list, or nil. | ||
| 51 | |||
| 52 | If it is nil, characters in the text are composed relatively | ||
| 53 | according to their metrics in font glyphs. | ||
| 54 | |||
| 55 | If it is a character or a string, the character or characters | ||
| 56 | in the string are composed relatively. | ||
| 57 | |||
| 58 | If it is a vector or list of integers, the element is a | ||
| 59 | character or an encoded composition rule. The characters are | ||
| 60 | composed according to the rules. (2N)th elements are | ||
| 61 | characters to be composed and (2N+1)th elements are | ||
| 62 | composition rules to tell how to compose (2N+2)th element with | ||
| 63 | the previously composed 2N glyphs. | ||
| 64 | |||
| 65 | COMPONENTS-VEC -- Vector of integers. In relative composition, the | ||
| 66 | elements are characters to be composed. In rule-base | ||
| 67 | composition, the elements are characters or encoded | ||
| 68 | composition rules. | ||
| 69 | |||
| 70 | MODIFICATION-FUNC -- If non nil, it is a function to call when the | ||
| 71 | composition gets invalid after a modification in a buffer. If | ||
| 72 | it is nil, a function in `composition-function-table' of the | ||
| 73 | first character in the sequence is called. | ||
| 74 | |||
| 75 | COMPOSITION-ID --Identification number of the composition. It is | ||
| 76 | used as an index to composition_table for the composition. | ||
| 77 | |||
| 78 | When Emacs has to display a composition or has to know its | ||
| 79 | displaying width, the function get_composition_id is called. It | ||
| 80 | returns COMPOSITION-ID so that the caller can access the | ||
| 81 | information about the composition through composition_table. If a | ||
| 82 | COMPOSITION-ID has not yet been assigned to the composition, | ||
| 83 | get_composition_id checks the validity of `composition' property, | ||
| 84 | and, if valid, assigns a new ID, registers the information in | ||
| 85 | composition_hash_table and composition_table, and changes the form | ||
| 86 | of the property value. If the property is invalid, return -1 | ||
| 87 | without changing the property value. | ||
| 88 | |||
| 89 | We use two tables to keep information about composition; | ||
| 90 | composition_hash_table and composition_table. | ||
| 91 | |||
| 92 | The former is a hash table in which keys are COMPONENTS-VECs and | ||
| 93 | values are the corresponding COMPOSITION-IDs. This hash table is | ||
| 94 | weak, but as each key (COMPONENTS-VEC) is also kept as a value of | ||
| 95 | `composition' property, it won't be collected as garbage until all | ||
| 96 | text that have the same COMPONENTS-VEC are deleted. | ||
| 97 | |||
| 98 | The latter is a table of pointers to `struct composition' indexed | ||
| 99 | by COMPOSITION-ID. This structure keep the other information (see | ||
| 100 | composite.h). | ||
| 101 | |||
| 102 | In general, a text property holds information about individual | ||
| 103 | characters. But, a `composition' property holds information about | ||
| 104 | a sequence of characters (in this sense, it is like `intangible' | ||
| 105 | property). That means that we should not share the property value | ||
| 106 | in adjacent compositions we can't distinguish them if they have the | ||
| 107 | same property. So, after any changes, we call | ||
| 108 | `update_compositions' and change a property of one of adjacent | ||
| 109 | compositions to a copy of it. This function also runs a proper | ||
| 110 | composition modification function to make a composition that gets | ||
| 111 | invalid by the change valid again. | ||
| 112 | |||
| 113 | As a value of `composition' property holds information about a | ||
| 114 | specific range of text, the value gets invalid if we change the | ||
| 115 | text in the range. We treat `composition' property always | ||
| 116 | rear-nonsticky (currently by setting default-text-properties to | ||
| 117 | (rear-nonsticky (composition))) and we never make properties of | ||
| 118 | adjacent compositions identical. Thus, any such changes make the | ||
| 119 | range just shorter. So, we can check the validity of `composition' | ||
| 120 | property by comparing LENGTH information with the actual length of | ||
| 121 | the composition. | ||
| 122 | |||
| 123 | */ | ||
| 124 | |||
| 125 | |||
| 126 | Lisp_Object Qcomposition; | ||
| 127 | |||
| 128 | /* Table of pointers to the structure `composition' indexed by | ||
| 129 | COMPOSITION-ID. This structure is for storing information about | ||
| 130 | each composition except for COMPONENTS-VEC. */ | ||
| 131 | struct composition **composition_table; | ||
| 132 | |||
| 133 | /* The current size of `composition_table'. */ | ||
| 134 | static int composition_table_size; | ||
| 135 | |||
| 136 | /* Number of compositions currently made. */ | ||
| 137 | int n_compositions; | ||
| 138 | |||
| 139 | /* Hash table for compositions. The key is COMPONENTS-VEC of | ||
| 140 | `composition' property. The value is the corresponding | ||
| 141 | COMPOSITION-ID. */ | ||
| 142 | Lisp_Object composition_hash_table; | ||
| 143 | |||
| 144 | /* Function to call to adjust composition. */ | ||
| 145 | Lisp_Object Vcompose_chars_after_function; | ||
| 146 | |||
| 147 | /* Temporary variable used in macros COMPOSITION_XXX. */ | ||
| 148 | Lisp_Object composition_temp; | ||
| 149 | |||
| 150 | /* Return how many columns C will occupy on the screen. It always | ||
| 151 | returns 1 for control characters and 8-bit characters because those | ||
| 152 | are just ignored in a composition. */ | ||
| 153 | #define CHAR_WIDTH(c) \ | ||
| 154 | (SINGLE_BYTE_CHAR_P (c) ? 1 : CHARSET_WIDTH (CHAR_CHARSET (c))) | ||
| 155 | |||
| 156 | /* The following macros for hash table are copied from fns.c. */ | ||
| 157 | /* Return the contents of vector V at index IDX. */ | ||
| 158 | #define AREF(V, IDX) XVECTOR (V)->contents[IDX] | ||
| 159 | /* Value is the key part of entry IDX in hash table H. */ | ||
| 160 | #define HASH_KEY(H, IDX) AREF ((H)->key_and_value, 2 * (IDX)) | ||
| 161 | /* Value is the value part of entry IDX in hash table H. */ | ||
| 162 | #define HASH_VALUE(H, IDX) AREF ((H)->key_and_value, 2 * (IDX) + 1) | ||
| 163 | |||
| 164 | /* Return COMPOSITION-ID of a composition at buffer position | ||
| 165 | CHARPOS/BYTEPOS and length NCHARS. The `composition' property of | ||
| 166 | the sequence is PROP. STRING, if non-nil, is a string that | ||
| 167 | contains the composition instead of the current buffer. | ||
| 168 | |||
| 169 | If the composition is invalid, return -1. */ | ||
| 170 | |||
| 171 | int | ||
| 172 | get_composition_id (charpos, bytepos, nchars, prop, string) | ||
| 173 | int charpos, bytepos, nchars; | ||
| 174 | Lisp_Object prop, string; | ||
| 175 | { | ||
| 176 | Lisp_Object id, length, components, key, *key_contents; | ||
| 177 | int glyph_len; | ||
| 178 | struct Lisp_Hash_Table *hash_table = XHASH_TABLE (composition_hash_table); | ||
| 179 | int hash_index; | ||
| 180 | unsigned hash_code; | ||
| 181 | struct composition *cmp; | ||
| 182 | int i, ch; | ||
| 183 | |||
| 184 | /* PROP should be | ||
| 185 | Form-A: ((LENGTH . COMPONENTS) . MODIFICATION-FUNC) | ||
| 186 | or | ||
| 187 | Form-B: (COMPOSITION-ID . (LENGTH COMPONENTS-VEC . MODIFICATION-FUNC)) | ||
| 188 | */ | ||
| 189 | if (nchars == 0 || !CONSP (prop)) | ||
| 190 | goto invalid_composition; | ||
| 191 | |||
| 192 | id = XCAR (prop); | ||
| 193 | if (INTEGERP (id)) | ||
| 194 | { | ||
| 195 | /* PROP should be Form-B. */ | ||
| 196 | if (XINT (id) < 0 || XINT (id) >= n_compositions) | ||
| 197 | goto invalid_composition; | ||
| 198 | return XINT (id); | ||
| 199 | } | ||
| 200 | |||
| 201 | /* PROP should be Form-A. | ||
| 202 | Thus, ID should be (LENGTH . COMPONENTS). */ | ||
| 203 | if (!CONSP (id)) | ||
| 204 | goto invalid_composition; | ||
| 205 | length = XCAR (id); | ||
| 206 | if (!INTEGERP (length) || XINT (length) != nchars) | ||
| 207 | goto invalid_composition; | ||
| 208 | |||
| 209 | components = XCDR (id); | ||
| 210 | |||
| 211 | /* Check if the same composition has already been registered or not | ||
| 212 | by consulting composition_hash_table. The key for this table is | ||
| 213 | COMPONENTS (converted to a vector COMPONENTS-VEC) or, if it is | ||
| 214 | nil, vector of characters in the composition range. */ | ||
| 215 | if (INTEGERP (components)) | ||
| 216 | key = Fmake_vector (make_number (1), components); | ||
| 217 | else if (STRINGP (components) || CONSP (components)) | ||
| 218 | key = Fvconcat (1, &components); | ||
| 219 | else if (VECTORP (components)) | ||
| 220 | key = components; | ||
| 221 | else if (NILP (components)) | ||
| 222 | { | ||
| 223 | key = Fmake_vector (make_number (nchars), Qnil); | ||
| 224 | if (STRINGP (string)) | ||
| 225 | for (i = 0; i < nchars; i++) | ||
| 226 | { | ||
| 227 | FETCH_STRING_CHAR_ADVANCE (ch, string, charpos, bytepos); | ||
| 228 | XVECTOR (key)->contents[i] = make_number (ch); | ||
| 229 | } | ||
| 230 | else | ||
| 231 | for (i = 0; i < nchars; i++) | ||
| 232 | { | ||
| 233 | FETCH_CHAR_ADVANCE (ch, charpos, bytepos); | ||
| 234 | XVECTOR (key)->contents[i] = make_number (ch); | ||
| 235 | } | ||
| 236 | } | ||
| 237 | else | ||
| 238 | goto invalid_composition; | ||
| 239 | |||
| 240 | hash_index = hash_lookup (hash_table, key, &hash_code); | ||
| 241 | if (hash_index >= 0) | ||
| 242 | { | ||
| 243 | /* We have already registered the same composition. Change PROP | ||
| 244 | from Form-A above to Form-B while replacing COMPONENTS with | ||
| 245 | COMPONENTS-VEC stored in the hash table. We can directly | ||
| 246 | modify the cons cell of PROP because it is not shared. */ | ||
| 247 | key = HASH_KEY (hash_table, hash_index); | ||
| 248 | id = HASH_VALUE (hash_table, hash_index); | ||
| 249 | XCAR (prop) = id; | ||
| 250 | XCDR (prop) = Fcons (make_number (nchars), Fcons (key, XCDR (prop))); | ||
| 251 | return XINT (id); | ||
| 252 | } | ||
| 253 | |||
| 254 | /* This composition is a new one. We must register it. */ | ||
| 255 | |||
| 256 | /* Check if we have sufficient memory to store this information. */ | ||
| 257 | if (composition_table_size == 0) | ||
| 258 | { | ||
| 259 | composition_table_size = 256; | ||
| 260 | composition_table | ||
| 261 | = (struct composition **) xmalloc (sizeof (composition_table[0]) | ||
| 262 | * composition_table_size); | ||
| 263 | } | ||
| 264 | else if (composition_table_size <= n_compositions) | ||
| 265 | { | ||
| 266 | composition_table_size += 256; | ||
| 267 | composition_table | ||
| 268 | = (struct composition **) xrealloc (composition_table, | ||
| 269 | sizeof (composition_table[0]) | ||
| 270 | * composition_table_size); | ||
| 271 | } | ||
| 272 | |||
| 273 | key_contents = XVECTOR (key)->contents; | ||
| 274 | |||
| 275 | /* Check if the contents of COMPONENTS are valid if COMPONENTS is a | ||
| 276 | vector or a list. It should be a sequence of: | ||
| 277 | char1 rule1 char2 rule2 char3 ... ruleN charN+1 */ | ||
| 278 | if (VECTORP (components) || CONSP (components)) | ||
| 279 | { | ||
| 280 | int len = XVECTOR (key)->size; | ||
| 281 | |||
| 282 | /* The number of elements should be odd. */ | ||
| 283 | if ((len % 2) == 0) | ||
| 284 | goto invalid_composition; | ||
| 285 | /* All elements should be integers (character or encoded | ||
| 286 | composition rule). */ | ||
| 287 | for (i = 0; i < len; i++) | ||
| 288 | { | ||
| 289 | if (!INTEGERP (key_contents[i])) | ||
| 290 | goto invalid_composition; | ||
| 291 | } | ||
| 292 | } | ||
| 293 | |||
| 294 | /* Change PROP from Form-A above to Form-B. We can directly modify | ||
| 295 | the cons cell of PROP because it is not shared. */ | ||
| 296 | XSETFASTINT (id, n_compositions); | ||
| 297 | XCAR (prop) = id; | ||
| 298 | XCDR (prop) = Fcons (make_number (nchars), Fcons (key, XCDR (prop))); | ||
| 299 | |||
| 300 | /* Register the composition in composition_hash_table. */ | ||
| 301 | hash_index = hash_put (hash_table, key, id, hash_code); | ||
| 302 | |||
| 303 | /* Register the composition in composition_table. */ | ||
| 304 | cmp = (struct composition *) xmalloc (sizeof (struct composition)); | ||
| 305 | |||
| 306 | cmp->method = (NILP (components) | ||
| 307 | ? COMPOSITION_RELATIVE | ||
| 308 | : ((INTEGERP (components) || STRINGP (components)) | ||
| 309 | ? COMPOSITION_WITH_ALTCHARS | ||
| 310 | : COMPOSITION_WITH_RULE_ALTCHARS)); | ||
| 311 | cmp->hash_index = hash_index; | ||
| 312 | glyph_len = (cmp->method == COMPOSITION_WITH_RULE_ALTCHARS | ||
| 313 | ? (XVECTOR (key)->size + 1) / 2 | ||
| 314 | : XVECTOR (key)->size); | ||
| 315 | cmp->glyph_len = glyph_len; | ||
| 316 | cmp->offsets = (short *) xmalloc (sizeof (short) * glyph_len * 2); | ||
| 317 | cmp->font = NULL; | ||
| 318 | |||
| 319 | /* Calculate the width of overall glyphs of the composition. */ | ||
| 320 | if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS) | ||
| 321 | { | ||
| 322 | /* Relative composition. */ | ||
| 323 | cmp->width = 0; | ||
| 324 | for (i = 0; i < glyph_len; i++) | ||
| 325 | { | ||
| 326 | int this_width; | ||
| 327 | ch = XINT (key_contents[i]); | ||
| 328 | this_width = CHAR_WIDTH (ch); | ||
| 329 | if (cmp->width < this_width) | ||
| 330 | cmp->width = this_width; | ||
| 331 | } | ||
| 332 | } | ||
| 333 | else | ||
| 334 | { | ||
| 335 | /* Rule-base composition. */ | ||
| 336 | float leftmost = 0.0, rightmost; | ||
| 337 | |||
| 338 | ch = XINT (key_contents[0]); | ||
| 339 | rightmost = CHAR_WIDTH (ch); | ||
| 340 | |||
| 341 | for (i = 1; i < glyph_len; i += 2) | ||
| 342 | { | ||
| 343 | int rule, gref, nref; | ||
| 344 | int this_width; | ||
| 345 | float this_left; | ||
| 346 | |||
| 347 | rule = XINT (key_contents[i]); | ||
| 348 | ch = XINT (key_contents[i + 1]); | ||
| 349 | this_width = CHAR_WIDTH (ch); | ||
| 350 | |||
| 351 | /* A composition rule is specified by an integer value | ||
| 352 | that encodes global and new reference points (GREF and | ||
| 353 | NREF). GREF and NREF are specified by numbers as | ||
| 354 | below: | ||
| 355 | 0---1---2 -- ascent | ||
| 356 | | | | ||
| 357 | | | | ||
| 358 | | | | ||
| 359 | 9--10--11 -- center | ||
| 360 | | | | ||
| 361 | ---3---4---5--- baseline | ||
| 362 | | | | ||
| 363 | 6---7---8 -- descent | ||
| 364 | */ | ||
| 365 | COMPOSITION_DECODE_RULE (rule, gref, nref); | ||
| 366 | this_left = (leftmost | ||
| 367 | + (gref % 3) * (rightmost - leftmost) / 2.0 | ||
| 368 | - (nref % 3) * this_width / 2.0); | ||
| 369 | |||
| 370 | if (this_left < leftmost) | ||
| 371 | leftmost = this_left; | ||
| 372 | if (this_left + this_width > rightmost) | ||
| 373 | rightmost = this_left + this_width; | ||
| 374 | } | ||
| 375 | |||
| 376 | cmp->width = rightmost - leftmost; | ||
| 377 | if (cmp->width < (rightmost - leftmost)) | ||
| 378 | /* To get a ceiling integer value. */ | ||
| 379 | cmp->width++; | ||
| 380 | } | ||
| 381 | |||
| 382 | composition_table[n_compositions] = cmp; | ||
| 383 | |||
| 384 | return n_compositions++; | ||
| 385 | |||
| 386 | invalid_composition: | ||
| 387 | /* Would it be better to remove this `composition' property? */ | ||
| 388 | return -1; | ||
| 389 | } | ||
| 390 | |||
| 391 | |||
| 392 | /* Find a composition at or nearest to position POS of OBJECT (buffer | ||
| 393 | or string). | ||
| 394 | |||
| 395 | OBJECT defaults to the current buffer. If there's a composition at | ||
| 396 | POS, set *START and *END to the start and end of the sequence, | ||
| 397 | *PROP to the `composition' property, and return 1. | ||
| 398 | |||
| 399 | If there's no composition at POS and LIMIT is negative, return 0. | ||
| 400 | |||
| 401 | Otherwise, search for a composition forward (LIMIT > POS) or | ||
| 402 | backward (LIMIT < POS). In this case, LIMIT bounds the search. | ||
| 403 | |||
| 404 | If a composition is found, set *START, *END, and *PROP as above, | ||
| 405 | and return 1, else return 0. | ||
| 406 | |||
| 407 | This doesn't check the validity of composition. */ | ||
| 408 | |||
| 409 | int | ||
| 410 | find_composition (pos, limit, start, end, prop, object) | ||
| 411 | int pos, limit, *start, *end; | ||
| 412 | Lisp_Object *prop, object; | ||
| 413 | { | ||
| 414 | Lisp_Object val; | ||
| 415 | |||
| 416 | if (get_property_and_range (pos, Qcomposition, prop, start, end, object)) | ||
| 417 | return 1; | ||
| 418 | |||
| 419 | if (limit < 0 || limit == pos) | ||
| 420 | return 0; | ||
| 421 | |||
| 422 | if (limit > pos) /* search forward */ | ||
| 423 | val = Fnext_single_property_change (make_number (pos), Qcomposition, | ||
| 424 | object, make_number (limit)); | ||
| 425 | else /* search backward */ | ||
| 426 | val = Fprevious_single_property_change (make_number (pos), Qcomposition, | ||
| 427 | object, make_number (limit)); | ||
| 428 | pos = XINT (val); | ||
| 429 | if (pos == limit) | ||
| 430 | return 0; | ||
| 431 | get_property_and_range (pos, Qcomposition, prop, start, end, object); | ||
| 432 | return 1; | ||
| 433 | } | ||
| 434 | |||
| 435 | /* Run a proper function to adjust the composition sitting between | ||
| 436 | FROM and TO with property PROP. */ | ||
| 437 | |||
| 438 | static void | ||
| 439 | run_composition_function (from, to, prop) | ||
| 440 | int from, to; | ||
| 441 | Lisp_Object prop; | ||
| 442 | { | ||
| 443 | Lisp_Object func, val; | ||
| 444 | int start, end; | ||
| 445 | |||
| 446 | func = COMPOSITION_MODIFICATION_FUNC (prop); | ||
| 447 | /* If an invalid composition precedes or follows, try to make them | ||
| 448 | valid too. */ | ||
| 449 | if (from > BEGV | ||
| 450 | && find_composition (from - 1, -1, &start, &end, &prop, Qnil) | ||
| 451 | && !COMPOSITION_VALID_P (start, end, prop)) | ||
| 452 | from = start; | ||
| 453 | if (to < ZV | ||
| 454 | && find_composition (to, -1, &start, &end, &prop, Qnil) | ||
| 455 | && !COMPOSITION_VALID_P (start, end, prop)) | ||
| 456 | to = end; | ||
| 457 | if (!NILP (func)) | ||
| 458 | call2 (func, make_number (from), make_number (to)); | ||
| 459 | else if (Ffboundp (Vcompose_chars_after_function)) | ||
| 460 | call2 (Vcompose_chars_after_function, | ||
| 461 | make_number (from), make_number (to)); | ||
| 462 | } | ||
| 463 | |||
| 464 | /* Make invalid compositions adjacent to or inside FROM and TO valid. | ||
| 465 | CHECK_MASK is bitwise `or' of mask bits defined by macros | ||
| 466 | CHECK_XXX (see the comment in composite.h). | ||
| 467 | |||
| 468 | This function is called when a buffer text is changed. If the | ||
| 469 | change is deletion, FROM == TO. Otherwise, FROM < TO. */ | ||
| 470 | |||
| 471 | void | ||
| 472 | update_compositions (from, to, check_mask) | ||
| 473 | int from, to; | ||
| 474 | { | ||
| 475 | Lisp_Object prop, hook; | ||
| 476 | int start, end; | ||
| 477 | |||
| 478 | if (check_mask & CHECK_HEAD) | ||
| 479 | { | ||
| 480 | /* FROM should be at composition boundary. But, insertion or | ||
| 481 | deletion will make two compositions adjacent and | ||
| 482 | indistinguishable when they have same (eq) property. To | ||
| 483 | avoid it, in such a case, we change the property of the | ||
| 484 | latter to the copy of it. */ | ||
| 485 | if (from > BEGV | ||
| 486 | && find_composition (from - 1, -1, &start, &end, &prop, Qnil)) | ||
| 487 | { | ||
| 488 | if (from < end) | ||
| 489 | Fput_text_property (make_number (from), make_number (end), | ||
| 490 | Qcomposition, | ||
| 491 | Fcons (XCAR (prop), XCDR (prop)), Qnil); | ||
| 492 | run_composition_function (start, end, prop); | ||
| 493 | from = end; | ||
| 494 | } | ||
| 495 | else if (from < end | ||
| 496 | && find_composition (from, -1, &start, &from, &prop, Qnil)) | ||
| 497 | run_composition_function (start, from, prop); | ||
| 498 | } | ||
| 499 | |||
| 500 | if (check_mask & CHECK_INSIDE) | ||
| 501 | { | ||
| 502 | /* In this case, we are sure that (check & CHECK_TAIL) is also | ||
| 503 | nonzero. Thus, here we should check only compositions before | ||
| 504 | (to - 1). */ | ||
| 505 | while (from < to - 1 | ||
| 506 | && find_composition (from, to, &start, &from, &prop, Qnil) | ||
| 507 | && from < to - 1) | ||
| 508 | run_composition_function (start, from, prop); | ||
| 509 | } | ||
| 510 | |||
| 511 | if (check_mask & CHECK_TAIL) | ||
| 512 | { | ||
| 513 | if (from < to | ||
| 514 | && find_composition (to - 1, -1, &start, &end, &prop, Qnil)) | ||
| 515 | { | ||
| 516 | /* TO should be also at composition boundary. But, | ||
| 517 | insertion or deletion will make two compositions adjacent | ||
| 518 | and indistinguishable when they have same (eq) property. | ||
| 519 | To avoid it, in such a case, we change the property of | ||
| 520 | the former to the copy of it. */ | ||
| 521 | if (to < end) | ||
| 522 | Fput_text_property (make_number (start), make_number (to), | ||
| 523 | Qcomposition, | ||
| 524 | Fcons (XCAR (prop), XCDR (prop)), Qnil); | ||
| 525 | run_composition_function (start, end, prop); | ||
| 526 | } | ||
| 527 | else if (to < ZV | ||
| 528 | && find_composition (to, -1, &start, &end, &prop, Qnil)) | ||
| 529 | run_composition_function (start, end, prop); | ||
| 530 | } | ||
| 531 | } | ||
| 532 | |||
| 533 | /* Make text in the region between START and END a composition that | ||
| 534 | has COMPONENTS and MODIFICATION-FUNC. | ||
| 535 | |||
| 536 | If STRING is non-nil, then operate on characters contained between | ||
| 537 | indices START and END in STRING. */ | ||
| 538 | |||
| 539 | void | ||
| 540 | compose_text (start, end, components, modification_func, string) | ||
| 541 | int start, end; | ||
| 542 | Lisp_Object components, modification_func, string; | ||
| 543 | { | ||
| 544 | Lisp_Object prop; | ||
| 545 | |||
| 546 | prop = Fcons (Fcons (make_number (end - start), components), | ||
| 547 | modification_func); | ||
| 548 | Fput_text_property (make_number (start), make_number (end), | ||
| 549 | Qcomposition, prop, string); | ||
| 550 | } | ||
| 551 | |||
| 552 | |||
| 553 | /* Emacs Lisp APIs. */ | ||
| 554 | |||
| 555 | DEFUN ("compose-region-internal", Fcompose_region_internal, | ||
| 556 | Scompose_region_internal, 2, 4, 0, | ||
| 557 | "Internal use only.\n\ | ||
| 558 | \n\ | ||
| 559 | Compose text in the region between START and END.\n\ | ||
| 560 | Optional 3rd and 4th arguments are COMPONENTS and MODIFICATION-FUNC\n\ | ||
| 561 | for the composition. See `compose-region' for more detial.") | ||
| 562 | (start, end, components, mod_func) | ||
| 563 | Lisp_Object start, end, components, mod_func; | ||
| 564 | { | ||
| 565 | validate_region (&start, &end); | ||
| 566 | if (!NILP (components) | ||
| 567 | && !INTEGERP (components) | ||
| 568 | && !CONSP (components) | ||
| 569 | && !STRINGP (components)) | ||
| 570 | CHECK_VECTOR (components, 2); | ||
| 571 | |||
| 572 | compose_text (XINT (start), XINT (end), components, mod_func, Qnil); | ||
| 573 | return Qnil; | ||
| 574 | } | ||
| 575 | |||
| 576 | DEFUN ("compose-string-internal", Fcompose_string_internal, | ||
| 577 | Scompose_string_internal, 3, 5, 0, | ||
| 578 | "Internal use only.\n\ | ||
| 579 | \n\ | ||
| 580 | Compose text between indices START and END of STRING.\n\ | ||
| 581 | Optional 4th and 5th arguments are COMPONENTS and MODIFICATION-FUNC\n\ | ||
| 582 | for the composition. See `compose-string' for more detial.") | ||
| 583 | (string, start, end, components, mod_func) | ||
| 584 | Lisp_Object string, start, end, components, mod_func; | ||
| 585 | { | ||
| 586 | CHECK_STRING (string, 0); | ||
| 587 | CHECK_NUMBER (start, 1); | ||
| 588 | CHECK_NUMBER (end, 2); | ||
| 589 | |||
| 590 | if (XINT (start) < 0 || | ||
| 591 | XINT (start) > XINT (end) | ||
| 592 | || XINT (end) > XSTRING (string)->size) | ||
| 593 | args_out_of_range (start, end); | ||
| 594 | |||
| 595 | compose_text (XINT (start), XINT (end), components, mod_func, string); | ||
| 596 | return string; | ||
| 597 | } | ||
| 598 | |||
| 599 | DEFUN ("find-composition-internal", Ffind_composition_internal, | ||
| 600 | Sfind_composition_internal, 4, 4, 0, | ||
| 601 | "Internal use only.\n\ | ||
| 602 | \n\ | ||
| 603 | Return information about composition at or nearest to position POS.\n\ | ||
| 604 | See `find-composition' for more detail.") | ||
| 605 | (pos, limit, string, detail_p) | ||
| 606 | Lisp_Object pos, limit, string, detail_p; | ||
| 607 | { | ||
| 608 | Lisp_Object prop, tail; | ||
| 609 | int start, end; | ||
| 610 | int id; | ||
| 611 | |||
| 612 | CHECK_NUMBER_COERCE_MARKER (pos, 0); | ||
| 613 | start = XINT (pos); | ||
| 614 | if (!NILP (limit)) | ||
| 615 | { | ||
| 616 | CHECK_NUMBER_COERCE_MARKER (limit, 1); | ||
| 617 | end = XINT (limit); | ||
| 618 | } | ||
| 619 | else | ||
| 620 | end = -1; | ||
| 621 | if (!NILP (string)) | ||
| 622 | CHECK_STRING (string, 2); | ||
| 623 | |||
| 624 | if (!find_composition (start, end, &start, &end, &prop, string)) | ||
| 625 | return Qnil; | ||
| 626 | if (!COMPOSITION_VALID_P (start, end, prop)) | ||
| 627 | return Fcons (make_number (start), Fcons (make_number (end), | ||
| 628 | Fcons (Qnil, Qnil))); | ||
| 629 | if (NILP (detail_p)) | ||
| 630 | return Fcons (make_number (start), Fcons (make_number (end), | ||
| 631 | Fcons (Qt, Qnil))); | ||
| 632 | |||
| 633 | if (COMPOSITION_REGISTERD_P (prop)) | ||
| 634 | id = COMPOSITION_ID (prop); | ||
| 635 | else | ||
| 636 | { | ||
| 637 | int start_byte = (NILP (string) | ||
| 638 | ? CHAR_TO_BYTE (start) | ||
| 639 | : string_char_to_byte (string, start)); | ||
| 640 | id = get_composition_id (start, start_byte, end - start, prop, string); | ||
| 641 | } | ||
| 642 | |||
| 643 | if (id >= 0) | ||
| 644 | { | ||
| 645 | Lisp_Object components, relative_p, mod_func; | ||
| 646 | enum composition_method method = COMPOSITION_METHOD (prop); | ||
| 647 | int width = composition_table[id]->width; | ||
| 648 | |||
| 649 | components = Fcopy_sequence (COMPOSITION_COMPONENTS (prop)); | ||
| 650 | relative_p = (method == COMPOSITION_WITH_RULE_ALTCHARS | ||
| 651 | ? Qnil : Qt); | ||
| 652 | mod_func = COMPOSITION_MODIFICATION_FUNC (prop); | ||
| 653 | tail = Fcons (components, | ||
| 654 | Fcons (relative_p, | ||
| 655 | Fcons (mod_func, | ||
| 656 | Fcons (make_number (width), Qnil)))); | ||
| 657 | } | ||
| 658 | else | ||
| 659 | tail = Qnil; | ||
| 660 | |||
| 661 | return Fcons (make_number (start), Fcons (make_number (end), tail)); | ||
| 662 | } | ||
| 663 | |||
| 664 | |||
| 665 | void | ||
| 666 | syms_of_composite () | ||
| 667 | { | ||
| 668 | Qcomposition = intern ("composition"); | ||
| 669 | staticpro (&Qcomposition); | ||
| 670 | |||
| 671 | /* Make a hash table for composition. */ | ||
| 672 | { | ||
| 673 | Lisp_Object args[6], nargs; | ||
| 674 | extern Lisp_Object QCsize; | ||
| 675 | |||
| 676 | args[0] = QCtest; | ||
| 677 | args[1] = Qequal; | ||
| 678 | args[2] = QCweakness; | ||
| 679 | args[3] = Qnil; | ||
| 680 | args[4] = QCsize; | ||
| 681 | args[5] = make_number (311); | ||
| 682 | XSETINT (nargs, 6); | ||
| 683 | composition_hash_table = Fmake_hash_table (nargs, args); | ||
| 684 | staticpro (&composition_hash_table); | ||
| 685 | } | ||
| 686 | |||
| 687 | /* Text property `composition' should be nonsticky by default. */ | ||
| 688 | Vtext_property_default_nonsticky | ||
| 689 | = Fcons (Fcons (Qcomposition, Qt), Vtext_property_default_nonsticky); | ||
| 690 | |||
| 691 | DEFVAR_LISP ("compose-chars-after-function", &Vcompose_chars_after_function, | ||
| 692 | "Function to adjust composition of buffer text.\n\ | ||
| 693 | \n\ | ||
| 694 | This function is called after a text with `composition' property is\n\ | ||
| 695 | inserted or deleted to keep `composition' property of buffer text\n\ | ||
| 696 | valid.\n\ | ||
| 697 | \n\ | ||
| 698 | The function is called with two arguments FROM and TO. They specify\n\ | ||
| 699 | the range of text of which composition should be adjusted.\n\ | ||
| 700 | \n\ | ||
| 701 | The default value is the function `compose-chars-after'."); | ||
| 702 | Vcompose_chars_after_function = intern ("compose-chars-after"); | ||
| 703 | |||
| 704 | defsubr (&Scompose_region_internal); | ||
| 705 | defsubr (&Scompose_string_internal); | ||
| 706 | defsubr (&Sfind_composition_internal); | ||
| 707 | } | ||
diff --git a/src/composite.h b/src/composite.h new file mode 100644 index 00000000000..b0af3b7d4ee --- /dev/null +++ b/src/composite.h | |||
| @@ -0,0 +1,207 @@ | |||
| 1 | /* Header for composite sequence handler. | ||
| 2 | Copyright (C) 1999 Electrotechnical Laboratory, JAPAN. | ||
| 3 | Licensed to the Free Software Foundation. | ||
| 4 | |||
| 5 | This file is part of GNU Emacs. | ||
| 6 | |||
| 7 | GNU Emacs is free software; you can redistribute it and/or modify | ||
| 8 | it under the terms of the GNU General Public License as published by | ||
| 9 | the Free Software Foundation; either version 2, or (at your option) | ||
| 10 | any later version. | ||
| 11 | |||
| 12 | GNU Emacs is distributed in the hope that it will be useful, | ||
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 15 | GNU General Public License for more details. | ||
| 16 | |||
| 17 | You should have received a copy of the GNU General Public License | ||
| 18 | along with GNU Emacs; see the file COPYING. If not, write to | ||
| 19 | the Free Software Foundation, Inc., 59 Temple Place - Suite 330, | ||
| 20 | Boston, MA 02111-1307, USA. */ | ||
| 21 | |||
| 22 | #ifndef _COMPOSITE_H | ||
| 23 | #define _COMPOSITE_H | ||
| 24 | |||
| 25 | /* Methods to display a sequence of components a composition. */ | ||
| 26 | enum composition_method { | ||
| 27 | /* The first two are actually not methods, but used in code | ||
| 28 | conversion to specify the current composing status. */ | ||
| 29 | COMPOSITION_DISABLED, /* Never handle composition data */ | ||
| 30 | COMPOSITION_NO, /* Not processing composition data */ | ||
| 31 | /* Compose relatively without alternate characters. */ | ||
| 32 | COMPOSITION_RELATIVE, | ||
| 33 | /* Compose by specified composition rule. This is not used in Emacs | ||
| 34 | 21 but we need it to decode files saved in the older versions of | ||
| 35 | Emacs. */ | ||
| 36 | COMPOSITION_WITH_RULE, | ||
| 37 | /* Compose relatively with alternate characters. */ | ||
| 38 | COMPOSITION_WITH_ALTCHARS, | ||
| 39 | /* Compose by specified composition rule with alternate characters. */ | ||
| 40 | COMPOSITION_WITH_RULE_ALTCHARS | ||
| 41 | }; | ||
| 42 | |||
| 43 | /* Maximum number of compoments a single composition can have. */ | ||
| 44 | #define MAX_COMPOSITION_COMPONENTS 16 | ||
| 45 | |||
| 46 | /* These macros access information about a composition that | ||
| 47 | has `composition' property PROP. PROP is: | ||
| 48 | ((LENGTH . COMPONENTS) . MODIFICATION-FUNC) | ||
| 49 | or | ||
| 50 | (COMPOSITION-ID . (LENGTH COMPONENTS . MODIFICATION-FUNC)) | ||
| 51 | They don't check validity of PROP. */ | ||
| 52 | |||
| 53 | /* Temporary variable used only in the following macros. */ | ||
| 54 | extern Lisp_Object composition_temp; | ||
| 55 | |||
| 56 | /* Return 1 iff the composition is already registered. */ | ||
| 57 | #define COMPOSITION_REGISTERD_P(prop) INTEGERP (XCAR (prop)) | ||
| 58 | |||
| 59 | /* Return ID number of the already registered composition. */ | ||
| 60 | #define COMPOSITION_ID(prop) XINT (XCAR (prop)) | ||
| 61 | |||
| 62 | /* Return length of the composition. */ | ||
| 63 | #define COMPOSITION_LENGTH(prop) \ | ||
| 64 | (COMPOSITION_REGISTERD_P (prop) \ | ||
| 65 | ? XINT (XCAR (XCDR (prop))) \ | ||
| 66 | : XINT (XCAR (XCAR (prop)))) | ||
| 67 | |||
| 68 | /* Return components of the composition. */ | ||
| 69 | #define COMPOSITION_COMPONENTS(prop) \ | ||
| 70 | (COMPOSITION_REGISTERD_P (prop) \ | ||
| 71 | ? XCAR (XCDR (XCDR (prop))) \ | ||
| 72 | : XCDR (XCAR (prop))) | ||
| 73 | |||
| 74 | /* Return modification function of the composition. */ | ||
| 75 | #define COMPOSITION_MODIFICATION_FUNC(prop) \ | ||
| 76 | (COMPOSITION_REGISTERD_P (prop) \ | ||
| 77 | ? XCDR (XCDR (XCDR (prop))) \ | ||
| 78 | : XCDR (prop)) | ||
| 79 | |||
| 80 | /* Return the method of composition. */ | ||
| 81 | #define COMPOSITION_METHOD(prop) \ | ||
| 82 | (COMPOSITION_REGISTERD_P (prop) \ | ||
| 83 | ? composition_table[COMPOSITION_ID (prop)]->method \ | ||
| 84 | : (composition_temp = XCDR (XCAR (prop)), \ | ||
| 85 | (NILP (composition_temp) \ | ||
| 86 | ? COMPOSITION_RELATIVE \ | ||
| 87 | : ((INTEGERP (composition_temp) || STRINGP (composition_temp)) \ | ||
| 88 | ? COMPOSITION_WITH_ALTCHARS \ | ||
| 89 | : COMPOSITION_WITH_RULE_ALTCHARS)))) | ||
| 90 | |||
| 91 | /* Return 1 iff the composition is valid. It is valid if length of | ||
| 92 | the composition equals to (END - START). */ | ||
| 93 | #define COMPOSITION_VALID_P(start, end, prop) \ | ||
| 94 | (CONSP (prop) \ | ||
| 95 | && (COMPOSITION_REGISTERD_P (prop) \ | ||
| 96 | ? (COMPOSITION_ID (prop) >= 0 \ | ||
| 97 | && COMPOSITION_ID (prop) <= n_compositions \ | ||
| 98 | && CONSP (XCDR (prop))) \ | ||
| 99 | : (composition_temp = XCAR (prop), \ | ||
| 100 | (CONSP (composition_temp) \ | ||
| 101 | && (composition_temp = XCDR (composition_temp), \ | ||
| 102 | (NILP (composition_temp) || STRINGP (composition_temp) \ | ||
| 103 | || VECTORP (composition_temp) || CONSP (composition_temp))))))\ | ||
| 104 | && (end - start) == COMPOSITION_LENGTH (prop)) | ||
| 105 | |||
| 106 | /* Return the Nth glyph of composition specified by CMP. CMP is a | ||
| 107 | pointer to `struct composition'. */ | ||
| 108 | #define COMPOSITION_GLYPH(cmp, n) \ | ||
| 109 | XINT (XVECTOR (XVECTOR (XHASH_TABLE (composition_hash_table) \ | ||
| 110 | ->key_and_value) \ | ||
| 111 | ->contents[cmp->hash_index * 2]) \ | ||
| 112 | ->contents[cmp->method == COMPOSITION_WITH_RULE_ALTCHARS \ | ||
| 113 | ? (n) * 2 : (n)]) | ||
| 114 | |||
| 115 | /* Return the encoded composition rule to compose the Nth glyph of | ||
| 116 | rule-base composition specified by CMP. CMP is a pointer to | ||
| 117 | `struct composition'. */ | ||
| 118 | #define COMPOSITION_RULE(cmp, n) \ | ||
| 119 | XINT (XVECTOR (XVECTOR (XHASH_TABLE (composition_hash_table) \ | ||
| 120 | ->key_and_value) \ | ||
| 121 | ->contents[cmp->hash_index * 2]) \ | ||
| 122 | ->contents[(n) * 2 - 1]) | ||
| 123 | |||
| 124 | /* Decode encoded composition rule RULE_CODE into GREF (global | ||
| 125 | reference point code) and NREF (new reference point code). Don't | ||
| 126 | check RULE_CODE, always set GREF and NREF to valid values. */ | ||
| 127 | #define COMPOSITION_DECODE_RULE(rule_code, gref, nref) \ | ||
| 128 | do { \ | ||
| 129 | gref = (rule_code) / 12; \ | ||
| 130 | if (gref > 12) gref = 11; \ | ||
| 131 | nref = (rule_code) % 12; \ | ||
| 132 | } while (0) | ||
| 133 | |||
| 134 | /* Return encoded composition rule for the pair of global reference | ||
| 135 | point GREF and new reference point NREF. If arguments are invalid, | ||
| 136 | return -1. */ | ||
| 137 | #define COMPOSITION_ENCODE_RULE(gref, nref) \ | ||
| 138 | ((unsigned) (gref) < 12 && (unsigned) (nref) < 12 \ | ||
| 139 | ? (gref) * 12 + (nref) : -1) | ||
| 140 | |||
| 141 | /* Data structure that records information about a composition | ||
| 142 | currently used in some buffers or strings. | ||
| 143 | |||
| 144 | When a composition is assigned an ID number (by | ||
| 145 | get_composition_id), this structure is allocated for the | ||
| 146 | composition and linked in composition_table[ID]. | ||
| 147 | |||
| 148 | Identical compositions appearing at different places have the same | ||
| 149 | ID, and thus share the same instance of this structure. */ | ||
| 150 | |||
| 151 | struct composition { | ||
| 152 | /* How many columns the overall glyphs occupy on the screen. This | ||
| 153 | gives an approximate value for column calculation in | ||
| 154 | Fcurrent_column, and etc. */ | ||
| 155 | unsigned char width; | ||
| 156 | |||
| 157 | /* Number of glyphs of the composition components. */ | ||
| 158 | unsigned char glyph_len; | ||
| 159 | |||
| 160 | /* Method of the composition. */ | ||
| 161 | enum composition_method method; | ||
| 162 | |||
| 163 | /* Index to the composition hash table. */ | ||
| 164 | int hash_index; | ||
| 165 | |||
| 166 | /* For which font we have calculated the remaining members. The | ||
| 167 | actual type is device dependent. */ | ||
| 168 | void *font; | ||
| 169 | |||
| 170 | /* Pointer to an array of x-offset and y-offset (by pixels) of | ||
| 171 | glyphs. This points to a sufficient memory space (sizeof (int) * | ||
| 172 | glyph_len * 2) that is allocated when the composition is | ||
| 173 | registered in composition_table. X-offset and Y-offset of Nth | ||
| 174 | glyph are (2N)th and (2N+1)th elements respectively. */ | ||
| 175 | short *offsets; | ||
| 176 | |||
| 177 | /* Width, ascent, and descent pixels of the composition. */ | ||
| 178 | short pixel_width, ascent, descent; | ||
| 179 | |||
| 180 | }; | ||
| 181 | |||
| 182 | /* Table of pointers to the structure `composition' indexed by | ||
| 183 | COMPOSITION-ID. */ | ||
| 184 | extern struct composition **composition_table; | ||
| 185 | /* Number of the currently registered compositions. */ | ||
| 186 | extern int n_compositions; | ||
| 187 | |||
| 188 | /* Mask bits for CHECK_MASK arg to update_compositions. | ||
| 189 | For a change in the region FROM and TO, check compositions ... */ | ||
| 190 | #define CHECK_HEAD 1 /* adjacent to FROM */ | ||
| 191 | #define CHECK_TAIL 2 /* adjacent to TO */ | ||
| 192 | #define CHECK_INSIDE 4 /* between FROM and TO */ | ||
| 193 | #define CHECK_BORDER (CHECK_HEAD | CHECK_TAIL) | ||
| 194 | #define CHECK_ALL (CHECK_BORDER | CHECK_INSIDE) | ||
| 195 | |||
| 196 | extern Lisp_Object Qcomposition; | ||
| 197 | extern Lisp_Object composition_hash_table; | ||
| 198 | |||
| 199 | extern int get_composition_id P_ ((int, int, int, Lisp_Object, Lisp_Object)); | ||
| 200 | extern int find_composition P_ ((int, int, int *, int *, Lisp_Object *, | ||
| 201 | Lisp_Object)); | ||
| 202 | extern void update_compositions P_ ((int, int, int)); | ||
| 203 | extern void compose_region P_ ((int, int, Lisp_Object, Lisp_Object, | ||
| 204 | Lisp_Object)); | ||
| 205 | extern void syms_of_composition P_ (()); | ||
| 206 | |||
| 207 | #endif /* not _COMPOSITE_H */ | ||