Mailbox support for texture layers.
[chromium-blink-merge.git] / third_party / harfbuzz-ng / src / hb-buffer.cc
blob06b5c05ee20ab686858f305c3bfca682fe271e52
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.
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
18 * DAMAGE.
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
30 #include "hb-buffer-private.hh"
31 #include "hb-utf-private.hh"
34 #ifndef HB_DEBUG_BUFFER
35 #define HB_DEBUG_BUFFER (HB_DEBUG+0)
36 #endif
38 /* Here is how the buffer works internally:
40 * There are two info pointers: info and out_info. They always have
41 * the same allocated size, but different lengths.
43 * As an optimization, both info and out_info may point to the
44 * same piece of memory, which is owned by info. This remains the
45 * case as long as out_len doesn't exceed i at any time.
46 * In that case, swap_buffers() is no-op and the glyph operations operate
47 * mostly in-place.
49 * As soon as out_info gets longer than info, out_info is moved over
50 * to an alternate buffer (which we reuse the pos buffer for!), and its
51 * current contents (out_len entries) are copied to the new place.
52 * This should all remain transparent to the user. swap_buffers() then
53 * switches info and out_info.
58 /* Internal API */
60 bool
61 hb_buffer_t::enlarge (unsigned int size)
63 if (unlikely (in_error))
64 return false;
66 unsigned int new_allocated = allocated;
67 hb_glyph_position_t *new_pos = NULL;
68 hb_glyph_info_t *new_info = NULL;
69 bool separate_out = out_info != info;
71 if (unlikely (_hb_unsigned_int_mul_overflows (size, sizeof (info[0]))))
72 goto done;
74 while (size >= new_allocated)
75 new_allocated += (new_allocated >> 1) + 32;
77 ASSERT_STATIC (sizeof (info[0]) == sizeof (pos[0]));
78 if (unlikely (_hb_unsigned_int_mul_overflows (new_allocated, sizeof (info[0]))))
79 goto done;
81 new_pos = (hb_glyph_position_t *) realloc (pos, new_allocated * sizeof (pos[0]));
82 new_info = (hb_glyph_info_t *) realloc (info, new_allocated * sizeof (info[0]));
84 done:
85 if (unlikely (!new_pos || !new_info))
86 in_error = true;
88 if (likely (new_pos))
89 pos = new_pos;
91 if (likely (new_info))
92 info = new_info;
94 out_info = separate_out ? (hb_glyph_info_t *) pos : info;
95 if (likely (!in_error))
96 allocated = new_allocated;
98 return likely (!in_error);
101 bool
102 hb_buffer_t::make_room_for (unsigned int num_in,
103 unsigned int num_out)
105 if (unlikely (!ensure (out_len + num_out))) return false;
107 if (out_info == info &&
108 out_len + num_out > idx + num_in)
110 assert (have_output);
112 out_info = (hb_glyph_info_t *) pos;
113 memcpy (out_info, info, out_len * sizeof (out_info[0]));
116 return true;
119 void *
120 hb_buffer_t::get_scratch_buffer (unsigned int *size)
122 have_output = false;
123 have_positions = false;
125 out_len = 0;
126 out_info = info;
128 *size = allocated * sizeof (pos[0]);
129 return pos;
134 /* HarfBuzz-Internal API */
136 void
137 hb_buffer_t::reset (void)
139 if (unlikely (hb_object_is_inert (this)))
140 return;
142 hb_unicode_funcs_destroy (unicode);
143 unicode = hb_unicode_funcs_get_default ();
145 hb_segment_properties_t default_props = _HB_BUFFER_PROPS_DEFAULT;
146 props = default_props;
148 content_type = HB_BUFFER_CONTENT_TYPE_INVALID;
149 in_error = false;
150 have_output = false;
151 have_positions = false;
153 idx = 0;
154 len = 0;
155 out_len = 0;
156 out_info = info;
158 serial = 0;
159 memset (allocated_var_bytes, 0, sizeof allocated_var_bytes);
160 memset (allocated_var_owner, 0, sizeof allocated_var_owner);
162 memset (context, 0, sizeof context);
163 memset (context_len, 0, sizeof context_len);
166 void
167 hb_buffer_t::add (hb_codepoint_t codepoint,
168 hb_mask_t mask,
169 unsigned int cluster)
171 hb_glyph_info_t *glyph;
173 if (unlikely (!ensure (len + 1))) return;
175 glyph = &info[len];
177 memset (glyph, 0, sizeof (*glyph));
178 glyph->codepoint = codepoint;
179 glyph->mask = mask;
180 glyph->cluster = cluster;
182 len++;
185 void
186 hb_buffer_t::remove_output (void)
188 if (unlikely (hb_object_is_inert (this)))
189 return;
191 have_output = false;
192 have_positions = false;
194 out_len = 0;
195 out_info = info;
198 void
199 hb_buffer_t::clear_output (void)
201 if (unlikely (hb_object_is_inert (this)))
202 return;
204 have_output = true;
205 have_positions = false;
207 out_len = 0;
208 out_info = info;
211 void
212 hb_buffer_t::clear_positions (void)
214 if (unlikely (hb_object_is_inert (this)))
215 return;
217 have_output = false;
218 have_positions = true;
220 out_len = 0;
221 out_info = info;
223 memset (pos, 0, sizeof (pos[0]) * len);
226 void
227 hb_buffer_t::swap_buffers (void)
229 if (unlikely (in_error)) return;
231 assert (have_output);
232 have_output = false;
234 if (out_info != info)
236 hb_glyph_info_t *tmp_string;
237 tmp_string = info;
238 info = out_info;
239 out_info = tmp_string;
240 pos = (hb_glyph_position_t *) out_info;
243 unsigned int tmp;
244 tmp = len;
245 len = out_len;
246 out_len = tmp;
248 idx = 0;
252 void
253 hb_buffer_t::replace_glyphs (unsigned int num_in,
254 unsigned int num_out,
255 const uint32_t *glyph_data)
257 if (unlikely (!make_room_for (num_in, num_out))) return;
259 merge_clusters (idx, idx + num_in);
261 hb_glyph_info_t orig_info = info[idx];
262 hb_glyph_info_t *pinfo = &out_info[out_len];
263 for (unsigned int i = 0; i < num_out; i++)
265 *pinfo = orig_info;
266 pinfo->codepoint = glyph_data[i];
267 pinfo++;
270 idx += num_in;
271 out_len += num_out;
274 void
275 hb_buffer_t::output_glyph (hb_codepoint_t glyph_index)
277 if (unlikely (!make_room_for (0, 1))) return;
279 out_info[out_len] = info[idx];
280 out_info[out_len].codepoint = glyph_index;
282 out_len++;
285 void
286 hb_buffer_t::output_info (hb_glyph_info_t &glyph_info)
288 if (unlikely (!make_room_for (0, 1))) return;
290 out_info[out_len] = glyph_info;
292 out_len++;
295 void
296 hb_buffer_t::copy_glyph (void)
298 if (unlikely (!make_room_for (0, 1))) return;
300 out_info[out_len] = info[idx];
302 out_len++;
305 void
306 hb_buffer_t::replace_glyph (hb_codepoint_t glyph_index)
308 if (unlikely (out_info != info || out_len != idx)) {
309 if (unlikely (!make_room_for (1, 1))) return;
310 out_info[out_len] = info[idx];
312 out_info[out_len].codepoint = glyph_index;
314 idx++;
315 out_len++;
319 void
320 hb_buffer_t::set_masks (hb_mask_t value,
321 hb_mask_t mask,
322 unsigned int cluster_start,
323 unsigned int cluster_end)
325 hb_mask_t not_mask = ~mask;
326 value &= mask;
328 if (!mask)
329 return;
331 if (cluster_start == 0 && cluster_end == (unsigned int)-1) {
332 unsigned int count = len;
333 for (unsigned int i = 0; i < count; i++)
334 info[i].mask = (info[i].mask & not_mask) | value;
335 return;
338 unsigned int count = len;
339 for (unsigned int i = 0; i < count; i++)
340 if (cluster_start <= info[i].cluster && info[i].cluster < cluster_end)
341 info[i].mask = (info[i].mask & not_mask) | value;
344 void
345 hb_buffer_t::reverse_range (unsigned int start,
346 unsigned int end)
348 unsigned int i, j;
350 if (start == end - 1)
351 return;
353 for (i = start, j = end - 1; i < j; i++, j--) {
354 hb_glyph_info_t t;
356 t = info[i];
357 info[i] = info[j];
358 info[j] = t;
361 if (pos) {
362 for (i = start, j = end - 1; i < j; i++, j--) {
363 hb_glyph_position_t t;
365 t = pos[i];
366 pos[i] = pos[j];
367 pos[j] = t;
372 void
373 hb_buffer_t::reverse (void)
375 if (unlikely (!len))
376 return;
378 reverse_range (0, len);
381 void
382 hb_buffer_t::reverse_clusters (void)
384 unsigned int i, start, count, last_cluster;
386 if (unlikely (!len))
387 return;
389 reverse ();
391 count = len;
392 start = 0;
393 last_cluster = info[0].cluster;
394 for (i = 1; i < count; i++) {
395 if (last_cluster != info[i].cluster) {
396 reverse_range (start, i);
397 start = i;
398 last_cluster = info[i].cluster;
401 reverse_range (start, i);
404 void
405 hb_buffer_t::merge_clusters (unsigned int start,
406 unsigned int end)
408 if (unlikely (end - start < 2))
409 return;
411 unsigned int cluster = info[start].cluster;
413 for (unsigned int i = start + 1; i < end; i++)
414 cluster = MIN (cluster, info[i].cluster);
416 /* Extend end */
417 while (end < len && info[end - 1].cluster == info[end].cluster)
418 end++;
420 /* Extend start */
421 while (idx < start && info[start - 1].cluster == info[start].cluster)
422 start--;
424 /* If we hit the start of buffer, continue in out-buffer. */
425 if (idx == start)
426 for (unsigned i = out_len; i && out_info[i - 1].cluster == info[start].cluster; i--)
427 out_info[i - 1].cluster = cluster;
429 for (unsigned int i = start; i < end; i++)
430 info[i].cluster = cluster;
432 void
433 hb_buffer_t::merge_out_clusters (unsigned int start,
434 unsigned int end)
436 if (unlikely (end - start < 2))
437 return;
439 unsigned int cluster = out_info[start].cluster;
441 for (unsigned int i = start + 1; i < end; i++)
442 cluster = MIN (cluster, out_info[i].cluster);
444 /* Extend start */
445 while (start && out_info[start - 1].cluster == out_info[start].cluster)
446 start--;
448 /* Extend end */
449 while (end < out_len && out_info[end - 1].cluster == out_info[end].cluster)
450 end++;
452 /* If we hit the end of out-buffer, continue in buffer. */
453 if (end == out_len)
454 for (unsigned i = idx; i < len && info[i].cluster == out_info[end - 1].cluster; i++)
455 info[i].cluster = cluster;
457 for (unsigned int i = start; i < end; i++)
458 out_info[i].cluster = cluster;
461 void
462 hb_buffer_t::guess_properties (void)
464 if (unlikely (!len)) return;
465 assert (content_type == HB_BUFFER_CONTENT_TYPE_UNICODE);
467 /* If script is set to INVALID, guess from buffer contents */
468 if (props.script == HB_SCRIPT_INVALID) {
469 for (unsigned int i = 0; i < len; i++) {
470 hb_script_t script = unicode->script (info[i].codepoint);
471 if (likely (script != HB_SCRIPT_COMMON &&
472 script != HB_SCRIPT_INHERITED &&
473 script != HB_SCRIPT_UNKNOWN)) {
474 props.script = script;
475 break;
480 /* If direction is set to INVALID, guess from script */
481 if (props.direction == HB_DIRECTION_INVALID) {
482 props.direction = hb_script_get_horizontal_direction (props.script);
485 /* If language is not set, use default language from locale */
486 if (props.language == HB_LANGUAGE_INVALID) {
487 /* TODO get_default_for_script? using $LANGUAGE */
488 props.language = hb_language_get_default ();
493 static inline void
494 dump_var_allocation (const hb_buffer_t *buffer)
496 char buf[80];
497 for (unsigned int i = 0; i < 8; i++)
498 buf[i] = '0' + buffer->allocated_var_bytes[7 - i];
499 buf[8] = '\0';
500 DEBUG_MSG (BUFFER, buffer,
501 "Current var allocation: %s",
502 buf);
505 void hb_buffer_t::allocate_var (unsigned int byte_i, unsigned int count, const char *owner)
507 assert (byte_i < 8 && byte_i + count <= 8);
509 if (DEBUG (BUFFER))
510 dump_var_allocation (this);
511 DEBUG_MSG (BUFFER, this,
512 "Allocating var bytes %d..%d for %s",
513 byte_i, byte_i + count - 1, owner);
515 for (unsigned int i = byte_i; i < byte_i + count; i++) {
516 assert (!allocated_var_bytes[i]);
517 allocated_var_bytes[i]++;
518 allocated_var_owner[i] = owner;
522 void hb_buffer_t::deallocate_var (unsigned int byte_i, unsigned int count, const char *owner)
524 if (DEBUG (BUFFER))
525 dump_var_allocation (this);
527 DEBUG_MSG (BUFFER, this,
528 "Deallocating var bytes %d..%d for %s",
529 byte_i, byte_i + count - 1, owner);
531 assert (byte_i < 8 && byte_i + count <= 8);
532 for (unsigned int i = byte_i; i < byte_i + count; i++) {
533 assert (allocated_var_bytes[i]);
534 assert (0 == strcmp (allocated_var_owner[i], owner));
535 allocated_var_bytes[i]--;
539 void hb_buffer_t::assert_var (unsigned int byte_i, unsigned int count, const char *owner)
541 if (DEBUG (BUFFER))
542 dump_var_allocation (this);
544 DEBUG_MSG (BUFFER, this,
545 "Asserting var bytes %d..%d for %s",
546 byte_i, byte_i + count - 1, owner);
548 assert (byte_i < 8 && byte_i + count <= 8);
549 for (unsigned int i = byte_i; i < byte_i + count; i++) {
550 assert (allocated_var_bytes[i]);
551 assert (0 == strcmp (allocated_var_owner[i], owner));
555 void hb_buffer_t::deallocate_var_all (void)
557 memset (allocated_var_bytes, 0, sizeof (allocated_var_bytes));
558 memset (allocated_var_owner, 0, sizeof (allocated_var_owner));
561 /* Public API */
563 hb_buffer_t *
564 hb_buffer_create ()
566 hb_buffer_t *buffer;
568 if (!(buffer = hb_object_create<hb_buffer_t> ()))
569 return hb_buffer_get_empty ();
571 buffer->reset ();
573 return buffer;
576 hb_buffer_t *
577 hb_buffer_get_empty (void)
579 static const hb_buffer_t _hb_buffer_nil = {
580 HB_OBJECT_HEADER_STATIC,
582 const_cast<hb_unicode_funcs_t *> (&_hb_unicode_funcs_nil),
583 _HB_BUFFER_PROPS_DEFAULT,
585 HB_BUFFER_CONTENT_TYPE_INVALID,
586 true, /* in_error */
587 true, /* have_output */
588 true /* have_positions */
590 /* Zero is good enough for everything else. */
593 return const_cast<hb_buffer_t *> (&_hb_buffer_nil);
596 hb_buffer_t *
597 hb_buffer_reference (hb_buffer_t *buffer)
599 return hb_object_reference (buffer);
602 void
603 hb_buffer_destroy (hb_buffer_t *buffer)
605 if (!hb_object_destroy (buffer)) return;
607 hb_unicode_funcs_destroy (buffer->unicode);
609 free (buffer->info);
610 free (buffer->pos);
612 free (buffer);
615 hb_bool_t
616 hb_buffer_set_user_data (hb_buffer_t *buffer,
617 hb_user_data_key_t *key,
618 void * data,
619 hb_destroy_func_t destroy,
620 hb_bool_t replace)
622 return hb_object_set_user_data (buffer, key, data, destroy, replace);
625 void *
626 hb_buffer_get_user_data (hb_buffer_t *buffer,
627 hb_user_data_key_t *key)
629 return hb_object_get_user_data (buffer, key);
633 void
634 hb_buffer_set_content_type (hb_buffer_t *buffer,
635 hb_buffer_content_type_t content_type)
637 buffer->content_type = content_type;
640 hb_buffer_content_type_t
641 hb_buffer_get_content_type (hb_buffer_t *buffer)
643 return buffer->content_type;
647 void
648 hb_buffer_set_unicode_funcs (hb_buffer_t *buffer,
649 hb_unicode_funcs_t *unicode)
651 if (unlikely (hb_object_is_inert (buffer)))
652 return;
654 if (!unicode)
655 unicode = hb_unicode_funcs_get_default ();
658 hb_unicode_funcs_reference (unicode);
659 hb_unicode_funcs_destroy (buffer->unicode);
660 buffer->unicode = unicode;
663 hb_unicode_funcs_t *
664 hb_buffer_get_unicode_funcs (hb_buffer_t *buffer)
666 return buffer->unicode;
669 void
670 hb_buffer_set_direction (hb_buffer_t *buffer,
671 hb_direction_t direction)
674 if (unlikely (hb_object_is_inert (buffer)))
675 return;
677 buffer->props.direction = direction;
680 hb_direction_t
681 hb_buffer_get_direction (hb_buffer_t *buffer)
683 return buffer->props.direction;
686 void
687 hb_buffer_set_script (hb_buffer_t *buffer,
688 hb_script_t script)
690 if (unlikely (hb_object_is_inert (buffer)))
691 return;
693 buffer->props.script = script;
696 hb_script_t
697 hb_buffer_get_script (hb_buffer_t *buffer)
699 return buffer->props.script;
702 void
703 hb_buffer_set_language (hb_buffer_t *buffer,
704 hb_language_t language)
706 if (unlikely (hb_object_is_inert (buffer)))
707 return;
709 buffer->props.language = language;
712 hb_language_t
713 hb_buffer_get_language (hb_buffer_t *buffer)
715 return buffer->props.language;
719 void
720 hb_buffer_reset (hb_buffer_t *buffer)
722 buffer->reset ();
725 hb_bool_t
726 hb_buffer_pre_allocate (hb_buffer_t *buffer, unsigned int size)
728 return buffer->ensure (size);
731 hb_bool_t
732 hb_buffer_allocation_successful (hb_buffer_t *buffer)
734 return !buffer->in_error;
737 void
738 hb_buffer_add (hb_buffer_t *buffer,
739 hb_codepoint_t codepoint,
740 hb_mask_t mask,
741 unsigned int cluster)
743 buffer->add (codepoint, mask, cluster);
744 buffer->clear_context (1);
747 hb_bool_t
748 hb_buffer_set_length (hb_buffer_t *buffer,
749 unsigned int length)
751 if (unlikely (hb_object_is_inert (buffer)))
752 return length == 0;
754 if (!buffer->ensure (length))
755 return false;
757 /* Wipe the new space */
758 if (length > buffer->len) {
759 memset (buffer->info + buffer->len, 0, sizeof (buffer->info[0]) * (length - buffer->len));
760 if (buffer->have_positions)
761 memset (buffer->pos + buffer->len, 0, sizeof (buffer->pos[0]) * (length - buffer->len));
764 buffer->len = length;
766 if (!length)
767 buffer->clear_context (0);
768 buffer->clear_context (1);
770 return true;
773 unsigned int
774 hb_buffer_get_length (hb_buffer_t *buffer)
776 return buffer->len;
779 /* Return value valid as long as buffer not modified */
780 hb_glyph_info_t *
781 hb_buffer_get_glyph_infos (hb_buffer_t *buffer,
782 unsigned int *length)
784 if (length)
785 *length = buffer->len;
787 return (hb_glyph_info_t *) buffer->info;
790 /* Return value valid as long as buffer not modified */
791 hb_glyph_position_t *
792 hb_buffer_get_glyph_positions (hb_buffer_t *buffer,
793 unsigned int *length)
795 if (!buffer->have_positions)
796 buffer->clear_positions ();
798 if (length)
799 *length = buffer->len;
801 return (hb_glyph_position_t *) buffer->pos;
804 void
805 hb_buffer_reverse (hb_buffer_t *buffer)
807 buffer->reverse ();
810 void
811 hb_buffer_reverse_clusters (hb_buffer_t *buffer)
813 buffer->reverse_clusters ();
816 void
817 hb_buffer_guess_properties (hb_buffer_t *buffer)
819 buffer->guess_properties ();
822 template <typename T>
823 static inline void
824 hb_buffer_add_utf (hb_buffer_t *buffer,
825 const T *text,
826 int text_length,
827 unsigned int item_offset,
828 int item_length)
830 assert (buffer->content_type == HB_BUFFER_CONTENT_TYPE_UNICODE ||
831 (!buffer->len && buffer->content_type == HB_BUFFER_CONTENT_TYPE_INVALID));
833 if (unlikely (hb_object_is_inert (buffer)))
834 return;
836 if (text_length == -1)
837 text_length = hb_utf_strlen (text);
839 if (item_length == -1)
840 item_length = text_length - item_offset;
842 buffer->ensure (buffer->len + item_length * sizeof (T) / 4);
844 /* If buffer is empty and pre-context provided, install it.
845 * This check is written this way, to make sure people can
846 * provide pre-context in one add_utf() call, then provide
847 * text in a follow-up call. See:
849 * https://bugzilla.mozilla.org/show_bug.cgi?id=801410#c13
851 if (!buffer->len && item_offset > 0)
853 /* Add pre-context */
854 buffer->clear_context (0);
855 const T *prev = text + item_offset;
856 const T *start = text;
857 while (start < prev && buffer->context_len[0] < buffer->CONTEXT_LENGTH)
859 hb_codepoint_t u;
860 prev = hb_utf_prev (prev, start, &u);
861 buffer->context[0][buffer->context_len[0]++] = u;
865 const T *next = text + item_offset;
866 const T *end = next + item_length;
867 while (next < end)
869 hb_codepoint_t u;
870 const T *old_next = next;
871 next = hb_utf_next (next, end, &u);
872 buffer->add (u, 1, old_next - (const T *) text);
875 /* Add post-context */
876 buffer->clear_context (1);
877 end = text + text_length;
878 while (next < end && buffer->context_len[1] < buffer->CONTEXT_LENGTH)
880 hb_codepoint_t u;
881 next = hb_utf_next (next, end, &u);
882 buffer->context[1][buffer->context_len[1]++] = u;
885 buffer->content_type = HB_BUFFER_CONTENT_TYPE_UNICODE;
888 void
889 hb_buffer_add_utf8 (hb_buffer_t *buffer,
890 const char *text,
891 int text_length,
892 unsigned int item_offset,
893 int item_length)
895 hb_buffer_add_utf (buffer, (const uint8_t *) text, text_length, item_offset, item_length);
898 void
899 hb_buffer_add_utf16 (hb_buffer_t *buffer,
900 const uint16_t *text,
901 int text_length,
902 unsigned int item_offset,
903 int item_length)
905 hb_buffer_add_utf (buffer, text, text_length, item_offset, item_length);
908 void
909 hb_buffer_add_utf32 (hb_buffer_t *buffer,
910 const uint32_t *text,
911 int text_length,
912 unsigned int item_offset,
913 int item_length)
915 hb_buffer_add_utf (buffer, text, text_length, item_offset, item_length);
919 static int
920 compare_info_codepoint (const hb_glyph_info_t *pa,
921 const hb_glyph_info_t *pb)
923 return (int) pb->codepoint - (int) pa->codepoint;
926 static inline void
927 normalize_glyphs_cluster (hb_buffer_t *buffer,
928 unsigned int start,
929 unsigned int end,
930 bool backward)
932 hb_glyph_position_t *pos = buffer->pos;
934 /* Total cluster advance */
935 hb_position_t total_x_advance = 0, total_y_advance = 0;
936 for (unsigned int i = start; i < end; i++)
938 total_x_advance += pos[i].x_advance;
939 total_y_advance += pos[i].y_advance;
942 hb_position_t x_advance = 0, y_advance = 0;
943 for (unsigned int i = start; i < end; i++)
945 pos[i].x_offset += x_advance;
946 pos[i].y_offset += y_advance;
948 x_advance += pos[i].x_advance;
949 y_advance += pos[i].y_advance;
951 pos[i].x_advance = 0;
952 pos[i].y_advance = 0;
955 if (backward)
957 /* Transfer all cluster advance to the last glyph. */
958 pos[end - 1].x_advance = total_x_advance;
959 pos[end - 1].y_advance = total_y_advance;
961 hb_bubble_sort (buffer->info + start, end - start - 1, compare_info_codepoint, buffer->pos + start);
962 } else {
963 /* Transfer all cluster advance to the first glyph. */
964 pos[start].x_advance += total_x_advance;
965 pos[start].y_advance += total_y_advance;
966 for (unsigned int i = start + 1; i < end; i++) {
967 pos[i].x_offset -= total_x_advance;
968 pos[i].y_offset -= total_y_advance;
970 hb_bubble_sort (buffer->info + start + 1, end - start - 1, compare_info_codepoint, buffer->pos + start + 1);
974 void
975 hb_buffer_normalize_glyphs (hb_buffer_t *buffer)
977 assert (buffer->have_positions);
978 assert (buffer->content_type == HB_BUFFER_CONTENT_TYPE_GLYPHS);
980 bool backward = HB_DIRECTION_IS_BACKWARD (buffer->props.direction);
982 unsigned int count = buffer->len;
983 if (unlikely (!count)) return;
984 hb_glyph_info_t *info = buffer->info;
986 unsigned int start = 0;
987 unsigned int end;
988 for (end = start + 1; end < count; end++)
989 if (info[start].cluster != info[end].cluster) {
990 normalize_glyphs_cluster (buffer, start, end, backward);
991 start = end;
993 normalize_glyphs_cluster (buffer, start, end, backward);