2 * Copyright © 1998-2004 David Turner and Werner Lemberg
3 * Copyright © 2004,2007,2009,2010 Red Hat, Inc.
4 * Copyright © 2011,2012 Google, Inc.
6 * This is part of HarfBuzz, a text shaping library.
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.
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
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.
26 * Red Hat Author(s): Owen Taylor, Behdad Esfahbod
27 * Google Author(s): Behdad Esfahbod
34 #include "hb-unicode.hh"
37 #ifndef HB_BUFFER_MAX_LEN_FACTOR
38 #define HB_BUFFER_MAX_LEN_FACTOR 64
40 #ifndef HB_BUFFER_MAX_LEN_MIN
41 #define HB_BUFFER_MAX_LEN_MIN 16384
43 #ifndef HB_BUFFER_MAX_LEN_DEFAULT
44 #define HB_BUFFER_MAX_LEN_DEFAULT 0x3FFFFFFF /* Shaping more than a billion chars? Let us know! */
47 #ifndef HB_BUFFER_MAX_OPS_FACTOR
48 #define HB_BUFFER_MAX_OPS_FACTOR 1024
50 #ifndef HB_BUFFER_MAX_OPS_MIN
51 #define HB_BUFFER_MAX_OPS_MIN 16384
53 #ifndef HB_BUFFER_MAX_OPS_DEFAULT
54 #define HB_BUFFER_MAX_OPS_DEFAULT 0x1FFFFFFF /* Shaping more than a billion operations? Let us know! */
57 static_assert ((sizeof (hb_glyph_info_t) == 20), "");
58 static_assert ((sizeof (hb_glyph_info_t) == sizeof (hb_glyph_position_t)), "");
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);
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_CGJ = 0x00000010u,
71 HB_BUFFER_SCRATCH_FLAG_HAS_GLYPH_FLAGS = 0x00000020u,
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,
79 HB_MARK_AS_FLAG_T (hb_buffer_scratch_flags_t);
88 hb_object_header_t header;
91 * Information about how the text in the buffer should be treated.
94 hb_unicode_funcs_t *unicode; /* Unicode functions */
95 hb_buffer_flags_t flags; /* BOT / EOT / etc. */
96 hb_buffer_cluster_level_t cluster_level;
97 hb_codepoint_t replacement; /* U+FFFD or something else. */
98 hb_codepoint_t invisible; /* 0 or something else. */
99 hb_codepoint_t not_found; /* 0 or something else. */
105 hb_buffer_content_type_t content_type;
106 hb_segment_properties_t props; /* Script, language, direction */
108 bool successful; /* Allocations successful */
109 bool have_output; /* Whether we have an output buffer going on */
110 bool have_positions; /* Whether we have positions */
112 unsigned int idx; /* Cursor into ->info and ->pos arrays */
113 unsigned int len; /* Length of ->info and ->pos arrays */
114 unsigned int out_len; /* Length of ->out_info array if have_output */
116 unsigned int allocated; /* Length of allocated arrays */
117 hb_glyph_info_t *info;
118 hb_glyph_info_t *out_info;
119 hb_glyph_position_t *pos;
121 /* Text before / after the main buffer contents.
122 * Always in Unicode, and ordered outward.
123 * Index 0 is for "pre-context", 1 for "post-context". */
124 static constexpr unsigned CONTEXT_LENGTH = 5u;
125 hb_codepoint_t context[2][CONTEXT_LENGTH];
126 unsigned int context_len[2];
130 * Managed by enter / leave
134 uint8_t allocated_var_bits;
137 hb_buffer_scratch_flags_t scratch_flags; /* Have space-fallback, etc. */
138 unsigned int max_len; /* Maximum allowed len. */
139 int max_ops; /* Maximum allowed operations. */
140 /* The bits here reflect current allocations of the bytes in glyph_info_t's var1 and var2. */
147 #ifndef HB_NO_BUFFER_MESSAGE
148 hb_buffer_message_func_t message_func;
150 hb_destroy_func_t message_destroy;
151 unsigned message_depth; /* How deeply are we inside a message callback? */
153 static constexpr unsigned message_depth = 0u;
160 HB_NODISCARD bool in_error () const { return !successful; }
162 void allocate_var (unsigned int start, unsigned int count)
165 unsigned int end = start + count;
167 unsigned int bits = (1u<<end) - (1u<<start);
168 assert (0 == (allocated_var_bits & bits));
169 allocated_var_bits |= bits;
172 void deallocate_var (unsigned int start, unsigned int count)
175 unsigned int end = start + count;
177 unsigned int bits = (1u<<end) - (1u<<start);
178 assert (bits == (allocated_var_bits & bits));
179 allocated_var_bits &= ~bits;
182 void assert_var (unsigned int start, unsigned int count)
185 unsigned int end = start + count;
187 unsigned int bits = (1u<<end) - (1u<<start);
188 assert (bits == (allocated_var_bits & bits));
191 void deallocate_var_all ()
194 allocated_var_bits = 0;
198 hb_glyph_info_t &cur (unsigned int i = 0) { return info[idx + i]; }
199 hb_glyph_info_t cur (unsigned int i = 0) const { return info[idx + i]; }
201 hb_glyph_position_t &cur_pos (unsigned int i = 0) { return pos[idx + i]; }
202 hb_glyph_position_t cur_pos (unsigned int i = 0) const { return pos[idx + i]; }
204 hb_glyph_info_t &prev () { return out_info[out_len ? out_len - 1 : 0]; }
205 hb_glyph_info_t prev () const { return out_info[out_len ? out_len - 1 : 0]; }
207 HB_INTERNAL void similar (const hb_buffer_t &src);
208 HB_INTERNAL void reset ();
209 HB_INTERNAL void clear ();
211 /* Called around shape() */
212 HB_INTERNAL void enter ();
213 HB_INTERNAL void leave ();
215 #ifndef HB_NO_BUFFER_VERIFY
218 bool verify (hb_buffer_t *text_buffer,
220 const hb_feature_t *features,
221 unsigned int num_features,
222 const char * const *shapers)
223 #ifndef HB_NO_BUFFER_VERIFY
229 unsigned int backtrack_len () const { return have_output ? out_len : idx; }
230 unsigned int lookahead_len () const { return len - idx; }
231 uint8_t next_serial () { return ++serial ? serial : ++serial; }
233 HB_INTERNAL void add (hb_codepoint_t codepoint,
234 unsigned int cluster);
235 HB_INTERNAL void add_info (const hb_glyph_info_t &glyph_info);
237 void reverse_range (unsigned start, unsigned end)
239 hb_array_t<hb_glyph_info_t> (info, len).reverse (start, end);
241 hb_array_t<hb_glyph_position_t> (pos, len).reverse (start, end);
243 void reverse () { reverse_range (0, len); }
245 template <typename FuncType>
246 void reverse_groups (const FuncType& group,
247 bool merge_clusters = false)
254 for (i = 1; i < len; i++)
256 if (!group (info[i - 1], info[i]))
259 this->merge_clusters (start, i);
260 reverse_range (start, i);
265 this->merge_clusters (start, i);
266 reverse_range (start, i);
271 template <typename FuncType>
272 unsigned group_end (unsigned start, const FuncType& group) const
274 while (++start < len && group (info[start - 1], info[start]))
280 static bool _cluster_group_func (const hb_glyph_info_t& a,
281 const hb_glyph_info_t& b)
282 { return a.cluster == b.cluster; }
284 void reverse_clusters () { reverse_groups (_cluster_group_func); }
286 HB_INTERNAL void guess_segment_properties ();
288 HB_INTERNAL void sync ();
289 HB_INTERNAL void clear_output ();
290 HB_INTERNAL void clear_positions ();
292 template <typename T>
293 HB_NODISCARD bool replace_glyphs (unsigned int num_in,
294 unsigned int num_out,
297 if (unlikely (!make_room_for (num_in, num_out))) return false;
299 assert (idx + num_in <= len);
301 merge_clusters (idx, idx + num_in);
303 hb_glyph_info_t &orig_info = idx < len ? cur() : prev();
305 hb_glyph_info_t *pinfo = &out_info[out_len];
306 for (unsigned int i = 0; i < num_out; i++)
309 pinfo->codepoint = glyph_data[i];
318 HB_NODISCARD bool replace_glyph (hb_codepoint_t glyph_index)
319 { return replace_glyphs (1, 1, &glyph_index); }
321 /* Makes a copy of the glyph at idx to output and replace glyph_index */
322 HB_NODISCARD bool output_glyph (hb_codepoint_t glyph_index)
323 { return replace_glyphs (0, 1, &glyph_index); }
325 HB_NODISCARD bool output_info (const hb_glyph_info_t &glyph_info)
327 if (unlikely (!make_room_for (0, 1))) return false;
329 out_info[out_len] = glyph_info;
334 /* Copies glyph at idx to output but doesn't advance idx */
335 HB_NODISCARD bool copy_glyph ()
337 /* Extra copy because cur()'s return can be freed within
338 * output_info() call if buffer reallocates. */
339 return output_info (hb_glyph_info_t (cur()));
342 /* Copies glyph at idx to output and advance idx.
343 * If there's no output, just advance idx. */
344 HB_NODISCARD bool next_glyph ()
348 if (out_info != info || out_len != idx)
350 if (unlikely (!make_room_for (1, 1))) return false;
351 out_info[out_len] = info[idx];
359 /* Copies n glyphs at idx to output and advance idx.
360 * If there's no output, just advance idx. */
361 HB_NODISCARD bool next_glyphs (unsigned int n)
365 if (out_info != info || out_len != idx)
367 if (unlikely (!make_room_for (n, n))) return false;
368 memmove (out_info + out_len, info + idx, n * sizeof (out_info[0]));
376 /* Advance idx without copying to output. */
377 void skip_glyph () { idx++; }
378 void reset_masks (hb_mask_t mask)
380 for (unsigned int j = 0; j < len; j++)
383 void add_masks (hb_mask_t mask)
385 for (unsigned int j = 0; j < len; j++)
386 info[j].mask |= mask;
388 HB_INTERNAL void set_masks (hb_mask_t value, hb_mask_t mask,
389 unsigned int cluster_start, unsigned int cluster_end);
391 void merge_clusters (unsigned int start, unsigned int end)
395 merge_clusters_impl (start, end);
397 HB_INTERNAL void merge_clusters_impl (unsigned int start, unsigned int end);
398 HB_INTERNAL void merge_out_clusters (unsigned int start, unsigned int end);
399 /* Merge clusters for deleting current glyph, and skip it. */
400 HB_INTERNAL void delete_glyph ();
403 /* Adds glyph flags in mask to infos with clusters between start and end.
404 * The start index will be from out-buffer if from_out_buffer is true.
405 * If interior is true, then the cluster having the minimum value is skipped. */
406 void _set_glyph_flags (hb_mask_t mask,
408 unsigned end = (unsigned) -1,
409 bool interior = false,
410 bool from_out_buffer = false)
412 end = hb_min (end, len);
414 if (interior && !from_out_buffer && end - start < 2)
417 scratch_flags |= HB_BUFFER_SCRATCH_FLAG_HAS_GLYPH_FLAGS;
419 if (!from_out_buffer || !have_output)
423 for (unsigned i = start; i < end; i++)
424 info[i].mask |= mask;
428 unsigned cluster = _infos_find_min_cluster (info, start, end);
429 _infos_set_glyph_flags (info, start, end, cluster, mask);
434 assert (start <= out_len);
439 for (unsigned i = start; i < out_len; i++)
440 out_info[i].mask |= mask;
441 for (unsigned i = idx; i < end; i++)
442 info[i].mask |= mask;
446 unsigned cluster = _infos_find_min_cluster (info, idx, end);
447 cluster = _infos_find_min_cluster (out_info, start, out_len, cluster);
449 _infos_set_glyph_flags (out_info, start, out_len, cluster, mask);
450 _infos_set_glyph_flags (info, idx, end, cluster, mask);
455 void unsafe_to_break (unsigned int start = 0, unsigned int end = -1)
457 _set_glyph_flags (HB_GLYPH_FLAG_UNSAFE_TO_BREAK | HB_GLYPH_FLAG_UNSAFE_TO_CONCAT,
461 void unsafe_to_concat (unsigned int start = 0, unsigned int end = -1)
463 _set_glyph_flags (HB_GLYPH_FLAG_UNSAFE_TO_CONCAT,
467 void unsafe_to_break_from_outbuffer (unsigned int start = 0, unsigned int end = -1)
469 _set_glyph_flags (HB_GLYPH_FLAG_UNSAFE_TO_BREAK | HB_GLYPH_FLAG_UNSAFE_TO_CONCAT,
473 void unsafe_to_concat_from_outbuffer (unsigned int start = 0, unsigned int end = -1)
475 _set_glyph_flags (HB_GLYPH_FLAG_UNSAFE_TO_CONCAT,
481 /* Internal methods */
482 HB_NODISCARD HB_INTERNAL bool move_to (unsigned int i); /* i is output-buffer index. */
484 HB_NODISCARD HB_INTERNAL bool enlarge (unsigned int size);
486 HB_NODISCARD bool ensure (unsigned int size)
487 { return likely (!size || size < allocated) ? true : enlarge (size); }
489 HB_NODISCARD bool ensure_inplace (unsigned int size)
490 { return likely (!size || size < allocated); }
492 void assert_glyphs ()
494 assert ((content_type == HB_BUFFER_CONTENT_TYPE_GLYPHS) ||
495 (!len && (content_type == HB_BUFFER_CONTENT_TYPE_INVALID)));
497 void assert_unicode ()
499 assert ((content_type == HB_BUFFER_CONTENT_TYPE_UNICODE) ||
500 (!len && (content_type == HB_BUFFER_CONTENT_TYPE_INVALID)));
502 HB_NODISCARD bool ensure_glyphs ()
504 if (unlikely (content_type != HB_BUFFER_CONTENT_TYPE_GLYPHS))
506 if (content_type != HB_BUFFER_CONTENT_TYPE_INVALID)
509 content_type = HB_BUFFER_CONTENT_TYPE_GLYPHS;
513 HB_NODISCARD bool ensure_unicode ()
515 if (unlikely (content_type != HB_BUFFER_CONTENT_TYPE_UNICODE))
517 if (content_type != HB_BUFFER_CONTENT_TYPE_INVALID)
520 content_type = HB_BUFFER_CONTENT_TYPE_UNICODE;
525 HB_NODISCARD HB_INTERNAL bool make_room_for (unsigned int num_in, unsigned int num_out);
526 HB_NODISCARD HB_INTERNAL bool shift_forward (unsigned int count);
528 typedef long scratch_buffer_t;
529 HB_INTERNAL scratch_buffer_t *get_scratch_buffer (unsigned int *size);
531 void clear_context (unsigned int side) { context_len[side] = 0; }
533 HB_INTERNAL void sort (unsigned int start, unsigned int end, int(*compar)(const hb_glyph_info_t *, const hb_glyph_info_t *));
537 #ifdef HB_NO_BUFFER_MESSAGE
540 return unlikely (message_func);
543 bool message (hb_font_t *font, const char *fmt, ...) HB_PRINTF_FUNC(3, 4)
545 #ifdef HB_NO_BUFFER_MESSAGE
555 bool ret = message_impl (font, fmt, ap);
563 HB_INTERNAL bool message_impl (hb_font_t *font, const char *fmt, va_list ap) HB_PRINTF_FUNC(3, 0);
566 set_cluster (hb_glyph_info_t &inf, unsigned int cluster, unsigned int mask = 0)
568 if (inf.cluster != cluster)
569 inf.mask = (inf.mask & ~HB_GLYPH_FLAG_DEFINED) | (mask & HB_GLYPH_FLAG_DEFINED);
570 inf.cluster = cluster;
573 _infos_set_glyph_flags (hb_glyph_info_t *infos,
574 unsigned int start, unsigned int end,
575 unsigned int cluster,
578 for (unsigned int i = start; i < end; i++)
579 if (cluster != infos[i].cluster)
581 scratch_flags |= HB_BUFFER_SCRATCH_FLAG_HAS_GLYPH_FLAGS;
582 infos[i].mask |= mask;
586 _infos_find_min_cluster (const hb_glyph_info_t *infos,
587 unsigned start, unsigned end,
588 unsigned cluster = UINT_MAX)
590 for (unsigned int i = start; i < end; i++)
591 cluster = hb_min (cluster, infos[i].cluster);
595 void clear_glyph_flags (hb_mask_t mask = 0)
597 for (unsigned int i = 0; i < len; i++)
598 info[i].mask = (info[i].mask & ~HB_GLYPH_FLAG_DEFINED) | (mask & HB_GLYPH_FLAG_DEFINED);
601 DECLARE_NULL_INSTANCE (hb_buffer_t);
604 #define foreach_group(buffer, start, end, group_func) \
606 _count = buffer->len, \
607 start = 0, end = _count ? buffer->group_end (0, group_func) : 0; \
609 start = end, end = buffer->group_end (start, group_func))
611 #define foreach_cluster(buffer, start, end) \
612 foreach_group (buffer, start, end, hb_buffer_t::_cluster_group_func)
615 #define HB_BUFFER_XALLOCATE_VAR(b, func, var) \
616 b->func (offsetof (hb_glyph_info_t, var) - offsetof(hb_glyph_info_t, var1), \
617 sizeof (b->info[0].var))
618 #define HB_BUFFER_ALLOCATE_VAR(b, var) HB_BUFFER_XALLOCATE_VAR (b, allocate_var, var ())
619 #define HB_BUFFER_DEALLOCATE_VAR(b, var) HB_BUFFER_XALLOCATE_VAR (b, deallocate_var, var ())
620 #define HB_BUFFER_ASSERT_VAR(b, var) HB_BUFFER_XALLOCATE_VAR (b, assert_var, var ())
623 #endif /* HB_BUFFER_HH */