/* * Copyright © 2011 Google, Inc. * * 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-test.h" /* Unit tests for hb-shape.h */ /* * This test provides a framework to test aspects of hb_shape() that are * font-independent. Please add tests for any feature that fits that * description. */ /* TODO Make this test data-driven and add some real test data */ /* TODO Test positions too. And test non-native direction. Test commit 2e18c6dbdfb */ static const char test_data[] = "test\0data"; static hb_position_t glyph_h_advance_func (hb_font_t *font HB_UNUSED, void *font_data HB_UNUSED, hb_codepoint_t glyph, void *user_data HB_UNUSED) { switch (glyph) { case 1: return 10; case 2: return 6; case 3: return 5; } return 0; } static hb_bool_t glyph_func (hb_font_t *font HB_UNUSED, void *font_data HB_UNUSED, hb_codepoint_t unicode, hb_codepoint_t *glyph, void *user_data HB_UNUSED) { switch (unicode) { case 'T': *glyph = 1; return TRUE; case 'e': *glyph = 2; return TRUE; case 's': *glyph = 3; return TRUE; } return FALSE; } static const char TesT[] = "TesT"; static void test_font (hb_font_t *font) { hb_buffer_t *buffer; unsigned int len; hb_glyph_info_t *glyphs; hb_glyph_position_t *positions; buffer = hb_buffer_create (); hb_buffer_set_direction (buffer, HB_DIRECTION_LTR); hb_buffer_add_utf8 (buffer, TesT, 4, 0, 4); hb_shape (font, buffer, NULL, 0); len = hb_buffer_get_length (buffer); glyphs = hb_buffer_get_glyph_infos (buffer, NULL); positions = hb_buffer_get_glyph_positions (buffer, NULL); { const hb_codepoint_t output_glyphs[] = {1, 2, 3, 1}; const hb_position_t output_x_advances[] = {10, 6, 5, 10}; const hb_position_t output_x_offsets[] = {0, 0, 0, 0}; unsigned int i; g_assert_cmpint (len, ==, 4); for (i = 0; i < len; i++) { g_assert_cmphex (glyphs[i].codepoint, ==, output_glyphs[i]); g_assert_cmphex (glyphs[i].cluster, ==, i); } for (i = 0; i < len; i++) { g_assert_cmpint (output_x_advances[i], ==, positions[i].x_advance); g_assert_cmpint (output_x_offsets [i], ==, positions[i].x_offset); g_assert_cmpint (0, ==, positions[i].y_advance); g_assert_cmpint (0, ==, positions[i].y_offset); } } hb_buffer_destroy (buffer); } static void test_shape (void) { hb_blob_t *blob; hb_face_t *face; hb_font_funcs_t *ffuncs; hb_font_t *font, *sub_font; blob = hb_blob_create (test_data, sizeof (test_data), HB_MEMORY_MODE_READONLY, NULL, NULL); face = hb_face_create (blob, 0); hb_blob_destroy (blob); font = hb_font_create (face); hb_face_destroy (face); hb_font_set_scale (font, 10, 10); ffuncs = hb_font_funcs_create (); hb_font_funcs_set_glyph_h_advance_func (ffuncs, glyph_h_advance_func, NULL, NULL); hb_font_funcs_set_nominal_glyph_func (ffuncs, glyph_func, malloc (10), free); hb_font_set_funcs (font, ffuncs, NULL, NULL); hb_font_funcs_destroy (ffuncs); test_font (font); sub_font = hb_font_create_sub_font (font); test_font (sub_font); hb_font_destroy (sub_font); hb_font_destroy (font); } static void test_shape_clusters (void) { hb_face_t *face; hb_font_t *font; hb_buffer_t *buffer; unsigned int len; hb_glyph_info_t *glyphs; face = hb_face_create (NULL, 0); font = hb_font_create (face); hb_face_destroy (face); buffer = hb_buffer_create (); hb_buffer_set_direction (buffer, HB_DIRECTION_LTR); { /* https://crbug.com/497578 */ hb_codepoint_t test[] = {0xFFF1, 0xF0B6}; hb_buffer_add_utf32 (buffer, test, 2, 0, 2); } hb_shape (font, buffer, NULL, 0); len = hb_buffer_get_length (buffer); glyphs = hb_buffer_get_glyph_infos (buffer, NULL); { const hb_codepoint_t output_glyphs[] = {0}; const hb_position_t output_clusters[] = {0}; unsigned int i; g_assert_cmpint (len, ==, 1); for (i = 0; i < len; i++) { g_assert_cmphex (glyphs[i].codepoint, ==, output_glyphs[i]); g_assert_cmphex (glyphs[i].cluster, ==, output_clusters[i]); } } hb_buffer_destroy (buffer); hb_font_destroy (font); } static void test_shape_overlong_mark_cluster (void) { hb_face_t *face = hb_test_open_font_file ("fonts/NotoSans-Bold.ttf"); hb_font_t *font = hb_font_create (face); hb_buffer_t *buffer = hb_buffer_create (); hb_font_set_scale (font, 12 * 65536, 12 * 65536); hb_buffer_set_content_type (buffer, HB_BUFFER_CONTENT_TYPE_UNICODE); hb_buffer_add (buffer, 'e', 0); for (unsigned i = 0; i < 35000; i++) hb_buffer_add (buffer, 0x0301, i + 1); hb_buffer_add (buffer, 'X', 35001); hb_buffer_guess_segment_properties (buffer); hb_shape (font, buffer, NULL, 0); unsigned len; hb_glyph_position_t *positions = hb_buffer_get_glyph_positions (buffer, &len); g_assert_cmpuint (len, ==, 35001); g_assert_cmpint (positions[len - 2].y_offset, ==, INT32_MAX); hb_buffer_destroy (buffer); hb_font_destroy (font); hb_face_destroy (face); } static void test_shape_overlong_cluster_remerge (void) { /* The font has no space glyph, so default-ignorables are deleted in * place. Shaped RTL, the deleted soft hyphens follow the big mark * cluster with descending cluster values, so each deletion would rewrite * the cluster of the whole preceding run. That re-merge walk must be * charged to the buffer's op budget and fail the buffer, instead of * taking O(N^2) time. */ hb_face_t *face = hb_test_open_font_file ("fonts/Roboto-Regular.abc.ttf"); hb_font_t *font = hb_font_create (face); hb_buffer_t *buffer = hb_buffer_create (); hb_buffer_set_content_type (buffer, HB_BUFFER_CONTENT_TYPE_UNICODE); for (unsigned i = 0; i < 20000; i++) hb_buffer_add (buffer, 0x00AD, i); hb_buffer_add (buffer, 'a', 20000); for (unsigned i = 0; i < 20000; i++) hb_buffer_add (buffer, 0x0301, 20001 + i); hb_buffer_guess_segment_properties (buffer); hb_buffer_set_direction (buffer, HB_DIRECTION_RTL); /* This exercises the native cluster re-merge op budget in hb-buffer.cc, so * force the "ot" shaper: other shapers (e.g. harfrust) do their own cluster * handling and never hit this path. */ const char *shaper_list[] = {"ot", NULL}; hb_shape_full (font, buffer, NULL, 0, shaper_list); g_assert_true (!hb_buffer_allocation_successful (buffer)); hb_buffer_destroy (buffer); hb_font_destroy (font); hb_face_destroy (face); } #ifndef HB_NO_AAT static hb_bool_t count_contour_point_calls (hb_font_t *font HB_UNUSED, void *font_data, hb_codepoint_t glyph HB_UNUSED, unsigned int point_index HB_UNUSED, hb_position_t *x, hb_position_t *y, void *user_data HB_UNUSED) { (*(unsigned *) font_data)++; *x = *y = 0; return TRUE; } static void test_shape_kerx_control_point_actions_are_charged (void) { /* A two-glyph font whose kerx format-4 subtable loops on the letter 'a' * in state 1 without advancing, performing a control-point action on * every transition. Each action asks the font for two contour points, * which may load a whole glyph outline, so shaping must charge them to * the buffer's work budget. */ static const char maxp[] = {0x00, 0x00, 0x50, 0x00, 0x00, 0x02}; static const char cmap[] = {0, 0, 0, 1, 0, 3, 0, 10, 0, 0, 0, 12, 0, 12, 0, 0, 0, 0, 0, 28, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 'a', 0, 0, 0, 'a', 0, 0, 0, 1}; static const unsigned char kerx[] = { 0, 2, 0, 0, 0, 0, 0, 1, /* version 2, one subtable */ 0, 0, 0, 82, 0, 0, 0, 4, 0, 0, 0, 0, /* length, format 4, tupleCount */ 0, 0, 0, 5, 0, 0, 0, 20, 0, 0, 0, 28, 0, 0, 0, 48, /* nClasses, class, state, entry offsets */ 0, 0, 0, 66, /* action type 0, control points at 66 */ 0, 8, 0, 1, 0, 1, 0, 4, /* class lookup format 8: glyph 1 is class 4 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, /* state 0: class 4 takes entry 1 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, /* state 1: class 4 takes entry 2 */ 0, 0, 0x00, 0, 0xFF, 0xFF, /* entry 0: to state 0 */ 0, 1, 0xC0, 0, 0xFF, 0xFF, /* entry 1: to state 1, Mark, DontAdvance */ 0, 1, 0x40, 0, 0, 0, /* entry 2: to state 1, DontAdvance, action 0 */ 0, 0, 0, 0 /* action 0: mark point 0, current point 0 */ }; static const char *shapers[] = {"ot", NULL}; hb_face_t *builder = hb_face_builder_create (); hb_blob_t *blob = hb_blob_create (maxp, sizeof (maxp), HB_MEMORY_MODE_READONLY, NULL, NULL); hb_face_builder_add_table (builder, HB_TAG ('m','a','x','p'), blob); hb_blob_destroy (blob); blob = hb_blob_create (cmap, sizeof (cmap), HB_MEMORY_MODE_READONLY, NULL, NULL); hb_face_builder_add_table (builder, HB_TAG ('c','m','a','p'), blob); hb_blob_destroy (blob); blob = hb_blob_create ((const char *) kerx, sizeof (kerx), HB_MEMORY_MODE_READONLY, NULL, NULL); hb_face_builder_add_table (builder, HB_TAG ('k','e','r','x'), blob); hb_blob_destroy (blob); blob = hb_face_reference_blob (builder); hb_face_destroy (builder); hb_face_t *face = hb_face_create (blob, 0); hb_blob_destroy (blob); unsigned calls = 0; hb_font_t *parent = hb_font_create (face); hb_font_t *font = hb_font_create_sub_font (parent); hb_font_funcs_t *funcs = hb_font_funcs_create (); hb_font_funcs_set_glyph_contour_point_func (funcs, count_contour_point_calls, NULL, NULL); hb_font_set_funcs (font, funcs, &calls, NULL); hb_font_funcs_destroy (funcs); hb_buffer_t *buffer = hb_buffer_create (); hb_buffer_add_utf8 (buffer, "aa", -1, 0, -1); hb_buffer_guess_segment_properties (buffer); hb_shape_full (font, buffer, NULL, 0, shapers); /* 131074 before the budget was charged. */ g_assert_cmpuint (calls, >=, 2); g_assert_cmpuint (calls, <=, 256); hb_buffer_destroy (buffer); hb_font_destroy (font); hb_font_destroy (parent); hb_face_destroy (face); } #endif static void test_shape_harfrust_budget (void) { /* A HarfRust max_ops / preprocessing-budget trip must propagate to the C * buffer's allocation_successful flag, the same way the ot shaper reports a * budget failure. Many ranged (non-global) features make HarfRust's * setup_masks() overrun the per-text op budget and mark its own buffer * unsuccessful; hb-harfrust.cc must forward that to the hb_buffer_t so that a * caller of the void-returning hb_shape() can see it. Only meaningful when * HarfRust is built in, so skip otherwise. */ hb_bool_t have_harfrust = false; for (const char * const *s = hb_shape_list_shapers (); *s; s++) if (0 == strcmp (*s, "harfrust")) { have_harfrust = true; break; } if (!have_harfrust) { g_test_skip ("HarfRust shaper not built"); return; } hb_face_t *face = hb_test_open_font_file ("fonts/Roboto-Regular.abc.ttf"); hb_font_t *font = hb_font_create (face); hb_buffer_t *buffer = hb_buffer_create (); hb_buffer_add_utf8 (buffer, "abcabcab", -1, 0, -1); hb_buffer_guess_segment_properties (buffer); /* One ranged feature per entry; count times the text length exceeds the * per-text op budget, which HarfRust charges in setup_masks(). */ const unsigned num_features = 20000; hb_feature_t *features = g_new0 (hb_feature_t, num_features); for (unsigned i = 0; i < num_features; i++) { features[i].tag = HB_TAG ('k', 'e', 'r', 'n'); features[i].value = 0; features[i].start = i; features[i].end = i + 1; } const char *shaper_list[] = {"harfrust", NULL}; hb_shape_full (font, buffer, features, num_features, shaper_list); g_assert_true (!hb_buffer_allocation_successful (buffer)); g_free (features); hb_buffer_destroy (buffer); hb_font_destroy (font); hb_face_destroy (face); } static void put16 (char *p, unsigned v) { p[0] = (char) (v >> 8); p[1] = (char) v; } static hb_face_t * create_aliased_lookups_face (unsigned lookup_count) { /* A GSUB whose one 'ccmp' feature lists two lookups that each replace * glyph 1 by 255 copies of it, growing one 'a' to 65025 glyphs, and then * lookup_count lookups, all of them the same 20-byte single substitution * of glyph 1 by itself. */ unsigned total = 2 + lookup_count; unsigned feature_len = 2 + 6 + 4 + 2 * total; unsigned multiple_len = 8 + 8 + 6 + 2 + 2 * 255; unsigned lookup_list_len = 2 + 2 * total + 20 + multiple_len; unsigned gsub_len = 10 + 20 + feature_len + lookup_list_len; char *gsub = calloc (gsub_len, 1); char *p = gsub; unsigned i; /* Header */ put16 (p, 1); put16 (p + 4, 10); put16 (p + 6, 10 + 20); put16 (p + 8, 10 + 20 + feature_len); p += 10; /* ScriptList: 'DFLT' with a default LangSys using feature 0 */ put16 (p, 1); memcpy (p + 2, "DFLT", 4); put16 (p + 6, 8); put16 (p + 8, 4); put16 (p + 10, 0); put16 (p + 12, 0); put16 (p + 14, 0xFFFF); put16 (p + 16, 1); put16 (p + 18, 0); p += 20; /* FeatureList: 'ccmp' listing every lookup */ put16 (p, 1); memcpy (p + 2, "ccmp", 4); put16 (p + 6, 8); put16 (p + 8, 0); put16 (p + 10, total); for (i = 0; i < total; i++) put16 (p + 12 + 2 * i, i); p += feature_len; /* LookupList: lookups 0 and 1 point at the multiple substitution, the * others at the single substitution, both after the offsets. */ unsigned single_offset = 2 + 2 * total; unsigned multiple_offset = single_offset + 20; put16 (p, total); for (i = 0; i < total; i++) put16 (p + 2 + 2 * i, i < 2 ? multiple_offset : single_offset); p += single_offset; put16 (p, 1); /* Lookup: type 1 */ put16 (p + 2, 0); /* flag */ put16 (p + 4, 1); /* one subtable */ put16 (p + 6, 8); put16 (p + 8, 1); /* SingleSubstFormat1 */ put16 (p + 10, 6); /* coverage */ put16 (p + 12, 0); /* deltaGlyphID */ put16 (p + 14, 1); /* CoverageFormat1 */ put16 (p + 16, 1); put16 (p + 18, 1); /* glyph 1 */ p += 20; put16 (p, 2); /* Lookup: type 2 */ put16 (p + 2, 0); /* flag */ put16 (p + 4, 1); /* one subtable */ put16 (p + 6, 8); put16 (p + 8, 1); /* MultipleSubstFormat1 */ put16 (p + 10, 8); /* coverage */ put16 (p + 12, 1); /* one sequence */ put16 (p + 14, 14); put16 (p + 16, 1); /* CoverageFormat1 */ put16 (p + 18, 1); put16 (p + 20, 1); /* glyph 1 */ put16 (p + 22, 255); /* Sequence: 255 times glyph 1 */ for (i = 0; i < 255; i++) put16 (p + 24 + 2 * i, 1); /* cmap: format 12, 'a' -> glyph 1 */ static const char cmap_data[] = {0, 0, 0, 1, 0, 3, 0, 10, 0, 0, 0, 12, 0, 12, 0, 0, 0, 0, 0, 28, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 'a', 0, 0, 0, 'a', 0, 0, 0, 1}; hb_face_t *face = hb_face_builder_create (); hb_blob_t *blob = hb_blob_create_or_fail (gsub, gsub_len, HB_MEMORY_MODE_WRITABLE, gsub, free); hb_face_builder_add_table (face, HB_TAG ('G','S','U','B'), blob); hb_blob_destroy (blob); HB_FACE_ADD_TABLE (face, "cmap", cmap_data); hb_face_set_glyph_count (face, 2); return face; } static void test_shape_lookup_budget (void) { /* Lookups and features can alias the same bytes, and a lookup can grow * the buffer far beyond the text, so a small font can make the shaper * walk a long buffer thousands of times for one character. Those passes * must be charged to the buffer's work budget, which one character sizes * at 65536, and cut off when it runs out; each costs 65025 / 16. */ hb_face_t *face; hb_font_t *font; hb_buffer_t *buffer; const char *shaper_list[] = {"ot", NULL}; face = create_aliased_lookups_face (4); font = hb_font_create (face); buffer = hb_buffer_create (); hb_buffer_set_content_type (buffer, HB_BUFFER_CONTENT_TYPE_UNICODE); hb_buffer_add (buffer, 'a', 0); hb_buffer_guess_segment_properties (buffer); hb_shape_full (font, buffer, NULL, 0, shaper_list); g_assert_true (hb_buffer_allocation_successful (buffer)); g_assert_cmpuint (hb_buffer_get_length (buffer), ==, 65025); hb_buffer_destroy (buffer); hb_font_destroy (font); hb_face_destroy (face); face = create_aliased_lookups_face (2000); font = hb_font_create (face); buffer = hb_buffer_create (); hb_buffer_set_content_type (buffer, HB_BUFFER_CONTENT_TYPE_UNICODE); hb_buffer_add (buffer, 'a', 0); hb_buffer_guess_segment_properties (buffer); hb_shape_full (font, buffer, NULL, 0, shaper_list); g_assert_false (hb_buffer_allocation_successful (buffer)); /* The glyphs made so far are kept. */ g_assert_cmpuint (hb_buffer_get_length (buffer), ==, 65025); hb_buffer_destroy (buffer); hb_font_destroy (font); hb_face_destroy (face); } static void test_shape_list (void) { const char **shapers = hb_shape_list_shapers (); unsigned int i; for (i = 0; shapers[i]; i++) ; g_assert_cmpint (i, >, 1); g_assert_true (!strcmp (shapers[i - 1], "fallback")); } static void test_shape_cursive_anchor_overflow (void) { hb_face_t *face = hb_test_open_font_file ("fonts/NotoNastaliqUrdu-Regular.ttf"); hb_font_t *font = hb_font_create (face); /* At this scale the cursive anchors of the font do not fit in hb_position_t. * They have to saturate; an out-of-range conversion from float is undefined, * and on x86 gives a large negative advance. */ hb_font_set_scale (font, INT32_MAX, INT32_MAX); hb_buffer_t *buffer = hb_buffer_create (); hb_buffer_add_utf8 (buffer, "\xD8\xB3\xD9\x84\xD8\xA7\xD9\x85", -1, 0, -1); hb_buffer_guess_segment_properties (buffer); hb_buffer_set_direction (buffer, HB_DIRECTION_LTR); hb_shape (font, buffer, NULL, 0); unsigned count; hb_glyph_position_t *pos = hb_buffer_get_glyph_positions (buffer, &count); g_assert_cmpuint (count, >, 0); for (unsigned i = 0; i < count; i++) g_assert_cmpint (pos[i].x_advance, >=, 0); hb_buffer_destroy (buffer); hb_font_destroy (font); hb_face_destroy (face); } int main (int argc, char **argv) { hb_test_init (&argc, &argv); hb_test_add (test_shape); hb_test_add (test_shape_clusters); hb_test_add (test_shape_overlong_mark_cluster); hb_test_add (test_shape_overlong_cluster_remerge); hb_test_add (test_shape_harfrust_budget); #ifndef HB_NO_AAT hb_test_add (test_shape_kerx_control_point_actions_are_charged); #endif hb_test_add (test_shape_lookup_budget); /* TODO test fallback shaper */ /* TODO test shaper_full */ hb_test_add (test_shape_list); hb_test_add (test_shape_cursive_anchor_overflow); return hb_test_run(); }