Skip to content

Commit

Permalink
Merge f72a404 into dfab7a2
Browse files Browse the repository at this point in the history
  • Loading branch information
ebraminio committed Mar 13, 2020
2 parents dfab7a2 + f72a404 commit 953b93a
Show file tree
Hide file tree
Showing 16 changed files with 475 additions and 16 deletions.
6 changes: 6 additions & 0 deletions docs/harfbuzz-sections.txt
Original file line number Diff line number Diff line change
Expand Up @@ -735,6 +735,12 @@ hb_shape_plan_set_user_data
hb_shape_plan_t
</SECTION>

<SECTION>
<FILE>hb-style</FILE>
hb_style_tag_t
hb_style_get_value
</SECTION>

<SECTION>
<FILE>hb-unicode</FILE>
HB_UNICODE_MAX
Expand Down
2 changes: 2 additions & 0 deletions src/Makefile.sources
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ HB_BASE_sources = \
hb-shaper.hh \
hb-static.cc \
hb-string-array.hh \
hb-style.cc \
hb-ucd-table.hh \
hb-ucd.cc \
hb-unicode-emoji-table.hh \
Expand Down Expand Up @@ -213,6 +214,7 @@ HB_BASE_headers = \
hb-set.h \
hb-shape-plan.h \
hb-shape.h \
hb-style.h \
hb-unicode.h \
hb-version.h \
hb.h \
Expand Down
1 change: 1 addition & 0 deletions src/harfbuzz.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
#include "hb-shape.cc"
#include "hb-shaper.cc"
#include "hb-static.cc"
#include "hb-style.cc"
#include "hb-ucd.cc"
#include "hb-unicode.cc"
#include "hb-glib.cc"
Expand Down
4 changes: 2 additions & 2 deletions src/hb-aat-fdsc-table.hh
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ struct FontDescriptor
protected:
Tag tag; /* The 4-byte table tag name. */
union {
HBFixed value; /* The value for the descriptor tag. */
HBFixed value; /* The value for the descriptor tag. */
HBUINT32 nalfType; /* If the tag is `nalf`, see non_alphabetic_value_t */
} u;
public:
Expand Down Expand Up @@ -108,7 +108,7 @@ struct fdsc
}

protected:
HBFixed version; /* Version number of the font descriptors
HBFixed version; /* Version number of the font descriptors
* table (0x00010000 for the current version). */
LArrayOf<FontDescriptor>
descriptors; /* List of tagged-coordinate pairs style descriptors
Expand Down
2 changes: 1 addition & 1 deletion src/hb-config.hh
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
#define HB_NO_SETLOCALE
#define HB_NO_OT_FONT_GLYPH_NAMES
#define HB_NO_OT_SHAPE_FRACTIONS
#define HB_NO_STAT
#define HB_NO_STYLE
#define HB_NO_SUBSET_LAYOUT
#define HB_NO_VAR
#endif
Expand Down
5 changes: 3 additions & 2 deletions src/hb-ot-face-table-list.hh
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,14 @@ HB_OT_ACCELERATOR (OT, cmap)
HB_OT_TABLE (OT, hhea)
HB_OT_ACCELERATOR (OT, hmtx)
HB_OT_TABLE (OT, OS2)
#if !defined(HB_NO_OT_FONT_GLYPH_NAMES) || !defined(HB_NO_METRICS)
#if !defined(HB_NO_OT_FONT_GLYPH_NAMES) || !defined(HB_NO_METRICS) || !defined(HB_NO_STYLE)
HB_OT_ACCELERATOR (OT, post)
#endif
#ifndef HB_NO_NAME
HB_OT_ACCELERATOR (OT, name)
#endif
#ifndef HB_NO_STAT
#ifndef HB_NO_STYLE
HB_OT_TABLE (AAT, fdsc)
HB_OT_TABLE (OT, STAT)
#endif
#ifndef HB_NO_META
Expand Down
87 changes: 80 additions & 7 deletions src/hb-ot-stat-table.hh
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ enum

struct AxisValueFormat1
{
unsigned int get_axis_index () const { return axisIndex; }
float get_value () const { return value.to_float (); }

hb_ot_name_id_t get_value_name_id () const { return valueNameID; }

bool sanitize (hb_sanitize_context_t *c) const
Expand All @@ -77,13 +80,16 @@ struct AxisValueFormat1
NameID valueNameID; /* The name ID for entries in the 'name' table
* that provide a display string for this
* attribute value. */
HBFixed value; /* A numeric value for this attribute value. */
HBFixed value; /* A numeric value for this attribute value. */
public:
DEFINE_SIZE_STATIC (12);
};

