Path: blob/master/src/java.desktop/share/native/libharfbuzz/hb-buffer-verify.cc
66644 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{65/* Check that clusters are monotone. */66if (buffer->cluster_level == HB_BUFFER_CLUSTER_LEVEL_MONOTONE_GRAPHEMES ||67buffer->cluster_level == HB_BUFFER_CLUSTER_LEVEL_MONOTONE_CHARACTERS)68{69bool is_forward = HB_DIRECTION_IS_FORWARD (hb_buffer_get_direction (buffer));7071unsigned int num_glyphs;72hb_glyph_info_t *info = hb_buffer_get_glyph_infos (buffer, &num_glyphs);7374for (unsigned int i = 1; i < num_glyphs; i++)75if (info[i-1].cluster != info[i].cluster &&76(info[i-1].cluster < info[i].cluster) != is_forward)77{78buffer_verify_error (buffer, font, BUFFER_VERIFY_ERROR "clusters are not monotone.");79return false;80}81}8283return true;84}8586static bool87buffer_verify_unsafe_to_break (hb_buffer_t *buffer,88hb_buffer_t *text_buffer,89hb_font_t *font,90const hb_feature_t *features,91unsigned int num_features,92const char * const *shapers)93{94if (buffer->cluster_level != HB_BUFFER_CLUSTER_LEVEL_MONOTONE_GRAPHEMES &&95buffer->cluster_level != HB_BUFFER_CLUSTER_LEVEL_MONOTONE_CHARACTERS)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 (0)152printf("start %d end %d text start %d end %d\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))165{166buffer_verify_error (buffer, font, BUFFER_VERIFY_ERROR "shaping failed while shaping fragment.");167hb_buffer_destroy (reconstruction);168hb_buffer_destroy (fragment);169return false;170}171else if (!fragment->successful || fragment->shaping_failed)172{173hb_buffer_destroy (reconstruction);174hb_buffer_destroy (fragment);175return true;176}177hb_buffer_append (reconstruction, fragment, 0, -1);178179start = end;180if (forward)181text_start = text_end;182else183text_end = text_start;184}185186bool ret = true;187hb_buffer_diff_flags_t diff = hb_buffer_diff (reconstruction, buffer, (hb_codepoint_t) -1, 0);188if (diff)189{190buffer_verify_error (buffer, font, BUFFER_VERIFY_ERROR "unsafe-to-break test failed.");191ret = false;192193/* Return the reconstructed result instead so it can be inspected. */194hb_buffer_set_length (buffer, 0);195hb_buffer_append (buffer, reconstruction, 0, -1);196}197198hb_buffer_destroy (reconstruction);199hb_buffer_destroy (fragment);200201return ret;202}203204static bool205buffer_verify_unsafe_to_concat (hb_buffer_t *buffer,206hb_buffer_t *text_buffer,207hb_font_t *font,208const hb_feature_t *features,209unsigned int num_features,210const char * const *shapers)211{212if (buffer->cluster_level != HB_BUFFER_CLUSTER_LEVEL_MONOTONE_GRAPHEMES &&213buffer->cluster_level != HB_BUFFER_CLUSTER_LEVEL_MONOTONE_CHARACTERS)214{215/* Cannot perform this check without monotone clusters. */216return true;217}218219/* Check that shuffling up text before shaping at safe-to-concat points220* is indeed safe. */221222/* This is what we do:223*224* 1. We shape text once. Then segment the text at all the safe-to-concat225* points;226*227* 2. Then we create two buffers, one containing all the even segments and228* one all the odd segments.229*230* 3. Because all these segments were safe-to-concat at both ends, we231* expect that concatenating them and shaping should NOT change the232* shaping results of each segment. As such, we expect that after233* shaping the two buffers, we still get cluster boundaries at the234* segment boundaries, and that those all are safe-to-concat points.235* Moreover, that there are NOT any safe-to-concat points within the236* segments.237*238* 4. Finally, we reconstruct the shaping results of the original text by239* simply interleaving the shaping results of the segments from the two240* buffers, and assert that the total shaping results is the same as241* the one from original buffer in step 1.242*/243244hb_buffer_t *fragments[2] {hb_buffer_create_similar (buffer),245hb_buffer_create_similar (buffer)};246hb_buffer_set_flags (fragments[0], (hb_buffer_flags_t (hb_buffer_get_flags (fragments[0]) & ~HB_BUFFER_FLAG_VERIFY)));247hb_buffer_set_flags (fragments[1], (hb_buffer_flags_t (hb_buffer_get_flags (fragments[1]) & ~HB_BUFFER_FLAG_VERIFY)));248hb_buffer_t *reconstruction = hb_buffer_create_similar (buffer);249hb_buffer_set_flags (reconstruction, (hb_buffer_flags_t (hb_buffer_get_flags (reconstruction) & ~HB_BUFFER_FLAG_VERIFY)));250hb_segment_properties_t props;251hb_buffer_get_segment_properties (buffer, &props);252hb_buffer_set_segment_properties (fragments[0], &props);253hb_buffer_set_segment_properties (fragments[1], &props);254hb_buffer_set_segment_properties (reconstruction, &props);255256unsigned num_glyphs;257hb_glyph_info_t *info = hb_buffer_get_glyph_infos (buffer, &num_glyphs);258259unsigned num_chars;260hb_glyph_info_t *text = hb_buffer_get_glyph_infos (text_buffer, &num_chars);261262bool forward = HB_DIRECTION_IS_FORWARD (hb_buffer_get_direction (buffer));263264if (!forward)265hb_buffer_reverse (buffer);266267/*268* Split text into segments and collect into to fragment streams.269*/270{271unsigned fragment_idx = 0;272unsigned start = 0;273unsigned text_start = 0;274unsigned text_end = 0;275for (unsigned end = 1; end < num_glyphs + 1; end++)276{277if (end < num_glyphs &&278(info[end].cluster == info[end-1].cluster ||279info[end].mask & HB_GLYPH_FLAG_UNSAFE_TO_CONCAT))280continue;281282/* Accumulate segment corresponding to glyphs start..end. */283if (end == num_glyphs)284text_end = num_chars;285else286{287unsigned cluster = info[end].cluster;288while (text_end < num_chars && text[text_end].cluster < cluster)289text_end++;290}291assert (text_start < text_end);292293if (0)294printf("start %d end %d text start %d end %d\n", start, end, text_start, text_end);295296#if 0297hb_buffer_flags_t flags = hb_buffer_get_flags (fragment);298if (0 < text_start)299flags = (hb_buffer_flags_t) (flags & ~HB_BUFFER_FLAG_BOT);300if (text_end < num_chars)301flags = (hb_buffer_flags_t) (flags & ~HB_BUFFER_FLAG_EOT);302hb_buffer_set_flags (fragment, flags);303#endif304305hb_buffer_append (fragments[fragment_idx], text_buffer, text_start, text_end);306307start = end;308text_start = text_end;309fragment_idx = 1 - fragment_idx;310}311}312313bool ret = true;314hb_buffer_diff_flags_t diff;315316/*317* Shape the two fragment streams.318*/319if (!hb_shape_full (font, fragments[0], features, num_features, shapers))320{321buffer_verify_error (buffer, font, BUFFER_VERIFY_ERROR "shaping failed while shaping fragment.");322ret = false;323goto out;324}325else if (!fragments[0]->successful || fragments[0]->shaping_failed)326{327ret = true;328goto out;329}330if (!hb_shape_full (font, fragments[1], features, num_features, shapers))331{332buffer_verify_error (buffer, font, BUFFER_VERIFY_ERROR "shaping failed while shaping fragment.");333ret = false;334goto out;335}336else if (!fragments[1]->successful || fragments[1]->shaping_failed)337{338ret = true;339goto out;340}341342if (!forward)343{344hb_buffer_reverse (fragments[0]);345hb_buffer_reverse (fragments[1]);346}347348/*349* Reconstruct results.350*/351{352unsigned fragment_idx = 0;353unsigned fragment_start[2] {0, 0};354unsigned fragment_num_glyphs[2];355hb_glyph_info_t *fragment_info[2];356for (unsigned i = 0; i < 2; i++)357fragment_info[i] = hb_buffer_get_glyph_infos (fragments[i], &fragment_num_glyphs[i]);358while (fragment_start[0] < fragment_num_glyphs[0] ||359fragment_start[1] < fragment_num_glyphs[1])360{361unsigned fragment_end = fragment_start[fragment_idx] + 1;362while (fragment_end < fragment_num_glyphs[fragment_idx] &&363(fragment_info[fragment_idx][fragment_end].cluster == fragment_info[fragment_idx][fragment_end - 1].cluster ||364fragment_info[fragment_idx][fragment_end].mask & HB_GLYPH_FLAG_UNSAFE_TO_CONCAT))365fragment_end++;366367hb_buffer_append (reconstruction, fragments[fragment_idx], fragment_start[fragment_idx], fragment_end);368369fragment_start[fragment_idx] = fragment_end;370fragment_idx = 1 - fragment_idx;371}372}373374if (!forward)375{376hb_buffer_reverse (buffer);377hb_buffer_reverse (reconstruction);378}379380/*381* Diff results.382*/383diff = hb_buffer_diff (reconstruction, buffer, (hb_codepoint_t) -1, 0);384if (diff)385{386buffer_verify_error (buffer, font, BUFFER_VERIFY_ERROR "unsafe-to-concat test failed.");387ret = false;388389/* Return the reconstructed result instead so it can be inspected. */390hb_buffer_set_length (buffer, 0);391hb_buffer_append (buffer, reconstruction, 0, -1);392}393394395out:396hb_buffer_destroy (reconstruction);397hb_buffer_destroy (fragments[0]);398hb_buffer_destroy (fragments[1]);399400return ret;401}402403bool404hb_buffer_t::verify (hb_buffer_t *text_buffer,405hb_font_t *font,406const hb_feature_t *features,407unsigned int num_features,408const char * const *shapers)409{410bool ret = true;411if (!buffer_verify_monotone (this, font))412ret = false;413if (!buffer_verify_unsafe_to_break (this, text_buffer, font, features, num_features, shapers))414ret = false;415if ((flags & HB_BUFFER_FLAG_PRODUCE_UNSAFE_TO_CONCAT) != 0 &&416!buffer_verify_unsafe_to_concat (this, text_buffer, font, features, num_features, shapers))417ret = false;418if (!ret)419{420#ifndef HB_NO_BUFFER_SERIALIZE421unsigned len = text_buffer->len;422hb_vector_t<char> bytes;423if (likely (bytes.resize (len * 10 + 16)))424{425hb_buffer_serialize_unicode (text_buffer,4260, len,427bytes.arrayZ, bytes.length,428&len,429HB_BUFFER_SERIALIZE_FORMAT_TEXT,430HB_BUFFER_SERIALIZE_FLAG_NO_CLUSTERS);431buffer_verify_error (this, font, BUFFER_VERIFY_ERROR "text was: %s.", bytes.arrayZ);432}433#endif434}435return ret;436}437438439#endif440441442