Added hb-ot-var-gvar-table.hh

implemented sanitize()
placeholder subset()
some code cleanup
This commit is contained in:
Michiharu Ariza 2019-02-26 11:11:50 -08:00
parent f289ffe5ea
commit eddbc9d0db
6 changed files with 222 additions and 221 deletions

View File

@ -126,6 +126,7 @@ HB_BASE_sources = \
hb-ot-var-fvar-table.hh \ hb-ot-var-fvar-table.hh \
hb-ot-var-hvar-table.hh \ hb-ot-var-hvar-table.hh \
hb-ot-var-mvar-table.hh \ hb-ot-var-mvar-table.hh \
hb-ot-var-gvar-table.hh \
hb-ot-var.cc \ hb-ot-var.cc \
hb-ot-vorg-table.hh \ hb-ot-vorg-table.hh \
hb-set-digest.hh \ hb-set-digest.hh \

View File

@ -38,14 +38,14 @@ struct hb_bimap_t
hb_bimap_t () { init (); } hb_bimap_t () { init (); }
~hb_bimap_t () { fini (); } ~hb_bimap_t () { fini (); }
void init (void) void init ()
{ {
count = 0; count = 0;
old_to_new.init (); old_to_new.init ();
new_to_old.init (); new_to_old.init ();
} }
void fini (void) void fini ()
{ {
old_to_new.fini (); old_to_new.fini ();
new_to_old.fini (); new_to_old.fini ();
@ -90,7 +90,7 @@ struct hb_bimap_t
/* Optional: after finished adding all mappings in a random order, /* Optional: after finished adding all mappings in a random order,
* reassign new ids to old ids so that both are in the same 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); new_to_old.qsort (cmp_id);
for (hb_codepoint_t _new = 0; _new < count; _new++) for (hb_codepoint_t _new = 0; _new < count; _new++)

View File

@ -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<F2DOT14>
coords;
public:
DEFINE_SIZE_ARRAY (0, coords);
};
struct TupleVarHeader
{
HBUINT16 varDataSize;
HBUINT16 tupleIndex;
/* Tuple peakTuple */
/* Tuple intermediateStartTuple */
/* Tuple intermediateEndTuple */
};
struct GlyphVarData
{
HBUINT16 tupleVarCount;
OffsetTo<HBUINT8>
data;
UnsizedArrayOf<TupleVarHeader>
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<VertOriginMetric> &subset_metrics,
unsigned int dest_sz,
void *dest) const
{
hb_serialize_context_t c (dest, dest_sz);
gvar *subset_table = c.start_serialize<gvar> ();
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<VertOriginMetric> (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<gvar> (plan->source);
const gvar *vorg_table = vorg_blob->as<gvar> ();
/* count the number of glyphs to be included in the subset table */
hb_vector_t<VertOriginMetric> 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<Tupple> sharedTuples;
HBUINT16 glyphCount;
HBUINT16 flags;
LOffsetTo<GlyphVarData>
glyphVarDataArray;
/* OffsetTo<GlyphVariationData> glyphVariationDataOffsets */
public:
DEFINE_SIZE_ARRAY(8, vertYOrigins);
};
} /* namespace OT */
#endif /* HB_OT_GVAR_TABLE_HH */

207
src/hb-ot-var-gvar-table.hh Normal file
View File

@ -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<F2DOT14> {};
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<Tuple> (tupleIndex); }
const Tuple &get_start_tuple (unsigned int axis_count) const
{ return StructAfter<Tuple> (get_peak_tuple ()[tupleIndex.has_peak ()? axis_count: 0]); }
const Tuple &get_end_tuple (unsigned int axis_count) const
{ return StructAfter<Tuple> (get_peak_tuple ()[tupleIndex.has_peak ()? (axis_count * 2): 0]); }
HBUINT16 varDataSize;
TuppleIndex tupleIndex;
/* UnsizedArrayOf<F2DOT14> peakTuple - optional */
/* UnsizedArrayOf<F2DOT14> intermediateStartTuple - optional */
/* UnsizedArrayOf<F2DOT14> 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<TupleVarHeader>(data);
while (i-- > 0)
header = &StructAtOffset<TupleVarHeader> (header, header->get_size (axis_count));
return *header;
}
TupleVarCount tupleVarCount;
OffsetTo<HBUINT8> 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<F2DOT14> sharedTuples; /* Actually LOffsetTo<UnsizedArrayOf<Tupple>> */
HBUINT16 glyphCount;
HBUINT16 flags;
LOffsetTo<GlyphVarData> dataZ; /* Array of GlyphVarData */
UnsizedArrayOf<HBUINT8> 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 */

View File

@ -178,7 +178,7 @@ struct index_map_subset_plan_t
} }
} }
void fini (void) void fini ()
{ {
max_inners.fini (); max_inners.fini ();
output_map.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_inner_bit_count () const { return inner_bit_count; }
unsigned int get_width (void) const { return ((outer_bit_count + inner_bit_count + 7) / 8); } unsigned int get_width () const { return ((outer_bit_count + inner_bit_count + 7) / 8); }
unsigned int get_map_count (void) const { return map_count; } 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); } { return (map_count? (DeltaSetIndexMap::min_size + get_width () * map_count): 0); }
hb_array_t<const unsigned int> get_output_map (void) const { return output_map.as_array (); } hb_array_t<const unsigned int> get_output_map () const { return output_map.as_array (); }
protected: protected:
unsigned int map_count; 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); index_map_plans[i].remap (plan, index_maps[i], outer_remap, inner_remaps);
} }
void fini (void) void fini ()
{ {
inner_remaps.fini_deep (); inner_remaps.fini_deep ();
index_map_plans.fini_deep (); index_map_plans.fini_deep ();

View File

@ -45,6 +45,7 @@
#include "hb-ot-vorg-table.hh" #include "hb-ot-vorg-table.hh"
#include "hb-ot-layout-gsub-table.hh" #include "hb-ot-layout-gsub-table.hh"
#include "hb-ot-layout-gpos-table.hh" #include "hb-ot-layout-gpos-table.hh"
#include "hb-ot-var-gvar-table.hh"
#include "hb-ot-var-hvar-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: case HB_OT_TAG_GPOS:
result = _subset2<const OT::GPOS> (plan); result = _subset2<const OT::GPOS> (plan);
break; break;
case HB_OT_TAG_gvar:
result = _subset2<const OT::gvar> (plan);
break;
case HB_OT_TAG_HVAR: case HB_OT_TAG_HVAR:
result = _subset2<const OT::HVAR> (plan); result = _subset2<const OT::HVAR> (plan);
break; break;