1// The glyphs being shaped, as HarfBuzz's buffer keeps them: each with2// its cluster, the features it takes (a mask), what Unicode says of its3// character, what GDEF and substitutions say of its glyph, the ligature4// it belongs to, its syllable and what its script's shaper found; and5// their positions, with what each is attached to. Glyphs stay in the6// text's order, right to left too.78import std::vector;9import std::option;10import std::math;1112import font::face;13import unicode::tables;14import unicode::properties;1516// What Unicode says of a character (HarfBuzz's unicode props): its17// General_Category in the low five bits, then these flags; a mark's18// modified combining class in the high byte, or for Format characters19// whether it is ZWJ, ZWNJ or a variation selector.20const CATEGORY_BITS: u16 = 0x1F;21const UNICODE_IGNORABLE: u16 = 0x20; // default ignorable, hidden when shaping ends22const UNICODE_HIDDEN: u16 = 0x40; // ignorable, but not skipped by GSUB23const UNICODE_CONTINUATION: u16 = 0x80; // continues its grapheme24const FORMAT_ZWJ: u16 = 0x100;25const FORMAT_ZWNJ: u16 = 0x200;26const FORMAT_VARIATION_SELECTOR: u16 = 0x400;2728// What GDEF and substitutions say of a glyph (HarfBuzz's glyph props):29// its class, then whether a substitution changed it, ligated it or30// multiplied it; a mark's attachment class in the high byte.31exp const BASE_GLYPH: u16 = 0x02;32exp const LIGATURE: u16 = 0x04;33exp const MARK: u16 = 0x08;34exp const SUBSTITUTED: u16 = 0x10;35exp const LIGATED: u16 = 0x20;36exp const MULTIPLIED: u16 = 0x40;37exp const PRESERVED: u16 = SUBSTITUTED | LIGATED | MULTIPLIED;3839// A ligature's props: its id in the high three bits, then whether this40// is the ligature itself (with its component count below) or a mark on41// one of its components (the component's number below).42const LIGATURE_BASE: u8 = 0x10;4344// A glyph being shaped and where its text started.45exp type GlyphInfo = struct {46 exp glyph: u32, // the character until glyphs are mapped, then the glyph47 exp cluster: u32, // the byte offset in the run's text where its cluster starts48 exp mask: u32 = 0, // the features applying to it49 exp mapped: u32 = 0, // the glyph normalization found for the character50 exp unicode: u16 = 0,51 exp props: u16 = 0,52 exp ligature: u8 = 0,53 exp syllable: u8 = 0, // its syllable's serial in the high four bits, its kind below54 exp category: u8 = 0, // its script's shaper's55 exp position: u8 = 0, // its script's shaper's56};5758// A glyph's position, in font units: the advance moves the pen, the59// offset moves the glyph only; what it attaches to is `chain` glyphs60// away, by `attach` (MARK_ATTACHMENT, CURSIVE_ATTACHMENT).61exp type GlyphPosition = struct {62 exp x_advance: f32 = 0.0,63 exp y_advance: f32 = 0.0,64 exp x_offset: f32 = 0.0,65 exp y_offset: f32 = 0.0,66 exp chain: i32 = 0,67 exp attach: u8 = 0,68};6970exp const MARK_ATTACHMENT: u8 = 1;71exp const CURSIVE_ATTACHMENT: u8 = 2;7273// The character's Unicode props, set on the glyph.74exp fn set_unicode(self info: &var GlyphInfo, buffer: &var Buffer) {75 const u = info.glyph;76 var props = general_category(u) to u16;77 if (u >= 0x80) {78 if (is_default_ignorable(u)) {79 buffer.has_ignorables = true;80 props |= UNICODE_IGNORABLE;81 if (u == 0x200C) {82 props |= FORMAT_ZWNJ;83 } else if (u == 0x200D) {84 props |= FORMAT_ZWJ;85 } else if ((u >= 0x180B && u <= 0x180D) || u == 0x180F) {86 // Mongolian's free variation selectors, hidden but seen by GSUB87 props |= UNICODE_HIDDEN;88 } else if (u >= 0xE0020 && u <= 0xE007F) {89 props |= UNICODE_HIDDEN; // tags90 } else if (u == 0x034F) {91 buffer.has_grapheme_joiner = true;92 props |= UNICODE_HIDDEN;93 }94 }95 if (is_mark_category(props & CATEGORY_BITS)) {96 buffer.has_continuations = true;97 props |= UNICODE_CONTINUATION;98 props |= modified_combining_class(u) to u16 << 8;99 }100 }101 info.unicode = props;102}103104// SpacingMark, EnclosingMark or NonspacingMark.105fn is_mark_category(category: u16) -> bool => category >= 10 && category <= 12;106107// HarfBuzz's Default_Ignorable_Code_Point, which leaves out the Hangul108// fillers and others shown as glyphs.109exp fn is_default_ignorable(c: u32) -> bool {110 if (c < 0x10000) {111 match (c >> 8) {112 0x00 {113 return c == 0x00AD;114 }115 0x03 {116 return c == 0x034F;117 }118 0x06 {119 return c == 0x061C;120 }121 0x17 {122 return c >= 0x17B4 && c <= 0x17B5;123 }124 0x18 {125 return c >= 0x180B && c <= 0x180E;126 }127 0x20 {128 return (c >= 0x200B && c <= 0x200F)129 || (c >= 0x202A && c <= 0x202E)130 || (c >= 0x2060 && c <= 0x206F);131 }132 0xFE {133 return (c >= 0xFE00 && c <= 0xFE0F) || c == 0xFEFF;134 }135 0xFF {136 return c >= 0xFFF0 && c <= 0xFFF8;137 }138 }139 return false;140 }141 return (c >= 0x1D173 && c <= 0x1D17A) || (c >= 0xE0000 && c <= 0xE0FFF);142}143144exp fn general(145 self info: &GlyphInfo146) -> GeneralCategory => (info.unicode & CATEGORY_BITS) to GeneralCategory;147148exp fn set_general(self info: &var GlyphInfo, category: GeneralCategory) {149 // the high byte cleared, as what it held depended on the category150 info.unicode = category to u16 | (info.unicode & (0xFF & ~CATEGORY_BITS));151}152153exp fn is_unicode_mark(154 self info: &GlyphInfo155) -> bool => is_mark_category(info.unicode & CATEGORY_BITS);156157// The mark's modified combining class, 0 for other characters.158exp fn combining_class(self info: &GlyphInfo) -> u32 => if (info.is_unicode_mark()) (159 info.unicode >> 8160) to u32 else 0;161162exp fn set_combining_class(self info: &var GlyphInfo, class: u32) {163 if (info.is_unicode_mark()) {164 info.unicode = (class to u16 << 8) | (info.unicode & 0xFF);165 }166}167168exp fn is_unicode_space(self info: &GlyphInfo) -> bool => (info.unicode & CATEGORY_BITS) == 29;169170exp fn is_format(self info: &GlyphInfo) -> bool => (info.unicode & CATEGORY_BITS) == 1;171172// The space's width when the face lacks its glyph (SPACE_*), 0 for none.173exp fn space_kind(self info: &GlyphInfo) -> u32 => if (info.is_unicode_space()) (174 info.unicode >> 8175) to u32 else 0;176177exp fn set_space_kind(self info: &var GlyphInfo, kind: u32) {178 if (info.is_unicode_space()) {179 info.unicode = (kind to u16 << 8) | (info.unicode & 0xFF);180 }181}182183exp fn is_variation_selector(self info: &GlyphInfo) -> bool => info.is_format()184 && (info.unicode & FORMAT_VARIATION_SELECTOR) != 0;185186// Marks a variation selector the face has no sequence for as Format, so187// that GSUB sees it, or gives it its own category back.188exp fn set_variation_selector(self info: &var GlyphInfo, customized: bool) {189 if (customized) {190 info.set_general(GeneralCategory::Format);191 info.unicode |= FORMAT_VARIATION_SELECTOR;192 } else {193 info.set_general(GeneralCategory::NonspacingMark);194 }195}196197// A default ignorable no substitution has changed.198exp fn is_default_ignorable(199 self info: &GlyphInfo200) -> bool => (info.unicode & UNICODE_IGNORABLE) != 0201 && (info.props & SUBSTITUTED) == 0;202203exp fn set_default_ignorable(self info: &var GlyphInfo) {204 info.unicode |= UNICODE_IGNORABLE;205}206207exp fn clear_default_ignorable(self info: &var GlyphInfo) {208 info.unicode &= ~UNICODE_IGNORABLE;209}210211exp fn is_hidden(self info: &GlyphInfo) -> bool => (info.unicode & UNICODE_HIDDEN) != 0;212213exp fn unhide(self info: &var GlyphInfo) {214 info.unicode &= ~UNICODE_HIDDEN;215}216217exp fn is_continuation(self info: &GlyphInfo) -> bool => (info.unicode & UNICODE_CONTINUATION) != 0;218219exp fn set_continuation(self info: &var GlyphInfo) {220 info.unicode |= UNICODE_CONTINUATION;221}222223exp fn clear_continuation(self info: &var GlyphInfo) {224 info.unicode &= ~UNICODE_CONTINUATION;225}226227exp fn is_zwj(self info: &GlyphInfo) -> bool => info.is_format()228 && (info.unicode & FORMAT_ZWJ) != 0;229230exp fn is_zwnj(self info: &GlyphInfo) -> bool => info.is_format()231 && (info.unicode & FORMAT_ZWNJ) != 0;232233exp fn is_base_glyph(self info: &GlyphInfo) -> bool => (info.props & BASE_GLYPH) != 0;234235exp fn is_ligature(self info: &GlyphInfo) -> bool => (info.props & LIGATURE) != 0;236237exp fn is_mark(self info: &GlyphInfo) -> bool => (info.props & MARK) != 0;238239exp fn substituted(self info: &GlyphInfo) -> bool => (info.props & SUBSTITUTED) != 0;240241exp fn ligated(self info: &GlyphInfo) -> bool => (info.props & LIGATED) != 0;242243exp fn multiplied(self info: &GlyphInfo) -> bool => (info.props & MULTIPLIED) != 0;244245exp fn clear_ligated_and_multiplied(self info: &var GlyphInfo) {246 info.props &= ~(LIGATED | MULTIPLIED);247}248249exp fn clear_substituted(self info: &var GlyphInfo) {250 info.props &= ~SUBSTITUTED;251}252253exp fn set_ligature_props(self info: &var GlyphInfo, id: u32, components: u32) {254 info.ligature = ((id << 5) to u8) | LIGATURE_BASE | (components & 0x0F) to u8;255}256257exp fn set_mark_props(self info: &var GlyphInfo, id: u32, component: u32) {258 info.ligature = ((id << 5) to u8) | (component & 0x0F) to u8;259}260261exp fn set_component(self info: &var GlyphInfo, component: u32) {262 info.set_mark_props(0, component);263}264265exp fn ligature_id(self info: &GlyphInfo) -> u32 => (info.ligature >> 5) to u32;266267// Which component of its ligature a mark is on, 0 for a ligature itself.268exp fn ligature_component(self info: &GlyphInfo) -> u32 => if (269 (info.ligature & LIGATURE_BASE) != 0270) 0 else (info.ligature & 0x0F) to u32;271272// The components a ligature was made of, 1 for any other glyph.273exp fn component_count(self info: &GlyphInfo) -> u32 => if (274 (info.props & LIGATURE) != 0275 && (info.ligature & LIGATURE_BASE) != 0276) (info.ligature & 0x0F) to u32 else 1;277278// The buffer of a run being shaped.279exp type Buffer = struct {280 exp infos: Vector<GlyphInfo>,281 exp positions: Vector<GlyphPosition> = Vector<GlyphPosition>::empty(),282 exp rtl: bool = false,283 exp script: Script = Script::Unknown,284 serial: u32 = 0, // for ligature ids285 // what the run holds, found as it is shaped286 exp has_ignorables: bool = false,287 exp has_continuations: bool = false,288 exp has_grapheme_joiner: bool = false,289 exp has_fraction_slash: bool = false,290 exp has_space_fallback: bool = false,291 exp has_broken_syllable: bool = false,292 exp has_attachments: bool = false,293 exp has_variation_selector_fallback: bool = false,294 exp has_stretch: bool = false,295 // up to five characters of the text on either side of the run, the296 // nearest first297 exp before: [u32 : 5] = [0, 0, 0, 0, 0],298 exp before_count: u64 = 0,299 exp after: [u32 : 5] = [0, 0, 0, 0, 0],300 exp after_count: u64 = 0,301};302303exp fn context_before(self buffer: &Buffer) -> &[u32] => &buffer.before[..buffer.before_count];304305exp fn context_after(self buffer: &Buffer) -> &[u32] => &buffer.after[..buffer.after_count];306307exp fn length(self buffer: &Buffer) -> u64 => buffer.infos.length();308309// A new ligature id, from 1 to 7 in turn.310exp fn next_ligature_id(self buffer: &var Buffer) -> u32 {311 buffer.serial += 1;312 var id = buffer.serial & 7;313 if (id == 0) {314 buffer.serial += 1;315 id = buffer.serial & 7;316 }317 return id;318}319320// Gives the glyphs from start to end one cluster, the least of theirs,321// with the glyphs before and after sharing their first and last ones'.322exp fn merge_clusters(self buffer: &var Buffer, first: u64, last: u64) {323 merge_clusters_of(&var buffer.infos.items(), first, last);324}325326// Gives infos[first..last] one cluster, as `merge_clusters` does.327exp fn merge_clusters_of(infos: &var [GlyphInfo], first: u64, last: u64) {328 if (last - first < 2) {329 return;330 }331 var start = first;332 var end = last;333 var cluster = infos[start].cluster;334 for (infos[start + 1..end]) |&info| {335 cluster = min(cluster, info.cluster);336 }337 if (cluster != infos[end - 1].cluster) {338 while (end < infos.length() && infos[end - 1].cluster == infos[end].cluster) {339 end += 1;340 }341 }342 if (cluster != infos[start].cluster) {343 while (start > 0 && infos[start - 1].cluster == infos[start].cluster) {344 start -= 1;345 }346 }347 for (infos[start..end]) |&var info| {348 info.cluster = cluster;349 }350}351352// Sorts the glyphs from start to end by `before`, stably, merging the353// clusters of those it moves (HarfBuzz's buffer sort, an insertion sort).354exp fn sort(355 self buffer: &var Buffer,356 start: u64,357 end: u64,358 before: |&| (&GlyphInfo, &GlyphInfo) -> bool359) {360 for ([start + 1..end]) |i| {361 var j = i;362 while (j > start && before(&buffer.infos.get(i), &buffer.infos.get(j - 1))) {363 j -= 1;364 }365 if (i == j) {366 continue;367 }368 buffer.merge_clusters(j, i + 1);369 const moved = buffer.infos.get(i);370 const infos = &var buffer.infos.items();371 var k = i;372 while (k > j) {373 infos[k] = infos[k - 1];374 k -= 1;375 }376 infos[j] = moved;377 }378}379380// Removes the glyph, giving its cluster to a neighbor when no glyph of it381// is left (HarfBuzz's delete_glyph, with the glyphs before `at` its out382// buffer).383exp fn delete_glyph(self buffer: &var Buffer, at: u64) {384 const infos = &var buffer.infos.items();385 const cluster = infos[at].cluster;386 const survives = (at + 1 < infos.length() && cluster == infos[at + 1].cluster)387 || (at > 0 && cluster == infos[at - 1].cluster);388 if (!survives) {389 if (at > 0) {390 if (cluster < infos[at - 1].cluster) {391 const old = infos[at - 1].cluster;392 var k = at;393 while (k > 0 && infos[k - 1].cluster == old) {394 infos[k - 1].cluster = cluster;395 k -= 1;396 }397 }398 } else if (at + 1 < infos.length()) {399 buffer.merge_clusters(at, at + 2);400 }401 }402 buffer.infos.remove(at);403}404405// The end of the syllable starting at `start`.406exp fn syllable_end(self buffer: &Buffer, start: u64) -> u64 {407 const infos = &buffer.infos.view();408 const syllable = infos[start].syllable;409 var end = start + 1;410 while (end < infos.length() && infos[end].syllable == syllable) {411 end += 1;412 }413 return end;414}415416// The end of the cluster starting at `start`.417exp fn cluster_end(self buffer: &Buffer, start: u64) -> u64 {418 const infos = &buffer.infos.view();419 const cluster = infos[start].cluster;420 var end = start + 1;421 while (end < infos.length() && infos[end].cluster == cluster) {422 end += 1;423 }424 return end;425}426427// Reverses the glyphs from start to end, and their positions when there428// are some.429exp fn reverse_range(self buffer: &var Buffer, start: u64, end: u64) {430 if (end - start < 2) {431 return;432 }433 const infos = &var buffer.infos.items();434 var i = start;435 var j = end - 1;436 while (i < j) {437 const t = infos[i];438 infos[i] = infos[j];439 infos[j] = t;440 i += 1;441 j -= 1;442 }443 if (buffer.positions.length() == buffer.infos.length()) {444 const positions = &var buffer.positions.items();445 i = start;446 j = end - 1;447 while (i < j) {448 const t = positions[i];449 positions[i] = positions[j];450 positions[j] = t;451 i += 1;452 j -= 1;453 }454 }455}456457// The character's canonical combining class as HarfBuzz orders marks by458// it: Hebrew's points in the order fonts expect (SBL Hebrew's: shin and459// sin dots, dagesh and rafe before the vowels), Arabic's shadda before460// the other harakat, and a few marks of Telugu, Thai, Lao and Tibetan.461exp fn modified_combining_class(c: u32) -> u32 {462 match (c) {463 0x1A60, 0x0FC6 {464 return 254; // sakot after tone marks, padma after vowels465 }466 0x0F39 {467 return 127; // tsa-phru before U+0F74468 }469 }470 const ccc = combining_class(c);471 return match (ccc) {472 10 22;473 11 15;474 12 16;475 13 17;476 14 23;477 15 18;478 16 19;479 17 20;480 18 21;481 19 14;482 20 24;483 21 12;484 22 25;485 23 13;486 24 10;487 25 11;488 27 28;489 28 29;490 29 30;491 30 31;492 31 32;493 32 33;494 33 27;495 84 4;496 91 5;497 103 3;498 130 132;499 132 131;500 else ccc;501 };502}