2018-02-19 00:47:44 +01:00
|
|
|
/*
|
|
|
|
* Copyright © 2018 Ebrahim Byagowi
|
2018-03-01 21:37:26 +01:00
|
|
|
* Copyright © 2018 Google, Inc.
|
2018-02-19 00:47:44 +01:00
|
|
|
*
|
|
|
|
* 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_AAT_LAYOUT_KERX_TABLE_HH
|
|
|
|
#define HB_AAT_LAYOUT_KERX_TABLE_HH
|
|
|
|
|
2018-08-26 07:36:36 +02:00
|
|
|
#include "hb-open-type.hh"
|
|
|
|
#include "hb-aat-layout-common.hh"
|
2018-10-08 04:33:41 +02:00
|
|
|
#include "hb-ot-kern-table.hh"
|
2018-02-19 00:47:44 +01:00
|
|
|
|
2018-04-12 11:08:19 +02:00
|
|
|
/*
|
|
|
|
* kerx -- Extended Kerning
|
|
|
|
* https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6kerx.html
|
|
|
|
*/
|
2018-03-26 09:34:30 +02:00
|
|
|
#define HB_AAT_TAG_kerx HB_TAG('k','e','r','x')
|
2018-02-19 00:47:44 +01:00
|
|
|
|
|
|
|
|
2018-03-01 21:37:26 +01:00
|
|
|
namespace AAT {
|
2018-02-19 00:47:44 +01:00
|
|
|
|
2018-03-01 21:37:26 +01:00
|
|
|
using namespace OT;
|
2018-02-19 00:47:44 +01:00
|
|
|
|
|
|
|
|
2018-10-08 04:33:41 +02:00
|
|
|
struct KerxSubTableFormat0
|
2018-02-19 00:47:44 +01:00
|
|
|
{
|
2018-10-08 04:33:41 +02:00
|
|
|
inline int get_kerning (hb_codepoint_t left, hb_codepoint_t right) const
|
2018-02-19 00:47:44 +01:00
|
|
|
{
|
2018-10-08 04:33:41 +02:00
|
|
|
hb_glyph_pair_t pair = {left, right};
|
|
|
|
int i = pairs.bsearch (pair);
|
|
|
|
if (i == -1)
|
|
|
|
return 0;
|
|
|
|
return pairs[i].get_kerning ();
|
2018-02-19 00:47:44 +01:00
|
|
|
}
|
|
|
|
|
2018-10-07 20:01:33 +02:00
|
|
|
inline bool apply (hb_aat_apply_context_t *c) const
|
|
|
|
{
|
|
|
|
TRACE_APPLY (this);
|
|
|
|
|
2018-10-10 04:35:22 +02:00
|
|
|
hb_kern_machine_t<KerxSubTableFormat0> machine (*this);
|
|
|
|
|
|
|
|
machine.kern (c->font, c->buffer, c->plan->kern_mask);
|
2018-10-07 20:01:33 +02:00
|
|
|
|
|
|
|
return_trace (true);
|
|
|
|
}
|
2018-02-19 00:47:44 +01:00
|
|
|
|
|
|
|
inline bool sanitize (hb_sanitize_context_t *c) const
|
|
|
|
{
|
|
|
|
TRACE_SANITIZE (this);
|
2018-10-08 04:33:41 +02:00
|
|
|
return_trace (likely (pairs.sanitize (c)));
|
2018-02-19 00:47:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2018-10-08 04:33:41 +02:00
|
|
|
BinSearchArrayOf<KernPair, HBUINT32>
|
|
|
|
pairs; /* Sorted kern records. */
|
2018-02-19 00:47:44 +01:00
|
|
|
public:
|
2018-10-08 04:33:41 +02:00
|
|
|
DEFINE_SIZE_ARRAY (16, pairs);
|
2018-02-19 00:47:44 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
struct KerxSubTableFormat1
|
|
|
|
{
|
2018-10-07 20:01:33 +02:00
|
|
|
inline bool apply (hb_aat_apply_context_t *c) const
|
|
|
|
{
|
|
|
|
TRACE_APPLY (this);
|
|
|
|
|
|
|
|
/* TODO */
|
|
|
|
|
|
|
|
return_trace (true);
|
|
|
|
}
|
|
|
|
|
2018-02-19 00:47:44 +01:00
|
|
|
inline bool sanitize (hb_sanitize_context_t *c) const
|
|
|
|
{
|
2018-03-01 21:37:26 +01:00
|
|
|
TRACE_SANITIZE (this);
|
2018-04-20 18:42:58 +02:00
|
|
|
return_trace (likely (c->check_struct (this) &&
|
|
|
|
stateHeader.sanitize (c)));
|
2018-02-19 00:47:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2018-03-01 21:37:26 +01:00
|
|
|
StateTable<HBUINT16> stateHeader;
|
|
|
|
LOffsetTo<ArrayOf<HBUINT16> > valueTable;
|
2018-02-19 00:47:44 +01:00
|
|
|
public:
|
2018-03-01 21:37:26 +01:00
|
|
|
DEFINE_SIZE_STATIC (20);
|
2018-02-19 00:47:44 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
struct KerxSubTableFormat2
|
|
|
|
{
|
2018-10-08 05:08:39 +02:00
|
|
|
inline int get_kerning (hb_codepoint_t left, hb_codepoint_t right,
|
|
|
|
const char *end, unsigned int num_glyphs) const
|
2018-02-19 00:47:44 +01:00
|
|
|
{
|
2018-10-08 04:52:53 +02:00
|
|
|
unsigned int l = *(this+leftClassTable).get_value (left, num_glyphs);
|
|
|
|
unsigned int r = *(this+rightClassTable).get_value (right, num_glyphs);
|
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);
|
|
|
|
if (unlikely ((const char *) v < (const char *) &array ||
|
|
|
|
(const char *) v > (const char *) end - 2))
|
2018-02-19 00:47:44 +01:00
|
|
|
return 0;
|
|
|
|
return *v;
|
|
|
|
}
|
|
|
|
|
2018-10-07 20:01:33 +02:00
|
|
|
inline bool apply (hb_aat_apply_context_t *c) const
|
|
|
|
{
|
|
|
|
TRACE_APPLY (this);
|
|
|
|
|
2018-10-10 04:35:22 +02:00
|
|
|
accelerator_t accel (*this,
|
2018-10-10 04:49:33 +02:00
|
|
|
c->sanitizer.end, /* XXX Use SubTable length? */
|
2018-10-10 04:35:22 +02:00
|
|
|
c->face->get_num_glyphs ());
|
|
|
|
hb_kern_machine_t<accelerator_t> machine (accel);
|
|
|
|
machine.kern (c->font, c->buffer, c->plan->kern_mask);
|
2018-10-07 20:01:33 +02:00
|
|
|
|
|
|
|
return_trace (true);
|
|
|
|
}
|
|
|
|
|
2018-02-19 00:47:44 +01:00
|
|
|
inline bool sanitize (hb_sanitize_context_t *c) const
|
|
|
|
{
|
|
|
|
TRACE_SANITIZE (this);
|
2018-04-20 18:42:58 +02:00
|
|
|
return_trace (likely (c->check_struct (this) &&
|
|
|
|
rowWidth.sanitize (c) &&
|
|
|
|
leftClassTable.sanitize (c, this) &&
|
|
|
|
rightClassTable.sanitize (c, this) &&
|
|
|
|
array.sanitize (c, this)));
|
2018-02-19 00:47:44 +01:00
|
|
|
}
|
|
|
|
|
2018-10-10 04:35:22 +02:00
|
|
|
struct accelerator_t
|
|
|
|
{
|
|
|
|
const KerxSubTableFormat2 &table;
|
|
|
|
const char *end;
|
|
|
|
unsigned int num_glyphs;
|
|
|
|
|
|
|
|
inline accelerator_t (const KerxSubTableFormat2 &table_,
|
|
|
|
const char *end_, unsigned int num_glyphs_)
|
|
|
|
: table (table_), end (end_), num_glyphs (num_glyphs_) {}
|
|
|
|
|
|
|
|
inline int get_kerning (hb_codepoint_t left, hb_codepoint_t right) const
|
|
|
|
{
|
|
|
|
return table.get_kerning (left, right, end, num_glyphs);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-02-19 00:47:44 +01:00
|
|
|
protected:
|
|
|
|
HBUINT32 rowWidth; /* The width, in bytes, of a row in the table. */
|
2018-10-08 04:52:53 +02:00
|
|
|
LOffsetTo<Lookup<HBUINT16> >
|
2018-02-19 00:47:44 +01:00
|
|
|
leftClassTable; /* Offset from beginning of this subtable to
|
|
|
|
* left-hand class table. */
|
2018-10-08 04:52:53 +02:00
|
|
|
LOffsetTo<Lookup<HBUINT16> >
|
2018-02-19 00:47:44 +01:00
|
|
|
rightClassTable;/* Offset from beginning of this subtable to
|
|
|
|
* right-hand class table. */
|
|
|
|
LOffsetTo<FWORD>
|
|
|
|
array; /* Offset from beginning of this subtable to
|
|
|
|
* the start of the kerning array. */
|
|
|
|
public:
|
2018-03-01 21:37:26 +01:00
|
|
|
DEFINE_SIZE_STATIC (16);
|
2018-02-19 00:47:44 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
struct KerxSubTableFormat4
|
|
|
|
{
|
2018-10-07 20:01:33 +02:00
|
|
|
inline bool apply (hb_aat_apply_context_t *c) const
|
|
|
|
{
|
|
|
|
TRACE_APPLY (this);
|
|
|
|
|
|
|
|
/* TODO */
|
|
|
|
|
|
|
|
return_trace (true);
|
|
|
|
}
|
|
|
|
|
2018-02-19 00:47:44 +01:00
|
|
|
inline bool sanitize (hb_sanitize_context_t *c) const
|
|
|
|
{
|
|
|
|
TRACE_SANITIZE (this);
|
2018-10-08 04:52:53 +02:00
|
|
|
|
|
|
|
/* TODO */
|
|
|
|
return_trace (likely (c->check_struct (this)));
|
2018-02-19 00:47:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
public:
|
2018-10-08 04:52:53 +02:00
|
|
|
DEFINE_SIZE_STATIC (1);
|
2018-02-19 00:47:44 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
struct KerxSubTableFormat6
|
|
|
|
{
|
2018-10-07 20:01:33 +02:00
|
|
|
inline bool apply (hb_aat_apply_context_t *c) const
|
|
|
|
{
|
|
|
|
TRACE_APPLY (this);
|
|
|
|
|
|
|
|
/* TODO */
|
|
|
|
|
|
|
|
return_trace (true);
|
|
|
|
}
|
|
|
|
|
2018-02-19 00:47:44 +01:00
|
|
|
inline bool sanitize (hb_sanitize_context_t *c) const
|
|
|
|
{
|
2018-03-01 21:37:26 +01:00
|
|
|
TRACE_SANITIZE (this);
|
2018-04-20 18:42:58 +02:00
|
|
|
return_trace (likely (c->check_struct (this) &&
|
|
|
|
rowIndexTable.sanitize (c, this) &&
|
|
|
|
columnIndexTable.sanitize (c, this) &&
|
|
|
|
kerningArray.sanitize (c, this) &&
|
|
|
|
kerningVector.sanitize (c, this)));
|
2018-02-19 00:47:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2018-03-01 21:37:26 +01:00
|
|
|
HBUINT32 flags;
|
|
|
|
HBUINT16 rowCount;
|
|
|
|
HBUINT16 columnCount;
|
2018-03-02 08:34:09 +01:00
|
|
|
LOffsetTo<Lookup<HBUINT16> > rowIndexTable;
|
|
|
|
LOffsetTo<Lookup<HBUINT16> > columnIndexTable;
|
|
|
|
LOffsetTo<Lookup<HBUINT16> > kerningArray;
|
|
|
|
LOffsetTo<Lookup<HBUINT16> > kerningVector;
|
2018-02-19 00:47:44 +01:00
|
|
|
public:
|
2018-03-01 21:37:26 +01:00
|
|
|
DEFINE_SIZE_STATIC (24);
|
2018-02-19 00:47:44 +01:00
|
|
|
};
|
|
|
|
|
2018-03-01 21:37:26 +01:00
|
|
|
struct KerxTable
|
|
|
|
{
|
2018-10-09 14:28:07 +02:00
|
|
|
friend struct kerx;
|
2018-10-09 04:41:08 +02:00
|
|
|
|
2018-10-07 20:01:33 +02:00
|
|
|
inline unsigned int get_size (void) const { return length; }
|
|
|
|
inline unsigned int get_type (void) const { return coverage & SubtableType; }
|
|
|
|
|
|
|
|
enum Coverage
|
2018-02-19 00:47:44 +01:00
|
|
|
{
|
2018-10-07 20:01:33 +02:00
|
|
|
Vertical = 0x80000000, /* Set if table has vertical kerning values. */
|
|
|
|
CrossStream = 0x40000000, /* Set if table has cross-stream kerning values. */
|
|
|
|
Variation = 0x20000000, /* Set if table has variation kerning values. */
|
|
|
|
ProcessDirection = 0x10000000, /* If clear, process the glyphs forwards, that
|
|
|
|
* is, from first to last in the glyph stream.
|
|
|
|
* If we, process them from last to first.
|
|
|
|
* This flag only applies to state-table based
|
|
|
|
* 'kerx' subtables (types 1 and 4). */
|
|
|
|
Reserved = 0x0FFFFF00, /* Reserved, set to zero. */
|
|
|
|
SubtableType = 0x000000FF, /* Subtable type. */
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename context_t>
|
|
|
|
inline typename context_t::return_t dispatch (context_t *c) const
|
|
|
|
{
|
|
|
|
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 4 : return_trace (c->dispatch (u.format4));
|
|
|
|
case 6 : return_trace (c->dispatch (u.format6));
|
|
|
|
default: return_trace (c->default_return_value ());
|
|
|
|
}
|
2018-02-19 00:47:44 +01:00
|
|
|
}
|
|
|
|
|
2018-03-01 21:37:26 +01:00
|
|
|
inline bool sanitize (hb_sanitize_context_t *c) const
|
2018-02-19 00:47:44 +01:00
|
|
|
{
|
|
|
|
TRACE_SANITIZE (this);
|
2018-10-07 20:01:33 +02:00
|
|
|
if (!length.sanitize (c) ||
|
|
|
|
length < min_size ||
|
|
|
|
!c->check_range (this, length))
|
2018-03-01 21:37:26 +01:00
|
|
|
return_trace (false);
|
|
|
|
|
2018-10-07 20:01:33 +02:00
|
|
|
return_trace (dispatch (c));
|
2018-02-19 00:47:44 +01:00
|
|
|
}
|
|
|
|
|
2018-03-01 21:37:26 +01:00
|
|
|
protected:
|
|
|
|
HBUINT32 length;
|
2018-10-07 20:01:33 +02:00
|
|
|
HBUINT32 coverage;
|
|
|
|
HBUINT32 tupleCount;
|
2018-02-19 00:47:44 +01:00
|
|
|
union {
|
|
|
|
KerxSubTableFormat0 format0;
|
2018-03-01 21:37:26 +01:00
|
|
|
KerxSubTableFormat1 format1;
|
2018-02-19 00:47:44 +01:00
|
|
|
KerxSubTableFormat2 format2;
|
|
|
|
KerxSubTableFormat4 format4;
|
|
|
|
KerxSubTableFormat6 format6;
|
|
|
|
} u;
|
2018-03-01 21:37:26 +01:00
|
|
|
public:
|
|
|
|
DEFINE_SIZE_MIN (12);
|
2018-02-19 00:47:44 +01:00
|
|
|
};
|
|
|
|
|
2018-10-07 20:01:33 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* The 'kerx' Table
|
|
|
|
*/
|
|
|
|
|
2018-02-19 00:47:44 +01:00
|
|
|
struct kerx
|
|
|
|
{
|
2018-03-26 09:34:30 +02:00
|
|
|
static const hb_tag_t tableTag = HB_AAT_TAG_kerx;
|
2018-02-19 00:47:44 +01:00
|
|
|
|
2018-10-07 20:01:33 +02:00
|
|
|
inline bool has_data (void) const { return version != 0; }
|
|
|
|
|
|
|
|
inline void apply (hb_aat_apply_context_t *c) const
|
2018-02-24 10:19:42 +01:00
|
|
|
{
|
2018-10-07 20:01:33 +02:00
|
|
|
c->set_lookup_index (0);
|
|
|
|
const KerxTable *table = &firstTable;
|
|
|
|
unsigned int count = tableCount;
|
|
|
|
for (unsigned int i = 0; i < count; i++)
|
|
|
|
{
|
2018-10-09 04:41:08 +02:00
|
|
|
bool reverse;
|
|
|
|
|
|
|
|
if (HB_DIRECTION_IS_VERTICAL (c->buffer->props.direction) !=
|
|
|
|
bool (table->coverage & KerxTable::Vertical))
|
|
|
|
goto skip;
|
|
|
|
|
|
|
|
if (table->coverage & KerxTable::CrossStream)
|
|
|
|
goto skip; /* We do NOT handle cross-stream kerning. */
|
|
|
|
|
|
|
|
reverse = bool (table->coverage & KerxTable::ProcessDirection) !=
|
|
|
|
HB_DIRECTION_IS_BACKWARD (c->buffer->props.direction);
|
|
|
|
|
|
|
|
if (!c->buffer->message (c->font, "start kerx subtable %d", c->lookup_index))
|
|
|
|
goto skip;
|
|
|
|
|
|
|
|
if (reverse)
|
|
|
|
c->buffer->reverse ();
|
|
|
|
|
2018-10-09 06:20:35 +02:00
|
|
|
/* XXX Reverse-kern is not working yet... */
|
2018-10-07 20:01:33 +02:00
|
|
|
table->dispatch (c);
|
2018-10-09 04:41:08 +02:00
|
|
|
|
|
|
|
if (reverse)
|
|
|
|
c->buffer->reverse ();
|
|
|
|
|
|
|
|
(void) c->buffer->message (c->font, "end kerx subtable %d", c->lookup_index);
|
|
|
|
|
|
|
|
skip:
|
2018-10-07 20:01:33 +02:00
|
|
|
table = &StructAfter<KerxTable> (*table);
|
|
|
|
}
|
2018-02-24 10:19:42 +01:00
|
|
|
}
|
|
|
|
|
2018-03-01 21:37:26 +01:00
|
|
|
inline bool sanitize (hb_sanitize_context_t *c) const
|
2018-02-19 00:47:44 +01:00
|
|
|
{
|
2018-03-01 21:37:26 +01:00
|
|
|
TRACE_SANITIZE (this);
|
2018-10-07 20:01:33 +02:00
|
|
|
if (!version.sanitize (c) || version < 2 ||
|
|
|
|
!tableCount.sanitize (c))
|
2018-03-01 21:37:26 +01:00
|
|
|
return_trace (false);
|
2018-02-19 00:47:44 +01:00
|
|
|
|
2018-10-07 20:01:33 +02:00
|
|
|
const KerxTable *table = &firstTable;
|
|
|
|
unsigned int count = tableCount;
|
|
|
|
for (unsigned int i = 0; i < count; i++)
|
2018-02-19 00:47:44 +01:00
|
|
|
{
|
2018-10-07 20:01:33 +02:00
|
|
|
if (!table->sanitize (c))
|
|
|
|
return_trace (false);
|
2018-03-01 21:37:26 +01:00
|
|
|
table = &StructAfter<KerxTable> (*table);
|
2018-02-19 00:47:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return_trace (true);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2018-10-07 20:01:33 +02:00
|
|
|
HBUINT16 version; /* The version number of the extended kerning table
|
|
|
|
* (currently 2, 3, or 4). */
|
|
|
|
HBUINT16 unused; /* Set to 0. */
|
|
|
|
HBUINT32 tableCount; /* The number of subtables included in the extended kerning
|
|
|
|
* table. */
|
|
|
|
KerxTable firstTable; /* Subtables. */
|
|
|
|
/*subtableGlyphCoverageArray*/ /* Only if version >= 3. We don't use. */
|
|
|
|
|
2018-02-19 00:47:44 +01:00
|
|
|
public:
|
2018-10-07 20:01:33 +02:00
|
|
|
DEFINE_SIZE_MIN (8);
|
2018-02-19 00:47:44 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
} /* namespace AAT */
|
|
|
|
|
|
|
|
|
|
|
|
#endif /* HB_AAT_LAYOUT_KERX_TABLE_HH */
|