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"
35 #include "hb-set-digest.hh"
38 static_assert ((sizeof (hb_glyph_info_t
) == 20), "");
39 static_assert ((sizeof (hb_glyph_info_t
) == sizeof (hb_glyph_position_t
)), "");
41 HB_MARK_AS_FLAG_T (hb_glyph_flags_t
);
42 HB_MARK_AS_FLAG_T (hb_buffer_flags_t
);
43 HB_MARK_AS_FLAG_T (hb_buffer_serialize_flags_t
);
44 HB_MARK_AS_FLAG_T (hb_buffer_diff_flags_t
);
46 enum hb_buffer_scratch_flags_t
{
47 HB_BUFFER_SCRATCH_FLAG_DEFAULT
= 0x00000000u
,
48 HB_BUFFER_SCRATCH_FLAG_HAS_NON_ASCII
= 0x00000001u
,
49 HB_BUFFER_SCRATCH_FLAG_HAS_DEFAULT_IGNORABLES
= 0x00000002u
,
50 HB_BUFFER_SCRATCH_FLAG_HAS_SPACE_FALLBACK
= 0x00000004u
,
51 HB_BUFFER_SCRATCH_FLAG_HAS_GPOS_ATTACHMENT
= 0x00000008u
,
52 HB_BUFFER_SCRATCH_FLAG_HAS_CGJ
= 0x00000010u
,
53 HB_BUFFER_SCRATCH_FLAG_HAS_GLYPH_FLAGS
= 0x00000020u
,
54 HB_BUFFER_SCRATCH_FLAG_HAS_BROKEN_SYLLABLE
= 0x00000040u
,
56 /* Reserved for shapers' internal use. */
57 HB_BUFFER_SCRATCH_FLAG_SHAPER0
= 0x01000000u
,
58 HB_BUFFER_SCRATCH_FLAG_SHAPER1
= 0x02000000u
,
59 HB_BUFFER_SCRATCH_FLAG_SHAPER2
= 0x04000000u
,
60 HB_BUFFER_SCRATCH_FLAG_SHAPER3
= 0x08000000u
,
62 HB_MARK_AS_FLAG_T (hb_buffer_scratch_flags_t
);
71 hb_object_header_t header
;
74 * Information about how the text in the buffer should be treated.
77 hb_unicode_funcs_t
*unicode
; /* Unicode functions */
78 hb_buffer_flags_t flags
; /* BOT / EOT / etc. */
79 hb_buffer_cluster_level_t cluster_level
;
80 hb_codepoint_t replacement
; /* U+FFFD or something else. */
81 hb_codepoint_t invisible
; /* 0 or something else. */
82 hb_codepoint_t not_found
; /* 0 or something else. */
88 hb_buffer_content_type_t content_type
;
89 hb_segment_properties_t props
; /* Script, language, direction */
91 bool successful
; /* Allocations successful */
92 bool shaping_failed
; /* Shaping failure */
93 bool have_output
; /* Whether we have an output buffer going on */
94 bool have_positions
; /* Whether we have positions */
96 unsigned int idx
; /* Cursor into ->info and ->pos arrays */
97 unsigned int len
; /* Length of ->info and ->pos arrays */
98 unsigned int out_len
; /* Length of ->out_info array if have_output */
100 unsigned int allocated
; /* Length of allocated arrays */
101 hb_glyph_info_t
*info
;
102 hb_glyph_info_t
*out_info
;
103 hb_glyph_position_t
*pos
;
105 /* Text before / after the main buffer contents.
106 * Always in Unicode, and ordered outward.
107 * Index 0 is for "pre-context", 1 for "post-context". */
108 static constexpr unsigned CONTEXT_LENGTH
= 5u;
109 hb_codepoint_t context
[2][CONTEXT_LENGTH
];
110 unsigned int context_len
[2];
114 * Managed by enter / leave
117 uint8_t allocated_var_bits
;
119 hb_buffer_scratch_flags_t scratch_flags
; /* Have space-fallback, etc. */
120 unsigned int max_len
; /* Maximum allowed len. */
121 int max_ops
; /* Maximum allowed operations. */
122 /* The bits here reflect current allocations of the bytes in glyph_info_t's var1 and var2. */
129 #ifndef HB_NO_BUFFER_MESSAGE
130 hb_buffer_message_func_t message_func
;
132 hb_destroy_func_t message_destroy
;
133 unsigned message_depth
; /* How deeply are we inside a message callback? */
135 static constexpr unsigned message_depth
= 0u;
142 HB_NODISCARD
bool in_error () const { return !successful
; }
144 void allocate_var (unsigned int start
, unsigned int count
)
146 unsigned int end
= start
+ count
;
148 unsigned int bits
= (1u<<end
) - (1u<<start
);
149 assert (0 == (allocated_var_bits
& bits
));
150 allocated_var_bits
|= bits
;
152 bool try_allocate_var (unsigned int start
, unsigned int count
)
154 unsigned int end
= start
+ count
;
156 unsigned int bits
= (1u<<end
) - (1u<<start
);
157 if (allocated_var_bits
& bits
)
159 allocated_var_bits
|= bits
;
162 void deallocate_var (unsigned int start
, unsigned int count
)
164 unsigned int end
= start
+ count
;
166 unsigned int bits
= (1u<<end
) - (1u<<start
);
167 assert (bits
== (allocated_var_bits
& bits
));
168 allocated_var_bits
&= ~bits
;
170 void assert_var (unsigned int start
, unsigned int count
)
172 unsigned int end
= start
+ count
;
174 HB_UNUSED
unsigned int bits
= (1u<<end
) - (1u<<start
);
175 assert (bits
== (allocated_var_bits
& bits
));
177 void deallocate_var_all ()
179 allocated_var_bits
= 0;
182 hb_glyph_info_t
&cur (unsigned int i
= 0) { return info
[idx
+ i
]; }
183 hb_glyph_info_t
cur (unsigned int i
= 0) const { return info
[idx
+ i
]; }
185 hb_glyph_position_t
&cur_pos (unsigned int i
= 0) { return pos
[idx
+ i
]; }
186 hb_glyph_position_t
cur_pos (unsigned int i
= 0) const { return pos
[idx
+ i
]; }
188 hb_glyph_info_t
&prev () { return out_info
[out_len
? out_len
- 1 : 0]; }
189 hb_glyph_info_t
prev () const { return out_info
[out_len
? out_len
- 1 : 0]; }
191 hb_set_digest_t
digest () const
195 d
.add_array (&info
[0].codepoint
, len
, sizeof (info
[0]));
199 HB_INTERNAL
void similar (const hb_buffer_t
&src
);
200 HB_INTERNAL
void reset ();
201 HB_INTERNAL
void clear ();
203 /* Called around shape() */
204 HB_INTERNAL
void enter ();
205 HB_INTERNAL
void leave ();
207 #ifndef HB_NO_BUFFER_VERIFY
210 bool verify (hb_buffer_t
*text_buffer
,
212 const hb_feature_t
*features
,
213 unsigned int num_features
,
214 const char * const *shapers
)
215 #ifndef HB_NO_BUFFER_VERIFY
221 unsigned int backtrack_len () const { return have_output
? out_len
: idx
; }
222 unsigned int lookahead_len () const { return len
- idx
; }
223 uint8_t next_serial () { return ++serial
? serial
: ++serial
; }
225 HB_INTERNAL
void add (hb_codepoint_t codepoint
,
226 unsigned int cluster
);
227 HB_INTERNAL
void add_info (const hb_glyph_info_t
&glyph_info
);
229 void reverse_range (unsigned start
, unsigned end
)
231 hb_array_t
<hb_glyph_info_t
> (info
, len
).reverse (start
, end
);
233 hb_array_t
<hb_glyph_position_t
> (pos
, len
).reverse (start
, end
);
235 void reverse () { reverse_range (0, len
); }
237 template <typename FuncType
>
238 void reverse_groups (const FuncType
& group
,
239 bool merge_clusters
= false)
246 for (i
= 1; i
< len
; i
++)
248 if (!group (info
[i
- 1], info
[i
]))
251 this->merge_clusters (start
, i
);
252 reverse_range (start
, i
);
257 this->merge_clusters (start
, i
);
258 reverse_range (start
, i
);
263 template <typename FuncType
>
264 unsigned group_end (unsigned start
, const FuncType
& group
) const
266 while (++start
< len
&& group (info
[start
- 1], info
[start
]))
272 static bool _cluster_group_func (const hb_glyph_info_t
& a
,
273 const hb_glyph_info_t
& b
)
274 { return a
.cluster
== b
.cluster
; }
276 void reverse_clusters () { reverse_groups (_cluster_group_func
); }
278 HB_INTERNAL
void guess_segment_properties ();
280 HB_INTERNAL
bool sync ();
281 HB_INTERNAL
int sync_so_far ();
282 HB_INTERNAL
void clear_output ();
283 HB_INTERNAL
void clear_positions ();
285 template <typename T
>
286 HB_NODISCARD
bool replace_glyphs (unsigned int num_in
,
287 unsigned int num_out
,
290 if (unlikely (!make_room_for (num_in
, num_out
))) return false;
292 assert (idx
+ num_in
<= len
);
294 merge_clusters (idx
, idx
+ num_in
);
296 hb_glyph_info_t
&orig_info
= idx
< len
? cur() : prev();
298 hb_glyph_info_t
*pinfo
= &out_info
[out_len
];
299 for (unsigned int i
= 0; i
< num_out
; i
++)
302 pinfo
->codepoint
= glyph_data
[i
];
311 HB_NODISCARD
bool replace_glyph (hb_codepoint_t glyph_index
)
312 { return replace_glyphs (1, 1, &glyph_index
); }
314 /* Makes a copy of the glyph at idx to output and replace glyph_index */
315 HB_NODISCARD
bool output_glyph (hb_codepoint_t glyph_index
)
316 { return replace_glyphs (0, 1, &glyph_index
); }
318 HB_NODISCARD
bool output_info (const hb_glyph_info_t
&glyph_info
)
320 if (unlikely (!make_room_for (0, 1))) return false;
322 out_info
[out_len
] = glyph_info
;
327 /* Copies glyph at idx to output but doesn't advance idx */
328 HB_NODISCARD
bool copy_glyph ()
330 /* Extra copy because cur()'s return can be freed within
331 * output_info() call if buffer reallocates. */
332 return output_info (hb_glyph_info_t (cur()));
335 /* Copies glyph at idx to output and advance idx.
336 * If there's no output, just advance idx. */
337 HB_NODISCARD
bool next_glyph ()
341 if (out_info
!= info
|| out_len
!= idx
)
343 if (unlikely (!make_room_for (1, 1))) return false;
344 out_info
[out_len
] = info
[idx
];
352 /* Copies n glyphs at idx to output and advance idx.
353 * If there's no output, just advance idx. */
354 HB_NODISCARD
bool next_glyphs (unsigned int n
)
358 if (out_info
!= info
|| out_len
!= idx
)
360 if (unlikely (!make_room_for (n
, n
))) return false;
361 memmove (out_info
+ out_len
, info
+ idx
, n
* sizeof (out_info
[0]));
369 /* Advance idx without copying to output. */
370 void skip_glyph () { idx
++; }
371 void reset_masks (hb_mask_t mask
)
373 for (unsigned int j
= 0; j
< len
; j
++)
376 void add_masks (hb_mask_t mask
)
378 for (unsigned int j
= 0; j
< len
; j
++)
379 info
[j
].mask
|= mask
;
381 HB_INTERNAL
void set_masks (hb_mask_t value
, hb_mask_t mask
,
382 unsigned int cluster_start
, unsigned int cluster_end
);
384 void merge_clusters (unsigned int start
, unsigned int end
)
388 merge_clusters_impl (start
, end
);
390 HB_INTERNAL
void merge_clusters_impl (unsigned int start
, unsigned int end
);
391 HB_INTERNAL
void merge_out_clusters (unsigned int start
, unsigned int end
);
392 /* Merge clusters for deleting current glyph, and skip it. */
393 HB_INTERNAL
void delete_glyph ();
394 HB_INTERNAL
void delete_glyphs_inplace (bool (*filter
) (const hb_glyph_info_t
*info
));
398 /* Adds glyph flags in mask to infos with clusters between start and end.
399 * The start index will be from out-buffer if from_out_buffer is true.
400 * If interior is true, then the cluster having the minimum value is skipped. */
401 void _set_glyph_flags (hb_mask_t mask
,
403 unsigned end
= (unsigned) -1,
404 bool interior
= false,
405 bool from_out_buffer
= false)
407 end
= hb_min (end
, len
);
409 if (interior
&& !from_out_buffer
&& end
- start
< 2)
412 scratch_flags
|= HB_BUFFER_SCRATCH_FLAG_HAS_GLYPH_FLAGS
;
414 if (!from_out_buffer
|| !have_output
)
418 for (unsigned i
= start
; i
< end
; i
++)
419 info
[i
].mask
|= mask
;
423 unsigned cluster
= _infos_find_min_cluster (info
, start
, end
);
424 _infos_set_glyph_flags (info
, start
, end
, cluster
, mask
);
429 assert (start
<= out_len
);
434 for (unsigned i
= start
; i
< out_len
; i
++)
435 out_info
[i
].mask
|= mask
;
436 for (unsigned i
= idx
; i
< end
; i
++)
437 info
[i
].mask
|= mask
;
441 unsigned cluster
= _infos_find_min_cluster (info
, idx
, end
);
442 cluster
= _infos_find_min_cluster (out_info
, start
, out_len
, cluster
);
444 _infos_set_glyph_flags (out_info
, start
, out_len
, cluster
, mask
);
445 _infos_set_glyph_flags (info
, idx
, end
, cluster
, mask
);
450 void unsafe_to_break (unsigned int start
= 0, unsigned int end
= -1)
452 _set_glyph_flags (HB_GLYPH_FLAG_UNSAFE_TO_BREAK
| HB_GLYPH_FLAG_UNSAFE_TO_CONCAT
,
456 void safe_to_insert_tatweel (unsigned int start
= 0, unsigned int end
= -1)
458 if ((flags
& HB_BUFFER_FLAG_PRODUCE_SAFE_TO_INSERT_TATWEEL
) == 0)
460 unsafe_to_break (start
, end
);
463 _set_glyph_flags (HB_GLYPH_FLAG_SAFE_TO_INSERT_TATWEEL
,
467 #ifndef HB_OPTIMIZE_SIZE
470 void unsafe_to_concat (unsigned int start
= 0, unsigned int end
= -1)
472 if (likely ((flags
& HB_BUFFER_FLAG_PRODUCE_UNSAFE_TO_CONCAT
) == 0))
474 _set_glyph_flags (HB_GLYPH_FLAG_UNSAFE_TO_CONCAT
,
478 void unsafe_to_break_from_outbuffer (unsigned int start
= 0, unsigned int end
= -1)
480 _set_glyph_flags (HB_GLYPH_FLAG_UNSAFE_TO_BREAK
| HB_GLYPH_FLAG_UNSAFE_TO_CONCAT
,
484 #ifndef HB_OPTIMIZE_SIZE
487 void unsafe_to_concat_from_outbuffer (unsigned int start
= 0, unsigned int end
= -1)
489 if (likely ((flags
& HB_BUFFER_FLAG_PRODUCE_UNSAFE_TO_CONCAT
) == 0))
491 _set_glyph_flags (HB_GLYPH_FLAG_UNSAFE_TO_CONCAT
,
497 /* Internal methods */
498 HB_NODISCARD HB_INTERNAL
bool move_to (unsigned int i
); /* i is output-buffer index. */
500 HB_NODISCARD HB_INTERNAL
bool enlarge (unsigned int size
);
502 HB_NODISCARD
bool resize (unsigned length
)
504 assert (!have_output
);
505 if (unlikely (!ensure (length
))) return false;
509 HB_NODISCARD
bool ensure (unsigned int size
)
510 { return likely (!size
|| size
< allocated
) ? true : enlarge (size
); }
512 HB_NODISCARD
bool ensure_inplace (unsigned int size
)
513 { return likely (!size
|| size
< allocated
); }
515 void assert_glyphs ()
517 assert ((content_type
== HB_BUFFER_CONTENT_TYPE_GLYPHS
) ||
518 (!len
&& (content_type
== HB_BUFFER_CONTENT_TYPE_INVALID
)));
520 void assert_unicode ()
522 assert ((content_type
== HB_BUFFER_CONTENT_TYPE_UNICODE
) ||
523 (!len
&& (content_type
== HB_BUFFER_CONTENT_TYPE_INVALID
)));
525 HB_NODISCARD
bool ensure_glyphs ()
527 if (unlikely (content_type
!= HB_BUFFER_CONTENT_TYPE_GLYPHS
))
529 if (content_type
!= HB_BUFFER_CONTENT_TYPE_INVALID
)
532 content_type
= HB_BUFFER_CONTENT_TYPE_GLYPHS
;
536 HB_NODISCARD
bool ensure_unicode ()
538 if (unlikely (content_type
!= HB_BUFFER_CONTENT_TYPE_UNICODE
))
540 if (content_type
!= HB_BUFFER_CONTENT_TYPE_INVALID
)
543 content_type
= HB_BUFFER_CONTENT_TYPE_UNICODE
;
548 HB_NODISCARD HB_INTERNAL
bool make_room_for (unsigned int num_in
, unsigned int num_out
);
549 HB_NODISCARD HB_INTERNAL
bool shift_forward (unsigned int count
);
551 typedef long scratch_buffer_t
;
552 HB_INTERNAL scratch_buffer_t
*get_scratch_buffer (unsigned int *size
);
554 void clear_context (unsigned int side
) { context_len
[side
] = 0; }
556 HB_INTERNAL
void sort (unsigned int start
, unsigned int end
, int(*compar
)(const hb_glyph_info_t
*, const hb_glyph_info_t
*));
560 #ifdef HB_NO_BUFFER_MESSAGE
563 return unlikely (message_func
);
566 bool message (hb_font_t
*font
, const char *fmt
, ...) HB_PRINTF_FUNC(3, 4)
568 #ifdef HB_NO_BUFFER_MESSAGE
571 if (likely (!messaging ()))
576 bool ret
= message_impl (font
, fmt
, ap
);
582 HB_INTERNAL
bool message_impl (hb_font_t
*font
, const char *fmt
, va_list ap
) HB_PRINTF_FUNC(3, 0);
585 set_cluster (hb_glyph_info_t
&inf
, unsigned int cluster
, unsigned int mask
= 0)
587 if (inf
.cluster
!= cluster
)
588 inf
.mask
= (inf
.mask
& ~HB_GLYPH_FLAG_DEFINED
) | (mask
& HB_GLYPH_FLAG_DEFINED
);
589 inf
.cluster
= cluster
;
592 _infos_set_glyph_flags (hb_glyph_info_t
*infos
,
593 unsigned int start
, unsigned int end
,
594 unsigned int cluster
,
597 if (unlikely (start
== end
))
600 unsigned cluster_first
= infos
[start
].cluster
;
601 unsigned cluster_last
= infos
[end
- 1].cluster
;
603 if (cluster_level
== HB_BUFFER_CLUSTER_LEVEL_CHARACTERS
||
604 (cluster
!= cluster_first
&& cluster
!= cluster_last
))
606 for (unsigned int i
= start
; i
< end
; i
++)
607 if (cluster
!= infos
[i
].cluster
)
609 scratch_flags
|= HB_BUFFER_SCRATCH_FLAG_HAS_GLYPH_FLAGS
;
610 infos
[i
].mask
|= mask
;
615 /* Monotone clusters */
617 if (cluster
== cluster_first
)
619 for (unsigned int i
= end
; start
< i
&& infos
[i
- 1].cluster
!= cluster_first
; i
--)
621 scratch_flags
|= HB_BUFFER_SCRATCH_FLAG_HAS_GLYPH_FLAGS
;
622 infos
[i
- 1].mask
|= mask
;
625 else /* cluster == cluster_last */
627 for (unsigned int i
= start
; i
< end
&& infos
[i
].cluster
!= cluster_last
; i
++)
629 scratch_flags
|= HB_BUFFER_SCRATCH_FLAG_HAS_GLYPH_FLAGS
;
630 infos
[i
].mask
|= mask
;
635 _infos_find_min_cluster (const hb_glyph_info_t
*infos
,
636 unsigned start
, unsigned end
,
637 unsigned cluster
= UINT_MAX
)
639 if (unlikely (start
== end
))
642 if (cluster_level
== HB_BUFFER_CLUSTER_LEVEL_CHARACTERS
)
644 for (unsigned int i
= start
; i
< end
; i
++)
645 cluster
= hb_min (cluster
, infos
[i
].cluster
);
649 return hb_min (cluster
, hb_min (infos
[start
].cluster
, infos
[end
- 1].cluster
));
652 void clear_glyph_flags (hb_mask_t mask
= 0)
654 for (unsigned int i
= 0; i
< len
; i
++)
655 info
[i
].mask
= (info
[i
].mask
& ~HB_GLYPH_FLAG_DEFINED
) | (mask
& HB_GLYPH_FLAG_DEFINED
);
658 DECLARE_NULL_INSTANCE (hb_buffer_t
);
661 #define foreach_group(buffer, start, end, group_func) \
663 _count = buffer->len, \
664 start = 0, end = _count ? buffer->group_end (0, group_func) : 0; \
666 start = end, end = buffer->group_end (start, group_func))
668 #define foreach_cluster(buffer, start, end) \
669 foreach_group (buffer, start, end, hb_buffer_t::_cluster_group_func)
672 #define HB_BUFFER_XALLOCATE_VAR(b, func, var) \
673 b->func (offsetof (hb_glyph_info_t, var) - offsetof(hb_glyph_info_t, var1), \
674 sizeof (b->info[0].var))
675 #define HB_BUFFER_ALLOCATE_VAR(b, var) HB_BUFFER_XALLOCATE_VAR (b, allocate_var, var ())
676 #define HB_BUFFER_TRY_ALLOCATE_VAR(b, var) HB_BUFFER_XALLOCATE_VAR (b, try_allocate_var, var ())
677 #define HB_BUFFER_DEALLOCATE_VAR(b, var) HB_BUFFER_XALLOCATE_VAR (b, deallocate_var, var ())
678 #define HB_BUFFER_ASSERT_VAR(b, var) HB_BUFFER_XALLOCATE_VAR (b, assert_var, var ())
681 #endif /* HB_BUFFER_HH */