[glyf] Move GlyphHeader::from_bytes to hb_bytes_t, introduce .as<T> ()

This commit is contained in:
Ebrahim Byagowi 2019-08-27 14:32:05 +04:30
parent 6e82d59b4f
commit dce42cef2b
2 changed files with 15 additions and 15 deletions

View File

@ -181,6 +181,12 @@ struct hb_array_t : hb_iter_with_fallback_t<hb_array_t<Type>, Type&>
hb_array_t<Type> sub_array (unsigned int start_offset, unsigned int seg_count) const hb_array_t<Type> sub_array (unsigned int start_offset, unsigned int seg_count) const
{ return sub_array (start_offset, &seg_count); } { return sub_array (start_offset, &seg_count); }
template <typename T,
unsigned P = sizeof (Type),
hb_enable_if (P == 1)>
const T *as () const
{ return length < hb_null_size (T) ? &Null (T) : reinterpret_cast<const T *> (arrayZ); }
/* Only call if you allocated the underlying array using malloc() or similar. */ /* Only call if you allocated the underlying array using malloc() or similar. */
void free () void free ()
{ ::free ((void *) arrayZ); arrayZ = nullptr; length = 0; } { ::free ((void *) arrayZ); arrayZ = nullptr; length = 0; }

View File

@ -230,7 +230,7 @@ struct glyf
static void static void
_zero_instruction_length (hb_bytes_t glyph) _zero_instruction_length (hb_bytes_t glyph)
{ {
auto &glyph_header = GlyphHeader::from_bytes (glyph); const GlyphHeader &glyph_header = *glyph.as<GlyphHeader> ();
if (!glyph_header.is_simple_glyph ()) return; // only for simple glyphs if (!glyph_header.is_simple_glyph ()) return; // only for simple glyphs
unsigned int instruction_len_offset = glyph_header.simple_instruction_len_offset (); 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) static bool _remove_composite_instruction_flag (hb_bytes_t glyph)
{ {
auto &glyph_header = GlyphHeader::from_bytes (glyph); 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 */
@ -279,12 +279,6 @@ struct glyf
struct GlyphHeader struct GlyphHeader
{ {
static const GlyphHeader &from_bytes (hb_bytes_t glyph_bytes)
{
return likely (glyph_bytes.length >= static_size)
? StructAtOffset<GlyphHeader> (&glyph_bytes, 0) : Null (GlyphHeader);
}
unsigned int simple_instruction_len_offset () const unsigned int simple_instruction_len_offset () const
{ return static_size + 2 * numberOfContours; } { return static_size + 2 * numberOfContours; }
@ -389,7 +383,7 @@ struct glyf
unsigned int length, unsigned int length,
CompositeGlyphHeader::Iterator *iterator /* OUT */) 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<GlyphHeader> ();
if (!glyph_header.has_data ()) return false; /* Empty glyph; zero extents. */ if (!glyph_header.has_data ()) return false; /* Empty glyph; zero extents. */
if (glyph_header.is_composite_glyph ()) if (glyph_header.is_composite_glyph ())
@ -473,7 +467,7 @@ struct glyf
{ {
const char *glyph = ((const char *) glyf_table) + start_offset; const char *glyph = ((const char *) glyf_table) + start_offset;
unsigned int glyph_length = *end_offset - 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<GlyphHeader> ();
if (!glyph_header.has_data ()) return true; if (!glyph_header.has_data ()) return true;
const char *glyph_end = glyph + glyph_length; const char *glyph_end = glyph + glyph_length;
@ -570,7 +564,7 @@ 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
{ {
auto &glyph_header = GlyphHeader::from_bytes (glyph); const GlyphHeader &glyph_header = *glyph.as<GlyphHeader> ();
/* Empty glyph; no instructions. */ /* Empty glyph; no instructions. */
if (!glyph_header.has_data ()) if (!glyph_header.has_data ())
{ {
@ -634,8 +628,8 @@ struct glyf
if (!get_offsets (glyph, &start_offset, &end_offset)) if (!get_offsets (glyph, &start_offset, &end_offset))
return false; return false;
GlyphHeader::from_bytes (hb_bytes_t ((const char *) glyf_table + start_offset, hb_bytes_t ((const char *) glyf_table + start_offset,
end_offset - start_offset)).get_extents (extents); end_offset - start_offset).as<GlyphHeader> ()->get_extents (extents);
return true; return true;
} }
@ -649,7 +643,7 @@ struct glyf
return hb_bytes_t (); return hb_bytes_t ();
} }
hb_bytes_t glyph_bytes = 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 (!GlyphHeader::from_bytes (glyph_bytes).has_data ()) if (!glyph_bytes.as<GlyphHeader> ()->has_data ())
{ {
DEBUG_MSG (SUBSET, nullptr, "Empty or invalid glyph size, %d", gid); DEBUG_MSG (SUBSET, nullptr, "Empty or invalid glyph size, %d", gid);
return hb_bytes_t (); return hb_bytes_t ();
@ -716,7 +710,7 @@ struct glyf
return ; return ;
} }
const GlyphHeader& header = GlyphHeader::from_bytes (source_glyph); const GlyphHeader& header = *source_glyph.as<GlyphHeader> ();
DEBUG_MSG (SUBSET, nullptr, "new_gid %d drop %d instruction bytes " DEBUG_MSG (SUBSET, nullptr, "new_gid %d drop %d instruction bytes "
"from %d byte source glyph", "from %d byte source glyph",
new_gid, instruction_len, source_glyph.length); new_gid, instruction_len, source_glyph.length);