2017-01-20 04:35:48 +01:00
|
|
|
/*
|
|
|
|
* Copyright © 2017 Google, 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.
|
|
|
|
*
|
|
|
|
* Google Author(s): Behdad Esfahbod
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef HB_OT_VAR_FVAR_TABLE_HH
|
|
|
|
#define HB_OT_VAR_FVAR_TABLE_HH
|
|
|
|
|
2018-08-26 07:36:36 +02:00
|
|
|
#include "hb-open-type.hh"
|
2017-01-20 04:35:48 +01:00
|
|
|
|
2018-04-12 11:08:19 +02:00
|
|
|
/*
|
|
|
|
* fvar -- Font Variations
|
|
|
|
* https://docs.microsoft.com/en-us/typography/opentype/spec/fvar
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define HB_OT_TAG_fvar HB_TAG('f','v','a','r')
|
|
|
|
|
|
|
|
|
2017-01-20 04:35:48 +01:00
|
|
|
namespace OT {
|
|
|
|
|
|
|
|
|
|
|
|
struct InstanceRecord
|
|
|
|
{
|
2018-11-19 20:27:19 +01:00
|
|
|
friend struct fvar;
|
|
|
|
|
|
|
|
inline hb_array_t<const Fixed> get_coordinates (unsigned int axis_count) const
|
|
|
|
{ return coordinatesZ.as_array (axis_count); }
|
|
|
|
|
2017-01-20 04:35:48 +01:00
|
|
|
inline bool sanitize (hb_sanitize_context_t *c, unsigned int axis_count) const
|
|
|
|
{
|
|
|
|
TRACE_SANITIZE (this);
|
|
|
|
return_trace (c->check_struct (this) &&
|
2018-09-10 23:18:07 +02:00
|
|
|
c->check_array (coordinatesZ.arrayZ, axis_count));
|
2017-01-20 04:35:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2018-03-14 16:31:53 +01:00
|
|
|
NameID subfamilyNameID;/* The name ID for entries in the 'name' table
|
2017-01-20 04:35:48 +01:00
|
|
|
* that provide subfamily names for this instance. */
|
2018-11-19 17:34:56 +01:00
|
|
|
HBUINT16 flags; /* Reserved for future use — set to 0. */
|
2018-09-10 23:02:24 +02:00
|
|
|
UnsizedArrayOf<Fixed>
|
|
|
|
coordinatesZ; /* The coordinates array for this instance. */
|
2018-03-14 16:31:53 +01:00
|
|
|
//NameID postScriptNameIDX;/*Optional. The name ID for entries in the 'name'
|
2017-01-20 04:35:48 +01:00
|
|
|
// * table that provide PostScript names for this
|
|
|
|
// * instance. */
|
|
|
|
|
|
|
|
public:
|
2018-09-10 23:02:24 +02:00
|
|
|
DEFINE_SIZE_ARRAY (4, coordinatesZ);
|
2017-01-20 04:35:48 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
struct AxisRecord
|
|
|
|
{
|
2018-11-19 17:34:56 +01:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
AXIS_FLAG_HIDDEN = 0x0001,
|
|
|
|
};
|
|
|
|
|
2017-01-20 04:35:48 +01:00
|
|
|
inline bool sanitize (hb_sanitize_context_t *c) const
|
|
|
|
{
|
|
|
|
TRACE_SANITIZE (this);
|
|
|
|
return_trace (c->check_struct (this));
|
|
|
|
}
|
|
|
|
|
2017-01-21 04:14:54 +01:00
|
|
|
public:
|
2017-01-20 04:35:48 +01:00
|
|
|
Tag axisTag; /* Tag identifying the design variation for the axis. */
|
|
|
|
Fixed minValue; /* The minimum coordinate value for the axis. */
|
|
|
|
Fixed defaultValue; /* The default coordinate value for the axis. */
|
|
|
|
Fixed maxValue; /* The maximum coordinate value for the axis. */
|
2018-11-19 17:34:56 +01:00
|
|
|
HBUINT16 flags; /* Axis flags. */
|
2018-03-14 16:31:53 +01:00
|
|
|
NameID axisNameID; /* The name ID for entries in the 'name' table that
|
2017-01-20 04:35:48 +01:00
|
|
|
* provide a display name for this axis. */
|
|
|
|
|
|
|
|
public:
|
|
|
|
DEFINE_SIZE_STATIC (20);
|
|
|
|
};
|
|
|
|
|
|
|
|
struct fvar
|
|
|
|
{
|
|
|
|
static const hb_tag_t tableTag = HB_OT_TAG_fvar;
|
|
|
|
|
2018-11-16 23:31:05 +01:00
|
|
|
inline bool has_data (void) const { return version.to_int (); }
|
2018-08-06 15:30:12 +02:00
|
|
|
|
2017-01-20 04:35:48 +01:00
|
|
|
inline bool sanitize (hb_sanitize_context_t *c) const
|
|
|
|
{
|
|
|
|
TRACE_SANITIZE (this);
|
|
|
|
return_trace (version.sanitize (c) &&
|
2017-01-21 04:14:54 +01:00
|
|
|
likely (version.major == 1) &&
|
|
|
|
c->check_struct (this) &&
|
2018-11-19 19:09:53 +01:00
|
|
|
axisSize == 20 && /* Assumed in our code. */
|
2017-01-21 04:14:54 +01:00
|
|
|
instanceSize >= axisCount * 4 + 4 &&
|
2018-11-19 19:09:53 +01:00
|
|
|
get_axes ().sanitize (c) &&
|
2018-11-19 20:27:19 +01:00
|
|
|
c->check_range (&get_instance (0), instanceCount, instanceSize));
|
2017-01-20 04:35:48 +01:00
|
|
|
}
|
|
|
|
|
2017-01-21 04:14:54 +01:00
|
|
|
inline unsigned int get_axis_count (void) const
|
|
|
|
{ return axisCount; }
|
2017-01-20 04:35:48 +01:00
|
|
|
|
2017-01-21 04:57:27 +01:00
|
|
|
inline bool get_axis (unsigned int index, hb_ot_var_axis_t *info) const
|
|
|
|
{
|
|
|
|
if (info)
|
|
|
|
{
|
|
|
|
const AxisRecord &axis = get_axes ()[index];
|
|
|
|
info->tag = axis.axisTag;
|
|
|
|
info->name_id = axis.axisNameID;
|
|
|
|
info->default_value = axis.defaultValue / 65536.;
|
|
|
|
/* Ensure order, to simplify client math. */
|
|
|
|
info->min_value = MIN<float> (info->default_value, axis.minValue / 65536.);
|
|
|
|
info->max_value = MAX<float> (info->default_value, axis.maxValue / 65536.);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-11-19 18:36:56 +01:00
|
|
|
inline hb_ot_var_axis_flags_t get_axis_flags (unsigned int index) const
|
|
|
|
{
|
|
|
|
const AxisRecord &axis = get_axes ()[index];
|
|
|
|
return (hb_ot_var_axis_flags_t) (unsigned int) axis.flags;
|
|
|
|
}
|
|
|
|
|
2017-01-21 04:57:27 +01:00
|
|
|
inline unsigned int get_axis_infos (unsigned int start_offset,
|
|
|
|
unsigned int *axes_count /* IN/OUT */,
|
|
|
|
hb_ot_var_axis_t *axes_array /* OUT */) const
|
|
|
|
{
|
|
|
|
if (axes_count)
|
|
|
|
{
|
|
|
|
unsigned int count = axisCount;
|
|
|
|
start_offset = MIN (start_offset, count);
|
|
|
|
|
|
|
|
count -= start_offset;
|
|
|
|
axes_array += start_offset;
|
|
|
|
|
|
|
|
count = MIN (count, *axes_count);
|
|
|
|
*axes_count = count;
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i < count; i++)
|
|
|
|
get_axis (start_offset + i, axes_array + i);
|
|
|
|
}
|
|
|
|
return axisCount;
|
|
|
|
}
|
|
|
|
|
2017-01-21 04:14:54 +01:00
|
|
|
inline bool find_axis (hb_tag_t tag, unsigned int *index, hb_ot_var_axis_t *info) const
|
|
|
|
{
|
|
|
|
const AxisRecord *axes = get_axes ();
|
|
|
|
unsigned int count = get_axis_count ();
|
|
|
|
for (unsigned int i = 0; i < count; i++)
|
|
|
|
if (axes[i].axisTag == tag)
|
|
|
|
{
|
|
|
|
if (index)
|
|
|
|
*index = i;
|
2017-01-21 04:57:27 +01:00
|
|
|
return get_axis (i, info);
|
2017-01-21 04:14:54 +01:00
|
|
|
}
|
|
|
|
if (index)
|
|
|
|
*index = HB_OT_VAR_NO_AXIS_INDEX;
|
|
|
|
return false;
|
|
|
|
}
|
2017-01-20 04:35:48 +01:00
|
|
|
|
2017-01-21 04:57:27 +01:00
|
|
|
inline int normalize_axis_value (unsigned int axis_index, float v) const
|
2017-01-21 04:14:54 +01:00
|
|
|
{
|
|
|
|
hb_ot_var_axis_t axis;
|
2017-01-21 04:57:27 +01:00
|
|
|
if (!get_axis (axis_index, &axis))
|
2017-01-21 04:14:54 +01:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
v = MAX (MIN (v, axis.max_value), axis.min_value); /* Clamp. */
|
|
|
|
|
|
|
|
if (v == axis.default_value)
|
|
|
|
return 0;
|
|
|
|
else if (v < axis.default_value)
|
|
|
|
v = (v - axis.default_value) / (axis.default_value - axis.min_value);
|
|
|
|
else
|
|
|
|
v = (v - axis.default_value) / (axis.max_value - axis.default_value);
|
2018-10-04 00:44:18 +02:00
|
|
|
return (int) (v * 16384.f + (v >= 0.f ? .5f : -.5f));
|
2017-01-21 04:14:54 +01:00
|
|
|
}
|
2017-01-20 04:35:48 +01:00
|
|
|
|
2018-11-19 20:27:19 +01:00
|
|
|
inline unsigned int get_instance_count (void) const
|
|
|
|
{ return instanceCount; }
|
|
|
|
|
|
|
|
inline hb_ot_name_id_t get_instance_subfamily_name_id (unsigned int index) const
|
|
|
|
{
|
|
|
|
const InstanceRecord &instance = get_instance (index);
|
|
|
|
return instance.subfamilyNameID;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline hb_ot_name_id_t get_instance_postscript_name_id (unsigned int index) const
|
|
|
|
{
|
|
|
|
const InstanceRecord &instance = get_instance (index);
|
|
|
|
if (instanceSize >= axisCount * 4 + 6)
|
|
|
|
return StructAfter<NameID> (instance.get_coordinates (axisCount));
|
|
|
|
return HB_OT_NAME_ID_INVALID;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline unsigned int get_instance_coords (unsigned int index,
|
|
|
|
unsigned int *coords_length, /* IN/OUT */
|
|
|
|
int *coords /* OUT */) const
|
|
|
|
{
|
|
|
|
if (unlikely (index >= instanceCount))
|
|
|
|
{
|
|
|
|
if (coords_length)
|
|
|
|
*coords_length = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (coords_length && *coords_length)
|
|
|
|
{
|
|
|
|
const InstanceRecord &instance = get_instance (index);
|
|
|
|
hb_array_t<const Fixed> instanceCoords = instance.get_coordinates (axisCount)
|
|
|
|
.sub_array (0, *coords_length);
|
|
|
|
for (unsigned int i = 0; i < instanceCoords.len; i++)
|
|
|
|
coords[i] = instanceCoords.arrayZ[i].to_float ();
|
|
|
|
}
|
|
|
|
return axisCount;
|
|
|
|
}
|
|
|
|
|
2017-01-21 04:57:27 +01:00
|
|
|
protected:
|
2018-11-19 19:04:43 +01:00
|
|
|
inline hb_array_t<const AxisRecord> get_axes (void) const
|
|
|
|
{ return hb_array (&(this+firstAxis), axisCount); }
|
2017-01-21 04:57:27 +01:00
|
|
|
|
2018-11-19 20:27:19 +01:00
|
|
|
inline const InstanceRecord &get_instance (unsigned int i) const
|
|
|
|
{
|
|
|
|
if (unlikely (i >= instanceCount)) return Null (InstanceRecord);
|
|
|
|
|
|
|
|
return StructAtOffset<InstanceRecord> (&StructAfter<InstanceRecord> (get_axes ()),
|
|
|
|
i * instanceSize);
|
|
|
|
}
|
2017-01-21 04:57:27 +01:00
|
|
|
|
2017-01-20 04:35:48 +01:00
|
|
|
protected:
|
|
|
|
FixedVersion<>version; /* Version of the fvar table
|
|
|
|
* initially set to 0x00010000u */
|
2018-11-19 18:53:53 +01:00
|
|
|
OffsetTo<AxisRecord>
|
|
|
|
firstAxis; /* Offset in bytes from the beginning of the table
|
2017-01-20 04:35:48 +01:00
|
|
|
* to the start of the AxisRecord array. */
|
2018-01-10 03:07:30 +01:00
|
|
|
HBUINT16 reserved; /* This field is permanently reserved. Set to 2. */
|
|
|
|
HBUINT16 axisCount; /* The number of variation axes in the font (the
|
2017-01-20 04:35:48 +01:00
|
|
|
* number of records in the axes array). */
|
2018-01-10 03:07:30 +01:00
|
|
|
HBUINT16 axisSize; /* The size in bytes of each VariationAxisRecord —
|
2017-01-20 04:35:48 +01:00
|
|
|
* set to 20 (0x0014) for this version. */
|
2018-01-10 03:07:30 +01:00
|
|
|
HBUINT16 instanceCount; /* The number of named instances defined in the font
|
2017-01-20 04:35:48 +01:00
|
|
|
* (the number of records in the instances array). */
|
2018-01-10 03:07:30 +01:00
|
|
|
HBUINT16 instanceSize; /* The size in bytes of each InstanceRecord — set
|
2017-01-20 04:35:48 +01:00
|
|
|
* to either axisCount * sizeof(Fixed) + 4, or to
|
|
|
|
* axisCount * sizeof(Fixed) + 6. */
|
|
|
|
|
|
|
|
public:
|
|
|
|
DEFINE_SIZE_STATIC (16);
|
|
|
|
};
|
|
|
|
|
|
|
|
} /* namespace OT */
|
|
|
|
|
|
|
|
|
|
|
|
#endif /* HB_OT_VAR_FVAR_TABLE_HH */
|