From f14d1b64c2d148ffdec95c17adbee3f185d5d436 Mon Sep 17 00:00:00 2001 From: Garret Rieger Date: Tue, 27 Feb 2018 13:49:00 -0800 Subject: [PATCH 1/4] [subset] when dropping hints, clear hint related fields in maxp. --- src/hb-ot-maxp-table.hh | 43 ++++++++++++++++++++++++++++++++++--- test/api/test-subset-glyf.c | 30 ++++++++++++++++++++------ 2 files changed, 63 insertions(+), 10 deletions(-) diff --git a/src/hb-ot-maxp-table.hh b/src/hb-ot-maxp-table.hh index 129292296..ea6b4f048 100644 --- a/src/hb-ot-maxp-table.hh +++ b/src/hb-ot-maxp-table.hh @@ -57,7 +57,7 @@ struct maxp { TRACE_SANITIZE (this); return_trace (c->check_struct (this) && - likely (version.major == 1 || + likely ((version.major == 1 && c->check_range (this, sizeof (maxp))) || (version.major == 0 && version.minor == 0x5000u))); } @@ -73,19 +73,56 @@ struct maxp OT::maxp *maxp_prime = (OT::maxp *) hb_blob_get_data (maxp_prime_blob, nullptr); maxp_prime->set_num_glyphs (plan->gids_to_retain_sorted.len); + if (plan->drop_hints) + drop_hint_fields (plan, maxp_prime); bool result = hb_subset_plan_add_table(plan, HB_OT_TAG_maxp, maxp_prime_blob); hb_blob_destroy (maxp_prime_blob); return result; } - /* We only implement version 0.5 as none of the extra fields in version 1.0 are useful. */ + static inline void drop_hint_fields (hb_subset_plan_t *plan, OT::maxp *maxp_prime) + { + if (maxp_prime->version.major == 1) + { + maxp_prime->version_1_data.maxZones.set (1); + maxp_prime->version_1_data.maxTwilightPoints.set (0); + maxp_prime->version_1_data.maxStorage.set (0); + maxp_prime->version_1_data.maxFunctionDefs.set (0); + maxp_prime->version_1_data.maxInstructionDefs.set (0); + maxp_prime->version_1_data.maxStackElements.set (0); + maxp_prime->version_1_data.maxSizeOfInstructions.set (0); + } + } + protected: FixedVersion<>version; /* Version of the maxp table (0.5 or 1.0), * 0x00005000u or 0x00010000u. */ HBUINT16 numGlyphs; /* The number of glyphs in the font. */ + + struct + { + HBUINT16 maxPoints; /* Maximum points in a non-composite glyph. */ + HBUINT16 maxContours; /* Maximum contours in a non-composite glyph. */ + HBUINT16 maxCompositePoints; /* Maximum points in a composite glyph. */ + HBUINT16 maxCompositeContours; /* Maximum contours in a composite glyph. */ + HBUINT16 maxZones; /* 1 if instructions do not use the twilight zone (Z0), + or 2 if instructions do use Z0; should be set to 2 in + most cases. */ + HBUINT16 maxTwilightPoints; /* Maximum points used in Z0. */ + HBUINT16 maxStorage; /* Number of Storage Area locations. */ + HBUINT16 maxFunctionDefs; /* Number of FDEFs, equal to the highest function number + 1. */ + HBUINT16 maxInstructionDefs; /* Number of IDEFs. */ + HBUINT16 maxStackElements; /* Maximum stack depth. (This includes Font and CVT + Programs, as well as the instructions for each glyph.) */ + HBUINT16 maxSizeOfInstructions; /* Maximum byte count for glyph instructions. */ + HBUINT16 maxComponentElements; /* Maximum number of components referenced at + "top level" for any composite glyph. */ + HBUINT16 maxComponentDepth; /* Maximum levels of recursion; 1 for simple components. */ + } version_1_data; + public: - DEFINE_SIZE_STATIC (6); + DEFINE_SIZE_MIN (6); }; diff --git a/test/api/test-subset-glyf.c b/test/api/test-subset-glyf.c index f5fdf32d0..df343220c 100644 --- a/test/api/test-subset-glyf.c +++ b/test/api/test-subset-glyf.c @@ -31,14 +31,30 @@ /* Unit tests for hb-subset-glyf.h */ -static void check_maxp_num_glyphs (hb_face_t *face, uint16_t expected_num_glyphs) +static void check_maxp_field (uint8_t *raw_maxp, unsigned int offset, uint16_t expected_value) +{ + uint16_t actual_value = (raw_maxp[offset] << 8) + raw_maxp[offset + 1]; + g_assert_cmpuint(expected_value, ==, actual_value); +} + +static void check_maxp_num_glyphs (hb_face_t *face, uint16_t expected_num_glyphs, bool hints) { hb_blob_t *maxp_blob = hb_face_reference_table (face, HB_TAG ('m','a','x', 'p')); unsigned int maxp_len; uint8_t *raw_maxp = (uint8_t *) hb_blob_get_data(maxp_blob, &maxp_len); - uint16_t num_glyphs = (raw_maxp[4] << 8) + raw_maxp[5]; - g_assert_cmpuint(expected_num_glyphs, ==, num_glyphs); + + check_maxp_field (raw_maxp, 4, expected_num_glyphs); // numGlyphs + if (!hints) + { + check_maxp_field (raw_maxp, 14, 1); // maxZones + check_maxp_field (raw_maxp, 16, 0); // maxTwilightPoints + check_maxp_field (raw_maxp, 18, 0); // maxStorage + check_maxp_field (raw_maxp, 20, 0); // maxFunctionDefs + check_maxp_field (raw_maxp, 22, 0); // maxInstructionDefs + check_maxp_field (raw_maxp, 24, 0); // maxStackElements + check_maxp_field (raw_maxp, 26, 0); // maxSizeOfInstructions + } hb_blob_destroy (maxp_blob); } @@ -57,7 +73,7 @@ test_subset_glyf (void) hb_subset_test_check (face_ac, face_abc_subset, HB_TAG ('g','l','y','f')); hb_subset_test_check (face_ac, face_abc_subset, HB_TAG ('l','o','c', 'a')); - check_maxp_num_glyphs(face_abc_subset, 3); + check_maxp_num_glyphs(face_abc_subset, 3, true); hb_face_destroy (face_abc_subset); hb_face_destroy (face_abc); @@ -77,7 +93,7 @@ test_subset_glyf_with_components (void) hb_subset_test_check (face_subset, face_generated_subset, HB_TAG ('g','l','y','f')); hb_subset_test_check (face_subset, face_generated_subset, HB_TAG ('l','o','c', 'a')); - check_maxp_num_glyphs(face_generated_subset, 4); + check_maxp_num_glyphs(face_generated_subset, 4, true); hb_face_destroy (face_generated_subset); hb_face_destroy (face_subset); @@ -98,7 +114,7 @@ test_subset_glyf_noop (void) hb_subset_test_check (face_abc, face_abc_subset, HB_TAG ('g','l','y','f')); hb_subset_test_check (face_abc, face_abc_subset, HB_TAG ('l','o','c', 'a')); - check_maxp_num_glyphs(face_abc_subset, 4); + check_maxp_num_glyphs(face_abc_subset, 4, true); hb_face_destroy (face_abc_subset); hb_face_destroy (face_abc); @@ -120,7 +136,7 @@ test_subset_glyf_strip_hints (void) hb_subset_test_check (face_ac, face_abc_subset, HB_TAG ('l','o','c', 'a')); hb_subset_test_check (face_ac, face_abc_subset, HB_TAG ('g','l','y','f')); - check_maxp_num_glyphs(face_abc_subset, 3); + check_maxp_num_glyphs(face_abc_subset, 3, false); hb_face_destroy (face_abc_subset); hb_face_destroy (face_abc); From a76344da796670f33b2fa48a9b1676545dac93c2 Mon Sep 17 00:00:00 2001 From: Garret Rieger Date: Tue, 27 Feb 2018 14:11:36 -0800 Subject: [PATCH 2/4] [subset] Add maxpV1Tail instead of defining a struct inside maxp for v1 data. --- src/hb-ot-maxp-table.hh | 81 +++++++++++++++++++++++++---------------- 1 file changed, 49 insertions(+), 32 deletions(-) diff --git a/src/hb-ot-maxp-table.hh b/src/hb-ot-maxp-table.hh index ea6b4f048..c87274644 100644 --- a/src/hb-ot-maxp-table.hh +++ b/src/hb-ot-maxp-table.hh @@ -39,6 +39,36 @@ namespace OT { #define HB_OT_TAG_maxp HB_TAG('m','a','x','p') +struct maxpV1Tail +{ + inline bool sanitize (hb_sanitize_context_t *c) const + { + TRACE_SANITIZE (this); + return_trace (c->check_struct (this)); + } + + HBUINT16 maxPoints; /* Maximum points in a non-composite glyph. */ + HBUINT16 maxContours; /* Maximum contours in a non-composite glyph. */ + HBUINT16 maxCompositePoints; /* Maximum points in a composite glyph. */ + HBUINT16 maxCompositeContours; /* Maximum contours in a composite glyph. */ + HBUINT16 maxZones; /* 1 if instructions do not use the twilight zone (Z0), + or 2 if instructions do use Z0; should be set to 2 in + most cases. */ + HBUINT16 maxTwilightPoints; /* Maximum points used in Z0. */ + HBUINT16 maxStorage; /* Number of Storage Area locations. */ + HBUINT16 maxFunctionDefs; /* Number of FDEFs, equal to the highest function number + 1. */ + HBUINT16 maxInstructionDefs; /* Number of IDEFs. */ + HBUINT16 maxStackElements; /* Maximum stack depth. (This includes Font and CVT + Programs, as well as the instructions for each glyph.) */ + HBUINT16 maxSizeOfInstructions; /* Maximum byte count for glyph instructions. */ + HBUINT16 maxComponentElements; /* Maximum number of components referenced at + "top level" for any composite glyph. */ + HBUINT16 maxComponentDepth; /* Maximum levels of recursion; 1 for simple components. */ + + DEFINE_SIZE_STATIC (26); +}; + + struct maxp { static const hb_tag_t tableTag = HB_OT_TAG_maxp; @@ -56,9 +86,15 @@ struct maxp inline bool sanitize (hb_sanitize_context_t *c) const { TRACE_SANITIZE (this); - return_trace (c->check_struct (this) && - likely ((version.major == 1 && c->check_range (this, sizeof (maxp))) || - (version.major == 0 && version.minor == 0x5000u))); + if (unlikely (!c->check_struct (this))) + return_trace (false); + + if (version.major == 1) + { + const maxpV1Tail &v1 = StructAfter (*this); + return v1.sanitize (c); + } + return_trace (likely (version.major == 0 && version.minor == 0x5000u)); } inline bool subset (hb_subset_plan_t *plan) const @@ -83,15 +119,16 @@ struct maxp static inline void drop_hint_fields (hb_subset_plan_t *plan, OT::maxp *maxp_prime) { + maxpV1Tail &v1 = StructAfter (*maxp_prime); if (maxp_prime->version.major == 1) { - maxp_prime->version_1_data.maxZones.set (1); - maxp_prime->version_1_data.maxTwilightPoints.set (0); - maxp_prime->version_1_data.maxStorage.set (0); - maxp_prime->version_1_data.maxFunctionDefs.set (0); - maxp_prime->version_1_data.maxInstructionDefs.set (0); - maxp_prime->version_1_data.maxStackElements.set (0); - maxp_prime->version_1_data.maxSizeOfInstructions.set (0); + v1.maxZones.set (1); + v1.maxTwilightPoints.set (0); + v1.maxStorage.set (0); + v1.maxFunctionDefs.set (0); + v1.maxInstructionDefs.set (0); + v1.maxStackElements.set (0); + v1.maxSizeOfInstructions.set (0); } } @@ -99,30 +136,10 @@ struct maxp FixedVersion<>version; /* Version of the maxp table (0.5 or 1.0), * 0x00005000u or 0x00010000u. */ HBUINT16 numGlyphs; /* The number of glyphs in the font. */ - - struct - { - HBUINT16 maxPoints; /* Maximum points in a non-composite glyph. */ - HBUINT16 maxContours; /* Maximum contours in a non-composite glyph. */ - HBUINT16 maxCompositePoints; /* Maximum points in a composite glyph. */ - HBUINT16 maxCompositeContours; /* Maximum contours in a composite glyph. */ - HBUINT16 maxZones; /* 1 if instructions do not use the twilight zone (Z0), - or 2 if instructions do use Z0; should be set to 2 in - most cases. */ - HBUINT16 maxTwilightPoints; /* Maximum points used in Z0. */ - HBUINT16 maxStorage; /* Number of Storage Area locations. */ - HBUINT16 maxFunctionDefs; /* Number of FDEFs, equal to the highest function number + 1. */ - HBUINT16 maxInstructionDefs; /* Number of IDEFs. */ - HBUINT16 maxStackElements; /* Maximum stack depth. (This includes Font and CVT - Programs, as well as the instructions for each glyph.) */ - HBUINT16 maxSizeOfInstructions; /* Maximum byte count for glyph instructions. */ - HBUINT16 maxComponentElements; /* Maximum number of components referenced at - "top level" for any composite glyph. */ - HBUINT16 maxComponentDepth; /* Maximum levels of recursion; 1 for simple components. */ - } version_1_data; +/*maxpV1Tail v1Tail; */ public: - DEFINE_SIZE_MIN (6); + DEFINE_SIZE_STATIC (6); }; From b26db48bfff804e139331817a86ecc811547afcd Mon Sep 17 00:00:00 2001 From: Garret Rieger Date: Tue, 27 Feb 2018 14:14:05 -0800 Subject: [PATCH 3/4] [subset] whitespace for hb-ot-maxp-table.hh --- src/hb-ot-maxp-table.hh | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/src/hb-ot-maxp-table.hh b/src/hb-ot-maxp-table.hh index c87274644..77aa4e514 100644 --- a/src/hb-ot-maxp-table.hh +++ b/src/hb-ot-maxp-table.hh @@ -47,23 +47,23 @@ struct maxpV1Tail return_trace (c->check_struct (this)); } - HBUINT16 maxPoints; /* Maximum points in a non-composite glyph. */ - HBUINT16 maxContours; /* Maximum contours in a non-composite glyph. */ - HBUINT16 maxCompositePoints; /* Maximum points in a composite glyph. */ + HBUINT16 maxPoints; /* Maximum points in a non-composite glyph. */ + HBUINT16 maxContours; /* Maximum contours in a non-composite glyph. */ + HBUINT16 maxCompositePoints; /* Maximum points in a composite glyph. */ HBUINT16 maxCompositeContours; /* Maximum contours in a composite glyph. */ - HBUINT16 maxZones; /* 1 if instructions do not use the twilight zone (Z0), - or 2 if instructions do use Z0; should be set to 2 in - most cases. */ - HBUINT16 maxTwilightPoints; /* Maximum points used in Z0. */ - HBUINT16 maxStorage; /* Number of Storage Area locations. */ - HBUINT16 maxFunctionDefs; /* Number of FDEFs, equal to the highest function number + 1. */ - HBUINT16 maxInstructionDefs; /* Number of IDEFs. */ - HBUINT16 maxStackElements; /* Maximum stack depth. (This includes Font and CVT - Programs, as well as the instructions for each glyph.) */ + HBUINT16 maxZones; /* 1 if instructions do not use the twilight zone (Z0), + or 2 if instructions do use Z0; should be set to 2 in + most cases. */ + HBUINT16 maxTwilightPoints; /* Maximum points used in Z0. */ + HBUINT16 maxStorage; /* Number of Storage Area locations. */ + HBUINT16 maxFunctionDefs; /* Number of FDEFs, equal to the highest function number + 1. */ + HBUINT16 maxInstructionDefs; /* Number of IDEFs. */ + HBUINT16 maxStackElements; /* Maximum stack depth. (This includes Font and CVT + Programs, as well as the instructions for each glyph.) */ HBUINT16 maxSizeOfInstructions; /* Maximum byte count for glyph instructions. */ HBUINT16 maxComponentElements; /* Maximum number of components referenced at - "top level" for any composite glyph. */ - HBUINT16 maxComponentDepth; /* Maximum levels of recursion; 1 for simple components. */ + "top level" for any composite glyph. */ + HBUINT16 maxComponentDepth; /* Maximum levels of recursion; 1 for simple components. */ DEFINE_SIZE_STATIC (26); }; @@ -71,7 +71,7 @@ struct maxpV1Tail struct maxp { - static const hb_tag_t tableTag = HB_OT_TAG_maxp; + static const hb_tag_t tableTag = HB_OT_TAG_maxp; inline unsigned int get_num_glyphs (void) const { From 921b65cbc29f0c2c9c13e9fb272dafe4c8a14591 Mon Sep 17 00:00:00 2001 From: Garret Rieger Date: Tue, 27 Feb 2018 14:37:18 -0800 Subject: [PATCH 4/4] [subset] minor fixes in hb-ot-maxp-table.hh --- src/hb-ot-maxp-table.hh | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/src/hb-ot-maxp-table.hh b/src/hb-ot-maxp-table.hh index 77aa4e514..881dedad5 100644 --- a/src/hb-ot-maxp-table.hh +++ b/src/hb-ot-maxp-table.hh @@ -52,19 +52,19 @@ struct maxpV1Tail HBUINT16 maxCompositePoints; /* Maximum points in a composite glyph. */ HBUINT16 maxCompositeContours; /* Maximum contours in a composite glyph. */ HBUINT16 maxZones; /* 1 if instructions do not use the twilight zone (Z0), - or 2 if instructions do use Z0; should be set to 2 in - most cases. */ + * or 2 if instructions do use Z0; should be set to 2 in + * most cases. */ HBUINT16 maxTwilightPoints; /* Maximum points used in Z0. */ HBUINT16 maxStorage; /* Number of Storage Area locations. */ HBUINT16 maxFunctionDefs; /* Number of FDEFs, equal to the highest function number + 1. */ HBUINT16 maxInstructionDefs; /* Number of IDEFs. */ HBUINT16 maxStackElements; /* Maximum stack depth. (This includes Font and CVT - Programs, as well as the instructions for each glyph.) */ + * Programs, as well as the instructions for each glyph.) */ HBUINT16 maxSizeOfInstructions; /* Maximum byte count for glyph instructions. */ HBUINT16 maxComponentElements; /* Maximum number of components referenced at - "top level" for any composite glyph. */ + * "top level" for any composite glyph. */ HBUINT16 maxComponentDepth; /* Maximum levels of recursion; 1 for simple components. */ - + public: DEFINE_SIZE_STATIC (26); }; @@ -119,9 +119,9 @@ struct maxp static inline void drop_hint_fields (hb_subset_plan_t *plan, OT::maxp *maxp_prime) { - maxpV1Tail &v1 = StructAfter (*maxp_prime); if (maxp_prime->version.major == 1) { + maxpV1Tail &v1 = StructAfter (*maxp_prime); v1.maxZones.set (1); v1.maxTwilightPoints.set (0); v1.maxStorage.set (0); @@ -136,8 +136,7 @@ struct maxp FixedVersion<>version; /* Version of the maxp table (0.5 or 1.0), * 0x00005000u or 0x00010000u. */ HBUINT16 numGlyphs; /* The number of glyphs in the font. */ -/*maxpV1Tail v1Tail; */ - +/*maxpV1Tail v1Tail[VAR]; */ public: DEFINE_SIZE_STATIC (6); };