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
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)
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;
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
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.
84 hb_buffer_t::enlarge (unsigned int size)
86 if (unlikely (in_error))
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]))))
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]))))
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]));
108 if (unlikely (!new_pos || !new_info))
111 if (likely (new_pos))
114 if (likely (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);
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]));
143 hb_buffer_t::get_scratch_buffer (unsigned int *size)
146 have_positions = false;
151 *size = allocated * sizeof (pos[0]);
157 /* HarfBuzz-Internal API */
160 hb_buffer_t::reset (void)
162 if (unlikely (hb_object_is_inert (this)))
165 hb_unicode_funcs_destroy (unicode);
166 unicode = hb_unicode_funcs_get_default ();
172 hb_buffer_t::clear (void)
174 if (unlikely (hb_object_is_inert (this)))
177 hb_segment_properties_t default_props = HB_SEGMENT_PROPERTIES_DEFAULT;
178 props = default_props;
179 flags = HB_BUFFER_FLAGS_DEFAULT;
181 content_type = HB_BUFFER_CONTENT_TYPE_INVALID;
184 have_positions = false;
192 memset (allocated_var_bytes, 0, sizeof allocated_var_bytes);
193 memset (allocated_var_owner, 0, sizeof allocated_var_owner);
195 memset (context, 0, sizeof context);
196 memset (context_len, 0, sizeof context_len);
200 hb_buffer_t::add (hb_codepoint_t codepoint,
201 unsigned int cluster)
203 hb_glyph_info_t *glyph;
205 if (unlikely (!ensure (len + 1))) return;
209 memset (glyph, 0, sizeof (*glyph));
210 glyph->codepoint = codepoint;
212 glyph->cluster = cluster;
218 hb_buffer_t::remove_output (void)
220 if (unlikely (hb_object_is_inert (this)))
224 have_positions = false;
231 hb_buffer_t::clear_output (void)
233 if (unlikely (hb_object_is_inert (this)))
237 have_positions = false;
244 hb_buffer_t::clear_positions (void)
246 if (unlikely (hb_object_is_inert (this)))
250 have_positions = true;
255 memset (pos, 0, sizeof (pos[0]) * len);
259 hb_buffer_t::swap_buffers (void)
261 if (unlikely (in_error)) return;
263 assert (have_output);
266 if (out_info != info)
268 hb_glyph_info_t *tmp_string;
271 out_info = tmp_string;
272 pos = (hb_glyph_position_t *) out_info;
285 hb_buffer_t::replace_glyphs (unsigned int num_in,
286 unsigned int num_out,
287 const uint32_t *glyph_data)
289 if (unlikely (!make_room_for (num_in, num_out))) return;
291 merge_clusters (idx, idx + num_in);
293 hb_glyph_info_t orig_info = info[idx];
294 hb_glyph_info_t *pinfo = &out_info[out_len];
295 for (unsigned int i = 0; i < num_out; i++)
298 pinfo->codepoint = glyph_data[i];
307 hb_buffer_t::output_glyph (hb_codepoint_t glyph_index)
309 if (unlikely (!make_room_for (0, 1))) return;
311 out_info[out_len] = info[idx];
312 out_info[out_len].codepoint = glyph_index;
318 hb_buffer_t::output_info (hb_glyph_info_t &glyph_info)
320 if (unlikely (!make_room_for (0, 1))) return;
322 out_info[out_len] = glyph_info;
328 hb_buffer_t::copy_glyph (void)
330 if (unlikely (!make_room_for (0, 1))) return;
332 out_info[out_len] = info[idx];
338 hb_buffer_t::replace_glyph (hb_codepoint_t glyph_index)
340 if (unlikely (out_info != info || out_len != idx)) {
341 if (unlikely (!make_room_for (1, 1))) return;
342 out_info[out_len] = info[idx];
344 out_info[out_len].codepoint = glyph_index;
352 hb_buffer_t::set_masks (hb_mask_t value,
354 unsigned int cluster_start,
355 unsigned int cluster_end)
357 hb_mask_t not_mask = ~mask;
363 if (cluster_start == 0 && cluster_end == (unsigned int)-1) {
364 unsigned int count = len;
365 for (unsigned int i = 0; i < count; i++)
366 info[i].mask = (info[i].mask & not_mask) | value;
370 unsigned int count = len;
371 for (unsigned int i = 0; i < count; i++)
372 if (cluster_start <= info[i].cluster && info[i].cluster < cluster_end)
373 info[i].mask = (info[i].mask & not_mask) | value;
377 hb_buffer_t::reverse_range (unsigned int start,
382 if (start == end - 1)
385 for (i = start, j = end - 1; i < j; i++, j--) {
394 for (i = start, j = end - 1; i < j; i++, j--) {
395 hb_glyph_position_t t;
405 hb_buffer_t::reverse (void)
410 reverse_range (0, len);
414 hb_buffer_t::reverse_clusters (void)
416 unsigned int i, start, count, last_cluster;
425 last_cluster = info[0].cluster;
426 for (i = 1; i < count; i++) {
427 if (last_cluster != info[i].cluster) {
428 reverse_range (start, i);
430 last_cluster = info[i].cluster;
433 reverse_range (start, i);
437 hb_buffer_t::merge_clusters (unsigned int start,
440 if (unlikely (end - start < 2))
443 unsigned int cluster = info[start].cluster;
445 for (unsigned int i = start + 1; i < end; i++)
446 cluster = MIN (cluster, info[i].cluster);
449 while (end < len && info[end - 1].cluster == info[end].cluster)
453 while (idx < start && info[start - 1].cluster == info[start].cluster)
456 /* If we hit the start of buffer, continue in out-buffer. */
458 for (unsigned i = out_len; i && out_info[i - 1].cluster == info[start].cluster; i--)
459 out_info[i - 1].cluster = cluster;
461 for (unsigned int i = start; i < end; i++)
462 info[i].cluster = cluster;
465 hb_buffer_t::merge_out_clusters (unsigned int start,
468 if (unlikely (end - start < 2))
471 unsigned int cluster = out_info[start].cluster;
473 for (unsigned int i = start + 1; i < end; i++)
474 cluster = MIN (cluster, out_info[i].cluster);
477 while (start && out_info[start - 1].cluster == out_info[start].cluster)
481 while (end < out_len && out_info[end - 1].cluster == out_info[end].cluster)
484 /* If we hit the end of out-buffer, continue in buffer. */
486 for (unsigned i = idx; i < len && info[i].cluster == out_info[end - 1].cluster; i++)
487 info[i].cluster = cluster;
489 for (unsigned int i = start; i < end; i++)
490 out_info[i].cluster = cluster;
494 hb_buffer_t::guess_segment_properties (void)
496 assert (content_type == HB_BUFFER_CONTENT_TYPE_UNICODE ||
497 (!len && content_type == HB_BUFFER_CONTENT_TYPE_INVALID));
499 /* If script is set to INVALID, guess from buffer contents */
500 if (props.script == HB_SCRIPT_INVALID) {
501 for (unsigned int i = 0; i < len; i++) {
502 hb_script_t script = unicode->script (info[i].codepoint);
503 if (likely (script != HB_SCRIPT_COMMON &&
504 script != HB_SCRIPT_INHERITED &&
505 script != HB_SCRIPT_UNKNOWN)) {
506 props.script = script;
512 /* If direction is set to INVALID, guess from script */
513 if (props.direction == HB_DIRECTION_INVALID) {
514 props.direction = hb_script_get_horizontal_direction (props.script);
517 /* If language is not set, use default language from locale */
518 if (props.language == HB_LANGUAGE_INVALID) {
519 /* TODO get_default_for_script? using $LANGUAGE */
520 props.language = hb_language_get_default ();
526 dump_var_allocation (const hb_buffer_t *buffer)
529 for (unsigned int i = 0; i < 8; i++)
530 buf[i] = '0' + buffer->allocated_var_bytes[7 - i];
532 DEBUG_MSG (BUFFER, buffer,
533 "Current var allocation: %s",
537 void hb_buffer_t::allocate_var (unsigned int byte_i, unsigned int count, const char *owner)
539 assert (byte_i < 8 && byte_i + count <= 8);
542 dump_var_allocation (this);
543 DEBUG_MSG (BUFFER, this,
544 "Allocating var bytes %d..%d for %s",
545 byte_i, byte_i + count - 1, owner);
547 for (unsigned int i = byte_i; i < byte_i + count; i++) {
548 assert (!allocated_var_bytes[i]);
549 allocated_var_bytes[i]++;
550 allocated_var_owner[i] = owner;
554 void hb_buffer_t::deallocate_var (unsigned int byte_i, unsigned int count, const char *owner)
557 dump_var_allocation (this);
559 DEBUG_MSG (BUFFER, this,
560 "Deallocating var bytes %d..%d for %s",
561 byte_i, byte_i + count - 1, owner);
563 assert (byte_i < 8 && byte_i + count <= 8);
564 for (unsigned int i = byte_i; i < byte_i + count; i++) {
565 assert (allocated_var_bytes[i]);
566 assert (0 == strcmp (allocated_var_owner[i], owner));
567 allocated_var_bytes[i]--;
571 void hb_buffer_t::assert_var (unsigned int byte_i, unsigned int count, const char *owner)
574 dump_var_allocation (this);
576 DEBUG_MSG (BUFFER, this,
577 "Asserting var bytes %d..%d for %s",
578 byte_i, byte_i + count - 1, owner);
580 assert (byte_i < 8 && byte_i + count <= 8);
581 for (unsigned int i = byte_i; i < byte_i + count; i++) {
582 assert (allocated_var_bytes[i]);
583 assert (0 == strcmp (allocated_var_owner[i], owner));
587 void hb_buffer_t::deallocate_var_all (void)
589 memset (allocated_var_bytes, 0, sizeof (allocated_var_bytes));
590 memset (allocated_var_owner, 0, sizeof (allocated_var_owner));
596 hb_buffer_create (void)
600 if (!(buffer = hb_object_create<hb_buffer_t> ()))
601 return hb_buffer_get_empty ();
609 hb_buffer_get_empty (void)
611 static const hb_buffer_t _hb_buffer_nil = {
612 HB_OBJECT_HEADER_STATIC,
614 const_cast<hb_unicode_funcs_t *> (&_hb_unicode_funcs_nil),
615 HB_SEGMENT_PROPERTIES_DEFAULT,
616 HB_BUFFER_FLAGS_DEFAULT,
618 HB_BUFFER_CONTENT_TYPE_INVALID,
620 true, /* have_output */
621 true /* have_positions */
623 /* Zero is good enough for everything else. */
626 return const_cast<hb_buffer_t *> (&_hb_buffer_nil);
630 hb_buffer_reference (hb_buffer_t *buffer)
632 return hb_object_reference (buffer);
636 hb_buffer_destroy (hb_buffer_t *buffer)
638 if (!hb_object_destroy (buffer)) return;
640 hb_unicode_funcs_destroy (buffer->unicode);
649 hb_buffer_set_user_data (hb_buffer_t *buffer,
650 hb_user_data_key_t *key,
652 hb_destroy_func_t destroy,
655 return hb_object_set_user_data (buffer, key, data, destroy, replace);
659 hb_buffer_get_user_data (hb_buffer_t *buffer,
660 hb_user_data_key_t *key)
662 return hb_object_get_user_data (buffer, key);
667 hb_buffer_set_content_type (hb_buffer_t *buffer,
668 hb_buffer_content_type_t content_type)
670 buffer->content_type = content_type;
673 hb_buffer_content_type_t
674 hb_buffer_get_content_type (hb_buffer_t *buffer)
676 return buffer->content_type;
681 hb_buffer_set_unicode_funcs (hb_buffer_t *buffer,
682 hb_unicode_funcs_t *unicode)
684 if (unlikely (hb_object_is_inert (buffer)))
688 unicode = hb_unicode_funcs_get_default ();
691 hb_unicode_funcs_reference (unicode);
692 hb_unicode_funcs_destroy (buffer->unicode);
693 buffer->unicode = unicode;
697 hb_buffer_get_unicode_funcs (hb_buffer_t *buffer)
699 return buffer->unicode;
703 hb_buffer_set_direction (hb_buffer_t *buffer,
704 hb_direction_t direction)
707 if (unlikely (hb_object_is_inert (buffer)))
710 buffer->props.direction = direction;
714 hb_buffer_get_direction (hb_buffer_t *buffer)
716 return buffer->props.direction;
720 hb_buffer_set_script (hb_buffer_t *buffer,
723 if (unlikely (hb_object_is_inert (buffer)))
726 buffer->props.script = script;
730 hb_buffer_get_script (hb_buffer_t *buffer)
732 return buffer->props.script;
736 hb_buffer_set_language (hb_buffer_t *buffer,
737 hb_language_t language)
739 if (unlikely (hb_object_is_inert (buffer)))
742 buffer->props.language = language;
746 hb_buffer_get_language (hb_buffer_t *buffer)
748 return buffer->props.language;
752 hb_buffer_set_segment_properties (hb_buffer_t *buffer,
753 const hb_segment_properties_t *props)
755 if (unlikely (hb_object_is_inert (buffer)))
758 buffer->props = *props;
762 hb_buffer_get_segment_properties (hb_buffer_t *buffer,
763 hb_segment_properties_t *props)
765 *props = buffer->props;
770 hb_buffer_set_flags (hb_buffer_t *buffer,
771 hb_buffer_flags_t flags)
773 if (unlikely (hb_object_is_inert (buffer)))
776 buffer->flags = flags;
780 hb_buffer_get_flags (hb_buffer_t *buffer)
782 return buffer->flags;
787 hb_buffer_reset (hb_buffer_t *buffer)
793 hb_buffer_clear_contents (hb_buffer_t *buffer)
799 hb_buffer_pre_allocate (hb_buffer_t *buffer, unsigned int size)
801 return buffer->ensure (size);
805 hb_buffer_allocation_successful (hb_buffer_t *buffer)
807 return !buffer->in_error;
811 hb_buffer_add (hb_buffer_t *buffer,
812 hb_codepoint_t codepoint,
813 unsigned int cluster)
815 buffer->add (codepoint, cluster);
816 buffer->clear_context (1);
820 hb_buffer_set_length (hb_buffer_t *buffer,
823 if (unlikely (hb_object_is_inert (buffer)))
826 if (!buffer->ensure (length))
829 /* Wipe the new space */
830 if (length > buffer->len) {
831 memset (buffer->info + buffer->len, 0, sizeof (buffer->info[0]) * (length - buffer->len));
832 if (buffer->have_positions)
833 memset (buffer->pos + buffer->len, 0, sizeof (buffer->pos[0]) * (length - buffer->len));
836 buffer->len = length;
839 buffer->clear_context (0);
840 buffer->clear_context (1);
846 hb_buffer_get_length (hb_buffer_t *buffer)
851 /* Return value valid as long as buffer not modified */
853 hb_buffer_get_glyph_infos (hb_buffer_t *buffer,
854 unsigned int *length)
857 *length = buffer->len;
859 return (hb_glyph_info_t *) buffer->info;
862 /* Return value valid as long as buffer not modified */
863 hb_glyph_position_t *
864 hb_buffer_get_glyph_positions (hb_buffer_t *buffer,
865 unsigned int *length)
867 if (!buffer->have_positions)
868 buffer->clear_positions ();
871 *length = buffer->len;
873 return (hb_glyph_position_t *) buffer->pos;
877 hb_buffer_reverse (hb_buffer_t *buffer)
883 hb_buffer_reverse_clusters (hb_buffer_t *buffer)
885 buffer->reverse_clusters ();
889 hb_buffer_guess_segment_properties (hb_buffer_t *buffer)
891 buffer->guess_segment_properties ();
894 template <typename T>
896 hb_buffer_add_utf (hb_buffer_t *buffer,
899 unsigned int item_offset,
902 assert (buffer->content_type == HB_BUFFER_CONTENT_TYPE_UNICODE ||
903 (!buffer->len && buffer->content_type == HB_BUFFER_CONTENT_TYPE_INVALID));
905 if (unlikely (hb_object_is_inert (buffer)))
908 if (text_length == -1)
909 text_length = hb_utf_strlen (text);
911 if (item_length == -1)
912 item_length = text_length - item_offset;
914 buffer->ensure (buffer->len + item_length * sizeof (T) / 4);
916 /* If buffer is empty and pre-context provided, install it.
917 * This check is written this way, to make sure people can
918 * provide pre-context in one add_utf() call, then provide
919 * text in a follow-up call. See:
921 * https://bugzilla.mozilla.org/show_bug.cgi?id=801410#c13
923 if (!buffer->len && item_offset > 0)
925 /* Add pre-context */
926 buffer->clear_context (0);
927 const T *prev = text + item_offset;
928 const T *start = text;
929 while (start < prev && buffer->context_len[0] < buffer->CONTEXT_LENGTH)
932 prev = hb_utf_prev (prev, start, &u);
933 buffer->context[0][buffer->context_len[0]++] = u;
937 const T *next = text + item_offset;
938 const T *end = next + item_length;
942 const T *old_next = next;
943 next = hb_utf_next (next, end, &u);
944 buffer->add (u, old_next - (const T *) text);
947 /* Add post-context */
948 buffer->clear_context (1);
949 end = text + text_length;
950 while (next < end && buffer->context_len[1] < buffer->CONTEXT_LENGTH)
953 next = hb_utf_next (next, end, &u);
954 buffer->context[1][buffer->context_len[1]++] = u;
957 buffer->content_type = HB_BUFFER_CONTENT_TYPE_UNICODE;
961 hb_buffer_add_utf8 (hb_buffer_t *buffer,
964 unsigned int item_offset,
967 hb_buffer_add_utf (buffer, (const uint8_t *) text, text_length, item_offset, item_length);
971 hb_buffer_add_utf16 (hb_buffer_t *buffer,
972 const uint16_t *text,
974 unsigned int item_offset,
977 hb_buffer_add_utf (buffer, text, text_length, item_offset, item_length);
981 hb_buffer_add_utf32 (hb_buffer_t *buffer,
982 const uint32_t *text,
984 unsigned int item_offset,
987 hb_buffer_add_utf (buffer, text, text_length, item_offset, item_length);
992 compare_info_codepoint (const hb_glyph_info_t *pa,
993 const hb_glyph_info_t *pb)
995 return (int) pb->codepoint - (int) pa->codepoint;
999 normalize_glyphs_cluster (hb_buffer_t *buffer,
1004 hb_glyph_position_t *pos = buffer->pos;
1006 /* Total cluster advance */
1007 hb_position_t total_x_advance = 0, total_y_advance = 0;
1008 for (unsigned int i = start; i < end; i++)
1010 total_x_advance += pos[i].x_advance;
1011 total_y_advance += pos[i].y_advance;
1014 hb_position_t x_advance = 0, y_advance = 0;
1015 for (unsigned int i = start; i < end; i++)
1017 pos[i].x_offset += x_advance;
1018 pos[i].y_offset += y_advance;
1020 x_advance += pos[i].x_advance;
1021 y_advance += pos[i].y_advance;
1023 pos[i].x_advance = 0;
1024 pos[i].y_advance = 0;
1029 /* Transfer all cluster advance to the last glyph. */
1030 pos[end - 1].x_advance = total_x_advance;
1031 pos[end - 1].y_advance = total_y_advance;
1033 hb_bubble_sort (buffer->info + start, end - start - 1, compare_info_codepoint, buffer->pos + start);
1035 /* Transfer all cluster advance to the first glyph. */
1036 pos[start].x_advance += total_x_advance;
1037 pos[start].y_advance += total_y_advance;
1038 for (unsigned int i = start + 1; i < end; i++) {
1039 pos[i].x_offset -= total_x_advance;
1040 pos[i].y_offset -= total_y_advance;
1042 hb_bubble_sort (buffer->info + start + 1, end - start - 1, compare_info_codepoint, buffer->pos + start + 1);
1047 hb_buffer_normalize_glyphs (hb_buffer_t *buffer)
1049 assert (buffer->have_positions);
1050 assert (buffer->content_type == HB_BUFFER_CONTENT_TYPE_GLYPHS);
1052 bool backward = HB_DIRECTION_IS_BACKWARD (buffer->props.direction);
1054 unsigned int count = buffer->len;
1055 if (unlikely (!count)) return;
1056 hb_glyph_info_t *info = buffer->info;
1058 unsigned int start = 0;
1060 for (end = start + 1; end < count; end++)
1061 if (info[start].cluster != info[end].cluster) {
1062 normalize_glyphs_cluster (buffer, start, end, backward);
1065 normalize_glyphs_cluster (buffer, start, end, backward);
1073 static const char *serialize_formats[] = {
1080 hb_buffer_serialize_list_formats (void)
1082 return serialize_formats;
1085 hb_buffer_serialize_format_t
1086 hb_buffer_serialize_format_from_string (const char *str, int len)
1088 /* Upper-case it. */
1089 return (hb_buffer_serialize_format_t) (hb_tag_from_string (str, len) & ~0x20202020);
1093 hb_buffer_serialize_format_to_string (hb_buffer_serialize_format_t format)
1097 case HB_BUFFER_SERIALIZE_FORMAT_TEXT: return serialize_formats[0];
1098 case HB_BUFFER_SERIALIZE_FORMAT_JSON: return serialize_formats[1];
1100 case HB_BUFFER_SERIALIZE_FORMAT_INVALID: return NULL;
1105 _hb_buffer_serialize_glyphs_json (hb_buffer_t *buffer,
1109 unsigned int buf_size,
1110 unsigned int *buf_consumed,
1112 hb_buffer_serialize_flags_t flags)
1114 hb_glyph_info_t *info = hb_buffer_get_glyph_infos (buffer, NULL);
1115 hb_glyph_position_t *pos = hb_buffer_get_glyph_positions (buffer, NULL);
1118 for (unsigned int i = start; i < end; i++)
1123 /* In the following code, we know b is large enough that no overflow can happen. */
1125 #define APPEND(s) HB_STMT_START { strcpy (p, s); p += strlen (s); } HB_STMT_END
1133 if (!(flags & HB_BUFFER_SERIALIZE_FLAG_NO_GLYPH_NAMES))
1136 hb_font_glyph_to_string (font, info[i].codepoint, g, sizeof (g));
1138 for (char *q = g; *q; q++) {
1146 p += snprintf (p, ARRAY_LENGTH (b) - (p - b), "%u", info[i].codepoint);
1148 if (!(flags & HB_BUFFER_SERIALIZE_FLAG_NO_CLUSTERS)) {
1149 p += snprintf (p, ARRAY_LENGTH (b) - (p - b), ",\"cl\":%u", info[i].cluster);
1152 if (!(flags & HB_BUFFER_SERIALIZE_FLAG_NO_POSITIONS))
1154 p += snprintf (p, ARRAY_LENGTH (b) - (p - b), ",\"dx\":%d,\"dy\":%d",
1155 pos[i].x_offset, pos[i].y_offset);
1156 p += snprintf (p, ARRAY_LENGTH (b) - (p - b), ",\"ax\":%d,\"ay\":%d",
1157 pos[i].x_advance, pos[i].y_advance);
1162 if (buf_size > (p - b))
1164 unsigned int l = p - b;
1178 _hb_buffer_serialize_glyphs_text (hb_buffer_t *buffer,
1182 unsigned int buf_size,
1183 unsigned int *buf_consumed,
1185 hb_buffer_serialize_flags_t flags)
1187 hb_glyph_info_t *info = hb_buffer_get_glyph_infos (buffer, NULL);
1188 hb_glyph_position_t *pos = hb_buffer_get_glyph_positions (buffer, NULL);
1189 hb_direction_t direction = hb_buffer_get_direction (buffer);
1192 for (unsigned int i = start; i < end; i++)
1197 /* In the following code, we know b is large enough that no overflow can happen. */
1202 if (!(flags & HB_BUFFER_SERIALIZE_FLAG_NO_GLYPH_NAMES))
1204 hb_font_glyph_to_string (font, info[i].codepoint, p, 128);
1208 p += snprintf (p, ARRAY_LENGTH (b) - (p - b), "%u", info[i].codepoint);
1210 if (!(flags & HB_BUFFER_SERIALIZE_FLAG_NO_CLUSTERS)) {
1211 p += snprintf (p, ARRAY_LENGTH (b) - (p - b), "=%u", info[i].cluster);
1214 if (!(flags & HB_BUFFER_SERIALIZE_FLAG_NO_POSITIONS))
1216 if (pos[i].x_offset || pos[i].y_offset)
1217 p += snprintf (p, ARRAY_LENGTH (b) - (p - b), "@%d,%d", pos[i].x_offset, pos[i].y_offset);
1220 if (HB_DIRECTION_IS_HORIZONTAL (direction) || pos[i].x_advance)
1221 p += snprintf (p, ARRAY_LENGTH (b) - (p - b), "%d", pos[i].x_advance);
1222 if (HB_DIRECTION_IS_VERTICAL (direction) || pos->y_advance)
1223 p += snprintf (p, ARRAY_LENGTH (b) - (p - b), ",%d", pos[i].y_advance);
1226 if (buf_size > (p - b))
1228 unsigned int l = p - b;
1241 /* Returns number of items, starting at start, that were serialized. */
1243 hb_buffer_serialize_glyphs (hb_buffer_t *buffer,
1247 unsigned int buf_size,
1248 unsigned int *buf_consumed,
1249 hb_font_t *font, /* May be NULL */
1250 hb_buffer_serialize_format_t format,
1251 hb_buffer_serialize_flags_t flags)
1253 assert (start <= end && end <= buffer->len);
1257 assert ((!buffer->len && buffer->content_type == HB_BUFFER_CONTENT_TYPE_INVALID) ||
1258 buffer->content_type == HB_BUFFER_CONTENT_TYPE_GLYPHS);
1260 if (unlikely (start == end))
1264 font = hb_font_get_empty ();
1268 case HB_BUFFER_SERIALIZE_FORMAT_TEXT:
1269 return _hb_buffer_serialize_glyphs_text (buffer, start, end,
1270 buf, buf_size, buf_consumed,
1273 case HB_BUFFER_SERIALIZE_FORMAT_JSON:
1274 return _hb_buffer_serialize_glyphs_json (buffer, start, end,
1275 buf, buf_size, buf_consumed,
1279 case HB_BUFFER_SERIALIZE_FORMAT_INVALID:
1286 hb_buffer_deserialize_glyphs (hb_buffer_t *buffer,
1288 unsigned int buf_len,
1289 unsigned int *buf_consumed,
1290 hb_font_t *font, /* May be NULL */
1291 hb_buffer_serialize_format_t format)