diff --git a/src/OT/Layout/GPOS/AnchorMatrix.hh b/src/OT/Layout/GPOS/AnchorMatrix.hh index 939442046..7d213ba29 100644 --- a/src/OT/Layout/GPOS/AnchorMatrix.hh +++ b/src/OT/Layout/GPOS/AnchorMatrix.hh @@ -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 &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 struct AnchorMatrix { diff --git a/src/OT/Layout/GPOS/Common.hh b/src/OT/Layout/GPOS/Common.hh index 696d25d75..701b1896b 100644 --- a/src/OT/Layout/GPOS/Common.hh +++ b/src/OT/Layout/GPOS/Common.hh @@ -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 +struct ValueBase; +struct ValueFormat; + +template static void SinglePos_serialize (hb_serialize_context_t *c, - const SrcLookup *src, + const ValueBase *src, + ValueFormat src_format, Iterator it, const hb_hashmap_t> *layout_variation_idx_delta_map, unsigned new_format); diff --git a/src/OT/Layout/GPOS/CursivePosFormat1.hh b/src/OT/Layout/GPOS/CursivePosFormat1.hh index 1505f30dd..e37d240b5 100644 --- a/src/OT/Layout/GPOS/CursivePosFormat1.hh +++ b/src/OT/Layout/GPOS/CursivePosFormat1.hh @@ -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 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 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 struct MarkArray : Types::template ArrayOf> /* Array of MarkRecords--in Coverage order */ @@ -50,7 +101,6 @@ struct MarkArray : Types::template ArrayOf> unsigned int glyph_pos) const { TRACE_APPLY (this); - hb_buffer_t *buffer = c->buffer; const MarkRecord &record = Super::operator[](mark_index); unsigned int mark_class = record.klass; @@ -61,48 +111,7 @@ struct MarkArray : Types::template ArrayOf> * 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 > } }; -template HB_INTERNAL inline -void Markclass_closure_and_remap_indexes (const Coverage &mark_coverage, - const MarkArray &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::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 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 +HB_INTERNAL inline +void Markclass_closure_and_remap_indexes (const Coverage &mark_coverage, + const MarkArray &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::static_size, + glyphset, klass_mapping); +} + } } } diff --git a/src/OT/Layout/GPOS/MarkBasePosFormat1.hh b/src/OT/Layout/GPOS/MarkBasePosFormat1.hh index 9f8dc7354..0bf5b03f0 100644 --- a/src/OT/Layout/GPOS/MarkBasePosFormat1.hh +++ b/src/OT/Layout/GPOS/MarkBasePosFormat1.hh @@ -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 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 ()); } diff --git a/src/OT/Layout/GPOS/MarkMarkPosFormat1.hh b/src/OT/Layout/GPOS/MarkMarkPosFormat1.hh index 9b546f3bb..4ef32f3c9 100644 --- a/src/OT/Layout/GPOS/MarkMarkPosFormat1.hh +++ b/src/OT/Layout/GPOS/MarkMarkPosFormat1.hh @@ -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 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 ()); } diff --git a/src/OT/Layout/GPOS/PairPosFormat2.hh b/src/OT/Layout/GPOS/PairPosFormat2.hh index eb7573dd2..fa07a2f45 100644 --- a/src/OT/Layout/GPOS/PairPosFormat2.hh +++ b/src/OT/Layout/GPOS/PairPosFormat2.hh @@ -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 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 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 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 diff --git a/src/OT/Layout/GPOS/SinglePos.hh b/src/OT/Layout/GPOS/SinglePos.hh index 31c2b4881..b500d6a3b 100644 --- a/src/OT/Layout/GPOS/SinglePos.hh +++ b/src/OT/Layout/GPOS/SinglePos.hh @@ -37,10 +37,10 @@ struct SinglePos } template 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> *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 +template static void SinglePos_serialize (hb_serialize_context_t *c, - const SrcLookup *src, + const ValueBase *src, + ValueFormat src_format, Iterator it, const hb_hashmap_t> *layout_variation_idx_delta_map, unsigned new_format) -{ c->start_embed ()->serialize (c, src, it, layout_variation_idx_delta_map, new_format); } +{ c->start_embed ()->serialize (c, src, src_format, it, layout_variation_idx_delta_map, new_format); } } diff --git a/src/OT/Layout/GPOS/SinglePosFormat1.hh b/src/OT/Layout/GPOS/SinglePosFormat1.hh index 05d5e4d5f..daf276cdf 100644 --- a/src/OT/Layout/GPOS/SinglePosFormat1.hh +++ b/src/OT/Layout/GPOS/SinglePosFormat1.hh @@ -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 void serialize (hb_serialize_context_t *c, - const SrcLookup *src, + const ValueBase *src, + ValueFormat src_format, Iterator it, ValueFormat newFormat, const hb_hashmap_t> *layout_variation_idx_delta_map) @@ -125,7 +121,7 @@ struct SinglePosFormat1_3 : ValueBase for (const hb_array_t& _ : + 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); } }; diff --git a/src/OT/Layout/GPOS/SinglePosFormat2.hh b/src/OT/Layout/GPOS/SinglePosFormat2.hh index 5af4361d4..c84ae5a47 100644 --- a/src/OT/Layout/GPOS/SinglePosFormat2.hh +++ b/src/OT/Layout/GPOS/SinglePosFormat2.hh @@ -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 &values; + const unsigned &sub_length; + + hb_pair_t> + operator () (const hb_pair_t &_) const + { + return hb_pair (glyph_map[_.first], + values.sub_array (_.second * sub_length, sub_length)); + } +}; + template 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 void serialize (hb_serialize_context_t *c, - const SrcLookup *src, + const ValueBase *src, + ValueFormat src_format, Iterator it, ValueFormat newFormat, const hb_hashmap_t> *layout_variation_idx_delta_map) @@ -134,7 +144,7 @@ struct SinglePosFormat2_4 : ValueBase + it | hb_map (hb_second) | hb_apply ([&] (hb_array_t _) - { 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& _) - { - 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); } }; diff --git a/src/OT/Layout/GPOS/ValueFormat.hh b/src/OT/Layout/GPOS/ValueFormat.hh index 08e55cbc6..1357afb49 100644 --- a/src/OT/Layout/GPOS/ValueFormat.hh +++ b/src/OT/Layout/GPOS/ValueFormat.hh @@ -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> *varidx_delta_map) const { diff --git a/src/OT/Layout/GSUB/SingleSubstFormat1.hh b/src/OT/Layout/GSUB/SingleSubstFormat1.hh index 5cdbb2d68..9683dc4f6 100644 --- a/src/OT/Layout/GSUB/SingleSubstFormat1.hh +++ b/src/OT/Layout/GSUB/SingleSubstFormat1.hh @@ -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 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 ())); } };