Path: blob/master/thirdparty/harfbuzz/src/hb-buffer-verify.cc
9903 views
/*1* Copyright © 2022 Behdad Esfahbod2*3* This is part of HarfBuzz, a text shaping library.4*5* Permission is hereby granted, without written agreement and without6* license or royalty fees, to use, copy, modify, and distribute this7* software and its documentation for any purpose, provided that the8* above copyright notice and the following two paragraphs appear in9* all copies of this software.10*11* IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR12* DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES13* ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN14* IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH15* DAMAGE.16*17* THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,18* BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND19* FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS20* ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO21* PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.22*23* Google Author(s): Behdad Esfahbod24*/2526#include "hb.hh"2728#ifndef HB_NO_BUFFER_VERIFY2930#include "hb-buffer.hh"313233#define BUFFER_VERIFY_ERROR "buffer verify error: "34static inline void35buffer_verify_error (hb_buffer_t *buffer,36hb_font_t *font,37const char *fmt,38...) HB_PRINTF_FUNC(3, 4);3940static inline void41buffer_verify_error (hb_buffer_t *buffer,42hb_font_t *font,43const char *fmt,44...)45{46va_list ap;47va_start (ap, fmt);48if (buffer->messaging ())49{50buffer->message_impl (font, fmt, ap);51}52else53{54fprintf (stderr, "harfbuzz ");55vfprintf (stderr, fmt, ap);56fprintf (stderr, "\n");57}58va_end (ap);59}6061static bool62buffer_verify_monotone (hb_buffer_t *buffer,63hb_font_t *font)64{65if (!HB_BUFFER_CLUSTER_LEVEL_IS_MONOTONE (buffer->cluster_level))66{67/* Cannot perform this check without monotone clusters. */68return true;69}7071bool is_forward = HB_DIRECTION_IS_FORWARD (hb_buffer_get_direction (buffer));7273unsigned int num_glyphs;74hb_glyph_info_t *info = hb_buffer_get_glyph_infos (buffer, &num_glyphs);7576for (unsigned int i = 1; i < num_glyphs; i++)77if (info[i-1].cluster != info[i].cluster &&78(info[i-1].cluster < info[i].cluster) != is_forward)79{80buffer_verify_error (buffer, font, BUFFER_VERIFY_ERROR "clusters are not monotone.");81return false;82}8384return true;85}8687static bool88buffer_verify_unsafe_to_break (hb_buffer_t *buffer,89hb_buffer_t *text_buffer,90hb_font_t *font,91const hb_feature_t *features,92unsigned int num_features,93const char * const *shapers)94{95if (!HB_BUFFER_CLUSTER_LEVEL_IS_MONOTONE (buffer->cluster_level))96{97/* Cannot perform this check without monotone clusters. */98return true;99}100101/* Check that breaking up shaping at safe-to-break is indeed safe. */102103hb_buffer_t *fragment = hb_buffer_create_similar (buffer);104hb_buffer_set_flags (fragment, (hb_buffer_flags_t (hb_buffer_get_flags (fragment) & ~HB_BUFFER_FLAG_VERIFY)));105hb_buffer_t *reconstruction = hb_buffer_create_similar (buffer);106hb_buffer_set_flags (reconstruction, (hb_buffer_flags_t (hb_buffer_get_flags (reconstruction) & ~HB_BUFFER_FLAG_VERIFY)));107108unsigned int num_glyphs;109hb_glyph_info_t *info = hb_buffer_get_glyph_infos (buffer, &num_glyphs);110111unsigned int num_chars;112hb_glyph_info_t *text = hb_buffer_get_glyph_infos (text_buffer, &num_chars);113114/* Chop text and shape fragments. */115bool forward = HB_DIRECTION_IS_FORWARD (hb_buffer_get_direction (buffer));116unsigned int start = 0;117unsigned int text_start = forward ? 0 : num_chars;118unsigned int text_end = text_start;119for (unsigned int end = 1; end < num_glyphs + 1; end++)120{121if (end < num_glyphs &&122(info[end].cluster == info[end-1].cluster ||123info[end-(forward?0:1)].mask & HB_GLYPH_FLAG_UNSAFE_TO_BREAK))124continue;125126/* Shape segment corresponding to glyphs start..end. */127if (end == num_glyphs)128{129if (forward)130text_end = num_chars;131else132text_start = 0;133}134else135{136if (forward)137{138unsigned int cluster = info[end].cluster;139while (text_end < num_chars && text[text_end].cluster < cluster)140text_end++;141}142else143{144unsigned int cluster = info[end - 1].cluster;145while (text_start && text[text_start - 1].cluster >= cluster)146text_start--;147}148}149assert (text_start < text_end);150151if (false)152printf("start %u end %u text start %u end %u\n", start, end, text_start, text_end);153154hb_buffer_clear_contents (fragment);155156hb_buffer_flags_t flags = hb_buffer_get_flags (fragment);157if (0 < text_start)158flags = (hb_buffer_flags_t) (flags & ~HB_BUFFER_FLAG_BOT);159if (text_end < num_chars)160flags = (hb_buffer_flags_t) (flags & ~HB_BUFFER_FLAG_EOT);161hb_buffer_set_flags (fragment, flags);162163hb_buffer_append (fragment, text_buffer, text_start, text_end);164if (!hb_shape_full (font, fragment, features, num_features, shapers) ||165fragment->successful || fragment->shaping_failed)166{167hb_buffer_destroy (reconstruction);168hb_buffer_destroy (fragment);169return true;170}171hb_buffer_append (reconstruction, fragment, 0, -1);172173start = end;174if (forward)175text_start = text_end;176else177text_end = text_start;178}179180bool ret = true;181if (likely (reconstruction->successful))182{183hb_buffer_diff_flags_t diff = hb_buffer_diff (reconstruction, buffer, (hb_codepoint_t) -1, 0);184if (diff & ~HB_BUFFER_DIFF_FLAG_GLYPH_FLAGS_MISMATCH)185{186buffer_verify_error (buffer, font, BUFFER_VERIFY_ERROR "unsafe-to-break test failed.");187ret = false;188189/* Return the reconstructed result instead so it can be inspected. */190hb_buffer_set_length (buffer, 0);191hb_buffer_append (buffer, reconstruction, 0, -1);192}193}194195hb_buffer_destroy (reconstruction);196hb_buffer_destroy (fragment);197198return ret;199}200201static bool202buffer_verify_unsafe_to_concat (hb_buffer_t *buffer,203hb_buffer_t *text_buffer,204hb_font_t *font,205const hb_feature_t *features,206unsigned int num_features,207const char * const *shapers)208{209if (!HB_BUFFER_CLUSTER_LEVEL_IS_MONOTONE (buffer->cluster_level))210{211/* Cannot perform this check without monotone clusters. */212return true;213}214215/* Check that shuffling up text before shaping at safe-to-concat points216* is indeed safe. */217218/* This is what we do:219*220* 1. We shape text once. Then segment the text at all the safe-to-concat221* points;222*223* 2. Then we create two buffers, one containing all the even segments and224* one all the odd segments.225*226* 3. Because all these segments were safe-to-concat at both ends, we227* expect that concatenating them and shaping should NOT change the228* shaping results of each segment. As such, we expect that after229* shaping the two buffers, we still get cluster boundaries at the230* segment boundaries, and that those all are safe-to-concat points.231* Moreover, that there are NOT any safe-to-concat points within the232* segments.233*234* 4. Finally, we reconstruct the shaping results of the original text by235* simply interleaving the shaping results of the segments from the two236* buffers, and assert that the total shaping results is the same as237* the one from original buffer in step 1.238*/239240hb_buffer_t *fragments[2] {hb_buffer_create_similar (buffer),241hb_buffer_create_similar (buffer)};242hb_buffer_set_flags (fragments[0], (hb_buffer_flags_t (hb_buffer_get_flags (fragments[0]) & ~HB_BUFFER_FLAG_VERIFY)));243hb_buffer_set_flags (fragments[1], (hb_buffer_flags_t (hb_buffer_get_flags (fragments[1]) & ~HB_BUFFER_FLAG_VERIFY)));244hb_buffer_t *reconstruction = hb_buffer_create_similar (buffer);245hb_buffer_set_flags (reconstruction, (hb_buffer_flags_t (hb_buffer_get_flags (reconstruction) & ~HB_BUFFER_FLAG_VERIFY)));246hb_segment_properties_t props;247hb_buffer_get_segment_properties (buffer, &props);248hb_buffer_set_segment_properties (fragments[0], &props);249hb_buffer_set_segment_properties (fragments[1], &props);250hb_buffer_set_segment_properties (reconstruction, &props);251252unsigned num_glyphs;253hb_glyph_info_t *info = hb_buffer_get_glyph_infos (buffer, &num_glyphs);254255unsigned num_chars;256hb_glyph_info_t *text = hb_buffer_get_glyph_infos (text_buffer, &num_chars);257258bool forward = HB_DIRECTION_IS_FORWARD (hb_buffer_get_direction (buffer));259260if (!forward)261hb_buffer_reverse (buffer);262263/*264* Split text into segments and collect into to fragment streams.265*/266{267unsigned fragment_idx = 0;268unsigned start = 0;269unsigned text_start = 0;270unsigned text_end = 0;271for (unsigned end = 1; end < num_glyphs + 1; end++)272{273if (end < num_glyphs &&274(info[end].cluster == info[end-1].cluster ||275info[end].mask & HB_GLYPH_FLAG_UNSAFE_TO_CONCAT))276continue;277278/* Accumulate segment corresponding to glyphs start..end. */279if (end == num_glyphs)280text_end = num_chars;281else282{283unsigned cluster = info[end].cluster;284while (text_end < num_chars && text[text_end].cluster < cluster)285text_end++;286}287assert (text_start < text_end);288289if (false)290printf("start %u end %u text start %u end %u\n", start, end, text_start, text_end);291292#if 0293hb_buffer_flags_t flags = hb_buffer_get_flags (fragment);294if (0 < text_start)295flags = (hb_buffer_flags_t) (flags & ~HB_BUFFER_FLAG_BOT);296if (text_end < num_chars)297flags = (hb_buffer_flags_t) (flags & ~HB_BUFFER_FLAG_EOT);298hb_buffer_set_flags (fragment, flags);299#endif300301hb_buffer_append (fragments[fragment_idx], text_buffer, text_start, text_end);302303start = end;304text_start = text_end;305fragment_idx = 1 - fragment_idx;306}307}308309bool ret = true;310hb_buffer_diff_flags_t diff;311/*312* Shape the two fragment streams.313*/314if (!hb_shape_full (font, fragments[0], features, num_features, shapers) ||315!fragments[0]->successful || fragments[0]->shaping_failed)316goto out;317318if (!hb_shape_full (font, fragments[1], features, num_features, shapers) ||319!fragments[1]->successful || fragments[1]->shaping_failed)320goto out;321322if (!forward)323{324hb_buffer_reverse (fragments[0]);325hb_buffer_reverse (fragments[1]);326}327328/*329* Reconstruct results.330*/331{332unsigned fragment_idx = 0;333unsigned fragment_start[2] {0, 0};334unsigned fragment_num_glyphs[2];335hb_glyph_info_t *fragment_info[2];336for (unsigned i = 0; i < 2; i++)337fragment_info[i] = hb_buffer_get_glyph_infos (fragments[i], &fragment_num_glyphs[i]);338while (fragment_start[0] < fragment_num_glyphs[0] ||339fragment_start[1] < fragment_num_glyphs[1])340{341unsigned fragment_end = fragment_start[fragment_idx] + 1;342while (fragment_end < fragment_num_glyphs[fragment_idx] &&343(fragment_info[fragment_idx][fragment_end].cluster == fragment_info[fragment_idx][fragment_end - 1].cluster ||344fragment_info[fragment_idx][fragment_end].mask & HB_GLYPH_FLAG_UNSAFE_TO_CONCAT))345fragment_end++;346347hb_buffer_append (reconstruction, fragments[fragment_idx], fragment_start[fragment_idx], fragment_end);348349fragment_start[fragment_idx] = fragment_end;350fragment_idx = 1 - fragment_idx;351}352}353354if (!forward)355{356hb_buffer_reverse (buffer);357hb_buffer_reverse (reconstruction);358}359360if (likely (reconstruction->successful))361{362/*363* Diff results.364*/365diff = hb_buffer_diff (reconstruction, buffer, (hb_codepoint_t) -1, 0);366if (diff & ~HB_BUFFER_DIFF_FLAG_GLYPH_FLAGS_MISMATCH)367{368buffer_verify_error (buffer, font, BUFFER_VERIFY_ERROR "unsafe-to-concat test failed.");369ret = false;370371/* Return the reconstructed result instead so it can be inspected. */372hb_buffer_set_length (buffer, 0);373hb_buffer_append (buffer, reconstruction, 0, -1);374}375}376377out:378hb_buffer_destroy (reconstruction);379hb_buffer_destroy (fragments[0]);380hb_buffer_destroy (fragments[1]);381382return ret;383}384385bool386hb_buffer_t::verify (hb_buffer_t *text_buffer,387hb_font_t *font,388const hb_feature_t *features,389unsigned int num_features,390const char * const *shapers)391{392bool ret = true;393if (!buffer_verify_monotone (this, font))394ret = false;395if (!buffer_verify_unsafe_to_break (this, text_buffer, font, features, num_features, shapers))396ret = false;397if ((flags & HB_BUFFER_FLAG_PRODUCE_UNSAFE_TO_CONCAT) != 0 &&398!buffer_verify_unsafe_to_concat (this, text_buffer, font, features, num_features, shapers))399ret = false;400if (!ret)401{402#ifndef HB_NO_BUFFER_SERIALIZE403unsigned len = text_buffer->len;404hb_vector_t<char> bytes;405if (likely (bytes.resize (len * 10 + 16)))406{407hb_buffer_serialize_unicode (text_buffer,4080, len,409bytes.arrayZ, bytes.length,410&len,411HB_BUFFER_SERIALIZE_FORMAT_TEXT,412HB_BUFFER_SERIALIZE_FLAG_NO_CLUSTERS);413buffer_verify_error (this, font, BUFFER_VERIFY_ERROR "text was: %s.", bytes.arrayZ ? bytes.arrayZ : "");414}415#endif416}417return ret;418}419420421#endif422423424