2017-11-02 00:31:29 +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_KERN_TABLE_HH
|
|
|
|
#define HB_OT_KERN_TABLE_HH
|
|
|
|
|
2018-08-26 07:36:36 +02:00
|
|
|
#include "hb-open-type.hh"
|
2018-10-09 05:09:48 +02:00
|
|
|
#include "hb-ot-shape.hh"
|
|
|
|
#include "hb-ot-layout-gsubgpos.hh"
|
2018-11-02 18:23:54 +01:00
|
|
|
#include "hb-aat-layout-common.hh"
|
2018-10-09 05:09:48 +02:00
|
|
|
|
|
|
|
|
|
|
|
template <typename Driver>
|
|
|
|
struct hb_kern_machine_t
|
|
|
|
{
|
2018-10-09 05:26:26 +02:00
|
|
|
hb_kern_machine_t (const Driver &driver_) : driver (driver_) {}
|
2018-10-09 05:09:48 +02:00
|
|
|
|
2018-10-15 00:20:50 +02:00
|
|
|
HB_NO_SANITIZE_SIGNED_INTEGER_OVERFLOW
|
2018-10-16 22:24:29 +02:00
|
|
|
inline void kern (hb_font_t *font,
|
|
|
|
hb_buffer_t *buffer,
|
|
|
|
hb_mask_t kern_mask,
|
|
|
|
bool scale = true) const
|
2018-10-09 05:09:48 +02:00
|
|
|
{
|
|
|
|
OT::hb_ot_apply_context_t c (1, font, buffer);
|
|
|
|
c.set_lookup_mask (kern_mask);
|
|
|
|
c.set_lookup_props (OT::LookupFlag::IgnoreMarks);
|
|
|
|
OT::hb_ot_apply_context_t::skipping_iterator_t &skippy_iter = c.iter_input;
|
|
|
|
skippy_iter.init (&c);
|
|
|
|
|
|
|
|
bool horizontal = HB_DIRECTION_IS_HORIZONTAL (buffer->props.direction);
|
|
|
|
unsigned int count = buffer->len;
|
|
|
|
hb_glyph_info_t *info = buffer->info;
|
|
|
|
hb_glyph_position_t *pos = buffer->pos;
|
|
|
|
for (unsigned int idx = 0; idx < count;)
|
|
|
|
{
|
|
|
|
if (!(info[idx].mask & kern_mask))
|
|
|
|
{
|
|
|
|
idx++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
skippy_iter.reset (idx, 1);
|
|
|
|
if (!skippy_iter.next ())
|
|
|
|
{
|
|
|
|
idx++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int i = idx;
|
|
|
|
unsigned int j = skippy_iter.idx;
|
2018-10-09 06:22:08 +02:00
|
|
|
|
2018-10-09 05:09:48 +02:00
|
|
|
hb_position_t kern = driver.get_kerning (info[i].codepoint,
|
|
|
|
info[j].codepoint);
|
|
|
|
|
2018-10-09 06:22:08 +02:00
|
|
|
|
|
|
|
if (likely (!kern))
|
|
|
|
goto skip;
|
|
|
|
|
2018-10-09 05:09:48 +02:00
|
|
|
|
|
|
|
if (horizontal)
|
|
|
|
{
|
2018-10-16 22:24:29 +02:00
|
|
|
if (scale)
|
|
|
|
kern = font->em_scale_x (kern);
|
|
|
|
hb_position_t kern1 = kern >> 1;
|
|
|
|
hb_position_t kern2 = kern - kern1;
|
2018-10-09 05:09:48 +02:00
|
|
|
pos[i].x_advance += kern1;
|
|
|
|
pos[j].x_advance += kern2;
|
|
|
|
pos[j].x_offset += kern2;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-10-16 22:24:29 +02:00
|
|
|
if (scale)
|
|
|
|
kern = font->em_scale_y (kern);
|
|
|
|
hb_position_t kern1 = kern >> 1;
|
|
|
|
hb_position_t kern2 = kern - kern1;
|
2018-10-09 05:09:48 +02:00
|
|
|
pos[i].y_advance += kern1;
|
|
|
|
pos[j].y_advance += kern2;
|
|
|
|
pos[j].y_offset += kern2;
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer->unsafe_to_break (i, j + 1);
|
|
|
|
|
2018-10-09 06:22:08 +02:00
|
|
|
skip:
|
2018-10-09 05:09:48 +02:00
|
|
|
idx = skippy_iter.idx;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-09 05:26:26 +02:00
|
|
|
const Driver &driver;
|
2018-10-09 05:09:48 +02:00
|
|
|
};
|
|
|
|
|
2017-11-02 00:31:29 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* kern -- Kerning
|
2018-04-12 11:08:19 +02:00
|
|
|
* https://docs.microsoft.com/en-us/typography/opentype/spec/kern
|
|
|
|
* https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6kern.html
|
2017-11-02 00:31:29 +01:00
|
|
|
*/
|
|
|
|
#define HB_OT_TAG_kern HB_TAG('k','e','r','n')
|
|
|
|
|
2018-04-12 11:08:19 +02:00
|
|
|
|
|
|
|
namespace OT {
|
|
|
|
|
|
|
|
|
2017-11-02 01:12:26 +01:00
|
|
|
struct hb_glyph_pair_t
|
|
|
|
{
|
|
|
|
hb_codepoint_t left;
|
|
|
|
hb_codepoint_t right;
|
|
|
|
};
|
2017-11-02 00:31:29 +01:00
|
|
|
|
2017-11-02 01:12:26 +01:00
|
|
|
struct KernPair
|
2017-11-02 00:54:56 +01:00
|
|
|
{
|
2017-11-02 01:12:26 +01:00
|
|
|
inline int get_kerning (void) const
|
|
|
|
{ return value; }
|
|
|
|
|
|
|
|
inline int cmp (const hb_glyph_pair_t &o) const
|
2017-11-02 00:54:56 +01:00
|
|
|
{
|
2017-11-02 01:12:26 +01:00
|
|
|
int ret = left.cmp (o.left);
|
|
|
|
if (ret) return ret;
|
|
|
|
return right.cmp (o.right);
|
2017-11-02 00:54:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
inline bool sanitize (hb_sanitize_context_t *c) const
|
|
|
|
{
|
|
|
|
TRACE_SANITIZE (this);
|
2017-11-02 01:12:26 +01:00
|
|
|
return_trace (c->check_struct (this));
|
|
|
|
}
|
2017-11-02 00:54:56 +01:00
|
|
|
|
2017-11-02 01:12:26 +01:00
|
|
|
protected:
|
|
|
|
GlyphID left;
|
|
|
|
GlyphID right;
|
|
|
|
FWORD value;
|
|
|
|
public:
|
|
|
|
DEFINE_SIZE_STATIC (6);
|
|
|
|
};
|
2017-11-02 00:54:56 +01:00
|
|
|
|
2018-11-06 18:07:15 +01:00
|
|
|
template <typename KernSubTableHeader>
|
2017-11-02 01:12:26 +01:00
|
|
|
struct KernSubTableFormat0
|
|
|
|
{
|
|
|
|
inline int get_kerning (hb_codepoint_t left, hb_codepoint_t right) const
|
|
|
|
{
|
|
|
|
hb_glyph_pair_t pair = {left, right};
|
|
|
|
int i = pairs.bsearch (pair);
|
|
|
|
if (i == -1)
|
|
|
|
return 0;
|
|
|
|
return pairs[i].get_kerning ();
|
2017-11-02 00:54:56 +01:00
|
|
|
}
|
2017-11-02 01:12:26 +01:00
|
|
|
|
2018-11-02 18:23:54 +01:00
|
|
|
inline bool apply (AAT::hb_aat_apply_context_t *c) const
|
|
|
|
{
|
|
|
|
TRACE_APPLY (this);
|
|
|
|
|
|
|
|
if (!c->plan->requested_kerning)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
hb_kern_machine_t<KernSubTableFormat0> machine (*this);
|
|
|
|
machine.kern (c->font, c->buffer, c->plan->kern_mask);
|
|
|
|
|
|
|
|
return_trace (true);
|
|
|
|
}
|
|
|
|
|
2017-11-02 01:12:26 +01:00
|
|
|
inline bool sanitize (hb_sanitize_context_t *c) const
|
|
|
|
{
|
|
|
|
TRACE_SANITIZE (this);
|
|
|
|
return_trace (pairs.sanitize (c));
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2018-11-06 18:07:15 +01:00
|
|
|
KernSubTableHeader header;
|
|
|
|
BinSearchArrayOf<KernPair> pairs; /* Array of kerning pairs. */
|
2017-11-02 01:12:26 +01:00
|
|
|
public:
|
2018-11-06 18:07:15 +01:00
|
|
|
DEFINE_SIZE_ARRAY (KernSubTableHeader::static_size + 8, pairs);
|
2017-11-02 00:54:56 +01:00
|
|
|
};
|
|
|
|
|
2018-11-06 18:07:15 +01:00
|
|
|
template <typename KernSubTableHeader>
|
2018-11-02 19:47:42 +01:00
|
|
|
struct KernSubTableFormat1
|
|
|
|
{
|
|
|
|
typedef void EntryData;
|
|
|
|
|
|
|
|
struct driver_context_t
|
|
|
|
{
|
|
|
|
static const bool in_place = true;
|
|
|
|
enum Flags
|
|
|
|
{
|
|
|
|
Push = 0x8000, /* If set, push this glyph on the kerning stack. */
|
|
|
|
DontAdvance = 0x4000, /* If set, don't advance to the next glyph
|
|
|
|
* before going to the new state. */
|
|
|
|
Offset = 0x3FFF, /* Byte offset from beginning of subtable to the
|
|
|
|
* value table for the glyphs on the kerning stack. */
|
|
|
|
};
|
|
|
|
|
|
|
|
inline driver_context_t (const KernSubTableFormat1 *table_,
|
|
|
|
AAT::hb_aat_apply_context_t *c_) :
|
|
|
|
c (c_),
|
|
|
|
table (table_),
|
|
|
|
/* Apparently the offset kernAction is from the beginning of the state-machine,
|
|
|
|
* similar to offsets in morx table, NOT from beginning of this table, like
|
|
|
|
* other subtables in kerx. Discovered via testing. */
|
|
|
|
kernAction (&table->machine + table->kernAction),
|
|
|
|
depth (0) {}
|
|
|
|
|
|
|
|
inline bool is_actionable (AAT::StateTableDriver<AAT::MortTypes, EntryData> *driver HB_UNUSED,
|
|
|
|
const AAT::Entry<EntryData> *entry)
|
|
|
|
{
|
|
|
|
return entry->flags & Offset;
|
|
|
|
}
|
|
|
|
inline bool transition (AAT::StateTableDriver<AAT::MortTypes, EntryData> *driver,
|
|
|
|
const AAT::Entry<EntryData> *entry)
|
|
|
|
{
|
|
|
|
hb_buffer_t *buffer = driver->buffer;
|
|
|
|
unsigned int flags = entry->flags;
|
|
|
|
|
|
|
|
if (flags & Push)
|
|
|
|
{
|
|
|
|
if (likely (depth < ARRAY_LENGTH (stack)))
|
|
|
|
stack[depth++] = buffer->idx;
|
|
|
|
else
|
|
|
|
depth = 0; /* Probably not what CoreText does, but better? */
|
|
|
|
}
|
|
|
|
|
|
|
|
if (entry->flags & Offset)
|
|
|
|
{
|
|
|
|
unsigned int kernIndex = AAT::MortTypes::offsetToIndex (entry->flags & Offset, &table->machine, kernAction.arrayZ);
|
|
|
|
const FWORD *actions = &kernAction[kernIndex];
|
|
|
|
if (!c->sanitizer.check_array (actions, depth))
|
|
|
|
{
|
|
|
|
depth = 0;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
hb_mask_t kern_mask = c->plan->kern_mask;
|
|
|
|
for (unsigned int i = 0; i < depth; i++)
|
|
|
|
{
|
|
|
|
/* Apparently, when spec says "Each pops one glyph from the kerning stack
|
|
|
|
* and applies the kerning value to it.", it doesn't mean it in that order.
|
|
|
|
* The deepest item in the stack corresponds to the first item in the action
|
|
|
|
* list. Discovered by testing. */
|
|
|
|
unsigned int idx = stack[i];
|
|
|
|
int v = *actions++;
|
|
|
|
if (idx < buffer->len && buffer->info[idx].mask & kern_mask)
|
|
|
|
{
|
|
|
|
if (HB_DIRECTION_IS_HORIZONTAL (buffer->props.direction))
|
|
|
|
{
|
|
|
|
buffer->pos[idx].x_advance += c->font->em_scale_x (v);
|
|
|
|
if (HB_DIRECTION_IS_BACKWARD (buffer->props.direction))
|
|
|
|
buffer->pos[idx].x_offset += c->font->em_scale_x (v);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
buffer->pos[idx].y_advance += c->font->em_scale_y (v);
|
|
|
|
if (HB_DIRECTION_IS_BACKWARD (buffer->props.direction))
|
|
|
|
buffer->pos[idx].y_offset += c->font->em_scale_y (v);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
depth = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
AAT::hb_aat_apply_context_t *c;
|
|
|
|
const KernSubTableFormat1 *table;
|
|
|
|
const UnsizedArrayOf<FWORD> &kernAction;
|
|
|
|
unsigned int stack[8];
|
|
|
|
unsigned int depth;
|
|
|
|
};
|
|
|
|
|
|
|
|
inline bool apply (AAT::hb_aat_apply_context_t *c) const
|
|
|
|
{
|
|
|
|
TRACE_APPLY (this);
|
|
|
|
|
|
|
|
if (!c->plan->requested_kerning)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
driver_context_t dc (this, c);
|
|
|
|
|
|
|
|
AAT::StateTableDriver<AAT::MortTypes, EntryData> driver (machine, c->buffer, c->font->face);
|
|
|
|
driver.drive (&dc);
|
|
|
|
|
|
|
|
return_trace (true);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool sanitize (hb_sanitize_context_t *c) const
|
|
|
|
{
|
|
|
|
TRACE_SANITIZE (this);
|
|
|
|
/* The rest of array sanitizations are done at run-time. */
|
|
|
|
return_trace (likely (c->check_struct (this) &&
|
|
|
|
machine.sanitize (c)));
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2018-11-06 18:07:15 +01:00
|
|
|
KernSubTableHeader header;
|
2018-11-02 19:47:42 +01:00
|
|
|
AAT::StateTable<AAT::MortTypes, EntryData> machine;
|
|
|
|
OffsetTo<UnsizedArrayOf<FWORD>, HBUINT16, false> kernAction;
|
|
|
|
public:
|
2018-11-06 18:07:15 +01:00
|
|
|
DEFINE_SIZE_STATIC (KernSubTableHeader::static_size + 10);
|
2018-11-02 19:47:42 +01:00
|
|
|
};
|
|
|
|
|
2017-11-04 17:01:11 +01:00
|
|
|
struct KernClassTable
|
2017-11-02 00:54:56 +01:00
|
|
|
{
|
2017-11-04 17:01:11 +01:00
|
|
|
inline unsigned int get_class (hb_codepoint_t g) const { return classes[g - firstGlyph]; }
|
|
|
|
|
|
|
|
inline bool sanitize (hb_sanitize_context_t *c) const
|
2017-11-02 02:33:09 +01:00
|
|
|
{
|
2017-11-04 17:01:11 +01:00
|
|
|
TRACE_SANITIZE (this);
|
2018-11-02 18:57:41 +01:00
|
|
|
return_trace (c->check_struct (this) &&
|
|
|
|
classes.sanitize (c));
|
2017-11-02 02:33:09 +01:00
|
|
|
}
|
|
|
|
|
2017-11-04 17:01:11 +01:00
|
|
|
protected:
|
2018-01-10 03:07:30 +01:00
|
|
|
HBUINT16 firstGlyph; /* First glyph in class range. */
|
|
|
|
ArrayOf<HBUINT16> classes; /* Glyph classes. */
|
2017-11-04 17:01:11 +01:00
|
|
|
public:
|
|
|
|
DEFINE_SIZE_ARRAY (4, classes);
|
|
|
|
};
|
|
|
|
|
2018-11-06 18:07:15 +01:00
|
|
|
template <typename KernSubTableHeader>
|
2017-11-04 17:01:11 +01:00
|
|
|
struct KernSubTableFormat2
|
|
|
|
{
|
2018-11-02 18:23:54 +01:00
|
|
|
inline int get_kerning (hb_codepoint_t left, hb_codepoint_t right,
|
|
|
|
AAT::hb_aat_apply_context_t *c) const
|
2017-11-02 00:54:56 +01:00
|
|
|
{
|
2018-10-11 00:14:41 +02:00
|
|
|
/* This subtable is disabled. It's not cleaer to me *exactly* where the offests are
|
|
|
|
* based from. I *think* they should be based from beginning of kern subtable wrapper,
|
|
|
|
* *NOT* "this". Since we know of no fonts that use this subtable, we are disabling
|
2018-11-02 18:26:15 +01:00
|
|
|
* it. Someday fix it and re-enable. */
|
2018-10-11 00:14:41 +02:00
|
|
|
return 0;
|
2017-11-04 17:01:11 +01:00
|
|
|
unsigned int l = (this+leftClassTable).get_class (left);
|
2018-01-26 19:38:06 +01:00
|
|
|
unsigned int r = (this+rightClassTable).get_class (right);
|
2018-10-08 04:43:59 +02:00
|
|
|
unsigned int offset = l + r;
|
2018-10-10 04:52:41 +02:00
|
|
|
const FWORD *v = &StructAtOffset<FWORD> (&(this+array), offset);
|
2018-11-02 18:23:54 +01:00
|
|
|
#if 0
|
2018-10-10 04:52:41 +02:00
|
|
|
if (unlikely ((const char *) v < (const char *) &array ||
|
|
|
|
(const char *) v > (const char *) end - 2))
|
2018-11-02 18:23:54 +01:00
|
|
|
#endif
|
2017-11-06 21:40:07 +01:00
|
|
|
return 0;
|
|
|
|
return *v;
|
2017-11-02 00:54:56 +01:00
|
|
|
}
|
|
|
|
|
2018-11-02 18:23:54 +01:00
|
|
|
inline bool apply (AAT::hb_aat_apply_context_t *c) const
|
|
|
|
{
|
|
|
|
TRACE_APPLY (this);
|
|
|
|
|
|
|
|
if (!c->plan->requested_kerning)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
accelerator_t accel (*this, c);
|
|
|
|
hb_kern_machine_t<accelerator_t> machine (accel);
|
|
|
|
machine.kern (c->font, c->buffer, c->plan->kern_mask);
|
|
|
|
|
|
|
|
return_trace (true);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct accelerator_t
|
|
|
|
{
|
|
|
|
const KernSubTableFormat2 &table;
|
|
|
|
AAT::hb_aat_apply_context_t *c;
|
|
|
|
|
|
|
|
inline accelerator_t (const KernSubTableFormat2 &table_,
|
|
|
|
AAT::hb_aat_apply_context_t *c_) :
|
|
|
|
table (table_), c (c_) {}
|
|
|
|
|
|
|
|
inline int get_kerning (hb_codepoint_t left, hb_codepoint_t right) const
|
|
|
|
{ return table.get_kerning (left, right, c); }
|
|
|
|
};
|
|
|
|
|
2017-11-02 00:54:56 +01:00
|
|
|
inline bool sanitize (hb_sanitize_context_t *c) const
|
|
|
|
{
|
|
|
|
TRACE_SANITIZE (this);
|
2018-10-11 00:14:41 +02:00
|
|
|
return_trace (true); /* Disabled. See above. */
|
2018-11-02 18:57:41 +01:00
|
|
|
return_trace (c->check_struct (this) &&
|
2017-11-04 17:01:11 +01:00
|
|
|
leftClassTable.sanitize (c, this) &&
|
|
|
|
rightClassTable.sanitize (c, this) &&
|
|
|
|
array.sanitize (c, this));
|
2017-11-02 00:54:56 +01:00
|
|
|
}
|
2017-11-04 17:01:11 +01:00
|
|
|
|
|
|
|
protected:
|
2018-11-06 18:07:15 +01:00
|
|
|
KernSubTableHeader header;
|
|
|
|
HBUINT16 rowWidth; /* The width, in bytes, of a row in the table. */
|
|
|
|
OffsetTo<KernClassTable> leftClassTable; /* Offset from beginning of this subtable to
|
|
|
|
* left-hand class table. */
|
|
|
|
OffsetTo<KernClassTable> rightClassTable;/* Offset from beginning of this subtable to
|
|
|
|
* right-hand class table. */
|
|
|
|
OffsetTo<FWORD> array; /* Offset from beginning of this subtable to
|
|
|
|
* the start of the kerning array. */
|
2017-11-04 17:01:11 +01:00
|
|
|
public:
|
2018-11-06 18:07:15 +01:00
|
|
|
DEFINE_SIZE_MIN (KernSubTableHeader::static_size + 8);
|
2017-11-02 00:54:56 +01:00
|
|
|
};
|
|
|
|
|
2018-11-06 18:07:15 +01:00
|
|
|
template <typename KernSubTableHeader>
|
2018-11-02 16:56:55 +01:00
|
|
|
struct KernSubTableFormat3
|
|
|
|
{
|
2018-11-02 18:23:54 +01:00
|
|
|
inline int get_kerning (hb_codepoint_t left, hb_codepoint_t right) const
|
2018-11-02 16:56:55 +01:00
|
|
|
{
|
|
|
|
hb_array_t<const FWORD> kernValue = kernValueZ.as_array (kernValueCount);
|
|
|
|
hb_array_t<const HBUINT8> leftClass = StructAfter<const UnsizedArrayOf<HBUINT8> > (kernValue).as_array (glyphCount);
|
|
|
|
hb_array_t<const HBUINT8> rightClass = StructAfter<const UnsizedArrayOf<HBUINT8> > (leftClass).as_array (glyphCount);
|
|
|
|
hb_array_t<const HBUINT8> kernIndex = StructAfter<const UnsizedArrayOf<HBUINT8> > (rightClass).as_array (leftClassCount * rightClassCount);
|
|
|
|
|
2018-11-03 19:58:54 +01:00
|
|
|
unsigned int leftC = leftClass[left];
|
|
|
|
unsigned int rightC = rightClass[right];
|
|
|
|
if (unlikely (leftC >= leftClassCount || rightC >= rightClassCount))
|
|
|
|
return 0;
|
|
|
|
unsigned int i = leftC * rightClassCount + rightC;
|
2018-11-02 16:56:55 +01:00
|
|
|
return kernValue[kernIndex[i]];
|
|
|
|
}
|
|
|
|
|
2018-11-02 18:23:54 +01:00
|
|
|
inline bool apply (AAT::hb_aat_apply_context_t *c) const
|
|
|
|
{
|
|
|
|
TRACE_APPLY (this);
|
|
|
|
|
|
|
|
if (!c->plan->requested_kerning)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
hb_kern_machine_t<KernSubTableFormat3> machine (*this);
|
|
|
|
machine.kern (c->font, c->buffer, c->plan->kern_mask);
|
|
|
|
|
|
|
|
return_trace (true);
|
|
|
|
}
|
|
|
|
|
2018-11-02 16:56:55 +01:00
|
|
|
inline bool sanitize (hb_sanitize_context_t *c) const
|
|
|
|
{
|
|
|
|
TRACE_SANITIZE (this);
|
|
|
|
return_trace (c->check_struct (this) &&
|
|
|
|
c->check_range (kernValueZ,
|
2018-11-03 20:06:45 +01:00
|
|
|
kernValueCount * sizeof (FWORD) +
|
2018-11-02 16:56:55 +01:00
|
|
|
glyphCount * 2 +
|
|
|
|
leftClassCount * rightClassCount));
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2018-11-06 18:07:15 +01:00
|
|
|
KernSubTableHeader header;
|
|
|
|
HBUINT16 glyphCount; /* The number of glyphs in this font. */
|
|
|
|
HBUINT8 kernValueCount; /* The number of kerning values. */
|
|
|
|
HBUINT8 leftClassCount; /* The number of left-hand classes. */
|
|
|
|
HBUINT8 rightClassCount;/* The number of right-hand classes. */
|
|
|
|
HBUINT8 flags; /* Set to zero (reserved for future use). */
|
|
|
|
UnsizedArrayOf<FWORD> kernValueZ; /* The kerning values.
|
|
|
|
* Length kernValueCount. */
|
2018-11-02 16:56:55 +01:00
|
|
|
#if 0
|
2018-11-06 18:07:15 +01:00
|
|
|
UnsizedArrayOf<HBUINT8>leftClass; /* The left-hand classes.
|
|
|
|
* Length glyphCount. */
|
|
|
|
UnsizedArrayOf<HBUINT8>rightClass; /* The right-hand classes.
|
|
|
|
* Length glyphCount. */
|
|
|
|
UnsizedArrayOf<HBUINT8>kernIndex; /* The indices into the kernValue array.
|
|
|
|
* Length leftClassCount * rightClassCount */
|
2018-11-02 16:56:55 +01:00
|
|
|
#endif
|
|
|
|
public:
|
2018-11-06 18:07:15 +01:00
|
|
|
DEFINE_SIZE_ARRAY (KernSubTableHeader::static_size + 6, kernValueZ);
|
2018-11-02 16:56:55 +01:00
|
|
|
};
|
|
|
|
|
2018-11-06 18:07:15 +01:00
|
|
|
template <typename KernSubTableHeader>
|
2017-11-02 00:54:56 +01:00
|
|
|
struct KernSubTable
|
|
|
|
{
|
2018-11-06 18:07:15 +01:00
|
|
|
inline unsigned int get_size (void) const { return u.header.length; }
|
2018-11-06 18:11:45 +01:00
|
|
|
inline unsigned int get_type (void) const { return u.header.format; }
|
2018-11-06 18:07:15 +01:00
|
|
|
|
2018-11-06 19:18:27 +01:00
|
|
|
inline bool is_crossStream (void) const
|
|
|
|
{ return u.header.coverage & u.header.CrossStream; }
|
|
|
|
|
|
|
|
inline bool is_variation (void) const
|
|
|
|
{ return u.header.coverage & u.header.Variation; }
|
2018-11-06 18:07:15 +01:00
|
|
|
|
|
|
|
inline bool is_horizontal (void) const
|
|
|
|
{ return (u.header.coverage & u.header.Direction) == u.header.DirectionHorizontal; }
|
|
|
|
|
|
|
|
inline bool is_override (void) const
|
|
|
|
{ return bool (u.header.coverage & u.header.Override); }
|
|
|
|
|
|
|
|
inline int get_kerning (hb_codepoint_t left, hb_codepoint_t right) const
|
2017-11-02 02:33:09 +01:00
|
|
|
{
|
2018-11-06 18:11:45 +01:00
|
|
|
switch (get_type ()) {
|
2018-11-02 18:23:54 +01:00
|
|
|
/* This method hooks up to hb_font_t's get_h_kerning. Only support Format0. */
|
2017-11-02 02:33:09 +01:00
|
|
|
case 0: return u.format0.get_kerning (left, right);
|
2017-11-02 00:54:56 +01:00
|
|
|
default:return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-06 18:11:45 +01:00
|
|
|
template <typename context_t>
|
|
|
|
inline typename context_t::return_t dispatch (context_t *c) const
|
2018-11-02 18:23:54 +01:00
|
|
|
{
|
2018-11-06 18:11:45 +01:00
|
|
|
unsigned int subtable_type = get_type ();
|
|
|
|
TRACE_DISPATCH (this, subtable_type);
|
|
|
|
switch (subtable_type) {
|
|
|
|
case 0: return_trace (c->dispatch (u.format0));
|
|
|
|
case 1: return_trace (c->dispatch (u.format1));
|
|
|
|
case 2: return_trace (c->dispatch (u.format2));
|
|
|
|
case 3: return_trace (c->dispatch (u.format3));
|
|
|
|
default: return_trace (c->default_return_value ());
|
2018-11-02 18:23:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-06 18:07:15 +01:00
|
|
|
inline bool sanitize (hb_sanitize_context_t *c) const
|
2017-11-02 00:54:56 +01:00
|
|
|
{
|
|
|
|
TRACE_SANITIZE (this);
|
2018-11-06 18:07:15 +01:00
|
|
|
if (unlikely (!u.header.sanitize (c) ||
|
|
|
|
u.header.length < u.header.min_size ||
|
|
|
|
!c->check_range (this, u.header.length))) return_trace (false);
|
2018-11-06 18:11:45 +01:00
|
|
|
|
|
|
|
return_trace (dispatch (c));
|
2017-11-02 00:54:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
union {
|
2018-11-06 18:07:15 +01:00
|
|
|
KernSubTableHeader header;
|
|
|
|
KernSubTableFormat0<KernSubTableHeader> format0;
|
|
|
|
KernSubTableFormat1<KernSubTableHeader> format1;
|
|
|
|
KernSubTableFormat2<KernSubTableHeader> format2;
|
|
|
|
KernSubTableFormat3<KernSubTableHeader> format3;
|
2017-11-02 00:54:56 +01:00
|
|
|
} u;
|
|
|
|
public:
|
|
|
|
DEFINE_SIZE_MIN (0);
|
|
|
|
};
|
|
|
|
|
2017-11-02 00:31:29 +01:00
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
struct KernTable
|
|
|
|
{
|
|
|
|
/* https://en.wikipedia.org/wiki/Curiously_recurring_template_pattern */
|
|
|
|
inline const T* thiz (void) const { return static_cast<const T *> (this); }
|
|
|
|
|
2018-10-10 04:46:12 +02:00
|
|
|
inline int get_h_kerning (hb_codepoint_t left, hb_codepoint_t right) const
|
2017-11-02 02:33:09 +01:00
|
|
|
{
|
2018-11-06 18:07:15 +01:00
|
|
|
typedef KernSubTable<typename T::SubTableHeader> SubTable;
|
|
|
|
|
2017-11-02 02:33:09 +01:00
|
|
|
int v = 0;
|
2018-11-06 18:07:15 +01:00
|
|
|
const SubTable *st = CastP<SubTable> (&thiz()->dataZ);
|
2017-11-02 02:33:09 +01:00
|
|
|
unsigned int count = thiz()->nTables;
|
|
|
|
for (unsigned int i = 0; i < count; i++)
|
|
|
|
{
|
2018-11-06 19:18:27 +01:00
|
|
|
if (!st->is_variation () || !st->is_crossStream () || !st->is_horizontal ())
|
2018-11-06 17:16:45 +01:00
|
|
|
continue;
|
|
|
|
if (st->is_override ())
|
2017-11-02 02:33:09 +01:00
|
|
|
v = 0;
|
2018-11-06 17:16:45 +01:00
|
|
|
v += st->get_kerning (left, right);
|
2018-11-06 18:07:15 +01:00
|
|
|
st = &StructAfter<SubTable> (*st);
|
2017-11-02 02:33:09 +01:00
|
|
|
}
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2018-11-02 18:23:54 +01:00
|
|
|
inline void apply (AAT::hb_aat_apply_context_t *c) const
|
|
|
|
{
|
2018-11-06 18:07:15 +01:00
|
|
|
typedef KernSubTable<typename T::SubTableHeader> SubTable;
|
|
|
|
|
2018-11-02 18:23:54 +01:00
|
|
|
c->set_lookup_index (0);
|
2018-11-06 18:07:15 +01:00
|
|
|
const SubTable *st = CastP<SubTable> (&thiz()->dataZ);
|
2018-11-02 18:23:54 +01:00
|
|
|
unsigned int count = thiz()->nTables;
|
|
|
|
/* If there's an override subtable, skip subtables before that. */
|
|
|
|
unsigned int last_override = 0;
|
|
|
|
for (unsigned int i = 0; i < count; i++)
|
|
|
|
{
|
2018-11-06 19:18:27 +01:00
|
|
|
if (!st->is_variation () && !st->is_crossStream () &&
|
|
|
|
st->is_override ())
|
2018-11-02 18:23:54 +01:00
|
|
|
last_override = i;
|
2018-11-06 18:07:15 +01:00
|
|
|
st = &StructAfter<SubTable> (*st);
|
2018-11-02 18:23:54 +01:00
|
|
|
}
|
2018-11-06 18:07:15 +01:00
|
|
|
st = CastP<SubTable> (&thiz()->dataZ);
|
2018-11-02 18:23:54 +01:00
|
|
|
for (unsigned int i = 0; i < count; i++)
|
|
|
|
{
|
2018-11-06 19:18:27 +01:00
|
|
|
if (st->is_variation () || st->is_crossStream ())
|
2018-11-02 19:26:04 +01:00
|
|
|
goto skip;
|
|
|
|
|
|
|
|
if (HB_DIRECTION_IS_HORIZONTAL (c->buffer->props.direction) != st->is_horizontal ())
|
|
|
|
goto skip;
|
|
|
|
|
2018-11-02 18:23:54 +01:00
|
|
|
if (i < last_override)
|
|
|
|
goto skip;
|
|
|
|
|
|
|
|
if (!c->buffer->message (c->font, "start kern subtable %d", c->lookup_index))
|
|
|
|
goto skip;
|
|
|
|
|
2018-11-02 19:43:38 +01:00
|
|
|
c->sanitizer.set_object (*st);
|
|
|
|
|
2018-11-06 18:11:45 +01:00
|
|
|
st->dispatch (c);
|
2018-11-02 18:23:54 +01:00
|
|
|
|
|
|
|
(void) c->buffer->message (c->font, "end kern subtable %d", c->lookup_index);
|
|
|
|
|
|
|
|
skip:
|
2018-11-06 18:07:15 +01:00
|
|
|
st = &StructAfter<SubTable> (*st);
|
2018-11-06 19:51:39 +01:00
|
|
|
c->set_lookup_index (c->lookup_index + 1);
|
2018-11-02 18:23:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-02 00:31:29 +01:00
|
|
|
inline bool sanitize (hb_sanitize_context_t *c) const
|
|
|
|
{
|
|
|
|
TRACE_SANITIZE (this);
|
|
|
|
if (unlikely (!c->check_struct (thiz()) ||
|
|
|
|
thiz()->version != T::VERSION))
|
|
|
|
return_trace (false);
|
|
|
|
|
2018-11-06 18:07:15 +01:00
|
|
|
typedef KernSubTable<typename T::SubTableHeader> SubTable;
|
|
|
|
|
|
|
|
const SubTable *st = CastP<SubTable> (&thiz()->dataZ);
|
2017-11-02 00:54:56 +01:00
|
|
|
unsigned int count = thiz()->nTables;
|
2017-11-02 00:31:29 +01:00
|
|
|
for (unsigned int i = 0; i < count; i++)
|
|
|
|
{
|
|
|
|
if (unlikely (!st->sanitize (c)))
|
|
|
|
return_trace (false);
|
2018-11-06 18:07:15 +01:00
|
|
|
st = &StructAfter<SubTable> (*st);
|
2017-11-02 00:31:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return_trace (true);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct KernOT : KernTable<KernOT>
|
|
|
|
{
|
2017-11-02 01:16:07 +01:00
|
|
|
friend struct KernTable<KernOT>;
|
2017-11-02 00:31:29 +01:00
|
|
|
|
|
|
|
static const uint16_t VERSION = 0x0000u;
|
|
|
|
|
2018-11-06 18:07:15 +01:00
|
|
|
struct SubTableHeader
|
2017-11-02 00:31:29 +01:00
|
|
|
{
|
2018-10-08 04:58:06 +02:00
|
|
|
enum Coverage
|
|
|
|
{
|
|
|
|
Direction = 0x01u,
|
|
|
|
Minimum = 0x02u,
|
|
|
|
CrossStream = 0x04u,
|
|
|
|
Override = 0x08u,
|
2017-11-02 02:33:09 +01:00
|
|
|
|
2018-10-08 04:58:06 +02:00
|
|
|
Variation = 0x00u, /* Not supported. */
|
2017-11-02 02:33:09 +01:00
|
|
|
|
2018-11-06 17:13:40 +01:00
|
|
|
DirectionHorizontal= 0x01u
|
2017-11-02 02:33:09 +01:00
|
|
|
};
|
|
|
|
|
2018-11-06 18:07:15 +01:00
|
|
|
inline bool sanitize (hb_sanitize_context_t *c) const
|
|
|
|
{
|
|
|
|
TRACE_SANITIZE (this);
|
|
|
|
return_trace (c->check_struct (this));
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
2018-01-10 03:07:30 +01:00
|
|
|
HBUINT16 versionZ; /* Unused. */
|
|
|
|
HBUINT16 length; /* Length of the subtable (including this header). */
|
|
|
|
HBUINT8 format; /* Subtable format. */
|
|
|
|
HBUINT8 coverage; /* Coverage bits. */
|
2017-11-02 00:31:29 +01:00
|
|
|
public:
|
2018-11-06 18:07:15 +01:00
|
|
|
DEFINE_SIZE_STATIC (6);
|
2017-11-02 00:31:29 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
protected:
|
2018-09-10 23:29:26 +02:00
|
|
|
HBUINT16 version; /* Version--0x0000u */
|
|
|
|
HBUINT16 nTables; /* Number of subtables in the kerning table. */
|
|
|
|
UnsizedArrayOf<HBUINT8> dataZ;
|
2017-11-02 00:31:29 +01:00
|
|
|
public:
|
2018-09-10 23:29:26 +02:00
|
|
|
DEFINE_SIZE_ARRAY (4, dataZ);
|
2017-11-02 00:31:29 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
struct KernAAT : KernTable<KernAAT>
|
|
|
|
{
|
2017-11-02 01:16:07 +01:00
|
|
|
friend struct KernTable<KernAAT>;
|
2017-11-02 00:31:29 +01:00
|
|
|
|
|
|
|
static const uint32_t VERSION = 0x00010000u;
|
|
|
|
|
2018-11-06 18:07:15 +01:00
|
|
|
struct SubTableHeader
|
2017-11-02 00:31:29 +01:00
|
|
|
{
|
2018-10-08 04:58:06 +02:00
|
|
|
enum Coverage
|
|
|
|
{
|
|
|
|
Direction = 0x80u,
|
|
|
|
CrossStream = 0x40u,
|
|
|
|
Variation = 0x20u,
|
2017-11-02 02:33:09 +01:00
|
|
|
|
2018-10-08 04:58:06 +02:00
|
|
|
Override = 0x00u, /* Not supported. */
|
2017-11-02 02:33:09 +01:00
|
|
|
|
2018-11-06 17:13:40 +01:00
|
|
|
DirectionHorizontal= 0x00u
|
2017-11-02 02:33:09 +01:00
|
|
|
};
|
|
|
|
|
2018-11-06 18:07:15 +01:00
|
|
|
inline bool sanitize (hb_sanitize_context_t *c) const
|
|
|
|
{
|
|
|
|
TRACE_SANITIZE (this);
|
|
|
|
return_trace (c->check_struct (this));
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
2018-01-10 03:07:30 +01:00
|
|
|
HBUINT32 length; /* Length of the subtable (including this header). */
|
|
|
|
HBUINT8 coverage; /* Coverage bits. */
|
|
|
|
HBUINT8 format; /* Subtable format. */
|
|
|
|
HBUINT16 tupleIndex; /* The tuple index (used for variations fonts).
|
2017-11-02 00:31:29 +01:00
|
|
|
* This value specifies which tuple this subtable covers. */
|
|
|
|
public:
|
2018-11-06 18:07:15 +01:00
|
|
|
DEFINE_SIZE_STATIC (8);
|
2017-11-02 00:31:29 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
protected:
|
2018-09-10 23:29:26 +02:00
|
|
|
HBUINT32 version; /* Version--0x00010000u */
|
|
|
|
HBUINT32 nTables; /* Number of subtables in the kerning table. */
|
|
|
|
UnsizedArrayOf<HBUINT8> dataZ;
|
2017-11-02 00:31:29 +01:00
|
|
|
public:
|
2018-09-10 23:29:26 +02:00
|
|
|
DEFINE_SIZE_ARRAY (8, dataZ);
|
2017-11-02 00:31:29 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
struct kern
|
|
|
|
{
|
|
|
|
static const hb_tag_t tableTag = HB_OT_TAG_kern;
|
|
|
|
|
2018-10-09 05:57:45 +02:00
|
|
|
inline bool has_data (void) const
|
|
|
|
{ return u.version32 != 0; }
|
|
|
|
|
2018-10-10 04:46:12 +02:00
|
|
|
inline int get_h_kerning (hb_codepoint_t left, hb_codepoint_t right) const
|
2017-11-02 02:33:09 +01:00
|
|
|
{
|
|
|
|
switch (u.major) {
|
2018-10-10 04:46:12 +02:00
|
|
|
case 0: return u.ot.get_h_kerning (left, right);
|
|
|
|
case 1: return u.aat.get_h_kerning (left, right);
|
2017-11-02 02:33:09 +01:00
|
|
|
default:return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-02 18:23:54 +01:00
|
|
|
inline void apply (AAT::hb_aat_apply_context_t *c) const
|
|
|
|
{
|
|
|
|
/* TODO Switch to dispatch(). */
|
|
|
|
switch (u.major) {
|
|
|
|
case 0: u.ot.apply (c); return;
|
|
|
|
case 1: u.aat.apply (c); return;
|
|
|
|
default: return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-02 00:31:29 +01:00
|
|
|
inline bool sanitize (hb_sanitize_context_t *c) const
|
|
|
|
{
|
|
|
|
TRACE_SANITIZE (this);
|
2018-10-10 03:56:37 +02:00
|
|
|
if (!u.version32.sanitize (c)) return_trace (false);
|
2017-11-02 00:31:29 +01:00
|
|
|
switch (u.major) {
|
|
|
|
case 0: return_trace (u.ot.sanitize (c));
|
|
|
|
case 1: return_trace (u.aat.sanitize (c));
|
|
|
|
default:return_trace (true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
union {
|
2018-10-09 05:57:45 +02:00
|
|
|
HBUINT32 version32;
|
2018-10-10 03:56:37 +02:00
|
|
|
HBUINT16 major;
|
2017-11-02 00:31:29 +01:00
|
|
|
KernOT ot;
|
|
|
|
KernAAT aat;
|
|
|
|
} u;
|
|
|
|
public:
|
2018-10-10 03:56:37 +02:00
|
|
|
DEFINE_SIZE_UNION (4, version32);
|
2017-11-02 00:31:29 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
} /* namespace OT */
|
|
|
|
|
|
|
|
|
|
|
|
#endif /* HB_OT_KERN_TABLE_HH */
|