Branch
Hash :
b51de936
Author :
Date :
2025-08-20T23:15:46
Remove duplicate buffer->leave call (#5487) * Remove duplicate buffer->leave call hb-shape.cc does it for us. * Fail shaping if max len reached * Increase buffer limits https://github.com/harfbuzz/harfrust/issues/107 * Remove shaping_failed; fail in buffer.successful
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
/*
* Copyright © 2022 Behdad Esfahbod
*
* This is part of HarfBuzz, a text shaping library.
*
* Permission is hereby granted, without written agreement and without
* license or royalty fees, to use, copy, modify, and distribute this
* software and its documentation for any purpose, provided that the
* above copyright notice and the following two paragraphs appear in
* all copies of this software.
*
* IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
* DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
* ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
* IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*
* THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
* BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
* ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
* PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
*
* Google Author(s): Behdad Esfahbod
*/
#include "hb.hh"
#ifndef HB_NO_BUFFER_VERIFY
#include "hb-buffer.hh"
#define BUFFER_VERIFY_ERROR "buffer verify error: "
static inline void
buffer_verify_error (hb_buffer_t *buffer,
hb_font_t *font,
const char *fmt,
...) HB_PRINTF_FUNC(3, 4);
static inline void
buffer_verify_error (hb_buffer_t *buffer,
hb_font_t *font,
const char *fmt,
...)
{
va_list ap;
va_start (ap, fmt);
if (buffer->messaging ())
{
buffer->message_impl (font, fmt, ap);
}
else
{
fprintf (stderr, "harfbuzz ");
vfprintf (stderr, fmt, ap);
fprintf (stderr, "\n");
}
va_end (ap);
}
static bool
buffer_verify_monotone (hb_buffer_t *buffer,
hb_font_t *font)
{
if (!HB_BUFFER_CLUSTER_LEVEL_IS_MONOTONE (buffer->cluster_level))
{
/* Cannot perform this check without monotone clusters. */
return true;
}
bool is_forward = HB_DIRECTION_IS_FORWARD (hb_buffer_get_direction (buffer));
unsigned int num_glyphs;
hb_glyph_info_t *info = hb_buffer_get_glyph_infos (buffer, &num_glyphs);
for (unsigned int i = 1; i < num_glyphs; i++)
if (info[i-1].cluster != info[i].cluster &&
(info[i-1].cluster < info[i].cluster) != is_forward)
{
buffer_verify_error (buffer, font, BUFFER_VERIFY_ERROR "clusters are not monotone.");
return false;
}
return true;
}
static bool
buffer_verify_unsafe_to_break (hb_buffer_t *buffer,
hb_buffer_t *text_buffer,
hb_font_t *font,
const hb_feature_t *features,
unsigned int num_features,
const char * const *shapers)
{
if (!HB_BUFFER_CLUSTER_LEVEL_IS_MONOTONE (buffer->cluster_level))
{
/* Cannot perform this check without monotone clusters. */
return true;
}
/* Check that breaking up shaping at safe-to-break is indeed safe. */
hb_buffer_t *fragment = hb_buffer_create_similar (buffer);
hb_buffer_set_flags (fragment, (hb_buffer_flags_t (hb_buffer_get_flags (fragment) & ~HB_BUFFER_FLAG_VERIFY)));
hb_buffer_t *reconstruction = hb_buffer_create_similar (buffer);
hb_buffer_set_flags (reconstruction, (hb_buffer_flags_t (hb_buffer_get_flags (reconstruction) & ~HB_BUFFER_FLAG_VERIFY)));
unsigned int num_glyphs;
hb_glyph_info_t *info = hb_buffer_get_glyph_infos (buffer, &num_glyphs);
unsigned int num_chars;
hb_glyph_info_t *text = hb_buffer_get_glyph_infos (text_buffer, &num_chars);
/* Chop text and shape fragments. */
bool forward = HB_DIRECTION_IS_FORWARD (hb_buffer_get_direction (buffer));
unsigned int start = 0;
unsigned int text_start = forward ? 0 : num_chars;
unsigned int text_end = text_start;
for (unsigned int end = 1; end < num_glyphs + 1; end++)
{
if (end < num_glyphs &&
(info[end].cluster == info[end-1].cluster ||
info[end-(forward?0:1)].mask & HB_GLYPH_FLAG_UNSAFE_TO_BREAK))
continue;
/* Shape segment corresponding to glyphs start..end. */
if (end == num_glyphs)
{
if (forward)
text_end = num_chars;
else
text_start = 0;
}
else
{
if (forward)
{
unsigned int cluster = info[end].cluster;
while (text_end < num_chars && text[text_end].cluster < cluster)
text_end++;
}
else
{
unsigned int cluster = info[end - 1].cluster;
while (text_start && text[text_start - 1].cluster >= cluster)
text_start--;
}
}
assert (text_start < text_end);
if (false)
printf("start %u end %u text start %u end %u\n", start, end, text_start, text_end);
hb_buffer_clear_contents (fragment);
hb_buffer_flags_t flags = hb_buffer_get_flags (fragment);
if (0 < text_start)
flags = (hb_buffer_flags_t) (flags & ~HB_BUFFER_FLAG_BOT);
if (text_end < num_chars)
flags = (hb_buffer_flags_t) (flags & ~HB_BUFFER_FLAG_EOT);
hb_buffer_set_flags (fragment, flags);
hb_buffer_append (fragment, text_buffer, text_start, text_end);
if (!hb_shape_full (font, fragment, features, num_features, shapers) ||
fragment->successful)
{
hb_buffer_destroy (reconstruction);
hb_buffer_destroy (fragment);
return true;
}
hb_buffer_append (reconstruction, fragment, 0, -1);
start = end;
if (forward)
text_start = text_end;
else
text_end = text_start;
}
bool ret = true;
if (likely (reconstruction->successful))
{
hb_buffer_diff_flags_t diff = hb_buffer_diff (reconstruction, buffer, (hb_codepoint_t) -1, 0);
if (diff & ~HB_BUFFER_DIFF_FLAG_GLYPH_FLAGS_MISMATCH)
{
buffer_verify_error (buffer, font, BUFFER_VERIFY_ERROR "unsafe-to-break test failed.");
ret = false;
/* Return the reconstructed result instead so it can be inspected. */
hb_buffer_set_length (buffer, 0);
hb_buffer_append (buffer, reconstruction, 0, -1);
}
}
hb_buffer_destroy (reconstruction);
hb_buffer_destroy (fragment);
return ret;
}
static bool
buffer_verify_unsafe_to_concat (hb_buffer_t *buffer,
hb_buffer_t *text_buffer,
hb_font_t *font,
const hb_feature_t *features,
unsigned int num_features,
const char * const *shapers)
{
if (!HB_BUFFER_CLUSTER_LEVEL_IS_MONOTONE (buffer->cluster_level))
{
/* Cannot perform this check without monotone clusters. */
return true;
}
/* Check that shuffling up text before shaping at safe-to-concat points
* is indeed safe. */
/* This is what we do:
*
* 1. We shape text once. Then segment the text at all the safe-to-concat
* points;
*
* 2. Then we create two buffers, one containing all the even segments and
* one all the odd segments.
*
* 3. Because all these segments were safe-to-concat at both ends, we
* expect that concatenating them and shaping should NOT change the
* shaping results of each segment. As such, we expect that after
* shaping the two buffers, we still get cluster boundaries at the
* segment boundaries, and that those all are safe-to-concat points.
* Moreover, that there are NOT any safe-to-concat points within the
* segments.
*
* 4. Finally, we reconstruct the shaping results of the original text by
* simply interleaving the shaping results of the segments from the two
* buffers, and assert that the total shaping results is the same as
* the one from original buffer in step 1.
*/
hb_buffer_t *fragments[2] {hb_buffer_create_similar (buffer),
hb_buffer_create_similar (buffer)};
hb_buffer_set_flags (fragments[0], (hb_buffer_flags_t (hb_buffer_get_flags (fragments[0]) & ~HB_BUFFER_FLAG_VERIFY)));
hb_buffer_set_flags (fragments[1], (hb_buffer_flags_t (hb_buffer_get_flags (fragments[1]) & ~HB_BUFFER_FLAG_VERIFY)));
hb_buffer_t *reconstruction = hb_buffer_create_similar (buffer);
hb_buffer_set_flags (reconstruction, (hb_buffer_flags_t (hb_buffer_get_flags (reconstruction) & ~HB_BUFFER_FLAG_VERIFY)));
hb_segment_properties_t props;
hb_buffer_get_segment_properties (buffer, &props);
hb_buffer_set_segment_properties (fragments[0], &props);
hb_buffer_set_segment_properties (fragments[1], &props);
hb_buffer_set_segment_properties (reconstruction, &props);
unsigned num_glyphs;
hb_glyph_info_t *info = hb_buffer_get_glyph_infos (buffer, &num_glyphs);
unsigned num_chars;
hb_glyph_info_t *text = hb_buffer_get_glyph_infos (text_buffer, &num_chars);
bool forward = HB_DIRECTION_IS_FORWARD (hb_buffer_get_direction (buffer));
if (!forward)
hb_buffer_reverse (buffer);
/*
* Split text into segments and collect into to fragment streams.
*/
{
unsigned fragment_idx = 0;
unsigned start = 0;
unsigned text_start = 0;
unsigned text_end = 0;
for (unsigned end = 1; end < num_glyphs + 1; end++)
{
if (end < num_glyphs &&
(info[end].cluster == info[end-1].cluster ||
info[end].mask & HB_GLYPH_FLAG_UNSAFE_TO_CONCAT))
continue;
/* Accumulate segment corresponding to glyphs start..end. */
if (end == num_glyphs)
text_end = num_chars;
else
{
unsigned cluster = info[end].cluster;
while (text_end < num_chars && text[text_end].cluster < cluster)
text_end++;
}
assert (text_start < text_end);
if (false)
printf("start %u end %u text start %u end %u\n", start, end, text_start, text_end);
#if 0
hb_buffer_flags_t flags = hb_buffer_get_flags (fragment);
if (0 < text_start)
flags = (hb_buffer_flags_t) (flags & ~HB_BUFFER_FLAG_BOT);
if (text_end < num_chars)
flags = (hb_buffer_flags_t) (flags & ~HB_BUFFER_FLAG_EOT);
hb_buffer_set_flags (fragment, flags);
#endif
hb_buffer_append (fragments[fragment_idx], text_buffer, text_start, text_end);
start = end;
text_start = text_end;
fragment_idx = 1 - fragment_idx;
}
}
bool ret = true;
hb_buffer_diff_flags_t diff;
/*
* Shape the two fragment streams.
*/
if (!hb_shape_full (font, fragments[0], features, num_features, shapers) ||
!fragments[0]->successful)
goto out;
if (!hb_shape_full (font, fragments[1], features, num_features, shapers) ||
!fragments[1]->successful)
goto out;
if (!forward)
{
hb_buffer_reverse (fragments[0]);
hb_buffer_reverse (fragments[1]);
}
/*
* Reconstruct results.
*/
{
unsigned fragment_idx = 0;
unsigned fragment_start[2] {0, 0};
unsigned fragment_num_glyphs[2];
hb_glyph_info_t *fragment_info[2];
for (unsigned i = 0; i < 2; i++)
fragment_info[i] = hb_buffer_get_glyph_infos (fragments[i], &fragment_num_glyphs[i]);
while (fragment_start[0] < fragment_num_glyphs[0] ||
fragment_start[1] < fragment_num_glyphs[1])
{
unsigned fragment_end = fragment_start[fragment_idx] + 1;
while (fragment_end < fragment_num_glyphs[fragment_idx] &&
(fragment_info[fragment_idx][fragment_end].cluster == fragment_info[fragment_idx][fragment_end - 1].cluster ||
fragment_info[fragment_idx][fragment_end].mask & HB_GLYPH_FLAG_UNSAFE_TO_CONCAT))
fragment_end++;
hb_buffer_append (reconstruction, fragments[fragment_idx], fragment_start[fragment_idx], fragment_end);
fragment_start[fragment_idx] = fragment_end;
fragment_idx = 1 - fragment_idx;
}
}
if (!forward)
{
hb_buffer_reverse (buffer);
hb_buffer_reverse (reconstruction);
}
if (likely (reconstruction->successful))
{
/*
* Diff results.
*/
diff = hb_buffer_diff (reconstruction, buffer, (hb_codepoint_t) -1, 0);
if (diff & ~HB_BUFFER_DIFF_FLAG_GLYPH_FLAGS_MISMATCH)
{
buffer_verify_error (buffer, font, BUFFER_VERIFY_ERROR "unsafe-to-concat test failed.");
ret = false;
/* Return the reconstructed result instead so it can be inspected. */
hb_buffer_set_length (buffer, 0);
hb_buffer_append (buffer, reconstruction, 0, -1);
}
}
out:
hb_buffer_destroy (reconstruction);
hb_buffer_destroy (fragments[0]);
hb_buffer_destroy (fragments[1]);
return ret;
}
bool
hb_buffer_t::verify (hb_buffer_t *text_buffer,
hb_font_t *font,
const hb_feature_t *features,
unsigned int num_features,
const char * const *shapers)
{
bool ret = true;
if (!buffer_verify_monotone (this, font))
ret = false;
if (!buffer_verify_unsafe_to_break (this, text_buffer, font, features, num_features, shapers))
ret = false;
if ((flags & HB_BUFFER_FLAG_PRODUCE_UNSAFE_TO_CONCAT) != 0 &&
!buffer_verify_unsafe_to_concat (this, text_buffer, font, features, num_features, shapers))
ret = false;
if (!ret)
{
#ifndef HB_NO_BUFFER_SERIALIZE
unsigned len = text_buffer->len;
hb_vector_t<char> bytes;
if (likely (bytes.resize (len * 10 + 16)))
{
hb_buffer_serialize_unicode (text_buffer,
0, len,
bytes.arrayZ, bytes.length,
&len,
HB_BUFFER_SERIALIZE_FORMAT_TEXT,
HB_BUFFER_SERIALIZE_FLAG_NO_CLUSTERS);
buffer_verify_error (this, font, BUFFER_VERIFY_ERROR "text was: %s.", bytes.arrayZ ? bytes.arrayZ : "");
}
#endif
}
return ret;
}
#endif