1// TrueType outlines at a position on a variable face's axes: `gvar`'s2// tuples of deltas, each weighted by its scalar there, moving a glyph's3// points (a composite's component offsets) and its four phantom points;4// points a tuple leaves out moved as their touched neighbours are5// (interpolation of untouched points, IUP).67import std::vector;8import std::option;910import pkg::alloy::text::font::reader;11import pkg::alloy::text::font::outline;12import variations;1314// tupleVariationCount's flag: point numbers shared by every tuple15const SHARED_POINT_NUMBERS: u32 = 0x8000;16// a tuple's index flags17const EMBEDDED_PEAK_TUPLE: u32 = 0x8000;18const INTERMEDIATE_REGION: u32 = 0x4000;19const PRIVATE_POINT_NUMBERS: u32 = 0x2000;2021// Adds the glyph's deltas at the coordinates to its points (the22// outline's, then the phantom ones), each contour ending at `ends`.23pub fn vary(d: &[u8], gvar: u64, glyph: u32, coords: &[f32], points: &var [Moved], ends: &[u32]) {24 const axes = u16_at(&d, gvar + 4) to u64;25 const shared = gvar + u32_at(&d, gvar + 8) to u64;26 const glyphs = u16_at(&d, gvar + 12);27 const long_offsets = (u16_at(&d, gvar + 14) & 1) != 0;28 const array = gvar + u32_at(&d, gvar + 16) to u64;29 if (glyph >= glyphs || axes != coords.length()) {30 return;31 }32 const start = array + data_offset(&d, gvar, long_offsets, glyph to u64);33 const end = array + data_offset(&d, gvar, long_offsets, glyph to u64 + 1);34 if (end <= start || end > d.length()) {35 return;36 }37 const counts = u16_at(&d, start);38 const tuples = (counts & 0x0FFF) to u64;39 var data = start + u16_at(&d, start + 2) to u64;40 const count = points.length();41 var shared_points = Vector<u32>::empty();42 var shared_all = true;43 if ((counts & SHARED_POINT_NUMBERS) != 0) {44 shared_all = read_points(&d, &var data, &var shared_points);45 }46 var original: Vector<Moved> = Vector<Moved>::empty(); // the points before any delta47 var header = start + 4;48 var listed = Vector<u32>::empty();49 var dx = Vector<f32>::empty();50 var dy = Vector<f32>::empty();51 for ([..tuples]) {52 const size = u16_at(&d, header) to u64;53 const index = u16_at(&d, header + 2);54 var peak = header + 4;55 if ((index & EMBEDDED_PEAK_TUPLE) == 0) {56 peak = shared + (index & 0x0FFF) to u64 * axes * 2;57 }58 var intermediate: u64 = 0;59 var next = header + 4;60 if ((index & EMBEDDED_PEAK_TUPLE) != 0) {61 next += axes * 2;62 }63 if ((index & INTERMEDIATE_REGION) != 0) {64 intermediate = next;65 next += axes * 4;66 }67 header = next;68 const tuple_data = data;69 data += size;70 var scalar: f32 = 1.0;71 for ([..axes]) |a| {72 const p = f2dot14_at(&d, peak + a * 2);73 var s = if (p < 0.0) p else 0.0;74 var e = if (p > 0.0) p else 0.0;75 if (intermediate != 0) {76 s = f2dot14_at(&d, intermediate + a * 2);77 e = f2dot14_at(&d, intermediate + axes * 2 + a * 2);78 }79 scalar *= axis_scalar(s, p, e, coords[a]);80 if (scalar == 0.0) {81 break;82 }83 }84 if (scalar == 0.0) {85 continue;86 }87 var at = tuple_data;88 var all = shared_all;89 listed.clear();90 if ((index & PRIVATE_POINT_NUMBERS) != 0) {91 all = read_points(&d, &var at, &var listed);92 } else {93 listed.append(&shared_points.view());94 }95 const n = if (all) count to u64 else listed.length();96 dx.clear();97 dy.clear();98 read_deltas(&d, &var at, n, &var dx);99 read_deltas(&d, &var at, n, &var dy);100 // IUP infers from the points before any tuple moved them101 if (original.length() == 0) {102 original.append(&points);103 }104 if (all) {105 for (points, dx.view(), dy.view()) |&var p, x, y| {106 p.x += x * scalar;107 p.y += y * scalar;108 }109 continue;110 }111 var touched = Vector<bool>::filled(false, count);112 var moved = Vector<Moved>::filled(Moved { .x = 0.0, .y = 0.0 }, count);113 for (listed.view(), dx.view(), dy.view()) |k, x, y| {114 if (k to u64 < count) {115 touched.get_var(k to u64) = true;116 moved.get_var(k to u64) = Moved { .x = x, .y = y };117 }118 }119 infer(&original.view(), &var moved.items(), &touched.view(), &ends);120 for (points, moved.view()) |&var p, &m| {121 p.x += m.x * scalar;122 p.y += m.y * scalar;123 }124 }125}126127fn data_offset(d: &[u8], gvar: u64, long: bool, k: u64) -> u64 {128 if (long) {129 return u32_at(&d, gvar + 20 + k * 4) to u64;130 }131 return u16_at(&d, gvar + 20 + k * 2) to u64 * 2;132}133134// Reads packed point numbers at `at` into `out`, moving `at` past them;135// true when they are every point (a count of 0).136fn read_points(d: &[u8], at: &var u64, out: &var Vector<u32>) -> bool {137 var count = u8_at(&d, at);138 at += 1;139 if (count == 0) {140 return true;141 }142 if ((count & 0x80) != 0) {143 count = ((count & 0x7F) << 8) | u8_at(&d, at);144 at += 1;145 }146 var point: u32 = 0;147 while (out.length() < count to u64 && at < d.length()) {148 const control = u8_at(&d, at);149 at += 1;150 const words = (control & 0x80) != 0;151 const run = (control & 0x7F) + 1;152 for ([..run to u64]) {153 if (words) {154 point += u16_at(&d, at);155 at += 2;156 } else {157 point += u8_at(&d, at);158 at += 1;159 }160 out.push(point);161 }162 }163 return false;164}165166// Reads `n` packed deltas at `at` into `out`, moving `at` past them.167fn read_deltas(d: &[u8], at: &var u64, n: u64, out: &var Vector<f32>) {168 while (out.length() < n && at < d.length()) {169 const control = u8_at(&d, at);170 at += 1;171 const run = (control & 0x3F) to u64 + 1;172 for ([..run]) {173 match (control & 0xC0) {174 0x80 {175 out.push(0.0);176 }177 0x40 {178 out.push(i16_at(&d, at) to f32);179 at += 2;180 }181 0xC0 {182 out.push((u32_at(&d, at) as i32) to f32);183 at += 4;184 }185 else {186 const v = u8_at(&d, at) to i32;187 out.push((if (v >= 0x80) v - 0x100 else v) to f32);188 at += 1;189 }190 }191 }192 }193 // a last run past n, in a malformed table194 out.truncate(n);195 while (out.length() < n) {196 out.push(0.0);197 }198}199200// The deltas of the points a tuple left untouched, from the touched ones201// before and after each in its contour: interpolated between them in202// each direction, or the nearer one's past both.203fn infer(original: &[Moved], moved: &var [Moved], touched: &[bool], ends: &[u32]) {204 var start: u64 = 0;205 for (ends) |end_point| {206 const end = end_point to u64;207 if (end >= moved.length() || end < start) {208 break;209 }210 var first: Option<u64> = ::None;211 for ([start..end + 1]) |k| {212 if (touched[k]) {213 first = ::Some(k);214 break;215 }216 }217 const f = if (first is ::Some |f|) f else {218 start = end + 1;219 continue;220 };221 // from each touched point to the next, around the contour222 var previous = f;223 var k = f;224 const n = end - start + 1;225 for ([..n]) {226 k = if (k == end) start else k + 1;227 if (!touched[k]) {228 continue;229 }230 fill(&original, &var moved, start, end, previous, k);231 previous = k;232 }233 start = end + 1;234 }235}236237// The untouched points strictly between touched points a and b, going238// forwards around the contour from `start` to `end`.239fn fill(original: &[Moved], moved: &var [Moved], start: u64, end: u64, a: u64, b: u64) {240 var k = if (a == end) start else a + 1;241 while (k != b) {242 moved[k] = Moved {243 .x = inferred(original[k].x, original[a].x, original[b].x, moved[a].x, moved[b].x),244 .y = inferred(original[k].y, original[a].y, original[b].y, moved[a].y, moved[b].y)245 };246 k = if (k == end) start else k + 1;247 }248}249250fn inferred(target: f32, previous: f32, next: f32, previous_delta: f32, next_delta: f32) -> f32 {251 if (previous == next) {252 return if (previous_delta == next_delta) previous_delta else 0.0;253 }254 if (target <= min_value(previous, next)) {255 return if (previous < next) previous_delta else next_delta;256 }257 if (target >= max_value(previous, next)) {258 return if (previous > next) previous_delta else next_delta;259 }260 const r = (target - previous) / (next - previous);261 return previous_delta + r * (next_delta - previous_delta);262}263264fn min_value(a: f32, b: f32) -> f32 => if (a < b) a else b;265266fn max_value(a: f32, b: f32) -> f32 => if (a > b) a else b;