1// Item variation stores, which CFF2, HVAR, MVAR and GDEF keep their2// deltas in: regions of the axes' space, each with a scalar at the3// face's position, and rows of deltas weighted by them.45import std::vector;6import std::option;7import std::math;89import pkg::alloy::text::font::reader;10import pkg::alloy::text::font::face;1112// The scalar of one axis of a region or tuple: 1 at its peak, falling13// to 0 at its start and end, 0 outside them (OpenType's algorithm, with14// HarfBuzz's tolerance of malformed ones).15pub fn axis_scalar(start: f32, peak: f32, end: f32, coord: f32) -> f32 {16 if (peak == 0.0 || coord == peak) {17 return 1.0;18 }19 if (coord == 0.0) {20 return 0.0;21 }22 if (start > peak || peak > end || (start < 0.0 && end > 0.0)) {23 return 1.0;24 }25 if (coord <= start || end <= coord) {26 return 0.0;27 }28 if (coord < peak) {29 return (coord - start) / (peak - start);30 }31 return (end - coord) / (end - peak);32}3334// Each region's scalar at the normalized coordinates (none for the35// default position, where every delta is 0).36pub fn region_scalars(d: &[u8], store: u64, coords: &[f32]) -> Vector<f32> {37 var out = Vector<f32>::empty();38 if (coords.length() == 0) {39 return move out;40 }41 const list = store + u32_at(&d, store + 2) to u64;42 const axes = u16_at(&d, list) to u64;43 const count = u16_at(&d, list + 2) to u64;44 out.reserve(count);45 for ([..count]) |r| {46 const region = list + 4 + r * axes * 6;47 var scalar: f32 = 1.0;48 for ([..axes]) |a| {49 const at = region + a * 6;50 const coord = if (a < coords.length()) coords[a] else 0.0;51 scalar *= axis_scalar(52 f2dot14_at(&d, at),53 f2dot14_at(&d, at + 2),54 f2dot14_at(&d, at + 4),55 coord56 );57 if (scalar == 0.0) {58 break;59 }60 }61 out.push(scalar);62 }63 return move out;64}6566// The region list's indices one item variation data (by its index in67// the store) weighs its deltas by.68pub fn data_regions(d: &[u8], store: u64, outer: u32) -> Vector<u32> {69 var out = Vector<u32>::empty();70 if (outer >= u16_at(&d, store + 6)) {71 return move out;72 }73 const data = store + u32_at(&d, store + 8 + outer to u64 * 4) to u64;74 const count = u16_at(&d, data + 4) to u64;75 for ([..count]) |k| {76 out.push(u16_at(&d, data + 6 + k * 2));77 }78 return move out;79}8081// The delta of the store's row (outer, inner) at the position whose82// region scalars are given: 0 at the default position.83pub fn store_delta(d: &[u8], store: u64, outer: u32, inner: u32, scalars: &[f32]) -> f32 {84 if (scalars.length() == 0 || outer >= u16_at(&d, store + 6)) {85 return 0.0;86 }87 const data = store + u32_at(&d, store + 8 + outer to u64 * 4) to u64;88 if (inner >= u16_at(&d, data)) {89 return 0.0;90 }91 const word_count = u16_at(&d, data + 2);92 const long = (word_count & 0x8000) != 0;93 const words = (word_count & 0x7FFF) to u64;94 const regions = u16_at(&d, data + 4) to u64;95 const word_size: u64 = if (long) 4 else 2;96 const rest_size: u64 = if (long) 2 else 1;97 const row_size = words * word_size + (regions - min_of(words, regions)) * rest_size;98 var at = data + 6 + regions * 2 + inner to u64 * row_size;99 var delta: f32 = 0.0;100 for ([..regions]) |k| {101 const region = u16_at(&d, data + 6 + k * 2) to u64;102 var value: i32 = 0;103 if (k < words) {104 value = if (long) u32_at(&d, at) as i32 else i16_at(&d, at);105 at += word_size;106 } else {107 value = if (long) i16_at(&d, at) else signed_byte_at(&d, at);108 at += rest_size;109 }110 if (region < scalars.length()) {111 delta += value to f32 * scalars[region];112 }113 }114 return delta;115}116117fn min_of(a: u64, b: u64) -> u64 => if (a < b) a else b;118119fn signed_byte_at(d: &[u8], at: u64) -> i32 {120 const v = u8_at(&d, at) to i32;121 return if (v >= 0x80) v - 0x100 else v;122}123124/* --- Axes --- */125126fn fixed_at(d: &[u8], at: u64) -> f32 => (u32_at(&d, at) as i32) to f32 / 65536.0;127128pub fn axes_of(d: &[u8], fvar: u64) -> Vector<Axis> {129 var out = Vector<Axis>::empty();130 const first = fvar + u16_at(&d, fvar + 4) to u64;131 const count = u16_at(&d, fvar + 8) to u64;132 const size = u16_at(&d, fvar + 10) to u64;133 for ([..count]) |k| {134 const at = first + k * size;135 out.push(Axis {136 .tag = u32_at(&d, at),137 .minimum = fixed_at(&d, at + 4),138 .default = fixed_at(&d, at + 8),139 .maximum = fixed_at(&d, at + 12),140 .hidden = (u16_at(&d, at + 16) & 1) != 0141 });142 }143 return move out;144}145146pub fn named_instances(d: &[u8], fvar: u64) -> Vector<NamedInstance> {147 var out = Vector<NamedInstance>::empty();148 const axes = u16_at(&d, fvar + 8) to u64;149 const first = fvar + u16_at(&d, fvar + 4) to u64 + axes * u16_at(&d, fvar + 10) to u64;150 const count = u16_at(&d, fvar + 12) to u64;151 const size = u16_at(&d, fvar + 14) to u64;152 for ([..count]) |k| {153 const at = first + k * size;154 var values = Vector<f32>::with_capacity(axes);155 for ([..axes]) |a| {156 values.push(fixed_at(&d, at + 4 + a * 4));157 }158 out.push(NamedInstance { .name = u16_at(&d, at), .values = move values });159 }160 return move out;161}162163// The user values as normalized coordinates, as HarfBuzz makes them:164// each clamped to its axis, mapped to -1..1 around the default, rounded165// to a 2.14 number, then through `avar`'s maps.166pub fn normalize(d: &[u8], axes: &[Axis], avar: Option<u64>, values: &[f32]) -> Vector<f32> {167 var units = Vector<i32>::with_capacity(axes.length());168 for (axes, [..axes.length()]) |&axis, k| {169 const v = clamp(170 if (k < values.length()) values[k] else axis.default,171 axis.minimum,172 axis.maximum173 );174 var n: f32 = 0.0;175 if (v < axis.default) {176 n = (v - axis.default) / (axis.default - axis.minimum);177 } else if (v > axis.default) {178 n = (v - axis.default) / (axis.maximum - axis.default);179 }180 units.push(round(n * 16384.0) to i32);181 }182 if (avar is ::Some |at|) {183 // one segment map an axis, each a count of (from, to) pairs184 var map = at + 8;185 for (units.items()) |&var u| {186 const count = u16_at(&d, map) to u64;187 u = segment_map(&d, map + 2, count, u);188 map += 2 + count * 4;189 }190 }191 var out = Vector<f32>::with_capacity(units.length());192 for (units.view()) |u| {193 out.push(u to f32 / 16384.0);194 }195 return move out;196}197198// A coordinate through one axis's `avar` map, in 2.14 units.199fn segment_map(d: &[u8], pairs: u64, count: u64, value: i32) -> i32 {200 if (count < 2) {201 return if (count == 0) value else value - from_at(&d, pairs, 0) + to_at(&d, pairs, 0);202 }203 if (value <= from_at(&d, pairs, 0)) {204 return value - from_at(&d, pairs, 0) + to_at(&d, pairs, 0);205 }206 var i: u64 = 1;207 while (i < count - 1 && value > from_at(&d, pairs, i)) {208 i += 1;209 }210 const from = from_at(&d, pairs, i);211 const target = to_at(&d, pairs, i);212 if (value >= from) {213 return value - from + target;214 }215 const from_before = from_at(&d, pairs, i - 1);216 const target_before = to_at(&d, pairs, i - 1);217 if (from_before == from) {218 return target_before;219 }220 const span = (from - from_before) to f32;221 return round(222 target_before to f32 + (target - target_before) to f32 * (value - from_before) to f32 / span223 ) to i32;224}225226fn from_at(d: &[u8], pairs: u64, k: u64) -> i32 => i16_at(&d, pairs + k * 4);227228fn to_at(d: &[u8], pairs: u64, k: u64) -> i32 => i16_at(&d, pairs + k * 4 + 2);229230/* --- Metrics --- */231232// The (outer, inner) row of an item variation store a DeltaSetIndexMap233// gives the index, or the index itself in row 0 with no map.234pub fn mapped_row(d: &[u8], map: u64, index: u32) -> [u32 : 2] {235 if (map == 0) {236 return [0, index];237 }238 const format = u8_at(&d, map);239 const entry = u8_at(&d, map + 1);240 const count = if (format == 0) u16_at(&d, map + 2) else u32_at(&d, map + 2);241 const data = if (format == 0) map + 4 else map + 6;242 if (count == 0) {243 return [0, index];244 }245 const k = if (index < count) index else count - 1;246 const size = ((entry >> 4) & 3) + 1;247 const at = data + k to u64 * size to u64;248 var value: u32 = 0;249 for ([..size to u64]) |b| {250 value = (value << 8) | u8_at(&d, at + b);251 }252 const inner_bits = (entry & 0x0F) + 1;253 return [value >> inner_bits, value & ((1 << inner_bits) - 1)];254}255256// MVAR's delta for the metric's tag, 0 when it has none.257pub fn metric_delta(d: &[u8], mvar: u64, tag: u32, scalars: &[f32]) -> f32 {258 const size = u16_at(&d, mvar + 6) to u64;259 const count = u16_at(&d, mvar + 8) to u64;260 const store = u16_at(&d, mvar + 10) to u64;261 if (store == 0) {262 return 0.0;263 }264 for ([..count]) |k| {265 const record = mvar + 12 + k * size;266 if (u32_at(&d, record) == tag) {267 return store_delta(268 &d,269 mvar + store,270 u16_at(&d, record + 4),271 u16_at(&d, record + 6),272 &scalars273 );274 }275 }276 return 0.0;277}278279/* --- Feature variations --- */280281// The feature table substitutions of the table's (GSUB's or GPOS's)282// feature variations at a variable face's normalized coordinates: the283// first record whose conditions all hold, if one does.284pub fn feature_variation(d: &[u8], table: Option<u64>, coords: &[f32]) -> Option<u64> {285 const t = if (table is ::Some |t|) t else {286 return ::None;287 };288 if (coords.length() == 0 || u16_at(&d, t + 2) < 1 || u32_at(&d, t + 10) == 0) {289 return ::None;290 }291 const variations = t + u32_at(&d, t + 10) to u64;292 const count = u32_at(&d, variations + 4) to u64;293 for ([..count]) |k| {294 const record = variations + 8 + k * 8;295 const set_offset = u32_at(&d, record) to u64;296 var holds = true;297 if (set_offset != 0) {298 const set = variations + set_offset;299 const conditions = u16_at(&d, set) to u64;300 for ([..conditions]) |c| {301 const condition = set + u32_at(&d, set + 2 + c * 4) to u64;302 // format 1: an axis's range, any other never holding303 const axis = u16_at(&d, condition + 2) to u64;304 const coord = if (axis < coords.length()) coords[axis] else 0.0;305 holds = holds306 && u16_at(&d, condition) == 1307 && coord >= f2dot14_at(&d, condition + 4)308 && coord <= f2dot14_at(&d, condition + 6);309 }310 }311 if (holds) {312 const substitutions = u32_at(&d, record + 4) to u64;313 return if (substitutions == 0) ::None else ::Some(variations + substitutions);314 }315 }316 return ::None;317}