struct AxisValueFormat2
{
unsigned int get_axis_index () const { return axisIndex; }
float get_value () const { return nominalValue.to_float (); }

hb_ot_name_id_t get_value_name_id () const { return valueNameID; }

bool sanitize (hb_sanitize_context_t *c) const
Expand All @@ -102,17 +108,20 @@ struct AxisValueFormat2
NameID valueNameID; /* The name ID for entries in the 'name' table
* that provide a display string for this
* attribute value. */
HBFixed nominalValue; /* A numeric value for this attribute value. */
HBFixed rangeMinValue; /* The minimum value for a range associated
HBFixed nominalValue; /* A numeric value for this attribute value. */
HBFixed rangeMinValue; /* The minimum value for a range associated
* with the specified name ID. */
HBFixed rangeMaxValue; /* The maximum value for a range associated
HBFixed rangeMaxValue; /* The maximum value for a range associated
* with the specified name ID. */
public:
DEFINE_SIZE_STATIC (20);
};

struct AxisValueFormat3
{
unsigned int get_axis_index () const { return axisIndex; }
float get_value () const { return value.to_float (); }

hb_ot_name_id_t get_value_name_id () const { return valueNameID; }

bool sanitize (hb_sanitize_context_t *c) const
Expand All @@ -131,15 +140,18 @@ struct AxisValueFormat3
NameID valueNameID; /* The name ID for entries in the 'name' table
* that provide a display string for this
* attribute value. */
HBFixed value; /* A numeric value for this attribute value. */
HBFixed linkedValue; /* The numeric value for a style-linked mapping
HBFixed value; /* A numeric value for this attribute value. */
HBFixed linkedValue; /* The numeric value for a style-linked mapping
* from this value. */
public:
DEFINE_SIZE_STATIC (16);
};

