From 6f91e0d903d7510a4612a9cc7306ec04260cefed Mon Sep 17 00:00:00 2001 From: Michiharu Ariza Date: Tue, 26 Feb 2019 11:11:50 -0800 Subject: [PATCH] Added hb-ot-var-gvar-table.hh implemented sanitize() placeholder subset() some code cleanup --- src/Makefile.sources | 1 + src/hb-bimap.hh | 6 +- src/hb-ot-gvar-table.hh | 211 ------------------------------------ src/hb-ot-var-gvar-table.hh | 207 +++++++++++++++++++++++++++++++++++ src/hb-ot-var-hvar-table.hh | 14 +-- src/hb-subset.cc | 4 + 6 files changed, 222 insertions(+), 221 deletions(-) delete mode 100644 src/hb-ot-gvar-table.hh create mode 100644 src/hb-ot-var-gvar-table.hh diff --git a/src/Makefile.sources b/src/Makefile.sources index db818283f..a8f609de4 100644 --- a/src/Makefile.sources +++ b/src/Makefile.sources @@ -126,6 +126,7 @@ HB_BASE_sources = \ hb-ot-var-fvar-table.hh \ hb-ot-var-hvar-table.hh \ hb-ot-var-mvar-table.hh \ + hb-ot-var-gvar-table.hh \ hb-ot-var.cc \ hb-ot-vorg-table.hh \ hb-set-digest.hh \ diff --git a/src/hb-bimap.hh b/src/hb-bimap.hh index 1c8fa553c..6ed3e1fbd 100644 --- a/src/hb-bimap.hh +++ b/src/hb-bimap.hh @@ -38,14 +38,14 @@ struct hb_bimap_t hb_bimap_t () { init (); } ~hb_bimap_t () { fini (); } - void init (void) + void init () { count = 0; old_to_new.init (); new_to_old.init (); } - void fini (void) + void fini () { old_to_new.fini (); new_to_old.fini (); @@ -90,7 +90,7 @@ struct hb_bimap_t /* Optional: after finished adding all mappings in a random order, * reassign new ids to old ids so that both are in the same order. */ - void reorder (void) + void reorder () { new_to_old.qsort (cmp_id); for (hb_codepoint_t _new = 0; _new < count; _new++) diff --git a/src/hb-ot-gvar-table.hh b/src/hb-ot-gvar-table.hh deleted file mode 100644 index f0077150b..000000000 --- a/src/hb-ot-gvar-table.hh +++ /dev/null @@ -1,211 +0,0 @@ -/* - * Copyright © 2018 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_GVAR_TABLE_HH -#define HB_OT_GVAR_TABLE_HH - -#include "hb-open-type.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 Tupple -{ - bool sanitize (hb_sanitize_context_t *c, unsigned int axis_count) const - { - TRACE_SANITIZE (this); - return_trace (coords.sanitize (c, axis_count, this));s - } - - protected: - UnsizedArrayOf - coords; - - public: - DEFINE_SIZE_ARRAY (0, coords); -}; - -struct TupleVarHeader -{ - HBUINT16 varDataSize; - HBUINT16 tupleIndex; - /* Tuple peakTuple */ - /* Tuple intermediateStartTuple */ - /* Tuple intermediateEndTuple */ -}; - -struct GlyphVarData -{ - HBUINT16 tupleVarCount; - OffsetTo - data; - UnsizedArrayOf - tupleVarHeaders; -}; - -struct gvar -{ - static constexpr hb_tag_t tableTag = HB_OT_TAG_gvar; - - bool has_data () const { return version.to_int (); } - - int get_y_origin (hb_codepoint_t glyph) const - { - unsigned int i; - if (!vertYOrigins.bfind (glyph, &i)) - return defaultVertOriginY; - return vertYOrigins[i].vertOriginY; - } - - bool _subset (const hb_subset_plan_t *plan HB_UNUSED, - const gvar *vorg_table, - const hb_vector_t &subset_metrics, - unsigned int dest_sz, - void *dest) const - { - hb_serialize_context_t c (dest, dest_sz); - - gvar *subset_table = c.start_serialize (); - if (unlikely (!c.extend_min (*subset_table))) - return false; - - subset_table->version.major.set (1); - subset_table->version.minor.set (0); - - subset_table->defaultVertOriginY.set (vorg_table->defaultVertOriginY); - subset_table->vertYOrigins.len.set (subset_metrics.length); - - bool success = true; - if (subset_metrics.length > 0) - { - unsigned int size = VertOriginMetric::static_size * subset_metrics.length; - VertOriginMetric *metrics = c.allocate_size (size); - if (likely (metrics != nullptr)) - memcpy (metrics, &subset_metrics[0], size); - else - success = false; - } - c.end_serialize (); - - return success; - } - - bool subset (hb_subset_plan_t *plan) const - { - hb_blob_t *vorg_blob = hb_sanitize_context_t().reference_table (plan->source); - const gvar *vorg_table = vorg_blob->as (); - - /* count the number of glyphs to be included in the subset table */ - hb_vector_t subset_metrics; - subset_metrics.init (); - - - hb_codepoint_t old_glyph = HB_SET_VALUE_INVALID; - unsigned int i = 0; - while (i < vertYOrigins.len - && plan->glyphset ()->next (&old_glyph)) - { - while (old_glyph > vertYOrigins[i].glyph) - { - i++; - if (i >= vertYOrigins.len) - break; - } - - if (old_glyph == vertYOrigins[i].glyph) - { - hb_codepoint_t new_glyph; - if (plan->new_gid_for_old_gid (old_glyph, &new_glyph)) - { - VertOriginMetric *metrics = subset_metrics.push (); - metrics->glyph.set (new_glyph); - metrics->vertOriginY.set (vertYOrigins[i].vertOriginY); - } - } - } - - /* alloc the new table */ - unsigned int dest_sz = gvar::min_size + VertOriginMetric::static_size * subset_metrics.length; - void *dest = (void *) malloc (dest_sz); - if (unlikely (!dest)) - { - subset_metrics.fini (); - hb_blob_destroy (vorg_blob); - return false; - } - - /* serialize the new table */ - if (!_subset (plan, vorg_table, subset_metrics, dest_sz, dest)) - { - subset_metrics.fini (); - free (dest); - hb_blob_destroy (vorg_blob); - return false; - } - - hb_blob_t *result = hb_blob_create ((const char *)dest, - dest_sz, - HB_MEMORY_MODE_READONLY, - dest, - free); - bool success = plan->add_table (HB_OT_TAG_gvar, result); - hb_blob_destroy (result); - subset_metrics.fini (); - hb_blob_destroy (vorg_blob); - return success; - } - - bool sanitize (hb_sanitize_context_t *c) const - { - TRACE_SANITIZE (this); - return_trace (c->check_struct (this) && - version.major == 1 && - glyphVarDataArray.sanitize (c)); - } - - protected: - FixedVersion<> version; /* Version of gvar table. Set to 0x00010000u. */ - HBUINT16 axisCount; - HBUINT16 sharedTupleCount; - LOffsetTo sharedTuples; - HBUINT16 glyphCount; - HBUINT16 flags; - LOffsetTo - glyphVarDataArray; - /* OffsetTo glyphVariationDataOffsets */ - - public: - DEFINE_SIZE_ARRAY(8, vertYOrigins); -}; - -} /* namespace OT */ - -#endif /* HB_OT_GVAR_TABLE_HH */ diff --git a/src/hb-ot-var-gvar-table.hh b/src/hb-ot-var-gvar-table.hh new file mode 100644 index 000000000..00d9bdbf3 --- /dev/null +++ b/src/hb-ot-var-gvar-table.hh @@ -0,0 +1,207 @@ +/* + * 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" + +/* + * 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 Tuple : UnsizedArrayOf {}; + +struct TuppleIndex : HBUINT16 +{ + bool has_peak () const { return ((*this) & EmbeddedPeakTuple) != 0; } + bool has_intermediate () const { return ((*this) & IntermediateRegion) != 0; } + bool has_private_points () const { return ((*this) & PrivatePointNumbers) != 0; } + unsigned int get_index () const { return (*this) & TupleIndexMask; } + + protected: + enum Flags { + EmbeddedPeakTuple = 0x8000u, + IntermediateRegion = 0x4000u, + PrivatePointNumbers = 0x2000u, + TupleIndexMask = 0x0FFFu + }; + + public: + DEFINE_SIZE_STATIC (2); +}; + +struct TupleVarHeader +{ + unsigned int get_size (unsigned int axis_count) const + { + return min_size + + (tupleIndex.has_peak ()? get_peak_tuple ().get_size (axis_count): 0) + + (tupleIndex.has_intermediate ()? (get_start_tuple (axis_count).get_size (axis_count) + + get_end_tuple (axis_count).get_size (axis_count)): 0); + } + + const Tuple &get_peak_tuple () const + { return StructAfter (tupleIndex); } + const Tuple &get_start_tuple (unsigned int axis_count) const + { return StructAfter (get_peak_tuple ()[tupleIndex.has_peak ()? axis_count: 0]); } + const Tuple &get_end_tuple (unsigned int axis_count) const + { return StructAfter (get_peak_tuple ()[tupleIndex.has_peak ()? (axis_count * 2): 0]); } + + HBUINT16 varDataSize; + TuppleIndex tupleIndex; + /* UnsizedArrayOf peakTuple - optional */ + /* UnsizedArrayOf intermediateStartTuple - optional */ + /* UnsizedArrayOf intermediateEndTuple - optional */ + + DEFINE_SIZE_MIN (4); +}; + +struct TupleVarCount : HBUINT16 +{ + bool has_shared_point_numbers () const { return ((*this) & SharedPointNumbers) != 0; } + unsigned int get_count () const { return (*this) & CountMask; } + + protected: + enum Flags { + SharedPointNumbers = 0x8000u, + CountMask = 0x0FFFu + }; + + public: + DEFINE_SIZE_STATIC (2); +}; + +struct GlyphVarData +{ + bool check_size (unsigned int axis_count, unsigned int len) const + { return (get_header_size (axis_count) <= len) && (data <= len); } + + unsigned int get_header_size (unsigned int axis_count) const + { + unsigned int size = min_size; + for (unsigned int i = 0; i < tupleVarCount.get_count (); i++) + size += get_tuple_var_header (axis_count, i).get_size (axis_count); // FIX THIS O(n^2) + + return size; + } + + const TupleVarHeader &get_tuple_var_header (unsigned int axis_count, unsigned int i) const + { + const TupleVarHeader *header = &StructAfter(data); + while (i-- > 0) + header = &StructAtOffset (header, header->get_size (axis_count)); + return *header; + } + + TupleVarCount tupleVarCount; + OffsetTo data; + /* TupleVarHeader tupleVarHeaders[] */ + + 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 + { return true; } // TOOD + + protected: + const GlyphVarData *get_glyph_var_data (unsigned int gid, unsigned int *length/*OUT*/) const + { + unsigned int start_offset = get_offset (gid); + unsigned int end_offset = get_offset (gid+1); + + if ((start_offset == end_offset) || + unlikely ((start_offset > get_offset (glyphCount)) || + (start_offset + GlyphVarData::min_size > end_offset))) + { + *length = 0; + return &Null(GlyphVarData); + } + const GlyphVarData *var_data = &(((unsigned char *)this+start_offset)+dataZ); + if (unlikely (!var_data->check_size (axisCount, end_offset - start_offset))) + { + *length = 0; + return &Null (GlyphVarData); + } + *length = end_offset - start_offset; + return var_data; + } + + bool is_long_offset () const { return (flags & 1)!=0; } + + unsigned int get_offset (unsigned int gid) const + { + if (is_long_offset ()) + return get_long_offset_array ()[gid]; + else + return get_short_offset_array ()[gid] * 2; + } + + const HBUINT32 *get_long_offset_array () const { return (const HBUINT32 *)&offsetZ; } + const HBUINT16 *get_short_offset_array () const { return (const HBUINT16 *)&offsetZ; } + + protected: + FixedVersion<> version; /* Version of gvar table. Set to 0x00010000u. */ + HBUINT16 axisCount; + HBUINT16 sharedTupleCount; + LOffsetTo sharedTuples; /* Actually 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); +}; + +} /* namespace OT */ + +#endif /* HB_OT_VAR_GVAR_TABLE_HH */ diff --git a/src/hb-ot-var-hvar-table.hh b/src/hb-ot-var-hvar-table.hh index 0e711d726..97e70605c 100644 --- a/src/hb-ot-var-hvar-table.hh +++ b/src/hb-ot-var-hvar-table.hh @@ -178,7 +178,7 @@ struct index_map_subset_plan_t } } - void fini (void) + void fini () { max_inners.fini (); output_map.fini (); @@ -207,14 +207,14 @@ struct index_map_subset_plan_t } } - unsigned int get_inner_bit_count (void) const { return inner_bit_count; } - unsigned int get_width (void) const { return ((outer_bit_count + inner_bit_count + 7) / 8); } - unsigned int get_map_count (void) const { return map_count; } + unsigned int get_inner_bit_count () const { return inner_bit_count; } + unsigned int get_width () const { return ((outer_bit_count + inner_bit_count + 7) / 8); } + unsigned int get_map_count () const { return map_count; } - unsigned int get_size (void) const + unsigned int get_size () const { return (map_count? (DeltaSetIndexMap::min_size + get_width () * map_count): 0); } - hb_array_t get_output_map (void) const { return output_map.as_array (); } + hb_array_t get_output_map () const { return output_map.as_array (); } protected: unsigned int map_count; @@ -254,7 +254,7 @@ struct hvarvvar_subset_plan_t index_map_plans[i].remap (plan, index_maps[i], outer_remap, inner_remaps); } - void fini (void) + void fini () { inner_remaps.fini_deep (); index_map_plans.fini_deep (); diff --git a/src/hb-subset.cc b/src/hb-subset.cc index 4441575ab..cff7c566f 100644 --- a/src/hb-subset.cc +++ b/src/hb-subset.cc @@ -45,6 +45,7 @@ #include "hb-ot-vorg-table.hh" #include "hb-ot-layout-gsub-table.hh" #include "hb-ot-layout-gpos-table.hh" +#include "hb-ot-var-gvar-table.hh" #include "hb-ot-var-hvar-table.hh" @@ -199,6 +200,9 @@ _subset_table (hb_subset_plan_t *plan, case HB_OT_TAG_GPOS: result = _subset2 (plan); break; + case HB_OT_TAG_gvar: + result = _subset2 (plan); + break; case HB_OT_TAG_HVAR: result = _subset2 (plan); break;