From dce42cef2b769379a0690053da0e7467ff086195 Mon Sep 17 00:00:00 2001 From: Ebrahim Byagowi Date: Tue, 27 Aug 2019 14:32:05 +0430 Subject: [PATCH] [glyf] Move GlyphHeader::from_bytes to hb_bytes_t, introduce .as () --- src/hb-array.hh | 6 ++++++ src/hb-ot-glyf-table.hh | 24 +++++++++--------------- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/hb-array.hh b/src/hb-array.hh index 4c0f73e2d..2e7a4fa21 100644 --- a/src/hb-array.hh +++ b/src/hb-array.hh @@ -181,6 +181,12 @@ struct hb_array_t : hb_iter_with_fallback_t, Type&> hb_array_t sub_array (unsigned int start_offset, unsigned int seg_count) const { return sub_array (start_offset, &seg_count); } + template + const T *as () const + { return length < hb_null_size (T) ? &Null (T) : reinterpret_cast (arrayZ); } + /* Only call if you allocated the underlying array using malloc() or similar. */ void free () { ::free ((void *) arrayZ); arrayZ = nullptr; length = 0; } diff --git a/src/hb-ot-glyf-table.hh b/src/hb-ot-glyf-table.hh index c667291d2..3dc3e27c8 100644 --- a/src/hb-ot-glyf-table.hh +++ b/src/hb-ot-glyf-table.hh @@ -230,7 +230,7 @@ struct glyf static void _zero_instruction_length (hb_bytes_t glyph) { - auto &glyph_header = GlyphHeader::from_bytes (glyph); + const GlyphHeader &glyph_header = *glyph.as (); if (!glyph_header.is_simple_glyph ()) return; // only for simple glyphs unsigned int instruction_len_offset = glyph_header.simple_instruction_len_offset (); @@ -241,7 +241,7 @@ struct glyf static bool _remove_composite_instruction_flag (hb_bytes_t glyph) { - auto &glyph_header = GlyphHeader::from_bytes (glyph); + const GlyphHeader &glyph_header = *glyph.as (); if (!glyph_header.is_composite_glyph ()) return true; // only for composites /* remove WE_HAVE_INSTRUCTIONS from flags in dest */ @@ -279,12 +279,6 @@ struct glyf struct GlyphHeader { - static const GlyphHeader &from_bytes (hb_bytes_t glyph_bytes) - { - return likely (glyph_bytes.length >= static_size) - ? StructAtOffset (&glyph_bytes, 0) : Null (GlyphHeader); - } - unsigned int simple_instruction_len_offset () const { return static_size + 2 * numberOfContours; } @@ -389,7 +383,7 @@ struct glyf unsigned int length, CompositeGlyphHeader::Iterator *iterator /* OUT */) { - auto &glyph_header = GlyphHeader::from_bytes (hb_bytes_t (glyph_data, length)); + const GlyphHeader &glyph_header = *hb_bytes_t (glyph_data, length).as (); if (!glyph_header.has_data ()) return false; /* Empty glyph; zero extents. */ if (glyph_header.is_composite_glyph ()) @@ -473,7 +467,7 @@ struct glyf { const char *glyph = ((const char *) glyf_table) + start_offset; unsigned int glyph_length = *end_offset - start_offset; - auto &glyph_header = GlyphHeader::from_bytes (hb_bytes_t (glyph, glyph_length)); + const GlyphHeader &glyph_header = *hb_bytes_t (glyph, glyph_length).as (); if (!glyph_header.has_data ()) return true; const char *glyph_end = glyph + glyph_length; @@ -570,7 +564,7 @@ struct glyf bool get_instruction_length (hb_bytes_t glyph, unsigned int * length /* OUT */) const { - auto &glyph_header = GlyphHeader::from_bytes (glyph); + const GlyphHeader &glyph_header = *glyph.as (); /* Empty glyph; no instructions. */ if (!glyph_header.has_data ()) { @@ -634,8 +628,8 @@ struct glyf if (!get_offsets (glyph, &start_offset, &end_offset)) return false; - GlyphHeader::from_bytes (hb_bytes_t ((const char *) glyf_table + start_offset, - end_offset - start_offset)).get_extents (extents); + hb_bytes_t ((const char *) glyf_table + start_offset, + end_offset - start_offset).as ()->get_extents (extents); return true; } @@ -649,7 +643,7 @@ struct glyf return hb_bytes_t (); } hb_bytes_t glyph_bytes = hb_bytes_t (glyf + start_offset, end_offset - start_offset); - if (!GlyphHeader::from_bytes (glyph_bytes).has_data ()) + if (!glyph_bytes.as ()->has_data ()) { DEBUG_MSG (SUBSET, nullptr, "Empty or invalid glyph size, %d", gid); return hb_bytes_t (); @@ -716,7 +710,7 @@ struct glyf return ; } - const GlyphHeader& header = GlyphHeader::from_bytes (source_glyph); + const GlyphHeader& header = *source_glyph.as (); DEBUG_MSG (SUBSET, nullptr, "new_gid %d drop %d instruction bytes " "from %d byte source glyph", new_gid, instruction_len, source_glyph.length);