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