[ot] Reduce duplicated layout code for beyond-64k tables (#6399)

* [ot] Share mark attachment positioning across table widths

MarkArray::apply instantiates the same positioning and attachment-chain
updates for each mark-array and anchor-matrix representation.

Resolve the format-specific anchors in the wrapper and pass them to a
common helper. Preserve the anchor checks, buffer flags, messages, and
attachment overflow behavior.

With GCC 15.3 on x86-64 without LTO, this removes 1576 bytes of core
code/read-only data at -O3 and 648 bytes at -Os. Validated with the full
Meson suite (292 passed, one skipped) and sampled shaping benchmarks.

Assisted-by: OpenAI Codex <codex@openai.com>

* [ot] Share cursive attachment positioning across table widths

CursivePosFormat1_2 duplicates the direction-dependent positioning and
attachment-chain updates for legacy and beyond-64k entry/exit records.

Keep record access and anchor validation in the format-specific wrapper,
then pass the resolved anchors to a common attachment helper. Preserve
all direction cases, chain reversal, overflow handling, and messages.

With GCC 15.3 on x86-64 without LTO, this removes another 2760 bytes of
core code/read-only data at -O3 and 864 bytes at -Os. Validated with the
full Meson suite (292 passed, one skipped) and sampled shaping benchmarks.

Assisted-by: OpenAI Codex <codex@openai.com>

* [subset] Share mark-class remapping across record widths

Mark classes remain 16-bit values in both mark-record representations;
only the record stride and array count differ. Decode those in the
wrapper and share the class collection and remapping work.

Give the common helper hidden inline linkage so its body is shared
across translation units as well as record formats. Class ordering,
existing mappings, and allocation behavior are preserved.

With GCC 15.3 on x86-64 without LTO, this removes 1152 bytes of subset
code/read-only data at -O3 and 1440 bytes at -Os. Validated with the full
Meson suite (292 passed, one skipped) and subset benchmarks.

Assisted-by: OpenAI Codex <codex@openai.com>

* [subset] Share SinglePos serializers across source formats

SinglePos output serializers only need the source value format and
ValueBase pointer, but carried the full source lookup type through
several template layers. The per-glyph mapper also had distinct lambda
types for legacy and beyond-64k source tables.

Pass the source format and base directly, and use a common mapper with
the same reference captures as the original lambda. Output formats,
device offsets, value copying, and allocation behavior are preserved.

With GCC 15.3 on x86-64 without LTO, this removes 18080 bytes of subset
code/read-only data at -O3 and 9472 bytes at -Os. Validated with the full
Meson suite (292 passed, one skipped) and subset benchmarks.

Assisted-by: OpenAI Codex <codex@openai.com>

* [subset] Share delta substitution subset serialization

SingleSubstFormat1_3 constructs equivalent remapping iterators in each
source-format specialization, giving the output and coverage serializers
distinct iterator types and duplicating their bodies.

Move the subset preparation into a common helper taking the decoded
coverage, delta, and glyph mask. Preserve the glyph intersection,
modular delta calculation, remapping, and serializer failure behavior.

With GCC 15.3 on x86-64 without LTO, this removes 11896 bytes of subset
code/read-only data at -O3 and 5776 bytes at -Os. Validated with the full
Meson suite (292 passed, one skipped) and subset benchmarks.

Assisted-by: OpenAI Codex <codex@openai.com>

* [ot] Share SinglePos optical-bound context setup

SinglePos position_single duplicates the temporary apply context and
value application for each source representation. Keep coverage and
value-index checks in the wrappers and share the context setup through
ValueFormat, passing the original value base explicitly.

With GCC 15.3 on x86-64 without LTO, this removes 2088 bytes of core
code/read-only data at -O3 and 616 bytes at -Os. Optical-bound benchmarks
show no consistent slowdown across the four source formats.

Validated with the full Meson suite (292 passed, one skipped) and
synthetic optical-bound checks for all directions, 16/24-bit glyph IDs,
missing glyphs, and device adjustments.

Assisted-by: OpenAI Codex <codex@openai.com>

* [subset] Share retained mark-anchor index collection

Mark-to-base and mark-to-mark variation collection construct the same
row-major anchor-index lists, but do so inside each table-width template.
Pass the decoded row and class counts to a common collector.

Preserve coverage filtering, class selection, index order, and the
existing vector allocation behavior. Anchor access remains in the
format-specific wrappers.

With GCC 15.3 on x86-64 without LTO, this removes 2896 bytes of subset
code/read-only data at -O3 and 2152 bytes at -Os. Validated with the full
Meson suite (292 passed, one skipped) and variable-font subset benchmarks.

Assisted-by: OpenAI Codex <codex@openai.com>

* [ot] Share delta substitution glyph traversal across widths

Closure, dependency extraction, and glyph collection for delta
substitutions only need a decoded coverage, delta, and glyph mask.
Share those traversals instead of instantiating them for each source
representation.

Keep the closure population and overlapping-range guards, dependency
bounds filtering, traversal order, and allocation behavior unchanged.

With GCC 15.3 on x86-64 without LTO, this removes 1112 bytes of core and
952 bytes of subset code/read-only data at -O3, and 608/712 bytes at -Os.
Validated with the full Meson suite (292 passed, one skipped), C++11
syntax checking, and subset benchmarks.

Assisted-by: OpenAI Codex <codex@openai.com>

* [subset] Share pair-position variation-index collection

Resolve the table-width-dependent PairPos fields before collecting
variation indices. Share the class sets and matrix traversal between
the small and beyond-64k formats while retaining the original value
base for device offsets.

Saves 3328 bytes at -O3 and 2312 bytes at -Os in the subset library
with GCC 15.3 without LTO. The full test suite passes, including the
HB_NO_BEYOND_64K build; variable-font subset timings remain close.

Assisted-by: OpenAI Codex <codex@openai.com>
This commit is contained in:
Behdad Esfahbod authored and GitHub committed 2026-10-15 14:15:15 -06:00
commit fda2374fd0
12 files changed
+446 -355

No files matched your search

+15
View File
@@ -5,6 +5,21 @@ namespace OT {
namespace Layout {
namespace GPOS_impl {
HB_INTERNAL inline
void collect_anchor_indexes (const Coverage &coverage,
unsigned rows,
unsigned class_count,
const hb_set_t &glyphset,
const hb_map_t &klass_mapping,
hb_sorted_vector_t<unsigned> &indexes)
{
for (const auto _ : hb_zip (coverage, hb_range (rows)))
if (glyphset.has (_.first))
for (unsigned col = 0; col < class_count; col++)
if (klass_mapping.has (col))
indexes.push (_.second * class_count + col);
}
template <typename Types, typename RowCount=typename Types::HBUINT>
struct AnchorMatrix
{
+6 -2
View File
@@ -18,9 +18,13 @@ enum attach_type_t {
#define attach_type() var.u8[2] /* attachment type */
/* Note! if attach_chain() is zero, the value of attach_type() is irrelevant. */
template<typename Iterator, typename SrcLookup>
struct ValueBase;
struct ValueFormat;
template<typename Iterator>
static void SinglePos_serialize (hb_serialize_context_t *c,
const SrcLookup *src,
const ValueBase *src,
ValueFormat src_format,
Iterator it,
const hb_hashmap_t<unsigned, hb_pair_t<unsigned, int>> *layout_variation_idx_delta_map,
unsigned new_format);
+128 -116
View File
@@ -100,6 +100,130 @@ reverse_cursive_minor_offset (hb_glyph_position_t *pos,
}
static inline bool
apply_cursive_attachment (hb_ot_apply_context_t *c,
const Anchor &exit_anchor,
const Anchor &entry_anchor,
unsigned i)
{
hb_buffer_t *buffer = c->buffer;
unsigned int j = buffer->idx;
if (HB_BUFFER_MESSAGE_MORE && c->buffer->messaging ())
{
c->buffer->message (c->font,
"cursive attaching glyph at %u to glyph at %u",
i, j);
}
buffer->unsafe_to_break (i, j + 1);
float entry_x, entry_y, exit_x, exit_y;
exit_anchor.get_anchor (c, buffer->info[i].codepoint, &exit_x, &exit_y);
entry_anchor.get_anchor (c, buffer->info[j].codepoint, &entry_x, &entry_y);
hb_glyph_position_t *pos = buffer->pos;
hb_position_t d;
/* Main-direction adjustment */
switch (c->direction) {
case HB_DIRECTION_LTR:
pos[i].x_advance = hb_saturate_add ((hb_position_t) roundf (exit_x), pos[i].x_offset);
d = hb_saturate_add ((hb_position_t) roundf (entry_x), pos[j].x_offset);
pos[j].x_advance = hb_saturate_sub (pos[j].x_advance, d);
pos[j].x_offset = hb_saturate_sub (pos[j].x_offset, d);
break;
case HB_DIRECTION_RTL:
d = hb_saturate_add ((hb_position_t) roundf (exit_x), pos[i].x_offset);
pos[i].x_advance = hb_saturate_sub (pos[i].x_advance, d);
pos[i].x_offset = hb_saturate_sub (pos[i].x_offset, d);
pos[j].x_advance = hb_saturate_add ((hb_position_t) roundf (entry_x), pos[j].x_offset);
break;
case HB_DIRECTION_TTB:
pos[i].y_advance = hb_saturate_add ((hb_position_t) roundf (exit_y), pos[i].y_offset);
d = hb_saturate_add ((hb_position_t) roundf (entry_y), pos[j].y_offset);
pos[j].y_advance = hb_saturate_sub (pos[j].y_advance, d);
pos[j].y_offset = hb_saturate_sub (pos[j].y_offset, d);
break;
case HB_DIRECTION_BTT:
d = hb_saturate_add ((hb_position_t) roundf (exit_y), pos[i].y_offset);
pos[i].y_advance = hb_saturate_sub (pos[i].y_advance, d);
pos[i].y_offset = hb_saturate_sub (pos[i].y_offset, d);
pos[j].y_advance = (hb_position_t) roundf (entry_y);
break;
case HB_DIRECTION_INVALID:
default:
break;
}
/* Cross-direction adjustment */
/* We attach child to parent (think graph theory and rooted trees whereas
* the root stays on baseline and each node aligns itself against its
* parent.
*
* Optimize things for the case of RightToLeft, as that's most common in
* Arabic. */
unsigned int child = i;
unsigned int parent = j;
hb_position_t x_offset = (hb_position_t) roundf (entry_x - exit_x);
hb_position_t y_offset = (hb_position_t) roundf (entry_y - exit_y);
if (!(c->lookup_props & LookupFlag::RightToLeft))
{
unsigned int k = child;
child = parent;
parent = k;
x_offset = -x_offset;
y_offset = -y_offset;
}
/* If child was already connected to someone else, walk through its old
* chain and reverse the link direction, such that the whole tree of its
* previous connection now attaches to new parent. Watch out for case
* where new parent is on the path from old chain...
*/
reverse_cursive_minor_offset (pos, buffer->len, child, c->direction, parent);
pos[child].attach_chain() = (int) parent - (int) child;
if (pos[child].attach_chain() != (int) parent - (int) child)
{
pos[child].attach_chain() = 0;
goto overflow;
}
pos[child].attach_type() = ATTACH_TYPE_CURSIVE;
buffer->scratch_flags |= HB_BUFFER_SCRATCH_FLAG_HAS_GPOS_ATTACHMENT;
if (likely (HB_DIRECTION_IS_HORIZONTAL (c->direction)))
pos[child].y_offset = y_offset;
else
pos[child].x_offset = x_offset;
/* If parent was attached to child, separate them.
* https://github.com/harfbuzz/harfbuzz/issues/2469
*/
if (unlikely (pos[parent].attach_chain() == -pos[child].attach_chain()))
{
pos[parent].attach_chain() = 0;
if (likely (HB_DIRECTION_IS_HORIZONTAL (c->direction)))
pos[parent].y_offset = 0;
else
pos[parent].x_offset = 0;
}
if (HB_BUFFER_MESSAGE_MORE && c->buffer->messaging ())
{
c->buffer->message (c->font,
"cursive attached glyph at %u to glyph at %u",
i, j);
}
overflow:
buffer->idx++;
return true;
}
template <typename Types>
struct CursivePosFormat1_2
{
@@ -176,122 +300,10 @@ struct CursivePosFormat1_2
}
hb_barrier ();
unsigned int i = skippy_iter.idx;
unsigned int j = buffer->idx;
if (HB_BUFFER_MESSAGE_MORE && c->buffer->messaging ())
{
c->buffer->message (c->font,
"cursive attaching glyph at %u to glyph at %u",
i, j);
}
buffer->unsafe_to_break (i, j + 1);
float entry_x, entry_y, exit_x, exit_y;
(this+prev_record.exitAnchor).get_anchor (c, buffer->info[i].codepoint, &exit_x, &exit_y);
(this+this_record.entryAnchor).get_anchor (c, buffer->info[j].codepoint, &entry_x, &entry_y);
hb_glyph_position_t *pos = buffer->pos;
hb_position_t d;
/* Main-direction adjustment */
switch (c->direction) {
case HB_DIRECTION_LTR:
pos[i].x_advance = hb_saturate_add ((hb_position_t) roundf (exit_x), pos[i].x_offset);
d = hb_saturate_add ((hb_position_t) roundf (entry_x), pos[j].x_offset);
pos[j].x_advance = hb_saturate_sub (pos[j].x_advance, d);
pos[j].x_offset = hb_saturate_sub (pos[j].x_offset, d);
break;
case HB_DIRECTION_RTL:
d = hb_saturate_add ((hb_position_t) roundf (exit_x), pos[i].x_offset);
pos[i].x_advance = hb_saturate_sub (pos[i].x_advance, d);
pos[i].x_offset = hb_saturate_sub (pos[i].x_offset, d);
pos[j].x_advance = hb_saturate_add ((hb_position_t) roundf (entry_x), pos[j].x_offset);
break;
case HB_DIRECTION_TTB:
pos[i].y_advance = hb_saturate_add ((hb_position_t) roundf (exit_y), pos[i].y_offset);
d = hb_saturate_add ((hb_position_t) roundf (entry_y), pos[j].y_offset);
pos[j].y_advance = hb_saturate_sub (pos[j].y_advance, d);
pos[j].y_offset = hb_saturate_sub (pos[j].y_offset, d);
break;
case HB_DIRECTION_BTT:
d = hb_saturate_add ((hb_position_t) roundf (exit_y), pos[i].y_offset);
pos[i].y_advance = hb_saturate_sub (pos[i].y_advance, d);
pos[i].y_offset = hb_saturate_sub (pos[i].y_offset, d);
pos[j].y_advance = (hb_position_t) roundf (entry_y);
break;
case HB_DIRECTION_INVALID:
default:
break;
}
/* Cross-direction adjustment */
/* We attach child to parent (think graph theory and rooted trees whereas
* the root stays on baseline and each node aligns itself against its
* parent.
*
* Optimize things for the case of RightToLeft, as that's most common in
* Arabic. */
unsigned int child = i;
unsigned int parent = j;
hb_position_t x_offset = (hb_position_t) roundf (entry_x - exit_x);
hb_position_t y_offset = (hb_position_t) roundf (entry_y - exit_y);
if (!(c->lookup_props & LookupFlag::RightToLeft))
{
unsigned int k = child;
child = parent;
parent = k;
x_offset = -x_offset;
y_offset = -y_offset;
}
/* If child was already connected to someone else, walk through its old
* chain and reverse the link direction, such that the whole tree of its
* previous connection now attaches to new parent. Watch out for case
* where new parent is on the path from old chain...
*/
reverse_cursive_minor_offset (pos, buffer->len, child, c->direction, parent);
pos[child].attach_chain() = (int) parent - (int) child;
if (pos[child].attach_chain() != (int) parent - (int) child)
{
pos[child].attach_chain() = 0;
goto overflow;
}
pos[child].attach_type() = ATTACH_TYPE_CURSIVE;
buffer->scratch_flags |= HB_BUFFER_SCRATCH_FLAG_HAS_GPOS_ATTACHMENT;
if (likely (HB_DIRECTION_IS_HORIZONTAL (c->direction)))
pos[child].y_offset = y_offset;
else
pos[child].x_offset = x_offset;
/* If parent was attached to child, separate them.
* https://github.com/harfbuzz/harfbuzz/issues/2469
*/
if (unlikely (pos[parent].attach_chain() == -pos[child].attach_chain()))
{
pos[parent].attach_chain() = 0;
if (likely (HB_DIRECTION_IS_HORIZONTAL (c->direction)))
pos[parent].y_offset = 0;
else
pos[parent].x_offset = 0;
}
if (HB_BUFFER_MESSAGE_MORE && c->buffer->messaging ())
{
c->buffer->message (c->font,
"cursive attached glyph at %u to glyph at %u",
i, j);
}
overflow:
buffer->idx++;
return_trace (true);
return_trace (apply_cursive_attachment (c,
this+prev_record.exitAnchor,
this+this_record.entryAnchor,
skippy_iter.idx));
}
template <typename Iterator,
+78 -55
View File
@@ -31,6 +31,57 @@ resolve_cross_offset (const hb_glyph_position_t *pos,
return offset;
}
static inline bool
apply_mark_attachment (hb_ot_apply_context_t *c,
const Anchor &mark_anchor,
const Anchor &glyph_anchor,
unsigned glyph_pos)
{
hb_buffer_t *buffer = c->buffer;
float mark_x, mark_y, base_x, base_y;
buffer->unsafe_to_break (glyph_pos, buffer->idx + 1);
mark_anchor.get_anchor (c, buffer->cur().codepoint, &mark_x, &mark_y);
glyph_anchor.get_anchor (c, buffer->info[glyph_pos].codepoint, &base_x, &base_y);
if (HB_BUFFER_MESSAGE_MORE && c->buffer->messaging ())
{
c->buffer->message (c->font,
"attaching mark glyph at %u to glyph at %u",
c->buffer->idx, glyph_pos);
}
hb_position_t base_offset = resolve_cross_offset (buffer->pos, buffer->len, glyph_pos,
buffer->props.direction);
hb_glyph_position_t &mark = buffer->cur_pos();
mark.attach_chain() = (int) glyph_pos - (int) buffer->idx;
if (mark.attach_chain() != (int) glyph_pos - (int) buffer->idx)
{
mark.attach_chain() = 0;
goto overflow;
}
mark.attach_type() = ATTACH_TYPE_MARK;
mark.x_offset = roundf (base_x - mark_x);
mark.y_offset = roundf (base_y - mark_y);
if (HB_DIRECTION_IS_HORIZONTAL (buffer->props.direction))
mark.y_offset = hb_saturate_add (mark.y_offset, base_offset);
else
mark.x_offset = hb_saturate_add (mark.x_offset, base_offset);
buffer->scratch_flags |= HB_BUFFER_SCRATCH_FLAG_HAS_GPOS_ATTACHMENT;
if (HB_BUFFER_MESSAGE_MORE && c->buffer->messaging ())
{
c->buffer->message (c->font,
"attached mark glyph at %u to glyph at %u",
c->buffer->idx, glyph_pos);
}
overflow:
buffer->idx++;
return true;
}
template <typename Types>
struct MarkArray : Types::template ArrayOf<MarkRecord<Types>>
/* Array of MarkRecords--in Coverage order */
@@ -50,7 +101,6 @@ struct MarkArray : Types::template ArrayOf<MarkRecord<Types>>
unsigned int glyph_pos) const
{
TRACE_APPLY (this);
hb_buffer_t *buffer = c->buffer;
const MarkRecord<Types> &record = Super::operator[](mark_index);
unsigned int mark_class = record.klass;
@@ -61,48 +111,7 @@ struct MarkArray : Types::template ArrayOf<MarkRecord<Types>>
* return false such that the subsequent subtables have a chance at it. */
if (unlikely (!found)) return_trace (false);
float mark_x, mark_y, base_x, base_y;
buffer->unsafe_to_break (glyph_pos, buffer->idx + 1);
mark_anchor.get_anchor (c, buffer->cur().codepoint, &mark_x, &mark_y);
glyph_anchor.get_anchor (c, buffer->info[glyph_pos].codepoint, &base_x, &base_y);
if (HB_BUFFER_MESSAGE_MORE && c->buffer->messaging ())
{
c->buffer->message (c->font,
"attaching mark glyph at %u to glyph at %u",
c->buffer->idx, glyph_pos);
}
hb_position_t base_offset = resolve_cross_offset (buffer->pos, buffer->len, glyph_pos,
buffer->props.direction);
hb_glyph_position_t &mark = buffer->cur_pos();
mark.attach_chain() = (int) glyph_pos - (int) buffer->idx;
if (mark.attach_chain() != (int) glyph_pos - (int) buffer->idx)
{
mark.attach_chain() = 0;
goto overflow;
}
mark.attach_type() = ATTACH_TYPE_MARK;
mark.x_offset = roundf (base_x - mark_x);
mark.y_offset = roundf (base_y - mark_y);
if (HB_DIRECTION_IS_HORIZONTAL (buffer->props.direction))
mark.y_offset = hb_saturate_add (mark.y_offset, base_offset);
else
mark.x_offset = hb_saturate_add (mark.x_offset, base_offset);
buffer->scratch_flags |= HB_BUFFER_SCRATCH_FLAG_HAS_GPOS_ATTACHMENT;
if (HB_BUFFER_MESSAGE_MORE && c->buffer->messaging ())
{
c->buffer->message (c->font,
"attached mark glyph at %u to glyph at %u",
c->buffer->idx, glyph_pos);
}
overflow:
buffer->idx++;
return_trace (true);
return_trace (apply_mark_attachment (c, mark_anchor, glyph_anchor, glyph_pos));
}
template <typename Iterator,
@@ -138,21 +147,22 @@ struct MarkArray : Types::template ArrayOf<MarkRecord<Types>>
}
};
template <typename Types>
HB_INTERNAL inline
void Markclass_closure_and_remap_indexes (const Coverage &mark_coverage,
const MarkArray<Types> &mark_array,
const hb_set_t &glyphset,
hb_map_t* klass_mapping /* INOUT */)
void Markclass_closure_and_remap_indexes (const Coverage &mark_coverage,
const void *mark_records,
unsigned count,
unsigned record_size,
const hb_set_t &glyphset,
hb_map_t *klass_mapping)
{
hb_set_t orig_classes;
+ hb_zip (mark_coverage, mark_array)
| hb_filter (glyphset, hb_first)
| hb_map (hb_second)
| hb_map (&MarkRecord<Types>::get_class)
| hb_sink (orig_classes)
;
for (const auto _ : hb_zip (mark_coverage, hb_range (count)))
if (glyphset.has (_.first))
{
const auto *klass = reinterpret_cast<const HBUINT16 *>
((const char *) mark_records + _.second * record_size);
orig_classes.add (*klass);
}
unsigned idx = 0;
for (auto klass : orig_classes.iter ())
@@ -163,6 +173,19 @@ void Markclass_closure_and_remap_indexes (const Coverage &mark_coverage,
}
}
template <typename Types>
HB_INTERNAL inline
void Markclass_closure_and_remap_indexes (const Coverage &mark_coverage,
const MarkArray<Types> &mark_array,
const hb_set_t &glyphset,
hb_map_t* klass_mapping /* INOUT */)
{
Markclass_closure_and_remap_indexes (mark_coverage,
mark_array.arrayZ, mark_array.len,
MarkRecord<Types>::static_size,
glyphset, klass_mapping);
}
}
}
}
+3 -15
View File
@@ -58,22 +58,10 @@ struct MarkBasePosFormat1_2
hb_map_t klass_mapping;
Markclass_closure_and_remap_indexes (this+markCoverage, this+markArray, *c->glyph_set, &klass_mapping);
unsigned basecount = (this+baseArray).rows;
auto base_iter =
+ hb_zip (this+baseCoverage, hb_range (basecount))
| hb_filter (c->glyph_set, hb_first)
| hb_map (hb_second)
;
hb_sorted_vector_t<unsigned> base_indexes;
for (const unsigned row : base_iter)
{
+ hb_range ((unsigned) classCount)
| hb_filter (klass_mapping)
| hb_map ([&] (const unsigned col) { return row * (unsigned) classCount + col; })
| hb_sink (base_indexes)
;
}
collect_anchor_indexes (this+baseCoverage, (this+baseArray).rows,
classCount, *c->glyph_set, klass_mapping,
base_indexes);
(this+baseArray).collect_variation_indices (c, base_indexes.iter ());
}
+3 -15
View File
@@ -60,22 +60,10 @@ struct MarkMarkPosFormat1_2
hb_map_t klass_mapping;
Markclass_closure_and_remap_indexes (this+mark1Coverage, this+mark1Array, *c->glyph_set, &klass_mapping);
unsigned mark2_count = (this+mark2Array).rows;
auto mark2_iter =
+ hb_zip (this+mark2Coverage, hb_range (mark2_count))
| hb_filter (c->glyph_set, hb_first)
| hb_map (hb_second)
;
hb_sorted_vector_t<unsigned> mark2_indexes;
for (const unsigned row : mark2_iter)
{
+ hb_range ((unsigned) classCount)
| hb_filter (klass_mapping)
| hb_map ([&] (const unsigned col) { return row * (unsigned) classCount + col; })
| hb_sink (mark2_indexes)
;
}
collect_anchor_indexes (this+mark2Coverage, (this+mark2Array).rows,
classCount, *c->glyph_set, klass_mapping,
mark2_indexes);
(this+mark2Array).collect_variation_indices (c, mark2_indexes.iter ());
}
+64 -45
View File
@@ -7,6 +7,66 @@ namespace OT {
namespace Layout {
namespace GPOS_impl {
HB_INTERNAL inline
void collect_pair_variation_indices (hb_collect_variation_indices_context_t *c,
const ValueBase *base,
const Coverage &coverage,
const ClassDef &class_def1,
const ClassDef &class_def2,
const ValueFormat &valueFormat1,
const ValueFormat &valueFormat2,
unsigned class1_count,
unsigned class2_count,
const ValueRecord &values)
{
if (!coverage.intersects (c->glyph_set) ||
!class_def2.intersects (c->glyph_set)) return;
if ((!valueFormat1.has_device ()) && (!valueFormat2.has_device ())) return;
hb_set_t klass1_glyphs, klass2_glyphs;
if (!class_def1.collect_coverage (&klass1_glyphs)) return;
if (!class_def2.collect_coverage (&klass2_glyphs)) return;
hb_set_t class1_set, class2_set;
for (const unsigned cp : + c->glyph_set->iter () | hb_filter (coverage))
{
if (!klass1_glyphs.has (cp)) class1_set.add (0);
else
{
unsigned klass1 = class_def1.get (cp);
class1_set.add (klass1);
}
}
class2_set.add (0);
for (const unsigned cp : + c->glyph_set->iter () | hb_filter (klass2_glyphs))
{
unsigned klass2 = class_def2.get (cp);
class2_set.add (klass2);
}
if (class1_set.is_empty ()
|| class2_set.is_empty ()
|| (class2_set.get_population() == 1 && class2_set.has(0)))
return;
unsigned len1 = valueFormat1.get_len ();
unsigned len2 = valueFormat2.get_len ();
const hb_array_t<const Value> values_array = values.as_array ((unsigned)class1_count * (unsigned) class2_count * (len1 + len2));
for (const unsigned class1_idx : class1_set.iter ())
{
for (const unsigned class2_idx : class2_set.iter ())
{
unsigned start_offset = (class1_idx * (unsigned) class2_count + class2_idx) * (len1 + len2);
if (valueFormat1.has_device ())
valueFormat1.collect_variation_indices (c, base, values_array.sub_array (start_offset, len1));
if (valueFormat2.has_device ())
valueFormat2.collect_variation_indices (c, base, values_array.sub_array (start_offset+len1, len2));
}
}
}
template <typename Types>
struct PairPosFormat2_4 : ValueBase
{
@@ -68,51 +128,10 @@ struct PairPosFormat2_4 : ValueBase
void closure_lookups (hb_closure_lookups_context_t *c) const {}
void collect_variation_indices (hb_collect_variation_indices_context_t *c) const
{
if (!intersects (c->glyph_set)) return;
if ((!valueFormat1.has_device ()) && (!valueFormat2.has_device ())) return;
hb_set_t klass1_glyphs, klass2_glyphs;
if (!(this+classDef1).collect_coverage (&klass1_glyphs)) return;
if (!(this+classDef2).collect_coverage (&klass2_glyphs)) return;
hb_set_t class1_set, class2_set;
for (const unsigned cp : + c->glyph_set->iter () | hb_filter (this + coverage))
{
if (!klass1_glyphs.has (cp)) class1_set.add (0);
else
{
unsigned klass1 = (this+classDef1).get (cp);
class1_set.add (klass1);
}
}
class2_set.add (0);
for (const unsigned cp : + c->glyph_set->iter () | hb_filter (klass2_glyphs))
{
unsigned klass2 = (this+classDef2).get (cp);
class2_set.add (klass2);
}
if (class1_set.is_empty ()
|| class2_set.is_empty ()
|| (class2_set.get_population() == 1 && class2_set.has(0)))
return;
unsigned len1 = valueFormat1.get_len ();
unsigned len2 = valueFormat2.get_len ();
const hb_array_t<const Value> values_array = values.as_array ((unsigned)class1Count * (unsigned) class2Count * (len1 + len2));
for (const unsigned class1_idx : class1_set.iter ())
{
for (const unsigned class2_idx : class2_set.iter ())
{
unsigned start_offset = (class1_idx * (unsigned) class2Count + class2_idx) * (len1 + len2);
if (valueFormat1.has_device ())
valueFormat1.collect_variation_indices (c, this, values_array.sub_array (start_offset, len1));
if (valueFormat2.has_device ())
valueFormat2.collect_variation_indices (c, this, values_array.sub_array (start_offset+len1, len2));
}
}
collect_pair_variation_indices (c, this, this+coverage,
this+classDef1, this+classDef2,
valueFormat1, valueFormat2,
class1Count, class2Count, values);
}
void collect_glyphs (hb_collect_glyphs_context_t *c) const
+10 -9
View File
@@ -37,10 +37,10 @@ struct SinglePos
}
template<typename Iterator,
typename SrcLookup,
hb_requires (hb_is_iterator (Iterator))>
void serialize (hb_serialize_context_t *c,
const SrcLookup* src,
const ValueBase* src,
ValueFormat src_format,
Iterator glyph_val_iter_pairs,
const hb_hashmap_t<unsigned, hb_pair_t<unsigned, int>> *layout_variation_idx_delta_map,
unsigned newFormat)
@@ -63,26 +63,26 @@ struct SinglePos
u.format.v = format;
switch (u.format.v) {
case 1: hb_barrier (); u.format1.serialize (c,
src,
src, src_format,
glyph_val_iter_pairs,
new_format,
layout_variation_idx_delta_map);
return;
case 2: hb_barrier (); u.format2.serialize (c,
src,
src, src_format,
glyph_val_iter_pairs,
new_format,
layout_variation_idx_delta_map);
return;
#ifndef HB_NO_BEYOND_64K
case 3: u.format3.serialize (c,
src,
src, src_format,
glyph_val_iter_pairs,
new_format,
layout_variation_idx_delta_map);
return;
case 4: u.format4.serialize (c,
src,
src, src_format,
glyph_val_iter_pairs,
new_format,
layout_variation_idx_delta_map);
@@ -110,14 +110,15 @@ struct SinglePos
};
template<typename Iterator, typename SrcLookup>
template<typename Iterator>
static void
SinglePos_serialize (hb_serialize_context_t *c,
const SrcLookup *src,
const ValueBase *src,
ValueFormat src_format,
Iterator it,
const hb_hashmap_t<unsigned, hb_pair_t<unsigned, int>> *layout_variation_idx_delta_map,
unsigned new_format)
{ c->start_embed<SinglePos> ()->serialize (c, src, it, layout_variation_idx_delta_map, new_format); }
{ c->start_embed<SinglePos> ()->serialize (c, src, src_format, it, layout_variation_idx_delta_map, new_format); }
}
+6 -10
View File
@@ -100,20 +100,16 @@ struct SinglePosFormat1_3 : ValueBase
unsigned int index = (this+coverage).get_coverage (gid);
if (likely (index == NOT_COVERED)) return false;
/* This is ugly... */
hb_buffer_t buffer {};
buffer.props.direction = direction;
OT::hb_ot_apply_context_t c (1, font, &buffer, table_blob);
valueFormat.apply_value (&c, this, values, pos);
valueFormat.position_single (font, table_blob, direction, this,
values, pos);
return true;
}
template<typename Iterator,
typename SrcLookup,
hb_requires (hb_is_iterator (Iterator))>
void serialize (hb_serialize_context_t *c,
const SrcLookup *src,
const ValueBase *src,
ValueFormat src_format,
Iterator it,
ValueFormat newFormat,
const hb_hashmap_t<unsigned, hb_pair_t<unsigned, int>> *layout_variation_idx_delta_map)
@@ -125,7 +121,7 @@ struct SinglePosFormat1_3 : ValueBase
for (const hb_array_t<const Value>& _ : + it | hb_map (hb_second))
{
src->get_value_format ().copy_values (c, newFormat, src, &_, layout_variation_idx_delta_map);
src_format.copy_values (c, newFormat, src, &_, layout_variation_idx_delta_map);
// Only serialize the first entry in the iterator, the rest are assumed to
// be the same.
break;
@@ -179,7 +175,7 @@ struct SinglePosFormat1_3 : ValueBase
;
bool ret = bool (it);
SinglePos_serialize (c->serializer, this, it, &c->plan->layout_variation_idx_delta_map, new_format);
SinglePos_serialize (c->serializer, this, valueFormat, it, &c->plan->layout_variation_idx_delta_map, new_format);
return_trace (ret);
}
};
+25 -18
View File
@@ -7,6 +7,22 @@ namespace OT {
namespace Layout {
namespace GPOS_impl {
/* Keep the iterator type independent of the source table width so
* the output serializers can share their instantiations. */
struct single_pos_value_mapper_t
{
const hb_map_t &glyph_map;
const hb_array_t<const Value> &values;
const unsigned &sub_length;
hb_pair_t<hb_codepoint_t, hb_array_t<const Value>>
operator () (const hb_pair_t<hb_codepoint_t, unsigned> &_) const
{
return hb_pair (glyph_map[_.first],
values.sub_array (_.second * sub_length, sub_length));
}
};
template <typename Types>
struct SinglePosFormat2_4 : ValueBase
{
@@ -105,23 +121,17 @@ struct SinglePosFormat2_4 : ValueBase
if (likely (index == NOT_COVERED)) return false;
if (unlikely (index >= valueCount)) return false;
/* This is ugly... */
hb_buffer_t buffer {};
buffer.props.direction = direction;
OT::hb_ot_apply_context_t c (1, font, &buffer, table_blob);
valueFormat.apply_value (&c, this,
&values[index * valueFormat.get_len ()],
pos);
valueFormat.position_single (font, table_blob, direction, this,
&values[index * valueFormat.get_len ()], pos);
return true;
}
template<typename Iterator,
typename SrcLookup,
hb_requires (hb_is_iterator (Iterator))>
void serialize (hb_serialize_context_t *c,
const SrcLookup *src,
const ValueBase *src,
ValueFormat src_format,
Iterator it,
ValueFormat newFormat,
const hb_hashmap_t<unsigned, hb_pair_t<unsigned, int>> *layout_variation_idx_delta_map)
@@ -134,7 +144,7 @@ struct SinglePosFormat2_4 : ValueBase
+ it
| hb_map (hb_second)
| hb_apply ([&] (hb_array_t<const Value> _)
{ src->get_value_format ().copy_values (c, newFormat, src, &_, layout_variation_idx_delta_map); })
{ src_format.copy_values (c, newFormat, src, &_, layout_variation_idx_delta_map); })
;
auto glyphs =
@@ -188,12 +198,9 @@ struct SinglePosFormat2_4 : ValueBase
auto it =
+ hb_zip (this+coverage, hb_range ((unsigned) valueCount))
| hb_filter (glyphset, hb_first)
| hb_map_retains_sorting ([&] (const hb_pair_t<hb_codepoint_t, unsigned>& _)
{
return hb_pair (glyph_map[_.first],
values_array.sub_array (_.second * sub_length,
sub_length));
})
| hb_map_retains_sorting (single_pos_value_mapper_t {glyph_map,
values_array,
sub_length})
;
unsigned new_format = compute_effective_format (c->plan->source, it,
@@ -202,7 +209,7 @@ struct SinglePosFormat2_4 : ValueBase
c->plan->has_gdef_varstore,
&c->plan->layout_variation_idx_delta_map);
bool ret = bool (it);
SinglePos_serialize (c->serializer, this, it, &c->plan->layout_variation_idx_delta_map, new_format);
SinglePos_serialize (c->serializer, this, valueFormat, it, &c->plan->layout_variation_idx_delta_map, new_format);
return_trace (ret);
}
};
+15
View File
@@ -157,6 +157,21 @@ struct ValueFormat : HBUINT16
return ret;
}
void position_single (hb_font_t *font,
hb_blob_t *table_blob,
hb_direction_t direction,
const ValueBase *base,
const Value *values,
hb_glyph_position_t &pos) const
{
/* This is ugly... */
hb_buffer_t buffer {};
buffer.props.direction = direction;
hb_ot_apply_context_t c (1, font, &buffer, table_blob);
apply_value (&c, base, values, pos);
}
unsigned int get_effective_format (const Value *values, bool strip_hints, bool strip_empty, const ValueBase *base,
const hb_hashmap_t<unsigned, hb_pair_t<unsigned, int>> *varidx_delta_map) const
{
+93 -70
View File
@@ -7,6 +7,95 @@ namespace OT {
namespace Layout {
namespace GSUB_impl {
static inline bool
subset_single_subst (hb_subset_context_t *c,
const Coverage &coverage,
hb_codepoint_t d,
hb_codepoint_t mask)
{
const hb_set_t &glyphset = *c->plan->glyphset_gsub ();
const hb_map_t &glyph_map = *c->plan->glyph_map;
hb_set_t intersection;
coverage.intersect_set (glyphset, intersection);
auto it =
+ hb_iter (intersection)
| hb_map_retains_sorting ([d, mask] (hb_codepoint_t g) {
return hb_codepoint_pair_t (g,
(g + d) & mask); })
| hb_filter (glyphset, hb_second)
| hb_map_retains_sorting ([&] (hb_codepoint_pair_t p) -> hb_codepoint_pair_t
{ return hb_pair (glyph_map[p.first], glyph_map[p.second]); })
;
bool ret = bool (it);
SingleSubst_serialize (c->serializer, it);
return ret;
}
HB_INTERNAL inline
void single_subst_depend (hb_depend_context_t *c,
const Coverage &coverage,
hb_codepoint_t d, hb_codepoint_t mask)
{
unsigned num_glyphs = c->face->get_num_glyphs ();
// Filter coverage by active glyphs (like closure does)
hb_set_t intersection;
coverage.intersect_set (c->parent_active_glyphs (), intersection);
+ hb_iter (intersection)
| hb_map ([&] (hb_codepoint_t _) { return hb_codepoint_pair_t (_, (_ + d) & mask); })
| hb_filter ([num_glyphs] (const hb_codepoint_pair_t &_) { return _.second < num_glyphs; })
| hb_apply ([&] (const hb_codepoint_pair_t &_) { c->depend_data->add_gsub_lookup(_.first, c->lookup_index, _.second); })
;
}
HB_INTERNAL inline
void single_subst_closure (hb_closure_context_t *c,
const Coverage &coverage,
hb_codepoint_t d, hb_codepoint_t mask)
{
/* Help fuzzer avoid this function as much. */
unsigned pop = coverage.get_population ();
if (pop >= mask)
return;
hb_set_t intersection;
coverage.intersect_set (c->parent_active_glyphs (), intersection);
/* In degenerate fuzzer-found fonts, but not real fonts,
* this table can keep adding new glyphs in each round of closure.
* Refuse to close-over, if it maps glyph range to overlapping range. */
hb_codepoint_t min_before = intersection.get_min ();
hb_codepoint_t max_before = intersection.get_max ();
hb_codepoint_t min_after = (min_before + d) & mask;
hb_codepoint_t max_after = (max_before + d) & mask;
if (intersection.get_population () == max_before - min_before + 1 &&
((min_before <= min_after && min_after <= max_before) ||
(min_before <= max_after && max_after <= max_before)))
return;
+ hb_iter (intersection)
| hb_map ([d, mask] (hb_codepoint_t g) { return (g + d) & mask; })
| hb_sink (c->output)
;
}
HB_INTERNAL inline
void single_subst_collect_glyphs (hb_collect_glyphs_context_t *c,
const Coverage &coverage,
hb_codepoint_t d, hb_codepoint_t mask)
{
if (unlikely (!coverage.collect_coverage (c->input))) return;
+ hb_iter (coverage)
| hb_map ([d, mask] (hb_codepoint_t g) { return (g + d) & mask; })
| hb_sink (c->output)
;
}
template <typename Types>
struct SingleSubstFormat1_3
{
@@ -47,64 +136,19 @@ struct SingleSubstFormat1_3
void depend (hb_depend_context_t *c) const
{
hb_codepoint_t d = deltaGlyphID;
hb_codepoint_t mask = get_mask ();
unsigned num_glyphs = c->face->get_num_glyphs ();
// Filter coverage by active glyphs (like closure does)
hb_set_t intersection;
(this+coverage).intersect_set (c->parent_active_glyphs (), intersection);
+ hb_iter (intersection)
| hb_map ([&] (hb_codepoint_t _) { return hb_codepoint_pair_t (_, (_ + d) & mask); })
| hb_filter ([num_glyphs] (const hb_codepoint_pair_t &_) { return _.second < num_glyphs; })
| hb_apply ([&] (const hb_codepoint_pair_t &_) { c->depend_data->add_gsub_lookup(_.first, c->lookup_index, _.second); })
;
single_subst_depend (c, this+coverage, deltaGlyphID, get_mask ());
}
void closure (hb_closure_context_t *c) const
{
hb_codepoint_t d = deltaGlyphID;
hb_codepoint_t mask = get_mask ();
/* Help fuzzer avoid this function as much. */
unsigned pop = (this+coverage).get_population ();
if (pop >= mask)
return;
hb_set_t intersection;
(this+coverage).intersect_set (c->parent_active_glyphs (), intersection);
/* In degenerate fuzzer-found fonts, but not real fonts,
* this table can keep adding new glyphs in each round of closure.
* Refuse to close-over, if it maps glyph range to overlapping range. */
hb_codepoint_t min_before = intersection.get_min ();
hb_codepoint_t max_before = intersection.get_max ();
hb_codepoint_t min_after = (min_before + d) & mask;
hb_codepoint_t max_after = (max_before + d) & mask;
if (intersection.get_population () == max_before - min_before + 1 &&
((min_before <= min_after && min_after <= max_before) ||
(min_before <= max_after && max_after <= max_before)))
return;
+ hb_iter (intersection)
| hb_map ([d, mask] (hb_codepoint_t g) { return (g + d) & mask; })
| hb_sink (c->output)
;
single_subst_closure (c, this+coverage, deltaGlyphID, get_mask ());
}
void closure_lookups (hb_closure_lookups_context_t *c) const {}
void collect_glyphs (hb_collect_glyphs_context_t *c) const
{
if (unlikely (!(this+coverage).collect_coverage (c->input))) return;
hb_codepoint_t d = deltaGlyphID;
hb_codepoint_t mask = get_mask ();
+ hb_iter (this+coverage)
| hb_map ([d, mask] (hb_codepoint_t g) { return (g + d) & mask; })
| hb_sink (c->output)
;
single_subst_collect_glyphs (c, this+coverage, deltaGlyphID, get_mask ());
}
const Coverage &get_coverage () const { return this+coverage; }
@@ -203,28 +247,7 @@ struct SingleSubstFormat1_3
bool subset (hb_subset_context_t *c) const
{
TRACE_SUBSET (this);
const hb_set_t &glyphset = *c->plan->glyphset_gsub ();
const hb_map_t &glyph_map = *c->plan->glyph_map;
hb_codepoint_t d = deltaGlyphID;
hb_codepoint_t mask = get_mask ();
hb_set_t intersection;
(this+coverage).intersect_set (glyphset, intersection);
auto it =
+ hb_iter (intersection)
| hb_map_retains_sorting ([d, mask] (hb_codepoint_t g) {
return hb_codepoint_pair_t (g,
(g + d) & mask); })
| hb_filter (glyphset, hb_second)
| hb_map_retains_sorting ([&] (hb_codepoint_pair_t p) -> hb_codepoint_pair_t
{ return hb_pair (glyph_map[p.first], glyph_map[p.second]); })
;
bool ret = bool (it);
SingleSubst_serialize (c->serializer, it);
return_trace (ret);
return_trace (subset_single_subst (c, this+coverage, deltaGlyphID, get_mask ()));
}
};