2 * Copyright © 2022 Behdad Esfahbod
4 * This is part of HarfBuzz, a text shaping library.
6 * Permission is hereby granted, without written agreement and without
7 * license or royalty fees, to use, copy, modify, and distribute this
8 * software and its documentation for any purpose, provided that the
9 * above copyright notice and the following two paragraphs appear in
10 * all copies of this software.
12 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
13 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
14 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
15 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
18 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
19 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20 * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
21 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
22 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
24 * Google Author(s): Behdad Esfahbod
29 #ifndef HB_NO_BUFFER_VERIFY
31 #include "hb-buffer.hh"
34 #define BUFFER_VERIFY_ERROR "buffer verify error: "
36 buffer_verify_error (hb_buffer_t *buffer,
39 ...) HB_PRINTF_FUNC(3, 4);
42 buffer_verify_error (hb_buffer_t *buffer,
49 if (buffer->messaging ())
51 buffer->message_impl (font, fmt, ap);
55 fprintf (stderr, "harfbuzz ");
56 vfprintf (stderr, fmt, ap);
57 fprintf (stderr, "\n");
63 buffer_verify_monotone (hb_buffer_t *buffer,
66 /* Check that clusters are monotone. */
67 if (buffer->cluster_level == HB_BUFFER_CLUSTER_LEVEL_MONOTONE_GRAPHEMES ||
68 buffer->cluster_level == HB_BUFFER_CLUSTER_LEVEL_MONOTONE_CHARACTERS)
70 bool is_forward = HB_DIRECTION_IS_FORWARD (hb_buffer_get_direction (buffer));
72 unsigned int num_glyphs;
73 hb_glyph_info_t *info = hb_buffer_get_glyph_infos (buffer, &num_glyphs);
75 for (unsigned int i = 1; i < num_glyphs; i++)
76 if (info[i-1].cluster != info[i].cluster &&
77 (info[i-1].cluster < info[i].cluster) != is_forward)
79 buffer_verify_error (buffer, font, BUFFER_VERIFY_ERROR "clusters are not monotone.");
88 buffer_verify_unsafe_to_break (hb_buffer_t *buffer,
89 hb_buffer_t *text_buffer,
91 const hb_feature_t *features,
92 unsigned int num_features,
93 const char * const *shapers)
95 if (buffer->cluster_level != HB_BUFFER_CLUSTER_LEVEL_MONOTONE_GRAPHEMES &&
96 buffer->cluster_level != HB_BUFFER_CLUSTER_LEVEL_MONOTONE_CHARACTERS)
98 /* Cannot perform this check without monotone clusters. */
102 /* Check that breaking up shaping at safe-to-break is indeed safe. */
104 hb_buffer_t *fragment = hb_buffer_create_similar (buffer);
105 hb_buffer_set_flags (fragment, hb_buffer_get_flags (fragment) & ~HB_BUFFER_FLAG_VERIFY);
106 hb_buffer_t *reconstruction = hb_buffer_create_similar (buffer);
107 hb_buffer_set_flags (reconstruction, hb_buffer_get_flags (reconstruction) & ~HB_BUFFER_FLAG_VERIFY);
109 unsigned int num_glyphs;
110 hb_glyph_info_t *info = hb_buffer_get_glyph_infos (buffer, &num_glyphs);
112 unsigned int num_chars;
113 hb_glyph_info_t *text = hb_buffer_get_glyph_infos (text_buffer, &num_chars);
115 /* Chop text and shape fragments. */
116 bool forward = HB_DIRECTION_IS_FORWARD (hb_buffer_get_direction (buffer));
117 unsigned int start = 0;
118 unsigned int text_start = forward ? 0 : num_chars;
119 unsigned int text_end = text_start;
120 for (unsigned int end = 1; end < num_glyphs + 1; end++)
122 if (end < num_glyphs &&
123 (info[end].cluster == info[end-1].cluster ||
124 info[end-(forward?0:1)].mask & HB_GLYPH_FLAG_UNSAFE_TO_BREAK))
127 /* Shape segment corresponding to glyphs start..end. */
128 if (end == num_glyphs)
131 text_end = num_chars;
139 unsigned int cluster = info[end].cluster;
140 while (text_end < num_chars && text[text_end].cluster < cluster)
145 unsigned int cluster = info[end - 1].cluster;
146 while (text_start && text[text_start - 1].cluster >= cluster)
150 assert (text_start < text_end);
153 printf("start %d end %d text start %d end %d\n", start, end, text_start, text_end);
155 hb_buffer_clear_contents (fragment);
157 hb_buffer_flags_t flags = hb_buffer_get_flags (fragment);
159 flags = (hb_buffer_flags_t) (flags & ~HB_BUFFER_FLAG_BOT);
160 if (text_end < num_chars)
161 flags = (hb_buffer_flags_t) (flags & ~HB_BUFFER_FLAG_EOT);
162 hb_buffer_set_flags (fragment, flags);
164 hb_buffer_append (fragment, text_buffer, text_start, text_end);
165 if (!hb_shape_full (font, fragment, features, num_features, shapers))
167 buffer_verify_error (buffer, font, BUFFER_VERIFY_ERROR "shaping failed while shaping fragment.");
168 hb_buffer_destroy (reconstruction);
169 hb_buffer_destroy (fragment);
172 hb_buffer_append (reconstruction, fragment, 0, -1);
176 text_start = text_end;
178 text_end = text_start;
182 hb_buffer_diff_flags_t diff = hb_buffer_diff (reconstruction, buffer, (hb_codepoint_t) -1, 0);
185 buffer_verify_error (buffer, font, BUFFER_VERIFY_ERROR "unsafe-to-break test failed.");
188 /* Return the reconstructed result instead so it can be inspected. */
189 hb_buffer_set_length (buffer, 0);
190 hb_buffer_append (buffer, reconstruction, 0, -1);
193 hb_buffer_destroy (reconstruction);
194 hb_buffer_destroy (fragment);
200 buffer_verify_unsafe_to_concat (hb_buffer_t *buffer,
201 hb_buffer_t *text_buffer,
203 const hb_feature_t *features,
204 unsigned int num_features,
205 const char * const *shapers)
207 if (buffer->cluster_level != HB_BUFFER_CLUSTER_LEVEL_MONOTONE_GRAPHEMES &&
208 buffer->cluster_level != HB_BUFFER_CLUSTER_LEVEL_MONOTONE_CHARACTERS)
210 /* Cannot perform this check without monotone clusters. */
214 /* Check that shuffling up text before shaping at safe-to-concat points
217 /* This is what we do:
219 * 1. We shape text once. Then segment the text at all the safe-to-concat
222 * 2. Then we create two buffers, one containing all the even segments and
223 * one all the odd segments.
225 * 3. Because all these segments were safe-to-concat at both ends, we
226 * expect that concatenating them and shaping should NOT change the
227 * shaping results of each segment. As such, we expect that after
228 * shaping the two buffers, we still get cluster boundaries at the
229 * segment boundaries, and that those all are safe-to-concat points.
230 * Moreover, that there are NOT any safe-to-concat points within the
233 * 4. Finally, we reconstruct the shaping results of the original text by
234 * simply interleaving the shaping results of the segments from the two
235 * buffers, and assert that the total shaping results is the same as
236 * the one from original buffer in step 1.
239 hb_buffer_t *fragments[2] {hb_buffer_create_similar (buffer),
240 hb_buffer_create_similar (buffer)};
241 hb_buffer_set_flags (fragments[0], hb_buffer_get_flags (fragments[0]) & ~HB_BUFFER_FLAG_VERIFY);
242 hb_buffer_set_flags (fragments[1], hb_buffer_get_flags (fragments[1]) & ~HB_BUFFER_FLAG_VERIFY);
243 hb_buffer_t *reconstruction = hb_buffer_create_similar (buffer);
244 hb_buffer_set_flags (reconstruction, hb_buffer_get_flags (reconstruction) & ~HB_BUFFER_FLAG_VERIFY);
245 hb_segment_properties_t props;
246 hb_buffer_get_segment_properties (buffer, &props);
247 hb_buffer_set_segment_properties (fragments[0], &props);
248 hb_buffer_set_segment_properties (fragments[1], &props);
249 hb_buffer_set_segment_properties (reconstruction, &props);
252 hb_glyph_info_t *info = hb_buffer_get_glyph_infos (buffer, &num_glyphs);
255 hb_glyph_info_t *text = hb_buffer_get_glyph_infos (text_buffer, &num_chars);
257 bool forward = HB_DIRECTION_IS_FORWARD (hb_buffer_get_direction (buffer));
260 hb_buffer_reverse (buffer);
263 * Split text into segments and collect into to fragment streams.
266 unsigned fragment_idx = 0;
268 unsigned text_start = 0;
269 unsigned text_end = 0;
270 for (unsigned end = 1; end < num_glyphs + 1; end++)
272 if (end < num_glyphs &&
273 (info[end].cluster == info[end-1].cluster ||
274 info[end].mask & HB_GLYPH_FLAG_UNSAFE_TO_CONCAT))
277 /* Accumulate segment corresponding to glyphs start..end. */
278 if (end == num_glyphs)
279 text_end = num_chars;
282 unsigned cluster = info[end].cluster;
283 while (text_end < num_chars && text[text_end].cluster < cluster)
286 assert (text_start < text_end);
289 printf("start %d end %d text start %d end %d\n", start, end, text_start, text_end);
292 hb_buffer_flags_t flags = hb_buffer_get_flags (fragment);
294 flags = (hb_buffer_flags_t) (flags & ~HB_BUFFER_FLAG_BOT);
295 if (text_end < num_chars)
296 flags = (hb_buffer_flags_t) (flags & ~HB_BUFFER_FLAG_EOT);
297 hb_buffer_set_flags (fragment, flags);
300 hb_buffer_append (fragments[fragment_idx], text_buffer, text_start, text_end);
303 text_start = text_end;
304 fragment_idx = 1 - fragment_idx;
309 hb_buffer_diff_flags_t diff;
312 * Shape the two fragment streams.
314 if (!hb_shape_full (font, fragments[0], features, num_features, shapers))
316 buffer_verify_error (buffer, font, BUFFER_VERIFY_ERROR "shaping failed while shaping fragment.");
320 if (!hb_shape_full (font, fragments[1], features, num_features, shapers))
322 buffer_verify_error (buffer, font, BUFFER_VERIFY_ERROR "shaping failed while shaping fragment.");
329 hb_buffer_reverse (fragments[0]);
330 hb_buffer_reverse (fragments[1]);
334 * Reconstruct results.
337 unsigned fragment_idx = 0;
338 unsigned fragment_start[2] {0, 0};
339 unsigned fragment_num_glyphs[2];
340 hb_glyph_info_t *fragment_info[2];
341 for (unsigned i = 0; i < 2; i++)
342 fragment_info[i] = hb_buffer_get_glyph_infos (fragments[i], &fragment_num_glyphs[i]);
343 while (fragment_start[0] < fragment_num_glyphs[0] ||
344 fragment_start[1] < fragment_num_glyphs[1])
346 unsigned fragment_end = fragment_start[fragment_idx] + 1;
347 while (fragment_end < fragment_num_glyphs[fragment_idx] &&
348 (fragment_info[fragment_idx][fragment_end].cluster == fragment_info[fragment_idx][fragment_end - 1].cluster ||
349 fragment_info[fragment_idx][fragment_end].mask & HB_GLYPH_FLAG_UNSAFE_TO_CONCAT))
352 hb_buffer_append (reconstruction, fragments[fragment_idx], fragment_start[fragment_idx], fragment_end);
354 fragment_start[fragment_idx] = fragment_end;
355 fragment_idx = 1 - fragment_idx;
361 hb_buffer_reverse (buffer);
362 hb_buffer_reverse (reconstruction);
368 diff = hb_buffer_diff (reconstruction, buffer, (hb_codepoint_t) -1, 0);
371 buffer_verify_error (buffer, font, BUFFER_VERIFY_ERROR "unsafe-to-concat test failed.");
374 /* Return the reconstructed result instead so it can be inspected. */
375 hb_buffer_set_length (buffer, 0);
376 hb_buffer_append (buffer, reconstruction, 0, -1);
381 hb_buffer_destroy (reconstruction);
382 hb_buffer_destroy (fragments[0]);
383 hb_buffer_destroy (fragments[1]);
389 hb_buffer_t::verify (hb_buffer_t *text_buffer,
391 const hb_feature_t *features,
392 unsigned int num_features,
393 const char * const *shapers)
396 if (!buffer_verify_monotone (this, font))
398 if (!buffer_verify_unsafe_to_break (this, text_buffer, font, features, num_features, shapers))
400 if (!buffer_verify_unsafe_to_concat (this, text_buffer, font, features, num_features, shapers))
404 unsigned len = text_buffer->len;
405 hb_vector_t<char> bytes;
406 if (likely (bytes.resize (len * 10 + 16)))
408 hb_buffer_serialize_unicode (text_buffer,
410 bytes.arrayZ, bytes.length,
412 HB_BUFFER_SERIALIZE_FORMAT_TEXT,
413 HB_BUFFER_SERIALIZE_FLAG_NO_CLUSTERS);
414 buffer_verify_error (this, font, BUFFER_VERIFY_ERROR "text was: %s.", bytes.arrayZ);