Files
harfbuzz/test/api/test-be-num-glyphs.c
T
Behdad Esfahbod 02c6a9f935 [glyf] Preserve cubic control pairing across contours
Starting every contour at its last point rotates all-off-curve cubic
controls by one point, changing the implied curves. Preserve their
original order instead. Reset the second leading control between
contours, and draw a two-control contour as a cubic, not a point.

Add regressions using current MAXP, GLYF, LOCA, HHEA, and HMTX tables
for all-off-curve contours, consecutive contours, and two controls.

Testing: meson test -C build (281 passed, 1 skipped).

Assisted-by: OpenAI Codex <noreply@openai.com>
2026-10-02 04:31:02 -06:00

501 lines
16 KiB
C

/*
* 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.
*/
#include "hb-test.h"
#include <hb.h>
static void
write_u16 (char *data, unsigned offset, unsigned value)
{
data[offset] = value >> 8;
data[offset + 1] = value;
}
static void
write_u32 (char *data, unsigned offset, unsigned value)
{
data[offset] = value >> 24;
data[offset + 1] = value >> 16;
data[offset + 2] = value >> 8;
data[offset + 3] = value;
}
static void
write_face (char *data, unsigned offset, hb_tag_t tag,
unsigned table_offset, unsigned table_length)
{
write_u32 (data, offset, 0x00010000);
write_u16 (data, offset + 4, 1);
write_u32 (data, offset + 12, tag);
write_u32 (data, offset + 20, table_offset);
write_u32 (data, offset + 24, table_length);
}
static hb_blob_t *
create_extended_ttc (unsigned major, unsigned minor, hb_bool_t malformed)
{
unsigned extension_offset = major == 2 ? 32 : 20;
unsigned old_face_offset = extension_offset + 8;
unsigned new_face_offset = old_face_offset + 28;
unsigned maxp_offset = new_face_offset + 28;
unsigned MAXP_offset = maxp_offset + 6;
unsigned length = MAXP_offset + 7;
char *data = g_malloc0 (length);
write_u32 (data, 0, HB_TAG ('t','t','c','f'));
write_u16 (data, 4, major);
write_u16 (data, 6, minor);
write_u32 (data, 8, 2);
write_u32 (data, 12, old_face_offset);
write_u32 (data, 16, old_face_offset);
write_u32 (data, extension_offset, malformed ? 0xFFFFFFFF : 1);
write_u32 (data, extension_offset + 4, new_face_offset);
write_face (data, old_face_offset, HB_TAG ('m','a','x','p'), maxp_offset, 6);
write_face (data, new_face_offset, HB_TAG ('M','A','X','P'), MAXP_offset, 7);
write_u32 (data, maxp_offset, 0x00005000);
write_u16 (data, maxp_offset + 4, 5);
write_u32 (data, MAXP_offset, 0x00005000);
data[MAXP_offset + 4] = 1;
data[MAXP_offset + 6] = 1;
return hb_blob_create (data, length, HB_MEMORY_MODE_WRITABLE, data, g_free);
}
static void
test_maxp_and_loca (void)
{
hb_face_t *face;
const char maxp_data[] = "\x00\x00\x50\x00" // version
"\x00\x05" // numGlyphs
;
face = hb_face_builder_create ();
g_assert_cmpuint (hb_face_get_glyph_count (face), ==, 0);
hb_face_destroy (face);
face = hb_face_builder_create ();
HB_FACE_ADD_TABLE (face, "maxp", maxp_data);
g_assert_cmpuint (hb_face_get_glyph_count (face), ==, 5);
hb_face_destroy (face);
}
static void
test_extended_ttc (void)
{
for (unsigned major = 1; major <= 2; major++)
for (unsigned minor = 1; minor <= 2; minor++)
{
hb_blob_t *blob = create_extended_ttc (major, minor, FALSE);
#ifndef HB_NO_BEYOND_64K
g_assert_cmpuint (hb_face_count (blob), ==, 1);
#else
g_assert_cmpuint (hb_face_count (blob), ==, 2);
#endif
hb_face_t *face = hb_face_create (blob, 0);
#ifndef HB_NO_BEYOND_64K
g_assert_cmpuint (hb_face_get_glyph_count (face), ==, 0x10001);
#else
g_assert_cmpuint (hb_face_get_glyph_count (face), ==, 5);
#endif
hb_face_destroy (face);
hb_blob_destroy (blob);
blob = create_extended_ttc (major, minor, TRUE);
#ifndef HB_NO_BEYOND_64K
g_assert_cmpuint (hb_face_count (blob), ==, 0);
#else
g_assert_cmpuint (hb_face_count (blob), ==, 2);
#endif
hb_blob_destroy (blob);
blob = create_extended_ttc (major, minor, FALSE);
char *data = hb_blob_get_data_writable (blob, NULL);
write_u32 (data, 12, 0xFFFFFFFF);
write_u32 (data, 16, 0xFFFFFFFF);
#ifndef HB_NO_BEYOND_64K
g_assert_cmpuint (hb_face_count (blob), ==, 1);
#else
g_assert_cmpuint (hb_face_count (blob), ==, 0);
#endif
hb_blob_destroy (blob);
}
}
#ifndef HB_NO_BEYOND_64K
static hb_face_t *
create_glyf_face (hb_bool_t add_GLYF, hb_bool_t add_LOCA)
{
static const char maxp_data[] =
"\x00\x00\x50\x00" /* version */
"\x00\x01" /* numGlyphs */
;
static const char head_data[54] =
"\x00\x01\x00\x00" /* version */
"\x00\x00\x00\x00" /* fontRevision */
"\x00\x00\x00\x00" /* checkSumAdjustment */
"\x5F\x0F\x3C\xF5" /* magicNumber */
"\x00\x00" /* flags */
"\x03\xE8" /* unitsPerEm */
"\x00\x00\x00\x00\x00\x00\x00\x00" /* created */
"\x00\x00\x00\x00\x00\x00\x00\x00" /* modified */
"\x00\x00\x00\x00\x00\x00\x00\x00" /* bounds */
"\x00\x00" /* macStyle */
"\x00\x00" /* lowestRecPPEM */
"\x00\x00" /* fontDirectionHint */
"\x00\x01" /* indexToLocFormat */
"\x00\x00" /* glyphDataFormat */
;
static const char loca_data[8] =
"\x00\x00\x00\x00"
"\x00\x00\x00\x15"
;
static const char glyf_data[21] =
"\x00\x01" /* numberOfContours */
"\x00\x01\x00\x02\x00\x04\x00\x06" /* bounds */
"\x00\x02" /* endPtsOfContours */
"\x00\x00" /* instructionLength */
"\x31\xB6\x96" /* flags */
"\x0A\x0A" /* xCoordinates */
"\x0A\x0A" /* yCoordinates */
;
static const char GLYF_data[21] =
"\x00\x01" /* numberOfContours */
"\x00\x0B\x00\x0C\x00\x18\x00\x1A" /* bounds */
"\x00\x02" /* endPtsOfContours */
"\x00\x00" /* instructionLength */
"\x31\xB6\x96" /* flags */
"\x0A\x0A" /* xCoordinates */
"\x0A\x0A" /* yCoordinates */
;
hb_face_t *face = hb_face_builder_create ();
HB_FACE_ADD_TABLE (face, "maxp", maxp_data);
HB_FACE_ADD_TABLE (face, "head", head_data);
HB_FACE_ADD_TABLE (face, "glyf", glyf_data);
HB_FACE_ADD_TABLE (face, "loca", loca_data);
if (add_GLYF)
HB_FACE_ADD_TABLE (face, "GLYF", GLYF_data);
if (add_LOCA)
HB_FACE_ADD_TABLE (face, "LOCA", loca_data);
return face;
}
static void
cubic_to (hb_draw_funcs_t *draw_funcs HB_UNUSED,
void *draw_data,
hb_draw_state_t *draw_state HB_UNUSED,
float control1_x HB_UNUSED, float control1_y HB_UNUSED,
float control2_x HB_UNUSED, float control2_y HB_UNUSED,
float to_x HB_UNUSED, float to_y HB_UNUSED,
void *user_data HB_UNUSED)
{
(*(unsigned int *) draw_data)++;
}
static void
quadratic_to (hb_draw_funcs_t *draw_funcs HB_UNUSED,
void *draw_data HB_UNUSED,
hb_draw_state_t *draw_state HB_UNUSED,
float control_x HB_UNUSED, float control_y HB_UNUSED,
float to_x HB_UNUSED, float to_y HB_UNUSED,
void *user_data HB_UNUSED)
{}
static void
move_to (hb_draw_funcs_t *draw_funcs HB_UNUSED,
void *draw_data,
hb_draw_state_t *draw_state HB_UNUSED,
float to_x HB_UNUSED, float to_y HB_UNUSED,
void *user_data HB_UNUSED)
{
(*(unsigned int *) draw_data)++;
}
static void
test_GLYF_and_LOCA (void)
{
hb_glyph_extents_t extents;
hb_draw_funcs_t *draw_funcs = hb_draw_funcs_create ();
hb_face_t *face;
hb_font_t *font;
unsigned int cubic_count;
hb_draw_funcs_set_cubic_to_func (draw_funcs, cubic_to, NULL, NULL);
hb_draw_funcs_set_quadratic_to_func (draw_funcs, quadratic_to, NULL, NULL);
face = create_glyf_face (TRUE, TRUE);
g_assert_cmpuint (hb_face_get_glyph_count (face), ==, 1);
font = hb_font_create (face);
hb_face_destroy (face);
g_assert_true (hb_font_get_glyph_extents (font, 0, &extents));
g_assert_cmpint (extents.width, ==, 13);
cubic_count = 0;
hb_font_draw_glyph (font, 0, draw_funcs, &cubic_count);
g_assert_cmpuint (cubic_count, ==, 1);
hb_font_destroy (font);
face = create_glyf_face (TRUE, FALSE);
font = hb_font_create (face);
hb_face_destroy (face);
g_assert_true (hb_font_get_glyph_extents (font, 0, &extents));
g_assert_cmpint (extents.width, ==, 3);
cubic_count = 0;
hb_font_draw_glyph (font, 0, draw_funcs, &cubic_count);
g_assert_cmpuint (cubic_count, ==, 0);
hb_font_destroy (font);
face = create_glyf_face (FALSE, TRUE);
font = hb_font_create (face);
hb_face_destroy (face);
g_assert_true (hb_font_get_glyph_extents (font, 0, &extents));
g_assert_cmpint (extents.width, ==, 3);
hb_font_destroy (font);
hb_draw_funcs_destroy (draw_funcs);
}
#ifndef HB_NO_CUBIC_GLYF
static void
record_cubic_move (hb_draw_funcs_t *draw_funcs HB_UNUSED,
void *draw_data,
hb_draw_state_t *draw_state HB_UNUSED,
float x, float y,
void *user_data HB_UNUSED)
{
g_string_append_printf (draw_data, "M%g,%g;", x, y);
}
static void
record_cubic (hb_draw_funcs_t *draw_funcs HB_UNUSED,
void *draw_data,
hb_draw_state_t *draw_state HB_UNUSED,
float c1x, float c1y, float c2x, float c2y, float x, float y,
void *user_data HB_UNUSED)
{
g_string_append_printf (draw_data, "C%g,%g,%g,%g,%g,%g;", c1x, c1y, c2x, c2y, x, y);
}
static void
record_cubic_close (hb_draw_funcs_t *draw_funcs HB_UNUSED,
void *draw_data,
hb_draw_state_t *draw_state HB_UNUSED,
void *user_data HB_UNUSED)
{
g_string_append (draw_data, "Z;");
}
static void
test_GLYF_cubic_contours (void)
{
static const char all_off[] =
"\x00\x01\x00\x00\x00\x00\x00\x64\x00\x64"
"\x00\x03\x00\x00\xB0\xB2\xB4\xA2\x64\x64\x64";
static const char two_contours[] =
"\x00\x02\x00\x00\x00\x00\x01\x2C\x00\x64"
"\x00\x03\x00\x07\x00\x00\xB0\xB2\xB4\xA2"
"\x96\xB2\xB4\xA2\x64\x64\xC8\x64\x64\x64\x64\x64";
static const char two_controls[] =
"\x00\x01\x00\x00\x00\x00\x00\x64\x00\x64"
"\x00\x01\x00\x00\xB0\xB6\x64\x64";
const struct {
const char *data;
unsigned length, points, contours, width;
const char *path;
} cases[] = {
{all_off, sizeof (all_off) - 1, 4, 1, 100,
"M100,50;C100,100,0,100,0,50;C0,0,100,0,100,50;Z;"},
{two_contours, sizeof (two_contours) - 1, 8, 2, 300,
"M100,50;C100,100,0,100,0,50;C0,0,100,0,100,50;Z;"
"M300,50;C300,100,200,100,200,50;C200,0,300,0,300,50;Z;"},
{two_controls, sizeof (two_controls) - 1, 2, 1, 100,
"M50,50;C0,0,100,100,50,50;Z;"},
};
hb_draw_funcs_t *draw_funcs = hb_draw_funcs_create ();
hb_draw_funcs_set_move_to_func (draw_funcs, record_cubic_move, NULL, NULL);
hb_draw_funcs_set_cubic_to_func (draw_funcs, record_cubic, NULL, NULL);
hb_draw_funcs_set_quadratic_to_func (draw_funcs, quadratic_to, NULL, NULL);
hb_draw_funcs_set_close_path_func (draw_funcs, record_cubic_close, NULL, NULL);
for (unsigned i = 0; i < G_N_ELEMENTS (cases); i++)
{
/* Current OFF uppercase companion tables. */
char MAXP_data[33] = {0};
char head_data[54] = {0};
char HHEA_data[38] = {0};
char HMTX_data[4] = {0};
char LOCA_data[8] = {0};
write_u32 (MAXP_data, 0, 0x00010000);
MAXP_data[6] = 1; /* numGlyphs: uint24 */
write_u16 (MAXP_data, 7, cases[i].points);
write_u16 (MAXP_data, 9, cases[i].contours);
write_u16 (MAXP_data, 15, 2); /* maxZones */
write_u32 (head_data, 0, 0x00010000);
write_u32 (head_data, 12, 0x5F0F3CF5);
write_u16 (head_data, 18, 1000);
write_u16 (head_data, 40, cases[i].width);
write_u16 (head_data, 42, 100);
write_u16 (head_data, 50, 1); /* long LOCA offsets */
write_u32 (HHEA_data, 0, 0x00010000);
write_u16 (HHEA_data, 4, 800);
write_u16 (HHEA_data, 6, (unsigned) -200);
write_u16 (HHEA_data, 10, 600);
write_u16 (HHEA_data, 14, 600 - cases[i].width);
write_u16 (HHEA_data, 16, cases[i].width);
write_u16 (HHEA_data, 18, 1);
write_u32 (HHEA_data, 34, 1); /* numberOfHMetrics: uint32 */
write_u16 (HMTX_data, 0, 600);
write_u32 (LOCA_data, 4, cases[i].length);
hb_face_t *face = hb_face_builder_create ();
HB_FACE_ADD_TABLE (face, "MAXP", MAXP_data);
HB_FACE_ADD_TABLE (face, "head", head_data);
HB_FACE_ADD_TABLE (face, "HHEA", HHEA_data);
HB_FACE_ADD_TABLE (face, "HMTX", HMTX_data);
HB_FACE_ADD_TABLE (face, "LOCA", LOCA_data);
hb_blob_t *blob = hb_blob_create (cases[i].data, cases[i].length,
HB_MEMORY_MODE_READONLY, NULL, NULL);
hb_face_builder_add_table (face, HB_TAG ('G','L','Y','F'), blob);
hb_blob_destroy (blob);
g_assert_cmpuint (hb_face_get_glyph_count (face), ==, 1);
hb_font_t *font = hb_font_create (face);
GString *path = g_string_new (NULL);
hb_font_draw_glyph (font, 0, draw_funcs, path);
g_assert_cmpstr (path->str, ==, cases[i].path);
g_string_free (path, TRUE);
hb_font_destroy (font);
hb_face_destroy (face);
}
hb_draw_funcs_destroy (draw_funcs);
}
#endif
static hb_face_t *
create_composite_face (hb_bool_t extended)
{
static const char maxp_data[] =
"\x00\x00\x50\x00" /* version */
"\x00\x02" /* numGlyphs */
;
static const char head_data[54] =
"\x00\x01\x00\x00" /* version */
"\x00\x00\x00\x00" /* fontRevision */
"\x00\x00\x00\x00" /* checkSumAdjustment */
"\x5F\x0F\x3C\xF5" /* magicNumber */
"\x00\x00" /* flags */
"\x03\xE8" /* unitsPerEm */
"\x00\x00\x00\x00\x00\x00\x00\x00" /* created */
"\x00\x00\x00\x00\x00\x00\x00\x00" /* modified */
"\x00\x00\x00\x00\x00\x00\x00\x00" /* bounds */
"\x00\x00" /* macStyle */
"\x00\x00" /* lowestRecPPEM */
"\x00\x00" /* fontDirectionHint */
"\x00\x01" /* indexToLocFormat */
"\x00\x00" /* glyphDataFormat */
;
static const char loca_data[12] =
"\x00\x00\x00\x00"
"\x00\x00\x00\x11"
"\x00\x00\x00\x26"
;
static const char glyf_data[38] =
"\xFF\xFF" /* numberOfContours */
"\x00\x00\x00\x00\x00\x14\x00\x14" /* bounds */
"\x20\x02" /* GID_IS_24BIT | ARGS_ARE_XY_VALUES */
"\x00\x00\x01" /* glyphIndex */
"\x00\x00" /* arguments */
"\x00\x01" /* numberOfContours */
"\x00\x00\x00\x00\x00\x14\x00\x14" /* bounds */
"\x00\x02" /* endPtsOfContours */
"\x00\x00" /* instructionLength */
"\x31\xB6\x96" /* flags */
"\x0A\x0A" /* xCoordinates */
"\x0A\x0A" /* yCoordinates */
;
hb_face_t *face = hb_face_builder_create ();
HB_FACE_ADD_TABLE (face, "maxp", maxp_data);
HB_FACE_ADD_TABLE (face, "head", head_data);
HB_FACE_ADD_TABLE (face, "glyf", glyf_data);
HB_FACE_ADD_TABLE (face, "loca", loca_data);
if (extended)
{
HB_FACE_ADD_TABLE (face, "GLYF", glyf_data);
HB_FACE_ADD_TABLE (face, "LOCA", loca_data);
}
return face;
}
static void
test_GLYF_composite_gid (void)
{
hb_draw_funcs_t *draw_funcs = hb_draw_funcs_create ();
unsigned int move_count;
hb_draw_funcs_set_move_to_func (draw_funcs, move_to, NULL, NULL);
hb_face_t *face = create_composite_face (TRUE);
hb_font_t *font = hb_font_create (face);
hb_face_destroy (face);
move_count = 0;
hb_font_draw_glyph (font, 0, draw_funcs, &move_count);
g_assert_cmpuint (move_count, >, 0);
hb_font_destroy (font);
face = create_composite_face (FALSE);
font = hb_font_create (face);
hb_face_destroy (face);
move_count = 0;
hb_font_draw_glyph (font, 0, draw_funcs, &move_count);
g_assert_cmpuint (move_count, ==, 0);
hb_font_destroy (font);
hb_draw_funcs_destroy (draw_funcs);
}
#endif
int
main (int argc, char **argv)
{
hb_test_init (&argc, &argv);
hb_test_add (test_maxp_and_loca);
hb_test_add (test_extended_ttc);
#ifndef HB_NO_BEYOND_64K
hb_test_add (test_GLYF_and_LOCA);
hb_test_add (test_GLYF_composite_gid);
#ifndef HB_NO_CUBIC_GLYF
hb_test_add (test_GLYF_cubic_contours);
#endif
#endif
return hb_test_run();
}