1// Glyph positioning (GPOS): single and pair adjustments, cursive2// attachment, marks attached to bases, ligatures and other marks,3// contextual and chained contextual lookups, through extensions, applied4// as HarfBuzz applies them. Attached glyphs keep what they attach to,5// resolved into offsets once every lookup ran (`resolve_attachments`).67import std::vector;8import std::option;9import std::math;1011import font::reader;12import font::face;13import opentype;14import buffer;15import apply;16import context;1718// Applies the lookup to every glyph its mask selects.19exp fn position_lookup(c: &var Apply, buffer: &var Buffer, index: u32) {20 const d = &c.data;21 const table = if (c.face.gpos is ::Some |t|) t else {22 return;23 };24 const at = if (lookup_at(&d, table, index) is ::Some |a|) a else {25 return;26 };27 const lookup = Lookup::read(&d, at);28 c.lookup_index = index;29 c.lookup_props = lookup.props();30 c.last_base = ::None;31 c.last_base_until = 0;32 const row = &c.face.gpos_sets.row(index);33 c.at = 0;34 while (c.at < buffer.infos.length()) {35 const info = &buffer.infos.get(c.at);36 if (37 has_glyph(&row, info.glyph)38 && (info.mask & c.lookup_mask) != 039 && c.sees(&info, c.lookup_props)40 && apply_subtables(&var c, &var buffer, &lookup)41 ) {42 continue;43 }44 c.at += 1;45 }46}4748fn nested(c: &var Apply, buffer: &var Buffer, index: u32) -> bool {49 const d = &c.data;50 const table = if (c.face.gpos is ::Some |t|) t else {51 return false;52 };53 const at = if (lookup_at(&d, table, index) is ::Some |a|) a else {54 return false;55 };56 const lookup = Lookup::read(&d, at);57 const saved_index = c.lookup_index;58 const saved_props = c.lookup_props;59 c.lookup_index = index;60 c.lookup_props = lookup.props();61 const applied = c.face.gpos_sets.may_apply(index, buffer.infos.get(c.at).glyph)62 && apply_subtables(&var c, &var buffer, &lookup);63 c.lookup_index = saved_index;64 c.lookup_props = saved_props;65 return applied;66}6768fn apply_subtables(c: &var Apply, buffer: &var Buffer, lookup: &Lookup) -> bool {69 const d = &c.data;70 // the subtables' digests passing over those that cannot apply, but a71 // lookup's one subtable, which its row has checked72 const digests = &c.face.gpos_sets.digests(c.lookup_index, if (c.digests) lookup.count else 0);73 for ([..lookup.count]) |k| {74 if (75 k * 3 < digests.length()76 && !digest_has(&digests, k * 3, buffer.infos.get(c.at).glyph)77 ) {78 continue;79 }80 const s = lookup.subtable(&d, k, LookupTable::Positioning);81 if (apply_subtable(&var c, &var buffer, s.kind, s.at)) {82 return true;83 }84 }85 return false;86}8788fn apply_subtable(c: &var Apply, buffer: &var Buffer, kind: u32, at: u64) -> bool {89 match (kind) {90 CONTEXT_POSITIONING, CHAINED_CONTEXT_POSITIONING {91 return apply_context(92 &var c,93 &var buffer,94 at,95 kind == CHAINED_CONTEXT_POSITIONING,96 nested97 );98 }99 }100 const d = &c.data;101 const index = if (102 coverage(&d, at + u16_at(&d, at + 2) to u64, buffer.infos.get(c.at).glyph) is ::Some |k|103 ) k to u64 else {104 return false;105 };106 match (kind) {107 SINGLE_ADJUSTMENT {108 // format 1 one value for every glyph, 2 one each109 const format = u16_at(&d, at + 4);110 var record = at + 6;111 if (u16_at(&d, at) != 1) {112 if (index >= u16_at(&d, at + 6) to u64) {113 return false;114 }115 record = at + 8 + index * value_size(format);116 }117 add_value(&c.face, at, record, format, &var buffer.positions.get_var(c.at));118 c.at += 1;119 return true;120 }121 PAIR_ADJUSTMENT {122 return position_pair(&var c, &var buffer, at, index);123 }124 CURSIVE_ATTACHMENT_LOOKUP {125 return attach_cursive(&var c, &var buffer, at, index);126 }127 MARK_TO_BASE {128 return attach_to_base(&var c, &var buffer, at, index);129 }130 MARK_TO_LIGATURE {131 return attach_to_ligature(&var c, &var buffer, at, index);132 }133 MARK_TO_MARK {134 return attach_to_mark(&var c, &var buffer, at, index);135 }136 }137 return false;138}139140// pair adjustment formats: pairs by their glyphs, by their classes141const PAIR_GLYPHS: u32 = 1;142const PAIR_CLASSES: u32 = 2;143144// A pair adjustment of the current glyph, covered at `index`, and the145// next one the lookup sees.146fn position_pair(c: &var Apply, buffer: &var Buffer, at: u64, index: u64) -> bool {147 const d = &c.data;148 const m = c.matcher(false);149 const infos = &buffer.infos.view();150 const j = if (151 m.next_match(&c, &infos, c.at, infos.length(), &Items::none(), 0) is ::Some |n|152 ) n else {153 return false;154 };155 const second = infos[j].glyph;156 const format1 = u16_at(&d, at + 4);157 const format2 = u16_at(&d, at + 6);158 const size1 = value_size(format1);159 const size2 = value_size(format2);160 var record: u64 = 0;161 var base = at; // what the value records' device tables are from162 match (u16_at(&d, at)) {163 PAIR_GLYPHS {164 // the second glyph among the first one's pairs165 if (index >= u16_at(&d, at + 8) to u64) {166 return false;167 }168 const set = at + u16_at(&d, at + 10 + index * 2) to u64;169 base = set;170 const stride = 2 + size1 + size2;171 var low: u64 = 0;172 var high = u16_at(&d, set) to u64;173 while (true) {174 if (low >= high) {175 return false;176 }177 const middle = (low + high) / 2;178 const pair = set + 2 + middle * stride;179 const g = u16_at(&d, pair);180 if (g < second) {181 low = middle + 1;182 } else if (g > second) {183 high = middle;184 } else {185 record = pair + 2;186 break;187 }188 }189 }190 PAIR_CLASSES {191 const class1 = class_of(&d, at + u16_at(&d, at + 8) to u64, infos[c.at].glyph) to u64;192 const class2 = class_of(&d, at + u16_at(&d, at + 10) to u64, second) to u64;193 const count1 = u16_at(&d, at + 12) to u64;194 const count2 = u16_at(&d, at + 14) to u64;195 if (class1 >= count1 || class2 >= count2) {196 return false;197 }198 record = at + 16 + (class1 * count2 + class2) * (size1 + size2);199 }200 else {201 return false;202 }203 }204 add_value(&c.face, base, record, format1, &var buffer.positions.get_var(c.at));205 add_value(&c.face, base, record + size1, format2, &var buffer.positions.get_var(j));206 c.at = if (format2 != 0) j + 1 else j;207 return true;208}209210// The current glyph's entry joined to the exit of the glyph before it211// the lookup sees (HarfBuzz's CursivePosFormat1): advances made to end212// at the joins, and the child (in the lookup's direction) attached to its213// parent across the line.214fn attach_cursive(c: &var Apply, buffer: &var Buffer, at: u64, index: u64) -> bool {215 const d = &c.data;216 const count = u16_at(&d, at + 4) to u64;217 if (index >= count) {218 return false;219 }220 const entry = u16_at(&d, at + 6 + index * 4) to u64;221 if (entry == 0) {222 return false;223 }224 const m = c.matcher(false);225 const infos = &buffer.infos.view();226 const i = if (m.previous_match(&c, &infos, c.at, &Items::none(), 0) is ::Some |p|) p else {227 return false;228 };229 const previous = if (230 coverage(&d, at + u16_at(&d, at + 2) to u64, infos[i].glyph) is ::Some |k|231 ) k to u64 else {232 return false;233 };234 if (previous >= count) {235 return false;236 }237 const exit = u16_at(&d, at + 6 + previous * 4 + 2) to u64;238 if (exit == 0) {239 return false;240 }241 const j = c.at;242 const exit_point = anchor_point(&c.face, at + exit);243 const entry_point = anchor_point(&c.face, at + entry);244 var positions = &var buffer.positions.items();245 if (c.rtl) {246 const d0 = round(exit_point[0]) + positions[i].x_offset;247 positions[i].x_advance -= d0;248 positions[i].x_offset -= d0;249 positions[j].x_advance = round(entry_point[0]) + positions[j].x_offset;250 } else {251 positions[i].x_advance = round(exit_point[0]) + positions[i].x_offset;252 const d0 = round(entry_point[0]) + positions[j].x_offset;253 positions[j].x_advance -= d0;254 positions[j].x_offset -= d0;255 }256 var child = i;257 var parent = j;258 var y_offset = round(entry_point[1] - exit_point[1]);259 if ((c.lookup_props & RIGHT_TO_LEFT) == 0) {260 child = j;261 parent = i;262 y_offset = -y_offset;263 }264 // a child attached before has its old chain reversed onto it265 reverse_cursive_chain(&var positions, child, parent);266 positions[child].chain = parent to i32 - child to i32;267 positions[child].attach = CURSIVE_ATTACHMENT;268 positions[child].y_offset = y_offset;269 buffer.has_attachments = true;270 // a parent attached to its child is let go271 if (positions[parent].chain == -positions[child].chain) {272 positions[parent].chain = 0;273 positions[parent].y_offset = 0.0;274 }275 c.at += 1;276 return true;277}278279fn reverse_cursive_chain(positions: &var [GlyphPosition], i: u64, new_parent: u64) {280 const chain = positions[i].chain;281 const kind = positions[i].attach;282 if (chain == 0 || (kind & CURSIVE_ATTACHMENT) == 0) {283 return;284 }285 positions[i].chain = 0;286 const j = (i to i64 + chain to i64) to u64;287 if (j >= positions.length() || j == new_parent) {288 return;289 }290 reverse_cursive_chain(&var positions, j, new_parent);291 positions[j].y_offset = -positions[i].y_offset;292 positions[j].chain = -chain;293 positions[j].attach = kind;294}295296// Whether a mark may attach to the glyph at `k`: the first of the glyphs297// a multiple substitution made, or another before a mark of them.298fn accepts_base(infos: &[GlyphInfo], k: u64) -> bool {299 const info = &infos[k];300 return !info.multiplied() || info.ligature_component() == 0 || k == 0 || infos[k - 1].is_mark()301 || !infos[k - 1].multiplied()302 || info.ligature_id() != infos[k - 1].ligature_id()303 || info.ligature_component() != infos[k - 1].ligature_component() + 1;304}305306// The base a mark at the current glyph attaches to: the nearest before307// it that is no mark, as the lookup sees it, found once per run of marks.308fn find_base(c: &var Apply, infos: &[GlyphInfo], base_coverage: Option<u64>) -> Option<u64> {309 if (c.last_base_until > c.at) {310 c.last_base_until = 0;311 c.last_base = ::None;312 }313 var m = c.matcher(false);314 m.lookup_props = IGNORE_MARKS;315 const d = &c.data;316 var j = c.at;317 while (j > c.last_base_until) {318 var seen = m.see(&c, &infos[j - 1], ::None);319 if (seen is ::Match && base_coverage is ::Some |covering|) {320 if (321 !accepts_base(&infos, j - 1)322 && coverage(&d, covering, infos[j - 1].glyph) is ::None323 ) {324 seen = Seen::Skip;325 }326 }327 if (seen is ::Match) {328 c.last_base = ::Some(j - 1);329 break;330 }331 j -= 1;332 }333 c.last_base_until = c.at;334 return c.last_base;335}336337fn attach_to_base(c: &var Apply, buffer: &var Buffer, at: u64, index: u64) -> bool {338 const d = &c.data;339 const base_coverage = at + u16_at(&d, at + 4) to u64;340 const b = if (341 find_base(&var c, &buffer.infos.view(), ::Some(base_coverage)) is ::Some |b|342 ) b else {343 return false;344 };345 const base_index = if (346 coverage(&d, base_coverage, buffer.infos.get(b).glyph) is ::Some |k|347 ) k to u64 else {348 return false;349 };350 const classes = u16_at(&d, at + 6) to u64;351 const bases = at + u16_at(&d, at + 10) to u64;352 return attach_mark(353 &var c,354 &var buffer,355 at + u16_at(&d, at + 8) to u64,356 index,357 bases,358 bases + 2 + base_index * classes * 2,359 classes,360 b361 );362}363364fn attach_to_ligature(c: &var Apply, buffer: &var Buffer, at: u64, index: u64) -> bool {365 const d = &c.data;366 const b = if (find_base(&var c, &buffer.infos.view(), ::None) is ::Some |b|) b else {367 return false;368 };369 const ligature = buffer.infos.get(b);370 const ligature_index = if (371 coverage(&d, at + u16_at(&d, at + 4) to u64, ligature.glyph) is ::Some |k|372 ) k to u64 else {373 return false;374 };375 const classes = u16_at(&d, at + 6) to u64;376 const ligatures = at + u16_at(&d, at + 10) to u64;377 const attach = ligatures + u16_at(&d, ligatures + 2 + ligature_index * 2) to u64;378 const components = u16_at(&d, attach) to u64;379 if (components == 0) {380 return false;381 }382 // the component the mark was on, if it was on this ligature, else383 // its last384 const mark = buffer.infos.get(c.at);385 var component = components - 1;386 if (387 ligature.ligature_id() != 0388 && ligature.ligature_id() == mark.ligature_id()389 && mark.ligature_component() > 0390 ) {391 component = min(components, mark.ligature_component() to u64) - 1;392 }393 return attach_mark(394 &var c,395 &var buffer,396 at + u16_at(&d, at + 8) to u64,397 index,398 attach,399 attach + 2 + component * classes * 2,400 classes,401 b402 );403}404405fn attach_to_mark(c: &var Apply, buffer: &var Buffer, at: u64, index: u64) -> bool {406 const d = &c.data;407 var m = c.matcher(false);408 m.lookup_props = c.lookup_props & ~IGNORE_FLAGS;409 const infos = &buffer.infos.view();410 const j = if (m.previous_match(&c, &infos, c.at, &Items::none(), 0) is ::Some |p|) p else {411 return false;412 };413 if (!infos[j].is_mark()) {414 return false;415 }416 const id1 = infos[c.at].ligature_id();417 const id2 = infos[j].ligature_id();418 const component1 = infos[c.at].ligature_component();419 const component2 = infos[j].ligature_component();420 // marks of the same base or ligature component, or one a ligature421 const good = if (id1 == id2) id1 == 0422 || component1 == component2 else (id1 > 0 && component1 == 0)423 || (id2 > 0 && component2 == 0);424 if (!good) {425 return false;426 }427 const mark2_index = if (428 coverage(&d, at + u16_at(&d, at + 4) to u64, infos[j].glyph) is ::Some |k|429 ) k to u64 else {430 return false;431 };432 const classes = u16_at(&d, at + 6) to u64;433 const marks2 = at + u16_at(&d, at + 10) to u64;434 return attach_mark(435 &var c,436 &var buffer,437 at + u16_at(&d, at + 8) to u64,438 index,439 marks2,440 marks2 + 2 + mark2_index * classes * 2,441 classes,442 j443 );444}445446// Attaches the current mark (its record in the mark array `marks`) to the447// glyph at `target` by the anchor its class has in the record at448// `record` (offsets from `anchors`); false when it has none there.449fn attach_mark(450 c: &var Apply,451 buffer: &var Buffer,452 marks: u64,453 index: u64,454 anchors: u64,455 record: u64,456 classes: u64,457 target: u64458) -> bool {459 const d = &c.data;460 if (index >= u16_at(&d, marks) to u64) {461 return false;462 }463 const mark_record = marks + 2 + index * 4;464 const class = u16_at(&d, mark_record) to u64;465 if (class >= classes) {466 return false;467 }468 const offset = u16_at(&d, record + class * 2) to u64;469 if (offset == 0) {470 return false;471 }472 const anchor = anchor_point(&c.face, anchors + offset);473 const mark = anchor_point(&c.face, marks + u16_at(&d, mark_record + 2) to u64);474 var p = &var buffer.positions.get_var(c.at);475 p.chain = target to i32 - c.at to i32;476 p.attach = MARK_ATTACHMENT;477 p.x_offset = round(anchor[0] - mark[0]);478 p.y_offset = round(anchor[1] - mark[1]);479 buffer.has_attachments = true;480 c.at += 1;481 return true;482}483484// Each attached glyph's offset made to follow what it attaches to:485// cursive ones across the line, marks also along it, less the advances486// between them (HarfBuzz's propagate_attachment_offsets).487exp fn resolve_attachments(buffer: &var Buffer) {488 if (!buffer.has_attachments) {489 return;490 }491 const n = buffer.positions.length();492 if (buffer.rtl) {493 var i = n;494 while (i > 0) {495 i -= 1;496 if (buffer.positions.get(i).chain != 0) {497 propagate(&var buffer.positions.items(), i, buffer.rtl, MAX_NESTING);498 }499 }500 } else {501 for ([..n]) |i| {502 if (buffer.positions.get(i).chain != 0) {503 propagate(&var buffer.positions.items(), i, buffer.rtl, MAX_NESTING);504 }505 }506 }507}508509fn propagate(positions: &var [GlyphPosition], i: u64, rtl: bool, nesting: u32) {510 const chain = positions[i].chain;511 const kind = positions[i].attach;512 positions[i].chain = 0;513 const j = (i to i64 + chain to i64) to u64;514 if (j >= positions.length() || nesting == 0) {515 return;516 }517 if (positions[j].chain != 0) {518 propagate(&var positions, j, rtl, nesting - 1);519 }520 if ((kind & CURSIVE_ATTACHMENT) != 0) {521 positions[i].y_offset += positions[j].y_offset;522 return;523 }524 positions[i].x_offset += positions[j].x_offset;525 positions[i].y_offset += positions[j].y_offset;526 if (j < i) {527 if (!rtl) {528 for ([j..i]) |k| {529 positions[i].x_offset -= positions[k].x_advance;530 positions[i].y_offset -= positions[k].y_advance;531 }532 } else {533 for ([j + 1..i + 1]) |k| {534 positions[i].x_offset += positions[k].x_advance;535 positions[i].y_offset += positions[k].y_advance;536 }537 }538 } else if (!rtl) {539 for ([i..j]) |k| {540 positions[i].x_offset += positions[k].x_advance;541 positions[i].y_offset += positions[k].y_advance;542 }543 } else {544 for ([i + 1..j + 1]) |k| {545 positions[i].x_offset -= positions[k].x_advance;546 positions[i].y_offset -= positions[k].y_advance;547 }548 }549}550551// The bytes a value record of the format takes: two for each field.552fn value_size(format: u32) -> u64 {553 var size: u64 = 0;554 for ([..8]) |bit| {555 if ((format & (1 << bit to u32)) != 0) {556 size += 2;557 }558 }559 return size;560}561562// An anchor's point, moved at a variable face's position when it is of563// format 3 with variation index device tables.564fn anchor_point(face: &Face, at: u64) -> [f32 : 2] {565 const d = &face.data();566 var point: [f32 : 2] = [i16_at(&d, at + 2) to f32, i16_at(&d, at + 4) to f32];567 if (u16_at(&d, at) == 3) {568 point[0] += face.device_delta(at, u16_at(&d, at + 6));569 point[1] += face.device_delta(at, u16_at(&d, at + 8));570 }571 return point;572}573574// The value record's placements and advances, moved at a variable face's575// position by the variation index device tables after them (`base` what576// their offsets are from); other device tables, for sizes in pixels, are577// left out.578fn add_value(face: &Face, base: u64, at: u64, format: u32, p: &var GlyphPosition) {579 const d = &face.data();580 var k = at;581 if ((format & 0x1) != 0) {582 p.x_offset += i16_at(&d, k) to f32;583 k += 2;584 }585 if ((format & 0x2) != 0) {586 p.y_offset += i16_at(&d, k) to f32;587 k += 2;588 }589 if ((format & 0x4) != 0) {590 p.x_advance += i16_at(&d, k) to f32;591 k += 2;592 }593 if ((format & 0x8) != 0) {594 p.y_advance += i16_at(&d, k) to f32;595 k += 2;596 }597 if ((format & 0xF0) == 0 || face.gdef_scalars.length() == 0) {598 return;599 }600 if ((format & 0x10) != 0) {601 p.x_offset += face.device_delta(base, u16_at(&d, k));602 k += 2;603 }604 if ((format & 0x20) != 0) {605 p.y_offset += face.device_delta(base, u16_at(&d, k));606 k += 2;607 }608 if ((format & 0x40) != 0) {609 p.x_advance += face.device_delta(base, u16_at(&d, k));610 k += 2;611 }612 if ((format & 0x80) != 0) {613 p.y_advance += face.device_delta(base, u16_at(&d, k));614 }615}