From 1024a013fd62eddc65c092f6fb2ff2fae176a618 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Mon, 22 Aug 2022 09:49:30 -0600 Subject: [PATCH 01/24] [glyf] Add CoordSetter --- src/OT/glyf/Glyph.hh | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/src/OT/glyf/Glyph.hh b/src/OT/glyf/Glyph.hh index 037d209e8..b8e547e32 100644 --- a/src/OT/glyf/Glyph.hh +++ b/src/OT/glyf/Glyph.hh @@ -15,6 +15,34 @@ struct glyf_accelerator_t; namespace glyf_impl { +struct CoordSetter +{ + CoordSetter (hb_font_t *font) : + font (font), + old_coords (font->coords, font->num_coords), + new_coords (old_coords) + { + font->coords = new_coords.arrayZ; + font->num_coords = new_coords.length; + } + + ~CoordSetter () + { + font->coords = old_coords.arrayZ; + font->num_coords = old_coords.length; + } + + int& operator [] (unsigned idx) + { + if (new_coords.length < idx + 1) + new_coords.resize (idx + 1); + return new_coords[idx]; + } + + hb_font_t *font; + hb_array_t old_coords; + hb_vector_t new_coords; +}; #ifndef HB_GLYF_MAX_POINTS #define HB_GLYF_MAX_POINTS 10000 @@ -32,7 +60,7 @@ enum phantom_point_index_t struct Glyph { - enum glyph_type_t { EMPTY, SIMPLE, COMPOSITE }; + enum glyph_type_t { EMPTY, SIMPLE, COMPOSITE, VAR_COMPOSITE }; public: composite_iter_t get_composite_iterator () const @@ -288,6 +316,7 @@ struct Glyph for (auto &item : get_composite_iterator ()) { comp_points.reset (); + if (unlikely (!glyf_accelerator.glyph_for_gid (item.get_gid ()) .get_points (font, glyf_accelerator, comp_points, deltas, shift_points_hori, use_my_metrics, phantom_only, depth + 1))) @@ -368,6 +397,7 @@ struct Glyph int num_contours = header->numberOfContours; if (unlikely (num_contours == 0)) type = EMPTY; else if (num_contours > 0) type = SIMPLE; + else if (num_contours == -2) type = VAR_COMPOSITE; else type = COMPOSITE; /* negative numbers */ } From 435c5eeffec54fe48e190699532ec063c0a12c3a Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 13 Oct 2022 10:54:58 -0600 Subject: [PATCH 02/24] [glyf] Split composite-iter --- src/OT/glyf/CompositeGlyph.hh | 51 ++------------------------ src/OT/glyf/composite-iter.hh | 68 +++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 49 deletions(-) create mode 100644 src/OT/glyf/composite-iter.hh diff --git a/src/OT/glyf/CompositeGlyph.hh b/src/OT/glyf/CompositeGlyph.hh index c5f563c78..edf8cd879 100644 --- a/src/OT/glyf/CompositeGlyph.hh +++ b/src/OT/glyf/CompositeGlyph.hh @@ -3,6 +3,7 @@ #include "../../hb-open-type.hh" +#include "composite-iter.hh" namespace OT { @@ -252,55 +253,7 @@ struct CompositeGlyphRecord DEFINE_SIZE_MIN (4); }; -struct composite_iter_t : hb_iter_with_fallback_t -{ - typedef const CompositeGlyphRecord *__item_t__; - composite_iter_t (hb_bytes_t glyph_, __item_t__ current_) : - glyph (glyph_), current (nullptr), current_size (0) - { - set_current (current_); - } - - composite_iter_t () : glyph (hb_bytes_t ()), current (nullptr), current_size (0) {} - - item_t __item__ () const { return *current; } - bool __more__ () const { return current; } - void __next__ () - { - if (!current->has_more ()) { current = nullptr; return; } - - set_current (&StructAtOffset (current, current_size)); - } - composite_iter_t __end__ () const { return composite_iter_t (); } - bool operator != (const composite_iter_t& o) const - { return current != o.current; } - - - void set_current (__item_t__ current_) - { - if (!glyph.check_range (current_, CompositeGlyphRecord::min_size)) - { - current = nullptr; - current_size = 0; - return; - } - unsigned size = current_->get_size (); - if (!glyph.check_range (current_, size)) - { - current = nullptr; - current_size = 0; - return; - } - - current = current_; - current_size = size; - } - - private: - hb_bytes_t glyph; - __item_t__ current; - unsigned current_size; -}; +using composite_iter_t = composite_iter_tmpl; struct CompositeGlyph { diff --git a/src/OT/glyf/composite-iter.hh b/src/OT/glyf/composite-iter.hh new file mode 100644 index 000000000..d05701f3d --- /dev/null +++ b/src/OT/glyf/composite-iter.hh @@ -0,0 +1,68 @@ +#ifndef OT_GLYF_COMPOSITE_ITER_HH +#define OT_GLYF_COMPOSITE_ITER_HH + + +#include "../../hb.hh" + + +namespace OT { +namespace glyf_impl { + + +template +struct composite_iter_tmpl : hb_iter_with_fallback_t, + const CompositeGlyphRecord &> +{ + typedef const CompositeGlyphRecord *__item_t__; + composite_iter_tmpl (hb_bytes_t glyph_, __item_t__ current_) : + glyph (glyph_), current (nullptr), current_size (0) + { + set_current (current_); + } + + composite_iter_tmpl () : glyph (hb_bytes_t ()), current (nullptr), current_size (0) {} + + const CompositeGlyphRecord & __item__ () const { return *current; } + bool __more__ () const { return current; } + void __next__ () + { + if (!current->has_more ()) { current = nullptr; return; } + + set_current (&StructAtOffset (current, current_size)); + } + composite_iter_tmpl __end__ () const { return composite_iter_tmpl (); } + bool operator != (const composite_iter_tmpl& o) const + { return current != o.current; } + + + void set_current (__item_t__ current_) + { + if (!glyph.check_range (current_, CompositeGlyphRecord::min_size)) + { + current = nullptr; + current_size = 0; + return; + } + unsigned size = current_->get_size (); + if (!glyph.check_range (current_, size)) + { + current = nullptr; + current_size = 0; + return; + } + + current = current_; + current_size = size; + } + + private: + hb_bytes_t glyph; + __item_t__ current; + unsigned current_size; +}; + + +} /* namespace glyf_impl */ +} /* namespace OT */ + +#endif /* OT_GLYF_COMPOSITE_ITER_HH */ From 21f671bc453ba42b03402774a7e07fa0d3733099 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 13 Oct 2022 11:25:29 -0600 Subject: [PATCH 03/24] [glyf] Add stub VarCompositeGlyph --- src/OT/glyf/Glyph.hh | 1 + src/OT/glyf/VarCompositeGlyph.hh | 157 +++++++++++++++++++++++++++++++ 2 files changed, 158 insertions(+) create mode 100644 src/OT/glyf/VarCompositeGlyph.hh diff --git a/src/OT/glyf/Glyph.hh b/src/OT/glyf/Glyph.hh index b8e547e32..d98ea5184 100644 --- a/src/OT/glyf/Glyph.hh +++ b/src/OT/glyf/Glyph.hh @@ -7,6 +7,7 @@ #include "GlyphHeader.hh" #include "SimpleGlyph.hh" #include "CompositeGlyph.hh" +#include "VarCompositeGlyph.hh" namespace OT { diff --git a/src/OT/glyf/VarCompositeGlyph.hh b/src/OT/glyf/VarCompositeGlyph.hh new file mode 100644 index 000000000..4599c81e0 --- /dev/null +++ b/src/OT/glyf/VarCompositeGlyph.hh @@ -0,0 +1,157 @@ +#ifndef OT_GLYF_VARCOMPOSITEGLYPH_HH +#define OT_GLYF_VARCOMPOSITEGLYPH_HH + + +#include "../../hb-open-type.hh" + + +namespace OT { +namespace glyf_impl { + + +struct VarCompositeGlyphRecord +{ + protected: + enum var_composite_glyph_flag_t + { + ARG_1_AND_2_ARE_WORDS = 0x0001, + ARGS_ARE_XY_VALUES = 0x0002, + ROUND_XY_TO_GRID = 0x0004, + WE_HAVE_A_SCALE = 0x0008, + MORE_COMPONENTS = 0x0020, + WE_HAVE_AN_X_AND_Y_SCALE = 0x0040, + WE_HAVE_A_TWO_BY_TWO = 0x0080, + WE_HAVE_INSTRUCTIONS = 0x0100, + USE_MY_METRICS = 0x0200, + OVERLAP_COMPOUND = 0x0400, + SCALED_COMPONENT_OFFSET = 0x0800, + UNSCALED_COMPONENT_OFFSET = 0x1000, +#ifndef HB_NO_BEYOND_64K + GID_IS_24BIT = 0x2000 +#endif + }; + + public: + unsigned int get_size () const + { + unsigned int size = min_size; + /* glyphIndex is 24bit instead of 16bit */ +#ifndef HB_NO_BEYOND_64K + if (flags & GID_IS_24BIT) size += HBGlyphID24::static_size - HBGlyphID16::static_size; +#endif + /* arg1 and 2 are int16 */ + if (flags & ARG_1_AND_2_ARE_WORDS) size += 4; + /* arg1 and 2 are int8 */ + else size += 2; + + /* One x 16 bit (scale) */ + if (flags & WE_HAVE_A_SCALE) size += 2; + /* Two x 16 bit (xscale, yscale) */ + else if (flags & WE_HAVE_AN_X_AND_Y_SCALE) size += 4; + /* Four x 16 bit (xscale, scale01, scale10, yscale) */ + else if (flags & WE_HAVE_A_TWO_BY_TWO) size += 8; + + return size; + } + + void transform_points (contour_point_vector_t &points) const + { + float matrix[4]; + contour_point_t trans; + if (get_transformation (matrix, trans)) + { + points.transform (matrix); + points.translate (trans); + } + } + + bool get_transformation (float (&matrix)[4], contour_point_t &trans) const + { + matrix[0] = matrix[3] = 1.f; + matrix[1] = matrix[2] = 0.f; + + const auto *p = &StructAfter (flags); +#ifndef HB_NO_BEYOND_64K + if (flags & GID_IS_24BIT) + p += HBGlyphID24::static_size; + else +#endif + p += HBGlyphID16::static_size; + int tx, ty; + if (flags & ARG_1_AND_2_ARE_WORDS) + { + tx = *(const HBINT16 *) p; + p += HBINT16::static_size; + ty = *(const HBINT16 *) p; + p += HBINT16::static_size; + } + else + { + tx = *p++; + ty = *p++; + } + trans.init ((float) tx, (float) ty); + + { + const F2DOT14 *points = (const F2DOT14 *) p; + if (flags & WE_HAVE_A_SCALE) + { + matrix[0] = matrix[3] = points[0].to_float (); + return true; + } + else if (flags & WE_HAVE_AN_X_AND_Y_SCALE) + { + matrix[0] = points[0].to_float (); + matrix[3] = points[1].to_float (); + return true; + } + else if (flags & WE_HAVE_A_TWO_BY_TWO) + { + matrix[0] = points[0].to_float (); + matrix[1] = points[1].to_float (); + matrix[2] = points[2].to_float (); + matrix[3] = points[3].to_float (); + return true; + } + } + return tx || ty; + } + + public: + hb_codepoint_t get_gid () const + { +#ifndef HB_NO_BEYOND_64K + if (flags & GID_IS_24BIT) + return StructAfter (flags); + else +#endif + return StructAfter (flags); + } + + protected: + HBUINT16 flags; + HBUINT24 pad; + public: + DEFINE_SIZE_MIN (4); +}; + +using var_composite_iter_t = composite_iter_tmpl; + +struct VarCompositeGlyph +{ + const GlyphHeader &header; + hb_bytes_t bytes; + VarCompositeGlyph (const GlyphHeader &header_, hb_bytes_t bytes_) : + header (header_), bytes (bytes_) {} + + var_composite_iter_t iter () const + { return var_composite_iter_t (bytes, &StructAfter (header)); } + +}; + + +} /* namespace glyf_impl */ +} /* namespace OT */ + + +#endif /* OT_GLYF_VARCOMPOSITEGLYPH_HH */ From 575d99406a5e40eab04b9d6c5c3a970b674b1753 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 13 Oct 2022 13:12:26 -0600 Subject: [PATCH 04/24] [glyf] Flesh out VarCompositeGlyph --- src/OT/glyf/VarCompositeGlyph.hh | 216 ++++++++++++++++++------------- src/hb-open-type.hh | 3 + 2 files changed, 131 insertions(+), 88 deletions(-) diff --git a/src/OT/glyf/VarCompositeGlyph.hh b/src/OT/glyf/VarCompositeGlyph.hh index 4599c81e0..d88003cfa 100644 --- a/src/OT/glyf/VarCompositeGlyph.hh +++ b/src/OT/glyf/VarCompositeGlyph.hh @@ -14,42 +14,37 @@ struct VarCompositeGlyphRecord protected: enum var_composite_glyph_flag_t { - ARG_1_AND_2_ARE_WORDS = 0x0001, - ARGS_ARE_XY_VALUES = 0x0002, - ROUND_XY_TO_GRID = 0x0004, - WE_HAVE_A_SCALE = 0x0008, - MORE_COMPONENTS = 0x0020, - WE_HAVE_AN_X_AND_Y_SCALE = 0x0040, - WE_HAVE_A_TWO_BY_TWO = 0x0080, - WE_HAVE_INSTRUCTIONS = 0x0100, - USE_MY_METRICS = 0x0200, - OVERLAP_COMPOUND = 0x0400, - SCALED_COMPONENT_OFFSET = 0x0800, - UNSCALED_COMPONENT_OFFSET = 0x1000, -#ifndef HB_NO_BEYOND_64K - GID_IS_24BIT = 0x2000 -#endif + USE_MY_METRICS = 0x0001, + AXIS_INDICES_ARE_SHORT = 0x0002, + UNIFORM_SCALE = 0x0004, + HAVE_TRANSLATE_X = 0x0008, + HAVE_TRANSLATE_Y = 0x0010, + HAVE_ROTATION = 0x0020, + HAVE_SCALE_X = 0x0040, + HAVE_SCALE_Y = 0x0080, + HAVE_SKEW_X = 0x0100, + HAVE_SKEW_Y = 0x0200, + HAVE_TCENTER_X = 0x0400, + HAVE_TCENTER_Y = 0x0800, }; public: unsigned int get_size () const { unsigned int size = min_size; - /* glyphIndex is 24bit instead of 16bit */ -#ifndef HB_NO_BEYOND_64K - if (flags & GID_IS_24BIT) size += HBGlyphID24::static_size - HBGlyphID16::static_size; -#endif - /* arg1 and 2 are int16 */ - if (flags & ARG_1_AND_2_ARE_WORDS) size += 4; - /* arg1 and 2 are int8 */ - else size += 2; - /* One x 16 bit (scale) */ - if (flags & WE_HAVE_A_SCALE) size += 2; - /* Two x 16 bit (xscale, yscale) */ - else if (flags & WE_HAVE_AN_X_AND_Y_SCALE) size += 4; - /* Four x 16 bit (xscale, scale01, scale10, yscale) */ - else if (flags & WE_HAVE_A_TWO_BY_TWO) size += 8; + unsigned axis_width = (flags & AXIS_INDICES_ARE_SHORT) ? 4 : 3; + size += num_axes * axis_width; + + if (flags & HAVE_TRANSLATE_X) size += 2; + if (flags & HAVE_TRANSLATE_Y) size += 2; + if (flags & HAVE_ROTATION) size += 2; + if (flags & HAVE_SCALE_X) size += 2; + if (flags & HAVE_SCALE_Y) size += 2; + if (flags & HAVE_SKEW_X) size += 2; + if (flags & HAVE_SKEW_Y) size += 2; + if (flags & HAVE_TCENTER_X) size += 2; + if (flags & HAVE_TCENTER_Y) size += 2; return size; } @@ -58,81 +53,126 @@ struct VarCompositeGlyphRecord { float matrix[4]; contour_point_t trans; - if (get_transformation (matrix, trans)) - { - points.transform (matrix); - points.translate (trans); - } + + points.transform (matrix); + points.translate (trans); } - bool get_transformation (float (&matrix)[4], contour_point_t &trans) const + static inline void transform (float (&matrix)[4], contour_point_t &trans, + float (other)[6]) { + // https://github.com/fonttools/fonttools/blob/f66ee05f71c8b57b5f519ee975e95edcd1466e14/Lib/fontTools/misc/transform.py#L268 + float xx1 = other[0]; + float xy1 = other[1]; + float yx1 = other[2]; + float yy1 = other[3]; + float dx1 = other[4]; + float dy1 = other[5]; + float xx2 = matrix[0]; + float xy2 = matrix[1]; + float yx2 = matrix[2]; + float yy2 = matrix[3]; + float dx2 = trans.x; + float dy2 = trans.y; + + matrix[0] = xx1*xx2 + xy1*yx2; + matrix[1] = xx1*xy2 + xy1*yy2; + matrix[2] = yx1*xx2 + yy1*yx2; + matrix[3] = yx1*xy2 + yy1*yy2; + trans.x = xx2*dx1 + yx2*dy1 + dx2; + trans.y = xy2*dx1 + yy2*dy1 + dy2; + } + + static void translate (float (&matrix)[4], contour_point_t &trans, + float translateX, float translateY) + { + // https://github.com/fonttools/fonttools/blob/f66ee05f71c8b57b5f519ee975e95edcd1466e14/Lib/fontTools/misc/transform.py#L213 + float other[6] = {1.f, 0.f, 0.f, 1.f, translateX, translateY}; + transform (matrix, trans, other); + } + + static void scale (float (&matrix)[4], contour_point_t &trans, + float scaleX, float scaleY) + { + // https://github.com/fonttools/fonttools/blob/f66ee05f71c8b57b5f519ee975e95edcd1466e14/Lib/fontTools/misc/transform.py#L224 + float other[6] = {scaleX, 0.f, 0.f, scaleY, 0.f, 0.f}; + transform (matrix, trans, other); + } + + static void rotate (float (&matrix)[4], contour_point_t &trans, + float rotation) + { + // https://github.com/fonttools/fonttools/blob/f66ee05f71c8b57b5f519ee975e95edcd1466e14/Lib/fontTools/misc/transform.py#L240 + rotation = rotation * float (M_PI); + float c = cosf (rotation); + float s = sinf (rotation); + float other[6] = {c, s, -s, c, 0.f, 0.f}; + transform (matrix, trans, other); + } + + static void skew (float (&matrix)[4], contour_point_t &trans, + float skewX, float skewY) + { + // https://github.com/fonttools/fonttools/blob/f66ee05f71c8b57b5f519ee975e95edcd1466e14/Lib/fontTools/misc/transform.py#L255 + skewX = skewX * float (M_PI); + skewY = skewY * float (M_PI); + float other[6] = {1.f, tanf (skewY), tanf (skewX), 0.f, 0.f}; + transform (matrix, trans, other); + } + + void get_transformation (float (&matrix)[4], contour_point_t &trans) const + { + float translateX = 0.f; + float translateY = 0.f; + float rotation = 0.f; + float scaleX = 1.f; + float scaleY = 1.f; + float skewX = 0.f; + float skewY = 0.f; + float tCenterX = 0.f; + float tCenterY = 0.f; + + unsigned axis_width = (flags & AXIS_INDICES_ARE_SHORT) ? 4 : 3; + unsigned axes_size = num_axes * axis_width; + const HBUINT16 *p = (const HBUINT16 *) (axes_size + + &StructAfter (num_axes)); + + if (flags & HAVE_TRANSLATE_X) translateX = * (const FWORD *) p++; + if (flags & HAVE_TRANSLATE_Y) translateY = * (const FWORD *) p++; + if (flags & HAVE_ROTATION) rotation = (* (const F2DOT14 *) p++).to_float (); + if (flags & HAVE_SCALE_X) scaleX = * (const F4DOT12 *) p++; + if (flags & HAVE_SCALE_Y) scaleY = * (const F4DOT12 *) p++; + if (flags & HAVE_SKEW_X) skewX = (* (const F2DOT14 *) p++).to_float (); + if (flags & HAVE_SKEW_Y) skewY = (* (const F2DOT14 *) p++).to_float (); + if (flags & HAVE_TCENTER_X) tCenterX = * (const FWORD *) p++; + if (flags & HAVE_TCENTER_Y) tCenterY = * (const FWORD *) p++; + + if ((flags & UNIFORM_SCALE) && !(flags & HAVE_SCALE_Y)) + scaleY = scaleX; + matrix[0] = matrix[3] = 1.f; matrix[1] = matrix[2] = 0.f; + trans.init (0.f, 0.f); - const auto *p = &StructAfter (flags); -#ifndef HB_NO_BEYOND_64K - if (flags & GID_IS_24BIT) - p += HBGlyphID24::static_size; - else -#endif - p += HBGlyphID16::static_size; - int tx, ty; - if (flags & ARG_1_AND_2_ARE_WORDS) - { - tx = *(const HBINT16 *) p; - p += HBINT16::static_size; - ty = *(const HBINT16 *) p; - p += HBINT16::static_size; - } - else - { - tx = *p++; - ty = *p++; - } - trans.init ((float) tx, (float) ty); - - { - const F2DOT14 *points = (const F2DOT14 *) p; - if (flags & WE_HAVE_A_SCALE) - { - matrix[0] = matrix[3] = points[0].to_float (); - return true; - } - else if (flags & WE_HAVE_AN_X_AND_Y_SCALE) - { - matrix[0] = points[0].to_float (); - matrix[3] = points[1].to_float (); - return true; - } - else if (flags & WE_HAVE_A_TWO_BY_TWO) - { - matrix[0] = points[0].to_float (); - matrix[1] = points[1].to_float (); - matrix[2] = points[2].to_float (); - matrix[3] = points[3].to_float (); - return true; - } - } - return tx || ty; + translate (matrix, trans, translateX + tCenterX, translateY + tCenterY); + rotate (matrix, trans, rotation); + scale (matrix, trans, scaleX, scaleY); + skew (matrix, trans, -skewX, skewY); + translate (matrix, trans, -tCenterX, -tCenterY); } public: hb_codepoint_t get_gid () const { -#ifndef HB_NO_BEYOND_64K - if (flags & GID_IS_24BIT) - return StructAfter (flags); - else -#endif - return StructAfter (flags); + return gid; } protected: HBUINT16 flags; - HBUINT24 pad; + HBGlyphID16 gid; + HBUINT8 num_axes; public: - DEFINE_SIZE_MIN (4); + DEFINE_SIZE_MIN (5); }; using var_composite_iter_t = composite_iter_tmpl; diff --git a/src/hb-open-type.hh b/src/hb-open-type.hh index 276ec9c39..d15f20a23 100644 --- a/src/hb-open-type.hh +++ b/src/hb-open-type.hh @@ -157,6 +157,9 @@ struct HBFixed : Type /* 16-bit signed fixed number with the low 14 bits of fraction (2.14). */ using F2DOT14 = HBFixed; +/* 16-bit signed fixed number with the low 12 bits of fraction (4.12). */ +using F4DOT12 = HBFixed; + /* 32-bit signed fixed-point number (16.16). */ using F16DOT16 = HBFixed; From 60d959a6e7b73ce6492fb1b49b91aefe81ad99a5 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 13 Oct 2022 14:15:36 -0600 Subject: [PATCH 05/24] [glyf/VarComposite] Add use_my_metrics() --- src/OT/glyf/VarCompositeGlyph.hh | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/OT/glyf/VarCompositeGlyph.hh b/src/OT/glyf/VarCompositeGlyph.hh index d88003cfa..aa785ca77 100644 --- a/src/OT/glyf/VarCompositeGlyph.hh +++ b/src/OT/glyf/VarCompositeGlyph.hh @@ -49,6 +49,8 @@ struct VarCompositeGlyphRecord return size; } + bool is_use_my_metrics () const { return flags & USE_MY_METRICS; } + void transform_points (contour_point_vector_t &points) const { float matrix[4]; From 12688ed3865fcbfcd34dd5c1c4ab3ca3bfc63e42 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 13 Oct 2022 17:06:07 -0600 Subject: [PATCH 06/24] [glyf] Fix distcheck --- src/Makefile.sources | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Makefile.sources b/src/Makefile.sources index 10db3c151..5fc16a165 100644 --- a/src/Makefile.sources +++ b/src/Makefile.sources @@ -96,7 +96,9 @@ HB_BASE_sources = \ OT/glyf/Glyph.hh \ OT/glyf/GlyphHeader.hh \ OT/glyf/SimpleGlyph.hh \ + OT/glyf/composite-iter.hh \ OT/glyf/CompositeGlyph.hh \ + OT/glyf/VarCompositeGlyph.hh \ OT/glyf/SubsetGlyph.hh \ OT/Layout/types.hh \ OT/Layout/Common/Coverage.hh \ From 65cc3b5e2b2181e82836c85ea060b2bd8c59ff49 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 13 Oct 2022 17:11:12 -0600 Subject: [PATCH 07/24] [glyf/VarComposite] More --- src/OT/glyf/VarCompositeGlyph.hh | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/src/OT/glyf/VarCompositeGlyph.hh b/src/OT/glyf/VarCompositeGlyph.hh index aa785ca77..45fe6ec03 100644 --- a/src/OT/glyf/VarCompositeGlyph.hh +++ b/src/OT/glyf/VarCompositeGlyph.hh @@ -29,6 +29,9 @@ struct VarCompositeGlyphRecord }; public: + + static constexpr unsigned NUM_TRANSFORM_POINTS = 9; + unsigned int get_size () const { unsigned int size = min_size; @@ -51,6 +54,21 @@ struct VarCompositeGlyphRecord bool is_use_my_metrics () const { return flags & USE_MY_METRICS; } + hb_codepoint_t get_gid () const + { + return gid; + } + + unsigned get_num_axes () const + { + return num_axes; + } + + unsigned get_num_points () const + { + return num_axes + NUM_TRANSFORM_POINTS; + } + void transform_points (contour_point_vector_t &points) const { float matrix[4]; @@ -163,12 +181,6 @@ struct VarCompositeGlyphRecord translate (matrix, trans, -tCenterX, -tCenterY); } - public: - hb_codepoint_t get_gid () const - { - return gid; - } - protected: HBUINT16 flags; HBGlyphID16 gid; From 0a939b48a60bc5b5cae0d3a5774218f1143b6759 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Mon, 17 Oct 2022 12:12:40 -0600 Subject: [PATCH 08/24] [glyf/VarComposite] Implement more --- src/OT/glyf/Glyph.hh | 49 ++++++++++++++++++++++++++++++-- src/OT/glyf/VarCompositeGlyph.hh | 8 ++++-- 2 files changed, 52 insertions(+), 5 deletions(-) diff --git a/src/OT/glyf/Glyph.hh b/src/OT/glyf/Glyph.hh index d98ea5184..8ebdb6124 100644 --- a/src/OT/glyf/Glyph.hh +++ b/src/OT/glyf/Glyph.hh @@ -69,6 +69,11 @@ struct Glyph if (type != COMPOSITE) return composite_iter_t (); return CompositeGlyph (*header, bytes).iter (); } + var_composite_iter_t get_var_composite_iterator () const + { + if (type != VAR_COMPOSITE) return var_composite_iter_t (); + return VarCompositeGlyph (*header, bytes).iter (); + } const hb_bytes_t trim_padding () const { @@ -242,6 +247,10 @@ struct Glyph contour_point_vector_t &points = inplace ? all_points : stack_points; switch (type) { + case SIMPLE: + if (unlikely (!SimpleGlyph (*header, bytes).get_contour_points (points, phantom_only))) + return false; + break; case COMPOSITE: { /* pseudo component points for each component in composite glyph */ @@ -249,9 +258,15 @@ struct Glyph if (unlikely (!points.resize (num_points))) return false; break; } - case SIMPLE: - if (unlikely (!SimpleGlyph (*header, bytes).get_contour_points (points, phantom_only))) - return false; + case VAR_COMPOSITE: + { + points.resize (0); + //for (auto &item : get_var_composite_iterator ()) + { + /* XXX */ + } + } + default: break; } @@ -358,8 +373,36 @@ struct Glyph all_points.extend (phantoms); } break; + case VAR_COMPOSITE: + { + contour_point_vector_t comp_points; + for (auto &item : get_var_composite_iterator ()) + { + comp_points.reset (); + + if (unlikely (!glyf_accelerator.glyph_for_gid (item.get_gid ()) + .get_points (font, glyf_accelerator, comp_points, + deltas, shift_points_hori, use_my_metrics, phantom_only, depth + 1))) + return false; + + /* Apply component transformation & translation */ + item.transform_points (comp_points); + + /* Apply translation from gvar */ + //comp_points.translate (points[comp_index]); + + /* Copy phantom points from component if USE_MY_METRICS flag set */ + if (use_my_metrics && item.is_use_my_metrics ()) + for (unsigned int i = 0; i < PHANTOM_COUNT; i++) + phantoms[i] = comp_points[comp_points.length - PHANTOM_COUNT + i]; + + all_points.extend (comp_points.sub_array (0, comp_points.length - PHANTOM_COUNT)); + } + all_points.extend (phantoms); + } break; default: all_points.extend (phantoms); + break; } if (depth == 0 && shift_points_hori) /* Apply at top level */ diff --git a/src/OT/glyf/VarCompositeGlyph.hh b/src/OT/glyf/VarCompositeGlyph.hh index 45fe6ec03..cf4a3f931 100644 --- a/src/OT/glyf/VarCompositeGlyph.hh +++ b/src/OT/glyf/VarCompositeGlyph.hh @@ -30,7 +30,7 @@ struct VarCompositeGlyphRecord public: - static constexpr unsigned NUM_TRANSFORM_POINTS = 9; + static constexpr unsigned NUM_TRANSFORM_POINTS = 5; unsigned int get_size () const { @@ -52,7 +52,9 @@ struct VarCompositeGlyphRecord return size; } - bool is_use_my_metrics () const { return flags & USE_MY_METRICS; } + bool has_more () const { return true; } + + bool is_use_my_metrics () const { return flags & USE_MY_METRICS; } hb_codepoint_t get_gid () const { @@ -74,6 +76,8 @@ struct VarCompositeGlyphRecord float matrix[4]; contour_point_t trans; + get_transformation (matrix, trans); + points.transform (matrix); points.translate (trans); } From dadb4ed71de2da768ee4f07a3b595181813fb0f4 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Mon, 17 Oct 2022 12:48:24 -0600 Subject: [PATCH 09/24] [glyf/VarComposite] More, almost there --- src/OT/glyf/Glyph.hh | 18 ++++++---- src/OT/glyf/VarCompositeGlyph.hh | 58 +++++++++++++++++++++++++++----- 2 files changed, 60 insertions(+), 16 deletions(-) diff --git a/src/OT/glyf/Glyph.hh b/src/OT/glyf/Glyph.hh index 8ebdb6124..fda6c191a 100644 --- a/src/OT/glyf/Glyph.hh +++ b/src/OT/glyf/Glyph.hh @@ -261,9 +261,9 @@ struct Glyph case VAR_COMPOSITE: { points.resize (0); - //for (auto &item : get_var_composite_iterator ()) + for (auto &item : get_var_composite_iterator ()) { - /* XXX */ + if (unlikely (!item.get_points (points))) return false; } } default: @@ -376,20 +376,22 @@ struct Glyph case VAR_COMPOSITE: { contour_point_vector_t comp_points; + hb_array_t points_left = points.as_array (); for (auto &item : get_var_composite_iterator ()) { + hb_array_t record_points = points_left.sub_array (0, item.get_num_points ()); + comp_points.reset (); + /* XXX Apply variations. */ + if (unlikely (!glyf_accelerator.glyph_for_gid (item.get_gid ()) .get_points (font, glyf_accelerator, comp_points, deltas, shift_points_hori, use_my_metrics, phantom_only, depth + 1))) return false; - /* Apply component transformation & translation */ - item.transform_points (comp_points); - - /* Apply translation from gvar */ - //comp_points.translate (points[comp_index]); + /* Apply component transformation */ + item.transform_points (record_points, comp_points); /* Copy phantom points from component if USE_MY_METRICS flag set */ if (use_my_metrics && item.is_use_my_metrics ()) @@ -397,6 +399,8 @@ struct Glyph phantoms[i] = comp_points[comp_points.length - PHANTOM_COUNT + i]; all_points.extend (comp_points.sub_array (0, comp_points.length - PHANTOM_COUNT)); + + points_left += item.get_num_points (); } all_points.extend (phantoms); } break; diff --git a/src/OT/glyf/VarCompositeGlyph.hh b/src/OT/glyf/VarCompositeGlyph.hh index cf4a3f931..86f291ac2 100644 --- a/src/OT/glyf/VarCompositeGlyph.hh +++ b/src/OT/glyf/VarCompositeGlyph.hh @@ -71,12 +71,13 @@ struct VarCompositeGlyphRecord return num_axes + NUM_TRANSFORM_POINTS; } - void transform_points (contour_point_vector_t &points) const + void transform_points (hb_array_t transformation_points, + contour_point_vector_t &points) const { float matrix[4]; contour_point_t trans; - get_transformation (matrix, trans); + get_transformation_from_points (transformation_points, matrix, trans); points.transform (matrix); points.translate (trans); @@ -144,7 +145,7 @@ struct VarCompositeGlyphRecord transform (matrix, trans, other); } - void get_transformation (float (&matrix)[4], contour_point_t &trans) const + bool get_points (contour_point_vector_t &points) const { float translateX = 0.f; float translateY = 0.f; @@ -156,28 +157,67 @@ struct VarCompositeGlyphRecord float tCenterX = 0.f; float tCenterY = 0.f; - unsigned axis_width = (flags & AXIS_INDICES_ARE_SHORT) ? 4 : 3; + if (unlikely (!points.resize (points.length + get_num_points ()))) return false; + + unsigned axis_width = (flags & AXIS_INDICES_ARE_SHORT) ? 2 : 1; unsigned axes_size = num_axes * axis_width; - const HBUINT16 *p = (const HBUINT16 *) (axes_size + - &StructAfter (num_axes)); + + const F2DOT14 *q = (const F2DOT14 *) (axes_size + + &StructAfter (num_axes)); + + hb_array_t axis_points = points.sub_array (points.length - get_num_points ()); + unsigned count = num_axes; + for (unsigned i = 0; i < count; i++) + axis_points[i].x = *q++; + + const HBUINT16 *p = (const HBUINT16 *) q; if (flags & HAVE_TRANSLATE_X) translateX = * (const FWORD *) p++; if (flags & HAVE_TRANSLATE_Y) translateY = * (const FWORD *) p++; - if (flags & HAVE_ROTATION) rotation = (* (const F2DOT14 *) p++).to_float (); + if (flags & HAVE_ROTATION) rotation = * (const F2DOT14 *) p++; if (flags & HAVE_SCALE_X) scaleX = * (const F4DOT12 *) p++; if (flags & HAVE_SCALE_Y) scaleY = * (const F4DOT12 *) p++; - if (flags & HAVE_SKEW_X) skewX = (* (const F2DOT14 *) p++).to_float (); - if (flags & HAVE_SKEW_Y) skewY = (* (const F2DOT14 *) p++).to_float (); + if (flags & HAVE_SKEW_X) skewX = * (const F2DOT14 *) p++; + if (flags & HAVE_SKEW_Y) skewY = * (const F2DOT14 *) p++; if (flags & HAVE_TCENTER_X) tCenterX = * (const FWORD *) p++; if (flags & HAVE_TCENTER_Y) tCenterY = * (const FWORD *) p++; if ((flags & UNIFORM_SCALE) && !(flags & HAVE_SCALE_Y)) scaleY = scaleX; + hb_array_t t = points.sub_array (points.length - NUM_TRANSFORM_POINTS); + t[0].x = translateX; + t[0].y = translateY; + t[1].x = rotation; + t[2].x = scaleX; + t[2].y = scaleY; + t[3].x = skewX; + t[3].y = skewY; + t[4].x = tCenterX; + t[4].y = tCenterY; + + return true; + } + + void get_transformation_from_points (hb_array_t points, + float (&matrix)[4], contour_point_t &trans) const + { matrix[0] = matrix[3] = 1.f; matrix[1] = matrix[2] = 0.f; trans.init (0.f, 0.f); + hb_array_t t = points.sub_array (points.length - NUM_TRANSFORM_POINTS); + + float translateX = t[0].x; + float translateY = t[0].y; + float rotation = t[1].x / (1 << 14); + float scaleX = t[2].x / (1 << 12); + float scaleY = t[2].y / (1 << 12); + float skewX = t[3].x / (1 << 14); + float skewY = t[3].y / (1 << 14); + float tCenterX = t[4].x; + float tCenterY = t[4].y; + translate (matrix, trans, translateX + tCenterX, translateY + tCenterY); rotate (matrix, trans, rotation); scale (matrix, trans, scaleX, scaleY); From 4ec77814978b675d0aa74e869c24abc8b5270678 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Mon, 17 Oct 2022 12:53:58 -0600 Subject: [PATCH 10/24] [glyf] Move coord-setter to its own file --- src/Makefile.sources | 1 + src/OT/glyf/Glyph.hh | 29 +------------------- src/OT/glyf/VarCompositeGlyph.hh | 1 + src/OT/glyf/coord-setter.hh | 45 ++++++++++++++++++++++++++++++++ 4 files changed, 48 insertions(+), 28 deletions(-) create mode 100644 src/OT/glyf/coord-setter.hh diff --git a/src/Makefile.sources b/src/Makefile.sources index 5fc16a165..6c4fc5de7 100644 --- a/src/Makefile.sources +++ b/src/Makefile.sources @@ -96,6 +96,7 @@ HB_BASE_sources = \ OT/glyf/Glyph.hh \ OT/glyf/GlyphHeader.hh \ OT/glyf/SimpleGlyph.hh \ + OT/glyf/coord-setter.hh \ OT/glyf/composite-iter.hh \ OT/glyf/CompositeGlyph.hh \ OT/glyf/VarCompositeGlyph.hh \ diff --git a/src/OT/glyf/Glyph.hh b/src/OT/glyf/Glyph.hh index fda6c191a..14c901825 100644 --- a/src/OT/glyf/Glyph.hh +++ b/src/OT/glyf/Glyph.hh @@ -8,6 +8,7 @@ #include "SimpleGlyph.hh" #include "CompositeGlyph.hh" #include "VarCompositeGlyph.hh" +#include "coord-setter.hh" namespace OT { @@ -16,34 +17,6 @@ struct glyf_accelerator_t; namespace glyf_impl { -struct CoordSetter -{ - CoordSetter (hb_font_t *font) : - font (font), - old_coords (font->coords, font->num_coords), - new_coords (old_coords) - { - font->coords = new_coords.arrayZ; - font->num_coords = new_coords.length; - } - - ~CoordSetter () - { - font->coords = old_coords.arrayZ; - font->num_coords = old_coords.length; - } - - int& operator [] (unsigned idx) - { - if (new_coords.length < idx + 1) - new_coords.resize (idx + 1); - return new_coords[idx]; - } - - hb_font_t *font; - hb_array_t old_coords; - hb_vector_t new_coords; -}; #ifndef HB_GLYF_MAX_POINTS #define HB_GLYF_MAX_POINTS 10000 diff --git a/src/OT/glyf/VarCompositeGlyph.hh b/src/OT/glyf/VarCompositeGlyph.hh index 86f291ac2..f15ead8c3 100644 --- a/src/OT/glyf/VarCompositeGlyph.hh +++ b/src/OT/glyf/VarCompositeGlyph.hh @@ -3,6 +3,7 @@ #include "../../hb-open-type.hh" +#include "coord-setter.hh" namespace OT { diff --git a/src/OT/glyf/coord-setter.hh b/src/OT/glyf/coord-setter.hh new file mode 100644 index 000000000..c3d5ce5b7 --- /dev/null +++ b/src/OT/glyf/coord-setter.hh @@ -0,0 +1,45 @@ +#ifndef OT_GLYF_COORD_SETTER_HH +#define OT_GLYF_COORD_SETTER_HH + + +#include "../../hb.hh" + + +namespace OT { +namespace glyf_impl { + + +struct coord_setter_t +{ + coord_setter_t (hb_font_t *font) : + font (font), + old_coords (font->coords, font->num_coords), + new_coords (old_coords) + { + font->coords = new_coords.arrayZ; + font->num_coords = new_coords.length; + } + + ~coord_setter_t () + { + font->coords = old_coords.arrayZ; + font->num_coords = old_coords.length; + } + + int& operator [] (unsigned idx) + { + if (new_coords.length < idx + 1) + new_coords.resize (idx + 1); + return new_coords[idx]; + } + + hb_font_t *font; + hb_array_t old_coords; + hb_vector_t new_coords; +}; + + +} /* namespace glyf_impl */ +} /* namespace OT */ + +#endif /* OT_GLYF_COORD_SETTER_HH */ From 7efd68da390f5cd125a1dd3a187ae28bbfb282e0 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Mon, 17 Oct 2022 13:05:22 -0600 Subject: [PATCH 11/24] [glyf/VarComposite] Set coordinates Code is untested but complete! --- src/OT/glyf/Glyph.hh | 3 ++- src/OT/glyf/VarCompositeGlyph.hh | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/OT/glyf/Glyph.hh b/src/OT/glyf/Glyph.hh index 14c901825..ee89175de 100644 --- a/src/OT/glyf/Glyph.hh +++ b/src/OT/glyf/Glyph.hh @@ -356,7 +356,8 @@ struct Glyph comp_points.reset (); - /* XXX Apply variations. */ + coord_setter_t coord_setter (font); + item.set_coordinates (coord_setter, record_points); if (unlikely (!glyf_accelerator.glyph_for_gid (item.get_gid ()) .get_points (font, glyf_accelerator, comp_points, diff --git a/src/OT/glyf/VarCompositeGlyph.hh b/src/OT/glyf/VarCompositeGlyph.hh index f15ead8c3..6ac7aea07 100644 --- a/src/OT/glyf/VarCompositeGlyph.hh +++ b/src/OT/glyf/VarCompositeGlyph.hh @@ -226,6 +226,22 @@ struct VarCompositeGlyphRecord translate (matrix, trans, -tCenterX, -tCenterY); } + void set_coordinates (coord_setter_t &setter, + hb_array_t axis_points) const + { + unsigned axis_width = (flags & AXIS_INDICES_ARE_SHORT) ? 2 : 1; + + const HBUINT8 *p = &StructAfter (num_axes); + const HBUINT16 *q = &StructAfter (num_axes); + + unsigned count = num_axes; + for (unsigned i = 0; i < count; i++) + { + unsigned axis_index = axis_width == 1 ? *p++ : *q++; + setter[axis_index] = axis_points[i].x; + } + } + protected: HBUINT16 flags; HBGlyphID16 gid; From 1a906162cb5d2445975cc571bc808e025f31b5d1 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Mon, 17 Oct 2022 14:01:34 -0600 Subject: [PATCH 12/24] [glyf/coord-setter] Fix memory issue --- src/OT/glyf/coord-setter.hh | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/OT/glyf/coord-setter.hh b/src/OT/glyf/coord-setter.hh index c3d5ce5b7..8ef20e76a 100644 --- a/src/OT/glyf/coord-setter.hh +++ b/src/OT/glyf/coord-setter.hh @@ -29,7 +29,11 @@ struct coord_setter_t int& operator [] (unsigned idx) { if (new_coords.length < idx + 1) + { new_coords.resize (idx + 1); + font->coords = new_coords.arrayZ; + font->num_coords = new_coords.length; + } return new_coords[idx]; } From 1233be61d89b876dee6901137a02b4d7ee7a0e13 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Mon, 17 Oct 2022 14:06:44 -0600 Subject: [PATCH 13/24] [glyf/VarComposite] Remove unneeded resize --- src/OT/glyf/Glyph.hh | 1 - 1 file changed, 1 deletion(-) diff --git a/src/OT/glyf/Glyph.hh b/src/OT/glyf/Glyph.hh index ee89175de..7ac31f898 100644 --- a/src/OT/glyf/Glyph.hh +++ b/src/OT/glyf/Glyph.hh @@ -233,7 +233,6 @@ struct Glyph } case VAR_COMPOSITE: { - points.resize (0); for (auto &item : get_var_composite_iterator ()) { if (unlikely (!item.get_points (points))) return false; From 1e71db2d264ebf9d86a43dc3378b653b8e5907ea Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Tue, 18 Oct 2022 10:47:32 -0600 Subject: [PATCH 14/24] [glyf/VarComposite] Fix transformation --- src/OT/glyf/VarCompositeGlyph.hh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/OT/glyf/VarCompositeGlyph.hh b/src/OT/glyf/VarCompositeGlyph.hh index 6ac7aea07..d3b9709f4 100644 --- a/src/OT/glyf/VarCompositeGlyph.hh +++ b/src/OT/glyf/VarCompositeGlyph.hh @@ -142,7 +142,7 @@ struct VarCompositeGlyphRecord // https://github.com/fonttools/fonttools/blob/f66ee05f71c8b57b5f519ee975e95edcd1466e14/Lib/fontTools/misc/transform.py#L255 skewX = skewX * float (M_PI); skewY = skewY * float (M_PI); - float other[6] = {1.f, tanf (skewY), tanf (skewX), 0.f, 0.f}; + float other[6] = {1.f, tanf (skewY), tanf (skewX), 1.f, 0.f, 0.f}; transform (matrix, trans, other); } @@ -151,8 +151,8 @@ struct VarCompositeGlyphRecord float translateX = 0.f; float translateY = 0.f; float rotation = 0.f; - float scaleX = 1.f; - float scaleY = 1.f; + float scaleX = 1.f * (1 << 12); + float scaleY = 1.f * (1 << 12); float skewX = 0.f; float skewY = 0.f; float tCenterX = 0.f; From a975be4c072b4370ee5efad5409b0b88e818259d Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Tue, 18 Oct 2022 11:00:35 -0600 Subject: [PATCH 15/24] [glyf/VarComposite] Minor rename --- src/OT/glyf/Glyph.hh | 2 +- src/OT/glyf/VarCompositeGlyph.hh | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/OT/glyf/Glyph.hh b/src/OT/glyf/Glyph.hh index 7ac31f898..b82cae895 100644 --- a/src/OT/glyf/Glyph.hh +++ b/src/OT/glyf/Glyph.hh @@ -356,7 +356,7 @@ struct Glyph comp_points.reset (); coord_setter_t coord_setter (font); - item.set_coordinates (coord_setter, record_points); + item.set_variations (coord_setter, record_points); if (unlikely (!glyf_accelerator.glyph_for_gid (item.get_gid ()) .get_points (font, glyf_accelerator, comp_points, diff --git a/src/OT/glyf/VarCompositeGlyph.hh b/src/OT/glyf/VarCompositeGlyph.hh index d3b9709f4..51e40bdbf 100644 --- a/src/OT/glyf/VarCompositeGlyph.hh +++ b/src/OT/glyf/VarCompositeGlyph.hh @@ -226,8 +226,8 @@ struct VarCompositeGlyphRecord translate (matrix, trans, -tCenterX, -tCenterY); } - void set_coordinates (coord_setter_t &setter, - hb_array_t axis_points) const + void set_variations (coord_setter_t &setter, + hb_array_t axis_points) const { unsigned axis_width = (flags & AXIS_INDICES_ARE_SHORT) ? 2 : 1; From 13deea7cbd5becb0746585177b9d67e0a52516e7 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Tue, 18 Oct 2022 13:38:12 -0600 Subject: [PATCH 16/24] [glyf/VarComposite] Clamp axis coordinates --- src/OT/glyf/VarCompositeGlyph.hh | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/OT/glyf/VarCompositeGlyph.hh b/src/OT/glyf/VarCompositeGlyph.hh index 51e40bdbf..489fb911d 100644 --- a/src/OT/glyf/VarCompositeGlyph.hh +++ b/src/OT/glyf/VarCompositeGlyph.hh @@ -238,7 +238,9 @@ struct VarCompositeGlyphRecord for (unsigned i = 0; i < count; i++) { unsigned axis_index = axis_width == 1 ? *p++ : *q++; - setter[axis_index] = axis_points[i].x; + signed v = axis_points[i].x; + v = hb_clamp (v, -(1<<14), (1<<14)); + setter[axis_index] = v; } } From bbe59e4211c6dbff929320c7a4332289353cf666 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 8 Dec 2022 16:04:19 -0700 Subject: [PATCH 17/24] Whitespace --- src/OT/glyf/Glyph.hh | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/OT/glyf/Glyph.hh b/src/OT/glyf/Glyph.hh index b82cae895..a3fa0982e 100644 --- a/src/OT/glyf/Glyph.hh +++ b/src/OT/glyf/Glyph.hh @@ -234,9 +234,7 @@ struct Glyph case VAR_COMPOSITE: { for (auto &item : get_var_composite_iterator ()) - { if (unlikely (!item.get_points (points))) return false; - } } default: break; From 3caa42a4a7b4879162d19273441c2cc8b44e3142 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 9 Dec 2022 14:48:41 -0700 Subject: [PATCH 18/24] Fix build after rebase --- src/OT/glyf/Glyph.hh | 2 +- src/OT/glyf/VarCompositeGlyph.hh | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/OT/glyf/Glyph.hh b/src/OT/glyf/Glyph.hh index a3fa0982e..1b63517db 100644 --- a/src/OT/glyf/Glyph.hh +++ b/src/OT/glyf/Glyph.hh @@ -369,7 +369,7 @@ struct Glyph for (unsigned int i = 0; i < PHANTOM_COUNT; i++) phantoms[i] = comp_points[comp_points.length - PHANTOM_COUNT + i]; - all_points.extend (comp_points.sub_array (0, comp_points.length - PHANTOM_COUNT)); + all_points.extend (comp_points.as_array ().sub_array (0, comp_points.length - PHANTOM_COUNT)); points_left += item.get_num_points (); } diff --git a/src/OT/glyf/VarCompositeGlyph.hh b/src/OT/glyf/VarCompositeGlyph.hh index 489fb911d..043ba645a 100644 --- a/src/OT/glyf/VarCompositeGlyph.hh +++ b/src/OT/glyf/VarCompositeGlyph.hh @@ -166,7 +166,7 @@ struct VarCompositeGlyphRecord const F2DOT14 *q = (const F2DOT14 *) (axes_size + &StructAfter (num_axes)); - hb_array_t axis_points = points.sub_array (points.length - get_num_points ()); + hb_array_t axis_points = points.as_array ().sub_array (points.length - get_num_points ()); unsigned count = num_axes; for (unsigned i = 0; i < count; i++) axis_points[i].x = *q++; @@ -186,7 +186,7 @@ struct VarCompositeGlyphRecord if ((flags & UNIFORM_SCALE) && !(flags & HAVE_SCALE_Y)) scaleY = scaleX; - hb_array_t t = points.sub_array (points.length - NUM_TRANSFORM_POINTS); + hb_array_t t = points.as_array ().sub_array (points.length - NUM_TRANSFORM_POINTS); t[0].x = translateX; t[0].y = translateY; t[1].x = rotation; From 8e46870093fe9c214679370bdf34dbd67f388d18 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 9 Dec 2022 18:35:30 -0700 Subject: [PATCH 19/24] [gvar] Take coords in instead of font in apply_deltas_to_points --- src/OT/glyf/Glyph.hh | 4 +++- src/hb-ot-var-gvar-table.hh | 6 +++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/OT/glyf/Glyph.hh b/src/OT/glyf/Glyph.hh index 1b63517db..d91235f24 100644 --- a/src/OT/glyf/Glyph.hh +++ b/src/OT/glyf/Glyph.hh @@ -276,7 +276,9 @@ struct Glyph } #ifndef HB_NO_VAR - glyf_accelerator.gvar->apply_deltas_to_points (gid, font, points.as_array ()); + glyf_accelerator.gvar->apply_deltas_to_points (gid, + hb_array (font->coords, font->num_coords), + points.as_array ()); #endif // mainly used by CompositeGlyph calculating new X/Y offset value so no need to extend it diff --git a/src/hb-ot-var-gvar-table.hh b/src/hb-ot-var-gvar-table.hh index c23e1f74b..e02063ca4 100644 --- a/src/hb-ot-var-gvar-table.hh +++ b/src/hb-ot-var-gvar-table.hh @@ -548,10 +548,11 @@ struct gvar { return (i >= end) ? start : (i + 1); } public: - bool apply_deltas_to_points (hb_codepoint_t glyph, hb_font_t *font, + bool apply_deltas_to_points (hb_codepoint_t glyph, + hb_array_t coords, const hb_array_t points) const { - if (!font->num_coords) return true; + if (!coords) return true; if (unlikely (glyph >= table->glyphCount)) return true; @@ -578,7 +579,6 @@ struct gvar if (points.arrayZ[i].is_end_point) end_points.push (i); - auto coords = hb_array (font->coords, font->num_coords); unsigned num_coords = table->axisCount; hb_array_t shared_tuples = (table+table->sharedTuples).as_array (table->sharedTupleCount * table->axisCount); From e9e503b80bba01d42b0987bab4dc240db6368e8f Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 9 Dec 2022 18:42:46 -0700 Subject: [PATCH 20/24] [Glyph] Pass down coords to get_points --- src/OT/glyf/Glyph.hh | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/src/OT/glyf/Glyph.hh b/src/OT/glyf/Glyph.hh index d91235f24..e04e00b2b 100644 --- a/src/OT/glyf/Glyph.hh +++ b/src/OT/glyf/Glyph.hh @@ -211,9 +211,14 @@ struct Glyph bool shift_points_hori = true, bool use_my_metrics = true, bool phantom_only = false, + hb_array_t coords = hb_array_t (), unsigned int depth = 0) const { if (unlikely (depth > HB_MAX_NESTING_LEVEL)) return false; + + if (!coords) + coords = hb_array (font->coords, font->num_coords); + contour_point_vector_t stack_points; bool inplace = type == SIMPLE && all_points.length == 0; /* Load into all_points if it's empty, as an optimization. */ @@ -277,7 +282,7 @@ struct Glyph #ifndef HB_NO_VAR glyf_accelerator.gvar->apply_deltas_to_points (gid, - hb_array (font->coords, font->num_coords), + coords, points.as_array ()); #endif @@ -306,8 +311,15 @@ struct Glyph comp_points.reset (); if (unlikely (!glyf_accelerator.glyph_for_gid (item.get_gid ()) - .get_points (font, glyf_accelerator, comp_points, - deltas, shift_points_hori, use_my_metrics, phantom_only, depth + 1))) + .get_points (font, + glyf_accelerator, + comp_points, + deltas, + shift_points_hori, + use_my_metrics, + phantom_only, + coords, + depth + 1))) return false; /* Apply component transformation & translation */ @@ -359,8 +371,15 @@ struct Glyph item.set_variations (coord_setter, record_points); if (unlikely (!glyf_accelerator.glyph_for_gid (item.get_gid ()) - .get_points (font, glyf_accelerator, comp_points, - deltas, shift_points_hori, use_my_metrics, phantom_only, depth + 1))) + .get_points (font, + glyf_accelerator, + comp_points, + deltas, + shift_points_hori, + use_my_metrics, + phantom_only, + hb_array (font->coords, font->num_coords), + depth + 1))) return false; /* Apply component transformation */ From 82b4f3791e1348273ebc8c13d410638842eef833 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 9 Dec 2022 18:45:37 -0700 Subject: [PATCH 21/24] [coord-setter] Don't modify font coords --- src/OT/glyf/Glyph.hh | 4 ++-- src/OT/glyf/coord-setter.hh | 33 +++++++++------------------------ 2 files changed, 11 insertions(+), 26 deletions(-) diff --git a/src/OT/glyf/Glyph.hh b/src/OT/glyf/Glyph.hh index e04e00b2b..0ba4c970e 100644 --- a/src/OT/glyf/Glyph.hh +++ b/src/OT/glyf/Glyph.hh @@ -367,7 +367,7 @@ struct Glyph comp_points.reset (); - coord_setter_t coord_setter (font); + coord_setter_t coord_setter (coords); item.set_variations (coord_setter, record_points); if (unlikely (!glyf_accelerator.glyph_for_gid (item.get_gid ()) @@ -378,7 +378,7 @@ struct Glyph shift_points_hori, use_my_metrics, phantom_only, - hb_array (font->coords, font->num_coords), + coord_setter.get_coords (), depth + 1))) return false; diff --git a/src/OT/glyf/coord-setter.hh b/src/OT/glyf/coord-setter.hh index 8ef20e76a..df64ed5af 100644 --- a/src/OT/glyf/coord-setter.hh +++ b/src/OT/glyf/coord-setter.hh @@ -11,35 +11,20 @@ namespace glyf_impl { struct coord_setter_t { - coord_setter_t (hb_font_t *font) : - font (font), - old_coords (font->coords, font->num_coords), - new_coords (old_coords) - { - font->coords = new_coords.arrayZ; - font->num_coords = new_coords.length; - } - - ~coord_setter_t () - { - font->coords = old_coords.arrayZ; - font->num_coords = old_coords.length; - } + coord_setter_t (hb_array_t coords) : + coords (coords) {} int& operator [] (unsigned idx) { - if (new_coords.length < idx + 1) - { - new_coords.resize (idx + 1); - font->coords = new_coords.arrayZ; - font->num_coords = new_coords.length; - } - return new_coords[idx]; + if (coords.length < idx + 1) + coords.resize (idx + 1); + return coords[idx]; } - hb_font_t *font; - hb_array_t old_coords; - hb_vector_t new_coords; + hb_array_t get_coords () + { return coords.as_array (); } + + hb_vector_t coords; }; From 5cf0b9ae7d21cdce0c822ac1f6fb8de1e922aed8 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 9 Dec 2022 18:59:43 -0700 Subject: [PATCH 22/24] [varc] Mark as experimental feature with HB_NO_VAR_COMPOSITES --- src/OT/glyf/Glyph.hh | 4 ++++ src/hb-config.hh | 1 + 2 files changed, 5 insertions(+) diff --git a/src/OT/glyf/Glyph.hh b/src/OT/glyf/Glyph.hh index 0ba4c970e..94f2790f8 100644 --- a/src/OT/glyf/Glyph.hh +++ b/src/OT/glyf/Glyph.hh @@ -236,11 +236,13 @@ struct Glyph if (unlikely (!points.resize (num_points))) return false; break; } +#ifndef HB_NO_VAR_COMPOSITES case VAR_COMPOSITE: { for (auto &item : get_var_composite_iterator ()) if (unlikely (!item.get_points (points))) return false; } +#endif default: break; } @@ -357,6 +359,7 @@ struct Glyph all_points.extend (phantoms); } break; +#ifndef HB_NO_VAR_COMPOSITES case VAR_COMPOSITE: { contour_point_vector_t comp_points; @@ -396,6 +399,7 @@ struct Glyph } all_points.extend (phantoms); } break; +#endif default: all_points.extend (phantoms); break; diff --git a/src/hb-config.hh b/src/hb-config.hh index 81c5b83df..c59e17c16 100644 --- a/src/hb-config.hh +++ b/src/hb-config.hh @@ -37,6 +37,7 @@ #ifndef HB_EXPERIMENTAL #define HB_NO_BEYOND_64K +#define HB_NO_VAR_COMPOSITES #endif #ifdef HB_TINY From af450a757d8471e55b71d1f3eb3c1e1fd3390d7b Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 9 Dec 2022 19:05:16 -0700 Subject: [PATCH 23/24] [config] Use HB_EXPERIMENTAL_API instead of adhoc HB_EXPERIMENTAL --- src/hb-config.hh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/hb-config.hh b/src/hb-config.hh index c59e17c16..98b1e9d0c 100644 --- a/src/hb-config.hh +++ b/src/hb-config.hh @@ -35,7 +35,7 @@ #include "config.h" #endif -#ifndef HB_EXPERIMENTAL +#ifndef HB_EXPERIMENTAL_API #define HB_NO_BEYOND_64K #define HB_NO_VAR_COMPOSITES #endif From 10d38dcdfd7e0b62f35ac5ff1d6cdf9ce1a78cc2 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 9 Dec 2022 19:19:57 -0700 Subject: [PATCH 24/24] [varc] Change format slightly Fixes https://github.com/harfbuzz/boring-expansion-spec/issues/70 --- src/OT/glyf/VarCompositeGlyph.hh | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/OT/glyf/VarCompositeGlyph.hh b/src/OT/glyf/VarCompositeGlyph.hh index 043ba645a..eb49f3ede 100644 --- a/src/OT/glyf/VarCompositeGlyph.hh +++ b/src/OT/glyf/VarCompositeGlyph.hh @@ -164,7 +164,7 @@ struct VarCompositeGlyphRecord unsigned axes_size = num_axes * axis_width; const F2DOT14 *q = (const F2DOT14 *) (axes_size + - &StructAfter (num_axes)); + &StructAfter (gid)); hb_array_t axis_points = points.as_array ().sub_array (points.length - get_num_points ()); unsigned count = num_axes; @@ -231,8 +231,8 @@ struct VarCompositeGlyphRecord { unsigned axis_width = (flags & AXIS_INDICES_ARE_SHORT) ? 2 : 1; - const HBUINT8 *p = &StructAfter (num_axes); - const HBUINT16 *q = &StructAfter (num_axes); + const HBUINT8 *p = &StructAfter (gid); + const HBUINT16 *q = &StructAfter (gid); unsigned count = num_axes; for (unsigned i = 0; i < count; i++) @@ -246,8 +246,8 @@ struct VarCompositeGlyphRecord protected: HBUINT16 flags; - HBGlyphID16 gid; HBUINT8 num_axes; + HBGlyphID16 gid; public: DEFINE_SIZE_MIN (5); };