From 103b81427692ae50b296ae94c63e8a35c67c950c Mon Sep 17 00:00:00 2001 From: chasse20 Date: Fri, 27 Nov 2026 08:20:53 -0500 Subject: [PATCH] Place implicit glyph joins on whole design coordinates --- crates/eval/src/tests/font_resource.rs | 42 +++++++++++++++++++++++--- crates/nodes/src/atomic/font.rs | 18 +++++++++-- docs/parity-fixes.md | 10 ++++++ docs/sbs-root-nodes.md | 2 +- 4 files changed, 64 insertions(+), 8 deletions(-) diff --git a/crates/eval/src/tests/font_resource.rs b/crates/eval/src/tests/font_resource.rs index ddfbb74d..561c6fe5 100644 --- a/crates/eval/src/tests/font_resource.rs +++ b/crates/eval/src/tests/font_resource.rs @@ -22,9 +22,13 @@ fn outline_font(metrics: [i16; 3], contour: &[(i16, i16, bool)]) -> Vec { head[..4].copy_from_slice(&0x10000u32.to_be_bytes()); head[12..16].copy_from_slice(&0x5f0f3cf5u32.to_be_bytes()); head[18..20].copy_from_slice(&1024i16.to_be_bytes()); - let width = contour.iter().map(|p| p.0).max().unwrap(); - let height = contour.iter().map(|p| p.1).max().unwrap(); - head[40..44].copy_from_slice(&words(&[width, height])); + let bounds = [ + contour.iter().map(|p| p.0).min().unwrap(), + contour.iter().map(|p| p.1).min().unwrap(), + contour.iter().map(|p| p.0).max().unwrap(), + contour.iter().map(|p| p.1).max().unwrap(), + ]; + head[36..44].copy_from_slice(&words(&bounds)); let mut hhea = vec![0; 36]; hhea[..4].copy_from_slice(&0x10000u32.to_be_bytes()); hhea[4..12].copy_from_slice(&words(&[metrics[0], metrics[1], metrics[2], 640])); @@ -35,7 +39,7 @@ fn outline_font(metrics: [i16; 3], contour: &[(i16, i16, bool)]) -> Vec { maxp[4..10].copy_from_slice(&words(&[2, count, 1])); let mut cmap = words(&[0, 1, 3, 1, 0, 12]); cmap.extend(words(&[4, 32, 0, 4, 4, 1, 0, 65, -1, 0, 65, -1, -64, 1, 0, 0])); - let mut glyf = words(&[1, 0, 0, width, height, count - 1, 0]); + let mut glyf = words(&[1, bounds[0], bounds[1], bounds[2], bounds[3], count - 1, 0]); glyf.extend(contour.iter().map(|p| u8::from(p.2))); for axis in 0..2 { let mut previous = 0; @@ -179,6 +183,36 @@ fn package_font_quadratic_detail_tracks_the_text_scale() { } } +#[test] +fn adjacent_quadratic_controls_place_their_join_in_whole_design_units() { + let metrics = [768, -256, 0]; + let (mut package, id) = text_sample( + [7, 6], + metrics, + &[ + ("text", Value::Text("A".into())), + ("fontsize", Value::Float(0.5)), + ("alignment", Value::Int(0)), + ("position", Value::Float2([-0.1875, 1.0 / 256.0])), + ], + ); + let implicit = [(0, 0, true), (55, -385, false), (492, 126, false), (675, 612, false), (1010, 0, true)]; + let explicit = [implicit[0], implicit[1], (273, -130, true), implicit[2], (583, 369, true), implicit[3], implicit[4]]; + let cache = Mutex::new(Cache::default()); + let mut render = |contour: &[(i16, i16, bool)]| { + package.resources[0].bytes = ResourceBytes::new(outline_font(metrics, contour)); + let result = cook(&package, 0, &Options::new(&no_res), &cache); + assert_eq!(result.nodes[&id].error, None); + result.image(id, 0).unwrap().clone() + }; + let expected = render(&explicit); + assert!(expected.data().contains(&1.0)); + let actual = render(&implicit); + for (pixel, (a, b)) in actual.data().iter().zip(expected.data()).enumerate() { + assert_eq!(a, b, "pixel {pixel} at the implicit join"); + } +} + #[test] fn package_font_coverage_interpolates_the_background_channels() { let (mut package, id) = text_sample( diff --git a/crates/nodes/src/atomic/font.rs b/crates/nodes/src/atomic/font.rs index cd5ab76f..dd5a3c00 100644 --- a/crates/nodes/src/atomic/font.rs +++ b/crates/nodes/src/atomic/font.rs @@ -3,7 +3,7 @@ use std::collections::BTreeMap; use craft_font_catalog::{FontOutline, RunGlyph, RunInput, ShapePlanCache, shape_run}; use harfrust::{Direction, Shaper}; -use read_fonts::TableProvider; +use read_fonts::{TableProvider, tables::glyf::Glyph as StoredGlyph, types::GlyphId}; use vello_cpu::{ Pixmap, RenderContext, Resources, color::palette::css::WHITE, @@ -22,9 +22,18 @@ struct Glyph { } impl Glyph { - fn new(path: BezPath, pixels_per_unit: f64, remaining: &mut usize) -> Result { + fn new(mut path: BezPath, simple_contour: bool, pixels_per_unit: f64, remaining: &mut usize) -> Result { let budget_error = "font exceeds the outline storage budget"; *remaining = remaining.checked_sub(path.elements().len()).ok_or(budget_error)?; + if simple_contour { + // Stored TrueType points have integer design coordinates. The join between + // adjacent controls, including a contour's starting join, uses the lower unit. + for element in path.elements_mut() { + if let PathEl::MoveTo(join) | PathEl::QuadTo(_, join) = element { + *join = join.floor(); + } + } + } let bounds = path.bounding_box(); if !path.elements().iter().any(|element| matches!(element, PathEl::QuadTo(..))) { return Ok(Self { path, bounds }); @@ -160,6 +169,7 @@ pub fn text_with_font(context: &CookContext<'_>, bytes: &[u8]) -> CookResult { return Err("font has no design units per em".into()); } let font = harfrust::FontRef::from_index(bytes, 0).map_err(|e| e.to_string())?; + let contours = font.glyf().ok().zip(font.loca(None).ok()); let header = font.hhea().map_err(|e| e.to_string())?; let ascent = f64::from(header.ascender().to_i16()); let descent = f64::from(header.descender().to_i16()); @@ -205,7 +215,9 @@ pub fn text_with_font(context: &CookContext<'_>, bytes: &[u8]) -> CookResult { for glyph in glyphs { if let std::collections::btree_map::Entry::Vacant(entry) = cache.entry(glyph.glyph_id) { let path = face.outline(glyph.glyph_id).map_err(|e| e.to_string())?; - entry.insert(Glyph::new(path, pixels_per_unit, &mut outline_budget)?); + let simple_contour = + contours.as_ref().is_some_and(|(glyf, loca)| matches!(loca.get_glyf(GlyphId::new(glyph.glyph_id), glyf), Ok(Some(StoredGlyph::Simple(_))))); + entry.insert(Glyph::new(path, simple_contour, pixels_per_unit, &mut outline_budget)?); } let ink = cache.get(&glyph.glyph_id).ok_or("missing glyph outline")?; let x = cursor + f64::from(glyph.x_offset); diff --git a/docs/parity-fixes.md b/docs/parity-fixes.md index 0404316b..b3c84f08 100644 --- a/docs/parity-fixes.md +++ b/docs/parity-fixes.md @@ -59,6 +59,16 @@ The first pass classifies all 546 roots: 166 Parity and 380 Too hard. These meas ## 2026-11-27 root comparisons +### Whole design coordinates at implicit glyph joins + +With the same linked, licensed monospace font, all **30 Points Preview settings match 17,920/17,920 pixels** on both one and eight reference processors, maximum zero. The seven previously differing pixels all had count 12. The independent 42 Text renders now match 83,328/83,328, up from 82,571. They cover individual glyphs, digits and the four-line label sheet from 8² through 128 × 64. The catalog-path comparison selects different faces and gives 17,548/17,920, maximum .380392157. [Whole-root inputs and sizes](#stored-and-wired-values-at-graph-instance-inputs) + +Draw an original TrueType A with a quadratic from (0,0) through consecutive off-curve controls (55,−385), (492,126) and (675,612), ending at (1010,0). Close along the baseline. Use 1,024 units per em, ascent 768, descent −256, size .5, left alignment and offset (−.1875,1/256). Insert explicit on-curve points (273,−130) and (583,369) between the controls in a second face. The reference renders both faces identically at 64², 128 × 64 and 64 × 128, or 20,480/20,480 pixels. Rounding each midpoint upwards instead gives 20,288/20,480, maximum .047058821. MaterialCraft now uses the lower whole design coordinate for these implied joins in simple TrueType contours. The original negative-coordinate face improves from 20,330 to 20,480 exact. A positive-coordinate variant with controls (51,789), (492,286) and (671,−125) reaches 20,462/20,480, maximum one grayscale level. Composite glyph transforms remain a separate measurement. + +For four ordinary file saves, compare original and reopened Text output in each application at one and eight processors. The positive-coordinate face at 64² U8, seed 0 gives 4,096/4,096. The negative-coordinate face at 128 × 64 U16, seed 42 gives 8,192/8,192. A licensed zero at 64 × 32 F16, seed 261017 gives 2,048/2,048. The licensed label sheet at 32 × 64 F32, seed 903331 gives 2,048/2,048. Maximum error is zero in all four cases. + +An additional original font uses rectangular digits that extend above the ascent and below the descent. In 15 whole-root settings with that face, Points Preview matches 8,952/8,960, maximum .0039215684. The same results recur after an ordinary save. Isolating individual digits reproduces their coverage in both applications. Where the combined glyphs overlap, the two partial coverages 34 and 94 produce 115 in MaterialCraft and 116 in the reference. Coverages 183 and 48 produce 197 and 196 respectively. The root stays in progress for these same-input differences. + ### Quadratic glyph outlines in Points Preview In the same 30 whole-root settings, Points Preview with the same licensed font now matches **17,913/17,920 pixels** on both one and eight reference processors. The previous result was 17,869/17,920. The largest difference fell from 42 grayscale levels to one (.0039215684 on F32 readback). Point-only output matches 5,376/5,376. Labels alone match 5,371/5,376, and points plus labels including the empty input match 7,166/7,168. All seven remaining differences occur at count 12. The catalog-path comparison, where the reference resolves a different font, gives 17,548/17,920 and a maximum of .380392157. The root remains in progress. [Whole-root inputs, offsets and label sizes](#stored-and-wired-values-at-graph-instance-inputs) diff --git a/docs/sbs-root-nodes.md b/docs/sbs-root-nodes.md index c206570f..47065198 100644 --- a/docs/sbs-root-nodes.md +++ b/docs/sbs-root-nodes.md @@ -736,7 +736,7 @@ Creased matches 10,947,840/10,947,840 pixels across 100 distinct settings throug | [spline_tools.sbs/internal_tools/get_spline_point_data](../assets/sbs/spline_tools.sbs) | 1 | Yes | [Parity: 4 cases](parity-fixes.md#verified-cases) | | [spline_tools.sbs/internal_tools/modify_spline_property_sub](../assets/sbs/spline_tools.sbs) | 4 | Yes | [Parity · 28,928 exact pixels in 19 renders](parity-fixes.md#clamped-texture-reads-in-spline-graphs) | | [spline_tools.sbs/internal_tools/normalize_spline_distance](../assets/sbs/spline_tools.sbs) | 1 | Yes | [Parity · 16 cases, 52,480 exact pixels](parity-fixes.md#tile-edge-masks-and-normalized-spline-distances) | -| [spline_tools.sbs/internal_tools/points_preview](../assets/sbs/spline_tools.sbs) | 3 | Yes | [In progress · 17,913/17,920 same-font whole-root pixels, seven differ by one level](parity-fixes.md#quadratic-glyph-outlines-in-points-preview) · [25,088/25,088 empty and point-only pixels](parity-fixes.md#font-line-height-and-coverage-over-a-constant-background) · [Previous whole-root comparison](parity-fixes.md#text-block-bounds-for-linked-font-outlines) | +| [spline_tools.sbs/internal_tools/points_preview](../assets/sbs/spline_tools.sbs) | 3 | Yes | [In progress · 17,920/17,920 with the linked monospace face, 8,952/8,960 with the overlapping digit outlines](parity-fixes.md#whole-design-coordinates-at-implicit-glyph-joins) · [25,088/25,088 empty and point-only pixels](parity-fixes.md#font-line-height-and-coverage-over-a-constant-background) | | [spline_tools.sbs/internal_tools/resample_spline_data](../assets/sbs/spline_tools.sbs) | 3 | Yes | [Parity · 29,952 exact pixels in 19 renders](parity-fixes.md#clamped-texture-reads-in-spline-graphs) | | [spline_tools.sbs/internal_tools/scatter_splines_on_splines_preprocess](../assets/sbs/spline_tools.sbs) | 5 | Yes | [Parity · 622,592/622,592 pixels and 16/16 counts, both full-library saves exact](parity-fixes.md#spline-preprocessing-with-empty-inputs-and-complete-library-saves) · [Four output formats](parity-fixes.md#spline-preprocessing-with-wired-counts) | | [spline_tools.sbs/internal_tools/select_spline_sub](../assets/sbs/spline_tools.sbs) | 3 | Yes | [Parity · 622,592 exact pixels in 19 renders](parity-fixes.md#clamped-texture-reads-in-spline-graphs) |