[glyf] Move a few structs out of Glyph{}

This commit is contained in:
Behdad Esfahbod 2022-06-25 17:37:59 -06:00
parent ae75f066b5
commit ef250eea7e
2 changed files with 224 additions and 223 deletions

View File

@ -15,7 +15,7 @@ struct CompositeGlyphChain
enum composite_glyph_flag_t enum composite_glyph_flag_t
{ {
ARG_1_AND_2_ARE_WORDS = 0x0001, ARG_1_AND_2_ARE_WORDS = 0x0001,
ARGS_ARE_XY_VALUES = 0x0002, ARGS_ARE_XY_VALUES = 0x0002,
ROUND_XY_TO_GRID = 0x0004, ROUND_XY_TO_GRID = 0x0004,
WE_HAVE_A_SCALE = 0x0008, WE_HAVE_A_SCALE = 0x0008,
MORE_COMPONENTS = 0x0020, MORE_COMPONENTS = 0x0020,
@ -212,7 +212,38 @@ enum phantom_point_index_t
PHANTOM_COUNT = 4 PHANTOM_COUNT = 4
}; };
struct Glyph struct GlyphHeader
{
bool has_data () const { return numberOfContours; }
template <typename accelerator_t>
bool get_extents (hb_font_t *font, const accelerator_t &glyf_accelerator,
hb_codepoint_t gid, hb_glyph_extents_t *extents) const
{
/* Undocumented rasterizer behavior: shift glyph to the left by (lsb - xMin), i.e., xMin = lsb */
/* extents->x_bearing = hb_min (glyph_header.xMin, glyph_header.xMax); */
extents->x_bearing = font->em_scale_x (glyf_accelerator.hmtx->get_side_bearing (gid));
extents->y_bearing = font->em_scale_y (hb_max (yMin, yMax));
extents->width = font->em_scale_x (hb_max (xMin, xMax) - hb_min (xMin, xMax));
extents->height = font->em_scale_y (hb_min (yMin, yMax) - hb_max (yMin, yMax));
return true;
}
HBINT16 numberOfContours;
/* If the number of contours is
* greater than or equal to zero,
* this is a simple glyph; if negative,
* this is a composite glyph. */
FWORD xMin; /* Minimum x for coordinate data. */
FWORD yMin; /* Minimum y for coordinate data. */
FWORD xMax; /* Maximum x for coordinate data. */
FWORD yMax; /* Maximum y for coordinate data. */
public:
DEFINE_SIZE_STATIC (10);
};
struct SimpleGlyph
{ {
enum simple_glyph_flag_t enum simple_glyph_flag_t
{ {
@ -226,257 +257,227 @@ struct Glyph
FLAG_RESERVED2 = 0x80 FLAG_RESERVED2 = 0x80
}; };
struct GlyphHeader const GlyphHeader &header;
hb_bytes_t bytes;
SimpleGlyph (const GlyphHeader &header_, hb_bytes_t bytes_) :
header (header_), bytes (bytes_) {}
unsigned int instruction_len_offset () const
{ return GlyphHeader::static_size + 2 * header.numberOfContours; }
unsigned int length (unsigned int instruction_len) const
{ return instruction_len_offset () + 2 + instruction_len; }
unsigned int instructions_length () const
{ {
bool has_data () const { return numberOfContours; } unsigned int instruction_length_offset = instruction_len_offset ();
if (unlikely (instruction_length_offset + 2 > bytes.length)) return 0;
template <typename accelerator_t> const HBUINT16 &instructionLength = StructAtOffset<HBUINT16> (&bytes, instruction_length_offset);
bool get_extents (hb_font_t *font, const accelerator_t &glyf_accelerator, /* Out of bounds of the current glyph */
hb_codepoint_t gid, hb_glyph_extents_t *extents) const if (unlikely (length (instructionLength) > bytes.length)) return 0;
{ return instructionLength;
/* Undocumented rasterizer behavior: shift glyph to the left by (lsb - xMin), i.e., xMin = lsb */ }
/* extents->x_bearing = hb_min (glyph_header.xMin, glyph_header.xMax); */
extents->x_bearing = font->em_scale_x (glyf_accelerator.hmtx->get_side_bearing (gid));
extents->y_bearing = font->em_scale_y (hb_max (yMin, yMax));
extents->width = font->em_scale_x (hb_max (xMin, xMax) - hb_min (xMin, xMax));
extents->height = font->em_scale_y (hb_min (yMin, yMax) - hb_max (yMin, yMax));
return true; const hb_bytes_t trim_padding () const
}
HBINT16 numberOfContours;
/* If the number of contours is
* greater than or equal to zero,
* this is a simple glyph; if negative,
* this is a composite glyph. */
FWORD xMin; /* Minimum x for coordinate data. */
FWORD yMin; /* Minimum y for coordinate data. */
FWORD xMax; /* Maximum x for coordinate data. */
FWORD yMax; /* Maximum y for coordinate data. */
public:
DEFINE_SIZE_STATIC (10);
};
struct SimpleGlyph
{ {
const GlyphHeader &header; /* based on FontTools _g_l_y_f.py::trim */
hb_bytes_t bytes; const uint8_t *glyph = (uint8_t*) bytes.arrayZ;
SimpleGlyph (const GlyphHeader &header_, hb_bytes_t bytes_) : const uint8_t *glyph_end = glyph + bytes.length;
header (header_), bytes (bytes_) {} /* simple glyph w/contours, possibly trimmable */
glyph += instruction_len_offset ();
unsigned int instruction_len_offset () const if (unlikely (glyph + 2 >= glyph_end)) return hb_bytes_t ();
{ return GlyphHeader::static_size + 2 * header.numberOfContours; } unsigned int num_coordinates = StructAtOffset<HBUINT16> (glyph - 2, 0) + 1;
unsigned int num_instructions = StructAtOffset<HBUINT16> (glyph, 0);
unsigned int length (unsigned int instruction_len) const glyph += 2 + num_instructions;
{ return instruction_len_offset () + 2 + instruction_len; }
unsigned int instructions_length () const unsigned int coord_bytes = 0;
unsigned int coords_with_flags = 0;
while (glyph < glyph_end)
{ {
unsigned int instruction_length_offset = instruction_len_offset (); uint8_t flag = *glyph;
if (unlikely (instruction_length_offset + 2 > bytes.length)) return 0; glyph++;
const HBUINT16 &instructionLength = StructAtOffset<HBUINT16> (&bytes, instruction_length_offset); unsigned int repeat = 1;
/* Out of bounds of the current glyph */ if (flag & FLAG_REPEAT)
if (unlikely (length (instructionLength) > bytes.length)) return 0;
return instructionLength;
}
const Glyph trim_padding () const
{
/* based on FontTools _g_l_y_f.py::trim */
const uint8_t *glyph = (uint8_t*) bytes.arrayZ;
const uint8_t *glyph_end = glyph + bytes.length;
/* simple glyph w/contours, possibly trimmable */
glyph += instruction_len_offset ();
if (unlikely (glyph + 2 >= glyph_end)) return Glyph ();
unsigned int num_coordinates = StructAtOffset<HBUINT16> (glyph - 2, 0) + 1;
unsigned int num_instructions = StructAtOffset<HBUINT16> (glyph, 0);
glyph += 2 + num_instructions;
unsigned int coord_bytes = 0;
unsigned int coords_with_flags = 0;
while (glyph < glyph_end)
{ {
uint8_t flag = *glyph; if (unlikely (glyph >= glyph_end)) return hb_bytes_t ();
repeat = *glyph + 1;
glyph++; glyph++;
unsigned int repeat = 1;
if (flag & FLAG_REPEAT)
{
if (unlikely (glyph >= glyph_end)) return Glyph ();
repeat = *glyph + 1;
glyph++;
}
unsigned int xBytes, yBytes;
xBytes = yBytes = 0;
if (flag & FLAG_X_SHORT) xBytes = 1;
else if ((flag & FLAG_X_SAME) == 0) xBytes = 2;
if (flag & FLAG_Y_SHORT) yBytes = 1;
else if ((flag & FLAG_Y_SAME) == 0) yBytes = 2;
coord_bytes += (xBytes + yBytes) * repeat;
coords_with_flags += repeat;
if (coords_with_flags >= num_coordinates) break;
} }
if (unlikely (coords_with_flags != num_coordinates)) return Glyph (); unsigned int xBytes, yBytes;
return Glyph (bytes.sub_array (0, bytes.length + coord_bytes - (glyph_end - glyph))); xBytes = yBytes = 0;
if (flag & FLAG_X_SHORT) xBytes = 1;
else if ((flag & FLAG_X_SAME) == 0) xBytes = 2;
if (flag & FLAG_Y_SHORT) yBytes = 1;
else if ((flag & FLAG_Y_SAME) == 0) yBytes = 2;
coord_bytes += (xBytes + yBytes) * repeat;
coords_with_flags += repeat;
if (coords_with_flags >= num_coordinates) break;
} }
/* zero instruction length */ if (unlikely (coords_with_flags != num_coordinates)) return hb_bytes_t ();
void drop_hints () return bytes.sub_array (0, bytes.length + coord_bytes - (glyph_end - glyph));
}
/* zero instruction length */
void drop_hints ()
{
GlyphHeader &glyph_header = const_cast<GlyphHeader &> (header);
(HBUINT16 &) StructAtOffset<HBUINT16> (&glyph_header, instruction_len_offset ()) = 0;
}
void drop_hints_bytes (hb_bytes_t &dest_start, hb_bytes_t &dest_end) const
{
unsigned int instructions_len = instructions_length ();
unsigned int glyph_length = length (instructions_len);
dest_start = bytes.sub_array (0, glyph_length - instructions_len);
dest_end = bytes.sub_array (glyph_length, bytes.length - glyph_length);
}
void set_overlaps_flag ()
{
if (unlikely (!header.numberOfContours)) return;
unsigned flags_offset = length (instructions_length ());
if (unlikely (flags_offset + 1 > bytes.length)) return;
HBUINT8 &first_flag = (HBUINT8 &) StructAtOffset<HBUINT16> (&bytes, flags_offset);
first_flag = (uint8_t) first_flag | FLAG_OVERLAP_SIMPLE;
}
static bool read_points (const HBUINT8 *&p /* IN/OUT */,
contour_point_vector_t &points_ /* IN/OUT */,
const hb_bytes_t &bytes,
void (* setter) (contour_point_t &_, float v),
const simple_glyph_flag_t short_flag,
const simple_glyph_flag_t same_flag)
{
float v = 0;
for (unsigned i = 0; i < points_.length; i++)
{ {
GlyphHeader &glyph_header = const_cast<GlyphHeader &> (header); uint8_t flag = points_[i].flag;
(HBUINT16 &) StructAtOffset<HBUINT16> (&glyph_header, instruction_len_offset ()) = 0; if (flag & short_flag)
}
void drop_hints_bytes (hb_bytes_t &dest_start, hb_bytes_t &dest_end) const
{
unsigned int instructions_len = instructions_length ();
unsigned int glyph_length = length (instructions_len);
dest_start = bytes.sub_array (0, glyph_length - instructions_len);
dest_end = bytes.sub_array (glyph_length, bytes.length - glyph_length);
}
void set_overlaps_flag ()
{
if (unlikely (!header.numberOfContours)) return;
unsigned flags_offset = length (instructions_length ());
if (unlikely (flags_offset + 1 > bytes.length)) return;
HBUINT8 &first_flag = (HBUINT8 &) StructAtOffset<HBUINT16> (&bytes, flags_offset);
first_flag = (uint8_t) first_flag | FLAG_OVERLAP_SIMPLE;
}
static bool read_points (const HBUINT8 *&p /* IN/OUT */,
contour_point_vector_t &points_ /* IN/OUT */,
const hb_bytes_t &bytes,
void (* setter) (contour_point_t &_, float v),
const simple_glyph_flag_t short_flag,
const simple_glyph_flag_t same_flag)
{
float v = 0;
for (unsigned i = 0; i < points_.length; i++)
{
uint8_t flag = points_[i].flag;
if (flag & short_flag)
{
if (unlikely (!bytes.check_range (p))) return false;
if (flag & same_flag)
v += *p++;
else
v -= *p++;
}
else
{
if (!(flag & same_flag))
{
if (unlikely (!bytes.check_range ((const HBUINT16 *) p))) return false;
v += *(const HBINT16 *) p;
p += HBINT16::static_size;
}
}
setter (points_[i], v);
}
return true;
}
bool get_contour_points (contour_point_vector_t &points_ /* OUT */,
bool phantom_only = false) const
{
const HBUINT16 *endPtsOfContours = &StructAfter<HBUINT16> (header);
int num_contours = header.numberOfContours;
if (unlikely (!bytes.check_range (&endPtsOfContours[num_contours + 1]))) return false;
unsigned int num_points = endPtsOfContours[num_contours - 1] + 1;
points_.resize (num_points);
for (unsigned int i = 0; i < points_.length; i++) points_[i].init ();
if (phantom_only) return true;
for (int i = 0; i < num_contours; i++)
points_[endPtsOfContours[i]].is_end_point = true;
/* Skip instructions */
const HBUINT8 *p = &StructAtOffset<HBUINT8> (&endPtsOfContours[num_contours + 1],
endPtsOfContours[num_contours]);
/* Read flags */
for (unsigned int i = 0; i < num_points; i++)
{ {
if (unlikely (!bytes.check_range (p))) return false; if (unlikely (!bytes.check_range (p))) return false;
uint8_t flag = *p++; if (flag & same_flag)
points_[i].flag = flag; v += *p++;
if (flag & FLAG_REPEAT) else
v -= *p++;
}
else
{
if (!(flag & same_flag))
{ {
if (unlikely (!bytes.check_range (p))) return false; if (unlikely (!bytes.check_range ((const HBUINT16 *) p))) return false;
unsigned int repeat_count = *p++; v += *(const HBINT16 *) p;
while ((repeat_count-- > 0) && (++i < num_points)) p += HBINT16::static_size;
points_[i].flag = flag;
} }
} }
setter (points_[i], v);
/* Read x & y coordinates */
return read_points (p, points_, bytes, [] (contour_point_t &p, float v) { p.x = v; },
FLAG_X_SHORT, FLAG_X_SAME)
&& read_points (p, points_, bytes, [] (contour_point_t &p, float v) { p.y = v; },
FLAG_Y_SHORT, FLAG_Y_SAME);
} }
}; return true;
}
struct CompositeGlyph bool get_contour_points (contour_point_vector_t &points_ /* OUT */,
bool phantom_only = false) const
{ {
const GlyphHeader &header; const HBUINT16 *endPtsOfContours = &StructAfter<HBUINT16> (header);
hb_bytes_t bytes; int num_contours = header.numberOfContours;
CompositeGlyph (const GlyphHeader &header_, hb_bytes_t bytes_) : if (unlikely (!bytes.check_range (&endPtsOfContours[num_contours + 1]))) return false;
header (header_), bytes (bytes_) {} unsigned int num_points = endPtsOfContours[num_contours - 1] + 1;
composite_iter_t get_iterator () const points_.resize (num_points);
{ return composite_iter_t (bytes, &StructAfter<CompositeGlyphChain, GlyphHeader> (header)); } for (unsigned int i = 0; i < points_.length; i++) points_[i].init ();
if (phantom_only) return true;
unsigned int instructions_length (hb_bytes_t bytes) const for (int i = 0; i < num_contours; i++)
points_[endPtsOfContours[i]].is_end_point = true;
/* Skip instructions */
const HBUINT8 *p = &StructAtOffset<HBUINT8> (&endPtsOfContours[num_contours + 1],
endPtsOfContours[num_contours]);
/* Read flags */
for (unsigned int i = 0; i < num_points; i++)
{ {
unsigned int start = bytes.length; if (unlikely (!bytes.check_range (p))) return false;
unsigned int end = bytes.length; uint8_t flag = *p++;
const CompositeGlyphChain *last = nullptr; points_[i].flag = flag;
for (auto &item : get_iterator ()) if (flag & FLAG_REPEAT)
last = &item; {
if (unlikely (!last)) return 0; if (unlikely (!bytes.check_range (p))) return false;
unsigned int repeat_count = *p++;
if (last->has_instructions ()) while ((repeat_count-- > 0) && (++i < num_points))
start = (char *) last - &bytes + last->get_size (); points_[i].flag = flag;
if (unlikely (start > end)) return 0; }
return end - start;
} }
/* Trimming for composites not implemented. /* Read x & y coordinates */
* If removing hints it falls out of that. */ return read_points (p, points_, bytes, [] (contour_point_t &p, float v) { p.x = v; },
const Glyph trim_padding () const { return Glyph (bytes); } FLAG_X_SHORT, FLAG_X_SAME)
&& read_points (p, points_, bytes, [] (contour_point_t &p, float v) { p.y = v; },
FLAG_Y_SHORT, FLAG_Y_SAME);
}
};
void drop_hints () struct CompositeGlyph
{ {
for (const auto &_ : get_iterator ()) const GlyphHeader &header;
const_cast<CompositeGlyphChain &> (_).drop_instructions_flag (); hb_bytes_t bytes;
} CompositeGlyph (const GlyphHeader &header_, hb_bytes_t bytes_) :
header (header_), bytes (bytes_) {}
/* Chop instructions off the end */ composite_iter_t get_iterator () const
void drop_hints_bytes (hb_bytes_t &dest_start) const { return composite_iter_t (bytes, &StructAfter<CompositeGlyphChain, GlyphHeader> (header)); }
{ dest_start = bytes.sub_array (0, bytes.length - instructions_length (bytes)); }
void set_overlaps_flag () unsigned int instructions_length (hb_bytes_t bytes) const
{ {
CompositeGlyphChain& glyph_chain = const_cast<CompositeGlyphChain &> ( unsigned int start = bytes.length;
StructAfter<CompositeGlyphChain, GlyphHeader> (header)); unsigned int end = bytes.length;
if (!bytes.check_range(&glyph_chain, CompositeGlyphChain::min_size)) const CompositeGlyphChain *last = nullptr;
return; for (auto &item : get_iterator ())
glyph_chain.set_overlaps_flag (); last = &item;
} if (unlikely (!last)) return 0;
};
if (last->has_instructions ())
start = (char *) last - &bytes + last->get_size ();
if (unlikely (start > end)) return 0;
return end - start;
}
/* Trimming for composites not implemented.
* If removing hints it falls out of that. */
const hb_bytes_t trim_padding () const { return bytes; }
void drop_hints ()
{
for (const auto &_ : get_iterator ())
const_cast<CompositeGlyphChain &> (_).drop_instructions_flag ();
}
/* Chop instructions off the end */
void drop_hints_bytes (hb_bytes_t &dest_start) const
{ dest_start = bytes.sub_array (0, bytes.length - instructions_length (bytes)); }
void set_overlaps_flag ()
{
CompositeGlyphChain& glyph_chain = const_cast<CompositeGlyphChain &> (
StructAfter<CompositeGlyphChain, GlyphHeader> (header));
if (!bytes.check_range(&glyph_chain, CompositeGlyphChain::min_size))
return;
glyph_chain.set_overlaps_flag ();
}
};
struct Glyph
{
enum glyph_type_t { EMPTY, SIMPLE, COMPOSITE }; enum glyph_type_t { EMPTY, SIMPLE, COMPOSITE };
public: public:
@ -486,7 +487,7 @@ struct Glyph
return CompositeGlyph (*header, bytes).get_iterator (); return CompositeGlyph (*header, bytes).get_iterator ();
} }
const Glyph trim_padding () const const hb_bytes_t trim_padding () const
{ {
switch (type) { switch (type) {
case COMPOSITE: return CompositeGlyph (*header, bytes).trim_padding (); case COMPOSITE: return CompositeGlyph (*header, bytes).trim_padding ();

View File

@ -391,7 +391,7 @@ struct glyf_accelerator_t
Glyph glyph (hb_bytes_t ((const char *) this->glyf_table + start_offset, Glyph glyph (hb_bytes_t ((const char *) this->glyf_table + start_offset,
end_offset - start_offset), gid); end_offset - start_offset), gid);
return needs_padding_removal ? glyph.trim_padding () : glyph; return needs_padding_removal ? Glyph (glyph.trim_padding ()) : glyph;
} }
struct path_builder_t struct path_builder_t
@ -425,7 +425,7 @@ struct glyf_accelerator_t
* https://stackoverflow.com/a/20772557 */ * https://stackoverflow.com/a/20772557 */
void consume_point (const contour_point_t &point) void consume_point (const contour_point_t &point)
{ {
bool is_on_curve = point.flag & Glyph::FLAG_ON_CURVE; bool is_on_curve = point.flag & SimpleGlyph::FLAG_ON_CURVE;
optional_point_t p (point.x, point.y); optional_point_t p (point.x, point.y);
if (!first_oncurve.has_data) if (!first_oncurve.has_data)
{ {