Merge pull request #842 from googlefonts/maxp

[subset] when dropping hints, clear hint related fields in maxp.
This commit is contained in:
Garret Rieger 2018-02-27 17:06:27 -07:00 committed by GitHub
commit 4c6023f861
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 81 additions and 12 deletions

View File

@ -39,9 +39,39 @@ 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. */
public:
DEFINE_SIZE_STATIC (26);
};
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
{
@ -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 ||
(version.major == 0 && version.minor == 0x5000u)));
if (unlikely (!c->check_struct (this)))
return_trace (false);
if (version.major == 1)
{
const maxpV1Tail &v1 = StructAfter<maxpV1Tail> (*this);
return v1.sanitize (c);
}
return_trace (likely (version.major == 0 && version.minor == 0x5000u));
}
inline bool subset (hb_subset_plan_t *plan) const
@ -73,17 +109,34 @@ 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)
{
maxpV1Tail &v1 = StructAfter<maxpV1Tail> (*maxp_prime);
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);
}
}
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. */
/*maxpV1Tail v1Tail[VAR]; */
public:
DEFINE_SIZE_STATIC (6);
};

View File

@ -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);