Clean up extension confirmation prompts and make them consistent between Views and...
[chromium-blink-merge.git] / third_party / harfbuzz-ng / src / hb-buffer.cc
blobb9fe263ce53c33bc975a5fa2f78723ec34500ef5
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
39 hb_bool_t
40 hb_segment_properties_equal (const hb_segment_properties_t *a,
41 const hb_segment_properties_t *b)
43 return a->direction == b->direction &&
44 a->script == b->script &&
45 a->language == b->language &&
46 a->reserved1 == b->reserved1 &&
47 a->reserved2 == b->reserved2;
51 unsigned int
52 hb_segment_properties_hash (const hb_segment_properties_t *p)
54 return (unsigned int) p->direction ^
55 (unsigned int) p->script ^
56 (intptr_t) (p->language);
61 /* Here is how the buffer works internally:
63 * There are two info pointers: info and out_info. They always have
64 * the same allocated size, but different lengths.
66 * As an optimization, both info and out_info may point to the
67 * same piece of memory, which is owned by info. This remains the
68 * case as long as out_len doesn't exceed i at any time.
69 * In that case, swap_buffers() is no-op and the glyph operations operate
70 * mostly in-place.
72 * As soon as out_info gets longer than info, out_info is moved over
73 * to an alternate buffer (which we reuse the pos buffer for!), and its
74 * current contents (out_len entries) are copied to the new place.
75 * This should all remain transparent to the user. swap_buffers() then
76 * switches info and out_info.
81 /* Internal API */
83 bool
84 hb_buffer_t::enlarge (unsigned int size)
86 if (unlikely (in_error))
87 return false;
89 unsigned int new_allocated = allocated;
90 hb_glyph_position_t *new_pos = NULL;
91 hb_glyph_info_t *new_info = NULL;
92 bool separate_out = out_info != info;
94 if (unlikely (_hb_unsigned_int_mul_overflows (size, sizeof (info[0]))))
95 goto done;
97 while (size >= new_allocated)
98 new_allocated += (new_allocated >> 1) + 32;
100 ASSERT_STATIC (sizeof (info[0]) == sizeof (pos[0]));
101 if (unlikely (_hb_unsigned_int_mul_overflows (new_allocated, sizeof (info[0]))))
102 goto done;
104 new_pos = (hb_glyph_position_t *) realloc (pos, new_allocated * sizeof (pos[0]));
105 new_info = (hb_glyph_info_t *) realloc (info, new_allocated * sizeof (info[0]));
107 done:
108 if (unlikely (!new_pos || !new_info))
109 in_error = true;
111 if (likely (new_pos))
112 pos = new_pos;
114 if (likely (new_info))
115 info = new_info;
117 out_info = separate_out ? (hb_glyph_info_t *) pos : info;
118 if (likely (!in_error))
119 allocated = new_allocated;
121 return likely (!in_error);
124 bool
125 hb_buffer_t::make_room_for (unsigned int num_in,
126 unsigned int num_out)
128 if (unlikely (!ensure (out_len + num_out))) return false;
130 if (out_info == info &&
131 out_len + num_out > idx + num_in)
133 assert (have_output);
135 out_info = (hb_glyph_info_t *) pos;
136 memcpy (out_info, info, out_len * sizeof (out_info[0]));
139 return true;
142 bool
143 hb_buffer_t::shift_forward (unsigned int count)
145 assert (have_output);
146 if (unlikely (!ensure (len + count))) return false;
148 memmove (info + idx + count, info + idx, (len - idx) * sizeof (info[0]));
149 len += count;
150 idx += count;
152 return true;
155 hb_buffer_t::scratch_buffer_t *
156 hb_buffer_t::get_scratch_buffer (unsigned int *size)
158 have_output = false;
159 have_positions = false;
161 out_len = 0;
162 out_info = info;
164 assert ((uintptr_t) pos % sizeof (scratch_buffer_t) == 0);
165 *size = allocated * sizeof (pos[0]) / sizeof (scratch_buffer_t);
166 return (scratch_buffer_t *) (void *) pos;
171 /* HarfBuzz-Internal API */
173 void
174 hb_buffer_t::reset (void)
176 if (unlikely (hb_object_is_inert (this)))
177 return;
179 hb_unicode_funcs_destroy (unicode);
180 unicode = hb_unicode_funcs_get_default ();
181 flags = HB_BUFFER_FLAG_DEFAULT;
182 replacement = HB_BUFFER_REPLACEMENT_CODEPOINT_DEFAULT;
184 clear ();
187 void
188 hb_buffer_t::clear (void)
190 if (unlikely (hb_object_is_inert (this)))
191 return;
193 hb_segment_properties_t default_props = HB_SEGMENT_PROPERTIES_DEFAULT;
194 props = default_props;
196 content_type = HB_BUFFER_CONTENT_TYPE_INVALID;
197 in_error = false;
198 have_output = false;
199 have_positions = false;
201 idx = 0;
202 len = 0;
203 out_len = 0;
204 out_info = info;
206 serial = 0;
207 memset (allocated_var_bytes, 0, sizeof allocated_var_bytes);
208 memset (allocated_var_owner, 0, sizeof allocated_var_owner);
210 memset (context, 0, sizeof context);
211 memset (context_len, 0, sizeof context_len);
214 void
215 hb_buffer_t::add (hb_codepoint_t codepoint,
216 unsigned int cluster)
218 hb_glyph_info_t *glyph;
220 if (unlikely (!ensure (len + 1))) return;
222 glyph = &info[len];
224 memset (glyph, 0, sizeof (*glyph));
225 glyph->codepoint = codepoint;
226 glyph->mask = 1;
227 glyph->cluster = cluster;
229 len++;
232 void
233 hb_buffer_t::add_info (const hb_glyph_info_t &glyph_info)
235 if (unlikely (!ensure (len + 1))) return;
237 info[len] = glyph_info;
239 len++;
243 void
244 hb_buffer_t::remove_output (void)
246 if (unlikely (hb_object_is_inert (this)))
247 return;
249 have_output = false;
250 have_positions = false;
252 out_len = 0;
253 out_info = info;
256 void
257 hb_buffer_t::clear_output (void)
259 if (unlikely (hb_object_is_inert (this)))
260 return;
262 have_output = true;
263 have_positions = false;
265 out_len = 0;
266 out_info = info;
269 void
270 hb_buffer_t::clear_positions (void)
272 if (unlikely (hb_object_is_inert (this)))
273 return;
275 have_output = false;
276 have_positions = true;
278 out_len = 0;
279 out_info = info;
281 memset (pos, 0, sizeof (pos[0]) * len);
284 void
285 hb_buffer_t::swap_buffers (void)
287 if (unlikely (in_error)) return;
289 assert (have_output);
290 have_output = false;
292 if (out_info != info)
294 hb_glyph_info_t *tmp_string;
295 tmp_string = info;
296 info = out_info;
297 out_info = tmp_string;
298 pos = (hb_glyph_position_t *) out_info;
301 unsigned int tmp;
302 tmp = len;
303 len = out_len;
304 out_len = tmp;
306 idx = 0;
310 void
311 hb_buffer_t::replace_glyphs (unsigned int num_in,
312 unsigned int num_out,
313 const uint32_t *glyph_data)
315 if (unlikely (!make_room_for (num_in, num_out))) return;
317 merge_clusters (idx, idx + num_in);
319 hb_glyph_info_t orig_info = info[idx];
320 hb_glyph_info_t *pinfo = &out_info[out_len];
321 for (unsigned int i = 0; i < num_out; i++)
323 *pinfo = orig_info;
324 pinfo->codepoint = glyph_data[i];
325 pinfo++;
328 idx += num_in;
329 out_len += num_out;
332 void
333 hb_buffer_t::output_glyph (hb_codepoint_t glyph_index)
335 if (unlikely (!make_room_for (0, 1))) return;
337 out_info[out_len] = info[idx];
338 out_info[out_len].codepoint = glyph_index;
340 out_len++;
343 void
344 hb_buffer_t::output_info (const hb_glyph_info_t &glyph_info)
346 if (unlikely (!make_room_for (0, 1))) return;
348 out_info[out_len] = glyph_info;
350 out_len++;
353 void
354 hb_buffer_t::copy_glyph (void)
356 if (unlikely (!make_room_for (0, 1))) return;
358 out_info[out_len] = info[idx];
360 out_len++;
363 bool
364 hb_buffer_t::move_to (unsigned int i)
366 if (!have_output)
368 assert (i <= len);
369 idx = i;
370 return true;
373 assert (i <= out_len + (len - idx));
375 if (out_len < i)
377 unsigned int count = i - out_len;
378 if (unlikely (!make_room_for (count, count))) return false;
380 memmove (out_info + out_len, info + idx, count * sizeof (out_info[0]));
381 idx += count;
382 out_len += count;
384 else if (out_len > i)
386 /* Tricky part: rewinding... */
387 unsigned int count = out_len - i;
389 if (unlikely (idx < count && !shift_forward (count + 32))) return false;
391 assert (idx >= count);
393 idx -= count;
394 out_len -= count;
395 memmove (info + idx, out_info + out_len, count * sizeof (out_info[0]));
398 return true;
401 void
402 hb_buffer_t::replace_glyph (hb_codepoint_t glyph_index)
404 if (unlikely (out_info != info || out_len != idx)) {
405 if (unlikely (!make_room_for (1, 1))) return;
406 out_info[out_len] = info[idx];
408 out_info[out_len].codepoint = glyph_index;
410 idx++;
411 out_len++;
415 void
416 hb_buffer_t::set_masks (hb_mask_t value,
417 hb_mask_t mask,
418 unsigned int cluster_start,
419 unsigned int cluster_end)
421 hb_mask_t not_mask = ~mask;
422 value &= mask;
424 if (!mask)
425 return;
427 if (cluster_start == 0 && cluster_end == (unsigned int)-1) {
428 unsigned int count = len;
429 for (unsigned int i = 0; i < count; i++)
430 info[i].mask = (info[i].mask & not_mask) | value;
431 return;
434 unsigned int count = len;
435 for (unsigned int i = 0; i < count; i++)
436 if (cluster_start <= info[i].cluster && info[i].cluster < cluster_end)
437 info[i].mask = (info[i].mask & not_mask) | value;
440 void
441 hb_buffer_t::reverse_range (unsigned int start,
442 unsigned int end)
444 unsigned int i, j;
446 if (end - start < 2)
447 return;
449 for (i = start, j = end - 1; i < j; i++, j--) {
450 hb_glyph_info_t t;
452 t = info[i];
453 info[i] = info[j];
454 info[j] = t;
457 if (have_positions) {
458 for (i = start, j = end - 1; i < j; i++, j--) {
459 hb_glyph_position_t t;
461 t = pos[i];
462 pos[i] = pos[j];
463 pos[j] = t;
468 void
469 hb_buffer_t::reverse (void)
471 if (unlikely (!len))
472 return;
474 reverse_range (0, len);
477 void
478 hb_buffer_t::reverse_clusters (void)
480 unsigned int i, start, count, last_cluster;
482 if (unlikely (!len))
483 return;
485 reverse ();
487 count = len;
488 start = 0;
489 last_cluster = info[0].cluster;
490 for (i = 1; i < count; i++) {
491 if (last_cluster != info[i].cluster) {
492 reverse_range (start, i);
493 start = i;
494 last_cluster = info[i].cluster;
497 reverse_range (start, i);
500 void
501 hb_buffer_t::merge_clusters (unsigned int start,
502 unsigned int end)
504 #ifdef HB_NO_MERGE_CLUSTERS
505 return;
506 #endif
508 if (unlikely (end - start < 2))
509 return;
511 unsigned int cluster = info[start].cluster;
513 for (unsigned int i = start + 1; i < end; i++)
514 cluster = MIN (cluster, info[i].cluster);
516 /* Extend end */
517 while (end < len && info[end - 1].cluster == info[end].cluster)
518 end++;
520 /* Extend start */
521 while (idx < start && info[start - 1].cluster == info[start].cluster)
522 start--;
524 /* If we hit the start of buffer, continue in out-buffer. */
525 if (idx == start)
526 for (unsigned i = out_len; i && out_info[i - 1].cluster == info[start].cluster; i--)
527 out_info[i - 1].cluster = cluster;
529 for (unsigned int i = start; i < end; i++)
530 info[i].cluster = cluster;
532 void
533 hb_buffer_t::merge_out_clusters (unsigned int start,
534 unsigned int end)
536 #ifdef HB_NO_MERGE_CLUSTERS
537 return;
538 #endif
540 if (unlikely (end - start < 2))
541 return;
543 unsigned int cluster = out_info[start].cluster;
545 for (unsigned int i = start + 1; i < end; i++)
546 cluster = MIN (cluster, out_info[i].cluster);
548 /* Extend start */
549 while (start && out_info[start - 1].cluster == out_info[start].cluster)
550 start--;
552 /* Extend end */
553 while (end < out_len && out_info[end - 1].cluster == out_info[end].cluster)
554 end++;
556 /* If we hit the end of out-buffer, continue in buffer. */
557 if (end == out_len)
558 for (unsigned i = idx; i < len && info[i].cluster == out_info[end - 1].cluster; i++)
559 info[i].cluster = cluster;
561 for (unsigned int i = start; i < end; i++)
562 out_info[i].cluster = cluster;
565 void
566 hb_buffer_t::guess_segment_properties (void)
568 assert (content_type == HB_BUFFER_CONTENT_TYPE_UNICODE ||
569 (!len && content_type == HB_BUFFER_CONTENT_TYPE_INVALID));
571 /* If script is set to INVALID, guess from buffer contents */
572 if (props.script == HB_SCRIPT_INVALID) {
573 for (unsigned int i = 0; i < len; i++) {
574 hb_script_t script = unicode->script (info[i].codepoint);
575 if (likely (script != HB_SCRIPT_COMMON &&
576 script != HB_SCRIPT_INHERITED &&
577 script != HB_SCRIPT_UNKNOWN)) {
578 props.script = script;
579 break;
584 /* If direction is set to INVALID, guess from script */
585 if (props.direction == HB_DIRECTION_INVALID) {
586 props.direction = hb_script_get_horizontal_direction (props.script);
589 /* If language is not set, use default language from locale */
590 if (props.language == HB_LANGUAGE_INVALID) {
591 /* TODO get_default_for_script? using $LANGUAGE */
592 props.language = hb_language_get_default ();
597 static inline void
598 dump_var_allocation (const hb_buffer_t *buffer)
600 char buf[80];
601 for (unsigned int i = 0; i < 8; i++)
602 buf[i] = '0' + buffer->allocated_var_bytes[7 - i];
603 buf[8] = '\0';
604 DEBUG_MSG (BUFFER, buffer,
605 "Current var allocation: %s",
606 buf);
609 void hb_buffer_t::allocate_var (unsigned int byte_i, unsigned int count, const char *owner)
611 assert (byte_i < 8 && byte_i + count <= 8);
613 if (DEBUG_ENABLED (BUFFER))
614 dump_var_allocation (this);
615 DEBUG_MSG (BUFFER, this,
616 "Allocating var bytes %d..%d for %s",
617 byte_i, byte_i + count - 1, owner);
619 for (unsigned int i = byte_i; i < byte_i + count; i++) {
620 assert (!allocated_var_bytes[i]);
621 allocated_var_bytes[i]++;
622 allocated_var_owner[i] = owner;
626 void hb_buffer_t::deallocate_var (unsigned int byte_i, unsigned int count, const char *owner)
628 if (DEBUG_ENABLED (BUFFER))
629 dump_var_allocation (this);
631 DEBUG_MSG (BUFFER, this,
632 "Deallocating var bytes %d..%d for %s",
633 byte_i, byte_i + count - 1, owner);
635 assert (byte_i < 8 && byte_i + count <= 8);
636 for (unsigned int i = byte_i; i < byte_i + count; i++) {
637 assert (allocated_var_bytes[i]);
638 assert (0 == strcmp (allocated_var_owner[i], owner));
639 allocated_var_bytes[i]--;
643 void hb_buffer_t::assert_var (unsigned int byte_i, unsigned int count, const char *owner)
645 if (DEBUG_ENABLED (BUFFER))
646 dump_var_allocation (this);
648 DEBUG_MSG (BUFFER, this,
649 "Asserting var bytes %d..%d for %s",
650 byte_i, byte_i + count - 1, owner);
652 assert (byte_i < 8 && byte_i + count <= 8);
653 for (unsigned int i = byte_i; i < byte_i + count; i++) {
654 assert (allocated_var_bytes[i]);
655 assert (0 == strcmp (allocated_var_owner[i], owner));
659 void hb_buffer_t::deallocate_var_all (void)
661 memset (allocated_var_bytes, 0, sizeof (allocated_var_bytes));
662 memset (allocated_var_owner, 0, sizeof (allocated_var_owner));
665 /* Public API */
668 * hb_buffer_create: (Xconstructor)
672 * Return value: (transfer full)
674 * Since: 1.0
676 hb_buffer_t *
677 hb_buffer_create (void)
679 hb_buffer_t *buffer;
681 if (!(buffer = hb_object_create<hb_buffer_t> ()))
682 return hb_buffer_get_empty ();
684 buffer->reset ();
686 return buffer;
690 * hb_buffer_get_empty:
694 * Return value: (transfer full):
696 * Since: 1.0
698 hb_buffer_t *
699 hb_buffer_get_empty (void)
701 static const hb_buffer_t _hb_buffer_nil = {
702 HB_OBJECT_HEADER_STATIC,
704 const_cast<hb_unicode_funcs_t *> (&_hb_unicode_funcs_nil),
705 HB_BUFFER_FLAG_DEFAULT,
706 HB_BUFFER_REPLACEMENT_CODEPOINT_DEFAULT,
708 HB_BUFFER_CONTENT_TYPE_INVALID,
709 HB_SEGMENT_PROPERTIES_DEFAULT,
710 true, /* in_error */
711 true, /* have_output */
712 true /* have_positions */
714 /* Zero is good enough for everything else. */
717 return const_cast<hb_buffer_t *> (&_hb_buffer_nil);
721 * hb_buffer_reference: (skip)
722 * @buffer: a buffer.
726 * Return value: (transfer full):
728 * Since: 1.0
730 hb_buffer_t *
731 hb_buffer_reference (hb_buffer_t *buffer)
733 return hb_object_reference (buffer);
737 * hb_buffer_destroy: (skip)
738 * @buffer: a buffer.
742 * Since: 1.0
744 void
745 hb_buffer_destroy (hb_buffer_t *buffer)
747 if (!hb_object_destroy (buffer)) return;
749 hb_unicode_funcs_destroy (buffer->unicode);
751 free (buffer->info);
752 free (buffer->pos);
754 free (buffer);
758 * hb_buffer_set_user_data: (skip)
759 * @buffer: a buffer.
760 * @key:
761 * @data:
762 * @destroy:
763 * @replace:
767 * Return value:
769 * Since: 1.0
771 hb_bool_t
772 hb_buffer_set_user_data (hb_buffer_t *buffer,
773 hb_user_data_key_t *key,
774 void * data,
775 hb_destroy_func_t destroy,
776 hb_bool_t replace)
778 return hb_object_set_user_data (buffer, key, data, destroy, replace);
782 * hb_buffer_get_user_data: (skip)
783 * @buffer: a buffer.
784 * @key:
788 * Return value:
790 * Since: 1.0
792 void *
793 hb_buffer_get_user_data (hb_buffer_t *buffer,
794 hb_user_data_key_t *key)
796 return hb_object_get_user_data (buffer, key);
801 * hb_buffer_set_content_type:
802 * @buffer: a buffer.
803 * @content_type:
807 * Since: 1.0
809 void
810 hb_buffer_set_content_type (hb_buffer_t *buffer,
811 hb_buffer_content_type_t content_type)
813 buffer->content_type = content_type;
817 * hb_buffer_get_content_type:
818 * @buffer: a buffer.
822 * Return value:
824 * Since: 1.0
826 hb_buffer_content_type_t
827 hb_buffer_get_content_type (hb_buffer_t *buffer)
829 return buffer->content_type;
834 * hb_buffer_set_unicode_funcs:
835 * @buffer: a buffer.
836 * @unicode_funcs:
840 * Since: 1.0
842 void
843 hb_buffer_set_unicode_funcs (hb_buffer_t *buffer,
844 hb_unicode_funcs_t *unicode_funcs)
846 if (unlikely (hb_object_is_inert (buffer)))
847 return;
849 if (!unicode_funcs)
850 unicode_funcs = hb_unicode_funcs_get_default ();
853 hb_unicode_funcs_reference (unicode_funcs);
854 hb_unicode_funcs_destroy (buffer->unicode);
855 buffer->unicode = unicode_funcs;
859 * hb_buffer_get_unicode_funcs:
860 * @buffer: a buffer.
864 * Return value:
866 * Since: 1.0
868 hb_unicode_funcs_t *
869 hb_buffer_get_unicode_funcs (hb_buffer_t *buffer)
871 return buffer->unicode;
875 * hb_buffer_set_direction:
876 * @buffer: a buffer.
877 * @direction:
881 * Since: 1.0
883 void
884 hb_buffer_set_direction (hb_buffer_t *buffer,
885 hb_direction_t direction)
888 if (unlikely (hb_object_is_inert (buffer)))
889 return;
891 buffer->props.direction = direction;
895 * hb_buffer_get_direction:
896 * @buffer: a buffer.
900 * Return value:
902 * Since: 1.0
904 hb_direction_t
905 hb_buffer_get_direction (hb_buffer_t *buffer)
907 return buffer->props.direction;
911 * hb_buffer_set_script:
912 * @buffer: a buffer.
913 * @script:
917 * Since: 1.0
919 void
920 hb_buffer_set_script (hb_buffer_t *buffer,
921 hb_script_t script)
923 if (unlikely (hb_object_is_inert (buffer)))
924 return;
926 buffer->props.script = script;
930 * hb_buffer_get_script:
931 * @buffer: a buffer.
935 * Return value:
937 * Since: 1.0
939 hb_script_t
940 hb_buffer_get_script (hb_buffer_t *buffer)
942 return buffer->props.script;
946 * hb_buffer_set_language:
947 * @buffer: a buffer.
948 * @language:
952 * Since: 1.0
954 void
955 hb_buffer_set_language (hb_buffer_t *buffer,
956 hb_language_t language)
958 if (unlikely (hb_object_is_inert (buffer)))
959 return;
961 buffer->props.language = language;
965 * hb_buffer_get_language:
966 * @buffer: a buffer.
970 * Return value:
972 * Since: 1.0
974 hb_language_t
975 hb_buffer_get_language (hb_buffer_t *buffer)
977 return buffer->props.language;
981 * hb_buffer_set_segment_properties:
982 * @buffer: a buffer.
983 * @props:
987 * Since: 1.0
989 void
990 hb_buffer_set_segment_properties (hb_buffer_t *buffer,
991 const hb_segment_properties_t *props)
993 if (unlikely (hb_object_is_inert (buffer)))
994 return;
996 buffer->props = *props;
1000 * hb_buffer_get_segment_properties:
1001 * @buffer: a buffer.
1002 * @props:
1006 * Since: 1.0
1008 void
1009 hb_buffer_get_segment_properties (hb_buffer_t *buffer,
1010 hb_segment_properties_t *props)
1012 *props = buffer->props;
1017 * hb_buffer_set_flags:
1018 * @buffer: a buffer.
1019 * @flags:
1023 * Since: 1.0
1025 void
1026 hb_buffer_set_flags (hb_buffer_t *buffer,
1027 hb_buffer_flags_t flags)
1029 if (unlikely (hb_object_is_inert (buffer)))
1030 return;
1032 buffer->flags = flags;
1036 * hb_buffer_get_flags:
1037 * @buffer: a buffer.
1041 * Return value:
1043 * Since: 1.0
1045 hb_buffer_flags_t
1046 hb_buffer_get_flags (hb_buffer_t *buffer)
1048 return buffer->flags;
1053 * hb_buffer_set_replacement_codepoint:
1054 * @buffer: a buffer.
1055 * @replacement:
1059 * Since: 1.0
1061 void
1062 hb_buffer_set_replacement_codepoint (hb_buffer_t *buffer,
1063 hb_codepoint_t replacement)
1065 if (unlikely (hb_object_is_inert (buffer)))
1066 return;
1068 buffer->replacement = replacement;
1072 * hb_buffer_get_replacement_codepoint:
1073 * @buffer: a buffer.
1077 * Return value:
1079 * Since: 1.0
1081 hb_codepoint_t
1082 hb_buffer_get_replacement_codepoint (hb_buffer_t *buffer)
1084 return buffer->replacement;
1089 * hb_buffer_reset:
1090 * @buffer: a buffer.
1094 * Since: 1.0
1096 void
1097 hb_buffer_reset (hb_buffer_t *buffer)
1099 buffer->reset ();
1103 * hb_buffer_clear_contents:
1104 * @buffer: a buffer.
1108 * Since: 1.0
1110 void
1111 hb_buffer_clear_contents (hb_buffer_t *buffer)
1113 buffer->clear ();
1117 * hb_buffer_pre_allocate:
1118 * @buffer: a buffer.
1119 * @size:
1123 * Return value:
1125 * Since: 1.0
1127 hb_bool_t
1128 hb_buffer_pre_allocate (hb_buffer_t *buffer, unsigned int size)
1130 return buffer->ensure (size);
1134 * hb_buffer_allocation_successful:
1135 * @buffer: a buffer.
1139 * Return value:
1141 * Since: 1.0
1143 hb_bool_t
1144 hb_buffer_allocation_successful (hb_buffer_t *buffer)
1146 return !buffer->in_error;
1150 * hb_buffer_add:
1151 * @buffer: a buffer.
1152 * @codepoint:
1153 * @cluster:
1157 * Since: 1.0
1159 void
1160 hb_buffer_add (hb_buffer_t *buffer,
1161 hb_codepoint_t codepoint,
1162 unsigned int cluster)
1164 buffer->add (codepoint, cluster);
1165 buffer->clear_context (1);
1169 * hb_buffer_set_length:
1170 * @buffer: a buffer.
1171 * @length:
1175 * Return value:
1177 * Since: 1.0
1179 hb_bool_t
1180 hb_buffer_set_length (hb_buffer_t *buffer,
1181 unsigned int length)
1183 if (unlikely (hb_object_is_inert (buffer)))
1184 return length == 0;
1186 if (!buffer->ensure (length))
1187 return false;
1189 /* Wipe the new space */
1190 if (length > buffer->len) {
1191 memset (buffer->info + buffer->len, 0, sizeof (buffer->info[0]) * (length - buffer->len));
1192 if (buffer->have_positions)
1193 memset (buffer->pos + buffer->len, 0, sizeof (buffer->pos[0]) * (length - buffer->len));
1196 buffer->len = length;
1198 if (!length)
1200 buffer->content_type = HB_BUFFER_CONTENT_TYPE_INVALID;
1201 buffer->clear_context (0);
1203 buffer->clear_context (1);
1205 return true;
1209 * hb_buffer_get_length:
1210 * @buffer: a buffer.
1212 * Returns the number of items in the buffer.
1214 * Return value: buffer length.
1216 * Since: 1.0
1218 unsigned int
1219 hb_buffer_get_length (hb_buffer_t *buffer)
1221 return buffer->len;
1225 * hb_buffer_get_glyph_infos:
1226 * @buffer: a buffer.
1227 * @length: (out): output array length.
1229 * Returns buffer glyph information array. Returned pointer
1230 * is valid as long as buffer contents are not modified.
1232 * Return value: (transfer none) (array length=length): buffer glyph information array.
1234 * Since: 1.0
1236 hb_glyph_info_t *
1237 hb_buffer_get_glyph_infos (hb_buffer_t *buffer,
1238 unsigned int *length)
1240 if (length)
1241 *length = buffer->len;
1243 return (hb_glyph_info_t *) buffer->info;
1247 * hb_buffer_get_glyph_positions:
1248 * @buffer: a buffer.
1249 * @length: (out): output length.
1251 * Returns buffer glyph position array. Returned pointer
1252 * is valid as long as buffer contents are not modified.
1254 * Return value: (transfer none) (array length=length): buffer glyph position array.
1256 * Since: 1.0
1258 hb_glyph_position_t *
1259 hb_buffer_get_glyph_positions (hb_buffer_t *buffer,
1260 unsigned int *length)
1262 if (!buffer->have_positions)
1263 buffer->clear_positions ();
1265 if (length)
1266 *length = buffer->len;
1268 return (hb_glyph_position_t *) buffer->pos;
1272 * hb_buffer_reverse:
1273 * @buffer: a buffer.
1275 * Reverses buffer contents.
1277 * Since: 1.0
1279 void
1280 hb_buffer_reverse (hb_buffer_t *buffer)
1282 buffer->reverse ();
1286 * hb_buffer_reverse_clusters:
1287 * @buffer: a buffer.
1289 * Reverses buffer clusters. That is, the buffer contents are
1290 * reversed, then each cluster (consecutive items having the
1291 * same cluster number) are reversed again.
1293 * Since: 1.0
1295 void
1296 hb_buffer_reverse_clusters (hb_buffer_t *buffer)
1298 buffer->reverse_clusters ();
1302 * hb_buffer_guess_segment_properties:
1303 * @buffer: a buffer.
1305 * Sets unset buffer segment properties based on buffer Unicode
1306 * contents. If buffer is not empty, it must have content type
1307 * %HB_BUFFER_CONTENT_TYPE_UNICODE.
1309 * If buffer script is not set (ie. is %HB_SCRIPT_INVALID), it
1310 * will be set to the Unicode script of the first character in
1311 * the buffer that has a script other than %HB_SCRIPT_COMMON,
1312 * %HB_SCRIPT_INHERITED, and %HB_SCRIPT_UNKNOWN.
1314 * Next, if buffer direction is not set (ie. is %HB_DIRECTION_INVALID),
1315 * it will be set to the natural horizontal direction of the
1316 * buffer script as returned by hb_script_get_horizontal_direction().
1318 * Finally, if buffer language is not set (ie. is %HB_LANGUAGE_INVALID),
1319 * it will be set to the process's default language as returned by
1320 * hb_language_get_default(). This may change in the future by
1321 * taking buffer script into consideration when choosing a language.
1323 * Since: 1.0
1325 void
1326 hb_buffer_guess_segment_properties (hb_buffer_t *buffer)
1328 buffer->guess_segment_properties ();
1331 template <typename utf_t>
1332 static inline void
1333 hb_buffer_add_utf (hb_buffer_t *buffer,
1334 const typename utf_t::codepoint_t *text,
1335 int text_length,
1336 unsigned int item_offset,
1337 int item_length)
1339 typedef typename utf_t::codepoint_t T;
1340 const hb_codepoint_t replacement = buffer->replacement;
1342 assert (buffer->content_type == HB_BUFFER_CONTENT_TYPE_UNICODE ||
1343 (!buffer->len && buffer->content_type == HB_BUFFER_CONTENT_TYPE_INVALID));
1345 if (unlikely (hb_object_is_inert (buffer)))
1346 return;
1348 if (text_length == -1)
1349 text_length = utf_t::strlen (text);
1351 if (item_length == -1)
1352 item_length = text_length - item_offset;
1354 buffer->ensure (buffer->len + item_length * sizeof (T) / 4);
1356 /* If buffer is empty and pre-context provided, install it.
1357 * This check is written this way, to make sure people can
1358 * provide pre-context in one add_utf() call, then provide
1359 * text in a follow-up call. See:
1361 * https://bugzilla.mozilla.org/show_bug.cgi?id=801410#c13
1363 if (!buffer->len && item_offset > 0)
1365 /* Add pre-context */
1366 buffer->clear_context (0);
1367 const T *prev = text + item_offset;
1368 const T *start = text;
1369 while (start < prev && buffer->context_len[0] < buffer->CONTEXT_LENGTH)
1371 hb_codepoint_t u;
1372 prev = utf_t::prev (prev, start, &u, replacement);
1373 buffer->context[0][buffer->context_len[0]++] = u;
1377 const T *next = text + item_offset;
1378 const T *end = next + item_length;
1379 while (next < end)
1381 hb_codepoint_t u;
1382 const T *old_next = next;
1383 next = utf_t::next (next, end, &u, replacement);
1384 buffer->add (u, old_next - (const T *) text);
1387 /* Add post-context */
1388 buffer->clear_context (1);
1389 end = text + text_length;
1390 while (next < end && buffer->context_len[1] < buffer->CONTEXT_LENGTH)
1392 hb_codepoint_t u;
1393 next = utf_t::next (next, end, &u, replacement);
1394 buffer->context[1][buffer->context_len[1]++] = u;
1397 buffer->content_type = HB_BUFFER_CONTENT_TYPE_UNICODE;
1401 * hb_buffer_add_utf8:
1402 * @buffer: a buffer.
1403 * @text: (array length=text_length) (element-type uint8_t):
1404 * @text_length:
1405 * @item_offset:
1406 * @item_length:
1410 * Since: 1.0
1412 void
1413 hb_buffer_add_utf8 (hb_buffer_t *buffer,
1414 const char *text,
1415 int text_length,
1416 unsigned int item_offset,
1417 int item_length)
1419 hb_buffer_add_utf<hb_utf8_t> (buffer, (const uint8_t *) text, text_length, item_offset, item_length);
1423 * hb_buffer_add_utf16:
1424 * @buffer: a buffer.
1425 * @text: (array length=text_length):
1426 * @text_length:
1427 * @item_offset:
1428 * @item_length:
1432 * Since: 1.0
1434 void
1435 hb_buffer_add_utf16 (hb_buffer_t *buffer,
1436 const uint16_t *text,
1437 int text_length,
1438 unsigned int item_offset,
1439 int item_length)
1441 hb_buffer_add_utf<hb_utf16_t> (buffer, text, text_length, item_offset, item_length);
1445 * hb_buffer_add_utf32:
1446 * @buffer: a buffer.
1447 * @text: (array length=text_length):
1448 * @text_length:
1449 * @item_offset:
1450 * @item_length:
1454 * Since: 1.0
1456 void
1457 hb_buffer_add_utf32 (hb_buffer_t *buffer,
1458 const uint32_t *text,
1459 int text_length,
1460 unsigned int item_offset,
1461 int item_length)
1463 hb_buffer_add_utf<hb_utf32_t<> > (buffer, text, text_length, item_offset, item_length);
1467 * hb_buffer_add_latin1:
1468 * @buffer: a buffer.
1469 * @text: (array length=text_length) (element-type uint8_t):
1470 * @text_length:
1471 * @item_offset:
1472 * @item_length:
1476 * Since: 1.0
1478 void
1479 hb_buffer_add_latin1 (hb_buffer_t *buffer,
1480 const uint8_t *text,
1481 int text_length,
1482 unsigned int item_offset,
1483 int item_length)
1485 hb_buffer_add_utf<hb_latin1_t> (buffer, text, text_length, item_offset, item_length);
1489 * hb_buffer_add_codepoints:
1490 * @buffer: a buffer.
1491 * @text: (array length=text_length):
1492 * @text_length:
1493 * @item_offset:
1494 * @item_length:
1498 * Since: 1.0
1500 void
1501 hb_buffer_add_codepoints (hb_buffer_t *buffer,
1502 const hb_codepoint_t *text,
1503 int text_length,
1504 unsigned int item_offset,
1505 int item_length)
1507 hb_buffer_add_utf<hb_utf32_t<false> > (buffer, text, text_length, item_offset, item_length);
1511 static int
1512 compare_info_codepoint (const hb_glyph_info_t *pa,
1513 const hb_glyph_info_t *pb)
1515 return (int) pb->codepoint - (int) pa->codepoint;
1518 static inline void
1519 normalize_glyphs_cluster (hb_buffer_t *buffer,
1520 unsigned int start,
1521 unsigned int end,
1522 bool backward)
1524 hb_glyph_position_t *pos = buffer->pos;
1526 /* Total cluster advance */
1527 hb_position_t total_x_advance = 0, total_y_advance = 0;
1528 for (unsigned int i = start; i < end; i++)
1530 total_x_advance += pos[i].x_advance;
1531 total_y_advance += pos[i].y_advance;
1534 hb_position_t x_advance = 0, y_advance = 0;
1535 for (unsigned int i = start; i < end; i++)
1537 pos[i].x_offset += x_advance;
1538 pos[i].y_offset += y_advance;
1540 x_advance += pos[i].x_advance;
1541 y_advance += pos[i].y_advance;
1543 pos[i].x_advance = 0;
1544 pos[i].y_advance = 0;
1547 if (backward)
1549 /* Transfer all cluster advance to the last glyph. */
1550 pos[end - 1].x_advance = total_x_advance;
1551 pos[end - 1].y_advance = total_y_advance;
1553 hb_bubble_sort (buffer->info + start, end - start - 1, compare_info_codepoint, buffer->pos + start);
1554 } else {
1555 /* Transfer all cluster advance to the first glyph. */
1556 pos[start].x_advance += total_x_advance;
1557 pos[start].y_advance += total_y_advance;
1558 for (unsigned int i = start + 1; i < end; i++) {
1559 pos[i].x_offset -= total_x_advance;
1560 pos[i].y_offset -= total_y_advance;
1562 hb_bubble_sort (buffer->info + start + 1, end - start - 1, compare_info_codepoint, buffer->pos + start + 1);
1567 * hb_buffer_normalize_glyphs:
1568 * @buffer: a buffer.
1572 * Since: 1.0
1574 void
1575 hb_buffer_normalize_glyphs (hb_buffer_t *buffer)
1577 assert (buffer->have_positions);
1578 assert (buffer->content_type == HB_BUFFER_CONTENT_TYPE_GLYPHS);
1580 bool backward = HB_DIRECTION_IS_BACKWARD (buffer->props.direction);
1582 unsigned int count = buffer->len;
1583 if (unlikely (!count)) return;
1584 hb_glyph_info_t *info = buffer->info;
1586 unsigned int start = 0;
1587 unsigned int end;
1588 for (end = start + 1; end < count; end++)
1589 if (info[start].cluster != info[end].cluster) {
1590 normalize_glyphs_cluster (buffer, start, end, backward);
1591 start = end;
1593 normalize_glyphs_cluster (buffer, start, end, backward);