1// The script shapers HarfBuzz chooses between (hb-ot-shaper.hh): what2// each asks of normalization and positioning; the complex ones3// (pkg::alloy::text_scripts) add steps of shaping through a4// `ComplexShaper` their plan keeps, the default shaper's done here.56import std::vector;7import std::option;89import font::reader;10import font::face;11import unicode::tables;12import unicode::properties;13import buffer;14import plan;1516exp type Shaper = enum {17 Default,18 Arabic,19 Hebrew,20 Indic,21 Khmer,22 Myanmar,23 Use,24 Thai,25 Hangul,26};2728// How normalization composes and decomposes for a shaper.29exp type Normalization = enum {30 Nothing,31 Decomposed,32 ComposedDiacritics, // never composing a base with a base33 ComposedDiacriticsNoShortCircuit, // decomposing fully, then composing back34 Auto,35};3637// When marks lose their advances: before GPOS, after it, or never.38exp type ZeroMarks = enum {39 Nothing,40 Early,41 Late,42};4344// A complex script's shaper (pkg::alloy::text_scripts), made for a plan:45// which it is, the features and pauses it adds, what it learns of the46// face, and the steps of shaping it takes.47exp interface ComplexShaper {48 fn kind(self: &) -> Shaper;49 // adds the shaper's features and pauses: whether it has any50 fn collect_features(self: &, b: &var Builder) -> bool;51 // changes the features after the common ones are added52 fn override_features(self: &, b: &var Builder);53 // keeps what the plan's lookups and the face tell it54 fn prepare(self: &var, plan: &Plan, face: &Face);55 fn compose(self: &, plan: &Plan, a: u32, b: u32) -> Option<u32>;56 fn decompose(self: &, ab: u32) -> Option<[u32 : 2]>;57 // reorders the marks from start to end, after their canonical sort58 fn reorder_marks(self: &, buffer: &var Buffer, start: u64, end: u64);59 // what it does to the characters before they are normalized60 fn preprocess_text(self: &, plan: &Plan, face: &Face, buffer: &var Buffer);61 // sets the masks of its features on the characters62 fn setup_masks(self: &, plan: &Plan, face: &Face, buffer: &var Buffer);63 // what it does to the glyphs once positioned64 fn postprocess_glyphs(self: &, face: &Face, buffer: &var Buffer);65 // runs its pause between two stages of lookups66 fn run_pause(self: &, pause: Pause, plan: &Plan, face: &Face, buffer: &var Buffer);67}6869// The script tag GPOS must have been read for to apply, 0 for any.70exp fn gpos_tag(self s: &Shaper) -> u32 => match (s) {71 ::Hebrew tag(&"hebr");72 else 0;73};7475exp fn normalization(self s: &Shaper) -> Normalization => match (s) {76 ::Hangul Normalization::Nothing;77 ::Indic, ::Khmer, ::Myanmar, ::Use Normalization::ComposedDiacriticsNoShortCircuit;78 else Normalization::Auto;79};8081exp fn zero_width_marks(self s: &Shaper) -> ZeroMarks => match (s) {82 ::Hangul, ::Indic, ::Khmer ZeroMarks::Nothing;83 ::Myanmar, ::Use ZeroMarks::Early;84 else ZeroMarks::Late;85};8687// Whether marks are placed without GPOS and `kern` applied by it.88exp fn fallback_position(self s: &Shaper) -> bool => match (s) {89 ::Default, ::Arabic, ::Hebrew, ::Hangul true;90 else false;91};9293// The character two compose into for the plan's shaper, if any.94exp fn compose(plan: &Plan, a: u32, b: u32) -> Option<u32> {95 if (plan.complex is ::Some |&c|) {96 return c.compose(&plan, a, b);97 }98 return unicode_composition(a, b);99}100101// The characters one decomposes into for the plan's shaper, the second102// 0 for a singleton, if any.103exp fn decompose(plan: &Plan, ab: u32) -> Option<[u32 : 2]> {104 if (plan.complex is ::Some |&c|) {105 return c.decompose(ab);106 }107 return canonical_decomposition(ab);108}109110// Reorders the marks from start to end, after their canonical sort.111exp fn reorder_marks(plan: &Plan, buffer: &var Buffer, start: u64, end: u64) {112 if (plan.complex is ::Some |&c|) {113 c.reorder_marks(&var buffer, start, end);114 }115}116117exp fn preprocess_text(plan: &Plan, face: &Face, buffer: &var Buffer) {118 if (plan.complex is ::Some |&c|) {119 c.preprocess_text(&plan, &face, &var buffer);120 }121}122123exp fn setup_masks(plan: &Plan, face: &Face, buffer: &var Buffer) {124 if (plan.complex is ::Some |&c|) {125 c.setup_masks(&plan, &face, &var buffer);126 }127}128129exp fn postprocess_glyphs(plan: &Plan, face: &Face, buffer: &var Buffer) {130 if (plan.complex is ::Some |&c|) {131 c.postprocess_glyphs(&face, &var buffer);132 }133}134135// Runs the pause between two stages of lookups.136exp fn run_pause(pause: Pause, plan: &Plan, face: &Face, buffer: &var Buffer) {137 match (pause) {138 ::Nothing {}139 ::ClearSubstitutionFlags {140 for (buffer.infos.items()) |&var info| {141 info.clear_substituted();142 }143 }144 else {145 if (plan.complex is ::Some |&c|) {146 c.run_pause(pause, &plan, &face, &var buffer);147 }148 }149 }150}151152// The character two canonically compose into, Hangul syllables too.153exp fn unicode_composition(a: u32, b: u32) -> Option<u32> {154 if (composed_hangul(a, b) is ::Some |c|) {155 return ::Some(c);156 }157 return composition(a, b);158}159160// The character's canonical decomposition, Hangul syllables' by161// arithmetic, the second part 0 for a singleton.162exp fn canonical_decomposition(c: u32) -> Option<[u32 : 2]> {163 if (c >= 0xAC00 && c < 0xAC00 + 11172) {164 const s = c - 0xAC00;165 const t = s % 28;166 if (t != 0) {167 return ::Some([c - t, 0x11A7 + t]);168 }169 return ::Some([0x1100 + s / 588, 0x1161 + (s % 588) / 28]);170 }171 return decomposition(c);172}