1// The formats beyond TrueType's outlines, as text's `FontFormats` calls2// them on a face: its CFF or CFF2 outlines, color tables and a variable3// face's axes read with it; instances at a position on the axes, whose4// variation stores give their scalars, MVAR their metrics and feature5// variations their lookups; advances moved by HVAR or `gvar`'s phantom6// points, GPOS's device tables by GDEF's store.78import std::vector;9import std::option;10import std::math;1112import pkg::alloy::text;13import pkg::alloy::text::font::reader;14import pkg::alloy::text::font::face;15import pkg::alloy::text::font::outline;16import cff;17import variations;18import gvar;19import color;20import bitmaps;2122// What text calls on the faces read after `install_into`.23pub fn formats() -> FontFormats => FontFormats {24 .read = read_formats,25 .at = instance_at,26 .named_instances = named_instances_of,27 .advance = varied_advance,28 .device_delta = varied_device_delta,29 .vary_points = vary_points,30 .is_color = is_color_glyph,31 .color_glyph = color_glyph32};3334// Reads the face's CFF or CFF2 outlines where it has no `glyf`, its35// color tables and a variable face's axes: false when its CFF does not36// read.37fn read_formats(face: &var Face, tables: &FaceTables) -> bool {38 if (face.glyphs is ::Nothing && (tables.cff != 0 || tables.cff2 != 0)) {39 const at = if (tables.cff2 != 0) tables.cff2 else tables.cff;40 if (Cff::read(&face.data(), at, tables.cff2 != 0) is ::Some |move c|) {41 face.glyphs = GlyphTable::Cff(new move c);42 } else {43 return false;44 }45 }46 face.colors = ColorTables {47 .colr = present(tables.colr),48 .cpal = present(tables.cpal),49 .cblc = present(tables.cblc),50 .cbdt = present(tables.cbdt),51 .sbix = present(tables.sbix)52 };53 if (tables.fvar != 0) {54 face.axes = axes_of(&face.data(), tables.fvar);55 face.variations = VariationTables {56 .fvar = present(tables.fvar),57 .avar = present(tables.avar),58 .gvar = present(tables.gvar),59 .hvar = present(tables.hvar),60 .mvar = present(tables.mvar)61 };62 }63 return true;64}6566fn present(offset: u64) -> Option<u64> => if (offset == 0) ::None else ::Some(offset);6768// The named instances of a variable face's `fvar`.69fn named_instances_of(face: &Face) -> Vector<NamedInstance> {70 if (face.variations.fvar is ::Some |fvar|) {71 return named_instances(&face.data(), fvar);72 }73 return Vector<NamedInstance>::empty();74}7576// The face at the user values on its axes, one an axis in `fvar`'s order77// (its default for those not given): an instance reading the face's78// bytes and sharing its lookup sets, which must outlive it.79fn instance_at(face: &Face, values: &[f32]) -> Face {80 const d = &face.data();81 var instance = face.shared();82 instance.values = Vector<f32>::from(&values);83 var coords = normalize(&d, &face.axes.view(), face.variations.avar, &values);84 var moved = false;85 for (coords.view()) |c| {86 moved = moved || c != 0.0;87 }88 if (moved) {89 instance.vary(move coords);90 }91 if (face.axis(&"wght") is ::Some |k|) {92 const a = &face.axes.get(k);93 const w = clamp(if (k < values.length()) values[k] else a.default, a.minimum, a.maximum);94 instance.weight = round(w) to u32;95 }96 instance.gsub_variation = feature_variation(&d, instance.gsub, &instance.coords.view());97 instance.gpos_variation = feature_variation(&d, instance.gpos, &instance.coords.view());98 return move instance;99}100101// Moves the instance to the coordinates: its variation stores' scalars102// there, and its metrics by MVAR's deltas.103fn vary(self face: &var Face, coords: Vector<f32>) {104 face.coords = move coords;105 const d = &face.data();106 const c = &face.coords.view();107 if (face.variations.hvar is ::Some |hvar|) {108 face.hvar_scalars = region_scalars(&d, hvar + u32_at(&d, hvar + 4) to u64, &c);109 }110 if (face.gdef_store() is ::Some |store|) {111 face.gdef_scalars = region_scalars(&d, store, &c);112 }113 if (face.glyphs is ::Cff |&outlines| && outlines is Cff |&cff| && cff.store is ::Some |store|) {114 face.cff_scalars = region_scalars(&d, store, &c);115 }116 if (face.variations.mvar is ::Some |mvar|) {117 const store = mvar + u16_at(&d, mvar + 10) to u64;118 const all = region_scalars(&d, store, &c);119 const scalars = &all.view();120 face.ascender += metric_delta(&d, mvar, tag(&"hasc"), &scalars);121 face.descender += metric_delta(&d, mvar, tag(&"hdsc"), &scalars);122 face.line_gap += metric_delta(&d, mvar, tag(&"hlgp"), &scalars);123 face.underline_position += metric_delta(&d, mvar, tag(&"undo"), &scalars);124 face.underline_thickness += metric_delta(&d, mvar, tag(&"unds"), &scalars);125 face.strikeout_position += metric_delta(&d, mvar, tag(&"stro"), &scalars);126 face.strikeout_thickness += metric_delta(&d, mvar, tag(&"strs"), &scalars);127 }128}129130// GDEF's item variation store, which GPOS's device tables index.131fn gdef_store(self face: &Face) -> Option<u64> {132 const gdef = if (face.gdef is ::Some |g|) g else {133 return ::None;134 };135 const d = &face.data();136 if (u16_at(&d, gdef + 2) < 3) {137 return ::None;138 }139 const offset = u32_at(&d, gdef + 14) to u64;140 return if (offset == 0) ::None else ::Some(gdef + offset);141}142143// A variation index device table's delta at the face's position, rounded144// to whole units as HarfBuzz rounds it; 0 for any other device table.145fn varied_device_delta(face: &Face, base: u64, offset: u32) -> f32 {146 const d = &face.data();147 const at = base + offset to u64;148 if (u16_at(&d, at + 4) != 0x8000) {149 return 0.0;150 }151 const store = if (face.gdef_store() is ::Some |s|) s else {152 return 0.0;153 };154 return round(store_delta(155 &d,156 store,157 u16_at(&d, at),158 u16_at(&d, at + 2),159 &face.gdef_scalars.view()160 ));161}162163// The glyph's horizontal advance at the face's position, from `hmtx`'s:164// moved by HVAR's delta or else by its phantom points', rounded as165// HarfBuzz rounds them.166fn varied_advance(face: &Face, glyph: u32, base: f32) -> f32 {167 const d = &face.data();168 if (face.variations.hvar is ::Some |hvar|) {169 const store = hvar + u32_at(&d, hvar + 4) to u64;170 const map = u32_at(&d, hvar + 8) to u64;171 const row = mapped_row(&d, if (map == 0) 0 else hvar + map, glyph);172 return round(base + store_delta(&d, store, row[0], row[1], &face.hvar_scalars.view()));173 }174 if (face.variations.gvar is ::Some |gvar| && face.glyphs is ::TrueType |&table|) {175 return max(round(face.phantom_advance(&table, gvar, glyph, base)), 0.0);176 }177 return base;178}179180// The advance between the glyph's phantom points once `gvar` moved them:181// its points' deltas are read, none inferred, as phantom points belong182// to no contour.183fn phantom_advance(184 self face: &Face,185 table: &TrueTypeGlyphs,186 gvar: u64,187 glyph: u32,188 base: f32189) -> f32 {190 const g = &face.glyph_data(&table, glyph);191 var count: u64 = 0;192 if (g.length() >= 10) {193 const contours = i16_at(&g, 0);194 if (contours > 0) {195 count = u16_at(&g, 10 + (contours to u64 - 1) * 2) to u64 + 1;196 } else if (contours < 0) {197 count = component_count(&g);198 }199 }200 var points = Vector<Moved>::filled(Moved { .x = 0.0, .y = 0.0 }, count + 4);201 points.get_var(count + 1).x = base;202 const no_contours = Vector<u32>::empty();203 vary(&face.data(), gvar, glyph, &face.coords.view(), &var points.items(), &no_contours.view());204 return points.get(count + 1).x - points.get(count).x;205}206207// Moves the points (the outline's or a composite's offsets, then the four208// phantom ones) by the glyph's `gvar` deltas at the face's position.209fn vary_points(face: &Face, glyph: u32, points: &var Vector<Moved>, ends: &[u32]) {210 if (face.variations.gvar is ::Some |gvar|) {211 vary(&face.data(), gvar, glyph, &face.coords.view(), &var points.items(), &ends);212 }213}214215// Whether the face draws the glyph in color (COLR, CBDT or sbix).216fn is_color_glyph(face: &Face, glyph: u32) -> bool => has_colr_glyph(&face, glyph)217 || has_bitmap_glyph(&face, glyph);218219// The color glyph's pixels at the size, COLR's painted first.220fn color_glyph(221 face: &Face,222 glyph: u32,223 size: f32,224 shift: f32,225 foreground: &TextColor226) -> Option<ColorMask> {227 if (228 paint_glyph(&face, glyph, size / face.units_per_em, shift, &foreground) is ::Some |move c|229 ) {230 return ::Some(move c);231 }232 return bitmap_glyph(&face, glyph, size);233}