From d817b446a132816e48e9ce4e3619a52a340ab35a Mon Sep 17 00:00:00 2001 From: Michiharu Ariza Date: Mon, 25 Feb 2019 09:59:27 -0800 Subject: [PATCH] renamed hb_map2_t to hb_bimap_h in its own .hh --- src/Makefile.sources | 1 + src/hb-bimap.hh | 109 +++++++++++++++++++ src/hb-ot-cff-common.hh | 4 +- src/hb-ot-cff1-table.hh | 4 +- src/hb-ot-gvar-table.hh | 211 ++++++++++++++++++++++++++++++++++++ src/hb-ot-layout-common.hh | 84 +------------- src/hb-ot-var-hvar-table.hh | 12 +- src/hb-subset-cff-common.cc | 2 +- src/hb-subset-cff-common.hh | 4 +- src/hb-subset-cff1.cc | 8 +- src/hb-subset-cff2.cc | 2 +- 11 files changed, 343 insertions(+), 98 deletions(-) create mode 100644 src/hb-bimap.hh create mode 100644 src/hb-ot-gvar-table.hh diff --git a/src/Makefile.sources b/src/Makefile.sources index 0da4abe05..db818283f 100644 --- a/src/Makefile.sources +++ b/src/Makefile.sources @@ -41,6 +41,7 @@ HB_BASE_sources = \ hb-machinery.hh \ hb-map.cc \ hb-map.hh \ + hb-bimap.hh \ hb-mutex.hh \ hb-null.hh \ hb-object.hh \ diff --git a/src/hb-bimap.hh b/src/hb-bimap.hh new file mode 100644 index 000000000..1c8fa553c --- /dev/null +++ b/src/hb-bimap.hh @@ -0,0 +1,109 @@ +/* + * 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_BIMAP_HH +#define HB_BIMAP_HH + +#include "hb.hh" + +/* Bi-directional map. + * new ids are assigned incrementally & contiguously to old ids + * which may be added randomly & sparsely + * all mappings are 1-to-1 in both directions */ +struct hb_bimap_t +{ + hb_bimap_t () { init (); } + ~hb_bimap_t () { fini (); } + + void init (void) + { + count = 0; + old_to_new.init (); + new_to_old.init (); + } + + void fini (void) + { + old_to_new.fini (); + new_to_old.fini (); + } + + bool has (hb_codepoint_t _old) const { return old_to_new.has (_old); } + + hb_codepoint_t add (hb_codepoint_t _old) + { + hb_codepoint_t _new = old_to_new[_old]; + if (_new == HB_MAP_VALUE_INVALID) + { + _new = count++; + old_to_new.set (_old, _new); + new_to_old.resize (count); + new_to_old[_new] = _old; + } + return _new; + } + + /* returns HB_MAP_VALUE_INVALID if unmapped */ + hb_codepoint_t operator [] (hb_codepoint_t _old) const { return to_new (_old); } + hb_codepoint_t to_new (hb_codepoint_t _old) const { return old_to_new[_old]; } + hb_codepoint_t to_old (hb_codepoint_t _new) const { return (_new >= count)? HB_MAP_VALUE_INVALID: new_to_old[_new]; } + + bool identity (unsigned int size) + { + hb_codepoint_t i; + old_to_new.clear (); + new_to_old.resize (size); + for (i = 0; i < size; i++) + { + old_to_new.set (i, i); + new_to_old[i] = i; + } + count = i; + return old_to_new.successful && !new_to_old.in_error (); + } + + static int cmp_id (const void* a, const void* b) + { return (int)*(const hb_codepoint_t *)a - (int)*(const hb_codepoint_t *)b; } + + /* 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) + { + new_to_old.qsort (cmp_id); + for (hb_codepoint_t _new = 0; _new < count; _new++) + old_to_new.set (to_old (_new), _new); + } + + unsigned int get_count () const { return count; } + + protected: + unsigned int count; + hb_map_t old_to_new; + hb_vector_t + new_to_old; +}; + +#endif /* HB_BIMAP_HH */ diff --git a/src/hb-ot-cff-common.hh b/src/hb-ot-cff-common.hh index 5e0082d20..78e80b2fa 100644 --- a/src/hb-ot-cff-common.hh +++ b/src/hb-ot-cff-common.hh @@ -457,7 +457,7 @@ struct FDArray : CFFIndexOf unsigned int offSize_, const hb_vector_t &fontDicts, unsigned int fdCount, - const hb_map2_t &fdmap, + const hb_bimap_t &fdmap, OP_SERIALIZER& opszr, const hb_vector_t &privateInfos) { @@ -495,7 +495,7 @@ struct FDArray : CFFIndexOf static unsigned int calculate_serialized_size (unsigned int &offSize_ /* OUT */, const hb_vector_t &fontDicts, unsigned int fdCount, - const hb_map2_t &fdmap, + const hb_bimap_t &fdmap, OP_SERIALIZER& opszr) { unsigned int dictsSize = 0; diff --git a/src/hb-ot-cff1-table.hh b/src/hb-ot-cff1-table.hh index d65b2250a..2f502a7d2 100644 --- a/src/hb-ot-cff1-table.hh +++ b/src/hb-ot-cff1-table.hh @@ -570,7 +570,7 @@ struct Charset { struct CFF1StringIndex : CFF1Index { bool serialize (hb_serialize_context_t *c, const CFF1StringIndex &strings, - unsigned int offSize_, const hb_map2_t &sidmap) + unsigned int offSize_, const hb_bimap_t &sidmap) { TRACE_SERIALIZE (this); if (unlikely ((strings.count == 0) || (sidmap.get_count () == 0))) @@ -598,7 +598,7 @@ struct CFF1StringIndex : CFF1Index } /* in parallel to above */ - unsigned int calculate_serialized_size (unsigned int &offSize /*OUT*/, const hb_map2_t &sidmap) const + unsigned int calculate_serialized_size (unsigned int &offSize /*OUT*/, const hb_bimap_t &sidmap) const { offSize = 0; if ((count == 0) || (sidmap.get_count () == 0)) diff --git a/src/hb-ot-gvar-table.hh b/src/hb-ot-gvar-table.hh new file mode 100644 index 000000000..f0077150b --- /dev/null +++ b/src/hb-ot-gvar-table.hh @@ -0,0 +1,211 @@ +/* + * 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-layout-common.hh b/src/hb-ot-layout-common.hh index a6e8e3235..4771bf7b8 100644 --- a/src/hb-ot-layout-common.hh +++ b/src/hb-ot-layout-common.hh @@ -33,6 +33,7 @@ #include "hb-ot-layout.hh" #include "hb-open-type.hh" #include "hb-set.hh" +#include "hb-bimap.hh" #ifndef HB_MAX_NESTING_LEVEL @@ -1586,83 +1587,6 @@ static inline void ClassDef_serialize (hb_serialize_context_t *c, hb_array_t klasses) { c->start_embed ()->serialize (c, glyphs, klasses); } -/* Bi-directional map. - * nww ids are assigned incrementally & contiguous; old ids may be random & sparse - * all mappings unique & no duplicate */ -struct hb_map2_t -{ - hb_map2_t () { init (); } - ~hb_map2_t () { fini (); } - - void init (void) - { - count = 0; - old_to_new_map.init (); - new_to_old_map.init (); - } - - void fini (void) - { - old_to_new_map.fini (); - new_to_old_map.fini (); - } - - bool has (hb_codepoint_t _old) const { return old_to_new_map.has (_old); } - - hb_codepoint_t add (hb_codepoint_t _old) - { - hb_codepoint_t _new = old_to_new_map[_old]; - if (_new == HB_MAP_VALUE_INVALID) - { - _new = count++; - old_to_new_map.set (_old, _new); - new_to_old_map.set (_new, _old); - } - return _new; - } - - /* returns HB_MAP_VALUE_INVALID if unmapped */ - hb_codepoint_t operator [] (hb_codepoint_t _old) const { return old_to_new (_old); } - hb_codepoint_t old_to_new (hb_codepoint_t _old) const { return old_to_new_map[_old]; } - hb_codepoint_t new_to_old (hb_codepoint_t _new) const { return new_to_old_map[_new]; } - - bool identity (unsigned int size) - { - hb_codepoint_t i; - old_to_new_map.clear (); - new_to_old_map.clear (); - for (i = 0; i < size; i++) - { - old_to_new_map.set (i, i); - new_to_old_map.set (i, i); - } - count = i; - return old_to_new_map.successful && new_to_old_map.successful; - } - - /* Optional: after finished adding all mappings in a random order, - * reassign new ids to old ids so that they are in the same order. */ - void reorder (void) - { - hb_codepoint_t _new = 0; - for (hb_codepoint_t _old = 0; _new < count; _old++) - { - if (!has (_old)) continue; - new_to_old_map.set (_new, _old); - old_to_new_map.set (_old, _new); - _old++; - _new++; - } - } - - unsigned int get_count () const { return count; } - - protected: - unsigned int count; - hb_map_t old_to_new_map; - hb_map_t new_to_old_map; -}; - /* * Item Variation Store */ @@ -1824,7 +1748,7 @@ struct VarData bool serialize (hb_serialize_context_t *c, const VarData *src, - const hb_map2_t &remap) + const hb_bimap_t &remap) { TRACE_SUBSET (this); if (unlikely (!c->extend_min (*this))) return_trace (false); @@ -1839,7 +1763,7 @@ struct VarData HBUINT8 *p = get_delta_bytes (); for (unsigned int i = 0; i < remap.get_count (); i++) { - memcpy (p, src->get_delta_bytes () + (row_size * remap.new_to_old (i)), row_size); + memcpy (p, src->get_delta_bytes () + (row_size * remap.to_old (i)), row_size); p += row_size; } @@ -1897,7 +1821,7 @@ struct VariationStore bool serialize (hb_serialize_context_t *c, const VariationStore *src, - const hb_array_t &inner_remaps) + const hb_array_t &inner_remaps) { TRACE_SUBSET (this); unsigned int size = min_size + HBUINT32::static_size * inner_remaps.length; diff --git a/src/hb-ot-var-hvar-table.hh b/src/hb-ot-var-hvar-table.hh index 382da7aff..0e711d726 100644 --- a/src/hb-ot-var-hvar-table.hh +++ b/src/hb-ot-var-hvar-table.hh @@ -122,8 +122,8 @@ struct DeltaSetIndexMap struct index_map_subset_plan_t { void init (const DeltaSetIndexMap &index_map, - hb_map2_t &outer_remap, - hb_vector_t &inner_remaps, + hb_bimap_t &outer_remap, + hb_vector_t &inner_remaps, const hb_subset_plan_t *plan) { map_count = 0; @@ -186,8 +186,8 @@ struct index_map_subset_plan_t void remap (const hb_subset_plan_t *plan, const DeltaSetIndexMap *input_map, - const hb_map2_t &outer_remap, - const hb_vector_t &inner_remaps) + const hb_bimap_t &outer_remap, + const hb_vector_t &inner_remaps) { for (unsigned int i = 0; i < max_inners.length; i++) { @@ -260,8 +260,8 @@ struct hvarvvar_subset_plan_t index_map_plans.fini_deep (); } - hb_map2_t outer_remap; - hb_vector_t inner_remaps; + hb_bimap_t outer_remap; + hb_vector_t inner_remaps; hb_vector_t index_map_plans; const VariationStore *var_store; diff --git a/src/hb-subset-cff-common.cc b/src/hb-subset-cff-common.cc index a7e05fe03..4c163ae63 100644 --- a/src/hb-subset-cff-common.cc +++ b/src/hb-subset-cff-common.cc @@ -50,7 +50,7 @@ hb_plan_subset_cff_fdselect (const hb_subset_plan_t *plan, unsigned int &subset_fdselect_size /* OUT */, unsigned int &subset_fdselect_format /* OUT */, hb_vector_t &fdselect_ranges /* OUT */, - hb_map2_t &fdmap /* OUT */) + hb_bimap_t &fdmap /* OUT */) { subset_fd_count = 0; subset_fdselect_size = 0; diff --git a/src/hb-subset-cff-common.hh b/src/hb-subset-cff-common.hh index 2694e0c56..331ddbc22 100644 --- a/src/hb-subset-cff-common.hh +++ b/src/hb-subset-cff-common.hh @@ -541,7 +541,7 @@ struct subr_subset_param_t bool drop_hints; }; -struct subr_remap_t : hb_map2_t +struct subr_remap_t : hb_bimap_t { void create (hb_set_t *closure) { @@ -1012,7 +1012,7 @@ hb_plan_subset_cff_fdselect (const hb_subset_plan_t *plan, unsigned int &subset_fdselect_size /* OUT */, unsigned int &subset_fdselect_format /* OUT */, hb_vector_t &fdselect_ranges /* OUT */, - CFF::hb_map2_t &fdmap /* OUT */); + hb_bimap_t &fdmap /* OUT */); HB_INTERNAL bool hb_serialize_cff_fdselect (hb_serialize_context_t *c, diff --git a/src/hb-subset-cff1.cc b/src/hb-subset-cff1.cc index 05ac471ee..88374fb5e 100644 --- a/src/hb-subset-cff1.cc +++ b/src/hb-subset-cff1.cc @@ -34,12 +34,12 @@ using namespace CFF; -struct remap_sid_t : hb_map2_t +struct remap_sid_t : hb_bimap_t { unsigned int add (unsigned int sid) { if ((sid != CFF_UNDEF_SID) && !is_std_std (sid)) - return offset_sid (hb_map2_t::add (unoffset_sid (sid))); + return offset_sid (hb_bimap_t::add (unoffset_sid (sid))); else return sid; } @@ -49,7 +49,7 @@ struct remap_sid_t : hb_map2_t if (is_std_std (sid) || (sid == CFF_UNDEF_SID)) return sid; else - return offset_sid (hb_map2_t::operator [] (unoffset_sid (sid))); + return offset_sid (hb_bimap_t::operator [] (unoffset_sid (sid))); } static const unsigned int num_std_strings = 391; @@ -850,7 +850,7 @@ struct cff_subset_plan { /* font dict index remap table from fullset FDArray to subset FDArray. * set to CFF_UNDEF_CODE if excluded from subset */ - hb_map2_t fdmap; + hb_bimap_t fdmap; str_buff_vec_t subset_charstrings; str_buff_vec_t subset_globalsubrs; diff --git a/src/hb-subset-cff2.cc b/src/hb-subset-cff2.cc index 1211a166a..a73b1d59e 100644 --- a/src/hb-subset-cff2.cc +++ b/src/hb-subset-cff2.cc @@ -425,7 +425,7 @@ struct cff2_subset_plan { unsigned int subset_fdselect_format; hb_vector_t subset_fdselect_ranges; - hb_map2_t fdmap; + hb_bimap_t fdmap; str_buff_vec_t subset_charstrings; str_buff_vec_t subset_globalsubrs;