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_buffer_scratch_flags_t scratch_flags; /* Have space-fallback, etc. */
97 unsigned int max_len; /* Maximum allowed len. */
98 int max_ops; /* Maximum allowed operations. */
99
100 /* Buffer contents */
101 hb_buffer_content_type_t content_type;
102 hb_segment_properties_t props; /* Script, language, direction */
103
104 bool successful; /* Allocations successful */
105 bool have_output; /* Whether we have an output buffer going on */
106 bool have_positions; /* Whether we have positions */
107
108 unsigned int idx; /* Cursor into ->info and ->pos arrays */
109 unsigned int len; /* Length of ->info and ->pos arrays */
110 unsigned int out_len; /* Length of ->out array if have_output */
111
112 unsigned int allocated; /* Length of allocated arrays */
113 hb_glyph_info_t *info;
114 hb_glyph_info_t *out_info;
115 hb_glyph_position_t *pos;
116
117 unsigned int serial;
118
119 /* Text before / after the main buffer contents.
120 * Always in Unicode, and ordered outward.
121 * Index 0 is for "pre-context", 1 for "post-context". */
122 static constexpr unsigned CONTEXT_LENGTH = 5u;
123 hb_codepoint_t context[2][CONTEXT_LENGTH];
124 unsigned int context_len[2];
125
126 /* Debugging API */
127 #ifndef HB_NO_BUFFER_MESSAGE
128 hb_buffer_message_func_t message_func;
129 void *message_data;
130 hb_destroy_func_t message_destroy;
131 #endif
132
133 /* Internal debugging. */
134 /* The bits here reflect current allocations of the bytes in glyph_info_t's var1 and var2. */
135 #ifndef HB_NDEBUG
136 uint8_t allocated_var_bits;
137 #endif
138
139
140 /* Methods */
141
in_errorhb_buffer_t142 HB_NODISCARD bool in_error () const { return !successful; }
143
allocate_varhb_buffer_t144 void allocate_var (unsigned int start, unsigned int count)
145 {
146 #ifndef HB_NDEBUG
147 unsigned int end = start + count;
148 assert (end <= 8);
149 unsigned int bits = (1u<<end) - (1u<<start);
150 assert (0 == (allocated_var_bits & bits));
151 allocated_var_bits |= bits;
152 #endif
153 }
deallocate_varhb_buffer_t154 void deallocate_var (unsigned int start, unsigned int count)
155 {
156 #ifndef HB_NDEBUG
157 unsigned int end = start + count;
158 assert (end <= 8);
159 unsigned int bits = (1u<<end) - (1u<<start);
160 assert (bits == (allocated_var_bits & bits));
161 allocated_var_bits &= ~bits;
162 #endif
163 }
assert_varhb_buffer_t164 void assert_var (unsigned int start, unsigned int count)
165 {
166 #ifndef HB_NDEBUG
167 unsigned int end = start + count;
168 assert (end <= 8);
169 unsigned int bits = (1u<<end) - (1u<<start);
170 assert (bits == (allocated_var_bits & bits));
171 #endif
172 }
deallocate_var_allhb_buffer_t173 void deallocate_var_all ()
174 {
175 #ifndef HB_NDEBUG
176 allocated_var_bits = 0;
177 #endif
178 }
179
curhb_buffer_t180 hb_glyph_info_t &cur (unsigned int i = 0) { return info[idx + i]; }
curhb_buffer_t181 hb_glyph_info_t cur (unsigned int i = 0) const { return info[idx + i]; }
182
cur_poshb_buffer_t183 hb_glyph_position_t &cur_pos (unsigned int i = 0) { return pos[idx + i]; }
cur_poshb_buffer_t184 hb_glyph_position_t cur_pos (unsigned int i = 0) const { return pos[idx + i]; }
185
prevhb_buffer_t186 hb_glyph_info_t &prev () { return out_info[out_len ? out_len - 1 : 0]; }
prevhb_buffer_t187 hb_glyph_info_t prev () const { return out_info[out_len ? out_len - 1 : 0]; }
188
has_separate_outputhb_buffer_t189 HB_NODISCARD bool has_separate_output () const { return info != out_info; }
190
191
192 HB_INTERNAL void reset ();
193 HB_INTERNAL void clear ();
194
backtrack_lenhb_buffer_t195 unsigned int backtrack_len () const { return have_output? out_len : idx; }
lookahead_lenhb_buffer_t196 unsigned int lookahead_len () const { return len - idx; }
next_serialhb_buffer_t197 unsigned int next_serial () { return serial++; }
198
199 HB_INTERNAL void add (hb_codepoint_t codepoint,
200 unsigned int cluster);
201 HB_INTERNAL void add_info (const hb_glyph_info_t &glyph_info);
202
203 HB_INTERNAL void reverse_range (unsigned int start, unsigned int end);
204 HB_INTERNAL void reverse ();
205 HB_INTERNAL void reverse_clusters ();
206 HB_INTERNAL void guess_segment_properties ();
207
208 HB_INTERNAL void swap_buffers ();
209 HB_INTERNAL void remove_output ();
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 va_list ap;
404 va_start (ap, fmt);
405 bool ret = message_impl (font, fmt, ap);
406 va_end (ap);
407 return ret;
408 #endif
409 }
410 HB_INTERNAL bool message_impl (hb_font_t *font, const char *fmt, va_list ap) HB_PRINTF_FUNC(3, 0);
411
412 static void
set_clusterhb_buffer_t413 set_cluster (hb_glyph_info_t &inf, unsigned int cluster, unsigned int mask = 0)
414 {
415 if (inf.cluster != cluster)
416 {
417 if (mask & HB_GLYPH_FLAG_UNSAFE_TO_BREAK)
418 inf.mask |= HB_GLYPH_FLAG_UNSAFE_TO_BREAK;
419 else
420 inf.mask &= ~HB_GLYPH_FLAG_UNSAFE_TO_BREAK;
421 }
422 inf.cluster = cluster;
423 }
424
425 unsigned int
_unsafe_to_break_find_min_clusterhb_buffer_t426 _unsafe_to_break_find_min_cluster (const hb_glyph_info_t *infos,
427 unsigned int start, unsigned int end,
428 unsigned int cluster) const
429 {
430 for (unsigned int i = start; i < end; i++)
431 cluster = hb_min (cluster, infos[i].cluster);
432 return cluster;
433 }
434 void
_unsafe_to_break_set_maskhb_buffer_t435 _unsafe_to_break_set_mask (hb_glyph_info_t *infos,
436 unsigned int start, unsigned int end,
437 unsigned int cluster)
438 {
439 for (unsigned int i = start; i < end; i++)
440 if (cluster != infos[i].cluster)
441 {
442 scratch_flags |= HB_BUFFER_SCRATCH_FLAG_HAS_UNSAFE_TO_BREAK;
443 infos[i].mask |= HB_GLYPH_FLAG_UNSAFE_TO_BREAK;
444 }
445 }
446
unsafe_to_break_allhb_buffer_t447 void unsafe_to_break_all () { unsafe_to_break_impl (0, len); }
safe_to_break_allhb_buffer_t448 void safe_to_break_all ()
449 {
450 for (unsigned int i = 0; i < len; i++)
451 info[i].mask &= ~HB_GLYPH_FLAG_UNSAFE_TO_BREAK;
452 }
453 };
454 DECLARE_NULL_INSTANCE (hb_buffer_t);
455
456
457 /* Loop over clusters. Duplicated in foreach_syllable(). */
458 #define foreach_cluster(buffer, start, end) \
459 for (unsigned int \
460 _count = buffer->len, \
461 start = 0, end = _count ? _next_cluster (buffer, 0) : 0; \
462 start < _count; \
463 start = end, end = _next_cluster (buffer, start))
464
465 static inline unsigned int
_next_cluster(hb_buffer_t * buffer,unsigned int start)466 _next_cluster (hb_buffer_t *buffer, unsigned int start)
467 {
468 hb_glyph_info_t *info = buffer->info;
469 unsigned int count = buffer->len;
470
471 unsigned int cluster = info[start].cluster;
472 while (++start < count && cluster == info[start].cluster)
473 ;
474
475 return start;
476 }
477
478
479 #define HB_BUFFER_XALLOCATE_VAR(b, func, var) \
480 b->func (offsetof (hb_glyph_info_t, var) - offsetof(hb_glyph_info_t, var1), \
481 sizeof (b->info[0].var))
482 #define HB_BUFFER_ALLOCATE_VAR(b, var) HB_BUFFER_XALLOCATE_VAR (b, allocate_var, var ())
483 #define HB_BUFFER_DEALLOCATE_VAR(b, var) HB_BUFFER_XALLOCATE_VAR (b, deallocate_var, var ())
484 #define HB_BUFFER_ASSERT_VAR(b, var) HB_BUFFER_XALLOCATE_VAR (b, assert_var, var ())
485
486
487 #endif /* HB_BUFFER_HH */
488