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;
|
|
|
|
|
2019-09-10 19:31:07 +02:00
|
|
|
hb_array_t<const HBFixed> get_coordinates (unsigned int axis_count) const
|
2018-11-19 20:27:19 +01:00
|
|
|
{ return coordinatesZ.as_array (axis_count); }
|
|
|
|
|
2018-12-16 20:08:10 +01:00
|
|
|
bool sanitize (hb_sanitize_context_t *c, unsigned int axis_count) const
|
2017-01-20 04:35:48 +01:00
|
|
|
{
|
|
|
|
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. */
|
2019-09-10 19:31:07 +02:00
|
|
|
UnsizedArrayOf<HBFixed>
|
2018-09-10 23:02:24 +02:00
|
|
|
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-11-22 07:18:55 +01:00
|
|
|
DEFINE_SIZE_UNBOUNDED (4);
|
2017-01-20 04:35:48 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
struct AxisRecord
|
|
|
|
{
|
2020-06-28 10:43:25 +02:00
|
|
|
int cmp (hb_tag_t key) const { return axisTag.cmp (key); }
|
|
|
|
|
2018-11-19 17:34:56 +01:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
AXIS_FLAG_HIDDEN = 0x0001,
|
|
|
|
};
|
|
|
|
|
2020-06-20 10:54:32 +02:00
|
|
|
#ifndef HB_DISABLE_DEPRECATED
|
|
|
|
void get_axis_deprecated (hb_ot_var_axis_t *info) const
|
|
|
|
{
|
|
|
|
info->tag = axisTag;
|
|
|
|
info->name_id = axisNameID;
|
2020-06-30 10:24:29 +02:00
|
|
|
get_coordinates (info->min_value, info->default_value, info->max_value);
|
2020-06-20 10:54:32 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void get_axis_info (unsigned axis_index, hb_ot_var_axis_info_t *info) const
|
|
|
|
{
|
|
|
|
info->axis_index = axis_index;
|
|
|
|
info->tag = axisTag;
|
|
|
|
info->name_id = axisNameID;
|
|
|
|
info->flags = (hb_ot_var_axis_flags_t) (unsigned int) flags;
|
2020-06-30 10:24:29 +02:00
|
|
|
get_coordinates (info->min_value, info->default_value, info->max_value);
|
2020-06-20 10:54:32 +02:00
|
|
|
info->reserved = 0;
|
|
|
|
}
|
|
|
|
|
2022-06-22 04:29:52 +02:00
|
|
|
hb_tag_t get_axis_tag () const { return axisTag; }
|
|
|
|
|
2020-06-30 08:38:42 +02:00
|
|
|
int normalize_axis_value (float v) const
|
|
|
|
{
|
2020-06-30 10:24:29 +02:00
|
|
|
float min_value, default_value, max_value;
|
|
|
|
get_coordinates (min_value, default_value, max_value);
|
2020-06-30 08:38:42 +02:00
|
|
|
|
|
|
|
v = hb_clamp (v, min_value, max_value);
|
|
|
|
|
|
|
|
if (v == default_value)
|
|
|
|
return 0;
|
|
|
|
else if (v < default_value)
|
|
|
|
v = (v - default_value) / (default_value - min_value);
|
|
|
|
else
|
|
|
|
v = (v - default_value) / (max_value - default_value);
|
|
|
|
return roundf (v * 16384.f);
|
|
|
|
}
|
|
|
|
|
|
|
|
float unnormalize_axis_value (int v) const
|
|
|
|
{
|
2020-06-30 10:24:29 +02:00
|
|
|
float min_value, default_value, max_value;
|
|
|
|
get_coordinates (min_value, default_value, max_value);
|
2020-06-30 08:38:42 +02:00
|
|
|
|
|
|
|
if (v == 0)
|
|
|
|
return default_value;
|
|
|
|
else if (v < 0)
|
|
|
|
return v * (default_value - min_value) / 16384.f + default_value;
|
|
|
|
else
|
|
|
|
return v * (max_value - default_value) / 16384.f + default_value;
|
|
|
|
}
|
|
|
|
|
2020-06-30 08:18:36 +02:00
|
|
|
hb_ot_name_id_t get_name_id () const { return axisNameID; }
|
|
|
|
|
2018-12-16 20:08:10 +01:00
|
|
|
bool sanitize (hb_sanitize_context_t *c) const
|
2017-01-20 04:35:48 +01:00
|
|
|
{
|
|
|
|
TRACE_SANITIZE (this);
|
|
|
|
return_trace (c->check_struct (this));
|
|
|
|
}
|
|
|
|
|
2020-06-30 08:38:42 +02:00
|
|
|
protected:
|
2020-06-30 10:24:29 +02:00
|
|
|
void get_coordinates (float &min, float &default_, float &max) const
|
2020-06-30 08:23:12 +02:00
|
|
|
{
|
2020-06-30 10:24:29 +02:00
|
|
|
default_ = defaultValue / 65536.f;
|
2020-06-30 08:23:12 +02:00
|
|
|
/* Ensure order, to simplify client math. */
|
2020-06-30 10:24:29 +02:00
|
|
|
min = hb_min (default_, minValue / 65536.f);
|
|
|
|
max = hb_max (default_, maxValue / 65536.f);
|
2020-06-30 08:23:12 +02:00
|
|
|
}
|
|
|
|
|
2021-07-22 21:18:48 +02:00
|
|
|
public:
|
2017-01-20 04:35:48 +01:00
|
|
|
Tag axisTag; /* Tag identifying the design variation for the axis. */
|
2021-07-22 21:18:48 +02:00
|
|
|
protected:
|
2020-04-24 21:15:17 +02:00
|
|
|
HBFixed minValue; /* The minimum coordinate value for the axis. */
|
|
|
|
HBFixed defaultValue; /* The default coordinate value for the axis. */
|
|
|
|
HBFixed maxValue; /* The maximum coordinate value for the axis. */
|
2021-07-22 21:18:48 +02:00
|
|
|
public:
|
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
|
|
|
|
{
|
2019-01-22 12:08:57 +01:00
|
|
|
static constexpr hb_tag_t tableTag = HB_OT_TAG_fvar;
|
2017-01-20 04:35:48 +01:00
|
|
|
|
2018-12-17 19:01:01 +01:00
|
|
|
bool has_data () const { return version.to_int (); }
|
2018-08-06 15:30:12 +02:00
|
|
|
|
2018-12-16 20:08:10 +01:00
|
|
|
bool sanitize (hb_sanitize_context_t *c) const
|
2017-01-20 04:35:48 +01:00
|
|
|
{
|
|
|
|
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-22 07:18:55 +01:00
|
|
|
c->check_range (get_instance (0), instanceCount, instanceSize));
|
2017-01-20 04:35:48 +01:00
|
|
|
}
|
|
|
|
|
2018-12-17 19:01:01 +01:00
|
|
|
unsigned int get_axis_count () const { return axisCount; }
|
2017-01-20 04:35:48 +01:00
|
|
|
|
2019-05-11 09:37:01 +02:00
|
|
|
#ifndef HB_DISABLE_DEPRECATED
|
2018-12-16 20:08:10 +01:00
|
|
|
unsigned int get_axes_deprecated (unsigned int start_offset,
|
|
|
|
unsigned int *axes_count /* IN/OUT */,
|
|
|
|
hb_ot_var_axis_t *axes_array /* OUT */) const
|
2018-11-21 02:40:55 +01:00
|
|
|
{
|
|
|
|
if (axes_count)
|
2017-01-21 04:57:27 +01:00
|
|
|
{
|
2020-06-20 23:06:16 +02:00
|
|
|
hb_array_t<const AxisRecord> arr = get_axes ().sub_array (start_offset, axes_count);
|
2020-03-03 10:41:11 +01:00
|
|
|
for (unsigned i = 0; i < arr.length; ++i)
|
2020-06-20 23:06:16 +02:00
|
|
|
arr[i].get_axis_deprecated (&axes_array[i]);
|
2018-11-21 02:40:55 +01:00
|
|
|
}
|
|
|
|
return axisCount;
|
2017-01-21 04:57:27 +01:00
|
|
|
}
|
2019-05-11 09:37:01 +02:00
|
|
|
#endif
|
2017-01-21 04:57:27 +01:00
|
|
|
|
2018-12-16 20:08:10 +01:00
|
|
|
unsigned int get_axis_infos (unsigned int start_offset,
|
|
|
|
unsigned int *axes_count /* IN/OUT */,
|
|
|
|
hb_ot_var_axis_info_t *axes_array /* OUT */) const
|
2017-01-21 04:57:27 +01:00
|
|
|
{
|
|
|
|
if (axes_count)
|
|
|
|
{
|
2020-06-20 23:06:16 +02:00
|
|
|
hb_array_t<const AxisRecord> arr = get_axes ().sub_array (start_offset, axes_count);
|
2020-03-03 10:41:11 +01:00
|
|
|
for (unsigned i = 0; i < arr.length; ++i)
|
2020-06-20 23:06:16 +02:00
|
|
|
arr[i].get_axis_info (start_offset + i, &axes_array[i]);
|
2017-01-21 04:57:27 +01:00
|
|
|
}
|
|
|
|
return axisCount;
|
|
|
|
}
|
|
|
|
|
2019-05-11 09:37:01 +02:00
|
|
|
#ifndef HB_DISABLE_DEPRECATED
|
2020-06-21 06:48:12 +02:00
|
|
|
bool
|
|
|
|
find_axis_deprecated (hb_tag_t tag, unsigned *axis_index, hb_ot_var_axis_t *info) const
|
2017-01-21 04:14:54 +01:00
|
|
|
{
|
2020-06-28 10:43:25 +02:00
|
|
|
unsigned i;
|
|
|
|
if (!axis_index) axis_index = &i;
|
|
|
|
*axis_index = HB_OT_VAR_NO_AXIS_INDEX;
|
2020-07-01 23:00:24 +02:00
|
|
|
auto axes = get_axes ();
|
2022-06-16 00:57:16 +02:00
|
|
|
return axes.lfind (tag, axis_index) && ((void) axes[*axis_index].get_axis_deprecated (info), true);
|
2018-11-21 02:40:55 +01:00
|
|
|
}
|
2019-05-11 09:37:01 +02:00
|
|
|
#endif
|
2020-06-21 06:48:12 +02:00
|
|
|
bool
|
|
|
|
find_axis_info (hb_tag_t tag, hb_ot_var_axis_info_t *info) const
|
2018-11-21 02:40:55 +01:00
|
|
|
{
|
2020-06-28 10:43:25 +02:00
|
|
|
unsigned i;
|
2020-07-01 23:00:24 +02:00
|
|
|
auto axes = get_axes ();
|
2022-06-16 00:57:16 +02:00
|
|
|
return axes.lfind (tag, &i) && ((void) axes[i].get_axis_info (i, info), true);
|
2017-01-21 04:14:54 +01:00
|
|
|
}
|
2017-01-20 04:35:48 +01:00
|
|
|
|
2018-12-16 20:08:10 +01:00
|
|
|
int normalize_axis_value (unsigned int axis_index, float v) const
|
2020-06-30 08:38:42 +02:00
|
|
|
{ return get_axes ()[axis_index].normalize_axis_value (v); }
|
2017-01-21 04:14:54 +01:00
|
|
|
|
2020-06-30 08:38:42 +02:00
|
|
|
float unnormalize_axis_value (unsigned int axis_index, int v) const
|
|
|
|
{ return get_axes ()[axis_index].unnormalize_axis_value (v); }
|
2019-08-07 16:15:39 +02:00
|
|
|
|
2018-12-17 19:01:01 +01:00
|
|
|
unsigned int get_instance_count () const { return instanceCount; }
|
2018-11-19 20:27:19 +01:00
|
|
|
|
2018-12-16 20:08:10 +01:00
|
|
|
hb_ot_name_id_t get_instance_subfamily_name_id (unsigned int instance_index) const
|
2018-11-19 20:27:19 +01:00
|
|
|
{
|
2018-11-22 07:18:55 +01:00
|
|
|
const InstanceRecord *instance = get_instance (instance_index);
|
|
|
|
if (unlikely (!instance)) return HB_OT_NAME_ID_INVALID;
|
|
|
|
return instance->subfamilyNameID;
|
2018-11-19 20:27:19 +01:00
|
|
|
}
|
|
|
|
|
2018-12-16 20:08:10 +01:00
|
|
|
hb_ot_name_id_t get_instance_postscript_name_id (unsigned int instance_index) const
|
2018-11-19 20:27:19 +01:00
|
|
|
{
|
2018-11-22 07:18:55 +01:00
|
|
|
const InstanceRecord *instance = get_instance (instance_index);
|
|
|
|
if (unlikely (!instance)) return HB_OT_NAME_ID_INVALID;
|
2018-11-19 20:27:19 +01:00
|
|
|
if (instanceSize >= axisCount * 4 + 6)
|
2018-11-22 07:18:55 +01:00
|
|
|
return StructAfter<NameID> (instance->get_coordinates (axisCount));
|
2018-11-19 20:27:19 +01:00
|
|
|
return HB_OT_NAME_ID_INVALID;
|
|
|
|
}
|
|
|
|
|
2018-12-16 20:08:10 +01:00
|
|
|
unsigned int get_instance_coords (unsigned int instance_index,
|
2019-05-15 07:45:03 +02:00
|
|
|
unsigned int *coords_length, /* IN/OUT */
|
|
|
|
float *coords /* OUT */) const
|
2018-11-19 20:27:19 +01:00
|
|
|
{
|
2018-11-22 07:18:55 +01:00
|
|
|
const InstanceRecord *instance = get_instance (instance_index);
|
|
|
|
if (unlikely (!instance))
|
2018-11-19 20:27:19 +01:00
|
|
|
{
|
|
|
|
if (coords_length)
|
2019-08-24 15:27:14 +02:00
|
|
|
*coords_length = 0;
|
2018-11-19 20:27:19 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (coords_length && *coords_length)
|
|
|
|
{
|
2019-09-10 19:31:07 +02:00
|
|
|
hb_array_t<const HBFixed> instanceCoords = instance->get_coordinates (axisCount)
|
2021-12-23 05:43:48 +01:00
|
|
|
.sub_array (0, coords_length);
|
2018-12-22 00:46:51 +01:00
|
|
|
for (unsigned int i = 0; i < instanceCoords.length; i++)
|
2019-08-24 15:27:14 +02:00
|
|
|
coords[i] = instanceCoords.arrayZ[i].to_float ();
|
2018-11-19 20:27:19 +01:00
|
|
|
}
|
|
|
|
return axisCount;
|
|
|
|
}
|
|
|
|
|
2019-05-13 18:38:42 +02:00
|
|
|
void collect_name_ids (hb_set_t *nameids) const
|
|
|
|
{
|
|
|
|
if (!has_data ()) return;
|
|
|
|
|
|
|
|
+ get_axes ()
|
2020-06-30 08:18:36 +02:00
|
|
|
| hb_map (&AxisRecord::get_name_id)
|
2019-05-13 18:38:42 +02:00
|
|
|
| hb_sink (nameids)
|
|
|
|
;
|
|
|
|
|
|
|
|
+ hb_range ((unsigned) instanceCount)
|
2019-05-16 03:15:05 +02:00
|
|
|
| hb_map ([this] (const unsigned _) { return get_instance_subfamily_name_id (_); })
|
2019-05-13 18:38:42 +02:00
|
|
|
| hb_sink (nameids)
|
|
|
|
;
|
|
|
|
|
|
|
|
+ hb_range ((unsigned) instanceCount)
|
2019-05-16 03:15:05 +02:00
|
|
|
| hb_map ([this] (const unsigned _) { return get_instance_postscript_name_id (_); })
|
2019-05-13 18:38:42 +02:00
|
|
|
| hb_sink (nameids)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
2021-07-23 01:49:56 +02:00
|
|
|
public:
|
2018-12-17 19:01:01 +01:00
|
|
|
hb_array_t<const AxisRecord> get_axes () const
|
2018-11-19 19:04:43 +01:00
|
|
|
{ return hb_array (&(this+firstAxis), axisCount); }
|
2017-01-21 04:57:27 +01:00
|
|
|
|
2018-12-16 20:08:10 +01:00
|
|
|
const InstanceRecord *get_instance (unsigned int i) const
|
2018-11-19 20:27:19 +01:00
|
|
|
{
|
2018-11-22 07:18:55 +01:00
|
|
|
if (unlikely (i >= instanceCount)) return nullptr;
|
|
|
|
return &StructAtOffset<InstanceRecord> (&StructAfter<InstanceRecord> (get_axes ()),
|
|
|
|
i * instanceSize);
|
2018-11-19 20:27:19 +01:00
|
|
|
}
|
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 */
|
2021-03-31 20:49:14 +02:00
|
|
|
Offset16To<AxisRecord>
|
2018-11-19 18:53:53 +01:00
|
|
|
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
|
2019-09-10 19:31:07 +02:00
|
|
|
* to either axisCount * sizeof(HBFixed) + 4, or to
|
|
|
|
* axisCount * sizeof(HBFixed) + 6. */
|
2017-01-20 04:35:48 +01:00
|
|
|
|
|
|
|
public:
|
|
|
|
DEFINE_SIZE_STATIC (16);
|
|
|
|
};
|
|
|
|
|
|
|
|
} /* namespace OT */
|
|
|
|
|
|
|
|
|
|
|
|
#endif /* HB_OT_VAR_FVAR_TABLE_HH */
|