struct AxisValueRecord
{
unsigned int get_axis_index () const { return axisIndex; }
float get_value () const { return value.to_float (); }

bool sanitize (hb_sanitize_context_t *c) const
{
TRACE_SANITIZE (this);
Expand All @@ -150,13 +162,16 @@ struct AxisValueRecord
HBUINT16 axisIndex; /* Zero-base index into the axis record array
* identifying the axis to which this value
* applies. Must be less than designAxisCount. */
HBFixed value; /* A numeric value for this attribute value. */
HBFixed value; /* A numeric value for this attribute value. */
public:
DEFINE_SIZE_STATIC (6);
};

struct AxisValueFormat4
{
const AxisValueRecord &get_axis_record (unsigned int axis_index) const
{ return axisValues.as_array (axisCount)[axis_index]; }

hb_ot_name_id_t get_value_name_id () const { return valueNameID; }

bool sanitize (hb_sanitize_context_t *c) const
Expand All @@ -183,6 +198,30 @@ struct AxisValueFormat4

struct AxisValue
{
bool get_value (unsigned int axis_index) const
{
switch (u.format)
{
case 1: return u.format1.get_value ();
case 2: return u.format2.get_value ();
case 3: return u.format3.get_value ();
case 4: return u.format4.get_axis_record (axis_index).get_value ();
default:return 0;
}
}

unsigned int get_axis_index () const
{
switch (u.format)
{
case 1: return u.format1.get_axis_index ();
case 2: return u.format2.get_axis_index ();
case 3: return u.format3.get_axis_index ();
/* case 4: Makes more sense for variable fonts which are handled by fvar in hb-style */
default:return -1;
}
}

hb_ot_name_id_t get_value_name_id () const
{
switch (u.format)
Expand Down Expand Up @@ -226,6 +265,8 @@ struct AxisValue

struct StatAxisRecord
{
int cmp (hb_tag_t key) const { return tag.cmp (key); }

hb_ot_name_id_t get_name_id () const { return nameID; }

bool sanitize (hb_sanitize_context_t *c) const
Expand All @@ -251,6 +292,38 @@ struct STAT

bool has_data () const { return version.to_int (); }

bool find_axis_index (hb_tag_t tag, unsigned int *axis_index) const
{
hb_array_t<const StatAxisRecord> axes = get_design_axes ();
/* TODO: add lfind in hb_array_t and use it in here and fvar's find_axis_info */
for (unsigned int i = 0; i < axes.length; i++)
if (!axes[i].cmp (tag))
{
*axis_index = i;
return true;
}
return false;
}

bool get_value (hb_tag_t tag, float *value) const
{
unsigned int axis_index;
if (!find_axis_index (tag, &axis_index)) return false;

hb_array_t<const OffsetTo<AxisValue>> axis_values = get_axis_value_offsets ();
for (unsigned int i = 0; i < axis_values.length; i++)
{
const AxisValue& axis_value = (this + axis_values[i]);
if (axis_value.get_axis_index () == axis_index)
{
if (value)
*value = axis_value.get_value (axis_index);
return true;
}
}
return false;
}

unsigned get_design_axis_count () const { return designAxisCount; }

hb_ot_name_id_t get_axis_record_name_id (unsigned axis_record_index) const
Expand Down
6 changes: 3 additions & 3 deletions src/hb-ot-var-fvar-table.hh
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ struct AxisRecord

public:
Tag axisTag; /* Tag identifying the design variation for the axis. */
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. */
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. */
HBUINT16 flags; /* Axis flags. */
NameID axisNameID; /* The name ID for entries in the 'name' table that
* provide a display name for this axis. */
Expand Down
113 changes: 113 additions & 0 deletions src/hb-style.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*
* Copyright © 2019 Ebrahim Byagowi
*
* 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.
*/

#include "hb.hh"

#ifndef HB_NO_STYLE

#include "hb-aat-fdsc-table.hh"
#include "hb-ot-var-avar-table.hh"
#include "hb-ot-var-fvar-table.hh"
#include "hb-ot-stat-table.hh"
#include "hb-ot-os2-table.hh"
#include "hb-ot-head-table.hh"
#include "hb-ot-post-table.hh"
#include "hb-ot-face.hh"

/**
* hb_style_get_value:
* @font: a #hb_font_t object.
* @style_tag: a style tag.
*
* Searches variation axes of a hb_font_t object for a specific axis first,
* if not set, then tries to get default style values from different
* tables of the font.
*
* Returns: Corresponding axis or default value to a style tag.
*
* Since: REPLACEME
**/
float
hb_style_get_value (hb_font_t *font, hb_style_tag_t style_tag)
{
hb_face_t *face = font->face;

#ifndef HB_NO_VAR
hb_ot_var_axis_info_t axis;
if (hb_ot_var_find_axis_info (face, style_tag, &axis))
{
if (axis.axis_index < font->num_coords) return font->design_coords[axis.axis_index];
/* If a face is variable, fvar's default_value is better than STAT records */
return axis.default_value;
}
#endif

if (style_tag == HB_STYLE_TAG_OPTICAL_SIZE && font->ptem)
return font->ptem;

/* STAT */
float value;
if (face->table.STAT->get_value (style_tag, &value))
return value;

/* Check Apple's fdsc as OS2 table is optional in AAT */
const AAT::FontDescriptor &descriptor = face->table.fdsc->get_descriptor (style_tag);
if (descriptor.has_data ())
{
float value = descriptor.get_value ();
/* https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6fdsc.html */
/* Percent weight relative to regular weight. */
if (style_tag == HB_STYLE_TAG_WEIGHT) value *= 400.f;
/* Percent width relative to regular width. */
if (style_tag == HB_STYLE_TAG_WIDTH) value *= 100.f;
return value;
}

switch ((unsigned) style_tag)
{
case HB_STYLE_TAG_ITALIC:
return face->table.OS2->is_italic () || face->table.head->is_italic () ? 1 : 0;
case HB_STYLE_TAG_OPTICAL_SIZE:
{
unsigned int lower, upper;
return face->table.OS2->v5 ().get_optical_size (&lower, &upper)
? (float) (lower + upper) / 2.f
: 12.f;
}
case HB_STYLE_TAG_SLANT:
return face->table.post->table->italicAngle.to_float ();
case HB_STYLE_TAG_WIDTH:
return face->table.OS2->has_data ()
? face->table.OS2->get_width ()
: (face->table.head->is_condensed () ? 75 : 100);
case HB_STYLE_TAG_WEIGHT:
return face->table.OS2->has_data ()
? face->table.OS2->usWeightClass
: (face->table.head->is_bold () ? 700 : 400);
default:
return 0;
}
}

#endif
Loading

0 comments on commit 953b93a

Please sign in to comment.