dymokomi/luce-fonts
Source of release 0.7.1
0.7.1 / src / shaping / position.lucb
#==============================================================================================
#
# position - OpenType positioning for the shaper: GPOS and the legacy kern table
#
# DESCRIPTION:
# The positioning half of HarfBuzz's hb-ot-layout for the default shaper (the plan and GSUB are in layout.lucb):
#
# - GPOS lookups 1 (single adjustment), 2 (pair adjustment), 4, 5 and 6 (mark to base,
# to ligature and to mark), 7 and 8 (contextual, in context.lucb) and 9
# (extension), with the lookup flags, values scaled as hb_font_t scales them, and
# the propagation of mark attachments into offsets (position_finish_offsets);
# - the legacy 'kern' table (Microsoft format 0) when GPOS has no kern feature, applied
# as hb_kern_machine_t does;
# - the AAT 'trak' table (tracking by point size), which HarfBuzz applies to any font
# that has one.
#
# FIXME: cursive attachment (GPOS 3), device tables and the ligature-component
# bookkeeping of mark-to-ligature (a mark attaches to the last component) are not
# implemented.
#
#==============================================================================================
# mark: GPOS and kern ===========================================================================
## Applies the plan's GPOS lookups, or the kern table.
func hb_layout_position(font: const HbFontT*, face: const opentype.SfntFace*, plan: const HbLayoutPlan*, buffer: HbBufferT*):
for planned in plan.gpos_lookups.view():
gpos_apply_lookup(font, face, planned, buffer)
if plan.apply_kern_table:
kern_table_apply(font, face, buffer)
if plan.apply_trak:
trak_apply(font, face, buffer)
# mark: AAT tracking ============================================================================
## TrackData::get_tracking (hb-aat-layout-trak-table.hh): the zero track's value at `ptem`,
## interpolated between the table's sizes.
func trak_get_tracking(trak: const u8[], track_data: usize, ptem: f32) -> i32:
let track_count = (usize)opentype.sfnt_u16(trak, track_data)
let sizes = (usize)opentype.sfnt_u16(trak, track_data + 2)
let size_table = (usize)opentype.sfnt_u32(trak, track_data + 4)
# Choose track: for now we only seek for track entries with zero tracking value.
var entry: usize? = none
for index in 0..<track_count:
let record = track_data + 8 + index * 8
if opentype.sfnt_i32(trak, record) == 0:
entry = record
break
let track = entry else return 0
let values = (usize)opentype.sfnt_u16(trak, track + 6)
# Choose size.
if sizes == 0:
return 0
if sizes == 1:
return (i32)opentype.sfnt_i16(trak, values)
var size_index: usize = 0
while size_index < sizes - 1:
if (f32)opentype.sfnt_i32(trak, size_table + size_index * 4) / 65536.0 >= ptem:
break
size_index += 1
# interpolate_at
let idx = size_index - 1 if size_index > 0 else 0
let s0 = (f32)opentype.sfnt_i32(trak, size_table + idx * 4) / 65536.0
let s1 = (f32)opentype.sfnt_i32(trak, size_table + (idx + 1) * 4) / 65536.0
let t: f32 = 0.0 if s0 == s1 else (ptem - s0) / (s1 - s0)
let value = t * (f32)opentype.sfnt_i16(trak, values + (idx + 1) * 2) + (1.0 - t) * (f32)opentype.sfnt_i16(trak, values + idx * 2)
return (i32)math32.round(value)
## trak::apply for horizontal text: each grapheme's first glyph gets the tracking as advance
## and half of it as offset.
func trak_apply(font: const HbFontT*, face: const opentype.SfntFace*, buffer: HbBufferT*):
let trak = opentype.sfnt_face_table(face, opentype.sfnt_tag('t', 'r', 'a', 'k')) else return
let ptem = font.ptem
if ptem <= 0.0:
return
let horizontal = (usize)opentype.sfnt_u16(trak, 6)
if horizontal == 0:
return
let tracking = trak_get_tracking(trak, horizontal, ptem)
let x_multf = (f32)font.x_scale / (f32)hb_face_units_per_em(font.face)
let offset_to_add = (i32)math32.round((f32)(tracking // 2) * x_multf)
let advance_to_add = (i32)math32.round((f32)tracking * x_multf)
let infos = buffer.infos.view()
let positions = buffer.positions.view()
for index in 0..<infos.length:
# foreach_grapheme: the first glyph of each grapheme.
if index > 0 and infos[index].is_continuation:
continue
positions[index].x_advance += advance_to_add
positions[index].x_offset += offset_to_add
func gpos_apply_lookup(font: const HbFontT*, face: const opentype.SfntFace*, planned: HbPlanLookup, buffer: HbBufferT*):
let lookup = ot_lookup(face.gpos, planned.index, planned.auto_zwj, planned.auto_zwnj, true) else return
let subtable_count = (usize)opentype.sfnt_u16(face.gpos, lookup.offset + 4)
var index: usize = 0
let count = buffer.infos.length()
while index < count:
let info = *buffer.infos.at(index)
if ot_lookup_skips(face, &lookup, &info):
index += 1
continue
var next = index + 1
for subtable_index in 0..<subtable_count:
let (lookup_type, subtable) = ot_lookup_subtable(&lookup, subtable_index, 9)
if let applied = gpos_apply_subtable(font, face, &lookup, lookup_type, subtable, buffer, index, 0):
next = applied
break
index = next
## The size of a ValueRecord of `value_format`.
func gpos_value_size(value_format: u16) -> usize:
var size: usize = 0
var bits = value_format
while bits != 0:
size += (usize)(bits & 1) * 2
bits >>= 1
return size
## ValueFormat::apply_value for horizontal text (device tables are not applied: HarfBuzz
## only reads them with a ppem or variations set).
func gpos_apply_value(font: const HbFontT*, table: const u8[], value_format: u16, offset: usize, position: HbGlyphPosition*):
var cursor = offset
if (value_format & 0x01) != 0:
position.x_offset += hb_font_em_scale_x(font, opentype.sfnt_i16(table, cursor))
cursor += 2
if (value_format & 0x02) != 0:
position.y_offset += (i32)(((i64)opentype.sfnt_i16(table, cursor) * font.y_mult + 32768) >> 16)
cursor += 2
if (value_format & 0x04) != 0:
position.x_advance += hb_font_em_scale_x(font, opentype.sfnt_i16(table, cursor))
cursor += 2
func gpos_apply_subtable(font: const HbFontT*, face: const opentype.SfntFace*, lookup: const OtLookup*, lookup_type: u16, subtable: usize, buffer: HbBufferT*, index: usize, nesting: u32) -> usize?:
let gpos = face.gpos
# Contextual lookups find their own coverage.
if lookup_type == 7:
return ot_apply_context(face, font, true, lookup, subtable, buffer, index, nesting)
if lookup_type == 8:
return ot_apply_chain_context(face, font, true, lookup, subtable, buffer, index, nesting)
let glyph = buffer.infos.at(index).codepoint
let coverage = ot_coverage_index(gpos, subtable + (usize)opentype.sfnt_u16(gpos, subtable + 2), glyph) else return none
let subtable_format = opentype.sfnt_u16(gpos, subtable)
match lookup_type:
1:
let value_format = opentype.sfnt_u16(gpos, subtable + 4)
var value = subtable + 6
if subtable_format == 2:
if coverage >= (usize)opentype.sfnt_u16(gpos, subtable + 6):
return none
value = subtable + 8 + coverage * gpos_value_size(value_format)
gpos_apply_value(font, gpos, value_format, value, buffer.positions.at(index))
return index + 1
2:
let second_index = ot_next_glyph(face, lookup, buffer.infos.view(), index) else return none
let second = buffer.infos.at(second_index).codepoint
let value_format1 = opentype.sfnt_u16(gpos, subtable + 4)
let value_format2 = opentype.sfnt_u16(gpos, subtable + 6)
let size1 = gpos_value_size(value_format1)
let size2 = gpos_value_size(value_format2)
var record: usize? = none
if subtable_format == 1:
if coverage >= (usize)opentype.sfnt_u16(gpos, subtable + 8):
return none
let pair_set = subtable + (usize)opentype.sfnt_u16(gpos, subtable + 10 + coverage * 2)
let pair_count = (usize)opentype.sfnt_u16(gpos, pair_set)
let record_size = 2 + size1 + size2
var low: usize = 0
var high = pair_count
while low < high:
let middle = low + (high - low) // 2
let candidate = pair_set + 2 + middle * record_size
let candidate_glyph = (u32)opentype.sfnt_u16(gpos, candidate)
if second < candidate_glyph:
high = middle
elif second > candidate_glyph:
low = middle + 1
else:
record = candidate + 2
break
elif subtable_format == 2:
let class1 = (usize)ot_class_of(gpos, subtable + (usize)opentype.sfnt_u16(gpos, subtable + 8), glyph)
let class2 = (usize)ot_class_of(gpos, subtable + (usize)opentype.sfnt_u16(gpos, subtable + 10), second)
let class1_count = (usize)opentype.sfnt_u16(gpos, subtable + 12)
let class2_count = (usize)opentype.sfnt_u16(gpos, subtable + 14)
if class1 >= class1_count or class2 >= class2_count:
return none
record = subtable + 16 + (class1 * class2_count + class2) * (size1 + size2)
let values = record else return none
gpos_apply_value(font, gpos, value_format1, values, buffer.positions.at(index))
gpos_apply_value(font, gpos, value_format2, values + size1, buffer.positions.at(second_index))
# With a second value record the pair is consumed; otherwise the second glyph can
# start the next pair.
if value_format2 != 0:
return second_index + 1
return second_index
4, 5:
return gpos_apply_mark_to_base(font, face, lookup, lookup_type, subtable, coverage, buffer, index)
6:
return gpos_apply_mark_to_mark(font, face, lookup, subtable, coverage, buffer, index)
_:
# FIXME: cursive attachment (3) is not applied.
return none
## The legacy kern table (hb-ot-kern-table.hh, Microsoft format 0), applied as
## hb_kern_machine_t does: half of each pair's kerning to each glyph.
func kern_table_apply(font: const HbFontT*, face: const opentype.SfntFace*, buffer: HbBufferT*):
let kern = face.kern
if opentype.sfnt_u16(kern, 0) != 0:
# FIXME: Apple's kern table (version 1.0) is not read.
return
let table_count = (usize)opentype.sfnt_u16(kern, 2)
var offset: usize = 4
for _table in 0..<table_count:
let length = (usize)opentype.sfnt_u16(kern, offset + 2)
let coverage = opentype.sfnt_u16(kern, offset + 4)
# Horizontal (bit 0), not minimum (bit 1) or cross-stream (bit 2), format 0.
if (coverage & 0x07) == 0x01 and (coverage >> 8) == 0:
kern_table_apply_subtable(font, kern, offset + 6, buffer)
if length == 0:
break
offset += length
func kern_table_apply_subtable(font: const HbFontT*, kern: const u8[], pairs_header: usize, buffer: HbBufferT*):
let pair_count = (usize)opentype.sfnt_u16(kern, pairs_header)
let infos = buffer.infos.view()
let positions = buffer.positions.view()
if infos.length < 2:
return
for index in 0..<infos.length - 1:
let key = (infos[index].codepoint << 16) | infos[index + 1].codepoint
var low: usize = 0
var high = pair_count
while low < high:
let middle = low + (high - low) // 2
let pair = pairs_header + 8 + middle * 6
let candidate = opentype.sfnt_u32(kern, pair)
if key < candidate:
high = middle
elif key > candidate:
low = middle + 1
else:
let value = hb_font_em_scale_x(font, opentype.sfnt_i16(kern, pair + 4))
let kern1 = value >> 1
let kern2 = value - kern1
positions[index].x_advance += kern1
positions[index + 1].x_advance += kern2
positions[index + 1].x_offset += kern2
break
# mark: Mark attachment =========================================================================
## Anchor::get_anchor: the anchor at `offset` in the font's scale as floats (em_fscale; the
## contour point of format 2 and the device tables of format 3 need a ppem HarfBuzz is not
## given).
func gpos_anchor(font: const HbFontT*, table: const u8[], offset: usize) -> (f32, f32):
let x_multf = (f32)font.x_scale / (f32)hb_face_units_per_em(font.face)
let y_multf = (f32)font.y_scale / (f32)hb_face_units_per_em(font.face)
return ((f32)opentype.sfnt_i16(table, offset + 2) * x_multf, (f32)opentype.sfnt_i16(table, offset + 4) * y_multf)
## MarkArray::apply: attaches the mark at `mark_position` to the glyph at `base_position`
## through the anchors, or answers false when the base has no anchor for the mark's class.
func gpos_mark_array_apply(font: const HbFontT*, table: const u8[], mark_array: usize, mark_index: usize, anchor_matrix: usize, rows: usize, row: usize, class_count: usize, base_position: usize, mark_position: usize, buffer: HbBufferT*) -> bool:
if mark_index >= (usize)opentype.sfnt_u16(table, mark_array):
return false
let record = mark_array + 2 + mark_index * 4
let mark_class = (usize)opentype.sfnt_u16(table, record)
let mark_anchor = mark_array + (usize)opentype.sfnt_u16(table, record + 2)
# AnchorMatrix::get_anchor
if row >= rows or mark_class >= class_count:
return false
let anchor_offset = (usize)opentype.sfnt_u16(table, anchor_matrix + 2 + (row * class_count + mark_class) * 2)
if anchor_offset == 0:
return false
let (mark_x, mark_y) = gpos_anchor(font, table, mark_anchor)
let (base_x, base_y) = gpos_anchor(font, table, anchor_matrix + anchor_offset)
let o = buffer.positions.at(mark_position)
o.x_offset = (i32)math32.round(base_x - mark_x)
o.y_offset = (i32)math32.round(base_y - mark_y)
o.attach_type = hb_attach_type_mark
o.attach_chain = (i32)base_position - (i32)mark_position
return true
## The glyph a mark attaches to: the nearest glyph before it that the skipping iterator
## matches with IgnoreMarks as the lookup props.
func gpos_find_base(face: const opentype.SfntFace*, lookup: const OtLookup*, buffer: const HbBufferT*, index: usize) -> usize?:
let infos = buffer.infos.view()
var props = *lookup
props.flags = 0x08
var j = index
while j > 0:
j -= 1
if ot_may_skip(face, &props, &infos[j], false) != 0:
continue
return j
return none
## MarkBasePos (4) and MarkLigPos (5), format 1.
func gpos_apply_mark_to_base(font: const HbFontT*, face: const opentype.SfntFace*, lookup: const OtLookup*, lookup_type: u16, subtable: usize, mark_index: usize, buffer: HbBufferT*, index: usize) -> usize?:
let gpos = face.gpos
if opentype.sfnt_u16(gpos, subtable) != 1:
return none
let base_index = gpos_find_base(face, lookup, buffer, index) else return none
let base_glyph = buffer.infos.at(base_index).codepoint
let base_coverage = ot_coverage_index(gpos, subtable + (usize)opentype.sfnt_u16(gpos, subtable + 4), base_glyph) else return none
let class_count = (usize)opentype.sfnt_u16(gpos, subtable + 6)
let mark_array = subtable + (usize)opentype.sfnt_u16(gpos, subtable + 8)
let second_array = subtable + (usize)opentype.sfnt_u16(gpos, subtable + 10)
if lookup_type == 4:
let rows = (usize)opentype.sfnt_u16(gpos, second_array)
if not gpos_mark_array_apply(font, gpos, mark_array, mark_index, second_array, rows, base_coverage, class_count, base_index, index, buffer):
return none
return index + 1
# MarkLigPos: the ligature's attach table; the mark takes the last component.
if base_coverage >= (usize)opentype.sfnt_u16(gpos, second_array):
return none
let ligature_attach = second_array + (usize)opentype.sfnt_u16(gpos, second_array + 2 + base_coverage * 2)
let component_count = (usize)opentype.sfnt_u16(gpos, ligature_attach)
if component_count == 0:
return none
if not gpos_mark_array_apply(font, gpos, mark_array, mark_index, ligature_attach, component_count, component_count - 1, class_count, base_index, index, buffer):
return none
return index + 1
## MarkMarkPos (6), format 1: the mark before, skipping what the lookup's mark filtering and
## attachment type skip, must be a mark covered by the subtable.
func gpos_apply_mark_to_mark(font: const HbFontT*, face: const opentype.SfntFace*, lookup: const OtLookup*, subtable: usize, mark1_index: usize, buffer: HbBufferT*, index: usize) -> usize?:
let gpos = face.gpos
if opentype.sfnt_u16(gpos, subtable) != 1:
return none
let infos = buffer.infos.view()
# The lookup's props without the ignore flags (skippy_iter.prev).
var props = *lookup
props.flags = lookup.flags & ~(u16)0x0E
var j = index
var found = false
while j > 0:
j -= 1
if ot_may_skip(face, &props, &infos[j], false) != 0:
continue
found = true
break
if not found or infos[j].glyph_class != 3:
return none
let mark2_index = ot_coverage_index(gpos, subtable + (usize)opentype.sfnt_u16(gpos, subtable + 4), infos[j].codepoint) else return none
let class_count = (usize)opentype.sfnt_u16(gpos, subtable + 6)
let mark1_array = subtable + (usize)opentype.sfnt_u16(gpos, subtable + 8)
let mark2_array = subtable + (usize)opentype.sfnt_u16(gpos, subtable + 10)
let rows = (usize)opentype.sfnt_u16(gpos, mark2_array)
if not gpos_mark_array_apply(font, gpos, mark1_array, mark1_index, mark2_array, rows, mark2_index, class_count, j, index, buffer):
return none
return index + 1
## propagate_attachment_offsets (hb-ot-layout-gpos-table.hh): a mark's offsets become relative
## to its own pen position, following chains of attachments.
func hb_propagate_attachment_offsets(buffer: HbBufferT*, i: usize, nesting_level: u32):
let positions = buffer.positions.view()
let chain = positions[i].attach_chain
if chain == 0:
return
positions[i].attach_chain = 0
let target = (isize)i + (isize)chain
if target < 0 or (usize)target >= positions.length:
return
if nesting_level == 0:
return
let j = (usize)target
hb_propagate_attachment_offsets(buffer, j, nesting_level - 1)
positions[i].x_offset += positions[j].x_offset
positions[i].y_offset += positions[j].y_offset
if j >= i:
return
if buffer.direction != HbDirection.rtl:
for k in j..<i:
positions[i].x_offset -= positions[k].x_advance
positions[i].y_offset -= positions[k].y_advance
else:
for k in j + 1..<i + 1:
positions[i].x_offset += positions[k].x_advance
positions[i].y_offset += positions[k].y_advance
## hb_ot_layout_position_finish_offsets.
func hb_position_finish_offsets(buffer: HbBufferT*):
for i in 0..<buffer.positions.length():
hb_propagate_attachment_offsets(buffer, i, hb_max_nesting_level)