1// Marks placed without GPOS, around their base's ink box by their2// combining class, as HarfBuzz places them (hb-ot-shape-fallback.cc).34import std::vector;5import std::option;6import std::math;78import font::face;9import font::outline;10import buffer;1112// Unicode's classes of mark positions13const ATTACHED_BELOW_LEFT: u32 = 200;14const ATTACHED_BELOW: u32 = 202;15const ATTACHED_ABOVE: u32 = 214;16const ATTACHED_ABOVE_RIGHT: u32 = 216;17const BELOW_LEFT: u32 = 218;18const BELOW: u32 = 220;19const BELOW_RIGHT: u32 = 222;20const ABOVE_LEFT: u32 = 228;21const ABOVE: u32 = 230;22const ABOVE_RIGHT: u32 = 232;23const DOUBLE_BELOW: u32 = 233;24const DOUBLE_ABOVE: u32 = 234;2526// Gives each nonspacing mark the position class it is placed by: the27// classes Hebrew, Arabic, Thai, Lao and Tibetan number their marks by28// mapped to where those marks go.29exp fn recategorize_marks(buffer: &var Buffer) {30 for (buffer.infos.items()) |&var info| {31 if (info.general() is ::NonspacingMark) {32 info.set_combining_class(position_class(info.glyph, info.combining_class()));33 }34 }35}3637fn position_class(c: u32, class: u32) -> u32 {38 if (class >= 200) {39 return class;40 }41 var k = class;42 if ((c & ~0xFF) == 0x0E00) {43 if (k == 0) {44 match (c) {45 0x0E31, 0x0E34, 0x0E35, 0x0E36, 0x0E37, 0x0E47, 0x0E4C, 0x0E4D, 0x0E4E {46 k = ABOVE_RIGHT;47 }48 0x0EB1, 0x0EB4, 0x0EB5, 0x0EB6, 0x0EB7, 0x0EBB, 0x0ECC, 0x0ECD {49 k = ABOVE;50 }51 0x0EBC {52 k = BELOW;53 }54 }55 } else if (c == 0x0E3A) {56 k = BELOW_RIGHT; // Thai's virama57 }58 }59 return match (k) {60 // Hebrew: the vowels and meteg below, rafe above, shin dot above61 // right, sin dot and holam above left, point varika above62 22, 15, 16, 17, 23, 18, 19, 20, 21, 24, 25 BELOW;63 13 ATTACHED_ABOVE;64 10 ABOVE_RIGHT;65 11, 14 ABOVE_LEFT;66 26 ABOVE;67 // Arabic and Syriac: the harakat above but kasratan and kasra68 28, 29, 31, 32, 27, 34, 35, 36 ABOVE;69 30, 33 BELOW;70 // Thai, Lao and Tibetan71 3 BELOW_RIGHT;72 107 ABOVE_RIGHT;73 118 BELOW;74 122 ABOVE;75 129 BELOW;76 132 ABOVE;77 131 BELOW;78 else k;79 };80}8182// Places the marks after each base glyph (HarfBuzz's83// _hb_ot_shape_fallback_mark_position).84exp fn position_marks(face: &Face, buffer: &var Buffer, adjust_offsets: bool) {85 var start: u64 = 0;86 const count = buffer.infos.length();87 if (count == 0) {88 return;89 }90 for ([1..count]) |i| {91 const info = &buffer.infos.get(i);92 if (!info.is_unicode_mark() && !info.is_hidden() && !info.is_default_ignorable()) {93 position_cluster(&face, &var buffer, start, i, adjust_offsets);94 start = i;95 }96 }97 position_cluster(&face, &var buffer, start, count, adjust_offsets);98}99100fn position_cluster(face: &Face, buffer: &var Buffer, start: u64, end: u64, adjust_offsets: bool) {101 if (end < start + 2) {102 return;103 }104 var i = start;105 while (i < end) {106 if (buffer.infos.get(i).is_unicode_mark()) {107 i += 1;108 continue;109 }110 var j = i + 1;111 while (j < end) {112 const info = &buffer.infos.get(j);113 if (!info.is_hidden() && !info.is_default_ignorable() && !info.is_unicode_mark()) {114 break;115 }116 j += 1;117 }118 position_around_base(&face, &var buffer, i, j, adjust_offsets);119 i = j;120 }121}122123fn position_around_base(124 face: &Face,125 buffer: &var Buffer,126 base: u64,127 end: u64,128 adjust_offsets: bool129) {130 const base_glyph = buffer.infos.get(base).glyph;131 var base_extents = if (face.extents(base_glyph) is ::Some |e|) e else {132 // no extents: the marks only lose their advances133 for ([base + 1..end]) |i| {134 if (buffer.infos.get(i).general() is ::NonspacingMark) {135 var p = &var buffer.positions.get_var(i);136 if (adjust_offsets) {137 p.x_offset -= p.x_advance;138 p.y_offset -= p.y_advance;139 }140 p.x_advance = 0.0;141 p.y_advance = 0.0;142 }143 }144 return;145 };146 base_extents.y_bearing += buffer.positions.get(base).y_offset;147 // horizontal placement by the advance, which also works for glyphs without ink148 base_extents.x_bearing = 0.0;149 base_extents.width = face.advance(base_glyph);150 const ligature = buffer.infos.get(base).ligature_id();151 const components = buffer.infos.get(base).component_count() to i64;152 var x_offset: f32 = 0.0;153 var y_offset: f32 = 0.0;154 if (!buffer.rtl) {155 x_offset -= buffer.positions.get(base).x_advance;156 y_offset -= buffer.positions.get(base).y_advance;157 }158 var component_extents = base_extents;159 var last_component: i64 = -1;160 var last_class: u32 = 255;161 var cluster_extents = base_extents;162 for ([base + 1..end]) |i| {163 const info = buffer.infos.get(i);164 const class = info.combining_class();165 if (class == 0) {166 if (!buffer.rtl) {167 x_offset -= buffer.positions.get(i).x_advance;168 y_offset -= buffer.positions.get(i).y_advance;169 } else {170 x_offset += buffer.positions.get(i).x_advance;171 y_offset += buffer.positions.get(i).y_advance;172 }173 continue;174 }175 if (components > 1) {176 var component = info.ligature_component() to i64 - 1;177 // on the last component unless it says it is on its ligature's178 if (ligature == 0 || ligature != info.ligature_id() || component >= components) {179 component = components - 1;180 }181 if (last_component != component) {182 last_component = component;183 last_class = 255;184 component_extents = base_extents;185 const width = component_extents.width to i64;186 if (!buffer.rtl) {187 component_extents.x_bearing += ((component * width) / components) to f32;188 } else {189 component_extents.x_bearing += (190 ((components - 1 - component) * width) / components191 ) to f32;192 }193 component_extents.width = (width / components) to f32;194 }195 }196 if (last_class != class) {197 last_class = class;198 cluster_extents = component_extents;199 }200 position_mark(&face, &var buffer, &var cluster_extents, i, class);201 var p = &var buffer.positions.get_var(i);202 p.x_advance = 0.0;203 p.y_advance = 0.0;204 p.x_offset += x_offset;205 p.y_offset += y_offset;206 }207}208209// Integer division as C truncates it.210fn half(value: f32) -> f32 => ((value to i64) / 2) to f32;211212fn position_mark(face: &Face, buffer: &var Buffer, base: &var Extents, i: u64, class: u32) {213 const mark = if (face.extents(buffer.infos.get(i).glyph) is ::Some |e|) e else {214 return;215 };216 const y_gap = ((face.units_per_em to i64) / 16) to f32;217 var p = &var buffer.positions.get_var(i);218 p.x_offset = 0.0;219 p.y_offset = 0.0;220 // across221 if ((class == DOUBLE_BELOW || class == DOUBLE_ABOVE) && !buffer.rtl) {222 p.x_offset += base.x_bearing + base.width - half(mark.width) - mark.x_bearing;223 } else if ((class == DOUBLE_BELOW || class == DOUBLE_ABOVE) && buffer.rtl) {224 p.x_offset += base.x_bearing - half(mark.width) - mark.x_bearing;225 } else if (class == ATTACHED_BELOW_LEFT || class == BELOW_LEFT || class == ABOVE_LEFT) {226 p.x_offset += base.x_bearing - mark.x_bearing;227 } else if (class == ATTACHED_ABOVE_RIGHT || class == BELOW_RIGHT || class == ABOVE_RIGHT) {228 p.x_offset += base.x_bearing + base.width - mark.width - mark.x_bearing;229 } else {230 p.x_offset += base.x_bearing + half(base.width - mark.width) - mark.x_bearing;231 }232 // up and down233 match (class) {234 DOUBLE_BELOW, BELOW_LEFT, BELOW, BELOW_RIGHT, ATTACHED_BELOW_LEFT, ATTACHED_BELOW {235 if (class != ATTACHED_BELOW_LEFT && class != ATTACHED_BELOW) {236 base.height -= y_gap;237 }238 p.y_offset = base.y_bearing + base.height - mark.y_bearing;239 // "below" marks never go up240 if ((y_gap > 0.0) == (p.y_offset > 0.0)) {241 base.height -= p.y_offset;242 p.y_offset = 0.0;243 }244 base.height += mark.height;245 }246 DOUBLE_ABOVE, ABOVE_LEFT, ABOVE, ABOVE_RIGHT, ATTACHED_ABOVE, ATTACHED_ABOVE_RIGHT {247 if (class != ATTACHED_ABOVE && class != ATTACHED_ABOVE_RIGHT) {248 base.y_bearing += y_gap;249 base.height -= y_gap;250 }251 p.y_offset = base.y_bearing - (mark.y_bearing + mark.height);252 // nor do "above" marks go down much253 if ((y_gap > 0.0) != (p.y_offset > 0.0)) {254 const correction = half(-p.y_offset);255 base.y_bearing += correction;256 base.height -= correction;257 p.y_offset += correction;258 }259 base.y_bearing -= mark.height;260 base.height += mark.height;261 }262 else {}263 }264}