[glyf] Reapply use of hb_bytes_t.as<GlyphHeader> ()

This commit is contained in:
Ebrahim Byagowi 2019-08-29 20:33:36 +04:30
parent 9e80fc6836
commit 8106ba7f88
1 changed files with 18 additions and 19 deletions

View File

@ -234,7 +234,7 @@ struct glyf
static void static void
_zero_instruction_length (hb_bytes_t glyph) _zero_instruction_length (hb_bytes_t glyph)
{ {
const GlyphHeader &glyph_header = StructAtOffset<GlyphHeader> (&glyph, 0); const GlyphHeader &glyph_header = *glyph.as<GlyphHeader> ();
int16_t num_contours = (int16_t) glyph_header.numberOfContours; int16_t num_contours = (int16_t) glyph_header.numberOfContours;
if (num_contours <= 0) return; // only for simple glyphs if (num_contours <= 0) return; // only for simple glyphs
@ -246,7 +246,7 @@ struct glyf
static bool _remove_composite_instruction_flag (hb_bytes_t glyph) static bool _remove_composite_instruction_flag (hb_bytes_t glyph)
{ {
const GlyphHeader &glyph_header = StructAtOffset<GlyphHeader> (&glyph, 0); const GlyphHeader &glyph_header = *glyph.as<GlyphHeader> ();
if (!glyph_header.is_composite_glyph ()) return true; // only for composites if (!glyph_header.is_composite_glyph ()) return true; // only for composites
/* remove WE_HAVE_INSTRUCTIONS from flags in dest */ /* remove WE_HAVE_INSTRUCTIONS from flags in dest */
@ -293,6 +293,8 @@ struct glyf
bool is_composite_glyph () const { return numberOfContours < 0; } bool is_composite_glyph () const { return numberOfContours < 0; }
bool is_simple_glyph () const { return numberOfContours > 0; } bool is_simple_glyph () const { return numberOfContours > 0; }
bool has_data () const { return numberOfContours; }
HBINT16 numberOfContours; /* If the number of contours is HBINT16 numberOfContours; /* If the number of contours is
* greater than or equal to zero, * greater than or equal to zero,
* this is a simple glyph; if negative, * this is a simple glyph; if negative,
@ -461,10 +463,9 @@ struct glyf
unsigned int length, unsigned int length,
CompositeGlyphHeader::Iterator *iterator /* OUT */) CompositeGlyphHeader::Iterator *iterator /* OUT */)
{ {
if (length < GlyphHeader::static_size) const GlyphHeader &glyph_header = *hb_bytes_t (glyph_data, length).as<GlyphHeader> ();
return false; /* Empty glyph; zero extents. */ if (!glyph_header.has_data ()) return false; /* Empty glyph; zero extents. */
const GlyphHeader &glyph_header = StructAtOffset<GlyphHeader> (glyph_data, 0);
if (glyph_header.is_composite_glyph ()) if (glyph_header.is_composite_glyph ())
{ {
const CompositeGlyphHeader *possible = const CompositeGlyphHeader *possible =
@ -862,18 +863,17 @@ struct glyf
bool remove_padding (unsigned int start_offset, bool remove_padding (unsigned int start_offset,
unsigned int *end_offset) const unsigned int *end_offset) const
{ {
if (*end_offset - start_offset < GlyphHeader::static_size) return true; unsigned int glyph_length = *end_offset - start_offset;
const char *glyph = ((const char *) glyf_table) + start_offset; const char *glyph = ((const char *) glyf_table) + start_offset;
const char * const glyph_end = glyph + (*end_offset - start_offset); const GlyphHeader &glyph_header = *hb_bytes_t (glyph, glyph_length).as<GlyphHeader> ();
const GlyphHeader &glyph_header = StructAtOffset<GlyphHeader> (glyph, 0); if (!glyph_header.has_data ()) return true;
int16_t num_contours = (int16_t) glyph_header.numberOfContours;
if (num_contours < 0) const char *glyph_end = glyph + glyph_length;
if (glyph_header.is_composite_glyph ())
/* Trimming for composites not implemented. /* Trimming for composites not implemented.
* If removing hints it falls out of that. */ * If removing hints it falls out of that. */
return true; return true;
else if (num_contours > 0) else
{ {
/* simple glyph w/contours, possibly trimmable */ /* simple glyph w/contours, possibly trimmable */
glyph += glyph_header.simple_instruction_len_offset (); glyph += glyph_header.simple_instruction_len_offset ();
@ -962,14 +962,14 @@ struct glyf
bool get_instruction_length (hb_bytes_t glyph, bool get_instruction_length (hb_bytes_t glyph,
unsigned int * length /* OUT */) const unsigned int * length /* OUT */) const
{ {
const GlyphHeader &glyph_header = *glyph.as<GlyphHeader> ();
/* Empty glyph; no instructions. */ /* Empty glyph; no instructions. */
if (glyph.length < GlyphHeader::static_size) if (!glyph_header.has_data ())
{ {
*length = 0; *length = 0;
// only 0 byte glyphs are healthy when missing GlyphHeader // only 0 byte glyphs are healthy when missing GlyphHeader
return glyph.length == 0; return glyph.length == 0;
} }
const GlyphHeader &glyph_header = StructAtOffset<GlyphHeader> (&glyph, 0);
int16_t num_contours = (int16_t) glyph_header.numberOfContours; int16_t num_contours = (int16_t) glyph_header.numberOfContours;
if (num_contours < 0) if (num_contours < 0)
{ {
@ -1085,14 +1085,13 @@ struct glyf
DEBUG_MSG(SUBSET, nullptr, "Unable to get offset or remove padding for %d", gid); DEBUG_MSG(SUBSET, nullptr, "Unable to get offset or remove padding for %d", gid);
return hb_bytes_t (); return hb_bytes_t ();
} }
hb_bytes_t glyph = hb_bytes_t (glyf + start_offset, end_offset - start_offset); hb_bytes_t glyph_bytes = hb_bytes_t (glyf + start_offset, end_offset - start_offset);
if (glyph.length == 0) return glyph; if (!glyph_bytes.as<GlyphHeader> ()->has_data ())
if (unlikely (glyph.length < GlyphHeader::static_size))
{ {
DEBUG_MSG(SUBSET, nullptr, "Glyph size smaller than minimum header %d", gid); DEBUG_MSG(SUBSET, nullptr, "Glyph size smaller than minimum header %d", gid);
return hb_bytes_t (); return hb_bytes_t ();
} }
return glyph; return glyph_bytes;
} }
private: private:
@ -1160,7 +1159,7 @@ struct glyf
return; return;
} }
const GlyphHeader& header = StructAtOffset<GlyphHeader> (&source_glyph, 0); const GlyphHeader& header = *source_glyph.as<GlyphHeader> ();
DEBUG_MSG (SUBSET, nullptr, "Unable to read instruction length for new_gid %d", DEBUG_MSG (SUBSET, nullptr, "Unable to read instruction length for new_gid %d",
new_gid); new_gid);
if (header.is_composite_glyph ()) if (header.is_composite_glyph ())