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.
5 *
6 * This is part of HarfBuzz, a text shaping library.
7 *
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.
13 *
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.
19 *
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.
25 *
26 * Red Hat Author(s): Owen Taylor, Behdad Esfahbod
27 * Google Author(s): Behdad Esfahbod
28 */
29
30#include "hb-buffer.hh"
31#include "hb-utf.hh"
32
33
34/**
35 * SECTION: hb-buffer
36 * @title: hb-buffer
37 * @short_description: Input and output buffers
38 * @include: hb.h
39 *
40 * Buffers serve dual role in HarfBuzz; they hold the input characters that are
41 * passed to hb_shape(), and after shaping they hold the output glyphs.
42 **/
43
44
45/**
46 * hb_segment_properties_equal:
47 * @a: first #hb_segment_properties_t to compare.
48 * @b: second #hb_segment_properties_t to compare.
49 *
50 * Checks the equality of two #hb_segment_properties_t's.
51 *
52 * Return value:
53 * %true if all properties of @a equal those of @b, false otherwise.
54 *
55 * Since: 0.9.7
56 **/
57hb_bool_t
58hb_segment_properties_equal (const hb_segment_properties_t *a,
59 const hb_segment_properties_t *b)
60{
61 return a->direction == b->direction &&
62 a->script == b->script &&
63 a->language == b->language &&
64 a->reserved1 == b->reserved1 &&
65 a->reserved2 == b->reserved2;
66
67}
68
69/**
70 * hb_segment_properties_hash:
71 * @p: #hb_segment_properties_t to hash.
72 *
73 * Creates a hash representing @p.
74 *
75 * Return value:
76 * A hash of @p.
77 *
78 * Since: 0.9.7
79 **/
80unsigned int
81hb_segment_properties_hash (const hb_segment_properties_t *p)
82{
83 return (unsigned int) p->direction ^
84 (unsigned int) p->script ^
85 (intptr_t) (p->language);
86}
87
88
89
90/* Here is how the buffer works internally:
91 *
92 * There are two info pointers: info and out_info. They always have
93 * the same allocated size, but different lengths.
94 *
95 * As an optimization, both info and out_info may point to the
96 * same piece of memory, which is owned by info. This remains the
97 * case as long as out_len doesn't exceed i at any time.
98 * In that case, swap_buffers() is no-op and the glyph operations operate
99 * mostly in-place.
100 *
101 * As soon as out_info gets longer than info, out_info is moved over
102 * to an alternate buffer (which we reuse the pos buffer for!), and its
103 * current contents (out_len entries) are copied to the new place.
104 * This should all remain transparent to the user. swap_buffers() then
105 * switches info and out_info.
106 */
107
108
109
110/* Internal API */
111
112bool
113hb_buffer_t::enlarge (unsigned int size)
114{
115 if (unlikely (!successful))
116 return false;
117 if (unlikely (size > max_len))
118 {
119 successful = false;
120 return false;
121 }
122
123 unsigned int new_allocated = allocated;
124 hb_glyph_position_t *new_pos = nullptr;
125 hb_glyph_info_t *new_info = nullptr;
126 bool separate_out = out_info != info;
127
128 if (unlikely (hb_unsigned_mul_overflows (size, sizeof (info[0]))))
129 goto done;
130
131 while (size >= new_allocated)
132 new_allocated += (new_allocated >> 1) + 32;
133
134 static_assert ((sizeof (info[0]) == sizeof (pos[0])), "");
135 if (unlikely (hb_unsigned_mul_overflows (new_allocated, sizeof (info[0]))))
136 goto done;
137
138 new_pos = (hb_glyph_position_t *) realloc (pos, new_allocated * sizeof (pos[0]));
139 new_info = (hb_glyph_info_t *) realloc (info, new_allocated * sizeof (info[0]));
140
141done:
142 if (unlikely (!new_pos || !new_info))
143 successful = false;
144
145 if (likely (new_pos))
146 pos = new_pos;
147
148 if (likely (new_info))
149 info = new_info;
150
151 out_info = separate_out ? (hb_glyph_info_t *) pos : info;
152 if (likely (successful))
153 allocated = new_allocated;
154
155 return likely (successful);
156}
157
158bool
159hb_buffer_t::make_room_for (unsigned int num_in,
160 unsigned int num_out)
161{
162 if (unlikely (!ensure (out_len + num_out))) return false;
163
164 if (out_info == info &&
165 out_len + num_out > idx + num_in)
166 {
167 assert (have_output);
168
169 out_info = (hb_glyph_info_t *) pos;
170 memcpy (out_info, info, out_len * sizeof (out_info[0]));
171 }
172
173 return true;
174}
175
176bool
177hb_buffer_t::shift_forward (unsigned int count)
178{
179 assert (have_output);
180 if (unlikely (!ensure (len + count))) return false;
181
182 memmove (info + idx + count, info + idx, (len - idx) * sizeof (info[0]));
183 if (idx + count > len)
184 {
185 /* Under memory failure we might expose this area. At least
186 * clean it up. Oh well...
187 *
188 * Ideally, we should at least set Default_Ignorable bits on
189 * these, as well as consistent cluster values. But the former
190 * is layering violation... */
191 memset (info + len, 0, (idx + count - len) * sizeof (info[0]));
192 }
193 len += count;
194 idx += count;
195
196 return true;
197}
198
199hb_buffer_t::scratch_buffer_t *
200hb_buffer_t::get_scratch_buffer (unsigned int *size)
201{
202 have_output = false;
203 have_positions = false;
204
205 out_len = 0;
206 out_info = info;
207
208 assert ((uintptr_t) pos % sizeof (scratch_buffer_t) == 0);
209 *size = allocated * sizeof (pos[0]) / sizeof (scratch_buffer_t);
210 return (scratch_buffer_t *) (void *) pos;
211}
212
213
214
215/* HarfBuzz-Internal API */
216
217void
218hb_buffer_t::reset ()
219{
220 if (unlikely (hb_object_is_immutable (this)))
221 return;
222
223 hb_unicode_funcs_destroy (unicode);
224 unicode = hb_unicode_funcs_reference (hb_unicode_funcs_get_default ());
225 flags = HB_BUFFER_FLAG_DEFAULT;
226 replacement = HB_BUFFER_REPLACEMENT_CODEPOINT_DEFAULT;
227 invisible = 0;
228
229 clear ();
230}
231
232void
233hb_buffer_t::clear ()
234{
235 if (unlikely (hb_object_is_immutable (this)))
236 return;
237
238 hb_segment_properties_t default_props = HB_SEGMENT_PROPERTIES_DEFAULT;
239 props = default_props;
240 scratch_flags = HB_BUFFER_SCRATCH_FLAG_DEFAULT;
241
242 content_type = HB_BUFFER_CONTENT_TYPE_INVALID;
243 successful = true;
244 have_output = false;
245 have_positions = false;
246
247 idx = 0;
248 len = 0;
249 out_len = 0;
250 out_info = info;
251
252 serial = 0;
253
254 memset (context, 0, sizeof context);
255 memset (context_len, 0, sizeof context_len);
256
257 deallocate_var_all ();
258}
259
260void
261hb_buffer_t::add (hb_codepoint_t codepoint,
262 unsigned int cluster)
263{
264 hb_glyph_info_t *glyph;
265
266 if (unlikely (!ensure (len + 1))) return;
267
268 glyph = &info[len];
269
270 memset (glyph, 0, sizeof (*glyph));
271 glyph->codepoint = codepoint;
272 glyph->mask = 0;
273 glyph->cluster = cluster;
274
275 len++;
276}
277
278void
279hb_buffer_t::add_info (const hb_glyph_info_t &glyph_info)
280{
281 if (unlikely (!ensure (len + 1))) return;
282
283 info[len] = glyph_info;
284
285 len++;
286}
287
288
289void
290hb_buffer_t::remove_output ()
291{
292 if (unlikely (hb_object_is_immutable (this)))
293 return;
294
295 have_output = false;
296 have_positions = false;
297
298 out_len = 0;
299 out_info = info;
300}
301
302void
303hb_buffer_t::clear_output ()
304{
305 if (unlikely (hb_object_is_immutable (this)))
306 return;
307
308 have_output = true;
309 have_positions = false;
310
311 out_len = 0;
312 out_info = info;
313}
314
315void
316hb_buffer_t::clear_positions ()
317{
318 if (unlikely (hb_object_is_immutable (this)))
319 return;
320
321 have_output = false;
322 have_positions = true;
323
324 out_len = 0;
325 out_info = info;
326
327 hb_memset (pos, 0, sizeof (pos[0]) * len);
328}
329
330void
331hb_buffer_t::swap_buffers ()
332{
333 if (unlikely (!successful)) return;
334
335 assert (have_output);
336 have_output = false;
337
338 if (out_info != info)
339 {
340 hb_glyph_info_t *tmp_string;
341 tmp_string = info;
342 info = out_info;
343 out_info = tmp_string;
344 pos = (hb_glyph_position_t *) out_info;
345 }
346
347 unsigned int tmp;
348 tmp = len;
349 len = out_len;
350 out_len = tmp;
351
352 idx = 0;
353}
354
355
356void
357hb_buffer_t::replace_glyphs (unsigned int num_in,
358 unsigned int num_out,
359 const uint32_t *glyph_data)
360{
361 if (unlikely (!make_room_for (num_in, num_out))) return;
362
363 assert (idx + num_in <= len);
364
365 merge_clusters (idx, idx + num_in);
366
367 hb_glyph_info_t orig_info = info[idx];
368 hb_glyph_info_t *pinfo = &out_info[out_len];
369 for (unsigned int i = 0; i < num_out; i++)
370 {
371 *pinfo = orig_info;
372 pinfo->codepoint = glyph_data[i];
373 pinfo++;
374 }
375
376 idx += num_in;
377 out_len += num_out;
378}
379
380bool
381hb_buffer_t::move_to (unsigned int i)
382{
383 if (!have_output)
384 {
385 assert (i <= len);
386 idx = i;
387 return true;
388 }
389 if (unlikely (!successful))
390 return false;
391
392 assert (i <= out_len + (len - idx));
393
394 if (out_len < i)
395 {
396 unsigned int count = i - out_len;
397 if (unlikely (!make_room_for (count, count))) return false;
398
399 memmove (out_info + out_len, info + idx, count * sizeof (out_info[0]));
400 idx += count;
401 out_len += count;
402 }
403 else if (out_len > i)
404 {
405 /* Tricky part: rewinding... */
406 unsigned int count = out_len - i;
407
408 /* This will blow in our face if memory allocation fails later
409 * in this same lookup...
410 *
411 * We used to shift with extra 32 items, instead of the 0 below.
412 * But that would leave empty slots in the buffer in case of allocation
413 * failures. Setting to zero for now to avoid other problems (see
414 * comments in shift_forward(). This can cause O(N^2) behavior more
415 * severely than adding 32 empty slots can... */
416 if (unlikely (idx < count && !shift_forward (count + 0))) return false;
417
418 assert (idx >= count);
419
420 idx -= count;
421 out_len -= count;
422 memmove (info + idx, out_info + out_len, count * sizeof (out_info[0]));
423 }
424
425 return true;
426}
427
428
429void
430hb_buffer_t::set_masks (hb_mask_t value,
431 hb_mask_t mask,
432 unsigned int cluster_start,
433 unsigned int cluster_end)
434{
435 hb_mask_t not_mask = ~mask;
436 value &= mask;
437
438 if (!mask)
439 return;
440
441 if (cluster_start == 0 && cluster_end == (unsigned int)-1) {
442 unsigned int count = len;
443 for (unsigned int i = 0; i < count; i++)
444 info[i].mask = (info[i].mask & not_mask) | value;
445 return;
446 }
447
448 unsigned int count = len;
449 for (unsigned int i = 0; i < count; i++)
450 if (cluster_start <= info[i].cluster && info[i].cluster < cluster_end)
451 info[i].mask = (info[i].mask & not_mask) | value;
452}
453
454void
455hb_buffer_t::reverse_range (unsigned int start,
456 unsigned int end)
457{
458 unsigned int i, j;
459
460 if (end - start < 2)
461 return;
462
463 for (i = start, j = end - 1; i < j; i++, j--) {
464 hb_glyph_info_t t;
465
466 t = info[i];
467 info[i] = info[j];
468 info[j] = t;
469 }
470
471 if (have_positions) {
472 for (i = start, j = end - 1; i < j; i++, j--) {
473 hb_glyph_position_t t;
474
475 t = pos[i];
476 pos[i] = pos[j];
477 pos[j] = t;
478 }
479 }
480}
481
482void
483hb_buffer_t::reverse ()
484{
485 if (unlikely (!len))
486 return;
487
488 reverse_range (0, len);
489}
490
491void
492hb_buffer_t::reverse_clusters ()
493{
494 unsigned int i, start, count, last_cluster;
495
496 if (unlikely (!len))
497 return;
498
499 reverse ();
500
501 count = len;
502 start = 0;
503 last_cluster = info[0].cluster;
504 for (i = 1; i < count; i++) {
505 if (last_cluster != info[i].cluster) {
506 reverse_range (start, i);
507 start = i;
508 last_cluster = info[i].cluster;
509 }
510 }
511 reverse_range (start, i);
512}
513
514void
515hb_buffer_t::merge_clusters_impl (unsigned int start,
516 unsigned int end)
517{
518 if (cluster_level == HB_BUFFER_CLUSTER_LEVEL_CHARACTERS)
519 {
520 unsafe_to_break (start, end);
521 return;
522 }
523
524 unsigned int cluster = info[start].cluster;
525
526 for (unsigned int i = start + 1; i < end; i++)
527 cluster = hb_min (cluster, info[i].cluster);
528
529 /* Extend end */
530 while (end < len && info[end - 1].cluster == info[end].cluster)
531 end++;
532
533 /* Extend start */
534 while (idx < start && info[start - 1].cluster == info[start].cluster)
535 start--;
536
537 /* If we hit the start of buffer, continue in out-buffer. */
538 if (idx == start)
539 for (unsigned int i = out_len; i && out_info[i - 1].cluster == info[start].cluster; i--)
540 set_cluster (out_info[i - 1], cluster);
541
542 for (unsigned int i = start; i < end; i++)
543 set_cluster (info[i], cluster);
544}
545void
546hb_buffer_t::merge_out_clusters (unsigned int start,
547 unsigned int end)
548{
549 if (cluster_level == HB_BUFFER_CLUSTER_LEVEL_CHARACTERS)
550 return;
551
552 if (unlikely (end - start < 2))
553 return;
554
555 unsigned int cluster = out_info[start].cluster;
556
557 for (unsigned int i = start + 1; i < end; i++)
558 cluster = hb_min (cluster, out_info[i].cluster);
559
560 /* Extend start */
561 while (start && out_info[start - 1].cluster == out_info[start].cluster)
562 start--;
563
564 /* Extend end */
565 while (end < out_len && out_info[end - 1].cluster == out_info[end].cluster)
566 end++;
567
568 /* If we hit the end of out-buffer, continue in buffer. */
569 if (end == out_len)
570 for (unsigned int i = idx; i < len && info[i].cluster == out_info[end - 1].cluster; i++)
571 set_cluster (info[i], cluster);
572
573 for (unsigned int i = start; i < end; i++)
574 set_cluster (out_info[i], cluster);
575}
576void
577hb_buffer_t::delete_glyph ()
578{
579 /* The logic here is duplicated in hb_ot_hide_default_ignorables(). */
580
581 unsigned int cluster = info[idx].cluster;
582 if (idx + 1 < len && cluster == info[idx + 1].cluster)
583 {
584 /* Cluster survives; do nothing. */
585 goto done;
586 }
587
588 if (out_len)
589 {
590 /* Merge cluster backward. */
591 if (cluster < out_info[out_len - 1].cluster)
592 {
593 unsigned int mask = info[idx].mask;
594 unsigned int old_cluster = out_info[out_len - 1].cluster;
595 for (unsigned i = out_len; i && out_info[i - 1].cluster == old_cluster; i--)
596 set_cluster (out_info[i - 1], cluster, mask);
597 }
598 goto done;
599 }
600
601 if (idx + 1 < len)
602 {
603 /* Merge cluster forward. */
604 merge_clusters (idx, idx + 2);
605 goto done;
606 }
607
608done:
609 skip_glyph ();
610}
611
612void
613hb_buffer_t::unsafe_to_break_impl (unsigned int start, unsigned int end)
614{
615 unsigned int cluster = (unsigned int) -1;
616 cluster = _unsafe_to_break_find_min_cluster (info, start, end, cluster);
617 _unsafe_to_break_set_mask (info, start, end, cluster);
618}
619void
620hb_buffer_t::unsafe_to_break_from_outbuffer (unsigned int start, unsigned int end)
621{
622 if (!have_output)
623 {
624 unsafe_to_break_impl (start, end);
625 return;
626 }
627
628 assert (start <= out_len);
629 assert (idx <= end);
630
631 unsigned int cluster = (unsigned int) -1;
632 cluster = _unsafe_to_break_find_min_cluster (out_info, start, out_len, cluster);
633 cluster = _unsafe_to_break_find_min_cluster (info, idx, end, cluster);
634 _unsafe_to_break_set_mask (out_info, start, out_len, cluster);
635 _unsafe_to_break_set_mask (info, idx, end, cluster);
636}
637
638void
639hb_buffer_t::guess_segment_properties ()
640{
641 assert (content_type == HB_BUFFER_CONTENT_TYPE_UNICODE ||
642 (!len && content_type == HB_BUFFER_CONTENT_TYPE_INVALID));
643
644 /* If script is set to INVALID, guess from buffer contents */
645 if (props.script == HB_SCRIPT_INVALID) {
646 for (unsigned int i = 0; i < len; i++) {
647 hb_script_t script = unicode->script (info[i].codepoint);
648 if (likely (script != HB_SCRIPT_COMMON &&
649 script != HB_SCRIPT_INHERITED &&
650 script != HB_SCRIPT_UNKNOWN)) {
651 props.script = script;
652 break;
653 }
654 }
655 }
656
657 /* If direction is set to INVALID, guess from script */
658 if (props.direction == HB_DIRECTION_INVALID) {
659 props.direction = hb_script_get_horizontal_direction (props.script);
660 if (props.direction == HB_DIRECTION_INVALID)
661 props.direction = HB_DIRECTION_LTR;
662 }
663
664 /* If language is not set, use default language from locale */
665 if (props.language == HB_LANGUAGE_INVALID) {
666 /* TODO get_default_for_script? using $LANGUAGE */
667 props.language = hb_language_get_default ();
668 }
669}
670
671
672/* Public API */
673
674DEFINE_NULL_INSTANCE (hb_buffer_t) =
675{
676 HB_OBJECT_HEADER_STATIC,
677
678 const_cast<hb_unicode_funcs_t *> (&_hb_Null_hb_unicode_funcs_t),
679 HB_BUFFER_FLAG_DEFAULT,
680 HB_BUFFER_CLUSTER_LEVEL_DEFAULT,
681 HB_BUFFER_REPLACEMENT_CODEPOINT_DEFAULT,
682 0, /* invisible */
683 HB_BUFFER_SCRATCH_FLAG_DEFAULT,
684 HB_BUFFER_MAX_LEN_DEFAULT,
685 HB_BUFFER_MAX_OPS_DEFAULT,
686
687 HB_BUFFER_CONTENT_TYPE_INVALID,
688 HB_SEGMENT_PROPERTIES_DEFAULT,
689 false, /* successful */
690 true, /* have_output */
691 true /* have_positions */
692
693 /* Zero is good enough for everything else. */
694};
695
696
697/**
698 * hb_buffer_create: (Xconstructor)
699 *
700 * Creates a new #hb_buffer_t with all properties to defaults.
701 *
702 * Return value: (transfer full):
703 * A newly allocated #hb_buffer_t with a reference count of 1. The initial
704 * reference count should be released with hb_buffer_destroy() when you are done
705 * using the #hb_buffer_t. This function never returns %NULL. If memory cannot
706 * be allocated, a special #hb_buffer_t object will be returned on which
707 * hb_buffer_allocation_successful() returns %false.
708 *
709 * Since: 0.9.2
710 **/
711hb_buffer_t *
712hb_buffer_create ()
713{
714 hb_buffer_t *buffer;
715
716 if (!(buffer = hb_object_create<hb_buffer_t> ()))
717 return hb_buffer_get_empty ();
718
719 buffer->max_len = HB_BUFFER_MAX_LEN_DEFAULT;
720 buffer->max_ops = HB_BUFFER_MAX_OPS_DEFAULT;
721
722 buffer->reset ();
723
724 return buffer;
725}
726
727/**
728 * hb_buffer_get_empty:
729 *
730 *
731 *
732 * Return value: (transfer full):
733 *
734 * Since: 0.9.2
735 **/
736hb_buffer_t *
737hb_buffer_get_empty ()
738{
739 return const_cast<hb_buffer_t *> (&Null(hb_buffer_t));
740}
741
742/**
743 * hb_buffer_reference: (skip)
744 * @buffer: an #hb_buffer_t.
745 *
746 * Increases the reference count on @buffer by one. This prevents @buffer from
747 * being destroyed until a matching call to hb_buffer_destroy() is made.
748 *
749 * Return value: (transfer full):
750 * The referenced #hb_buffer_t.
751 *
752 * Since: 0.9.2
753 **/
754hb_buffer_t *
755hb_buffer_reference (hb_buffer_t *buffer)
756{
757 return hb_object_reference (buffer);
758}
759
760/**
761 * hb_buffer_destroy: (skip)
762 * @buffer: an #hb_buffer_t.
763 *
764 * Deallocate the @buffer.
765 * Decreases the reference count on @buffer by one. If the result is zero, then
766 * @buffer and all associated resources are freed. See hb_buffer_reference().
767 *
768 * Since: 0.9.2
769 **/
770void
771hb_buffer_destroy (hb_buffer_t *buffer)
772{
773 if (!hb_object_destroy (buffer)) return;
774
775 hb_unicode_funcs_destroy (buffer->unicode);
776
777 free (buffer->info);
778 free (buffer->pos);
779#ifndef HB_NO_BUFFER_MESSAGE
780 if (buffer->message_destroy)
781 buffer->message_destroy (buffer->message_data);
782#endif
783
784 free (buffer);
785}
786
787/**
788 * hb_buffer_set_user_data: (skip)
789 * @buffer: an #hb_buffer_t.
790 * @key:
791 * @data:
792 * @destroy:
793 * @replace:
794 *
795 *
796 *
797 * Return value:
798 *
799 * Since: 0.9.2
800 **/
801hb_bool_t
802hb_buffer_set_user_data (hb_buffer_t *buffer,
803 hb_user_data_key_t *key,
804 void * data,
805 hb_destroy_func_t destroy,
806 hb_bool_t replace)
807{
808 return hb_object_set_user_data (buffer, key, data, destroy, replace);
809}
810
811/**
812 * hb_buffer_get_user_data: (skip)
813 * @buffer: an #hb_buffer_t.
814 * @key:
815 *
816 *
817 *
818 * Return value:
819 *
820 * Since: 0.9.2
821 **/
822void *
823hb_buffer_get_user_data (hb_buffer_t *buffer,
824 hb_user_data_key_t *key)
825{
826 return hb_object_get_user_data (buffer, key);
827}
828
829
830/**
831 * hb_buffer_set_content_type:
832 * @buffer: an #hb_buffer_t.
833 * @content_type: the type of buffer contents to set
834 *
835 * Sets the type of @buffer contents, buffers are either empty, contain
836 * characters (before shaping) or glyphs (the result of shaping).
837 *
838 * Since: 0.9.5
839 **/
840void
841hb_buffer_set_content_type (hb_buffer_t *buffer,
842 hb_buffer_content_type_t content_type)
843{
844 buffer->content_type = content_type;
845}
846
847/**
848 * hb_buffer_get_content_type:
849 * @buffer: an #hb_buffer_t.
850 *
851 * see hb_buffer_set_content_type().
852 *
853 * Return value:
854 * The type of @buffer contents.
855 *
856 * Since: 0.9.5
857 **/
858hb_buffer_content_type_t
859hb_buffer_get_content_type (hb_buffer_t *buffer)
860{
861 return buffer->content_type;
862}
863
864
865/**
866 * hb_buffer_set_unicode_funcs:
867 * @buffer: an #hb_buffer_t.
868 * @unicode_funcs:
869 *
870 *
871 *
872 * Since: 0.9.2
873 **/
874void
875hb_buffer_set_unicode_funcs (hb_buffer_t *buffer,
876 hb_unicode_funcs_t *unicode_funcs)
877{
878 if (unlikely (hb_object_is_immutable (buffer)))
879 return;
880
881 if (!unicode_funcs)
882 unicode_funcs = hb_unicode_funcs_get_default ();
883
884 hb_unicode_funcs_reference (unicode_funcs);
885 hb_unicode_funcs_destroy (buffer->unicode);
886 buffer->unicode = unicode_funcs;
887}
888
889/**
890 * hb_buffer_get_unicode_funcs:
891 * @buffer: an #hb_buffer_t.
892 *
893 *
894 *
895 * Return value:
896 *
897 * Since: 0.9.2
898 **/
899hb_unicode_funcs_t *
900hb_buffer_get_unicode_funcs (hb_buffer_t *buffer)
901{
902 return buffer->unicode;
903}
904
905/**
906 * hb_buffer_set_direction:
907 * @buffer: an #hb_buffer_t.
908 * @direction: the #hb_direction_t of the @buffer
909 *
910 * Set the text flow direction of the buffer. No shaping can happen without
911 * setting @buffer direction, and it controls the visual direction for the
912 * output glyphs; for RTL direction the glyphs will be reversed. Many layout
913 * features depend on the proper setting of the direction, for example,
914 * reversing RTL text before shaping, then shaping with LTR direction is not
915 * the same as keeping the text in logical order and shaping with RTL
916 * direction.
917 *
918 * Since: 0.9.2
919 **/
920void
921hb_buffer_set_direction (hb_buffer_t *buffer,
922 hb_direction_t direction)
923
924{
925 if (unlikely (hb_object_is_immutable (buffer)))
926 return;
927
928 buffer->props.direction = direction;
929}
930
931/**
932 * hb_buffer_get_direction:
933 * @buffer: an #hb_buffer_t.
934 *
935 * See hb_buffer_set_direction()
936 *
937 * Return value:
938 * The direction of the @buffer.
939 *
940 * Since: 0.9.2
941 **/
942hb_direction_t
943hb_buffer_get_direction (hb_buffer_t *buffer)
944{
945 return buffer->props.direction;
946}
947
948/**
949 * hb_buffer_set_script:
950 * @buffer: an #hb_buffer_t.
951 * @script: an #hb_script_t to set.
952 *
953 * Sets the script of @buffer to @script.
954 *
955 * Script is crucial for choosing the proper shaping behaviour for scripts that
956 * require it (e.g. Arabic) and the which OpenType features defined in the font
957 * to be applied.
958 *
959 * You can pass one of the predefined #hb_script_t values, or use
960 * hb_script_from_string() or hb_script_from_iso15924_tag() to get the
961 * corresponding script from an ISO 15924 script tag.
962 *
963 * Since: 0.9.2
964 **/
965void
966hb_buffer_set_script (hb_buffer_t *buffer,
967 hb_script_t script)
968{
969 if (unlikely (hb_object_is_immutable (buffer)))
970 return;
971
972 buffer->props.script = script;
973}
974
975/**
976 * hb_buffer_get_script:
977 * @buffer: an #hb_buffer_t.
978 *
979 * See hb_buffer_set_script().
980 *
981 * Return value:
982 * The #hb_script_t of the @buffer.
983 *
984 * Since: 0.9.2
985 **/
986hb_script_t
987hb_buffer_get_script (hb_buffer_t *buffer)
988{
989 return buffer->props.script;
990}
991
992/**
993 * hb_buffer_set_language:
994 * @buffer: an #hb_buffer_t.
995 * @language: an hb_language_t to set.
996 *
997 * Sets the language of @buffer to @language.
998 *
999 * Languages are crucial for selecting which OpenType feature to apply to the
1000 * buffer which can result in applying language-specific behaviour. Languages
1001 * are orthogonal to the scripts, and though they are related, they are
1002 * different concepts and should not be confused with each other.
1003 *
1004 * Use hb_language_from_string() to convert from BCP 47 language tags to
1005 * #hb_language_t.
1006 *
1007 * Since: 0.9.2
1008 **/
1009void
1010hb_buffer_set_language (hb_buffer_t *buffer,
1011 hb_language_t language)
1012{
1013 if (unlikely (hb_object_is_immutable (buffer)))
1014 return;
1015
1016 buffer->props.language = language;
1017}
1018
1019/**
1020 * hb_buffer_get_language:
1021 * @buffer: an #hb_buffer_t.
1022 *
1023 * See hb_buffer_set_language().
1024 *
1025 * Return value: (transfer none):
1026 * The #hb_language_t of the buffer. Must not be freed by the caller.
1027 *
1028 * Since: 0.9.2
1029 **/
1030hb_language_t
1031hb_buffer_get_language (hb_buffer_t *buffer)
1032{
1033 return buffer->props.language;
1034}
1035
1036/**
1037 * hb_buffer_set_segment_properties:
1038 * @buffer: an #hb_buffer_t.
1039 * @props: an #hb_segment_properties_t to use.
1040 *
1041 * Sets the segment properties of the buffer, a shortcut for calling
1042 * hb_buffer_set_direction(), hb_buffer_set_script() and
1043 * hb_buffer_set_language() individually.
1044 *
1045 * Since: 0.9.7
1046 **/
1047void
1048hb_buffer_set_segment_properties (hb_buffer_t *buffer,
1049 const hb_segment_properties_t *props)
1050{
1051 if (unlikely (hb_object_is_immutable (buffer)))
1052 return;
1053
1054 buffer->props = *props;
1055}
1056
1057/**
1058 * hb_buffer_get_segment_properties:
1059 * @buffer: an #hb_buffer_t.
1060 * @props: (out): the output #hb_segment_properties_t.
1061 *
1062 * Sets @props to the #hb_segment_properties_t of @buffer.
1063 *
1064 * Since: 0.9.7
1065 **/
1066void
1067hb_buffer_get_segment_properties (hb_buffer_t *buffer,
1068 hb_segment_properties_t *props)
1069{
1070 *props = buffer->props;
1071}
1072
1073
1074/**
1075 * hb_buffer_set_flags:
1076 * @buffer: an #hb_buffer_t.
1077 * @flags: the buffer flags to set.
1078 *
1079 * Sets @buffer flags to @flags. See #hb_buffer_flags_t.
1080 *
1081 * Since: 0.9.7
1082 **/
1083void
1084hb_buffer_set_flags (hb_buffer_t *buffer,
1085 hb_buffer_flags_t flags)
1086{
1087 if (unlikely (hb_object_is_immutable (buffer)))
1088 return;
1089
1090 buffer->flags = flags;
1091}
1092
1093/**
1094 * hb_buffer_get_flags:
1095 * @buffer: an #hb_buffer_t.
1096 *
1097 * See hb_buffer_set_flags().
1098 *
1099 * Return value:
1100 * The @buffer flags.
1101 *
1102 * Since: 0.9.7
1103 **/
1104hb_buffer_flags_t
1105hb_buffer_get_flags (hb_buffer_t *buffer)
1106{
1107 return buffer->flags;
1108}
1109
1110/**
1111 * hb_buffer_set_cluster_level:
1112 * @buffer: an #hb_buffer_t.
1113 * @cluster_level:
1114 *
1115 *
1116 *
1117 * Since: 0.9.42
1118 **/
1119void
1120hb_buffer_set_cluster_level (hb_buffer_t *buffer,
1121 hb_buffer_cluster_level_t cluster_level)
1122{
1123 if (unlikely (hb_object_is_immutable (buffer)))
1124 return;
1125
1126 buffer->cluster_level = cluster_level;
1127}
1128
1129/**
1130 * hb_buffer_get_cluster_level:
1131 * @buffer: an #hb_buffer_t.
1132 *
1133 *
1134 *
1135 * Return value:
1136 *
1137 * Since: 0.9.42
1138 **/
1139hb_buffer_cluster_level_t
1140hb_buffer_get_cluster_level (hb_buffer_t *buffer)
1141{
1142 return buffer->cluster_level;
1143}
1144
1145
1146/**
1147 * hb_buffer_set_replacement_codepoint:
1148 * @buffer: an #hb_buffer_t.
1149 * @replacement: the replacement #hb_codepoint_t
1150 *
1151 * Sets the #hb_codepoint_t that replaces invalid entries for a given encoding
1152 * when adding text to @buffer.
1153 *
1154 * Default is %HB_BUFFER_REPLACEMENT_CODEPOINT_DEFAULT.
1155 *
1156 * Since: 0.9.31
1157 **/
1158void
1159hb_buffer_set_replacement_codepoint (hb_buffer_t *buffer,
1160 hb_codepoint_t replacement)
1161{
1162 if (unlikely (hb_object_is_immutable (buffer)))
1163 return;
1164
1165 buffer->replacement = replacement;
1166}
1167
1168/**
1169 * hb_buffer_get_replacement_codepoint:
1170 * @buffer: an #hb_buffer_t.
1171 *
1172 * See hb_buffer_set_replacement_codepoint().
1173 *
1174 * Return value:
1175 * The @buffer replacement #hb_codepoint_t.
1176 *
1177 * Since: 0.9.31
1178 **/
1179hb_codepoint_t
1180hb_buffer_get_replacement_codepoint (hb_buffer_t *buffer)
1181{
1182 return buffer->replacement;
1183}
1184
1185
1186/**
1187 * hb_buffer_set_invisible_glyph:
1188 * @buffer: an #hb_buffer_t.
1189 * @invisible: the invisible #hb_codepoint_t
1190 *
1191 * Sets the #hb_codepoint_t that replaces invisible characters in
1192 * the shaping result. If set to zero (default), the glyph for the
1193 * U+0020 SPACE character is used. Otherwise, this value is used
1194 * verbatim.
1195 *
1196 * Since: 2.0.0
1197 **/
1198void
1199hb_buffer_set_invisible_glyph (hb_buffer_t *buffer,
1200 hb_codepoint_t invisible)
1201{
1202 if (unlikely (hb_object_is_immutable (buffer)))
1203 return;
1204
1205 buffer->invisible = invisible;
1206}
1207
1208/**
1209 * hb_buffer_get_invisible_glyph:
1210 * @buffer: an #hb_buffer_t.
1211 *
1212 * See hb_buffer_set_invisible_glyph().
1213 *
1214 * Return value:
1215 * The @buffer invisible #hb_codepoint_t.
1216 *
1217 * Since: 2.0.0
1218 **/
1219hb_codepoint_t
1220hb_buffer_get_invisible_glyph (hb_buffer_t *buffer)
1221{
1222 return buffer->invisible;
1223}
1224
1225
1226/**
1227 * hb_buffer_reset:
1228 * @buffer: an #hb_buffer_t.
1229 *
1230 * Resets the buffer to its initial status, as if it was just newly created
1231 * with hb_buffer_create().
1232 *
1233 * Since: 0.9.2
1234 **/
1235void
1236hb_buffer_reset (hb_buffer_t *buffer)
1237{
1238 buffer->reset ();
1239}
1240
1241/**
1242 * hb_buffer_clear_contents:
1243 * @buffer: an #hb_buffer_t.
1244 *
1245 * Similar to hb_buffer_reset(), but does not clear the Unicode functions and
1246 * the replacement code point.
1247 *
1248 * Since: 0.9.11
1249 **/
1250void
1251hb_buffer_clear_contents (hb_buffer_t *buffer)
1252{
1253 buffer->clear ();
1254}
1255
1256/**
1257 * hb_buffer_pre_allocate:
1258 * @buffer: an #hb_buffer_t.
1259 * @size: number of items to pre allocate.
1260 *
1261 * Pre allocates memory for @buffer to fit at least @size number of items.
1262 *
1263 * Return value:
1264 * %true if @buffer memory allocation succeeded, %false otherwise.
1265 *
1266 * Since: 0.9.2
1267 **/
1268hb_bool_t
1269hb_buffer_pre_allocate (hb_buffer_t *buffer, unsigned int size)
1270{
1271 return buffer->ensure (size);
1272}
1273
1274/**
1275 * hb_buffer_allocation_successful:
1276 * @buffer: an #hb_buffer_t.
1277 *
1278 * Check if allocating memory for the buffer succeeded.
1279 *
1280 * Return value:
1281 * %true if @buffer memory allocation succeeded, %false otherwise.
1282 *
1283 * Since: 0.9.2
1284 **/
1285hb_bool_t
1286hb_buffer_allocation_successful (hb_buffer_t *buffer)
1287{
1288 return buffer->successful;
1289}
1290
1291/**
1292 * hb_buffer_add:
1293 * @buffer: an #hb_buffer_t.
1294 * @codepoint: a Unicode code point.
1295 * @cluster: the cluster value of @codepoint.
1296 *
1297 * Appends a character with the Unicode value of @codepoint to @buffer, and
1298 * gives it the initial cluster value of @cluster. Clusters can be any thing
1299 * the client wants, they are usually used to refer to the index of the
1300 * character in the input text stream and are output in
1301 * #hb_glyph_info_t.cluster field.
1302 *
1303 * This function does not check the validity of @codepoint, it is up to the
1304 * caller to ensure it is a valid Unicode code point.
1305 *
1306 * Since: 0.9.7
1307 **/
1308void
1309hb_buffer_add (hb_buffer_t *buffer,
1310 hb_codepoint_t codepoint,
1311 unsigned int cluster)
1312{
1313 buffer->add (codepoint, cluster);
1314 buffer->clear_context (1);
1315}
1316
1317/**
1318 * hb_buffer_set_length:
1319 * @buffer: an #hb_buffer_t.
1320 * @length: the new length of @buffer.
1321 *
1322 * Similar to hb_buffer_pre_allocate(), but clears any new items added at the
1323 * end.
1324 *
1325 * Return value:
1326 * %true if @buffer memory allocation succeeded, %false otherwise.
1327 *
1328 * Since: 0.9.2
1329 **/
1330hb_bool_t
1331hb_buffer_set_length (hb_buffer_t *buffer,
1332 unsigned int length)
1333{
1334 if (unlikely (hb_object_is_immutable (buffer)))
1335 return length == 0;
1336
1337 if (!buffer->ensure (length))
1338 return false;
1339
1340 /* Wipe the new space */
1341 if (length > buffer->len) {
1342 memset (buffer->info + buffer->len, 0, sizeof (buffer->info[0]) * (length - buffer->len));
1343 if (buffer->have_positions)
1344 memset (buffer->pos + buffer->len, 0, sizeof (buffer->pos[0]) * (length - buffer->len));
1345 }
1346
1347 buffer->len = length;
1348
1349 if (!length)
1350 {
1351 buffer->content_type = HB_BUFFER_CONTENT_TYPE_INVALID;
1352 buffer->clear_context (0);
1353 }
1354 buffer->clear_context (1);
1355
1356 return true;
1357}
1358
1359/**
1360 * hb_buffer_get_length:
1361 * @buffer: an #hb_buffer_t.
1362 *
1363 * Returns the number of items in the buffer.
1364 *
1365 * Return value:
1366 * The @buffer length.
1367 * The value valid as long as buffer has not been modified.
1368 *
1369 * Since: 0.9.2
1370 **/
1371unsigned int
1372hb_buffer_get_length (hb_buffer_t *buffer)
1373{
1374 return buffer->len;
1375}
1376
1377/**
1378 * hb_buffer_get_glyph_infos:
1379 * @buffer: an #hb_buffer_t.
1380 * @length: (out): output array length.
1381 *
1382 * Returns @buffer glyph information array. Returned pointer
1383 * is valid as long as @buffer contents are not modified.
1384 *
1385 * Return value: (transfer none) (array length=length):
1386 * The @buffer glyph information array.
1387 * The value valid as long as buffer has not been modified.
1388 *
1389 * Since: 0.9.2
1390 **/
1391hb_glyph_info_t *
1392hb_buffer_get_glyph_infos (hb_buffer_t *buffer,
1393 unsigned int *length)
1394{
1395 if (length)
1396 *length = buffer->len;
1397
1398 return (hb_glyph_info_t *) buffer->info;
1399}
1400
1401/**
1402 * hb_buffer_get_glyph_positions:
1403 * @buffer: an #hb_buffer_t.
1404 * @length: (out): output length.
1405 *
1406 * Returns @buffer glyph position array. Returned pointer
1407 * is valid as long as @buffer contents are not modified.
1408 *
1409 * Return value: (transfer none) (array length=length):
1410 * The @buffer glyph position array.
1411 * The value valid as long as buffer has not been modified.
1412 *
1413 * Since: 0.9.2
1414 **/
1415hb_glyph_position_t *
1416hb_buffer_get_glyph_positions (hb_buffer_t *buffer,
1417 unsigned int *length)
1418{
1419 if (!buffer->have_positions)
1420 buffer->clear_positions ();
1421
1422 if (length)
1423 *length = buffer->len;
1424
1425 return (hb_glyph_position_t *) buffer->pos;
1426}
1427
1428/**
1429 * hb_glyph_info_get_glyph_flags:
1430 * @info: a #hb_glyph_info_t.
1431 *
1432 * Returns glyph flags encoded within a #hb_glyph_info_t.
1433 *
1434 * Return value:
1435 * The #hb_glyph_flags_t encoded within @info.
1436 *
1437 * Since: 1.5.0
1438 **/
1439hb_glyph_flags_t
1440(hb_glyph_info_get_glyph_flags) (const hb_glyph_info_t *info)
1441{
1442 return hb_glyph_info_get_glyph_flags (info);
1443}
1444
1445/**
1446 * hb_buffer_reverse:
1447 * @buffer: an #hb_buffer_t.
1448 *
1449 * Reverses buffer contents.
1450 *
1451 * Since: 0.9.2
1452 **/
1453void
1454hb_buffer_reverse (hb_buffer_t *buffer)
1455{
1456 buffer->reverse ();
1457}
1458
1459/**
1460 * hb_buffer_reverse_range:
1461 * @buffer: an #hb_buffer_t.
1462 * @start: start index.
1463 * @end: end index.
1464 *
1465 * Reverses buffer contents between start to end.
1466 *
1467 * Since: 0.9.41
1468 **/
1469void
1470hb_buffer_reverse_range (hb_buffer_t *buffer,
1471 unsigned int start, unsigned int end)
1472{
1473 buffer->reverse_range (start, end);
1474}
1475
1476/**
1477 * hb_buffer_reverse_clusters:
1478 * @buffer: an #hb_buffer_t.
1479 *
1480 * Reverses buffer clusters. That is, the buffer contents are
1481 * reversed, then each cluster (consecutive items having the
1482 * same cluster number) are reversed again.
1483 *
1484 * Since: 0.9.2
1485 **/
1486void
1487hb_buffer_reverse_clusters (hb_buffer_t *buffer)
1488{
1489 buffer->reverse_clusters ();
1490}
1491
1492/**
1493 * hb_buffer_guess_segment_properties:
1494 * @buffer: an #hb_buffer_t.
1495 *
1496 * Sets unset buffer segment properties based on buffer Unicode
1497 * contents. If buffer is not empty, it must have content type
1498 * %HB_BUFFER_CONTENT_TYPE_UNICODE.
1499 *
1500 * If buffer script is not set (ie. is %HB_SCRIPT_INVALID), it
1501 * will be set to the Unicode script of the first character in
1502 * the buffer that has a script other than %HB_SCRIPT_COMMON,
1503 * %HB_SCRIPT_INHERITED, and %HB_SCRIPT_UNKNOWN.
1504 *
1505 * Next, if buffer direction is not set (ie. is %HB_DIRECTION_INVALID),
1506 * it will be set to the natural horizontal direction of the
1507 * buffer script as returned by hb_script_get_horizontal_direction().
1508 * If hb_script_get_horizontal_direction() returns %HB_DIRECTION_INVALID,
1509 * then %HB_DIRECTION_LTR is used.
1510 *
1511 * Finally, if buffer language is not set (ie. is %HB_LANGUAGE_INVALID),
1512 * it will be set to the process's default language as returned by
1513 * hb_language_get_default(). This may change in the future by
1514 * taking buffer script into consideration when choosing a language.
1515 * Note that hb_language_get_default() is NOT threadsafe the first time
1516 * it is called. See documentation for that function for details.
1517 *
1518 * Since: 0.9.7
1519 **/
1520void
1521hb_buffer_guess_segment_properties (hb_buffer_t *buffer)
1522{
1523 buffer->guess_segment_properties ();
1524}
1525
1526template <typename utf_t>
1527static inline void
1528hb_buffer_add_utf (hb_buffer_t *buffer,
1529 const typename utf_t::codepoint_t *text,
1530 int text_length,
1531 unsigned int item_offset,
1532 int item_length)
1533{
1534 typedef typename utf_t::codepoint_t T;
1535 const hb_codepoint_t replacement = buffer->replacement;
1536
1537 assert (buffer->content_type == HB_BUFFER_CONTENT_TYPE_UNICODE ||
1538 (!buffer->len && buffer->content_type == HB_BUFFER_CONTENT_TYPE_INVALID));
1539
1540 if (unlikely (hb_object_is_immutable (buffer)))
1541 return;
1542
1543 if (text_length == -1)
1544 text_length = utf_t::strlen (text);
1545
1546 if (item_length == -1)
1547 item_length = text_length - item_offset;
1548
1549 buffer->ensure (buffer->len + item_length * sizeof (T) / 4);
1550
1551 /* If buffer is empty and pre-context provided, install it.
1552 * This check is written this way, to make sure people can
1553 * provide pre-context in one add_utf() call, then provide
1554 * text in a follow-up call. See:
1555 *
1556 * https://bugzilla.mozilla.org/show_bug.cgi?id=801410#c13
1557 */
1558 if (!buffer->len && item_offset > 0)
1559 {
1560 /* Add pre-context */
1561 buffer->clear_context (0);
1562 const T *prev = text + item_offset;
1563 const T *start = text;
1564 while (start < prev && buffer->context_len[0] < buffer->CONTEXT_LENGTH)
1565 {
1566 hb_codepoint_t u;
1567 prev = utf_t::prev (prev, start, &u, replacement);
1568 buffer->context[0][buffer->context_len[0]++] = u;
1569 }
1570 }
1571
1572 const T *next = text + item_offset;
1573 const T *end = next + item_length;
1574 while (next < end)
1575 {
1576 hb_codepoint_t u;
1577 const T *old_next = next;
1578 next = utf_t::next (next, end, &u, replacement);
1579 buffer->add (u, old_next - (const T *) text);
1580 }
1581
1582 /* Add post-context */
1583 buffer->clear_context (1);
1584 end = text + text_length;
1585 while (next < end && buffer->context_len[1] < buffer->CONTEXT_LENGTH)
1586 {
1587 hb_codepoint_t u;
1588 next = utf_t::next (next, end, &u, replacement);
1589 buffer->context[1][buffer->context_len[1]++] = u;
1590 }
1591
1592 buffer->content_type = HB_BUFFER_CONTENT_TYPE_UNICODE;
1593}
1594
1595/**
1596 * hb_buffer_add_utf8:
1597 * @buffer: an #hb_buffer_t.
1598 * @text: (array length=text_length) (element-type uint8_t): an array of UTF-8
1599 * characters to append.
1600 * @text_length: the length of the @text, or -1 if it is %NULL terminated.
1601 * @item_offset: the offset of the first character to add to the @buffer.
1602 * @item_length: the number of characters to add to the @buffer, or -1 for the
1603 * end of @text (assuming it is %NULL terminated).
1604 *
1605 * See hb_buffer_add_codepoints().
1606 *
1607 * Replaces invalid UTF-8 characters with the @buffer replacement code point,
1608 * see hb_buffer_set_replacement_codepoint().
1609 *
1610 * Since: 0.9.2
1611 **/
1612void
1613hb_buffer_add_utf8 (hb_buffer_t *buffer,
1614 const char *text,
1615 int text_length,
1616 unsigned int item_offset,
1617 int item_length)
1618{
1619 hb_buffer_add_utf<hb_utf8_t> (buffer, (const uint8_t *) text, text_length, item_offset, item_length);
1620}
1621
1622/**
1623 * hb_buffer_add_utf16:
1624 * @buffer: an #hb_buffer_t.
1625 * @text: (array length=text_length): an array of UTF-16 characters to append.
1626 * @text_length: the length of the @text, or -1 if it is %NULL terminated.
1627 * @item_offset: the offset of the first character to add to the @buffer.
1628 * @item_length: the number of characters to add to the @buffer, or -1 for the
1629 * end of @text (assuming it is %NULL terminated).
1630 *
1631 * See hb_buffer_add_codepoints().
1632 *
1633 * Replaces invalid UTF-16 characters with the @buffer replacement code point,
1634 * see hb_buffer_set_replacement_codepoint().
1635 *
1636 * Since: 0.9.2
1637 **/
1638void
1639hb_buffer_add_utf16 (hb_buffer_t *buffer,
1640 const uint16_t *text,
1641 int text_length,
1642 unsigned int item_offset,
1643 int item_length)
1644{
1645 hb_buffer_add_utf<hb_utf16_t> (buffer, text, text_length, item_offset, item_length);
1646}
1647
1648/**
1649 * hb_buffer_add_utf32:
1650 * @buffer: an #hb_buffer_t.
1651 * @text: (array length=text_length): an array of UTF-32 characters to append.
1652 * @text_length: the length of the @text, or -1 if it is %NULL terminated.
1653 * @item_offset: the offset of the first character to add to the @buffer.
1654 * @item_length: the number of characters to add to the @buffer, or -1 for the
1655 * end of @text (assuming it is %NULL terminated).
1656 *
1657 * See hb_buffer_add_codepoints().
1658 *
1659 * Replaces invalid UTF-32 characters with the @buffer replacement code point,
1660 * see hb_buffer_set_replacement_codepoint().
1661 *
1662 * Since: 0.9.2
1663 **/
1664void
1665hb_buffer_add_utf32 (hb_buffer_t *buffer,
1666 const uint32_t *text,
1667 int text_length,
1668 unsigned int item_offset,
1669 int item_length)
1670{
1671 hb_buffer_add_utf<hb_utf32_t> (buffer, text, text_length, item_offset, item_length);
1672}
1673
1674/**
1675 * hb_buffer_add_latin1:
1676 * @buffer: an #hb_buffer_t.
1677 * @text: (array length=text_length) (element-type uint8_t): an array of UTF-8
1678 * characters to append.
1679 * @text_length: the length of the @text, or -1 if it is %NULL terminated.
1680 * @item_offset: the offset of the first character to add to the @buffer.
1681 * @item_length: the number of characters to add to the @buffer, or -1 for the
1682 * end of @text (assuming it is %NULL terminated).
1683 *
1684 * Similar to hb_buffer_add_codepoints(), but allows only access to first 256
1685 * Unicode code points that can fit in 8-bit strings.
1686 *
1687 * <note>Has nothing to do with non-Unicode Latin-1 encoding.</note>
1688 *
1689 * Since: 0.9.39
1690 **/
1691void
1692hb_buffer_add_latin1 (hb_buffer_t *buffer,
1693 const uint8_t *text,
1694 int text_length,
1695 unsigned int item_offset,
1696 int item_length)
1697{
1698 hb_buffer_add_utf<hb_latin1_t> (buffer, text, text_length, item_offset, item_length);
1699}
1700
1701/**
1702 * hb_buffer_add_codepoints:
1703 * @buffer: a #hb_buffer_t to append characters to.
1704 * @text: (array length=text_length): an array of Unicode code points to append.
1705 * @text_length: the length of the @text, or -1 if it is %NULL terminated.
1706 * @item_offset: the offset of the first code point to add to the @buffer.
1707 * @item_length: the number of code points to add to the @buffer, or -1 for the
1708 * end of @text (assuming it is %NULL terminated).
1709 *
1710 * Appends characters from @text array to @buffer. The @item_offset is the
1711 * position of the first character from @text that will be appended, and
1712 * @item_length is the number of character. When shaping part of a larger text
1713 * (e.g. a run of text from a paragraph), instead of passing just the substring
1714 * corresponding to the run, it is preferable to pass the whole
1715 * paragraph and specify the run start and length as @item_offset and
1716 * @item_length, respectively, to give HarfBuzz the full context to be able,
1717 * for example, to do cross-run Arabic shaping or properly handle combining
1718 * marks at stat of run.
1719 *
1720 * This function does not check the validity of @text, it is up to the caller
1721 * to ensure it contains a valid Unicode code points.
1722 *
1723 * Since: 0.9.31
1724 **/
1725void
1726hb_buffer_add_codepoints (hb_buffer_t *buffer,
1727 const hb_codepoint_t *text,
1728 int text_length,
1729 unsigned int item_offset,
1730 int item_length)
1731{
1732 hb_buffer_add_utf<hb_utf32_novalidate_t> (buffer, text, text_length, item_offset, item_length);
1733}
1734
1735
1736/**
1737 * hb_buffer_append:
1738 * @buffer: an #hb_buffer_t.
1739 * @source: source #hb_buffer_t.
1740 * @start: start index into source buffer to copy. Use 0 to copy from start of buffer.
1741 * @end: end index into source buffer to copy. Use (unsigned int) -1 to copy to end of buffer.
1742 *
1743 * Append (part of) contents of another buffer to this buffer.
1744 *
1745 * Since: 1.5.0
1746 **/
1747HB_EXTERN void
1748hb_buffer_append (hb_buffer_t *buffer,
1749 hb_buffer_t *source,
1750 unsigned int start,
1751 unsigned int end)
1752{
1753 assert (!buffer->have_output && !source->have_output);
1754 assert (buffer->have_positions == source->have_positions ||
1755 !buffer->len || !source->len);
1756 assert (buffer->content_type == source->content_type ||
1757 !buffer->len || !source->len);
1758
1759 if (end > source->len)
1760 end = source->len;
1761 if (start > end)
1762 start = end;
1763 if (start == end)
1764 return;
1765
1766 if (!buffer->len)
1767 buffer->content_type = source->content_type;
1768 if (!buffer->have_positions && source->have_positions)
1769 buffer->clear_positions ();
1770
1771 if (buffer->len + (end - start) < buffer->len) /* Overflows. */
1772 {
1773 buffer->successful = false;
1774 return;
1775 }
1776
1777 unsigned int orig_len = buffer->len;
1778 hb_buffer_set_length (buffer, buffer->len + (end - start));
1779 if (unlikely (!buffer->successful))
1780 return;
1781
1782 memcpy (buffer->info + orig_len, source->info + start, (end - start) * sizeof (buffer->info[0]));
1783 if (buffer->have_positions)
1784 memcpy (buffer->pos + orig_len, source->pos + start, (end - start) * sizeof (buffer->pos[0]));
1785}
1786
1787
1788static int
1789compare_info_codepoint (const hb_glyph_info_t *pa,
1790 const hb_glyph_info_t *pb)
1791{
1792 return (int) pb->codepoint - (int) pa->codepoint;
1793}
1794
1795static inline void
1796normalize_glyphs_cluster (hb_buffer_t *buffer,
1797 unsigned int start,
1798 unsigned int end,
1799 bool backward)
1800{
1801 hb_glyph_position_t *pos = buffer->pos;
1802
1803 /* Total cluster advance */
1804 hb_position_t total_x_advance = 0, total_y_advance = 0;
1805 for (unsigned int i = start; i < end; i++)
1806 {
1807 total_x_advance += pos[i].x_advance;
1808 total_y_advance += pos[i].y_advance;
1809 }
1810
1811 hb_position_t x_advance = 0, y_advance = 0;
1812 for (unsigned int i = start; i < end; i++)
1813 {
1814 pos[i].x_offset += x_advance;
1815 pos[i].y_offset += y_advance;
1816
1817 x_advance += pos[i].x_advance;
1818 y_advance += pos[i].y_advance;
1819
1820 pos[i].x_advance = 0;
1821 pos[i].y_advance = 0;
1822 }
1823
1824 if (backward)
1825 {
1826 /* Transfer all cluster advance to the last glyph. */
1827 pos[end - 1].x_advance = total_x_advance;
1828 pos[end - 1].y_advance = total_y_advance;
1829
1830 hb_stable_sort (buffer->info + start, end - start - 1, compare_info_codepoint, buffer->pos + start);
1831 } else {
1832 /* Transfer all cluster advance to the first glyph. */
1833 pos[start].x_advance += total_x_advance;
1834 pos[start].y_advance += total_y_advance;
1835 for (unsigned int i = start + 1; i < end; i++) {
1836 pos[i].x_offset -= total_x_advance;
1837 pos[i].y_offset -= total_y_advance;
1838 }
1839 hb_stable_sort (buffer->info + start + 1, end - start - 1, compare_info_codepoint, buffer->pos + start + 1);
1840 }
1841}
1842
1843/**
1844 * hb_buffer_normalize_glyphs:
1845 * @buffer: an #hb_buffer_t.
1846 *
1847 * Reorders a glyph buffer to have canonical in-cluster glyph order / position.
1848 * The resulting clusters should behave identical to pre-reordering clusters.
1849 *
1850 * <note>This has nothing to do with Unicode normalization.</note>
1851 *
1852 * Since: 0.9.2
1853 **/
1854void
1855hb_buffer_normalize_glyphs (hb_buffer_t *buffer)
1856{
1857 assert (buffer->have_positions);
1858 assert (buffer->content_type == HB_BUFFER_CONTENT_TYPE_GLYPHS ||
1859 (!buffer->len && buffer->content_type == HB_BUFFER_CONTENT_TYPE_INVALID));
1860
1861 bool backward = HB_DIRECTION_IS_BACKWARD (buffer->props.direction);
1862
1863 unsigned int count = buffer->len;
1864 if (unlikely (!count)) return;
1865 hb_glyph_info_t *info = buffer->info;
1866
1867 unsigned int start = 0;
1868 unsigned int end;
1869 for (end = start + 1; end < count; end++)
1870 if (info[start].cluster != info[end].cluster) {
1871 normalize_glyphs_cluster (buffer, start, end, backward);
1872 start = end;
1873 }
1874 normalize_glyphs_cluster (buffer, start, end, backward);
1875}
1876
1877void
1878hb_buffer_t::sort (unsigned int start, unsigned int end, int(*compar)(const hb_glyph_info_t *, const hb_glyph_info_t *))
1879{
1880 assert (!have_positions);
1881 for (unsigned int i = start + 1; i < end; i++)
1882 {
1883 unsigned int j = i;
1884 while (j > start && compar (&info[j - 1], &info[i]) > 0)
1885 j--;
1886 if (i == j)
1887 continue;
1888 /* Move item i to occupy place for item j, shift what's in between. */
1889 merge_clusters (j, i + 1);
1890 {
1891 hb_glyph_info_t t = info[i];
1892 memmove (&info[j + 1], &info[j], (i - j) * sizeof (hb_glyph_info_t));
1893 info[j] = t;
1894 }
1895 }
1896}
1897
1898
1899/*
1900 * Comparing buffers.
1901 */
1902
1903/**
1904 * hb_buffer_diff:
1905 * @buffer: a buffer.
1906 * @reference: other buffer to compare to.
1907 * @dottedcircle_glyph: glyph id of U+25CC DOTTED CIRCLE, or (hb_codepont_t) -1.
1908 * @position_fuzz: allowed absolute difference in position values.
1909 *
1910 * If dottedcircle_glyph is (hb_codepoint_t) -1 then %HB_BUFFER_DIFF_FLAG_DOTTED_CIRCLE_PRESENT
1911 * and %HB_BUFFER_DIFF_FLAG_NOTDEF_PRESENT are never returned. This should be used by most
1912 * callers if just comparing two buffers is needed.
1913 *
1914 * Since: 1.5.0
1915 **/
1916hb_buffer_diff_flags_t
1917hb_buffer_diff (hb_buffer_t *buffer,
1918 hb_buffer_t *reference,
1919 hb_codepoint_t dottedcircle_glyph,
1920 unsigned int position_fuzz)
1921{
1922 if (buffer->content_type != reference->content_type && buffer->len && reference->len)
1923 return HB_BUFFER_DIFF_FLAG_CONTENT_TYPE_MISMATCH;
1924
1925 hb_buffer_diff_flags_t result = HB_BUFFER_DIFF_FLAG_EQUAL;
1926 bool contains = dottedcircle_glyph != (hb_codepoint_t) -1;
1927
1928 unsigned int count = reference->len;
1929
1930 if (buffer->len != count)
1931 {
1932 /*
1933 * we can't compare glyph-by-glyph, but we do want to know if there
1934 * are .notdef or dottedcircle glyphs present in the reference buffer
1935 */
1936 const hb_glyph_info_t *info = reference->info;
1937 unsigned int i;
1938 for (i = 0; i < count; i++)
1939 {
1940 if (contains && info[i].codepoint == dottedcircle_glyph)
1941 result |= HB_BUFFER_DIFF_FLAG_DOTTED_CIRCLE_PRESENT;
1942 if (contains && info[i].codepoint == 0)
1943 result |= HB_BUFFER_DIFF_FLAG_NOTDEF_PRESENT;
1944 }
1945 result |= HB_BUFFER_DIFF_FLAG_LENGTH_MISMATCH;
1946 return hb_buffer_diff_flags_t (result);
1947 }
1948
1949 if (!count)
1950 return hb_buffer_diff_flags_t (result);
1951
1952 const hb_glyph_info_t *buf_info = buffer->info;
1953 const hb_glyph_info_t *ref_info = reference->info;
1954 for (unsigned int i = 0; i < count; i++)
1955 {
1956 if (buf_info->codepoint != ref_info->codepoint)
1957 result |= HB_BUFFER_DIFF_FLAG_CODEPOINT_MISMATCH;
1958 if (buf_info->cluster != ref_info->cluster)
1959 result |= HB_BUFFER_DIFF_FLAG_CLUSTER_MISMATCH;
1960 if ((buf_info->mask & ~ref_info->mask & HB_GLYPH_FLAG_DEFINED))
1961 result |= HB_BUFFER_DIFF_FLAG_GLYPH_FLAGS_MISMATCH;
1962 if (contains && ref_info->codepoint == dottedcircle_glyph)
1963 result |= HB_BUFFER_DIFF_FLAG_DOTTED_CIRCLE_PRESENT;
1964 if (contains && ref_info->codepoint == 0)
1965 result |= HB_BUFFER_DIFF_FLAG_NOTDEF_PRESENT;
1966 buf_info++;
1967 ref_info++;
1968 }
1969
1970 if (buffer->content_type == HB_BUFFER_CONTENT_TYPE_GLYPHS)
1971 {
1972 assert (buffer->have_positions);
1973 const hb_glyph_position_t *buf_pos = buffer->pos;
1974 const hb_glyph_position_t *ref_pos = reference->pos;
1975 for (unsigned int i = 0; i < count; i++)
1976 {
1977 if ((unsigned int) abs (buf_pos->x_advance - ref_pos->x_advance) > position_fuzz ||
1978 (unsigned int) abs (buf_pos->y_advance - ref_pos->y_advance) > position_fuzz ||
1979 (unsigned int) abs (buf_pos->x_offset - ref_pos->x_offset) > position_fuzz ||
1980 (unsigned int) abs (buf_pos->y_offset - ref_pos->y_offset) > position_fuzz)
1981 {
1982 result |= HB_BUFFER_DIFF_FLAG_POSITION_MISMATCH;
1983 break;
1984 }
1985 buf_pos++;
1986 ref_pos++;
1987 }
1988 }
1989
1990 return result;
1991}
1992
1993
1994/*
1995 * Debugging.
1996 */
1997
1998#ifndef HB_NO_BUFFER_MESSAGE
1999/**
2000 * hb_buffer_set_message_func:
2001 * @buffer: an #hb_buffer_t.
2002 * @func: (closure user_data) (destroy destroy) (scope notified):
2003 * @user_data:
2004 * @destroy:
2005 *
2006 *
2007 *
2008 * Since: 1.1.3
2009 **/
2010void
2011hb_buffer_set_message_func (hb_buffer_t *buffer,
2012 hb_buffer_message_func_t func,
2013 void *user_data, hb_destroy_func_t destroy)
2014{
2015 if (buffer->message_destroy)
2016 buffer->message_destroy (buffer->message_data);
2017
2018 if (func) {
2019 buffer->message_func = func;
2020 buffer->message_data = user_data;
2021 buffer->message_destroy = destroy;
2022 } else {
2023 buffer->message_func = nullptr;
2024 buffer->message_data = nullptr;
2025 buffer->message_destroy = nullptr;
2026 }
2027}
2028bool
2029hb_buffer_t::message_impl (hb_font_t *font, const char *fmt, va_list ap)
2030{
2031 char buf[100];
2032 vsnprintf (buf, sizeof (buf), fmt, ap);
2033 return (bool) this->message_func (this, font, buf, this->message_data);
2034}
2035#endif
2036