Merge from mainline.
[bpt/emacs.git] / src / composite.h
1 /* Header for composite sequence handler.
2 Copyright (C) 2001-2013 Free Software Foundation, Inc.
3 Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
4 National Institute of Advanced Industrial Science and Technology (AIST)
5 Registration Number H14PRO021
6 Copyright (C) 2003, 2006
7 National Institute of Advanced Industrial Science and Technology (AIST)
8 Registration Number H13PRO009
9
10 This file is part of GNU Emacs.
11
12 GNU Emacs is free software: you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation, either version 3 of the License, or
15 (at your option) any later version.
16
17 GNU Emacs is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
24
25 #ifndef EMACS_COMPOSITE_H
26 #define EMACS_COMPOSITE_H
27
28 INLINE_HEADER_BEGIN
29 #ifndef COMPOSITE_INLINE
30 # define COMPOSITE_INLINE INLINE
31 #endif
32
33 /* Methods to display a sequence of components of a composition. */
34 enum composition_method {
35 /* Compose relatively without alternate characters. */
36 COMPOSITION_RELATIVE,
37 /* Compose by specified composition rules. This is not used in
38 Emacs 21 but we need it to decode files saved in the older
39 versions of Emacs. */
40 COMPOSITION_WITH_RULE,
41 /* Compose relatively with alternate characters. */
42 COMPOSITION_WITH_ALTCHARS,
43 /* Compose by specified composition rules with alternate characters. */
44 COMPOSITION_WITH_RULE_ALTCHARS,
45 /* This is not a method. */
46 COMPOSITION_NO
47 };
48
49 /* Maximum number of components a single composition can have. */
50 #define MAX_COMPOSITION_COMPONENTS 16
51
52 /* These operations access information about a composition that
53 has `composition' property PROP. PROP is:
54 ((LENGTH . COMPONENTS) . MODIFICATION-FUNC)
55 or
56 (COMPOSITION-ID . (LENGTH COMPONENTS . MODIFICATION-FUNC))
57 They don't check validity of PROP. */
58
59 /* Return true if PROP is already registered. */
60 COMPOSITE_INLINE bool
61 composition_registered_p (Lisp_Object prop)
62 {
63 return INTEGERP (XCAR (prop));
64 }
65
66 /* Return ID number of the already registered composition. */
67 #define COMPOSITION_ID(prop) XINT (XCAR (prop))
68
69 /* Return length of the composition. */
70 #define COMPOSITION_LENGTH(prop) \
71 (composition_registered_p (prop) \
72 ? XINT (XCAR (XCDR (prop))) \
73 : XINT (XCAR (XCAR (prop))))
74
75 /* Return components of the composition. */
76 #define COMPOSITION_COMPONENTS(prop) \
77 (composition_registered_p (prop) \
78 ? XCAR (XCDR (XCDR (prop))) \
79 : XCDR (XCAR (prop)))
80
81 /* Return modification function of the composition. */
82 #define COMPOSITION_MODIFICATION_FUNC(prop) \
83 (composition_registered_p (prop) \
84 ? XCDR (XCDR (XCDR (prop))) \
85 : CONSP (prop) ? XCDR (prop) : Qnil)
86
87 /* Return the Nth glyph of composition specified by CMP. CMP is a
88 pointer to `struct composition'. */
89 #define COMPOSITION_GLYPH(cmp, n) \
90 XINT (XVECTOR (XVECTOR (XHASH_TABLE (composition_hash_table) \
91 ->key_and_value) \
92 ->contents[cmp->hash_index * 2]) \
93 ->contents[cmp->method == COMPOSITION_WITH_RULE_ALTCHARS \
94 ? (n) * 2 : (n)])
95
96 /* Return the encoded composition rule to compose the Nth glyph of
97 rule-base composition specified by CMP. CMP is a pointer to
98 `struct composition'. */
99 #define COMPOSITION_RULE(cmp, n) \
100 XINT (XVECTOR (XVECTOR (XHASH_TABLE (composition_hash_table) \
101 ->key_and_value) \
102 ->contents[cmp->hash_index * 2]) \
103 ->contents[(n) * 2 - 1])
104
105 /* Decode encoded composition rule RULE_CODE into GREF (global
106 reference point code), NREF (new ref. point code). Don't check RULE_CODE;
107 always set GREF and NREF to valid values. By side effect,
108 RULE_CODE is modified. */
109
110 #define COMPOSITION_DECODE_REFS(rule_code, gref, nref) \
111 do { \
112 rule_code &= 0xFF; \
113 gref = (rule_code) / 12; \
114 if (gref > 12) gref = 11; \
115 nref = (rule_code) % 12; \
116 } while (0)
117
118 /* Like COMPOSITION_DECODE_REFS (RULE_CODE, GREF, NREF), but also
119 decode RULE_CODE into XOFF and YOFF (vertical offset). */
120
121 #define COMPOSITION_DECODE_RULE(rule_code, gref, nref, xoff, yoff) \
122 do { \
123 xoff = (rule_code) >> 16; \
124 yoff = ((rule_code) >> 8) & 0xFF; \
125 COMPOSITION_DECODE_REFS (rule_code, gref, nref); \
126 } while (0)
127
128 /* Nonzero if the global reference point GREF and new reference point NREF are
129 valid. */
130 #define COMPOSITION_ENCODE_RULE_VALID(gref, nref) \
131 (UNSIGNED_CMP (gref, <, 12) && UNSIGNED_CMP (nref, <, 12))
132
133 /* Return encoded composition rule for the pair of global reference
134 point GREF and new reference point NREF. Arguments must be valid. */
135 #define COMPOSITION_ENCODE_RULE(gref, nref) \
136 ((gref) * 12 + (nref))
137
138 /* Data structure that records information about a composition
139 currently used in some buffers or strings.
140
141 When a composition is assigned an ID number (by
142 get_composition_id), this structure is allocated for the
143 composition and linked in composition_table[ID].
144
145 Identical compositions appearing at different places have the same
146 ID, and thus share the same instance of this structure. */
147
148 struct composition {
149 /* Number of glyphs of the composition components. */
150 int glyph_len;
151
152 /* Width, ascent, and descent pixels of the composition. */
153 short pixel_width, ascent, descent;
154
155 short lbearing, rbearing;
156
157 /* How many columns the overall glyphs occupy on the screen. This
158 gives an approximate value for column calculation in
159 Fcurrent_column, and etc. */
160 unsigned short width;
161
162 /* Method of the composition. */
163 enum composition_method method;
164
165 /* Index to the composition hash table. */
166 ptrdiff_t hash_index;
167
168 /* For which font we have calculated the remaining members. The
169 actual type is device dependent. */
170 void *font;
171
172 /* Pointer to an array of x-offset and y-offset (by pixels) of
173 glyphs. This points to a sufficient memory space (sizeof (short) *
174 glyph_len * 2) that is allocated when the composition is
175 registered in composition_table. X-offset and Y-offset of Nth
176 glyph are (2N)th and (2N+1)th elements respectively. */
177 short *offsets;
178 };
179
180 /* Table of pointers to the structure `composition' indexed by
181 COMPOSITION-ID. */
182 extern struct composition **composition_table;
183 /* Number of the currently registered compositions. */
184 extern ptrdiff_t n_compositions;
185
186 /* Mask bits for CHECK_MASK arg to update_compositions.
187 For a change in the region FROM and TO, check compositions ... */
188 #define CHECK_HEAD 1 /* adjacent to FROM */
189 #define CHECK_TAIL 2 /* adjacent to TO */
190 #define CHECK_INSIDE 4 /* between FROM and TO */
191 #define CHECK_BORDER (CHECK_HEAD | CHECK_TAIL)
192 #define CHECK_ALL (CHECK_BORDER | CHECK_INSIDE)
193
194 extern Lisp_Object Qcomposition;
195 extern Lisp_Object composition_hash_table;
196 extern ptrdiff_t get_composition_id (ptrdiff_t, ptrdiff_t, ptrdiff_t,
197 Lisp_Object, Lisp_Object);
198 extern bool find_composition (ptrdiff_t, ptrdiff_t, ptrdiff_t *, ptrdiff_t *,
199 Lisp_Object *, Lisp_Object);
200 extern void update_compositions (ptrdiff_t, ptrdiff_t, int);
201 extern void make_composition_value_copy (Lisp_Object);
202 extern void syms_of_composite (void);
203 extern void compose_text (ptrdiff_t, ptrdiff_t, Lisp_Object, Lisp_Object,
204 Lisp_Object);
205
206 /* Return the method of a composition with property PROP. */
207
208 COMPOSITE_INLINE enum composition_method
209 composition_method (Lisp_Object prop)
210 {
211 if (composition_registered_p (prop))
212 return composition_table[COMPOSITION_ID (prop)]->method;
213 else
214 {
215 Lisp_Object temp = XCDR (XCAR (prop));
216 return (NILP (temp)
217 ? COMPOSITION_RELATIVE
218 : INTEGERP (temp) || STRINGP (temp)
219 ? COMPOSITION_WITH_ALTCHARS
220 : COMPOSITION_WITH_RULE_ALTCHARS);
221 }
222 }
223
224 /* Given offsets START and END, return true if PROP is a valid composition
225 property with length END - START. */
226
227 COMPOSITE_INLINE bool
228 composition_valid_p (ptrdiff_t start, ptrdiff_t end, Lisp_Object prop)
229 {
230 return (CONSP (prop)
231 && (composition_registered_p (prop)
232 ? (COMPOSITION_ID (prop) >= 0
233 && COMPOSITION_ID (prop) <= n_compositions
234 && CONSP (XCDR (prop)))
235 : (CONSP (XCAR (prop))
236 && (NILP (XCDR (XCAR (prop)))
237 || STRINGP (XCDR (XCAR (prop)))
238 || VECTORP (XCDR (XCAR (prop)))
239 || INTEGERP (XCDR (XCAR (prop)))
240 || CONSP (XCDR (XCAR (prop))))))
241 && COMPOSITION_LENGTH (prop) == end - start);
242 }
243
244 /* Macros for lispy glyph-string. This is completely different from
245 struct glyph_string. */
246
247 #define LGSTRING_HEADER(lgs) AREF (lgs, 0)
248 #define LGSTRING_SET_HEADER(lgs, header) ASET (lgs, 0, header)
249
250 #define LGSTRING_FONT(lgs) AREF (LGSTRING_HEADER (lgs), 0)
251 #define LGSTRING_CHAR(lgs, i) AREF (LGSTRING_HEADER (lgs), (i) + 1)
252 #define LGSTRING_CHAR_LEN(lgs) (ASIZE (LGSTRING_HEADER (lgs)) - 1)
253
254 #define LGSTRING_SET_FONT(lgs, val) ASET (LGSTRING_HEADER (lgs), 0, (val))
255 #define LGSTRING_SET_CHAR(lgs, i, c) ASET (LGSTRING_HEADER (lgs), (i) + 1, (c))
256
257 #define LGSTRING_ID(lgs) AREF (lgs, 1)
258 #define LGSTRING_SET_ID(lgs, id) ASET (lgs, 1, id)
259
260 #define LGSTRING_GLYPH_LEN(lgs) (ASIZE ((lgs)) - 2)
261 #define LGSTRING_GLYPH(lgs, idx) AREF ((lgs), (idx) + 2)
262 #define LGSTRING_SET_GLYPH(lgs, idx, val) ASET ((lgs), (idx) + 2, (val))
263 COMPOSITE_INLINE Lisp_Object *
264 lgstring_glyph_addr (Lisp_Object lgs, ptrdiff_t idx)
265 {
266 return aref_addr (lgs, idx + 2);
267 }
268
269 /* Vector size of Lispy glyph. */
270 enum lglyph_indices
271 {
272 LGLYPH_IX_FROM, LGLYPH_IX_TO, LGLYPH_IX_CHAR, LGLYPH_IX_CODE,
273 LGLYPH_IX_WIDTH, LGLYPH_IX_LBEARING, LGLYPH_IX_RBEARING,
274 LGLYPH_IX_ASCENT, LGLYPH_IX_DESCENT, LGLYPH_IX_ADJUSTMENT,
275 /* Not an index. */
276 LGLYPH_SIZE
277 };
278
279 #define LGLYPH_NEW() Fmake_vector (make_number (LGLYPH_SIZE), Qnil)
280 #define LGLYPH_FROM(g) XINT (AREF ((g), LGLYPH_IX_FROM))
281 #define LGLYPH_TO(g) XINT (AREF ((g), LGLYPH_IX_TO))
282 #define LGLYPH_CHAR(g) XINT (AREF ((g), LGLYPH_IX_CHAR))
283 #define LGLYPH_CODE(g) \
284 (NILP (AREF ((g), LGLYPH_IX_CODE)) \
285 ? FONT_INVALID_CODE \
286 : cons_to_unsigned (AREF (g, LGLYPH_IX_CODE), TYPE_MAXIMUM (unsigned)))
287 #define LGLYPH_WIDTH(g) XINT (AREF ((g), LGLYPH_IX_WIDTH))
288 #define LGLYPH_LBEARING(g) XINT (AREF ((g), LGLYPH_IX_LBEARING))
289 #define LGLYPH_RBEARING(g) XINT (AREF ((g), LGLYPH_IX_RBEARING))
290 #define LGLYPH_ASCENT(g) XINT (AREF ((g), LGLYPH_IX_ASCENT))
291 #define LGLYPH_DESCENT(g) XINT (AREF ((g), LGLYPH_IX_DESCENT))
292 #define LGLYPH_ADJUSTMENT(g) AREF ((g), LGLYPH_IX_ADJUSTMENT)
293 #define LGLYPH_SET_FROM(g, val) ASET ((g), LGLYPH_IX_FROM, make_number (val))
294 #define LGLYPH_SET_TO(g, val) ASET ((g), LGLYPH_IX_TO, make_number (val))
295 #define LGLYPH_SET_CHAR(g, val) ASET ((g), LGLYPH_IX_CHAR, make_number (val))
296 /* Callers must assure that VAL is not negative! */
297 #define LGLYPH_SET_CODE(g, val) \
298 ASET (g, LGLYPH_IX_CODE, \
299 val == FONT_INVALID_CODE ? Qnil : INTEGER_TO_CONS (val))
300
301 #define LGLYPH_SET_WIDTH(g, val) ASET ((g), LGLYPH_IX_WIDTH, make_number (val))
302 #define LGLYPH_SET_LBEARING(g, val) ASET ((g), LGLYPH_IX_LBEARING, make_number (val))
303 #define LGLYPH_SET_RBEARING(g, val) ASET ((g), LGLYPH_IX_RBEARING, make_number (val))
304 #define LGLYPH_SET_ASCENT(g, val) ASET ((g), LGLYPH_IX_ASCENT, make_number (val))
305 #define LGLYPH_SET_DESCENT(g, val) ASET ((g), LGLYPH_IX_DESCENT, make_number (val))
306 #define LGLYPH_SET_ADJUSTMENT(g, val) ASET ((g), LGLYPH_IX_ADJUSTMENT, (val))
307
308 #define LGLYPH_XOFF(g) (VECTORP (LGLYPH_ADJUSTMENT (g)) \
309 ? XINT (AREF (LGLYPH_ADJUSTMENT (g), 0)) : 0)
310 #define LGLYPH_YOFF(g) (VECTORP (LGLYPH_ADJUSTMENT (g)) \
311 ? XINT (AREF (LGLYPH_ADJUSTMENT (g), 1)) : 0)
312 #define LGLYPH_WADJUST(g) (VECTORP (LGLYPH_ADJUSTMENT (g)) \
313 ? XINT (AREF (LGLYPH_ADJUSTMENT (g), 2)) : 0)
314
315 struct composition_it;
316 struct face;
317 struct font_metrics;
318
319 extern Lisp_Object composition_gstring_put_cache (Lisp_Object, ptrdiff_t);
320 extern Lisp_Object composition_gstring_from_id (ptrdiff_t);
321 extern bool composition_gstring_p (Lisp_Object);
322 extern int composition_gstring_width (Lisp_Object, ptrdiff_t, ptrdiff_t,
323 struct font_metrics *);
324
325 extern void composition_compute_stop_pos (struct composition_it *,
326 ptrdiff_t, ptrdiff_t, ptrdiff_t,
327 Lisp_Object);
328 extern bool composition_reseat_it (struct composition_it *, ptrdiff_t,
329 ptrdiff_t, ptrdiff_t, struct window *,
330 struct face *, Lisp_Object);
331 extern int composition_update_it (struct composition_it *,
332 ptrdiff_t, ptrdiff_t, Lisp_Object);
333
334 extern ptrdiff_t composition_adjust_point (ptrdiff_t, ptrdiff_t);
335
336 INLINE_HEADER_END
337
338 #endif /* not EMACS_COMPOSITE_H */