1 /*
2 * Copyright © 1998-2004 David Turner and Werner Lemberg
3 * Copyright © 2004,2007,2009,2010 Red Hat, Inc.
4 * Copyright © 2011,2012 Google, Inc.
5 *
6 * This is part of HarfBuzz, a text shaping library.
7 *
8 * Permission is hereby granted, without written agreement and without
9 * license or royalty fees, to use, copy, modify, and distribute this
10 * software and its documentation for any purpose, provided that the
11 * above copyright notice and the following two paragraphs appear in
12 * all copies of this software.
13 *
14 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
15 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
16 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
17 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
18 * DAMAGE.
19 *
20 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
21 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
22 * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
23 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
24 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
25 *
26 * Red Hat Author(s): Owen Taylor, Behdad Esfahbod
27 * Google Author(s): Behdad Esfahbod
28 */
29
30 #ifndef HB_BUFFER_HH
31 #define HB_BUFFER_HH
32
33 #include "hb.hh"
34 #include "hb-unicode.hh"
35
36
37 #ifndef HB_BUFFER_MAX_LEN_FACTOR
38 #define HB_BUFFER_MAX_LEN_FACTOR 64
39 #endif
40 #ifndef HB_BUFFER_MAX_LEN_MIN
41 #define HB_BUFFER_MAX_LEN_MIN 16384
42 #endif
43 #ifndef HB_BUFFER_MAX_LEN_DEFAULT
44 #define HB_BUFFER_MAX_LEN_DEFAULT 0x3FFFFFFF /* Shaping more than a billion chars? Let us know! */
45 #endif
46
47 #ifndef HB_BUFFER_MAX_OPS_FACTOR
48 #define HB_BUFFER_MAX_OPS_FACTOR 1024
49 #endif
50 #ifndef HB_BUFFER_MAX_OPS_MIN
51 #define HB_BUFFER_MAX_OPS_MIN 16384
52 #endif
53 #ifndef HB_BUFFER_MAX_OPS_DEFAULT
54 #define HB_BUFFER_MAX_OPS_DEFAULT 0x1FFFFFFF /* Shaping more than a billion operations? Let us know! */
55 #endif
56
57 static_assert ((sizeof (hb_glyph_info_t) == 20), "");
58 static_assert ((sizeof (hb_glyph_info_t) == sizeof (hb_glyph_position_t)), "");
59
60 HB_MARK_AS_FLAG_T (hb_buffer_flags_t);
61 HB_MARK_AS_FLAG_T (hb_buffer_serialize_flags_t);
62 HB_MARK_AS_FLAG_T (hb_buffer_diff_flags_t);
63
64 enum hb_buffer_scratch_flags_t {
65 HB_BUFFER_SCRATCH_FLAG_DEFAULT = 0x00000000u,
66 HB_BUFFER_SCRATCH_FLAG_HAS_NON_ASCII = 0x00000001u,
67 HB_BUFFER_SCRATCH_FLAG_HAS_DEFAULT_IGNORABLES = 0x00000002u,
68 HB_BUFFER_SCRATCH_FLAG_HAS_SPACE_FALLBACK = 0x00000004u,
69 HB_BUFFER_SCRATCH_FLAG_HAS_GPOS_ATTACHMENT = 0x00000008u,
70 HB_BUFFER_SCRATCH_FLAG_HAS_UNSAFE_TO_BREAK = 0x00000010u,
71 HB_BUFFER_SCRATCH_FLAG_HAS_CGJ = 0x00000020u,
72
73 /* Reserved for complex shapers' internal use. */
74 HB_BUFFER_SCRATCH_FLAG_COMPLEX0 = 0x01000000u,
75 HB_BUFFER_SCRATCH_FLAG_COMPLEX1 = 0x02000000u,
76 HB_BUFFER_SCRATCH_FLAG_COMPLEX2 = 0x04000000u,
77 HB_BUFFER_SCRATCH_FLAG_COMPLEX3 = 0x08000000u,
78 };
79 HB_MARK_AS_FLAG_T (hb_buffer_scratch_flags_t);
80
81
82 /*
83 * hb_buffer_t
84 */
85
86 struct hb_buffer_t
87 {
88 hb_object_header_t header;
89
90 /* Information about how the text in the buffer should be treated */
91 hb_unicode_funcs_t *unicode; /* Unicode functions */
92 hb_buffer_flags_t flags; /* BOT / EOT / etc. */
93 hb_buffer_cluster_level_t cluster_level;
94 hb_codepoint_t replacement; /* U+FFFD or something else. */
95 hb_codepoint_t invisible; /* 0 or something else. */
96 hb_codepoint_t not_found; /* 0 or something else. */
97 hb_buffer_scratch_flags_t scratch_flags; /* Have space-fallback, etc. */
98 unsigned int max_len; /* Maximum allowed len. */
99 int max_ops; /* Maximum allowed operations. */
100
101 /* Buffer contents */
102 hb_buffer_content_type_t content_type;
103 hb_segment_properties_t props; /* Script, language, direction */
104
105 bool successful; /* Allocations successful */
106 bool have_output; /* Whether we have an output buffer going on */
107 bool have_positions; /* Whether we have positions */
108
109 unsigned int idx; /* Cursor into ->info and ->pos arrays */
110 unsigned int len; /* Length of ->info and ->pos arrays */
111 unsigned int out_len; /* Length of ->out_info array if have_output */
112
113 unsigned int allocated; /* Length of allocated arrays */
114 hb_glyph_info_t *info;
115 hb_glyph_info_t *out_info;
116 hb_glyph_position_t *pos;
117
118 unsigned int serial;
119
120 /* Text before / after the main buffer contents.
121 * Always in Unicode, and ordered outward.
122 * Index 0 is for "pre-context", 1 for "post-context". */
123 static constexpr unsigned CONTEXT_LENGTH = 5u;
124 hb_codepoint_t context[2][CONTEXT_LENGTH];
125 unsigned int context_len[2];
126
127 /* Debugging API */
128 #ifndef HB_NO_BUFFER_MESSAGE
129 hb_buffer_message_func_t message_func;
130 void *message_data;
131 hb_destroy_func_t message_destroy;
132 unsigned message_depth; /* How deeply are we inside a message callback? */
133 #else
134 static constexpr unsigned message_depth = 0u;
135 #endif
136
137 /* Internal debugging. */
138 /* The bits here reflect current allocations of the bytes in glyph_info_t's var1 and var2. */
139 #ifndef HB_NDEBUG
140 uint8_t allocated_var_bits;
141 #endif
142
143
144 /* Methods */
145
in_errorhb_buffer_t146 HB_NODISCARD bool in_error () const { return !successful; }
147
allocate_varhb_buffer_t148 void allocate_var (unsigned int start, unsigned int count)
149 {
150 #ifndef HB_NDEBUG
151 unsigned int end = start + count;
152 assert (end <= 8);
153 unsigned int bits = (1u<<end) - (1u<<start);
154 assert (0 == (allocated_var_bits & bits));
155 allocated_var_bits |= bits;
156 #endif
157 }
deallocate_varhb_buffer_t158 void deallocate_var (unsigned int start, unsigned int count)
159 {
160 #ifndef HB_NDEBUG
161 unsigned int end = start + count;
162 assert (end <= 8);
163 unsigned int bits = (1u<<end) - (1u<<start);
164 assert (bits == (allocated_var_bits & bits));
165 allocated_var_bits &= ~bits;
166 #endif
167 }
assert_varhb_buffer_t168 void assert_var (unsigned int start, unsigned int count)
169 {
170 #ifndef HB_NDEBUG
171 unsigned int end = start + count;
172 assert (end <= 8);
173 unsigned int bits = (1u<<end) - (1u<<start);
174 assert (bits == (allocated_var_bits & bits));
175 #endif
176 }
deallocate_var_allhb_buffer_t177 void deallocate_var_all ()
178 {
179 #ifndef HB_NDEBUG
180 allocated_var_bits = 0;
181 #endif
182 }
183
curhb_buffer_t184 hb_glyph_info_t &cur (unsigned int i = 0) { return info[idx + i]; }
curhb_buffer_t185 hb_glyph_info_t cur (unsigned int i = 0) const { return info[idx + i]; }
186
cur_poshb_buffer_t187 hb_glyph_position_t &cur_pos (unsigned int i = 0) { return pos[idx + i]; }
cur_poshb_buffer_t188 hb_glyph_position_t cur_pos (unsigned int i = 0) const { return pos[idx + i]; }
189
prevhb_buffer_t190 hb_glyph_info_t &prev () { return out_info[out_len ? out_len - 1 : 0]; }
prevhb_buffer_t191 hb_glyph_info_t prev () const { return out_info[out_len ? out_len - 1 : 0]; }
192
193 HB_INTERNAL void reset ();
194 HB_INTERNAL void clear ();
195
backtrack_lenhb_buffer_t196 unsigned int backtrack_len () const { return have_output ? out_len : idx; }
lookahead_lenhb_buffer_t197 unsigned int lookahead_len () const { return len - idx; }
next_serialhb_buffer_t198 unsigned int next_serial () { return serial++; }
199
200 HB_INTERNAL void add (hb_codepoint_t codepoint,
201 unsigned int cluster);
202 HB_INTERNAL void add_info (const hb_glyph_info_t &glyph_info);
203
204 HB_INTERNAL void reverse_range (unsigned int start, unsigned int end);
205 HB_INTERNAL void reverse ();
206 HB_INTERNAL void reverse_clusters ();
207 HB_INTERNAL void guess_segment_properties ();
208
209 HB_INTERNAL void swap_buffers ();
210 HB_INTERNAL void clear_output ();
211 HB_INTERNAL void clear_positions ();
212
213 template <typename T>
replace_glyphshb_buffer_t214 HB_NODISCARD bool replace_glyphs (unsigned int num_in,
215 unsigned int num_out,
216 const T *glyph_data)
217 {
218 if (unlikely (!make_room_for (num_in, num_out))) return false;
219
220 assert (idx + num_in <= len);
221
222 merge_clusters (idx, idx + num_in);
223
224 hb_glyph_info_t &orig_info = idx < len ? cur() : prev();
225
226 hb_glyph_info_t *pinfo = &out_info[out_len];
227 for (unsigned int i = 0; i < num_out; i++)
228 {
229 *pinfo = orig_info;
230 pinfo->codepoint = glyph_data[i];
231 pinfo++;
232 }
233
234 idx += num_in;
235 out_len += num_out;
236 return true;
237 }
238
replace_glyphhb_buffer_t239 HB_NODISCARD bool replace_glyph (hb_codepoint_t glyph_index)
240 { return replace_glyphs (1, 1, &glyph_index); }
241
242 /* Makes a copy of the glyph at idx to output and replace glyph_index */
output_glyphhb_buffer_t243 HB_NODISCARD bool output_glyph (hb_codepoint_t glyph_index)
244 { return replace_glyphs (0, 1, &glyph_index); }
245
output_infohb_buffer_t246 HB_NODISCARD bool output_info (const hb_glyph_info_t &glyph_info)
247 {
248 if (unlikely (!make_room_for (0, 1))) return false;
249
250 out_info[out_len] = glyph_info;
251
252 out_len++;
253 return true;
254 }
255 /* Copies glyph at idx to output but doesn't advance idx */
copy_glyphhb_buffer_t256 HB_NODISCARD bool copy_glyph ()
257 {
258 /* Extra copy because cur()'s return can be freed within
259 * output_info() call if buffer reallocates. */
260 return output_info (hb_glyph_info_t (cur()));
261 }
262
263 /* Copies glyph at idx to output and advance idx.
264 * If there's no output, just advance idx. */
next_glyphhb_buffer_t265 HB_NODISCARD bool next_glyph ()
266 {
267 if (have_output)
268 {
269 if (out_info != info || out_len != idx)
270 {
271 if (unlikely (!make_room_for (1, 1))) return false;
272 out_info[out_len] = info[idx];
273 }
274 out_len++;
275 }
276
277 idx++;
278 return true;
279 }
280 /* Copies n glyphs at idx to output and advance idx.
281 * If there's no output, just advance idx. */
next_glyphshb_buffer_t282 HB_NODISCARD bool next_glyphs (unsigned int n)
283 {
284 if (have_output)
285 {
286 if (out_info != info || out_len != idx)
287 {
288 if (unlikely (!make_room_for (n, n))) return false;
289 memmove (out_info + out_len, info + idx, n * sizeof (out_info[0]));
290 }
291 out_len += n;
292 }
293
294 idx += n;
295 return true;
296 }
297 /* Advance idx without copying to output. */
skip_glyphhb_buffer_t298 void skip_glyph () { idx++; }
reset_maskshb_buffer_t299 void reset_masks (hb_mask_t mask)
300 {
301 for (unsigned int j = 0; j < len; j++)
302 info[j].mask = mask;
303 }
add_maskshb_buffer_t304 void add_masks (hb_mask_t mask)
305 {
306 for (unsigned int j = 0; j < len; j++)
307 info[j].mask |= mask;
308 }
309 HB_INTERNAL void set_masks (hb_mask_t value, hb_mask_t mask,
310 unsigned int cluster_start, unsigned int cluster_end);
311
merge_clustershb_buffer_t312 void merge_clusters (unsigned int start, unsigned int end)
313 {
314 if (end - start < 2)
315 return;
316 merge_clusters_impl (start, end);
317 }
318 HB_INTERNAL void merge_clusters_impl (unsigned int start, unsigned int end);
319 HB_INTERNAL void merge_out_clusters (unsigned int start, unsigned int end);
320 /* Merge clusters for deleting current glyph, and skip it. */
321 HB_INTERNAL void delete_glyph ();
322
unsafe_to_breakhb_buffer_t323 void unsafe_to_break (unsigned int start,
324 unsigned int end)
325 {
326 if (end - start < 2)
327 return;
328 unsafe_to_break_impl (start, end);
329 }
330 HB_INTERNAL void unsafe_to_break_impl (unsigned int start, unsigned int end);
331 HB_INTERNAL void unsafe_to_break_from_outbuffer (unsigned int start, unsigned int end);
332
333
334 /* Internal methods */
335 HB_NODISCARD HB_INTERNAL bool move_to (unsigned int i); /* i is output-buffer index. */
336
337 HB_NODISCARD HB_INTERNAL bool enlarge (unsigned int size);
338
ensurehb_buffer_t339 HB_NODISCARD bool ensure (unsigned int size)
340 { return likely (!size || size < allocated) ? true : enlarge (size); }
341
ensure_inplacehb_buffer_t342 HB_NODISCARD bool ensure_inplace (unsigned int size)
343 { return likely (!size || size < allocated); }
344
assert_glyphshb_buffer_t345 void assert_glyphs ()
346 {
347 assert ((content_type == HB_BUFFER_CONTENT_TYPE_GLYPHS) ||
348 (!len && (content_type == HB_BUFFER_CONTENT_TYPE_INVALID)));
349 }
assert_unicodehb_buffer_t350 void assert_unicode ()
351 {
352 assert ((content_type == HB_BUFFER_CONTENT_TYPE_UNICODE) ||
353 (!len && (content_type == HB_BUFFER_CONTENT_TYPE_INVALID)));
354 }
ensure_glyphshb_buffer_t355 HB_NODISCARD bool ensure_glyphs ()
356 {
357 if (unlikely (content_type != HB_BUFFER_CONTENT_TYPE_GLYPHS))
358 {
359 if (content_type != HB_BUFFER_CONTENT_TYPE_INVALID)
360 return false;
361 assert (len == 0);
362 content_type = HB_BUFFER_CONTENT_TYPE_GLYPHS;
363 }
364 return true;
365 }
ensure_unicodehb_buffer_t366 HB_NODISCARD bool ensure_unicode ()
367 {
368 if (unlikely (content_type != HB_BUFFER_CONTENT_TYPE_UNICODE))
369 {
370 if (content_type != HB_BUFFER_CONTENT_TYPE_INVALID)
371 return false;
372 assert (len == 0);
373 content_type = HB_BUFFER_CONTENT_TYPE_UNICODE;
374 }
375 return true;
376 }
377
378 HB_NODISCARD HB_INTERNAL bool make_room_for (unsigned int num_in, unsigned int num_out);
379 HB_NODISCARD HB_INTERNAL bool shift_forward (unsigned int count);
380
381 typedef long scratch_buffer_t;
382 HB_INTERNAL scratch_buffer_t *get_scratch_buffer (unsigned int *size);
383
clear_contexthb_buffer_t384 void clear_context (unsigned int side) { context_len[side] = 0; }
385
386 HB_INTERNAL void sort (unsigned int start, unsigned int end, int(*compar)(const hb_glyph_info_t *, const hb_glyph_info_t *));
387
messaginghb_buffer_t388 bool messaging ()
389 {
390 #ifdef HB_NO_BUFFER_MESSAGE
391 return false;
392 #else
393 return unlikely (message_func);
394 #endif
395 }
messagehb_buffer_t396 bool message (hb_font_t *font, const char *fmt, ...) HB_PRINTF_FUNC(3, 4)
397 {
398 #ifdef HB_NO_BUFFER_MESSAGE
399 return true;
400 #else
401 if (!messaging ())
402 return true;
403
404 message_depth++;
405
406 va_list ap;
407 va_start (ap, fmt);
408 bool ret = message_impl (font, fmt, ap);
409 va_end (ap);
410
411 message_depth--;
412
413 return ret;
414 #endif
415 }
416 HB_INTERNAL bool message_impl (hb_font_t *font, const char *fmt, va_list ap) HB_PRINTF_FUNC(3, 0);
417
418 static void
set_clusterhb_buffer_t419 set_cluster (hb_glyph_info_t &inf, unsigned int cluster, unsigned int mask = 0)
420 {
421 if (inf.cluster != cluster)
422 {
423 if (mask & HB_GLYPH_FLAG_UNSAFE_TO_BREAK)
424 inf.mask |= HB_GLYPH_FLAG_UNSAFE_TO_BREAK;
425 else
426 inf.mask &= ~HB_GLYPH_FLAG_UNSAFE_TO_BREAK;
427 }
428 inf.cluster = cluster;
429 }
430
431 unsigned int
_unsafe_to_break_find_min_clusterhb_buffer_t432 _unsafe_to_break_find_min_cluster (const hb_glyph_info_t *infos,
433 unsigned int start, unsigned int end,
434 unsigned int cluster) const
435 {
436 for (unsigned int i = start; i < end; i++)
437 cluster = hb_min (cluster, infos[i].cluster);
438 return cluster;
439 }
440 void
_unsafe_to_break_set_maskhb_buffer_t441 _unsafe_to_break_set_mask (hb_glyph_info_t *infos,
442 unsigned int start, unsigned int end,
443 unsigned int cluster)
444 {
445 for (unsigned int i = start; i < end; i++)
446 if (cluster != infos[i].cluster)
447 {
448 scratch_flags |= HB_BUFFER_SCRATCH_FLAG_HAS_UNSAFE_TO_BREAK;
449 infos[i].mask |= HB_GLYPH_FLAG_UNSAFE_TO_BREAK;
450 }
451 }
452
unsafe_to_break_allhb_buffer_t453 void unsafe_to_break_all () { unsafe_to_break_impl (0, len); }
safe_to_break_allhb_buffer_t454 void safe_to_break_all ()
455 {
456 for (unsigned int i = 0; i < len; i++)
457 info[i].mask &= ~HB_GLYPH_FLAG_UNSAFE_TO_BREAK;
458 }
459 };
460 DECLARE_NULL_INSTANCE (hb_buffer_t);
461
462
463 /* Loop over clusters. Duplicated in foreach_syllable(). */
464 #define foreach_cluster(buffer, start, end) \
465 for (unsigned int \
466 _count = buffer->len, \
467 start = 0, end = _count ? _next_cluster (buffer, 0) : 0; \
468 start < _count; \
469 start = end, end = _next_cluster (buffer, start))
470
471 static inline unsigned int
_next_cluster(hb_buffer_t * buffer,unsigned int start)472 _next_cluster (hb_buffer_t *buffer, unsigned int start)
473 {
474 hb_glyph_info_t *info = buffer->info;
475 unsigned int count = buffer->len;
476
477 unsigned int cluster = info[start].cluster;
478 while (++start < count && cluster == info[start].cluster)
479 ;
480
481 return start;
482 }
483
484
485 #define HB_BUFFER_XALLOCATE_VAR(b, func, var) \
486 b->func (offsetof (hb_glyph_info_t, var) - offsetof(hb_glyph_info_t, var1), \
487 sizeof (b->info[0].var))
488 #define HB_BUFFER_ALLOCATE_VAR(b, var) HB_BUFFER_XALLOCATE_VAR (b, allocate_var, var ())
489 #define HB_BUFFER_DEALLOCATE_VAR(b, var) HB_BUFFER_XALLOCATE_VAR (b, deallocate_var, var ())
490 #define HB_BUFFER_ASSERT_VAR(b, var) HB_BUFFER_XALLOCATE_VAR (b, assert_var, var ())
491
492
493 #endif /* HB_BUFFER_HH */
494