From b9ff43c162e8f61cff8e48d3dcbf7c1fe263d625 Mon Sep 17 00:00:00 2001 From: Michiharu Ariza Date: Tue, 1 Oct 2019 16:13:40 +0330 Subject: [PATCH 1/8] Implement gvar table and variable glyf Split from Ariza's "[subset] TrueType/CFF2 variable font" work on #1594 --- src/Makefile.sources | 2 + src/harfbuzz.cc | 1 + src/hb-ot-font.cc | 4 +- src/hb-ot-glyf-table.hh | 626 ++++++++++++--- src/hb-ot-hmtx-table.cc | 54 ++ src/hb-ot-hmtx-table.hh | 33 +- src/hb-ot-var-gvar-table.hh | 734 ++++++++++++++++++ test/api/Makefile.am | 1 + .../SourceSansVariable-Roman-nohvar-41,C1.ttf | Bin 0 -> 4696 bytes .../fonts/SourceSansVariable-Roman.anchor.ttf | Bin 0 -> 4708 bytes .../SourceSansVariable-Roman.modcomp.ttf | Bin 0 -> 3252 bytes .../SourceSerifVariable-Roman-VVAR.abc.ttf | Bin 0 -> 5632 bytes test/api/test-ot-metrics-tt-var.c | 250 ++++++ 13 files changed, 1592 insertions(+), 113 deletions(-) create mode 100644 src/hb-ot-hmtx-table.cc create mode 100644 src/hb-ot-var-gvar-table.hh create mode 100644 test/api/fonts/SourceSansVariable-Roman-nohvar-41,C1.ttf create mode 100644 test/api/fonts/SourceSansVariable-Roman.anchor.ttf create mode 100644 test/api/fonts/SourceSansVariable-Roman.modcomp.ttf create mode 100644 test/api/fonts/SourceSerifVariable-Roman-VVAR.abc.ttf create mode 100644 test/api/test-ot-metrics-tt-var.c diff --git a/src/Makefile.sources b/src/Makefile.sources index eff0ad347..31cd31f89 100644 --- a/src/Makefile.sources +++ b/src/Makefile.sources @@ -75,6 +75,7 @@ HB_BASE_sources = \ hb-ot-hdmx-table.hh \ hb-ot-head-table.hh \ hb-ot-hhea-table.hh \ + hb-ot-hmtx-table.cc \ hb-ot-hmtx-table.hh \ hb-ot-kern-table.hh \ hb-ot-layout-base-table.hh \ @@ -136,6 +137,7 @@ HB_BASE_sources = \ hb-ot-tag.cc \ hb-ot-var-avar-table.hh \ hb-ot-var-fvar-table.hh \ + hb-ot-var-gvar-table.hh \ hb-ot-var-hvar-table.hh \ hb-ot-var-mvar-table.hh \ hb-ot-var.cc \ diff --git a/src/harfbuzz.cc b/src/harfbuzz.cc index fe1c36023..2aa5a7942 100644 --- a/src/harfbuzz.cc +++ b/src/harfbuzz.cc @@ -14,6 +14,7 @@ #include "hb-ot-color.cc" #include "hb-ot-face.cc" #include "hb-ot-font.cc" +#include "hb-ot-hmtx-table.cc" #include "hb-ot-layout.cc" #include "hb-ot-map.cc" #include "hb-ot-math.cc" diff --git a/src/hb-ot-font.cc b/src/hb-ot-font.cc index 78ff6d294..4b7446e62 100644 --- a/src/hb-ot-font.cc +++ b/src/hb-ot-font.cc @@ -160,7 +160,7 @@ hb_ot_get_glyph_v_origin (hb_font_t *font, #endif hb_glyph_extents_t extents = {0}; - if (ot_face->glyf->get_extents (glyph, &extents)) + if (ot_face->glyf->get_extents (font, glyph, &extents)) { const OT::vmtx_accelerator_t &vmtx = *ot_face->vmtx; hb_position_t tsb = vmtx.get_side_bearing (font, glyph); @@ -188,7 +188,7 @@ hb_ot_get_glyph_extents (hb_font_t *font, #if !defined(HB_NO_OT_FONT_BITMAP) && !defined(HB_NO_COLOR) if (!ret) ret = ot_face->sbix->get_extents (font, glyph, extents); #endif - if (!ret) ret = ot_face->glyf->get_extents (glyph, extents); + if (!ret) ret = ot_face->glyf->get_extents (font, glyph, extents); #ifndef HB_NO_OT_FONT_CFF if (!ret) ret = ot_face->cff1->get_extents (glyph, extents); if (!ret) ret = ot_face->cff2->get_extents (font, glyph, extents); diff --git a/src/hb-ot-glyf-table.hh b/src/hb-ot-glyf-table.hh index 9a791bbf0..72c138f6d 100644 --- a/src/hb-ot-glyf-table.hh +++ b/src/hb-ot-glyf-table.hh @@ -1,5 +1,6 @@ /* * Copyright © 2015 Google, Inc. + * Copyright © 2019 Adobe Inc. * Copyright © 2019 Ebrahim Byagowi * * This is part of HarfBuzz, a text shaping library. @@ -23,6 +24,7 @@ * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. * * Google Author(s): Behdad Esfahbod, Garret Rieger, Roderick Sheeter + * Adobe Author(s): Michiharu Ariza */ #ifndef HB_OT_GLYF_TABLE_HH @@ -30,6 +32,10 @@ #include "hb-open-type.hh" #include "hb-ot-head-table.hh" +#include "hb-ot-hmtx-table.hh" +#include "hb-ot-var-gvar-table.hh" + +#include namespace OT { @@ -54,12 +60,11 @@ struct loca } protected: - UnsizedArrayOf - dataZ; /* Location data. */ + UnsizedArrayOf dataZ; /* Location data. */ public: - DEFINE_SIZE_MIN (0); /* In reality, this is UNBOUNDED() type; but since we always - * check the size externally, allow Null() object of it by - * defining it _MIN instead. */ + DEFINE_SIZE_MIN (0); /* In reality, this is UNBOUNDED() type; but since we always + * check the size externally, allow Null() object of it by + * defining it _MIN instead. */ }; @@ -111,7 +116,7 @@ struct glyf free); bool result = plan->add_table (HB_OT_TAG_loca, loca_blob) - && _add_head_and_set_loca_version(plan, use_short_loca); + && _add_head_and_set_loca_version (plan, use_short_loca); hb_blob_destroy (loca_blob); return result; @@ -230,8 +235,7 @@ struct glyf if (!glyph_header.is_simple_glyph ()) return; // only for simple glyphs unsigned int instruction_len_offset = glyph_header.simple_instruction_len_offset (); - const HBUINT16 &instruction_len = StructAtOffset (&glyph, - instruction_len_offset); + const HBUINT16 &instruction_len = StructAtOffset (&glyph, instruction_len_offset); (HBUINT16 &) instruction_len = 0; } @@ -284,26 +288,17 @@ struct glyf bool is_composite_glyph () const { return numberOfContours < 0; } bool is_simple_glyph () const { return numberOfContours > 0; } - void get_extents (hb_glyph_extents_t *extents) const - { - extents->x_bearing = hb_min (xMin, xMax); - extents->y_bearing = hb_max (yMin, yMax); - extents->width = hb_max (xMin, xMax) - extents->x_bearing; - extents->height = hb_min (yMin, yMax) - extents->y_bearing; - } - bool has_data () const { return numberOfContours; } - protected: - 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: + 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. */ + DEFINE_SIZE_STATIC (10); }; @@ -325,8 +320,8 @@ struct glyf UNSCALED_COMPONENT_OFFSET = 0x1000 }; - HBUINT16 flags; - HBGlyphID glyphIndex; + HBUINT16 flags; + HBGlyphID glyphIndex; unsigned int get_size () const { @@ -346,6 +341,91 @@ struct glyf return size; } + bool is_anchored () const { return (flags & ARGS_ARE_XY_VALUES) == 0; } + void get_anchor_points (unsigned int &point1, unsigned int &point2) const + { + const HBUINT8 *p = &StructAfter (glyphIndex); + if (flags & ARG_1_AND_2_ARE_WORDS) + { + point1 = ((const HBUINT16 *)p)[0]; + point2 = ((const HBUINT16 *)p)[1]; + } + else + { + point1 = p[0]; + point2 = p[1]; + } + } + + void transform_points (contour_point_vector_t &points) const + { + float matrix[4]; + contour_point_t trans; + if (get_transformation (matrix, trans)) + { + if (scaled_offsets ()) + { + points.translate (trans); + points.transform (matrix); + } + else + { + points.transform (matrix); + points.translate (trans); + } + } + } + + protected: + bool scaled_offsets () const + { return (flags & (SCALED_COMPONENT_OFFSET|UNSCALED_COMPONENT_OFFSET)) == SCALED_COMPONENT_OFFSET; } + + bool get_transformation (float (&matrix)[4], contour_point_t &trans) const + { + matrix[0] = matrix[3] = 1.f; + matrix[1] = matrix[2] = 0.f; + + int tx, ty; + const HBINT8 *p = &StructAfter (glyphIndex); + 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++; + } + if (is_anchored ()) tx = ty = 0; + + trans.init ((float)tx, (float)ty); + + if (flags & WE_HAVE_A_SCALE) + { + matrix[0] = matrix[3] = ((const F2DOT14*)p)->to_float (); + return true; + } + else if (flags & WE_HAVE_AN_X_AND_Y_SCALE) + { + matrix[0] = ((const F2DOT14*)p)[0].to_float (); + matrix[3] = ((const F2DOT14*)p)[1].to_float (); + return true; + } + else if (flags & WE_HAVE_A_TWO_BY_TWO) + { + matrix[0] = ((const F2DOT14*)p)[0].to_float (); + matrix[1] = ((const F2DOT14*)p)[1].to_float (); + matrix[2] = ((const F2DOT14*)p)[2].to_float (); + matrix[3] = ((const F2DOT14*)p)[3].to_float (); + return true; + } + return tx || ty; + } + + public: // TODO rewrite using new iterator framework if possible struct Iterator { @@ -359,8 +439,7 @@ struct glyf { const CompositeGlyphHeader *possible = &StructAfter (*current); - if (!in_range (possible)) - return false; + if (unlikely (!in_range (possible))) return false; current = possible; return true; } @@ -417,12 +496,19 @@ struct glyf glyf_table = hb_sanitize_context_t ().reference_table (face); num_glyphs = hb_max (1u, loca_table.get_length () / (short_offset ? 2 : 4)) - 1; + + gvar_accel.init (face); + hmtx_accel.init (face); + vmtx_accel.init (face); } void fini () { loca_table.destroy (); glyf_table.destroy (); + gvar_accel.fini (); + hmtx_accel.fini (); + vmtx_accel.fini (); } /* @@ -457,12 +543,323 @@ struct glyf FLAG_RESERVED2 = 0x80 }; + enum phantom_point_index_t { + PHANTOM_LEFT = 0, + PHANTOM_RIGHT = 1, + PHANTOM_TOP = 2, + PHANTOM_BOTTOM = 3, + PHANTOM_COUNT = 4 + }; + + protected: + const GlyphHeader &get_header (hb_codepoint_t glyph) const + { + unsigned int start_offset, end_offset; + if (!get_offsets (glyph, &start_offset, &end_offset) || end_offset - start_offset < GlyphHeader::static_size) + return Null(GlyphHeader); + + return StructAtOffset (glyf_table, start_offset); + } + + struct x_setter_t + { + void set (contour_point_t &point, float v) const { point.x = v; } + bool is_short (uint8_t flag) const { return (flag & FLAG_X_SHORT) != 0; } + bool is_same (uint8_t flag) const { return (flag & FLAG_X_SAME) != 0; } + }; + + struct y_setter_t + { + void set (contour_point_t &point, float v) const { point.y = v; } + bool is_short (uint8_t flag) const { return (flag & FLAG_Y_SHORT) != 0; } + bool is_same (uint8_t flag) const { return (flag & FLAG_Y_SAME) != 0; } + }; + + template + static bool read_points (const HBUINT8 *&p /* IN/OUT */, + contour_point_vector_t &points_ /* IN/OUT */, + const range_checker_t &checker) + { + T coord_setter; + float v = 0; + for (unsigned int i = 0; i < points_.length - PHANTOM_COUNT; i++) + { + uint8_t flag = points_[i].flag; + if (coord_setter.is_short (flag)) + { + if (unlikely (!checker.in_range (p))) return false; + if (coord_setter.is_same (flag)) + v += *p++; + else + v -= *p++; + } + else + { + if (!coord_setter.is_same (flag)) + { + if (unlikely (!checker.in_range ((const HBUINT16 *)p))) return false; + v += *(const HBINT16 *)p; + p += HBINT16::static_size; + } + } + coord_setter.set (points_[i], v); + } + return true; + } + + void init_phantom_points (hb_codepoint_t glyph, hb_array_t &phantoms /* IN/OUT */) const + { + const GlyphHeader &header = get_header (glyph); + int h_delta = (int)header.xMin - hmtx_accel.get_side_bearing (glyph); + int v_orig = (int)header.yMax + vmtx_accel.get_side_bearing (glyph); + unsigned int h_adv = hmtx_accel.get_advance (glyph); + unsigned int v_adv = vmtx_accel.get_advance (glyph); + + phantoms[PHANTOM_LEFT].x = h_delta; + phantoms[PHANTOM_RIGHT].x = h_adv + h_delta; + phantoms[PHANTOM_TOP].y = v_orig; + phantoms[PHANTOM_BOTTOM].y = -(int)v_adv + v_orig; + } + + /* for a simple glyph, return contour end points, flags, along with coordinate points + * for a composite glyph, return pseudo component points + * in both cases points trailed with four phantom points + */ + bool get_contour_points (hb_codepoint_t glyph, + contour_point_vector_t &points_ /* OUT */, + hb_vector_t &end_points_ /* OUT */, + const bool phantom_only=false) const + { + unsigned int num_points = 0; + unsigned int start_offset, end_offset; + if (unlikely (!get_offsets (glyph, &start_offset, &end_offset))) return false; + if (unlikely (end_offset - start_offset < GlyphHeader::static_size)) + { + /* empty glyph */ + points_.resize (PHANTOM_COUNT); + for (unsigned int i = 0; i < points_.length; i++) points_[i].init (); + return true; + } + + CompositeGlyphHeader::Iterator composite; + if (get_composite (glyph, &composite)) + { + /* For a composite glyph, add one pseudo point for each component */ + do { num_points++; } while (composite.move_to_next()); + points_.resize (num_points + PHANTOM_COUNT); + for (unsigned int i = 0; i < points_.length; i++) points_[i].init (); + return true; + } + + const GlyphHeader &glyph_header = StructAtOffset (glyf_table, start_offset); + int16_t num_contours = (int16_t) glyph_header.numberOfContours; + const HBUINT16 *end_pts = &StructAfter (glyph_header); + + range_checker_t checker (glyf_table, start_offset, end_offset); + num_points = 0; + if (num_contours > 0) + { + if (unlikely (!checker.in_range (&end_pts[num_contours + 1]))) return false; + num_points = end_pts[num_contours - 1] + 1; + } + else if (num_contours < 0) + { + CompositeGlyphHeader::Iterator composite; + if (unlikely (!get_composite (glyph, &composite))) return false; + do + { + num_points++; + } while (composite.move_to_next()); + } + + points_.resize (num_points + PHANTOM_COUNT); + for (unsigned int i = 0; i < points_.length; i++) points_[i].init (); + if ((num_contours <= 0) || phantom_only) return true; + + /* Read simple glyph points if !phantom_only */ + end_points_.resize (num_contours); + + for (int16_t i = 0; i < num_contours; i++) + end_points_[i] = end_pts[i]; + + /* Skip instructions */ + const HBUINT8 *p = &StructAtOffset (&end_pts[num_contours+1], end_pts[num_contours]); + + /* Read flags */ + for (unsigned int i = 0; i < num_points; i++) + { + if (unlikely (!checker.in_range (p))) return false; + uint8_t flag = *p++; + points_[i].flag = flag; + if ((flag & FLAG_REPEAT) != 0) + { + if (unlikely (!checker.in_range (p))) return false; + unsigned int repeat_count = *p++; + while ((repeat_count-- > 0) && (++i < num_points)) + points_[i].flag = flag; + } + } + + /* Read x & y coordinates */ + return (read_points (p, points_, checker) && + read_points (p, points_, checker)); + } + + struct contour_bounds_t + { + contour_bounds_t () { min.x = min.y = FLT_MAX; max.x = max.y = -FLT_MAX; } + + void add (const contour_point_t &p) + { + min.x = hb_min (min.x, p.x); + min.y = hb_min (min.y, p.y); + max.x = hb_max (max.x, p.x); + max.y = hb_max (max.y, p.y); + } + + bool empty () const { return (min.x >= max.x) || (min.y >= max.y); } + + contour_point_t min; + contour_point_t max; + }; + + /* Note: Recursively calls itself. + * all_points includes phantom points + */ + bool get_points_var (hb_codepoint_t glyph, + const int *coords, unsigned int coord_count, + contour_point_vector_t &all_points /* OUT */, + unsigned int depth=0) const + { + if (unlikely (depth++ > HB_MAX_NESTING_LEVEL)) return false; + contour_point_vector_t points; + hb_vector_t end_points; + if (unlikely (!get_contour_points (glyph, points, end_points))) return false; + hb_array_t phantoms = points.sub_array (points.length-PHANTOM_COUNT, PHANTOM_COUNT); + init_phantom_points (glyph, phantoms); + if (unlikely (!gvar_accel.apply_deltas_to_points (glyph, coords, coord_count, points.as_array (), end_points.as_array ()))) return false; + + unsigned int comp_index = 0; + CompositeGlyphHeader::Iterator composite; + if (!get_composite (glyph, &composite)) + { + /* simple glyph */ + all_points.extend (points.as_array ()); + } + else + { + /* composite glyph */ + do + { + contour_point_vector_t comp_points; + if (unlikely (!get_points_var (composite.current->glyphIndex, coords, coord_count, + comp_points, depth)) + || comp_points.length < PHANTOM_COUNT) return false; + + /* Copy phantom points from component if USE_MY_METRICS flag set */ + if (composite.current->flags & CompositeGlyphHeader::USE_MY_METRICS) + for (unsigned int i = 0; i < PHANTOM_COUNT; i++) + phantoms[i] = comp_points[comp_points.length - PHANTOM_COUNT + i]; + + /* Apply component transformation & translation */ + composite.current->transform_points (comp_points); + + /* Apply translatation from gvar */ + comp_points.translate (points[comp_index]); + + if (composite.current->is_anchored ()) + { + unsigned int p1, p2; + composite.current->get_anchor_points (p1, p2); + if (likely (p1 < all_points.length && p2 < comp_points.length)) + { + contour_point_t delta; + delta.init (all_points[p1].x - comp_points[p2].x, + all_points[p1].y - comp_points[p2].y); + + comp_points.translate (delta); + } + } + + all_points.extend (comp_points.sub_array (0, comp_points.length - PHANTOM_COUNT)); + + comp_index++; + } while (composite.move_to_next()); + + all_points.extend (phantoms); + } + + return true; + } + + bool get_var_extents_and_phantoms (hb_codepoint_t glyph, + const int *coords, unsigned int coord_count, + hb_glyph_extents_t *extents=nullptr /* OUt */, + contour_point_vector_t *phantoms=nullptr /* OUT */) const + { + contour_point_vector_t all_points; + if (unlikely (!get_points_var (glyph, coords, coord_count, all_points) || + all_points.length < PHANTOM_COUNT)) return false; + + /* Undocumented rasterizer behavior: + * Shift points horizontally by the updated left side bearing + */ + contour_point_t delta; + delta.init (-all_points[all_points.length - PHANTOM_COUNT + PHANTOM_LEFT].x, 0.f); + if (delta.x != 0.f) all_points.translate (delta); + + if (extents != nullptr) + { + contour_bounds_t bounds; + for (unsigned int i = 0; i + PHANTOM_COUNT < all_points.length; i++) + bounds.add (all_points[i]); + + if (bounds.min.x > bounds.max.x) + { + extents->width = 0; + extents->x_bearing = 0; + } + else + { + extents->x_bearing = (int32_t)floorf (bounds.min.x); + extents->width = (int32_t)ceilf (bounds.max.x) - extents->x_bearing; + } + if (bounds.min.y > bounds.max.y) + { + extents->height = 0; + extents->y_bearing = 0; + } + else + { + extents->y_bearing = (int32_t)ceilf (bounds.max.y); + extents->height = (int32_t)floorf (bounds.min.y) - extents->y_bearing; + } + } + if (phantoms != nullptr) + { + for (unsigned int i = 0; i < PHANTOM_COUNT; i++) + (*phantoms)[i] = all_points[all_points.length - PHANTOM_COUNT + i]; + } + return true; + } + + bool get_var_metrics (hb_codepoint_t glyph, + const int *coords, unsigned int coord_count, + contour_point_vector_t &phantoms) const + { return get_var_extents_and_phantoms (glyph, coords, coord_count, nullptr, &phantoms); } + + bool get_extents_var (hb_codepoint_t glyph, + const int *coords, unsigned int coord_count, + hb_glyph_extents_t *extents) const + { return get_var_extents_and_phantoms (glyph, coords, coord_count, extents); } + + public: /* based on FontTools _g_l_y_f.py::trim */ bool remove_padding (unsigned int start_offset, unsigned int *end_offset) const { - const char *glyph = ((const char *) glyf_table) + start_offset; unsigned int glyph_length = *end_offset - start_offset; + const char *glyph = ((const char *) glyf_table) + start_offset; const GlyphHeader &glyph_header = *hb_bytes_t (glyph, glyph_length).as (); if (!glyph_header.has_data ()) return true; @@ -599,18 +996,15 @@ struct glyf unsigned int instruction_len_offset = glyph_header.simple_instruction_len_offset (); if (unlikely (instruction_len_offset + 2 > glyph.length)) { - DEBUG_MSG (SUBSET, nullptr, "Glyph size is too short, missing field " - "instructionLength."); + DEBUG_MSG(SUBSET, nullptr, "Glyph size is too short, missing field instructionLength."); return false; } - const HBUINT16 &instruction_len = StructAtOffset (&glyph, - instruction_len_offset); + const HBUINT16 &instruction_len = StructAtOffset (&glyph, instruction_len_offset); /* Out of bounds of the current glyph */ if (unlikely (glyph_header.simple_length (instruction_len) > glyph.length)) { - DEBUG_MSG (SUBSET, nullptr, "The instructions array overruns the " - "glyph's boundaries."); + DEBUG_MSG(SUBSET, nullptr, "The instructions array overruns the glyph's boundaries."); return false; } *length = (uint16_t) instruction_len; @@ -618,42 +1012,95 @@ struct glyf return true; } - bool get_extents (hb_codepoint_t glyph, hb_glyph_extents_t *extents) const + unsigned int get_advance_var (hb_codepoint_t glyph, + const int *coords, unsigned int coord_count, + bool vertical) const { + bool success = false; + contour_point_vector_t phantoms; + phantoms.resize (PHANTOM_COUNT); + + if (likely (coord_count == gvar_accel.get_axis_count ())) + success = get_var_metrics (glyph, coords, coord_count, phantoms); + + if (unlikely (!success)) + return vertical? vmtx_accel.get_advance (glyph): hmtx_accel.get_advance (glyph); + + if (vertical) + return roundf (phantoms[PHANTOM_TOP].y - phantoms[PHANTOM_BOTTOM].y); + else + return roundf (phantoms[PHANTOM_RIGHT].x - phantoms[PHANTOM_LEFT].x); + } + + int get_side_bearing_var (hb_codepoint_t glyph, const int *coords, unsigned int coord_count, bool vertical) const + { + hb_glyph_extents_t extents; + contour_point_vector_t phantoms; + phantoms.resize (PHANTOM_COUNT); + + if (unlikely (!get_var_extents_and_phantoms (glyph, coords, coord_count, &extents, &phantoms))) + return vertical? vmtx_accel.get_side_bearing (glyph): hmtx_accel.get_side_bearing (glyph); + + return vertical? (int)ceilf (phantoms[PHANTOM_TOP].y) - extents.y_bearing: (int)floorf (phantoms[PHANTOM_LEFT].x); + } + + bool get_extents (hb_font_t *font, hb_codepoint_t glyph, hb_glyph_extents_t *extents) const + { + unsigned int coord_count; + const int *coords = hb_font_get_var_coords_normalized (font, &coord_count); + if (coords && coord_count > 0 && coord_count == gvar_accel.get_axis_count ()) + return get_extents_var (glyph, coords, coord_count, extents); + unsigned int start_offset, end_offset; if (!get_offsets (glyph, &start_offset, &end_offset)) return false; - hb_bytes_t ((const char *) glyf_table + start_offset, - end_offset - start_offset).as ()->get_extents (extents); + if (end_offset - start_offset < GlyphHeader::static_size) + return true; /* Empty glyph; zero extents. */ + + const GlyphHeader &glyph_header = StructAtOffset (glyf_table, start_offset); + + /* 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 = hmtx_accel.get_side_bearing (glyph); + extents->y_bearing = hb_max (glyph_header.yMin, glyph_header.yMax); + extents->width = hb_max (glyph_header.xMin, glyph_header.xMax) - hb_min (glyph_header.xMin, glyph_header.xMax); + extents->height = hb_min (glyph_header.yMin, glyph_header.yMax) - extents->y_bearing; + return true; } - hb_bytes_t bytes_for_glyph (const char *glyf, hb_codepoint_t gid) + hb_bytes_t bytes_for_glyph (const char * glyf, hb_codepoint_t gid) + { + unsigned int start_offset, end_offset; + if (unlikely (!(get_offsets (gid, &start_offset, &end_offset) && + remove_padding (start_offset, &end_offset)))) { - unsigned int start_offset, end_offset; - if (unlikely (!(get_offsets (gid, &start_offset, &end_offset) && - remove_padding (start_offset, &end_offset)))) - { - DEBUG_MSG (SUBSET, nullptr, "Unable to get offset or remove padding for %d", gid); - return hb_bytes_t (); - } - hb_bytes_t glyph_bytes = hb_bytes_t (glyf + start_offset, end_offset - start_offset); - if (!glyph_bytes.as ()->has_data ()) - { - DEBUG_MSG (SUBSET, nullptr, "Empty or invalid glyph size, %d", gid); - return hb_bytes_t (); - } - return glyph_bytes; + DEBUG_MSG(SUBSET, nullptr, "Unable to get offset or remove padding for %d", gid); + return hb_bytes_t (); } + hb_bytes_t glyph_bytes = hb_bytes_t (glyf + start_offset, end_offset - start_offset); + if (!glyph_bytes.as ()->has_data ()) + { + DEBUG_MSG(SUBSET, nullptr, "Glyph size smaller than minimum header %d", gid); + return hb_bytes_t (); + } + return glyph_bytes; + } private: bool short_offset; unsigned int num_glyphs; hb_blob_ptr_t loca_table; hb_blob_ptr_t glyf_table; + + /* variable font support */ + gvar::accelerator_t gvar_accel; + hmtx::accelerator_t hmtx_accel; + vmtx::accelerator_t vmtx_accel; }; + struct SubsetGlyph { hb_codepoint_t new_gid; @@ -662,38 +1109,38 @@ struct glyf hb_bytes_t dest_start; /* region of source_glyph to copy first */ hb_bytes_t dest_end; /* region of source_glyph to copy second */ - bool serialize (hb_serialize_context_t *c, - const hb_subset_plan_t *plan) const + + bool serialize (hb_serialize_context_t *c, + const hb_subset_plan_t *plan) const + { + TRACE_SERIALIZE (this); + + hb_bytes_t dest_glyph = dest_start.copy (c); + dest_glyph = hb_bytes_t (&dest_glyph, dest_glyph.length + dest_end.copy(c).length); + unsigned int pad_length = padding (); + DEBUG_MSG(SUBSET, nullptr, "serialize %d byte glyph, width %d pad %d", dest_glyph.length, dest_glyph.length + pad_length, pad_length); + + HBUINT8 pad; + pad = 0; + while (pad_length > 0) { - TRACE_SERIALIZE (this); - - hb_bytes_t dest_glyph = dest_start.copy (c); - dest_glyph = hb_bytes_t (&dest_glyph, dest_glyph.length + dest_end.copy (c).length); - unsigned int pad_length = padding (); - DEBUG_MSG (SUBSET, nullptr, "serialize %d byte glyph, width %d pad %d", - dest_glyph.length, dest_glyph.length + pad_length, pad_length); - - HBUINT8 pad; - pad = 0; - while (pad_length > 0) - { - c->embed (pad); - pad_length--; - } - - if (dest_glyph.length) - { - _fix_component_gids (plan, dest_glyph); - if (plan->drop_hints) - { - _zero_instruction_length (dest_glyph); - c->check_success (_remove_composite_instruction_flag (dest_glyph)); - } - } - - return_trace (true); + c->embed(pad); + pad_length--; } + if (dest_glyph.length) + { + _fix_component_gids (plan, dest_glyph); + if (plan->drop_hints) + { + _zero_instruction_length (dest_glyph); + c->check_success (_remove_composite_instruction_flag (dest_glyph)); + } + } + + return_trace (true); + } + void drop_hints (const OT::glyf::accelerator_t& glyf) { if (source_glyph.length == 0) return; @@ -703,13 +1150,12 @@ struct glyf { DEBUG_MSG (SUBSET, nullptr, "Unable to read instruction length for new_gid %d", new_gid); - return ; + return; } 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); + DEBUG_MSG (SUBSET, nullptr, "Unable to read instruction length for new_gid %d", + new_gid); if (header.is_composite_glyph ()) { /* just chop instructions off the end for composite glyphs */ @@ -728,9 +1174,9 @@ struct glyf } } - unsigned int length () const { return dest_start.length + dest_end.length; } + unsigned int length () const { return dest_start.length + dest_end.length; } /* pad to 2 to ensure 2-byte loca will be ok */ - unsigned int padding () const { return length () % 2; } + unsigned int padding () const { return length () % 2; } unsigned int padded_size () const { return length () + padding (); } }; diff --git a/src/hb-ot-hmtx-table.cc b/src/hb-ot-hmtx-table.cc new file mode 100644 index 000000000..012a9f2d9 --- /dev/null +++ b/src/hb-ot-hmtx-table.cc @@ -0,0 +1,54 @@ +/* + * Copyright © 2019 Adobe Inc. + * + * This is part of HarfBuzz, a text shaping library. + * + * Permission is hereby granted, without written agreement and without + * license or royalty fees, to use, copy, modify, and distribute this + * software and its documentation for any purpose, provided that the + * above copyright notice and the following two paragraphs appear in + * all copies of this software. + * + * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR + * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES + * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN + * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH + * DAMAGE. + * + * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, + * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS + * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO + * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. + * + * Adobe Author(s): Michiharu Ariza + */ + +#include "hb-ot-hmtx-table.hh" +#include "hb-ot-glyf-table.hh" + +namespace OT { + +int hmtxvmtx_accelerator_base_t::get_side_bearing_var_tt (hb_font_t *font, hb_codepoint_t glyph, bool vertical) +{ + glyf::accelerator_t glyf_accel; + glyf_accel.init (font->face); + + int side_bearing = glyf_accel.get_side_bearing_var (glyph, font->coords, font->num_coords, vertical); + glyf_accel.fini (); + + return side_bearing; +} + +unsigned int hmtxvmtx_accelerator_base_t::get_advance_var_tt (hb_font_t *font, hb_codepoint_t glyph, bool vertical) +{ + glyf::accelerator_t glyf_accel; + glyf_accel.init (font->face); + + unsigned int advance = glyf_accel.get_advance_var (glyph, font->coords, font->num_coords, vertical); + glyf_accel.fini (); + + return advance; +} + +} diff --git a/src/hb-ot-hmtx-table.hh b/src/hb-ot-hmtx-table.hh index f8788b2df..90c9c4866 100644 --- a/src/hb-ot-hmtx-table.hh +++ b/src/hb-ot-hmtx-table.hh @@ -55,14 +55,8 @@ struct LongMetric struct hmtxvmtx_accelerator_base_t { - static int get_side_bearing_var_tt (hb_font_t *font HB_UNUSED, - hb_codepoint_t glyph HB_UNUSED, - bool is_vertical HB_UNUSED) - { return 0; } /* Not implemented yet */ - static unsigned int get_advance_var_tt (hb_font_t *font HB_UNUSED, - hb_codepoint_t glyph HB_UNUSED, - bool is_vertical HB_UNUSED) - { return 0; } /* Not implemented yet */ + HB_INTERNAL static int get_side_bearing_var_tt (hb_font_t *font, hb_codepoint_t glyph, bool vertical); + HB_INTERNAL static unsigned int get_advance_var_tt (hb_font_t *font, hb_codepoint_t glyph, bool vertical); }; template @@ -140,13 +134,12 @@ struct hmtxvmtx auto it = + hb_range (c->plan->num_output_glyphs ()) | hb_map ([c, &_mtx] (unsigned _) - { - hb_codepoint_t old_gid; - if (c->plan->old_gid_for_new_gid (_, &old_gid)) - return hb_pair (_mtx.get_advance (old_gid), _mtx.get_side_bearing (old_gid)); - else - return hb_pair (0u, 0); - }) + { + hb_codepoint_t old_gid; + if (!c->plan->old_gid_for_new_gid (_, &old_gid)) + return hb_pair (0u, 0); + return hb_pair (_mtx.get_advance (old_gid), _mtx.get_side_bearing (old_gid)); + }) ; table_prime->serialize (c->serializer, it, num_advances); @@ -158,9 +151,7 @@ struct hmtxvmtx // Amend header num hmetrics if (unlikely (!subset_update_header (c->plan, num_advances))) - { return_trace (false); - } return_trace (true); } @@ -221,8 +212,8 @@ struct hmtxvmtx if (unlikely (glyph >= num_metrics) || !font->num_coords) return side_bearing; -// if (var_table.get_blob () == &Null (hb_blob_t)) -// return get_side_bearing_var_tt (font, glyph, T::tableTag == HB_OT_TAG_vmtx); + if (var_table.get_blob () == &Null (hb_blob_t)) + return get_side_bearing_var_tt (font, glyph, T::tableTag == HB_OT_TAG_vmtx); return side_bearing + var_table->get_side_bearing_var (glyph, font->coords, font->num_coords); // TODO Optimize?! } @@ -251,8 +242,8 @@ struct hmtxvmtx if (unlikely (glyph >= num_metrics) || !font->num_coords) return advance; -// if (var_table.get_blob () == &Null (hb_blob_t)) -// return get_advance_var_tt (font, glyph, T::tableTag == HB_OT_TAG_vmtx); + if (var_table.get_blob () == &Null (hb_blob_t)) + return get_advance_var_tt (font, glyph, T::tableTag == HB_OT_TAG_vmtx); return advance + roundf (var_table->get_advance_var (glyph, font->coords, font->num_coords)); // TODO Optimize?! } diff --git a/src/hb-ot-var-gvar-table.hh b/src/hb-ot-var-gvar-table.hh new file mode 100644 index 000000000..630249b71 --- /dev/null +++ b/src/hb-ot-var-gvar-table.hh @@ -0,0 +1,734 @@ +/* + * Copyright © 2019 Adobe Inc. + * + * This is part of HarfBuzz, a text shaping library. + * + * Permission is hereby granted, without written agreement and without + * license or royalty fees, to use, copy, modify, and distribute this + * software and its documentation for any purpose, provided that the + * above copyright notice and the following two paragraphs appear in + * all copies of this software. + * + * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR + * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES + * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN + * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH + * DAMAGE. + * + * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, + * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS + * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO + * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. + * + * Adobe Author(s): Michiharu Ariza + */ + +#ifndef HB_OT_VAR_GVAR_TABLE_HH +#define HB_OT_VAR_GVAR_TABLE_HH + +#include "hb-open-type.hh" +#include "hb-ot-glyf-table.hh" +#include "hb-ot-var-fvar-table.hh" + +/* + * gvar -- Glyph Variation Table + * https://docs.microsoft.com/en-us/typography/opentype/spec/gvar + */ +#define HB_OT_TAG_gvar HB_TAG('g','v','a','r') + +namespace OT { + +struct contour_point_t +{ + void init (float x_=0.f, float y_=0.f) { flag = 0; x = x_; y = y_; } + + void translate (const contour_point_t &p) { x += p.x; y += p.y; } + + uint8_t flag; + float x, y; +}; + +struct contour_point_vector_t : hb_vector_t +{ + void extend (const hb_array_t &a) + { + unsigned int old_len = length; + resize (old_len + a.length); + for (unsigned int i = 0; i < a.length; i++) + (*this)[old_len + i] = a[i]; + } + + void transform (const float (&matrix)[4]) + { + for (unsigned int i = 0; i < length; i++) + { + contour_point_t &p = (*this)[i]; + float x_ = p.x * matrix[0] + p.y * matrix[2]; + p.y = p.x * matrix[1] + p.y * matrix[3]; + p.x = x_; + } + } + + void translate (const contour_point_t& delta) + { + for (unsigned int i = 0; i < length; i++) + (*this)[i].translate (delta); + } +}; + +struct range_checker_t +{ + range_checker_t (const void *table_, unsigned int start_offset_, unsigned int end_offset_) + : table ((const char*)table_), start_offset (start_offset_), end_offset (end_offset_) {} + + template + bool in_range (const T *p) const + { + return ((const char *) p) >= table + start_offset + && ((const char *) p + T::static_size) <= table + end_offset; + } + + protected: + const char *table; + const unsigned int start_offset; + const unsigned int end_offset; +}; + +struct Tuple : UnsizedArrayOf {}; + +struct TuppleIndex : HBUINT16 +{ + enum Flags { + EmbeddedPeakTuple = 0x8000u, + IntermediateRegion = 0x4000u, + PrivatePointNumbers = 0x2000u, + TupleIndexMask = 0x0FFFu + }; + + DEFINE_SIZE_STATIC (2); +}; + +struct TupleVarHeader +{ + unsigned int get_size (unsigned int axis_count) const + { + return min_size + + (has_peak ()? get_peak_tuple ().get_size (axis_count): 0) + + (has_intermediate ()? (get_start_tuple (axis_count).get_size (axis_count) + + get_end_tuple (axis_count).get_size (axis_count)): 0); + } + + const TupleVarHeader &get_next (unsigned int axis_count) const + { return StructAtOffset (this, get_size (axis_count)); } + + float calculate_scalar (const int *coords, unsigned int coord_count, + const hb_array_t shared_tuples) const + { + const F2DOT14 *peak_tuple; + + if (has_peak ()) + peak_tuple = &(get_peak_tuple ()[0]); + else + { + unsigned int index = get_index (); + if (unlikely (index * coord_count >= shared_tuples.length)) + return 0.f; + peak_tuple = &shared_tuples[coord_count * index]; + } + + const F2DOT14 *start_tuple = nullptr; + const F2DOT14 *end_tuple = nullptr; + if (has_intermediate ()) + { + start_tuple = get_start_tuple (coord_count); + end_tuple = get_end_tuple (coord_count); + } + + float scalar = 1.f; + for (unsigned int i = 0; i < coord_count; i++) + { + int v = coords[i]; + int peak = peak_tuple[i]; + if (!peak || v == peak) continue; + + if (has_intermediate ()) + { + int start = start_tuple[i]; + int end = end_tuple[i]; + if (unlikely (start > peak || peak > end || + (start < 0 && end > 0 && peak))) continue; + if (v < start || v > end) return 0.f; + if (v < peak) + { if (peak != start) scalar *= (float)(v - start) / (peak - start); } + else + { if (peak != end) scalar *= (float)(end - v) / (end - peak); } + } + else if (!v || v < hb_min (0, peak) || v > hb_max (0, peak)) return 0.f; + else + scalar *= (float)v / peak; + } + return scalar; + } + + unsigned int get_data_size () const { return varDataSize; } + + bool has_peak () const { return (tupleIndex & TuppleIndex::EmbeddedPeakTuple); } + bool has_intermediate () const { return (tupleIndex & TuppleIndex::IntermediateRegion); } + bool has_private_points () const { return (tupleIndex & TuppleIndex::PrivatePointNumbers); } + unsigned int get_index () const { return (tupleIndex & TuppleIndex::TupleIndexMask); } + + protected: + const Tuple &get_peak_tuple () const + { return StructAfter (tupleIndex); } + const Tuple &get_start_tuple (unsigned int axis_count) const + { return *(const Tuple *)&get_peak_tuple ()[has_peak ()? axis_count: 0]; } + const Tuple &get_end_tuple (unsigned int axis_count) const + { return *(const Tuple *)&get_peak_tuple ()[has_peak ()? (axis_count * 2): axis_count]; } + + HBUINT16 varDataSize; + TuppleIndex tupleIndex; + /* UnsizedArrayOf peakTuple - optional */ + /* UnsizedArrayOf intermediateStartTuple - optional */ + /* UnsizedArrayOf intermediateEndTuple - optional */ + + public: + DEFINE_SIZE_MIN (4); +}; + +struct TupleVarCount : HBUINT16 +{ + bool has_shared_point_numbers () const { return ((*this) & SharedPointNumbers); } + unsigned int get_count () const { return (*this) & CountMask; } + + protected: + enum Flags { + SharedPointNumbers = 0x8000u, + CountMask = 0x0FFFu + }; + + public: + DEFINE_SIZE_STATIC (2); +}; + +struct GlyphVarData +{ + const TupleVarHeader &get_tuple_var_header (void) const + { return StructAfter(data); } + + struct tuple_iterator_t + { + void init (const GlyphVarData *var_data_, unsigned int length_, unsigned int axis_count_) + { + var_data = var_data_; + length = length_; + index = 0; + axis_count = axis_count_; + current_tuple = &var_data->get_tuple_var_header (); + data_offset = 0; + } + + bool get_shared_indices (hb_vector_t &shared_indices /* OUT */) + { + if (var_data->has_shared_point_numbers ()) + { + range_checker_t checker (var_data, 0, length); + const HBUINT8 *base = &(var_data+var_data->data); + const HBUINT8 *p = base; + if (!unpack_points (p, shared_indices, checker)) return false; + data_offset = p - base; + } + return true; + } + + bool is_valid () const + { + return (index < var_data->tupleVarCount.get_count ()) && + in_range (current_tuple) && + current_tuple->get_size (axis_count); + } + + bool move_to_next () + { + data_offset += current_tuple->get_data_size (); + current_tuple = ¤t_tuple->get_next (axis_count); + index++; + return is_valid (); + } + + bool in_range (const void *p, unsigned int l) const + { return (const char*)p >= (const char*)var_data && (const char*)p+l <= (const char*)var_data + length; } + + template bool in_range (const T *p) const { return in_range (p, sizeof (*p)); } + + const HBUINT8 *get_serialized_data () const + { return &(var_data+var_data->data) + data_offset; } + + private: + const GlyphVarData *var_data; + unsigned int length; + unsigned int index; + unsigned int axis_count; + unsigned int data_offset; + + public: + const TupleVarHeader *current_tuple; + }; + + static bool get_tuple_iterator (const GlyphVarData *var_data, + unsigned int length, + unsigned int axis_count, + hb_vector_t &shared_indices /* OUT */, + tuple_iterator_t *iterator /* OUT */) + { + iterator->init (var_data, length, axis_count); + if (!iterator->get_shared_indices (shared_indices)) + return false; + return iterator->is_valid (); + } + + bool has_shared_point_numbers () const { return tupleVarCount.has_shared_point_numbers (); } + + static bool unpack_points (const HBUINT8 *&p /* IN/OUT */, + hb_vector_t &points /* OUT */, + const range_checker_t &check) + { + enum packed_point_flag_t + { + POINTS_ARE_WORDS = 0x80, + POINT_RUN_COUNT_MASK = 0x7F + }; + + if (!check.in_range (p)) return false; + uint16_t count = *p++; + if (count & POINTS_ARE_WORDS) + { + if (!check.in_range (p)) return false; + count = ((count & POINT_RUN_COUNT_MASK) << 8) | *p++; + } + points.resize (count); + + unsigned int n = 0; + uint16_t i = 0; + while (i < count) + { + if (!check.in_range (p)) return false; + uint16_t j; + uint8_t control = *p++; + uint16_t run_count = (control & POINT_RUN_COUNT_MASK) + 1; + if (control & POINTS_ARE_WORDS) + { + for (j = 0; j < run_count && i < count; j++, i++) + { + if (!check.in_range ((const HBUINT16 *)p)) return false; + n += *(const HBUINT16 *)p; + points[i] = n; + p += HBUINT16::static_size; + } + } + else + { + for (j = 0; j < run_count && i < count; j++, i++) + { + if (!check.in_range (p)) return false; + n += *p++; + points[i] = n; + } + } + if (j < run_count) return false; + } + return true; + } + + static bool unpack_deltas (const HBUINT8 *&p /* IN/OUT */, + hb_vector_t &deltas /* IN/OUT */, + const range_checker_t &check) + { + enum packed_delta_flag_t + { + DELTAS_ARE_ZERO = 0x80, + DELTAS_ARE_WORDS = 0x40, + DELTA_RUN_COUNT_MASK = 0x3F + }; + + unsigned int i = 0; + unsigned int count = deltas.length; + while (i < count) + { + if (!check.in_range (p)) return false; + uint16_t j; + uint8_t control = *p++; + uint16_t run_count = (control & DELTA_RUN_COUNT_MASK) + 1; + if (control & DELTAS_ARE_ZERO) + { + for (j = 0; j < run_count && i < count; j++, i++) + deltas[i] = 0; + } + else if (control & DELTAS_ARE_WORDS) + { + for (j = 0; j < run_count && i < count; j++, i++) + { + if (!check.in_range ((const HBUINT16 *)p)) + return false; + deltas[i] = *(const HBINT16 *)p; + p += HBUINT16::static_size; + } + } + else + { + for (j = 0; j < run_count && i < count; j++, i++) + { + if (!check.in_range (p)) + return false; + deltas[i] = *(const HBINT8 *)p++; + } + } + if (j < run_count) + return false; + } + return true; + } + + protected: + TupleVarCount tupleVarCount; + OffsetTo data; + /* TupleVarHeader tupleVarHeaders[] */ + + public: + DEFINE_SIZE_MIN (4); +}; + +struct gvar +{ + static constexpr hb_tag_t tableTag = HB_OT_TAG_gvar; + + bool sanitize_shallow (hb_sanitize_context_t *c) const + { + TRACE_SANITIZE (this); + return_trace (c->check_struct (this) && (version.major == 1) && + (glyphCount == c->get_num_glyphs ()) && + c->check_array (&(this+sharedTuples), axisCount * sharedTupleCount) && + (is_long_offset ()? + c->check_array (get_long_offset_array (), glyphCount+1): + c->check_array (get_short_offset_array (), glyphCount+1)) && + c->check_array (((const HBUINT8*)&(this+dataZ)) + get_offset (0), + get_offset (glyphCount) - get_offset (0))); + } + + /* GlyphVarData not sanitized here; must be checked while accessing each glyph varation data */ + bool sanitize (hb_sanitize_context_t *c) const + { return sanitize_shallow (c); } + + bool subset (hb_subset_context_t *c) const + { + TRACE_SUBSET (this); + + gvar *out = c->serializer->allocate_min (); + if (unlikely (!out)) return_trace (false); + + out->version.major = 1; + out->version.minor = 0; + out->axisCount = axisCount; + out->sharedTupleCount = sharedTupleCount; + + unsigned int num_glyphs = c->plan->num_output_glyphs (); + out->glyphCount = num_glyphs; + + unsigned int subset_data_size = 0; + for (hb_codepoint_t gid = 0; gid < num_glyphs; gid++) + { + hb_codepoint_t old_gid; + if (!c->plan->old_gid_for_new_gid (gid, &old_gid)) continue; + subset_data_size += get_glyph_var_data_length (old_gid); + } + + bool long_offset = subset_data_size & ~0xFFFFu; + out->flags = long_offset? 1: 0; + + HBUINT8 *subset_offsets = c->serializer->allocate_size ((long_offset? 4: 2) * (num_glyphs+1)); + if (!subset_offsets) return_trace (false); + + /* shared tuples */ + if (!sharedTupleCount || !sharedTuples) + out->sharedTuples = 0; + else + { + unsigned int shared_tuple_size = F2DOT14::static_size * axisCount * sharedTupleCount; + F2DOT14 *tuples = c->serializer->allocate_size (shared_tuple_size); + if (!tuples) return_trace (false); + out->sharedTuples = (char *)tuples - (char *)out; + memcpy (tuples, &(this+sharedTuples), shared_tuple_size); + } + + char *subset_data = c->serializer->allocate_size(subset_data_size); + if (!subset_data) return_trace (false); + out->dataZ = subset_data - (char *)out; + + unsigned int glyph_offset = 0; + for (hb_codepoint_t gid = 0; gid < num_glyphs; gid++) + { + hb_codepoint_t old_gid; + unsigned int length = c->plan->old_gid_for_new_gid (gid, &old_gid)? get_glyph_var_data_length (old_gid): 0; + + if (long_offset) + ((HBUINT32 *)subset_offsets)[gid] = glyph_offset; + else + ((HBUINT16 *)subset_offsets)[gid] = glyph_offset / 2; + + if (length > 0) memcpy (subset_data, get_glyph_var_data (old_gid), length); + subset_data += length; + glyph_offset += length; + } + if (long_offset) + ((HBUINT32 *)subset_offsets)[num_glyphs] = glyph_offset; + else + ((HBUINT16 *)subset_offsets)[num_glyphs] = glyph_offset / 2; + + return_trace (true); + } + + protected: + const GlyphVarData *get_glyph_var_data (hb_codepoint_t glyph) const + { + unsigned int start_offset = get_offset (glyph); + unsigned int end_offset = get_offset (glyph+1); + + if ((start_offset == end_offset) || + unlikely ((start_offset > get_offset (glyphCount)) || + (start_offset + GlyphVarData::min_size > end_offset))) + return &Null(GlyphVarData); + return &(((unsigned char *)this+start_offset)+dataZ); + } + + bool is_long_offset () const { return (flags & 1)!=0; } + + unsigned int get_offset (unsigned int i) const + { + if (is_long_offset ()) + return get_long_offset_array ()[i]; + else + return get_short_offset_array ()[i] * 2; + } + + unsigned int get_glyph_var_data_length (unsigned int glyph) const + { + unsigned int end_offset = get_offset (glyph+1); + unsigned int start_offset = get_offset (glyph); + if (unlikely (start_offset > end_offset || end_offset > get_offset(glyphCount))) + return 0; + return end_offset - start_offset; + } + + const HBUINT32 *get_long_offset_array () const { return (const HBUINT32 *)&offsetZ; } + const HBUINT16 *get_short_offset_array () const { return (const HBUINT16 *)&offsetZ; } + + public: + struct accelerator_t + { + void init (hb_face_t *face) + { + memset (this, 0, sizeof (accelerator_t)); + + gvar_table = hb_sanitize_context_t ().reference_table (face); + hb_blob_ptr_t fvar_table = hb_sanitize_context_t ().reference_table (face); + unsigned int axis_count = fvar_table->get_axis_count (); + fvar_table.destroy (); + + if (unlikely ((gvar_table->glyphCount != face->get_num_glyphs ()) || + (gvar_table->axisCount != axis_count))) + fini (); + + unsigned int num_shared_coord = gvar_table->sharedTupleCount * gvar_table->axisCount; + shared_tuples.resize (num_shared_coord); + for (unsigned int i = 0; i < num_shared_coord; i++) + shared_tuples[i] = (&(gvar_table+gvar_table->sharedTuples))[i]; + } + + void fini () + { + gvar_table.destroy (); + shared_tuples.fini (); + } + + private: + struct x_getter { static float get (const contour_point_t &p) { return p.x; } }; + struct y_getter { static float get (const contour_point_t &p) { return p.y; } }; + + template + static float infer_delta (const hb_array_t points, + const hb_array_t deltas, + unsigned int target, unsigned int prev, unsigned int next) + { + float target_val = T::get (points[target]); + float prev_val = T::get (points[prev]); + float next_val = T::get (points[next]); + float prev_delta = T::get (deltas[prev]); + float next_delta = T::get (deltas[next]); + + if (prev_val == next_val) + return (prev_delta == next_delta)? prev_delta: 0.f; + else if (target_val <= hb_min (prev_val, next_val)) + return (prev_val < next_val) ? prev_delta: next_delta; + else if (target_val >= hb_max (prev_val, next_val)) + return (prev_val > next_val)? prev_delta: next_delta; + + /* linear interpolation */ + float r = (target_val - prev_val) / (next_val - prev_val); + return (1.f - r) * prev_delta + r * next_delta; + } + + static unsigned int next_index (unsigned int i, unsigned int start, unsigned int end) + { return (i >= end)? start: (i + 1); } + + public: + bool apply_deltas_to_points (hb_codepoint_t glyph, + const int *coords, unsigned int coord_count, + const hb_array_t points, + const hb_array_t end_points) const + { + if (unlikely (coord_count != gvar_table->axisCount)) return false; + + const GlyphVarData *var_data = gvar_table->get_glyph_var_data (glyph); + if (var_data == &Null(GlyphVarData)) return true; + hb_vector_t shared_indices; + GlyphVarData::tuple_iterator_t iterator; + if (!GlyphVarData::get_tuple_iterator (var_data, + gvar_table->get_glyph_var_data_length (glyph), + gvar_table->axisCount, + shared_indices, + &iterator)) + return false; + + /* Save original points for inferred delta calculation */ + contour_point_vector_t orig_points; + orig_points.resize (points.length); + for (unsigned int i = 0; i < orig_points.length; i++) + orig_points[i] = points[i]; + + contour_point_vector_t deltas; /* flag is used to indicate referenced point */ + deltas.resize (points.length); + + do { + float scalar = iterator.current_tuple->calculate_scalar (coords, coord_count, shared_tuples.as_array ()); + if (scalar == 0.f) continue; + const HBUINT8 *p = iterator.get_serialized_data (); + unsigned int length = iterator.current_tuple->get_data_size (); + if (unlikely (!iterator.in_range (p, length))) + return false; + + range_checker_t checker (p, 0, length); + hb_vector_t private_indices; + if (iterator.current_tuple->has_private_points () && + !GlyphVarData::unpack_points (p, private_indices, checker)) + return false; + const hb_array_t &indices = private_indices.length? private_indices: shared_indices; + + bool apply_to_all = (indices.length == 0); + unsigned int num_deltas = apply_to_all? points.length: indices.length; + hb_vector_t x_deltas; + x_deltas.resize (num_deltas); + if (!GlyphVarData::unpack_deltas (p, x_deltas, checker)) + return false; + hb_vector_t y_deltas; + y_deltas.resize (num_deltas); + if (!GlyphVarData::unpack_deltas (p, y_deltas, checker)) + return false; + + for (unsigned int i = 0; i < deltas.length; i++) + deltas[i].init (); + for (unsigned int i = 0; i < num_deltas; i++) + { + unsigned int pt_index = apply_to_all? i: indices[i]; + deltas[pt_index].flag = 1; /* this point is referenced, i.e., explicit deltas specified */ + deltas[pt_index].x += x_deltas[i] * scalar; + deltas[pt_index].y += y_deltas[i] * scalar; + } + + /* infer deltas for unreferenced points */ + unsigned int start_point = 0; + for (unsigned int c = 0; c < end_points.length; c++) + { + unsigned int end_point = end_points[c]; + unsigned int i, j; + + /* Check the number of unreferenced points in a contour. If no unref points or no ref points, nothing to do. */ + unsigned int unref_count = 0; + for (i = start_point; i <= end_point; i++) + if (!deltas[i].flag) unref_count++; + if (unref_count == 0 || unref_count > end_point - start_point) + goto no_more_gaps; + + j = start_point; + for (;;) + { + /* Locate the next gap of unreferenced points between two referenced points prev and next. + * Note that a gap may wrap around at left (start_point) and/or at right (end_point). + */ + unsigned int prev, next; + for (;;) { + i = j; + j = next_index (i, start_point, end_point); + if (deltas[i].flag && !deltas[j].flag) break; + } + prev = j = i; + for (;;) { + i = j; + j = next_index (i, start_point, end_point); + if (!deltas[i].flag && deltas[j].flag) break; + } + next = j; + /* Infer deltas for all unref points in the gap between prev and next */ + i = prev; + for (;;) { + i = next_index (i, start_point, end_point); + if (i == next) break; + deltas[i].x = infer_delta (orig_points.as_array (), deltas.as_array (), i, prev, next); + deltas[i].y = infer_delta (orig_points.as_array (), deltas.as_array (), i, prev, next); + if (--unref_count == 0) goto no_more_gaps; + } + } +no_more_gaps: + start_point = end_point + 1; + } + + /* apply specified / inferred deltas to points */ + for (unsigned int i = 0; i < points.length; i++) + { + points[i].x += (float) roundf (deltas[i].x); + points[i].y += (float) roundf (deltas[i].y); + } + } while (iterator.move_to_next ()); + + return true; + } + + unsigned int get_axis_count () const { return gvar_table->axisCount; } + + protected: + const GlyphVarData *get_glyph_var_data (hb_codepoint_t glyph) const + { return gvar_table->get_glyph_var_data (glyph); } + + private: + hb_blob_ptr_t gvar_table; + hb_vector_t shared_tuples; + }; + + protected: + FixedVersion<> version; /* Version of gvar table. Set to 0x00010000u. */ + HBUINT16 axisCount; + HBUINT16 sharedTupleCount; + LOffsetTo sharedTuples; /* LOffsetTo> */ + HBUINT16 glyphCount; + HBUINT16 flags; + LOffsetTo dataZ; /* Array of GlyphVarData */ + UnsizedArrayOf offsetZ; /* Array of 16-bit or 32-bit (glyphCount+1) offsets */ + + public: + DEFINE_SIZE_MIN (20); +}; + +struct gvar_accelerator_t : gvar::accelerator_t {}; + +} /* namespace OT */ + +#endif /* HB_OT_VAR_GVAR_TABLE_HH */ diff --git a/test/api/Makefile.am b/test/api/Makefile.am index 2386e53ea..9d4084ba0 100644 --- a/test/api/Makefile.am +++ b/test/api/Makefile.am @@ -88,6 +88,7 @@ TEST_PROGS += \ test-ot-metrics \ test-ot-tag \ test-ot-extents-cff \ + test-ot-metrics-tt-var \ $(NULL) diff --git a/test/api/fonts/SourceSansVariable-Roman-nohvar-41,C1.ttf b/test/api/fonts/SourceSansVariable-Roman-nohvar-41,C1.ttf new file mode 100644 index 0000000000000000000000000000000000000000..dc237e7904d6137cb9343c7e1418782ac8b241da GIT binary patch literal 4696 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 G{Qd)<{URO! literal 0 HcmV?d00001 diff --git a/test/api/fonts/SourceSansVariable-Roman.anchor.ttf b/test/api/fonts/SourceSansVariable-Roman.anchor.ttf new file mode 100644 index 0000000000000000000000000000000000000000..4e8dc9db4741fbdf45a1dfe4517a08f4c1e1fab5 GIT binary patch literal 4708 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 literal 0 HcmV?d00001 diff --git a/test/api/fonts/SourceSansVariable-Roman.modcomp.ttf b/test/api/fonts/SourceSansVariable-Roman.modcomp.ttf new file mode 100644 index 0000000000000000000000000000000000000000..c75041f5db7b7b8d17200bcc2d1e8ab6dfcad7ab GIT binary patch literal 3252 zcma)8TTEO<82)F@au!aPWueQmrDVI}vTSL&bh(t$2I+?05CMT=(-_OjZAo_*vRx^} zOMseqsU}8^G4Vm))ECp37=36O6Af(|qtzyrhsKAdF=`YWU6It5_50^6mujP(lbQLi z-~9i7=A0Q4L{x~uLR|-1_wBE$Y{@6eejU;s2f8|Y^g!$y_zU1)I?!`C^jhJpizufT ze8a)Mz1>I3-+KuBOW>Ox0>5pcd>44O-_zMs9X$GZvXe;6gYWO@-P@aMT>hTOuz>#} zJP}Szd19Y{zXaYt5DSlsZ8_OQlJTD(hW@$bli-ad_8*B(4d43JHVgd?VERU&??3Ru zb)pSNz?Vjb!h`vdAFZGtg?&Q=0=bo-7o3kT5=%}RAKxy9eX$+Cpf{oq@=V0hrq95FQ8jd5 zhVBK&G5RnYn0sMSgE<=yXMJAi!j?|2QR&^wx0jdJ`~j{w;~-+AFZzOxV--M)5ngz+)+{C z+*XSm8q-0>mwt}kVI`~RCPT@wu6D$fla;bW^E&xqQ=V-l9r9cf>8o;%B3cLMq?PY1 za@i+ba?Ua7Mh@5$b2O_R1d7tt?kuv}llFuqVNc4rgkx~fk$}~ibbwk=pFARKc|${e zoxgl%t<~i!bn0z-sHSXdpv2?#@1HNu+UzPS()(JiTRdd}6p38I&Tq_;19}eKqy+`Q znY6^0vXG%Q>ZV&6+Op0jiv|V9&p0~Oh!0I#SjiCAGITcC#LpQz2lP^gHpq#+seHu- zs*~;vZ6iarXXsq2kbN22z7~(jqbfO_kvqsCKgiJeR3p!2=mN@?nfS#<3gD|X*K(4f zc`|e%RcHZvANxIseY6AJOO>l!BdEPJNMqPrL!f(TicU}xbPThX4pE#2piR)Y8pBFA z2u*+@pp%d{;p?rYlj^CW@G4rB@{B>ZmA1pMZPo>3*=0nZwh{chyiN+If` z-FINEApH}k5=di+hSxEDJK?>L`hcIn%A+c3FRHq>LIkp;$~=zjR&-UW^1Fb-Xvg8t zR>PPl(fQp}Q9XJC#(LU?d0pmDVizWI#)Q z$!FTFoqagg&TKHeC`T`p>ZD6l9lcjZkdoL9aOeLaJJdzkXPQ}r1 zGET+jiGV+`@;g41h~xBGL9r13>jWf%PZV`7d9mY(D)2}p17o)IPdJG3c@8^OMSLVrjzp3I6&X#2qk?PVz}el1-QSPz;CXrz>#)0lI|iwK z6SFC^iS3}lIDo+Otae5Qk8Q!D z_Bc_vaShBm2(o@kpR$Z{%JkuaLq#w9UiA-Geq499g<4q5b`C@=P;Z?K&UYmjwGN3K zWT$cy8`PZwS&3X>$H=+l2?rwO4Dv+*D8wQ*3cNe^7KIAmth`^e ze5X&h&@}J3HpmN#-FhmOqP$|acSlp|PkTuy9->)(jN{FoyrmziD%|9rnc1BBCAD;O zX>oD!_k}l3Pft(3edT6J@5IDJ{HVM=D{%W%m>m1Gw8P_+SS(pD^C(%<9d@P+xhY>{ zMi%=eNAJ(G+SSsg>ocOsXKIR|k!LR1W@M_S>y&lzH+yNR{kO##VKB;ixKiRN^ZWg`Pvu>=8G`;yrKHp?wEWAp86itxi4uy`GVW@)7k8wmEWB&Y_Xk2X T+?YiQ`=|?dfDZi4_@Tc6i^d2h literal 0 HcmV?d00001 diff --git a/test/api/fonts/SourceSerifVariable-Roman-VVAR.abc.ttf b/test/api/fonts/SourceSerifVariable-Roman-VVAR.abc.ttf new file mode 100644 index 0000000000000000000000000000000000000000..7d94abcb0b2cd08e99adcc2d59f51cb3371c12e3 GIT binary patch literal 5632 zcmbVQX>b$Q9sj@G)vhkfmMqD(u#GI+G8iPwhinc5Mga$i!2}Z^orEA8UyZNIhCn~0 zm`T!3rcK+lli@>~CTYL)LpyCgv}va^rR_|I=}bs6kW2`HCdLU2<}#N7U;X`e*VrVa zIaag#{`dP|?`hwXAfik>6pF3ewt7ot!Gn8=G6q21un~Cw^L?)aKMcIKv8k-$@s0ay ziNqVgcQtKYz4gHGi+SKr0DpZOF-2e#QgAq zsyjeXw~H0P+25j$?ty*QSLvV7<@qwYdRvm>M|2hV0pRxTEV!9=RXFL_1 zqR<-Dg)g4z-bD*8iOQ$#pcCH>17N-l992^VuGU5D#{U`(jri0^Uejt7J0N|tc8z3j0%2CZ<(IP zGdVpy{farW6FAEo7t17Zx8P>Dh@PXPLIb4GW_TWh4>D=wpmd^2Q7H-vMWwsIT_EGJ zS^TDU>M{LMD>|kQ)rya#mnWNX|6E#z8!${zbo!dOA8rm=!5a zD+_t@N`hWjUVd3Dzot>77FJ}I7Puq13j$iUw^Vj@JhZXF>rV4ny`F`AxfQW&YlcfH z(z0!yqL6Dr$R6;g`||QjGFR0ui4K>?y_#eGHhM@ zbZN0|@vh4Fw#w?qmqov|z*f>&=&kWLJUm~#VK0nWUpSmLb@b7y(ygnLZLV;!CsDhs zXXD2HRq;(36{TFMMab#DkkcX{m=)>aL~?#GX*iA~BWwOF-G!2@a9Ct6S`%KlzHHHk ztZeTS^=n$IS9jDdZm3YTa88ybCz?|s{uf$lOt! zX7o%eF=8yqHNu^(!us|>W z+Jq&VN53;+8S+O=SRp^XX~LFSAB0NTbjGA>WTkNvwqorVYlVc36k+-)?Bo+cQ^r9S z5jSBc`Nc9`U5X8n{ab7{VL_#m--IP{Np&VH(^hGd2`h7CER>R7GwCW-$X*lH=&)RG z!d9wMR1>yQNST?}K^4klCOu6KDleF@i>kEWo3NYg+K>t7P*A&Iz{sc5>NH_NUTd}q zOH^%LY{J+xtj#8@&|+(s30r34LOx^GLnd9L4C@gSwo--leG|5k)n+$gJC)izChQ=) zt=5E{wAfbP)Vn9u(i&`PO?9>fwy%sEv@Z)x{|5#XskT8oOi_KhL|ojBNW-v z+nwwIyD_nLTWhMnv$rQ`Xl!m`V0lOHKud4W-e7rkv^pBETHc*}vbA@hE!x%DToH{& ztK;#du`jf%8{gjA+1@d*cHclM*}%{oX`LULW!;~Df2tW?Q)_o;b8pw~&sm*eB<{y# zVxlYA^5o~#bSnLpg7}Y&Q8`rsirmF&0IjCo)Jx6O3b={(Q$Gy=?#39T_0)qszn4mM^YRK{(M>Uq~wx5JGmTCjF3pSfkE#|Xa9KWUT{pE|&*L}ig-$&KTwCs;M}TpqjmkCwGP=f?y$AD_VYZY$r=i0z=VuQz*|$#U z_9OOh;!V9CTaO;ogQl(j!ZYqF?yD5wuiCc*SPgy9*$N#ad$WDYxov}`r}PYR+=c{K z5B-WrxWl=(IV#RIM>&(DCakOm;ElR{?w?u94fj~!m2%dqUbJrpFLOrtlFh;J?GK}|TrysF!J^H}qig3SoVdVO?qmGTxZ}^NSvypsJj&{Qe z?=oz;8Tpy%6=NmwO5@ePT3Ameit!bo=$LXu&LPxLaVA>(`_2Xh}RRbtS-Oh0m8t*@j1b!6}T=izv`2F=KC7dqw3ef_LP;wx*; zNG9}dX~7Qo<+IJo@<|cb^ie)b-{%rK&5LK^E|+Oe{XLl9lBL6RSZCGYsETtTzw<*P zI7w72#VmjofkP_jhAk^bZ~s85S6BLfwwR~uO|g!^+d5Occ$?=Fs`M0I!gP}^3gQ29 z1MNg7Kc{Pu5I6~te%~>zkdVb)ae=eTtSc*1OI#u5pU2W#{~H zhQzZw3iJrTK;HDM2??hm;(Q53;LC-cC6!P9dTy*ZBN0c*ZW-|lqazRtEadBc6Y4^E zWBiARi02N&hXWG&rU0}8Om)CsNT&$*slvij-#;KU?VwfDhKCCa2_U9AV7~xIaoca+ zv_s(1rGwT>m+bc8VFD;Fkzwzru=g|ATjg+Mx+Y{*Q4|_Gcj4pX$B%z_?y4gYaLZYK zmqL=m<|@d~&-dHq(T`5Q{r-pNt~u?_i7OY*Q+; zIXQ?AGOI>LuB+b6?A$;gJAeMl4T(geyg1;ve)`UhGb2~8oINu#b>-~o)2BbVbmh|M zNM@ue;2OXD@%7M(dR)sZ78P5<+XRaJRu;?{(Uk6`0M!nYyCYO$)CYSYLvctDH}Ka-nSve0U^ z-kuy+hlbAGws``9fZvOtTw+W5(!OHY_M!PJ$;%m?WbohUOa!XsA%j?I2`o5q~YP= zH;)Vt-9%vSGz%f_$^5*6NO(|Ol=aTqg1DLxS6IF^{N8)-ogTS%eOwv4ct^E6Yj}O^?w + +/* Unit tests for glyph advance widths and extents of TrueType variable fonts */ + +static void +test_extents_tt_var (void) +{ + hb_face_t *face = hb_test_open_font_file ("fonts/SourceSansVariable-Roman-nohvar-41,C1.ttf"); + g_assert (face); + hb_font_t *font = hb_font_create (face); + hb_face_destroy (face); + g_assert (font); + hb_ot_font_set_funcs (font); + + hb_glyph_extents_t extents; + hb_bool_t result = hb_font_get_glyph_extents (font, 2, &extents); + g_assert (result); + + g_assert_cmpint (extents.x_bearing, ==, 10); + g_assert_cmpint (extents.y_bearing, ==, 846); + g_assert_cmpint (extents.width, ==, 500); + g_assert_cmpint (extents.height, ==, -846); + + float coords[1] = { 500.0f }; + hb_font_set_var_coords_design (font, coords, 1); + result = hb_font_get_glyph_extents (font, 2, &extents); + g_assert (result); + + g_assert_cmpint (extents.x_bearing, ==, 0); + g_assert_cmpint (extents.y_bearing, ==, 874); + g_assert_cmpint (extents.width, ==, 551); + g_assert_cmpint (extents.height, ==, -874); + + hb_font_destroy (font); +} + +static void +test_advance_tt_var_nohvar (void) +{ + hb_face_t *face = hb_test_open_font_file ("fonts/SourceSansVariable-Roman-nohvar-41,C1.ttf"); + g_assert (face); + hb_font_t *font = hb_font_create (face); + hb_face_destroy (face); + g_assert (font); + hb_ot_font_set_funcs (font); + + hb_position_t x, y; + hb_font_get_glyph_advance_for_direction(font, 2, HB_DIRECTION_LTR, &x, &y); + + g_assert_cmpint (x, ==, 520); + g_assert_cmpint (y, ==, 0); + + hb_font_get_glyph_advance_for_direction(font, 2, HB_DIRECTION_TTB, &x, &y); + + g_assert_cmpint (x, ==, 0); + g_assert_cmpint (y, ==, -1000); + + float coords[1] = { 500.0f }; + hb_font_set_var_coords_design (font, coords, 1); + hb_font_get_glyph_advance_for_direction(font, 2, HB_DIRECTION_LTR, &x, &y); + + g_assert_cmpint (x, ==, 551); + g_assert_cmpint (y, ==, 0); + + hb_font_get_glyph_advance_for_direction(font, 2, HB_DIRECTION_TTB, &x, &y); + + g_assert_cmpint (x, ==, 0); + g_assert_cmpint (y, ==, -1000); + + hb_font_destroy (font); +} + +static void +test_advance_tt_var_hvarvvar (void) +{ + hb_face_t *face = hb_test_open_font_file ("fonts/SourceSerifVariable-Roman-VVAR.abc.ttf"); + g_assert (face); + hb_font_t *font = hb_font_create (face); + hb_face_destroy (face); + g_assert (font); + hb_ot_font_set_funcs (font); + + hb_position_t x, y; + hb_font_get_glyph_advance_for_direction(font, 1, HB_DIRECTION_LTR, &x, &y); + + g_assert_cmpint (x, ==, 508); + g_assert_cmpint (y, ==, 0); + + hb_font_get_glyph_advance_for_direction(font, 1, HB_DIRECTION_TTB, &x, &y); + + g_assert_cmpint (x, ==, 0); + g_assert_cmpint (y, ==, -1000); + + float coords[1] = { 700.0f }; + hb_font_set_var_coords_design (font, coords, 1); + hb_font_get_glyph_advance_for_direction(font, 1, HB_DIRECTION_LTR, &x, &y); + + g_assert_cmpint (x, ==, 531); + g_assert_cmpint (y, ==, 0); + + hb_font_get_glyph_advance_for_direction(font, 1, HB_DIRECTION_TTB, &x, &y); + + g_assert_cmpint (x, ==, 0); + g_assert_cmpint (y, ==, -1012); + + hb_font_destroy (font); +} + +static void +test_advance_tt_var_anchor (void) +{ + hb_face_t *face = hb_test_open_font_file ("fonts/SourceSansVariable-Roman.anchor.ttf"); + g_assert (face); + hb_font_t *font = hb_font_create (face); + hb_face_destroy (face); + g_assert (font); + hb_ot_font_set_funcs (font); + + hb_glyph_extents_t extents; + hb_bool_t result = hb_font_get_glyph_extents (font, 2, &extents); + g_assert (result); + + g_assert_cmpint (extents.x_bearing, ==, 56); + g_assert_cmpint (extents.y_bearing, ==, 672); + g_assert_cmpint (extents.width, ==, 556); + g_assert_cmpint (extents.height, ==, -684); + + float coords[1] = { 500.0f }; + hb_font_set_var_coords_design (font, coords, 1); + result = hb_font_get_glyph_extents (font, 2, &extents); + g_assert (result); + + g_assert_cmpint (extents.x_bearing, ==, 50); + g_assert_cmpint (extents.y_bearing, ==, 667); + g_assert_cmpint (extents.width, ==, 593); + g_assert_cmpint (extents.height, ==, -679); + + hb_font_destroy (font); +} + +static void +test_extents_tt_var_comp (void) +{ + hb_face_t *face = hb_test_open_font_file ("fonts/SourceSansVariable-Roman.modcomp.ttf"); + g_assert (face); + hb_font_t *font = hb_font_create (face); + hb_face_destroy (face); + g_assert (font); + hb_ot_font_set_funcs (font); + + hb_glyph_extents_t extents; + float coords[1] = { 800.0f }; + hb_font_set_var_coords_design (font, coords, 1); + + hb_bool_t result; + result = hb_font_get_glyph_extents (font, 2, &extents); /* Ccedilla, cedilla y-scaled by 0.8, with unscaled component offset */ + g_assert (result); + + g_assert_cmpint (extents.x_bearing, ==, 19); + g_assert_cmpint (extents.y_bearing, ==, 663); + g_assert_cmpint (extents.width, ==, 519); + g_assert_cmpint (extents.height, ==, -895); + + result = hb_font_get_glyph_extents (font, 3, &extents); /* Cacute, acute y-scaled by 0.8, with unscaled component offset (default) */ + g_assert (result); + + g_assert_cmpint (extents.x_bearing, ==, 19); + g_assert_cmpint (extents.y_bearing, ==, 909); + g_assert_cmpint (extents.width, ==, 519); + g_assert_cmpint (extents.height, ==, -921); + + result = hb_font_get_glyph_extents (font, 4, &extents); /* Ccaron, caron y-scaled by 0.8, with scaled component offset */ + g_assert (result); + + g_assert_cmpint (extents.x_bearing, ==, 19); + g_assert_cmpint (extents.y_bearing, ==, 866); + g_assert_cmpint (extents.width, ==, 519); + g_assert_cmpint (extents.height, ==, -878); + + hb_font_destroy (font); +} + +static void +test_advance_tt_var_comp_v (void) +{ + hb_face_t *face = hb_test_open_font_file ("fonts/SourceSansVariable-Roman.modcomp.ttf"); + g_assert (face); + hb_font_t *font = hb_font_create (face); + hb_face_destroy (face); + g_assert (font); + hb_ot_font_set_funcs (font); + + float coords[1] = { 800.0f }; + hb_font_set_var_coords_design (font, coords, 1); + + hb_position_t x, y; + hb_font_get_glyph_advance_for_direction(font, 2, HB_DIRECTION_TTB, &x, &y); /* No VVAR; 'C' in composite Ccedilla determines metrics */ + + g_assert_cmpint (x, ==, 0); + g_assert_cmpint (y, ==, -991); + + hb_font_get_glyph_origin_for_direction(font, 2, HB_DIRECTION_TTB, &x, &y); + + g_assert_cmpint (x, ==, 292); + g_assert_cmpint (y, ==, 1013); + + hb_font_destroy (font); +} + +int +main (int argc, char **argv) +{ + hb_test_init (&argc, &argv); + + hb_test_add (test_extents_tt_var); + hb_test_add (test_advance_tt_var_nohvar); + hb_test_add (test_advance_tt_var_hvarvvar); + hb_test_add (test_advance_tt_var_anchor); + hb_test_add (test_extents_tt_var_comp); + hb_test_add (test_advance_tt_var_comp_v); + + return hb_test_run (); +} From c9577a630fd5bd08482ee7b94bc03c91fcf366c7 Mon Sep 17 00:00:00 2001 From: Ebrahim Byagowi Date: Thu, 3 Oct 2019 15:48:35 +0330 Subject: [PATCH 2/8] [hmtx] Use more optimal way to access glyf table --- src/hb-ot-hmtx-table.cc | 20 ++++---------------- src/hb-ot-hmtx-table.hh | 4 ++-- 2 files changed, 6 insertions(+), 18 deletions(-) diff --git a/src/hb-ot-hmtx-table.cc b/src/hb-ot-hmtx-table.cc index 012a9f2d9..93afd5610 100644 --- a/src/hb-ot-hmtx-table.cc +++ b/src/hb-ot-hmtx-table.cc @@ -29,26 +29,14 @@ namespace OT { -int hmtxvmtx_accelerator_base_t::get_side_bearing_var_tt (hb_font_t *font, hb_codepoint_t glyph, bool vertical) +int hmtxvmtx_accelerator_base_t::get_side_bearing_var_tt (hb_font_t *font, hb_codepoint_t glyph, bool is_vertical) { - glyf::accelerator_t glyf_accel; - glyf_accel.init (font->face); - - int side_bearing = glyf_accel.get_side_bearing_var (glyph, font->coords, font->num_coords, vertical); - glyf_accel.fini (); - - return side_bearing; + return font->face->table.glyf->get_side_bearing_var (glyph, font->coords, font->num_coords, is_vertical); } -unsigned int hmtxvmtx_accelerator_base_t::get_advance_var_tt (hb_font_t *font, hb_codepoint_t glyph, bool vertical) +unsigned int hmtxvmtx_accelerator_base_t::get_advance_var_tt (hb_font_t *font, hb_codepoint_t glyph, bool is_vertical) { - glyf::accelerator_t glyf_accel; - glyf_accel.init (font->face); - - unsigned int advance = glyf_accel.get_advance_var (glyph, font->coords, font->num_coords, vertical); - glyf_accel.fini (); - - return advance; + return font->face->table.glyf->get_advance_var (glyph, font->coords, font->num_coords, is_vertical); } } diff --git a/src/hb-ot-hmtx-table.hh b/src/hb-ot-hmtx-table.hh index 90c9c4866..f5c094463 100644 --- a/src/hb-ot-hmtx-table.hh +++ b/src/hb-ot-hmtx-table.hh @@ -55,8 +55,8 @@ struct LongMetric struct hmtxvmtx_accelerator_base_t { - HB_INTERNAL static int get_side_bearing_var_tt (hb_font_t *font, hb_codepoint_t glyph, bool vertical); - HB_INTERNAL static unsigned int get_advance_var_tt (hb_font_t *font, hb_codepoint_t glyph, bool vertical); + HB_INTERNAL static int get_side_bearing_var_tt (hb_font_t *font, hb_codepoint_t glyph, bool is_vertical); + HB_INTERNAL static unsigned int get_advance_var_tt (hb_font_t *font, hb_codepoint_t glyph, bool is_vertical); }; template From ee7610181cee5bbc0b5dc291228fbe032097bde4 Mon Sep 17 00:00:00 2001 From: Ebrahim Byagowi Date: Thu, 3 Oct 2019 15:59:45 +0330 Subject: [PATCH 3/8] [hmtx] Use more conventional internal API style --- src/Makefile.sources | 1 - src/harfbuzz.cc | 1 - src/hb-ot-font.cc | 12 ++++++++++++ src/hb-ot-hmtx-table.cc | 42 ----------------------------------------- src/hb-ot-hmtx-table.hh | 18 ++++++++++-------- 5 files changed, 22 insertions(+), 52 deletions(-) delete mode 100644 src/hb-ot-hmtx-table.cc diff --git a/src/Makefile.sources b/src/Makefile.sources index 31cd31f89..911ee2f54 100644 --- a/src/Makefile.sources +++ b/src/Makefile.sources @@ -75,7 +75,6 @@ HB_BASE_sources = \ hb-ot-hdmx-table.hh \ hb-ot-head-table.hh \ hb-ot-hhea-table.hh \ - hb-ot-hmtx-table.cc \ hb-ot-hmtx-table.hh \ hb-ot-kern-table.hh \ hb-ot-layout-base-table.hh \ diff --git a/src/harfbuzz.cc b/src/harfbuzz.cc index 2aa5a7942..fe1c36023 100644 --- a/src/harfbuzz.cc +++ b/src/harfbuzz.cc @@ -14,7 +14,6 @@ #include "hb-ot-color.cc" #include "hb-ot-face.cc" #include "hb-ot-font.cc" -#include "hb-ot-hmtx-table.cc" #include "hb-ot-layout.cc" #include "hb-ot-map.cc" #include "hb-ot-math.cc" diff --git a/src/hb-ot-font.cc b/src/hb-ot-font.cc index 4b7446e62..283cfb689 100644 --- a/src/hb-ot-font.cc +++ b/src/hb-ot-font.cc @@ -315,5 +315,17 @@ hb_ot_font_set_funcs (hb_font_t *font) nullptr); } +int +hb_ot_get_side_bearing_var_tt (hb_font_t *font, hb_codepoint_t glyph, bool is_vertical) +{ + return font->face->table.glyf->get_side_bearing_var (glyph, font->coords, font->num_coords, is_vertical); +} + +unsigned +hb_ot_get_advance_var_tt (hb_font_t *font, hb_codepoint_t glyph, bool is_vertical) +{ + return font->face->table.glyf->get_advance_var (glyph, font->coords, font->num_coords, is_vertical); +} + #endif diff --git a/src/hb-ot-hmtx-table.cc b/src/hb-ot-hmtx-table.cc deleted file mode 100644 index 93afd5610..000000000 --- a/src/hb-ot-hmtx-table.cc +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Copyright © 2019 Adobe Inc. - * - * This is part of HarfBuzz, a text shaping library. - * - * Permission is hereby granted, without written agreement and without - * license or royalty fees, to use, copy, modify, and distribute this - * software and its documentation for any purpose, provided that the - * above copyright notice and the following two paragraphs appear in - * all copies of this software. - * - * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR - * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES - * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN - * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH - * DAMAGE. - * - * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, - * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND - * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS - * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO - * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. - * - * Adobe Author(s): Michiharu Ariza - */ - -#include "hb-ot-hmtx-table.hh" -#include "hb-ot-glyf-table.hh" - -namespace OT { - -int hmtxvmtx_accelerator_base_t::get_side_bearing_var_tt (hb_font_t *font, hb_codepoint_t glyph, bool is_vertical) -{ - return font->face->table.glyf->get_side_bearing_var (glyph, font->coords, font->num_coords, is_vertical); -} - -unsigned int hmtxvmtx_accelerator_base_t::get_advance_var_tt (hb_font_t *font, hb_codepoint_t glyph, bool is_vertical) -{ - return font->face->table.glyf->get_advance_var (glyph, font->coords, font->num_coords, is_vertical); -} - -} diff --git a/src/hb-ot-hmtx-table.hh b/src/hb-ot-hmtx-table.hh index f5c094463..8a7395982 100644 --- a/src/hb-ot-hmtx-table.hh +++ b/src/hb-ot-hmtx-table.hh @@ -42,6 +42,13 @@ #define HB_OT_TAG_vmtx HB_TAG('v','m','t','x') +HB_INTERNAL int +hb_ot_get_side_bearing_var_tt (hb_font_t *font, hb_codepoint_t glyph, bool is_vertical); + +HB_INTERNAL unsigned +hb_ot_get_advance_var_tt (hb_font_t *font, hb_codepoint_t glyph, bool is_vertical); + + namespace OT { @@ -53,11 +60,6 @@ struct LongMetric DEFINE_SIZE_STATIC (4); }; -struct hmtxvmtx_accelerator_base_t -{ - HB_INTERNAL static int get_side_bearing_var_tt (hb_font_t *font, hb_codepoint_t glyph, bool is_vertical); - HB_INTERNAL static unsigned int get_advance_var_tt (hb_font_t *font, hb_codepoint_t glyph, bool is_vertical); -}; template struct hmtxvmtx @@ -156,7 +158,7 @@ struct hmtxvmtx return_trace (true); } - struct accelerator_t : hmtxvmtx_accelerator_base_t + struct accelerator_t { friend struct hmtxvmtx; @@ -213,7 +215,7 @@ struct hmtxvmtx return side_bearing; if (var_table.get_blob () == &Null (hb_blob_t)) - return get_side_bearing_var_tt (font, glyph, T::tableTag == HB_OT_TAG_vmtx); + return hb_ot_get_side_bearing_var_tt (font, glyph, T::tableTag == HB_OT_TAG_vmtx); return side_bearing + var_table->get_side_bearing_var (glyph, font->coords, font->num_coords); // TODO Optimize?! } @@ -243,7 +245,7 @@ struct hmtxvmtx return advance; if (var_table.get_blob () == &Null (hb_blob_t)) - return get_advance_var_tt (font, glyph, T::tableTag == HB_OT_TAG_vmtx); + return hb_ot_get_advance_var_tt (font, glyph, T::tableTag == HB_OT_TAG_vmtx); return advance + roundf (var_table->get_advance_var (glyph, font->coords, font->num_coords)); // TODO Optimize?! } From 88bd342c0ff686daad88bb6b4f2968bbd8b01a24 Mon Sep 17 00:00:00 2001 From: Ebrahim Byagowi Date: Thu, 3 Oct 2019 22:45:37 +0330 Subject: [PATCH 4/8] [glyf] minor --- src/hb-ot-glyf-table.hh | 249 ++++++++++++++++++------------------ src/hb-ot-var-gvar-table.hh | 54 ++++---- 2 files changed, 156 insertions(+), 147 deletions(-) diff --git a/src/hb-ot-glyf-table.hh b/src/hb-ot-glyf-table.hh index 72c138f6d..a3d1f8c34 100644 --- a/src/hb-ot-glyf-table.hh +++ b/src/hb-ot-glyf-table.hh @@ -60,11 +60,12 @@ struct loca } protected: - UnsizedArrayOf dataZ; /* Location data. */ + UnsizedArrayOf + dataZ; /* Location data. */ public: - DEFINE_SIZE_MIN (0); /* In reality, this is UNBOUNDED() type; but since we always - * check the size externally, allow Null() object of it by - * defining it _MIN instead. */ + DEFINE_SIZE_MIN (0); /* In reality, this is UNBOUNDED() type; but since we always + * check the size externally, allow Null() object of it by + * defining it _MIN instead. */ }; @@ -290,14 +291,15 @@ struct glyf bool has_data () const { return numberOfContours; } - 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. */ + 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. */ DEFINE_SIZE_STATIC (10); }; @@ -347,8 +349,8 @@ struct glyf const HBUINT8 *p = &StructAfter (glyphIndex); if (flags & ARG_1_AND_2_ARE_WORDS) { - point1 = ((const HBUINT16 *)p)[0]; - point2 = ((const HBUINT16 *)p)[1]; + point1 = ((const HBUINT16 *) p)[0]; + point2 = ((const HBUINT16 *) p)[1]; } else { @@ -378,7 +380,7 @@ struct glyf protected: bool scaled_offsets () const - { return (flags & (SCALED_COMPONENT_OFFSET|UNSCALED_COMPONENT_OFFSET)) == SCALED_COMPONENT_OFFSET; } + { return (flags & (SCALED_COMPONENT_OFFSET | UNSCALED_COMPONENT_OFFSET)) == SCALED_COMPONENT_OFFSET; } bool get_transformation (float (&matrix)[4], contour_point_t &trans) const { @@ -389,9 +391,9 @@ struct glyf const HBINT8 *p = &StructAfter (glyphIndex); if (flags & ARG_1_AND_2_ARE_WORDS) { - tx = *(const HBINT16 *)p; + tx = *(const HBINT16 *) p; p += HBINT16::static_size; - ty = *(const HBINT16 *)p; + ty = *(const HBINT16 *) p; p += HBINT16::static_size; } else @@ -401,26 +403,29 @@ struct glyf } if (is_anchored ()) tx = ty = 0; - trans.init ((float)tx, (float)ty); + trans.init ((float) tx, (float) ty); - if (flags & WE_HAVE_A_SCALE) { - matrix[0] = matrix[3] = ((const F2DOT14*)p)->to_float (); - return true; - } - else if (flags & WE_HAVE_AN_X_AND_Y_SCALE) - { - matrix[0] = ((const F2DOT14*)p)[0].to_float (); - matrix[3] = ((const F2DOT14*)p)[1].to_float (); - return true; - } - else if (flags & WE_HAVE_A_TWO_BY_TWO) - { - matrix[0] = ((const F2DOT14*)p)[0].to_float (); - matrix[1] = ((const F2DOT14*)p)[1].to_float (); - matrix[2] = ((const F2DOT14*)p)[2].to_float (); - matrix[3] = ((const F2DOT14*)p)[3].to_float (); - return true; + 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; } @@ -449,8 +454,8 @@ struct glyf bool in_range (const CompositeGlyphHeader *composite) const { return (const char *) composite >= glyph_start - && ((const char *) composite + CompositeGlyphHeader::min_size) <= glyph_end - && ((const char *) composite + composite->get_size ()) <= glyph_end; + && ((const char *) composite + CompositeGlyphHeader::min_size) <= glyph_end + && ((const char *) composite + composite->get_size ()) <= glyph_end; } }; @@ -543,7 +548,8 @@ struct glyf FLAG_RESERVED2 = 0x80 }; - enum phantom_point_index_t { + enum phantom_point_index_t + { PHANTOM_LEFT = 0, PHANTOM_RIGHT = 1, PHANTOM_TOP = 2, @@ -556,7 +562,7 @@ struct glyf { unsigned int start_offset, end_offset; if (!get_offsets (glyph, &start_offset, &end_offset) || end_offset - start_offset < GlyphHeader::static_size) - return Null(GlyphHeader); + return Null (GlyphHeader); return StructAtOffset (glyf_table, start_offset); } @@ -564,15 +570,15 @@ struct glyf struct x_setter_t { void set (contour_point_t &point, float v) const { point.x = v; } - bool is_short (uint8_t flag) const { return (flag & FLAG_X_SHORT) != 0; } - bool is_same (uint8_t flag) const { return (flag & FLAG_X_SAME) != 0; } + bool is_short (uint8_t flag) const { return flag & FLAG_X_SHORT; } + bool is_same (uint8_t flag) const { return flag & FLAG_X_SAME; } }; struct y_setter_t { void set (contour_point_t &point, float v) const { point.y = v; } - bool is_short (uint8_t flag) const { return (flag & FLAG_Y_SHORT) != 0; } - bool is_same (uint8_t flag) const { return (flag & FLAG_Y_SAME) != 0; } + bool is_short (uint8_t flag) const { return flag & FLAG_Y_SHORT; } + bool is_same (uint8_t flag) const { return flag & FLAG_Y_SAME; } }; template @@ -598,7 +604,7 @@ struct glyf if (!coord_setter.is_same (flag)) { if (unlikely (!checker.in_range ((const HBUINT16 *)p))) return false; - v += *(const HBINT16 *)p; + v += *(const HBINT16 *) p; p += HBINT16::static_size; } } @@ -610,15 +616,15 @@ struct glyf void init_phantom_points (hb_codepoint_t glyph, hb_array_t &phantoms /* IN/OUT */) const { const GlyphHeader &header = get_header (glyph); - int h_delta = (int)header.xMin - hmtx_accel.get_side_bearing (glyph); - int v_orig = (int)header.yMax + vmtx_accel.get_side_bearing (glyph); + int h_delta = (int) header.xMin - hmtx_accel.get_side_bearing (glyph); + int v_orig = (int) header.yMax + vmtx_accel.get_side_bearing (glyph); unsigned int h_adv = hmtx_accel.get_advance (glyph); unsigned int v_adv = vmtx_accel.get_advance (glyph); phantoms[PHANTOM_LEFT].x = h_delta; phantoms[PHANTOM_RIGHT].x = h_adv + h_delta; phantoms[PHANTOM_TOP].y = v_orig; - phantoms[PHANTOM_BOTTOM].y = -(int)v_adv + v_orig; + phantoms[PHANTOM_BOTTOM].y = v_orig - (int) v_adv; } /* for a simple glyph, return contour end points, flags, along with coordinate points @@ -635,16 +641,16 @@ struct glyf if (unlikely (!get_offsets (glyph, &start_offset, &end_offset))) return false; if (unlikely (end_offset - start_offset < GlyphHeader::static_size)) { - /* empty glyph */ + /* empty glyph */ points_.resize (PHANTOM_COUNT); for (unsigned int i = 0; i < points_.length; i++) points_[i].init (); - return true; + return true; } CompositeGlyphHeader::Iterator composite; if (get_composite (glyph, &composite)) { - /* For a composite glyph, add one pseudo point for each component */ + /* For a composite glyph, add one pseudo point for each component */ do { num_points++; } while (composite.move_to_next()); points_.resize (num_points + PHANTOM_COUNT); for (unsigned int i = 0; i < points_.length; i++) points_[i].init (); @@ -652,17 +658,17 @@ struct glyf } const GlyphHeader &glyph_header = StructAtOffset (glyf_table, start_offset); - int16_t num_contours = (int16_t) glyph_header.numberOfContours; + int num_contours = glyph_header.numberOfContours; const HBUINT16 *end_pts = &StructAfter (glyph_header); range_checker_t checker (glyf_table, start_offset, end_offset); num_points = 0; - if (num_contours > 0) + if (glyph_header.is_simple_glyph ()) { if (unlikely (!checker.in_range (&end_pts[num_contours + 1]))) return false; - num_points = end_pts[num_contours - 1] + 1; + num_points = end_pts[glyph_header.numberOfContours - 1] + 1; } - else if (num_contours < 0) + else if (glyph_header.is_composite_glyph ()) { CompositeGlyphHeader::Iterator composite; if (unlikely (!get_composite (glyph, &composite))) return false; @@ -674,22 +680,23 @@ struct glyf points_.resize (num_points + PHANTOM_COUNT); for (unsigned int i = 0; i < points_.length; i++) points_[i].init (); - if ((num_contours <= 0) || phantom_only) return true; + if (!glyph_header.is_simple_glyph () || phantom_only) return true; /* Read simple glyph points if !phantom_only */ end_points_.resize (num_contours); - for (int16_t i = 0; i < num_contours; i++) - end_points_[i] = end_pts[i]; + for (int i = 0; i < num_contours; i++) + end_points_[i] = end_pts[i]; /* Skip instructions */ - const HBUINT8 *p = &StructAtOffset (&end_pts[num_contours+1], end_pts[num_contours]); + const HBUINT8 *p = &StructAtOffset (&end_pts[num_contours+1], + end_pts[num_contours]); /* Read flags */ for (unsigned int i = 0; i < num_points; i++) { - if (unlikely (!checker.in_range (p))) return false; - uint8_t flag = *p++; + if (unlikely (!checker.in_range (p))) return false; + uint8_t flag = *p++; points_[i].flag = flag; if ((flag & FLAG_REPEAT) != 0) { @@ -732,8 +739,8 @@ struct glyf unsigned int depth=0) const { if (unlikely (depth++ > HB_MAX_NESTING_LEVEL)) return false; - contour_point_vector_t points; - hb_vector_t end_points; + contour_point_vector_t points; + hb_vector_t end_points; if (unlikely (!get_contour_points (glyph, points, end_points))) return false; hb_array_t phantoms = points.sub_array (points.length-PHANTOM_COUNT, PHANTOM_COUNT); init_phantom_points (glyph, phantoms); @@ -743,7 +750,7 @@ struct glyf CompositeGlyphHeader::Iterator composite; if (!get_composite (glyph, &composite)) { - /* simple glyph */ + /* simple glyph */ all_points.extend (points.as_array ()); } else @@ -753,8 +760,8 @@ struct glyf { contour_point_vector_t comp_points; if (unlikely (!get_points_var (composite.current->glyphIndex, coords, coord_count, - comp_points, depth)) - || comp_points.length < PHANTOM_COUNT) return false; + comp_points, depth)) + || comp_points.length < PHANTOM_COUNT) return false; /* Copy phantom points from component if USE_MY_METRICS flag set */ if (composite.current->flags & CompositeGlyphHeader::USE_MY_METRICS) @@ -775,7 +782,7 @@ struct glyf { contour_point_t delta; delta.init (all_points[p1].x - comp_points[p2].x, - all_points[p1].y - comp_points[p2].y); + all_points[p1].y - comp_points[p2].y); comp_points.translate (delta); } @@ -799,7 +806,7 @@ struct glyf { contour_point_vector_t all_points; if (unlikely (!get_points_var (glyph, coords, coord_count, all_points) || - all_points.length < PHANTOM_COUNT)) return false; + all_points.length < PHANTOM_COUNT)) return false; /* Undocumented rasterizer behavior: * Shift points horizontally by the updated left side bearing @@ -810,7 +817,7 @@ struct glyf if (extents != nullptr) { - contour_bounds_t bounds; + contour_bounds_t bounds; for (unsigned int i = 0; i + PHANTOM_COUNT < all_points.length; i++) bounds.add (all_points[i]); @@ -821,8 +828,8 @@ struct glyf } else { - extents->x_bearing = (int32_t)floorf (bounds.min.x); - extents->width = (int32_t)ceilf (bounds.max.x) - extents->x_bearing; + extents->x_bearing = (int) floor (bounds.min.x); + extents->width = (int) ceil (bounds.max.x) - extents->x_bearing; } if (bounds.min.y > bounds.max.y) { @@ -831,13 +838,13 @@ struct glyf } else { - extents->y_bearing = (int32_t)ceilf (bounds.max.y); - extents->height = (int32_t)floorf (bounds.min.y) - extents->y_bearing; + extents->y_bearing = (int) ceil (bounds.max.y); + extents->height = (int) floor (bounds.min.y) - extents->y_bearing; } } if (phantoms != nullptr) { - for (unsigned int i = 0; i < PHANTOM_COUNT; i++) + for (unsigned int i = 0; i < PHANTOM_COUNT; i++) (*phantoms)[i] = all_points[all_points.length - PHANTOM_COUNT + i]; } return true; @@ -962,7 +969,7 @@ struct glyf if (!glyph_header.has_data ()) { *length = 0; - // only 0 byte glyphs are healthy when missing GlyphHeader + /* only 0 byte glyphs are healthy when missing GlyphHeader */ return glyph.length == 0; } if (glyph_header.is_composite_glyph ()) @@ -1017,14 +1024,14 @@ struct glyf bool vertical) const { bool success = false; - contour_point_vector_t phantoms; + contour_point_vector_t phantoms; phantoms.resize (PHANTOM_COUNT); if (likely (coord_count == gvar_accel.get_axis_count ())) success = get_var_metrics (glyph, coords, coord_count, phantoms); if (unlikely (!success)) - return vertical? vmtx_accel.get_advance (glyph): hmtx_accel.get_advance (glyph); + return vertical ? vmtx_accel.get_advance (glyph) : hmtx_accel.get_advance (glyph); if (vertical) return roundf (phantoms[PHANTOM_TOP].y - phantoms[PHANTOM_BOTTOM].y); @@ -1039,9 +1046,9 @@ struct glyf phantoms.resize (PHANTOM_COUNT); if (unlikely (!get_var_extents_and_phantoms (glyph, coords, coord_count, &extents, &phantoms))) - return vertical? vmtx_accel.get_side_bearing (glyph): hmtx_accel.get_side_bearing (glyph); + return vertical ? vmtx_accel.get_side_bearing (glyph) : hmtx_accel.get_side_bearing (glyph); - return vertical? (int)ceilf (phantoms[PHANTOM_TOP].y) - extents.y_bearing: (int)floorf (phantoms[PHANTOM_LEFT].x); + return vertical ? (int) ceil (phantoms[PHANTOM_TOP].y) - extents.y_bearing : (int)floorf (phantoms[PHANTOM_LEFT].x); } bool get_extents (hb_font_t *font, hb_codepoint_t glyph, hb_glyph_extents_t *extents) const @@ -1070,23 +1077,23 @@ struct glyf return true; } - hb_bytes_t bytes_for_glyph (const char * glyf, hb_codepoint_t gid) - { - unsigned int start_offset, end_offset; - if (unlikely (!(get_offsets (gid, &start_offset, &end_offset) && - remove_padding (start_offset, &end_offset)))) + hb_bytes_t bytes_for_glyph (const char * glyf, hb_codepoint_t gid) { - DEBUG_MSG(SUBSET, nullptr, "Unable to get offset or remove padding for %d", gid); - return hb_bytes_t (); + unsigned int start_offset, end_offset; + if (unlikely (!(get_offsets (gid, &start_offset, &end_offset) && + remove_padding (start_offset, &end_offset)))) + { + DEBUG_MSG(SUBSET, nullptr, "Unable to get offset or remove padding for %d", gid); + return hb_bytes_t (); + } + hb_bytes_t glyph_bytes = hb_bytes_t (glyf + start_offset, end_offset - start_offset); + if (!glyph_bytes.as ()->has_data ()) + { + DEBUG_MSG(SUBSET, nullptr, "Glyph size smaller than minimum header %d", gid); + return hb_bytes_t (); + } + return glyph_bytes; } - hb_bytes_t glyph_bytes = hb_bytes_t (glyf + start_offset, end_offset - start_offset); - if (!glyph_bytes.as ()->has_data ()) - { - DEBUG_MSG(SUBSET, nullptr, "Glyph size smaller than minimum header %d", gid); - return hb_bytes_t (); - } - return glyph_bytes; - } private: bool short_offset; @@ -1095,9 +1102,9 @@ struct glyf hb_blob_ptr_t glyf_table; /* variable font support */ - gvar::accelerator_t gvar_accel; - hmtx::accelerator_t hmtx_accel; - vmtx::accelerator_t vmtx_accel; + gvar::accelerator_t gvar_accel; + hmtx::accelerator_t hmtx_accel; + vmtx::accelerator_t vmtx_accel; }; @@ -1110,36 +1117,36 @@ struct glyf hb_bytes_t dest_end; /* region of source_glyph to copy second */ - bool serialize (hb_serialize_context_t *c, - const hb_subset_plan_t *plan) const - { - TRACE_SERIALIZE (this); - - hb_bytes_t dest_glyph = dest_start.copy (c); - dest_glyph = hb_bytes_t (&dest_glyph, dest_glyph.length + dest_end.copy(c).length); - unsigned int pad_length = padding (); - DEBUG_MSG(SUBSET, nullptr, "serialize %d byte glyph, width %d pad %d", dest_glyph.length, dest_glyph.length + pad_length, pad_length); - - HBUINT8 pad; - pad = 0; - while (pad_length > 0) + bool serialize (hb_serialize_context_t *c, + const hb_subset_plan_t *plan) const { - c->embed(pad); - pad_length--; - } + TRACE_SERIALIZE (this); - if (dest_glyph.length) - { - _fix_component_gids (plan, dest_glyph); - if (plan->drop_hints) + hb_bytes_t dest_glyph = dest_start.copy (c); + dest_glyph = hb_bytes_t (&dest_glyph, dest_glyph.length + dest_end.copy(c).length); + unsigned int pad_length = padding (); + DEBUG_MSG(SUBSET, nullptr, "serialize %d byte glyph, width %d pad %d", dest_glyph.length, dest_glyph.length + pad_length, pad_length); + + HBUINT8 pad; + pad = 0; + while (pad_length > 0) { - _zero_instruction_length (dest_glyph); - c->check_success (_remove_composite_instruction_flag (dest_glyph)); + c->embed(pad); + pad_length--; } - } - return_trace (true); - } + if (dest_glyph.length) + { + _fix_component_gids (plan, dest_glyph); + if (plan->drop_hints) + { + _zero_instruction_length (dest_glyph); + c->check_success (_remove_composite_instruction_flag (dest_glyph)); + } + } + + return_trace (true); + } void drop_hints (const OT::glyf::accelerator_t& glyf) { diff --git a/src/hb-ot-var-gvar-table.hh b/src/hb-ot-var-gvar-table.hh index 630249b71..ad942e390 100644 --- a/src/hb-ot-var-gvar-table.hh +++ b/src/hb-ot-var-gvar-table.hh @@ -154,37 +154,37 @@ struct TupleVarHeader if (has_intermediate ()) { - int start = start_tuple[i]; - int end = end_tuple[i]; + int start = start_tuple[i]; + int end = end_tuple[i]; if (unlikely (start > peak || peak > end || (start < 0 && end > 0 && peak))) continue; if (v < start || v > end) return 0.f; if (v < peak) - { if (peak != start) scalar *= (float)(v - start) / (peak - start); } + { if (peak != start) scalar *= (float) (v - start) / (peak - start); } else - { if (peak != end) scalar *= (float)(end - v) / (end - peak); } + { if (peak != end) scalar *= (float) (end - v) / (end - peak); } } else if (!v || v < hb_min (0, peak) || v > hb_max (0, peak)) return 0.f; else - scalar *= (float)v / peak; + scalar *= (float) v / peak; } return scalar; } unsigned int get_data_size () const { return varDataSize; } - bool has_peak () const { return (tupleIndex & TuppleIndex::EmbeddedPeakTuple); } - bool has_intermediate () const { return (tupleIndex & TuppleIndex::IntermediateRegion); } + bool has_peak () const { return (tupleIndex & TuppleIndex::EmbeddedPeakTuple); } + bool has_intermediate () const { return (tupleIndex & TuppleIndex::IntermediateRegion); } bool has_private_points () const { return (tupleIndex & TuppleIndex::PrivatePointNumbers); } - unsigned int get_index () const { return (tupleIndex & TuppleIndex::TupleIndexMask); } + unsigned int get_index () const { return (tupleIndex & TuppleIndex::TupleIndexMask); } protected: const Tuple &get_peak_tuple () const { return StructAfter (tupleIndex); } const Tuple &get_start_tuple (unsigned int axis_count) const - { return *(const Tuple *)&get_peak_tuple ()[has_peak ()? axis_count: 0]; } + { return *(const Tuple *) &get_peak_tuple ()[has_peak () ? axis_count : 0]; } const Tuple &get_end_tuple (unsigned int axis_count) const - { return *(const Tuple *)&get_peak_tuple ()[has_peak ()? (axis_count * 2): axis_count]; } + { return *(const Tuple *) &get_peak_tuple ()[has_peak () ? (axis_count * 2) : axis_count]; } HBUINT16 varDataSize; TuppleIndex tupleIndex; @@ -202,7 +202,8 @@ struct TupleVarCount : HBUINT16 unsigned int get_count () const { return (*this) & CountMask; } protected: - enum Flags { + enum Flags + { SharedPointNumbers = 0x8000u, CountMask = 0x0FFFu }; @@ -257,7 +258,7 @@ struct GlyphVarData } bool in_range (const void *p, unsigned int l) const - { return (const char*)p >= (const char*)var_data && (const char*)p+l <= (const char*)var_data + length; } + { return (const char*) p >= (const char*) var_data && (const char*) p+l <= (const char*) var_data + length; } template bool in_range (const T *p) const { return in_range (p, sizeof (*p)); } @@ -295,11 +296,12 @@ struct GlyphVarData { enum packed_point_flag_t { - POINTS_ARE_WORDS = 0x80, + POINTS_ARE_WORDS = 0x80, POINT_RUN_COUNT_MASK = 0x7F }; - if (!check.in_range (p)) return false; + if (unlikely (!check.in_range (p))) return false; + uint16_t count = *p++; if (count & POINTS_ARE_WORDS) { @@ -370,7 +372,7 @@ struct GlyphVarData { if (!check.in_range ((const HBUINT16 *)p)) return false; - deltas[i] = *(const HBINT16 *)p; + deltas[i] = *(const HBINT16 *) p; p += HBUINT16::static_size; } } @@ -393,7 +395,6 @@ struct GlyphVarData TupleVarCount tupleVarCount; OffsetTo data; /* TupleVarHeader tupleVarHeaders[] */ - public: DEFINE_SIZE_MIN (4); }; @@ -471,18 +472,18 @@ struct gvar unsigned int length = c->plan->old_gid_for_new_gid (gid, &old_gid)? get_glyph_var_data_length (old_gid): 0; if (long_offset) - ((HBUINT32 *)subset_offsets)[gid] = glyph_offset; + ((HBUINT32 *) subset_offsets)[gid] = glyph_offset; else - ((HBUINT16 *)subset_offsets)[gid] = glyph_offset / 2; + ((HBUINT16 *) subset_offsets)[gid] = glyph_offset / 2; if (length > 0) memcpy (subset_data, get_glyph_var_data (old_gid), length); subset_data += length; glyph_offset += length; } if (long_offset) - ((HBUINT32 *)subset_offsets)[num_glyphs] = glyph_offset; + ((HBUINT32 *) subset_offsets)[num_glyphs] = glyph_offset; else - ((HBUINT16 *)subset_offsets)[num_glyphs] = glyph_offset / 2; + ((HBUINT16 *) subset_offsets)[num_glyphs] = glyph_offset / 2; return_trace (true); } @@ -497,10 +498,10 @@ struct gvar unlikely ((start_offset > get_offset (glyphCount)) || (start_offset + GlyphVarData::min_size > end_offset))) return &Null(GlyphVarData); - return &(((unsigned char *)this+start_offset)+dataZ); + return &(((unsigned char *) this + start_offset) + dataZ); } - bool is_long_offset () const { return (flags & 1)!=0; } + bool is_long_offset () const { return (flags & 1) != 0; } unsigned int get_offset (unsigned int i) const { @@ -519,7 +520,7 @@ struct gvar return end_offset - start_offset; } - const HBUINT32 *get_long_offset_array () const { return (const HBUINT32 *)&offsetZ; } + const HBUINT32 * get_long_offset_array () const { return (const HBUINT32 *)&offsetZ; } const HBUINT16 *get_short_offset_array () const { return (const HBUINT16 *)&offsetZ; } public: @@ -541,7 +542,7 @@ struct gvar unsigned int num_shared_coord = gvar_table->sharedTupleCount * gvar_table->axisCount; shared_tuples.resize (num_shared_coord); for (unsigned int i = 0; i < num_shared_coord; i++) - shared_tuples[i] = (&(gvar_table+gvar_table->sharedTuples))[i]; + shared_tuples[i] = (&(gvar_table + gvar_table->sharedTuples))[i]; } void fini () @@ -605,10 +606,11 @@ struct gvar for (unsigned int i = 0; i < orig_points.length; i++) orig_points[i] = points[i]; - contour_point_vector_t deltas; /* flag is used to indicate referenced point */ + contour_point_vector_t deltas; /* flag is used to indicate referenced point */ deltas.resize (points.length); - do { + do + { float scalar = iterator.current_tuple->calculate_scalar (coords, coord_count, shared_tuples.as_array ()); if (scalar == 0.f) continue; const HBUINT8 *p = iterator.get_serialized_data (); From b3afa8164460d63031a80d399200bcaa769c0fa1 Mon Sep 17 00:00:00 2001 From: Ebrahim Byagowi Date: Fri, 4 Oct 2019 00:32:04 +0330 Subject: [PATCH 5/8] [glyf] Use common convention of other tables access, format --- src/hb-ot-face-table-list.hh | 1 + src/hb-ot-glyf-table.hh | 65 ++++++++++++++++-------------------- 2 files changed, 29 insertions(+), 37 deletions(-) diff --git a/src/hb-ot-face-table-list.hh b/src/hb-ot-face-table-list.hh index 30bab5cb5..6fa9baf13 100644 --- a/src/hb-ot-face-table-list.hh +++ b/src/hb-ot-face-table-list.hh @@ -84,6 +84,7 @@ HB_OT_TABLE (OT, VORG) #ifndef HB_NO_VAR HB_OT_TABLE (OT, fvar) HB_OT_TABLE (OT, avar) +HB_OT_ACCELERATOR (OT, gvar) HB_OT_TABLE (OT, MVAR) #endif diff --git a/src/hb-ot-glyf-table.hh b/src/hb-ot-glyf-table.hh index a3d1f8c34..0855d8647 100644 --- a/src/hb-ot-glyf-table.hh +++ b/src/hb-ot-glyf-table.hh @@ -487,10 +487,13 @@ struct glyf struct accelerator_t { - void init (hb_face_t *face) + void init (hb_face_t *face_) { - memset (this, 0, sizeof (accelerator_t)); - + short_offset = false; + num_glyphs = 0; + loca_table = nullptr; + glyf_table = nullptr; + face = face_; const OT::head &head = *face->table.head; if (head.indexToLocFormat > 1 || head.glyphDataFormat != 0) /* Unknown format. Leave num_glyphs=0, that takes care of disabling us. */ @@ -501,19 +504,12 @@ struct glyf glyf_table = hb_sanitize_context_t ().reference_table (face); num_glyphs = hb_max (1u, loca_table.get_length () / (short_offset ? 2 : 4)) - 1; - - gvar_accel.init (face); - hmtx_accel.init (face); - vmtx_accel.init (face); } void fini () { loca_table.destroy (); glyf_table.destroy (); - gvar_accel.fini (); - hmtx_accel.fini (); - vmtx_accel.fini (); } /* @@ -616,10 +612,10 @@ struct glyf void init_phantom_points (hb_codepoint_t glyph, hb_array_t &phantoms /* IN/OUT */) const { const GlyphHeader &header = get_header (glyph); - int h_delta = (int) header.xMin - hmtx_accel.get_side_bearing (glyph); - int v_orig = (int) header.yMax + vmtx_accel.get_side_bearing (glyph); - unsigned int h_adv = hmtx_accel.get_advance (glyph); - unsigned int v_adv = vmtx_accel.get_advance (glyph); + int h_delta = (int) header.xMin - face->table.hmtx->get_side_bearing (glyph); + int v_orig = (int) header.yMax + face->table.vmtx->get_side_bearing (glyph); + unsigned int h_adv = face->table.hmtx->get_advance (glyph); + unsigned int v_adv = face->table.vmtx->get_advance (glyph); phantoms[PHANTOM_LEFT].x = h_delta; phantoms[PHANTOM_RIGHT].x = h_adv + h_delta; @@ -742,9 +738,9 @@ struct glyf contour_point_vector_t points; hb_vector_t end_points; if (unlikely (!get_contour_points (glyph, points, end_points))) return false; - hb_array_t phantoms = points.sub_array (points.length-PHANTOM_COUNT, PHANTOM_COUNT); + hb_array_t phantoms = points.sub_array (points.length - PHANTOM_COUNT, PHANTOM_COUNT); init_phantom_points (glyph, phantoms); - if (unlikely (!gvar_accel.apply_deltas_to_points (glyph, coords, coord_count, points.as_array (), end_points.as_array ()))) return false; + if (unlikely (!face->table.gvar->apply_deltas_to_points (glyph, coords, coord_count, points.as_array (), end_points.as_array ()))) return false; unsigned int comp_index = 0; CompositeGlyphHeader::Iterator composite; @@ -1003,7 +999,7 @@ struct glyf unsigned int instruction_len_offset = glyph_header.simple_instruction_len_offset (); if (unlikely (instruction_len_offset + 2 > glyph.length)) { - DEBUG_MSG(SUBSET, nullptr, "Glyph size is too short, missing field instructionLength."); + DEBUG_MSG (SUBSET, nullptr, "Glyph size is too short, missing field instructionLength."); return false; } @@ -1011,7 +1007,7 @@ struct glyf /* Out of bounds of the current glyph */ if (unlikely (glyph_header.simple_length (instruction_len) > glyph.length)) { - DEBUG_MSG(SUBSET, nullptr, "The instructions array overruns the glyph's boundaries."); + DEBUG_MSG (SUBSET, nullptr, "The instructions array overruns the glyph's boundaries."); return false; } *length = (uint16_t) instruction_len; @@ -1027,11 +1023,11 @@ struct glyf contour_point_vector_t phantoms; phantoms.resize (PHANTOM_COUNT); - if (likely (coord_count == gvar_accel.get_axis_count ())) + if (likely (coord_count == face->table.gvar->get_axis_count ())) success = get_var_metrics (glyph, coords, coord_count, phantoms); if (unlikely (!success)) - return vertical ? vmtx_accel.get_advance (glyph) : hmtx_accel.get_advance (glyph); + return vertical ? face->table.vmtx->get_advance (glyph) : face->table.hmtx->get_advance (glyph); if (vertical) return roundf (phantoms[PHANTOM_TOP].y - phantoms[PHANTOM_BOTTOM].y); @@ -1046,16 +1042,16 @@ struct glyf phantoms.resize (PHANTOM_COUNT); if (unlikely (!get_var_extents_and_phantoms (glyph, coords, coord_count, &extents, &phantoms))) - return vertical ? vmtx_accel.get_side_bearing (glyph) : hmtx_accel.get_side_bearing (glyph); + return vertical ? face->table.vmtx->get_side_bearing (glyph) : face->table.hmtx->get_side_bearing (glyph); - return vertical ? (int) ceil (phantoms[PHANTOM_TOP].y) - extents.y_bearing : (int)floorf (phantoms[PHANTOM_LEFT].x); + return vertical ? ceil (phantoms[PHANTOM_TOP].y) - extents.y_bearing : floor (phantoms[PHANTOM_LEFT].x); } bool get_extents (hb_font_t *font, hb_codepoint_t glyph, hb_glyph_extents_t *extents) const { unsigned int coord_count; const int *coords = hb_font_get_var_coords_normalized (font, &coord_count); - if (coords && coord_count > 0 && coord_count == gvar_accel.get_axis_count ()) + if (coords && coord_count > 0 && coord_count == face->table.gvar->get_axis_count ()) return get_extents_var (glyph, coords, coord_count, extents); unsigned int start_offset, end_offset; @@ -1069,7 +1065,7 @@ struct glyf /* 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 = hmtx_accel.get_side_bearing (glyph); + extents->x_bearing = face->table.hmtx->get_side_bearing (glyph); extents->y_bearing = hb_max (glyph_header.yMin, glyph_header.yMax); extents->width = hb_max (glyph_header.xMin, glyph_header.xMax) - hb_min (glyph_header.xMin, glyph_header.xMax); extents->height = hb_min (glyph_header.yMin, glyph_header.yMax) - extents->y_bearing; @@ -1077,19 +1073,19 @@ struct glyf return true; } - hb_bytes_t bytes_for_glyph (const char * glyf, hb_codepoint_t gid) + hb_bytes_t bytes_for_glyph (const char *glyf, hb_codepoint_t gid) { unsigned int start_offset, end_offset; if (unlikely (!(get_offsets (gid, &start_offset, &end_offset) && - remove_padding (start_offset, &end_offset)))) + remove_padding (start_offset, &end_offset)))) { - 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 (); } hb_bytes_t glyph_bytes = hb_bytes_t (glyf + start_offset, end_offset - start_offset); if (!glyph_bytes.as ()->has_data ()) { - 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 glyph_bytes; @@ -1100,11 +1096,7 @@ struct glyf unsigned int num_glyphs; hb_blob_ptr_t loca_table; hb_blob_ptr_t glyf_table; - - /* variable font support */ - gvar::accelerator_t gvar_accel; - hmtx::accelerator_t hmtx_accel; - vmtx::accelerator_t vmtx_accel; + hb_face_t *face; }; @@ -1116,22 +1108,21 @@ struct glyf hb_bytes_t dest_start; /* region of source_glyph to copy first */ hb_bytes_t dest_end; /* region of source_glyph to copy second */ - bool serialize (hb_serialize_context_t *c, const hb_subset_plan_t *plan) const { TRACE_SERIALIZE (this); hb_bytes_t dest_glyph = dest_start.copy (c); - dest_glyph = hb_bytes_t (&dest_glyph, dest_glyph.length + dest_end.copy(c).length); + dest_glyph = hb_bytes_t (&dest_glyph, dest_glyph.length + dest_end.copy (c).length); unsigned int pad_length = padding (); - DEBUG_MSG(SUBSET, nullptr, "serialize %d byte glyph, width %d pad %d", dest_glyph.length, dest_glyph.length + pad_length, pad_length); + DEBUG_MSG (SUBSET, nullptr, "serialize %d byte glyph, width %d pad %d", dest_glyph.length, dest_glyph.length + pad_length, pad_length); HBUINT8 pad; pad = 0; while (pad_length > 0) { - c->embed(pad); + c->embed (pad); pad_length--; } From 8e33510343d151afe49730f3a0b263a2853822b8 Mon Sep 17 00:00:00 2001 From: Ebrahim Byagowi Date: Fri, 4 Oct 2019 01:11:03 +0330 Subject: [PATCH 6/8] [gvar] minor --- src/hb-ot-var-gvar-table.hh | 132 ++++++++++++++++++------------------ 1 file changed, 66 insertions(+), 66 deletions(-) diff --git a/src/hb-ot-var-gvar-table.hh b/src/hb-ot-var-gvar-table.hh index ad942e390..87d5fc7f1 100644 --- a/src/hb-ot-var-gvar-table.hh +++ b/src/hb-ot-var-gvar-table.hh @@ -1,5 +1,6 @@ /* - * Copyright © 2019 Adobe Inc. + * Copyright © 2019 Adobe Inc. + * Copyright © 2019 Ebrahim Byagowi * * This is part of HarfBuzz, a text shaping library. * @@ -45,8 +46,8 @@ struct contour_point_t void translate (const contour_point_t &p) { x += p.x; y += p.y; } - uint8_t flag; - float x, y; + uint8_t flag; + float x, y; }; struct contour_point_vector_t : hb_vector_t @@ -80,7 +81,7 @@ struct contour_point_vector_t : hb_vector_t struct range_checker_t { range_checker_t (const void *table_, unsigned int start_offset_, unsigned int end_offset_) - : table ((const char*)table_), start_offset (start_offset_), end_offset (end_offset_) {} + : table ((const char*) table_), start_offset (start_offset_), end_offset (end_offset_) {} template bool in_range (const T *p) const @@ -100,10 +101,10 @@ struct Tuple : UnsizedArrayOf {}; struct TuppleIndex : HBUINT16 { enum Flags { - EmbeddedPeakTuple = 0x8000u, - IntermediateRegion = 0x4000u, - PrivatePointNumbers = 0x2000u, - TupleIndexMask = 0x0FFFu + EmbeddedPeakTuple = 0x8000u, + IntermediateRegion = 0x4000u, + PrivatePointNumbers = 0x2000u, + TupleIndexMask = 0x0FFFu }; DEFINE_SIZE_STATIC (2); @@ -114,9 +115,9 @@ struct TupleVarHeader unsigned int get_size (unsigned int axis_count) const { return min_size + - (has_peak ()? get_peak_tuple ().get_size (axis_count): 0) + - (has_intermediate ()? (get_start_tuple (axis_count).get_size (axis_count) + - get_end_tuple (axis_count).get_size (axis_count)): 0); + (has_peak () ? get_peak_tuple ().get_size (axis_count) : 0) + + (has_intermediate () ? (get_start_tuple (axis_count).get_size (axis_count) + + get_end_tuple (axis_count).get_size (axis_count)) : 0); } const TupleVarHeader &get_next (unsigned int axis_count) const @@ -133,7 +134,7 @@ struct TupleVarHeader { unsigned int index = get_index (); if (unlikely (index * coord_count >= shared_tuples.length)) - return 0.f; + return 0.f; peak_tuple = &shared_tuples[coord_count * index]; } @@ -215,7 +216,7 @@ struct TupleVarCount : HBUINT16 struct GlyphVarData { const TupleVarHeader &get_tuple_var_header (void) const - { return StructAfter(data); } + { return StructAfter (data); } struct tuple_iterator_t { @@ -266,14 +267,14 @@ struct GlyphVarData { return &(var_data+var_data->data) + data_offset; } private: - const GlyphVarData *var_data; - unsigned int length; - unsigned int index; - unsigned int axis_count; - unsigned int data_offset; + const GlyphVarData *var_data; + unsigned int length; + unsigned int index; + unsigned int axis_count; + unsigned int data_offset; public: - const TupleVarHeader *current_tuple; + const TupleVarHeader *current_tuple; }; static bool get_tuple_iterator (const GlyphVarData *var_data, @@ -348,8 +349,8 @@ struct GlyphVarData { enum packed_delta_flag_t { - DELTAS_ARE_ZERO = 0x80, - DELTAS_ARE_WORDS = 0x40, + DELTAS_ARE_ZERO = 0x80, + DELTAS_ARE_WORDS = 0x40, DELTA_RUN_COUNT_MASK = 0x3F }; @@ -358,16 +359,13 @@ struct GlyphVarData while (i < count) { if (!check.in_range (p)) return false; - uint16_t j; uint8_t control = *p++; - uint16_t run_count = (control & DELTA_RUN_COUNT_MASK) + 1; + unsigned int run_count = (control & DELTA_RUN_COUNT_MASK) + 1; + unsigned int j; if (control & DELTAS_ARE_ZERO) - { for (j = 0; j < run_count && i < count; j++, i++) deltas[i] = 0; - } else if (control & DELTAS_ARE_WORDS) - { for (j = 0; j < run_count && i < count; j++, i++) { if (!check.in_range ((const HBUINT16 *)p)) @@ -375,16 +373,13 @@ struct GlyphVarData deltas[i] = *(const HBINT16 *) p; p += HBUINT16::static_size; } - } else - { for (j = 0; j < run_count && i < count; j++, i++) { if (!check.in_range (p)) return false; - deltas[i] = *(const HBINT8 *)p++; + deltas[i] = *(const HBINT8 *) p++; } - } if (j < run_count) return false; } @@ -407,13 +402,13 @@ struct gvar { TRACE_SANITIZE (this); return_trace (c->check_struct (this) && (version.major == 1) && - (glyphCount == c->get_num_glyphs ()) && - c->check_array (&(this+sharedTuples), axisCount * sharedTupleCount) && - (is_long_offset ()? - c->check_array (get_long_offset_array (), glyphCount+1): - c->check_array (get_short_offset_array (), glyphCount+1)) && - c->check_array (((const HBUINT8*)&(this+dataZ)) + get_offset (0), - get_offset (glyphCount) - get_offset (0))); + (glyphCount == c->get_num_glyphs ()) && + c->check_array (&(this+sharedTuples), axisCount * sharedTupleCount) && + (is_long_offset ()? + c->check_array (get_long_offset_array (), glyphCount+1): + c->check_array (get_short_offset_array (), glyphCount+1)) && + c->check_array (((const HBUINT8*)&(this+dataZ)) + get_offset (0), + get_offset (glyphCount) - get_offset (0))); } /* GlyphVarData not sanitized here; must be checked while accessing each glyph varation data */ @@ -457,11 +452,11 @@ struct gvar unsigned int shared_tuple_size = F2DOT14::static_size * axisCount * sharedTupleCount; F2DOT14 *tuples = c->serializer->allocate_size (shared_tuple_size); if (!tuples) return_trace (false); - out->sharedTuples = (char *)tuples - (char *)out; + out->sharedTuples = (char *) tuples - (char *) out; memcpy (tuples, &(this+sharedTuples), shared_tuple_size); } - char *subset_data = c->serializer->allocate_size(subset_data_size); + char *subset_data = c->serializer->allocate_size (subset_data_size); if (!subset_data) return_trace (false); out->dataZ = subset_data - (char *)out; @@ -469,7 +464,7 @@ struct gvar for (hb_codepoint_t gid = 0; gid < num_glyphs; gid++) { hb_codepoint_t old_gid; - unsigned int length = c->plan->old_gid_for_new_gid (gid, &old_gid)? get_glyph_var_data_length (old_gid): 0; + unsigned int length = c->plan->old_gid_for_new_gid (gid, &old_gid) ? get_glyph_var_data_length (old_gid) : 0; if (long_offset) ((HBUINT32 *) subset_offsets)[gid] = glyph_offset; @@ -497,7 +492,7 @@ struct gvar if ((start_offset == end_offset) || unlikely ((start_offset > get_offset (glyphCount)) || (start_offset + GlyphVarData::min_size > end_offset))) - return &Null(GlyphVarData); + return &Null (GlyphVarData); return &(((unsigned char *) this + start_offset) + dataZ); } @@ -513,15 +508,15 @@ struct gvar unsigned int get_glyph_var_data_length (unsigned int glyph) const { - unsigned int end_offset = get_offset (glyph+1); + unsigned int end_offset = get_offset (glyph + 1); unsigned int start_offset = get_offset (glyph); - if (unlikely (start_offset > end_offset || end_offset > get_offset(glyphCount))) + if (unlikely (start_offset > end_offset || end_offset > get_offset (glyphCount))) return 0; return end_offset - start_offset; } - const HBUINT32 * get_long_offset_array () const { return (const HBUINT32 *)&offsetZ; } - const HBUINT16 *get_short_offset_array () const { return (const HBUINT16 *)&offsetZ; } + const HBUINT32 * get_long_offset_array () const { return (const HBUINT32 *) &offsetZ; } + const HBUINT16 *get_short_offset_array () const { return (const HBUINT16 *) &offsetZ; } public: struct accelerator_t @@ -567,11 +562,11 @@ struct gvar float next_delta = T::get (deltas[next]); if (prev_val == next_val) - return (prev_delta == next_delta)? prev_delta: 0.f; + return (prev_delta == next_delta) ? prev_delta : 0.f; else if (target_val <= hb_min (prev_val, next_val)) - return (prev_val < next_val) ? prev_delta: next_delta; + return (prev_val < next_val) ? prev_delta : next_delta; else if (target_val >= hb_max (prev_val, next_val)) - return (prev_val > next_val)? prev_delta: next_delta; + return (prev_val > next_val) ? prev_delta : next_delta; /* linear interpolation */ float r = (target_val - prev_val) / (next_val - prev_val); @@ -579,7 +574,7 @@ struct gvar } static unsigned int next_index (unsigned int i, unsigned int start, unsigned int end) - { return (i >= end)? start: (i + 1); } + { return (i >= end) ? start : (i + 1); } public: bool apply_deltas_to_points (hb_codepoint_t glyph, @@ -619,19 +614,19 @@ struct gvar return false; range_checker_t checker (p, 0, length); - hb_vector_t private_indices; + hb_vector_t private_indices; if (iterator.current_tuple->has_private_points () && !GlyphVarData::unpack_points (p, private_indices, checker)) return false; - const hb_array_t &indices = private_indices.length? private_indices: shared_indices; + const hb_array_t &indices = private_indices.length ? private_indices : shared_indices; bool apply_to_all = (indices.length == 0); - unsigned int num_deltas = apply_to_all? points.length: indices.length; - hb_vector_t x_deltas; + unsigned int num_deltas = apply_to_all ? points.length : indices.length; + hb_vector_t x_deltas; x_deltas.resize (num_deltas); if (!GlyphVarData::unpack_deltas (p, x_deltas, checker)) return false; - hb_vector_t y_deltas; + hb_vector_t y_deltas; y_deltas.resize (num_deltas); if (!GlyphVarData::unpack_deltas (p, y_deltas, checker)) return false; @@ -667,13 +662,15 @@ struct gvar * Note that a gap may wrap around at left (start_point) and/or at right (end_point). */ unsigned int prev, next; - for (;;) { + for (;;) + { i = j; j = next_index (i, start_point, end_point); if (deltas[i].flag && !deltas[j].flag) break; } prev = j = i; - for (;;) { + for (;;) + { i = j; j = next_index (i, start_point, end_point); if (!deltas[i].flag && deltas[j].flag) break; @@ -681,7 +678,8 @@ struct gvar next = j; /* Infer deltas for all unref points in the gap between prev and next */ i = prev; - for (;;) { + for (;;) + { i = next_index (i, start_point, end_point); if (i == next) break; deltas[i].x = infer_delta (orig_points.as_array (), deltas.as_array (), i, prev, next); @@ -716,15 +714,17 @@ no_more_gaps: }; protected: - FixedVersion<> version; /* Version of gvar table. Set to 0x00010000u. */ - HBUINT16 axisCount; - HBUINT16 sharedTupleCount; - LOffsetTo sharedTuples; /* LOffsetTo> */ - HBUINT16 glyphCount; - HBUINT16 flags; - LOffsetTo dataZ; /* Array of GlyphVarData */ - UnsizedArrayOf offsetZ; /* Array of 16-bit or 32-bit (glyphCount+1) offsets */ - + FixedVersion<>version; /* Version of gvar table. Set to 0x00010000u. */ + HBUINT16 axisCount; + HBUINT16 sharedTupleCount; + LOffsetTo + sharedTuples; /* LOffsetTo> */ + HBUINT16 glyphCount; + HBUINT16 flags; + LOffsetTo + dataZ; /* Array of GlyphVarData */ + UnsizedArrayOf + offsetZ; /* Array of 16-bit or 32-bit (glyphCount+1) offsets */ public: DEFINE_SIZE_MIN (20); }; From 5752863640cf4292dec313edebb57aecd47496f3 Mon Sep 17 00:00:00 2001 From: Ebrahim Byagowi Date: Sat, 5 Oct 2019 23:51:50 +0330 Subject: [PATCH 7/8] [glyf][gvar] Make HB_NO_VAR buildable --- src/hb-ot-font.cc | 2 ++ src/hb-ot-glyf-table.hh | 6 ++++++ src/hb-ot-hmtx-table.hh | 8 ++++++++ 3 files changed, 16 insertions(+) diff --git a/src/hb-ot-font.cc b/src/hb-ot-font.cc index 283cfb689..303cd4c68 100644 --- a/src/hb-ot-font.cc +++ b/src/hb-ot-font.cc @@ -315,6 +315,7 @@ hb_ot_font_set_funcs (hb_font_t *font) nullptr); } +#ifndef HB_NO_VAR int hb_ot_get_side_bearing_var_tt (hb_font_t *font, hb_codepoint_t glyph, bool is_vertical) { @@ -326,6 +327,7 @@ hb_ot_get_advance_var_tt (hb_font_t *font, hb_codepoint_t glyph, bool is_vertica { return font->face->table.glyf->get_advance_var (glyph, font->coords, font->num_coords, is_vertical); } +#endif #endif diff --git a/src/hb-ot-glyf-table.hh b/src/hb-ot-glyf-table.hh index 0855d8647..a7b934da2 100644 --- a/src/hb-ot-glyf-table.hh +++ b/src/hb-ot-glyf-table.hh @@ -726,6 +726,7 @@ struct glyf contour_point_t max; }; +#ifndef HB_NO_VAR /* Note: Recursively calls itself. * all_points includes phantom points */ @@ -855,6 +856,7 @@ struct glyf const int *coords, unsigned int coord_count, hb_glyph_extents_t *extents) const { return get_var_extents_and_phantoms (glyph, coords, coord_count, extents); } +#endif public: /* based on FontTools _g_l_y_f.py::trim */ @@ -1015,6 +1017,7 @@ struct glyf return true; } +#ifndef HB_NO_VAR unsigned int get_advance_var (hb_codepoint_t glyph, const int *coords, unsigned int coord_count, bool vertical) const @@ -1046,13 +1049,16 @@ struct glyf return vertical ? ceil (phantoms[PHANTOM_TOP].y) - extents.y_bearing : floor (phantoms[PHANTOM_LEFT].x); } +#endif bool get_extents (hb_font_t *font, hb_codepoint_t glyph, hb_glyph_extents_t *extents) const { +#ifndef HB_NO_VAR unsigned int coord_count; const int *coords = hb_font_get_var_coords_normalized (font, &coord_count); if (coords && coord_count > 0 && coord_count == face->table.gvar->get_axis_count ()) return get_extents_var (glyph, coords, coord_count, extents); +#endif unsigned int start_offset, end_offset; if (!get_offsets (glyph, &start_offset, &end_offset)) diff --git a/src/hb-ot-hmtx-table.hh b/src/hb-ot-hmtx-table.hh index 8a7395982..9d179330c 100644 --- a/src/hb-ot-hmtx-table.hh +++ b/src/hb-ot-hmtx-table.hh @@ -211,6 +211,7 @@ struct hmtxvmtx { int side_bearing = get_side_bearing (glyph); +#ifndef HB_NO_VAR if (unlikely (glyph >= num_metrics) || !font->num_coords) return side_bearing; @@ -218,6 +219,9 @@ struct hmtxvmtx return hb_ot_get_side_bearing_var_tt (font, glyph, T::tableTag == HB_OT_TAG_vmtx); return side_bearing + var_table->get_side_bearing_var (glyph, font->coords, font->num_coords); // TODO Optimize?! +#else + return side_bearing; +#endif } unsigned int get_advance (hb_codepoint_t glyph) const @@ -241,6 +245,7 @@ struct hmtxvmtx { unsigned int advance = get_advance (glyph); +#ifndef HB_NO_VAR if (unlikely (glyph >= num_metrics) || !font->num_coords) return advance; @@ -248,6 +253,9 @@ struct hmtxvmtx return hb_ot_get_advance_var_tt (font, glyph, T::tableTag == HB_OT_TAG_vmtx); return advance + roundf (var_table->get_advance_var (glyph, font->coords, font->num_coords)); // TODO Optimize?! +#else + return advance; +#endif } unsigned int num_advances_for_subset (const hb_subset_plan_t *plan) const From 21c80d955f69c1242c77e0ec7ccd852360dd54dd Mon Sep 17 00:00:00 2001 From: Ebrahim Byagowi Date: Sat, 5 Oct 2019 23:58:52 +0330 Subject: [PATCH 8/8] [gvar] minor --- src/hb-ot-var-gvar-table.hh | 43 ++++++++++++++++++------------------- 1 file changed, 21 insertions(+), 22 deletions(-) diff --git a/src/hb-ot-var-gvar-table.hh b/src/hb-ot-var-gvar-table.hh index 87d5fc7f1..510174c35 100644 --- a/src/hb-ot-var-gvar-table.hh +++ b/src/hb-ot-var-gvar-table.hh @@ -306,7 +306,7 @@ struct GlyphVarData uint16_t count = *p++; if (count & POINTS_ARE_WORDS) { - if (!check.in_range (p)) return false; + if (unlikely (!check.in_range (p))) return false; count = ((count & POINT_RUN_COUNT_MASK) << 8) | *p++; } points.resize (count); @@ -315,7 +315,7 @@ struct GlyphVarData uint16_t i = 0; while (i < count) { - if (!check.in_range (p)) return false; + if (unlikely (!check.in_range (p))) return false; uint16_t j; uint8_t control = *p++; uint16_t run_count = (control & POINT_RUN_COUNT_MASK) + 1; @@ -323,7 +323,8 @@ struct GlyphVarData { for (j = 0; j < run_count && i < count; j++, i++) { - if (!check.in_range ((const HBUINT16 *)p)) return false; + if (unlikely (!check.in_range ((const HBUINT16 *) p))) + return false; n += *(const HBUINT16 *)p; points[i] = n; p += HBUINT16::static_size; @@ -333,7 +334,7 @@ struct GlyphVarData { for (j = 0; j < run_count && i < count; j++, i++) { - if (!check.in_range (p)) return false; + if (unlikely (!check.in_range (p))) return false; n += *p++; points[i] = n; } @@ -358,7 +359,7 @@ struct GlyphVarData unsigned int count = deltas.length; while (i < count) { - if (!check.in_range (p)) return false; + if (unlikely (!check.in_range (p))) return false; uint8_t control = *p++; unsigned int run_count = (control & DELTA_RUN_COUNT_MASK) + 1; unsigned int j; @@ -368,7 +369,7 @@ struct GlyphVarData else if (control & DELTAS_ARE_WORDS) for (j = 0; j < run_count && i < count; j++, i++) { - if (!check.in_range ((const HBUINT16 *)p)) + if (unlikely (!check.in_range ((const HBUINT16 *) p))) return false; deltas[i] = *(const HBINT16 *) p; p += HBUINT16::static_size; @@ -376,7 +377,7 @@ struct GlyphVarData else for (j = 0; j < run_count && i < count; j++, i++) { - if (!check.in_range (p)) + if (unlikely (!check.in_range (p))) return false; deltas[i] = *(const HBINT8 *) p++; } @@ -404,8 +405,8 @@ struct gvar return_trace (c->check_struct (this) && (version.major == 1) && (glyphCount == c->get_num_glyphs ()) && c->check_array (&(this+sharedTuples), axisCount * sharedTupleCount) && - (is_long_offset ()? - c->check_array (get_long_offset_array (), glyphCount+1): + (is_long_offset () ? + c->check_array (get_long_offset_array (), glyphCount+1) : c->check_array (get_short_offset_array (), glyphCount+1)) && c->check_array (((const HBUINT8*)&(this+dataZ)) + get_offset (0), get_offset (glyphCount) - get_offset (0))); @@ -439,9 +440,9 @@ struct gvar } bool long_offset = subset_data_size & ~0xFFFFu; - out->flags = long_offset? 1: 0; + out->flags = long_offset ? 1 : 0; - HBUINT8 *subset_offsets = c->serializer->allocate_size ((long_offset? 4: 2) * (num_glyphs+1)); + HBUINT8 *subset_offsets = c->serializer->allocate_size ((long_offset ? 4 : 2) * (num_glyphs + 1)); if (!subset_offsets) return_trace (false); /* shared tuples */ @@ -523,8 +524,6 @@ struct gvar { void init (hb_face_t *face) { - memset (this, 0, sizeof (accelerator_t)); - gvar_table = hb_sanitize_context_t ().reference_table (face); hb_blob_ptr_t fvar_table = hb_sanitize_context_t ().reference_table (face); unsigned int axis_count = fvar_table->get_axis_count (); @@ -585,8 +584,8 @@ struct gvar if (unlikely (coord_count != gvar_table->axisCount)) return false; const GlyphVarData *var_data = gvar_table->get_glyph_var_data (glyph); - if (var_data == &Null(GlyphVarData)) return true; - hb_vector_t shared_indices; + if (var_data == &Null (GlyphVarData)) return true; + hb_vector_t shared_indices; GlyphVarData::tuple_iterator_t iterator; if (!GlyphVarData::get_tuple_iterator (var_data, gvar_table->get_glyph_var_data_length (glyph), @@ -614,19 +613,19 @@ struct gvar return false; range_checker_t checker (p, 0, length); - hb_vector_t private_indices; + hb_vector_t private_indices; if (iterator.current_tuple->has_private_points () && !GlyphVarData::unpack_points (p, private_indices, checker)) return false; const hb_array_t &indices = private_indices.length ? private_indices : shared_indices; - bool apply_to_all = (indices.length == 0); + bool apply_to_all = (indices.length == 0); unsigned int num_deltas = apply_to_all ? points.length : indices.length; - hb_vector_t x_deltas; + hb_vector_t x_deltas; x_deltas.resize (num_deltas); if (!GlyphVarData::unpack_deltas (p, x_deltas, checker)) return false; - hb_vector_t y_deltas; + hb_vector_t y_deltas; y_deltas.resize (num_deltas); if (!GlyphVarData::unpack_deltas (p, y_deltas, checker)) return false; @@ -635,7 +634,7 @@ struct gvar deltas[i].init (); for (unsigned int i = 0; i < num_deltas; i++) { - unsigned int pt_index = apply_to_all? i: indices[i]; + unsigned int pt_index = apply_to_all ? i : indices[i]; deltas[pt_index].flag = 1; /* this point is referenced, i.e., explicit deltas specified */ deltas[pt_index].x += x_deltas[i] * scalar; deltas[pt_index].y += y_deltas[i] * scalar; @@ -709,8 +708,8 @@ no_more_gaps: { return gvar_table->get_glyph_var_data (glyph); } private: - hb_blob_ptr_t gvar_table; - hb_vector_t shared_tuples; + hb_blob_ptr_t gvar_table; + hb_vector_t shared_tuples; }; protected: