Blame src/hb-ot-var-fvar-table.hh

Packit 874993
/*
Packit 874993
 * Copyright © 2017  Google, Inc.
Packit 874993
 *
Packit 874993
 *  This is part of HarfBuzz, a text shaping library.
Packit 874993
 *
Packit 874993
 * Permission is hereby granted, without written agreement and without
Packit 874993
 * license or royalty fees, to use, copy, modify, and distribute this
Packit 874993
 * software and its documentation for any purpose, provided that the
Packit 874993
 * above copyright notice and the following two paragraphs appear in
Packit 874993
 * all copies of this software.
Packit 874993
 *
Packit 874993
 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
Packit 874993
 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
Packit 874993
 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
Packit 874993
 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
Packit 874993
 * DAMAGE.
Packit 874993
 *
Packit 874993
 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
Packit 874993
 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
Packit 874993
 * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
Packit 874993
 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
Packit 874993
 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
Packit 874993
 *
Packit 874993
 * Google Author(s): Behdad Esfahbod
Packit 874993
 */
Packit 874993
Packit 874993
#ifndef HB_OT_VAR_FVAR_TABLE_HH
Packit 874993
#define HB_OT_VAR_FVAR_TABLE_HH
Packit 874993
Packit 874993
#include "hb-open-type-private.hh"
Packit 874993
Packit 874993
namespace OT {
Packit 874993
Packit 874993
Packit 874993
struct InstanceRecord
Packit 874993
{
Packit 874993
  inline bool sanitize (hb_sanitize_context_t *c, unsigned int axis_count) const
Packit 874993
  {
Packit 874993
    TRACE_SANITIZE (this);
Packit 874993
    return_trace (c->check_struct (this) &&
Packit 874993
		  c->check_array (coordinates, coordinates[0].static_size, axis_count));
Packit 874993
  }
Packit 874993
Packit 874993
  protected:
Packit 874993
  USHORT	subfamilyNameID;/* The name ID for entries in the 'name' table
Packit 874993
				 * that provide subfamily names for this instance. */
Packit 874993
  USHORT	reserved;	/* Reserved for future use — set to 0. */
Packit 874993
  Fixed		coordinates[VAR];/* The coordinates array for this instance. */
Packit 874993
  //USHORT	postScriptNameIDX;/*Optional. The name ID for entries in the 'name'
Packit 874993
  //				  * table that provide PostScript names for this
Packit 874993
  //				  * instance. */
Packit 874993
Packit 874993
  public:
Packit 874993
  DEFINE_SIZE_ARRAY (4, coordinates);
Packit 874993
};
Packit 874993
Packit 874993
struct AxisRecord
Packit 874993
{
Packit 874993
  inline bool sanitize (hb_sanitize_context_t *c) const
Packit 874993
  {
Packit 874993
    TRACE_SANITIZE (this);
Packit 874993
    return_trace (c->check_struct (this));
Packit 874993
  }
Packit 874993
Packit 874993
  public:
Packit 874993
  Tag		axisTag;	/* Tag identifying the design variation for the axis. */
Packit 874993
  Fixed		minValue;	/* The minimum coordinate value for the axis. */
Packit 874993
  Fixed		defaultValue;	/* The default coordinate value for the axis. */
Packit 874993
  Fixed		maxValue;	/* The maximum coordinate value for the axis. */
Packit 874993
  USHORT	reserved;	/* Reserved for future use — set to 0. */
Packit 874993
  USHORT	axisNameID;	/* The name ID for entries in the 'name' table that
Packit 874993
				 * provide a display name for this axis. */
Packit 874993
Packit 874993
  public:
Packit 874993
  DEFINE_SIZE_STATIC (20);
Packit 874993
};
Packit 874993
Packit 874993
Packit 874993
/*
Packit 874993
 * fvar — Font Variations Table
Packit 874993
 */
Packit 874993
Packit 874993
#define HB_OT_TAG_fvar HB_TAG('f','v','a','r')
Packit 874993
Packit 874993
struct fvar
Packit 874993
{
Packit 874993
  static const hb_tag_t tableTag	= HB_OT_TAG_fvar;
Packit 874993
Packit 874993
  inline bool sanitize (hb_sanitize_context_t *c) const
Packit 874993
  {
Packit 874993
    TRACE_SANITIZE (this);
Packit 874993
    return_trace (version.sanitize (c) &&
Packit 874993
		  likely (version.major == 1) &&
Packit 874993
		  c->check_struct (this) &&
Packit 874993
		  instanceSize >= axisCount * 4 + 4 &&
Packit 874993
		  axisSize <= 1024 && /* Arbitrary, just to simplify overflow checks. */
Packit 874993
		  instanceSize <= 1024 && /* Arbitrary, just to simplify overflow checks. */
Packit 874993
		  c->check_range (this, things) &&
Packit 874993
		  c->check_range (&StructAtOffset<char> (this, things),
Packit 874993
				  axisCount * axisSize + instanceCount * instanceSize));
Packit 874993
  }
Packit 874993
Packit 874993
  inline unsigned int get_axis_count (void) const
Packit 874993
  { return axisCount; }
Packit 874993
Packit 874993
  inline bool get_axis (unsigned int index, hb_ot_var_axis_t *info) const
Packit 874993
  {
Packit 874993
    if (unlikely (index >= axisCount))
Packit 874993
      return false;
Packit 874993
Packit 874993
    if (info)
Packit 874993
    {
Packit 874993
      const AxisRecord &axis = get_axes ()[index];
Packit 874993
      info->tag = axis.axisTag;
Packit 874993
      info->name_id =  axis.axisNameID;
Packit 874993
      info->default_value = axis.defaultValue / 65536.;
Packit 874993
      /* Ensure order, to simplify client math. */
Packit 874993
      info->min_value = MIN<float> (info->default_value, axis.minValue / 65536.);
Packit 874993
      info->max_value = MAX<float> (info->default_value, axis.maxValue / 65536.);
Packit 874993
    }
Packit 874993
Packit 874993
    return true;
Packit 874993
  }
Packit 874993
Packit 874993
  inline unsigned int get_axis_infos (unsigned int      start_offset,
Packit 874993
				      unsigned int     *axes_count /* IN/OUT */,
Packit 874993
				      hb_ot_var_axis_t *axes_array /* OUT */) const
Packit 874993
  {
Packit 874993
    if (axes_count)
Packit 874993
    {
Packit 874993
      unsigned int count = axisCount;
Packit 874993
      start_offset = MIN (start_offset, count);
Packit 874993
Packit 874993
      count -= start_offset;
Packit 874993
      axes_array += start_offset;
Packit 874993
Packit 874993
      count = MIN (count, *axes_count);
Packit 874993
      *axes_count = count;
Packit 874993
Packit 874993
      for (unsigned int i = 0; i < count; i++)
Packit 874993
	get_axis (start_offset + i, axes_array + i);
Packit 874993
    }
Packit 874993
    return axisCount;
Packit 874993
  }
Packit 874993
Packit 874993
  inline bool find_axis (hb_tag_t tag, unsigned int *index, hb_ot_var_axis_t *info) const
Packit 874993
  {
Packit 874993
    const AxisRecord *axes = get_axes ();
Packit 874993
    unsigned int count = get_axis_count ();
Packit 874993
    for (unsigned int i = 0; i < count; i++)
Packit 874993
      if (axes[i].axisTag == tag)
Packit 874993
      {
Packit 874993
        if (index)
Packit 874993
	  *index = i;
Packit 874993
	return get_axis (i, info);
Packit 874993
      }
Packit 874993
    if (index)
Packit 874993
      *index = HB_OT_VAR_NO_AXIS_INDEX;
Packit 874993
    return false;
Packit 874993
  }
Packit 874993
Packit 874993
  inline int normalize_axis_value (unsigned int axis_index, float v) const
Packit 874993
  {
Packit 874993
    hb_ot_var_axis_t axis;
Packit 874993
    if (!get_axis (axis_index, &axis))
Packit 874993
      return 0;
Packit 874993
Packit 874993
    v = MAX (MIN (v, axis.max_value), axis.min_value); /* Clamp. */
Packit 874993
Packit 874993
    if (v == axis.default_value)
Packit 874993
      return 0;
Packit 874993
    else if (v < axis.default_value)
Packit 874993
      v = (v - axis.default_value) / (axis.default_value - axis.min_value);
Packit 874993
    else
Packit 874993
      v = (v - axis.default_value) / (axis.max_value - axis.default_value);
Packit 874993
    return (int) (v * 16384. + (v >= 0. ? .5 : -.5));
Packit 874993
  }
Packit 874993
Packit 874993
  protected:
Packit 874993
  inline const AxisRecord * get_axes (void) const
Packit 874993
  { return &StructAtOffset<AxisRecord> (this, things); }
Packit 874993
Packit 874993
  inline const InstanceRecord * get_instances (void) const
Packit 874993
  { return &StructAtOffset<InstanceRecord> (get_axes () + axisCount, 0); }
Packit 874993
Packit 874993
  protected:
Packit 874993
  FixedVersion<>version;	/* Version of the fvar table
Packit 874993
				 * initially set to 0x00010000u */
Packit 874993
  Offset<>	things;		/* Offset in bytes from the beginning of the table
Packit 874993
				 * to the start of the AxisRecord array. */
Packit 874993
  USHORT	reserved;	/* This field is permanently reserved. Set to 2. */
Packit 874993
  USHORT	axisCount;	/* The number of variation axes in the font (the
Packit 874993
				 * number of records in the axes array). */
Packit 874993
  USHORT	axisSize;	/* The size in bytes of each VariationAxisRecord —
Packit 874993
				 * set to 20 (0x0014) for this version. */
Packit 874993
  USHORT	instanceCount;	/* The number of named instances defined in the font
Packit 874993
				 * (the number of records in the instances array). */
Packit 874993
  USHORT	instanceSize;	/* The size in bytes of each InstanceRecord — set
Packit 874993
				 * to either axisCount * sizeof(Fixed) + 4, or to
Packit 874993
				 * axisCount * sizeof(Fixed) + 6. */
Packit 874993
Packit 874993
  public:
Packit 874993
  DEFINE_SIZE_STATIC (16);
Packit 874993
};
Packit 874993
Packit 874993
} /* namespace OT */
Packit 874993
Packit 874993
Packit 874993
#endif /* HB_OT_VAR_FVAR_TABLE_HH */