mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-20 17:37:10 +01:00
Metafile import: keep the opaque text background to its own change
This takes three behavior changes out of the branch, so that it holds the opaque background and the character cell it fills: - A single line without advances is aligned as before, without its trailing spaces. Its cells run across its glyphs' advances from where its type starts; blanks alone, which draw no type, are filled where the alignment puts them. - NULs after a line break add their advance to the character before them, as before. - Blanks that are underlined or struck through draw nothing, as before. A string of NULs alone in an underlined font now fills its cells and adds no empty type object. docs/mcp.md no longer describes the three, and docs/target-app-parity.md lists the background and the cell. The first commit's body names FontFace::win_ascent_descent as Face::win_ascent_descent. A device context starts with a background color that is typically white; the first commit states white as the playback default.
This commit is contained in:
1 parent
9dcc6913d8
commit
8993be043f
4 files changed
+35
-58
No files matched your search
@@ -16,13 +16,11 @@
|
||||
//! range reaches, or a tab) are point type of their own, and an underline or strikethrough is carried across the space between.
|
||||
//! It is also carried on to the end of the last character's advance. Type on more than one line
|
||||
//! is placed a line at a time: each line is aligned by its own advances and split as a single
|
||||
//! line is. NULs at the start, or after a line break, move the start of their line. A
|
||||
//! character cell runs from the font's OS/2 usWinAscent above the baseline to its usWinDescent
|
||||
//! below it, and top or bottom aligned text has the top or the bottom of its cells at the
|
||||
//! reference point. One line without advances is aligned as long as its glyphs' advances,
|
||||
//! trailing spaces included. Text drawn in the opaque background mode (the mode a playback
|
||||
//! starts in) has one rectangle of the background color behind the character cells of each
|
||||
//! line, as far as its advances reach.
|
||||
//! line is. A character cell runs from the font's OS/2 usWinAscent above the baseline to its
|
||||
//! usWinDescent below it, and top or bottom aligned text has the top or the bottom of its cells
|
||||
//! at the reference point. Text drawn in the opaque background mode (the mode a playback starts
|
||||
//! in) has one rectangle of the background color behind the character cells of each line, as
|
||||
//! far as its advances reach.
|
||||
//! - Clip regions and clip paths become clipping groups around what they clip; a rectangle around
|
||||
//! the whole picture clips nothing and is left out.
|
||||
//!
|
||||
@@ -545,22 +543,20 @@ impl Mapper {
|
||||
fn text(&mut self, t: &craft_metafile::Text, chain: Vec<Arc<Clip>>) {
|
||||
// The characters without NULs, and each one's advance (text units) when the file gives
|
||||
// one for every character: a NUL's advance goes to the character before it, or moves the
|
||||
// start when the string, or a line after a line break, begins with one.
|
||||
// start when the string begins with one.
|
||||
let advances = t.dx.len() == t.text.chars().count() && t.dx.iter().all(|d| d.is_finite());
|
||||
let (mut s, mut dx) = (String::new(), Vec::new());
|
||||
// How far before and after its start each character's advances (its own and the NULs'
|
||||
// after it) reach (text units): an advance may be negative. And before each character,
|
||||
// the advance of the NULs that move its line's start, with how far they reach.
|
||||
let (mut reach, mut leads) = (Vec::new(), Vec::new());
|
||||
// after it) reach (text units): an advance may be negative. And the advance of the NULs
|
||||
// that move the start, with how far they reach.
|
||||
let mut reach = Vec::new();
|
||||
let mut lead = Lead::default();
|
||||
let breaks = |c: char| matches!(c, '\n' | '\r' | '\u{b}' | '\u{c}' | '\u{85}' | '\u{2028}' | '\u{2029}');
|
||||
for (c, d) in t.text.chars().zip(t.dx.iter().map(|d| f64::from(*d)).chain(std::iter::repeat(0.0))) {
|
||||
if c != '\0' {
|
||||
s.push(c);
|
||||
dx.push(d);
|
||||
reach.push((d.min(0.0), d.max(0.0)));
|
||||
leads.push(std::mem::take(&mut lead));
|
||||
} else if let (Some(last), Some(r), false) = (dx.last_mut(), reach.last_mut(), s.chars().next_back().is_none_or(breaks)) {
|
||||
} else if let (Some(last), Some(r)) = (dx.last_mut(), reach.last_mut()) {
|
||||
*last += d;
|
||||
*r = (r.0.min(*last), r.1.max(*last));
|
||||
} else {
|
||||
@@ -571,15 +567,13 @@ impl Mapper {
|
||||
if !advances {
|
||||
dx.clear();
|
||||
reach.clear();
|
||||
leads.clear();
|
||||
lead = Lead::default();
|
||||
}
|
||||
// The leading NULs of the first line, or of a string of NULs alone.
|
||||
let first = leads.first().copied().unwrap_or(lead);
|
||||
// Type that draws nothing still fills its character cells on an opaque background. Blanks
|
||||
// draw when they are underlined or struck through.
|
||||
// The leading NULs, of the text or of a string of NULs alone.
|
||||
let first = lead;
|
||||
// Type that draws nothing still fills its character cells on an opaque background.
|
||||
let background = t.background.filter(|c| c[3] > 0);
|
||||
let shows = (!s.trim().is_empty() || t.font.underline || t.font.strike) && t.color[3] > 0;
|
||||
let shows = !s.trim().is_empty() && t.color[3] > 0;
|
||||
// A string of NULs only fills the cells across its advances.
|
||||
let nuls = s.is_empty() && first.reach.0 < first.reach.1 && background.is_some();
|
||||
if (s.is_empty() && !nuls) || !(shows || background.is_some()) {
|
||||
@@ -692,23 +686,25 @@ impl Mapper {
|
||||
if dx.is_empty() {
|
||||
let mut text = whole;
|
||||
let laid = vectorcraft_text::layout(db, &text);
|
||||
// One line is as long as its advances, its trailing blanks included, where the
|
||||
// alignment puts a line that long.
|
||||
// Where one line's glyphs start, and the sum of their advances, its trailing blanks
|
||||
// included.
|
||||
let along = laid
|
||||
.glyphs
|
||||
.iter()
|
||||
.fold((f64::INFINITY, 0.0), |(from, width), g| (from.min(g.origin.x.min(g.origin.x + g.advance)), width + g.advance));
|
||||
let one = laid.lines.len() == 1 && along.0.is_finite() && along.1.is_finite();
|
||||
let shift = if one { aligned(0.0, along.1, &t.h_align) - along.0 } else { 0.0 };
|
||||
text.xf = base * Affine::translate((if shift.is_finite() { shift } else { 0.0 }, drop));
|
||||
text.xf = base * Affine::translate((0.0, drop));
|
||||
text.cached_bounds = Some(laid.bounds);
|
||||
// The cells of one line run across its advances, those of more than one across each
|
||||
// line's glyphs. NULs alone span their advances, where the alignment puts them.
|
||||
// The cells of one line run across its advances, from where its type starts, or for
|
||||
// blanks alone, which draw no type, where the alignment puts a line that long. Those
|
||||
// of more than one line run across each line's glyphs. NULs alone span their
|
||||
// advances, where the alignment puts them.
|
||||
let cells = if nuls {
|
||||
let start = aligned(first.by * k, 0.0, &t.h_align) - first.by * k;
|
||||
cell_rect(start + first.reach.0 * k, start + first.reach.1 * k, 0.0, cell)
|
||||
} else if one {
|
||||
cell_rect(along.0, along.0 + along.1, laid.glyphs.first().map_or(0.0, |g| g.origin.y), cell)
|
||||
let x0 = if shows { along.0 } else { aligned(0.0, along.1, &t.h_align) };
|
||||
cell_rect(x0, x0 + along.1, laid.glyphs.first().map_or(0.0, |g| g.origin.y), cell)
|
||||
} else {
|
||||
cells(&laid, &s, cell, &t.h_align)
|
||||
};
|
||||
@@ -734,11 +730,11 @@ impl Mapper {
|
||||
let starts: Vec<usize> = s.char_indices().map(|(at, _)| at).collect();
|
||||
let count = |to: usize| starts.partition_point(|at| *at < to);
|
||||
let top = laid.lines.first().map_or(0.0, |l| l.baseline);
|
||||
for l in &laid.lines {
|
||||
for (n, l) in laid.lines.iter().enumerate() {
|
||||
let (from, to) = (count(l.start), count(l.end));
|
||||
let (Some(cs), Some(dx), Some(reach)) = (chars.get(from..to), dx.get(from..to), reach.get(from..to)) else { continue };
|
||||
// The NULs before the line's first character, at the start or after a line break.
|
||||
let lead = leads.get(from).copied().unwrap_or_default().scaled(k);
|
||||
// A leading NUL's advance comes before the first line only.
|
||||
let lead = if n == 0 { first.scaled(k) } else { Lead::default() };
|
||||
let down = l.baseline - top;
|
||||
self.line(&line, cs, Advances { dx, reach, lead }, if down.is_finite() { down } else { 0.0 }, &mut out);
|
||||
}
|
||||
|
||||
@@ -1126,22 +1126,6 @@ fn opaque_backgrounds_fill_behind_the_character_cells() {
|
||||
assert_eq!(kinds, [false, true, true]);
|
||||
let b = layer[0].geometric_bounds().unwrap();
|
||||
assert!((b.x0 - 10.0 * mm).abs() < 0.01 && (b.x1 - 145.0 * mm).abs() < 0.01, "{b:?}");
|
||||
// A NUL after a line break moves the start of the line it begins, as it moves the first
|
||||
// line's: "\0B" on a line of its own is placed and filled as the same record alone.
|
||||
// Where the last text object's first glyph starts, and the left edges of the backgrounds.
|
||||
let placed = |layer: &[std::sync::Arc<Node>]| {
|
||||
let last = layer.iter().rev().find_map(|n| match &n.kind {
|
||||
NodeKind::Text(t) => Some(glyph_xs(t).0[0]),
|
||||
_ => None,
|
||||
});
|
||||
let backs: Vec<f64> =
|
||||
layer.iter().filter(|n| matches!(n.kind, NodeKind::Path { .. })).filter_map(|n| n.geometric_bounds()).map(|b| b.x0).collect();
|
||||
(last.unwrap(), backs)
|
||||
};
|
||||
let (b_alone, _) = placed(&opaque(arial(), "\0B", &[2000, 1000]));
|
||||
let (b_together, backs) = placed(&opaque(arial(), "A\n\0B", &[1000, 0, 2000, 1000]));
|
||||
assert!((b_alone - 30.0 * mm).abs() < 0.01 && (b_together - b_alone).abs() < 0.01, "{b_alone} {b_together}");
|
||||
assert!(backs.len() == 2 && backs.iter().all(|x| (x - 10.0 * mm).abs() < 0.01), "{backs:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -1156,13 +1140,13 @@ fn opaque_backgrounds_reach_as_far_as_the_advances() {
|
||||
let NodeKind::Path { path, .. } = &layer[0].kind else { panic!("a path") };
|
||||
let rects = path.to_bezpath().elements().iter().filter(|e| matches!(e, vectorcraft_geom::PathEl::MoveTo(_))).count();
|
||||
assert_eq!(rects, 2);
|
||||
// Without advances, a line is aligned as long as its advances, trailing spaces included, and
|
||||
// its cells run across them: right aligned at 40 mm, "Hi " and its cells end there.
|
||||
// Without advances, one line's cells run across its glyphs' advances, trailing spaces
|
||||
// included: right aligned, they run from the "H" to the end of the space after the "i".
|
||||
let layer = aligned(2, 26, 4000, arial(), "Hi ", &[]);
|
||||
let NodeKind::Text(t) = &layer[1].kind else { panic!("text") };
|
||||
let (xs, end) = glyph_xs(t);
|
||||
let b = layer[0].geometric_bounds().unwrap();
|
||||
assert!(xs.len() == 3 && (end - 40.0 * mm).abs() < 0.01, "{xs:?} {end}");
|
||||
assert!(xs.len() == 3, "{xs:?} {end}");
|
||||
assert!((b.x0 - xs[0]).abs() < 0.01 && (b.x1 - end).abs() < 0.01, "{b:?} {xs:?} {end}");
|
||||
// On more than one line, each line is aligned without its trailing spaces, and so are its
|
||||
// cells: right aligned at 40 mm, neither passes it.
|
||||
@@ -1190,11 +1174,9 @@ fn opaque_backgrounds_reach_as_far_as_the_advances() {
|
||||
let b = layer[0].geometric_bounds().unwrap();
|
||||
assert!((b.x0 - 10.0 * mm).abs() < 0.01 && b.x1 > 10.0 * mm, "{b:?}");
|
||||
assert!(drawn(1, arial(), " \t", &[]).is_empty());
|
||||
// Underlined blanks draw their underline, on a transparent background too.
|
||||
for mode in [1, 2] {
|
||||
let layer = drawn(mode, font_of("Arial", true), " ", &[]);
|
||||
assert!(layer.iter().any(|n| matches!(&n.kind, NodeKind::Text(t) if t.runs[0].style.underline)), "{mode}");
|
||||
}
|
||||
// NULs alone in an underlined font fill their cells and add no type.
|
||||
let layer = opaque(font_of("Arial", true), "\0", &[2000]);
|
||||
assert!(layer.len() == 1 && matches!(layer[0].kind, NodeKind::Path { .. }), "{layer:?}");
|
||||
// A file that never sets the mode draws in the opaque mode a playback starts in.
|
||||
let layer = drawn(0, arial(), "Hi", &[]);
|
||||
assert_eq!(fill_rgb(&layer[0]), Some([0, 255, 0]));
|
||||
|
||||
+2
-4
@@ -9875,11 +9875,9 @@ images and point type in the font the file names. Type keeps the file's characte
|
||||
kerning off. On a single left to right line, an advance that tracking can't give (wider than its range, or a tab)
|
||||
starts a new point type object at the position the advance gives, and an underline or strikethrough is filled across the space between and on to the end
|
||||
of the last character's advance. Type on more than one line is placed a line at a time: each line is aligned by its own advances and
|
||||
split as a single line is. NULs at the start of the text, or after a line break, move the start of their line by their
|
||||
advances. Blanks that are underlined or struck through draw their bars. A character cell runs from the font's OS/2
|
||||
split as a single line is. A character cell runs from the font's OS/2
|
||||
`usWinAscent` above the baseline to its `usWinDescent` below it: top and bottom aligned text has the top or the
|
||||
bottom of its cells at the reference point. A single line without character advances is aligned as long as its
|
||||
glyphs' advances, trailing spaces included. Text drawn in the opaque background mode, which a file is in until it
|
||||
bottom of its cells at the reference point. Text drawn in the opaque background mode, which a file is in until it
|
||||
sets the mode to transparent, gets one filled rectangle of the background color behind the character cells of each
|
||||
line, as far as its advances reach (NULs' included), placed before the type. An EMF that
|
||||
draws with EMF+ records is read from them. Hatched and pattern brushes fill with their average color. Records VectorCraft doesn't read are skipped with one warning.
|
||||
|
||||
@@ -2271,6 +2271,7 @@ Moved from the ROADMAP's "Shipped so far" on 2026-10-10. Update it when a featur
|
||||
- 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`).
|
||||
- EMF, WMF and EMF+ text keeps the character advances the file gives (ExtTextOut's `lpDx`, DrawDriverString positions) as tracking, with pair kerning off; on a single left to right line, an advance that tracking can't give (wider than its range, or a tab) starts a new point type object where the advance puts it, with an underline or strikethrough filled across the space between and on to the end of the last character's advance; type on more than one line is placed a line at a time, each line aligned by its own advances and split as a single line is (`text_is_spaced_by_its_advances`, `multiline_text_with_advances_aligns_each_line`, `wmf_text_is_spaced_by_its_advances`, `emf_plus_driver_strings_keep_their_positions`, `split_type_keeps_its_underline_and_right_to_left_order`).
|
||||
- EMF and WMF text drawn in the opaque background mode, which a playback starts in ([MS-EMF 2.1.4 BackgroundMode](https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-emf/2f57054c-2b69-4303-a6f8-5aacd401f759), [SetBkMode](https://learn.microsoft.com/en-us/windows/win32/api/wingdi/nf-wingdi-setbkmode)), gets one rectangle of the background color behind each line's character cells, placed before the type; transparent text gets none. A character cell runs from the font's OS/2 `usWinAscent` above the baseline to its `usWinDescent` below it, and top or bottom aligned text puts the top or the bottom of the cell at the reference point (`opaque_backgrounds_fill_behind_the_character_cells`, `opaque_backgrounds_reach_as_far_as_the_advances`, `wmf_text_fills_its_cells_in_the_opaque_background_mode`).
|
||||
- EMF and WMF import warns about what the reader leaves out or does not play as the file asks: a file, or a metafile image inside it, cut short by one of the reader's limits (items, bytes kept, saved states, clip paths), with one warning without a count, and, with counts, images and pattern brushes that are not read, clips that are not applied, and raster operations other than a plain copy (`a_file_cut_short_by_another_limit_says_so`, `images_the_reader_does_not_read_are_counted`, `clips_that_are_not_applied_are_counted`, `raster_operations_that_are_not_played_are_counted`, `a_wmf_reports_unread_pattern_brushes_and_raster_operations`).
|
||||
- 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`).
|
||||
|
||||
Reference in new issue
Block a user