From d1b588279f37e95581fc7fa8facffdd8c11a2296 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Mon, 16 Nov 2026 17:00:27 -0800 Subject: [PATCH] Text: a variable font's Windows cell height takes its MVAR changes FontFace::win_cell_height adds the MVAR `hcla` and `hcld` deltas to usWinAscent and usWinDescent at the face's location, so EMF and WMF type with a positive font height is sized against the instance it is drawn in. The variable test font gains the two records, and a test checks that Bold's cell is 200 units taller than Regular's. The comments and docs/mcp.md also say that characters the mapped face has no glyph for are drawn from other faces at the same size and Horizontal Scale. --- crates/metafile/src/import/picture.rs | 7 ++++--- crates/text/src/fontdb/face.rs | 15 +++++++++++---- crates/text/src/test_fonts.rs | 15 +++++++++++---- crates/text/src/tests.rs | 13 +++++++++++++ docs/mcp.md | 7 ++++--- docs/target-app-parity.md | 2 +- 6 files changed, 44 insertions(+), 15 deletions(-) diff --git a/crates/metafile/src/import/picture.rs b/crates/metafile/src/import/picture.rs index 2b1ae87380..3b1d4ef76e 100644 --- a/crates/metafile/src/import/picture.rs +++ b/crates/metafile/src/import/picture.rs @@ -548,11 +548,12 @@ impl Mapper { (false, false) => "Regular", } .into(); - // The face layout draws the type in (the fallback face for a font that isn't installed). + // The face the font maps to (the fallback face for a font that isn't installed). Characters + // it has no glyph for are drawn from other faces at the same size and Horizontal Scale. let face = vectorcraft_text::FontDb::global().style_face(&style); // A negative height is the em. A positive height is the character cell, and the em is - // the cell times unitsPerEm divided by the face's OS/2 usWinAscent plus usWinDescent - // (its own ascent plus descent when it has no OS/2 table). + // the cell times unitsPerEm divided by the face's OS/2 usWinAscent plus usWinDescent at + // its instance of a variable font (its own ascent plus descent when it has no OS/2 table). let cell_em = face.as_ref().and_then(|face| { let (a, d) = face.vertical_metrics(); Some(face.units_per_em() / face.win_cell_height().unwrap_or(a + d)).filter(|r| r.is_finite() && *r > 0.0) diff --git a/crates/text/src/fontdb/face.rs b/crates/text/src/fontdb/face.rs index 0e96e48008..f02ebea3fc 100644 --- a/crates/text/src/fontdb/face.rs +++ b/crates/text/src/fontdb/face.rs @@ -369,12 +369,19 @@ impl FontFace { self.skrifa().map_or(0.0, |f| f64::from(f.metrics(Size::unscaled(), self.location()).leading)).max(0.0) } /// The height of the face's character cell on Windows (the OS/2 table's `usWinAscent` plus - /// `usWinDescent`), in font units; `None` when the font has no OS/2 table or both are 0. + /// `usWinDescent`, at the face's location in a variable font), in font units; `None` when the + /// font has no OS/2 table or the sum isn't positive. pub fn win_cell_height(&self) -> Option { use skrifa::raw::TableProvider; - let o = self.skrifa()?.os2().ok()?; - let h = f64::from(o.us_win_ascent()) + f64::from(o.us_win_descent()); - (h > 0.0).then_some(h) + let f = self.skrifa()?; + let o = f.os2().ok()?; + // A variable font's MVAR table moves usWinAscent (`hcla`) and usWinDescent (`hcld`): + // https://learn.microsoft.com/en-us/typography/opentype/spec/mvar + let varied = |tag: &[u8; 4]| { + f.mvar().ok().and_then(|m| m.metric_delta(skrifa::Tag::new(tag), self.location().coords()).ok()).map_or(0.0, |d| d.to_f64()) + }; + let h = f64::from(o.us_win_ascent()) + varied(b"hcla") + f64::from(o.us_win_descent()) + varied(b"hcld"); + (h > 0.0 && h.is_finite()).then_some(h) } /// The average width of the face's characters (the OS/2 table's `xAvgCharWidth`), in font /// units; `None` when the font has no OS/2 table or gives no positive width. diff --git a/crates/text/src/test_fonts.rs b/crates/text/src/test_fonts.rs index b2deb5044f..1e1a195db0 100644 --- a/crates/text/src/test_fonts.rs +++ b/crates/text/src/test_fonts.rs @@ -1598,7 +1598,8 @@ pub const PATH_ALIGN_TYPO_FAMILY: &str = "Pathalign Typo Sans"; pub const PATH_ALIGN_HHEA_FAMILY: &str = "Pathalign Hhea Sans"; /// A family of [`path_align_font`]: as [`PATH_ALIGN_TYPO_FAMILY`], as a variable font with one /// weight axis (400–700, named instances Regular and Bold) whose `MVAR` table raises the -/// typographic ascender and descender by 100 at 700: a box from 0 to 1,000 in Bold. +/// typographic ascender and descender by 100 at 700: a box from 0 to 1,000 in Bold. It also raises +/// `usWinAscent` by 120 and `usWinDescent` by 80 at 700. pub const PATH_ALIGN_VARIABLE_FAMILY: &str = "Pathalign Variable Sans"; /// A family of [`path_align_font`]: Japanese code page, with no reference ideograph and no ┼. pub const PATH_ALIGN_NONE_FAMILY: &str = "Pathalign None Sans"; @@ -1693,10 +1694,16 @@ pub fn path_align_font(family: &str) -> Option> { } if family == PATH_ALIGN_VARIABLE_FAMILY { let region = VariationRegion::new(vec![RegionAxisCoordinates::new(F2Dot14::from_f32(0.0), F2Dot14::from_f32(1.0), F2Dot14::from_f32(1.0))]); - // Two delta sets (hasc, hdsc) of one 16-bit delta each for the one region. - let data = ItemVariationData::new(2, 1, vec![0], [100_i16.to_be_bytes(), 100_i16.to_be_bytes()].concat()); + // Four delta sets (hasc, hdsc, hcla, hcld) of one 16-bit delta each for the one region. + let deltas = [100_i16, 100, 120, 80].map(i16::to_be_bytes).concat(); + let data = ItemVariationData::new(4, 1, vec![0], deltas); let store = ItemVariationStore::new(VariationRegionList::new(1, vec![region]), vec![Some(data)]); - let records = vec![ValueRecord::new(Tag::new(b"hasc"), 0, 0), ValueRecord::new(Tag::new(b"hdsc"), 0, 1)]; + let records = vec![ + ValueRecord::new(Tag::new(b"hasc"), 0, 0), + ValueRecord::new(Tag::new(b"hcla"), 0, 2), + ValueRecord::new(Tag::new(b"hcld"), 0, 3), + ValueRecord::new(Tag::new(b"hdsc"), 0, 1), + ]; let axis = VariationAxisRecord { axis_tag: Tag::new(b"wght"), min_value: Fixed::from_i32(400), diff --git a/crates/text/src/tests.rs b/crates/text/src/tests.rs index ea5270636f..05a1ca1714 100644 --- a/crates/text/src/tests.rs +++ b/crates/text/src/tests.rs @@ -873,6 +873,19 @@ fn on_path_align_takes_a_variable_font_instances_em_box() { assert!(ascender.len() == 2 && (ascender[0] - 9.0).abs() < 1e-6 && (ascender[1] - 10.0).abs() < 1e-6, "{ascender:?}"); } +/// A variable font's instance takes its Windows character cell from the `usWinAscent` and +/// `usWinDescent` its `MVAR` table varies: 200 units taller in Bold. +#[test] +fn win_cell_height_takes_a_variable_font_instances_mvar() { + use crate::test_fonts::{PATH_ALIGN_VARIABLE_FAMILY, path_align_font}; + let db = FontDb::with_font_dirs(vec![]); + db.add_font(path_align_font(PATH_ALIGN_VARIABLE_FAMILY).unwrap()); + let st = |font_style: &str| CharStyle { font_family: PATH_ALIGN_VARIABLE_FAMILY.into(), font_style: font_style.into(), ..style(10.0) }; + let cell = |font_style: &str| db.style_face(&st(font_style)).and_then(|f| f.win_cell_height()).unwrap(); + let (regular, bold) = (cell("Regular"), cell("Bold")); + assert!((bold - regular - 200.0).abs() < 1e-6, "Regular {regular}, Bold {bold}"); +} + /// In vertical type, Align to Path moves the glyphs half an em, with their horizontal scale and not /// their vertical scale, for Ascender and Descender, and leaves them on the path for Center: their /// baselines and their drawn outlines both. diff --git a/docs/mcp.md b/docs/mcp.md index 3fa5c6d9c0..84956dec1f 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -8725,12 +8725,13 @@ pastes `image/emf` from the system clipboard ahead of text and bitmaps when the (the Windows desktop service doesn't read metafiles yet). The type size comes from the font's height. A negative height is the em. A positive height is the character cell, and -the em is the cell times the face's units per em divided by its OS/2 `usWinAscent` plus `usWinDescent` (its ascent -plus descent when it has no OS/2 table). When the face gives an average width (OS/2 `xAvgCharWidth`), a font's average +the em is the cell times the face's units per em divided by its OS/2 `usWinAscent` plus `usWinDescent`, with a +variable font's `MVAR` changes at the face's instance (its ascent plus descent when it has no OS/2 table). When the face gives an average width (OS/2 `xAvgCharWidth`), a font's average character width, negative or positive, becomes Horizontal Scale: the face's average width at the type size is stretched or narrowed to the font's width. VectorCraft's own rules are that a negative width is read as its absolute value and that the width is ignored when the height is 0. For a font the system doesn't have, both values are measured -in the face VectorCraft draws the type in. +in the face that replaces it. Characters that face has no glyph for are drawn from other faces at the same size and +Horizontal Scale. ```json {"name":"export","arguments":{"path":"/tmp/logo.emf"}} diff --git a/docs/target-app-parity.md b/docs/target-app-parity.md index 28aa0613c0..8e5caa4efb 100644 --- a/docs/target-app-parity.md +++ b/docs/target-app-parity.md @@ -1982,7 +1982,7 @@ Moved from the ROADMAP's "Shipped so far" on 2026-10-10. Update it when a featur - HEIC and HEIF images open and place from `.heic` and `.heif`, as the [supported file formats](https://helpx.adobe.com/illustrator/kb/supported-file-formats-illustrator.html) list High Efficiency Image under both Open and Place (and [Fix HEIF file import errors on Windows](https://helpx.adobe.com/illustrator/desktop/add-and-import-files/import-other-file-types/fix-heif-file-import-errors-on-windows.html) covers both ways of bringing HEIF files in): HEVC-coded single pictures and grids of tiles, 8 to 10 bits, with an alpha image, decoded by heic-rs in the engine's default `heif` feature. The container's rotation, mirror and crop are applied. Without an `nclx` box, the HEVC stream's video signal type gives the samples' matrix and range. An RGB ICC profile, or Display P3 with the sRGB curve named by an `nclx` box or, without a color box, by the stream, is converted to sRGB; the EXIF resolution tags size the image, or 72 ppi. Image sequences, other codings and grids of cropped tiles are refused with a message, and so is every HEIF file in a build without the feature. The decoder checks image size before allocating (`crates/engine/src/cmd/fileio/tests_heifread.rs`). - HP-GL/2 plot files (`.plt`, `.hpgl`, `.hgl`, `.hpg`, or a name no other format reads when the file starts with plotter instructions) open and place as paths on one layer, the artboard round the art: each line drawn with the pen down is a stroked path in its pen's color and width, line types become dash patterns, circles, arcs, wedges and Bézier curves are curves, filled and edged rectangles, wedges and polygons (polygon mode) are filled or stroked paths, and encoded polylines, scaling points, user units and rotation are read. Labels, symbol mode's symbols, raster images and hatching aren't drawn and are listed in the warnings (`crates/cad/src/hpgl/tests.rs`, `crates/engine/src/cmd/fileio/tests_hpgl.rs`). - PICT pictures (`.pct`, `.pic`, and `.pict` as VectorCraft's own name) open and place, as [the help page](https://helpx.adobe.com/archive/illustrator/illustrator-cs5-5-troubleshooting.pdf) lists Macintosh PICT under both opening and placing: versions 1 and 2, with or without the file's 512 bytes of application data, read by the opcodes of Apple's [Opcodes in Pictures](https://dev.os9.ca/techpubs/mac/QuickDraw/QuickDraw-461.html). Lines, rectangles, rounded rectangles, ovals, arcs, polygons and regions become paths in their pen and pattern colors, text becomes point type in the font the picture names, bitmaps, pixel maps and QuickTime JPEG images become images, and the clip region clips what follows it; File › Place places the picture as one group clipped to its frame. Inverted shapes, other transfer modes and other QuickTime formats come back as warnings, and a picture cut short keeps what came before it (`crates/metafile/src/tests/pict.rs`, `crates/engine/src/cmd/fileio/tests_pict.rs`). - - EMF and WMF type takes its size from the font's height, where a negative height is the em and a positive height is the character cell, and a font's average character width becomes Horizontal Scale against the face's own average width (OS/2 `xAvgCharWidth`) when the face gives one, as [LOGFONT](https://learn.microsoft.com/en-us/windows/win32/api/wingdi/ns-wingdi-logfontw) and [MS-EMF 2.2.13](https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-emf/1cd7cb7a-1a7c-48d2-b4a4-c218f9d12100) describe the two fields. VectorCraft's own rules are that the cell is the face's `usWinAscent` plus `usWinDescent` (its ascent plus descent without an OS/2 table), that a negative width is read as its absolute value, and that the width is ignored when the height is 0 (`a_font_width_becomes_horizontal_scale`, `a_positive_font_height_is_the_character_cell`, `a_font_the_system_lacks_is_measured_in_the_face_that_draws_it`, `wmf_font_width_becomes_horizontal_scale`). + - EMF and WMF type takes its size from the font's height, where a negative height is the em and a positive height is the character cell, and a font's average character width becomes Horizontal Scale against the face's own average width (OS/2 `xAvgCharWidth`) when the face gives one, as [LOGFONT](https://learn.microsoft.com/en-us/windows/win32/api/wingdi/ns-wingdi-logfontw) and [MS-EMF 2.2.13](https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-emf/1cd7cb7a-1a7c-48d2-b4a4-c218f9d12100) describe the two fields. VectorCraft's own rules are that the cell is the face's `usWinAscent` plus `usWinDescent` at a variable font's instance (its ascent plus descent without an OS/2 table), that a negative width is read as its absolute value, and that the width is ignored when the height is 0 (`a_font_width_becomes_horizontal_scale`, `a_positive_font_height_is_the_character_cell`, `a_font_the_system_lacks_is_measured_in_the_face_that_draws_it`, `wmf_font_width_becomes_horizontal_scale`). - EMF and WMF files are read by the shared `craft-metafile` reader and open, place and paste as editable paths, clipping groups, images and point type: an EMF drawn with EMF+ records (on their own, or beside plain records) is read from them instead of opening empty, and hatched and pattern brushes fill with their average color (`crates/metafile/src/tests/import.rs`). - Export As › HP-GL (`document.export {format: "hpgl"}`) writes a `.plt` plot of an artboard, each artboard or the art for pen plotters and cutters: each visible path once along its line in plotter units from the artboard's bottom-left corner or center, a pen per color or a pen table, curves flattened to a tolerance, type as outlines, clipping masks and the artboard's edges applied, and HP-GL or HP-GL/2 (pen colors and encoded polylines); plots read back by the HP-GL reader give the same lines (`crates/cad/src/hpgl/write/tests.rs`, `crates/engine/src/cmd/fileio/tests_hpgl.rs`). - HP-GL export's `insideFirst` cuts each pen's paths inside before outside: a path is drawn only after the pen's paths that lie inside it (a letter's counter, a part's holes, rings within rings), touching outlines counted as inside and crossing ones not, in paint order or nearest first with `sortPaths`, within the plot's work budget; these order rules are VectorCraft's own (`crates/cad/src/hpgl/write/nest.rs`, `crates/cad/src/hpgl/write/tests.rs`, `crates/engine/src/cmd/fileio/tests_hpgl.rs`).