mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-25 08:11:41 +00:00
Metafile import: opaque backgrounds cover trailing spaces and NUL-only lines
On more than one line without advances, each line's background now runs across its glyphs' advances, trailing spaces included, as a single line's does; layout leaves the trailing spaces out of the width it aligns. A first line that holds only NULs fills its cells across their advances instead of drawing nothing.
This commit is contained in:
@@ -773,12 +773,10 @@ impl Mapper {
|
||||
/// a new piece after each cluster that tracking can't space by its advance.
|
||||
fn line(&mut self, line: &Line, chars: &[char], adv: Advances, down: f64, out: &mut TextOut) {
|
||||
let Advances { dx, reach, lead } = adv;
|
||||
if chars.is_empty() {
|
||||
return;
|
||||
}
|
||||
// The line's cells, one rectangle behind every piece, run as far each way as its
|
||||
// advances reach (an advance may be negative), from where the leading NULs' advances
|
||||
// start, which the alignment puts where it puts a line as long as its advances.
|
||||
// start, which the alignment puts where it puts a line as long as its advances. A line
|
||||
// of leading NULs alone fills the cells across their advances.
|
||||
let start = aligned(lead.by, dx.iter().sum(), line.align);
|
||||
let (mut lo, mut hi, mut pos) = (lead.reach.0 - lead.by, lead.reach.1 - lead.by, 0.0f64);
|
||||
for (d, r) in dx.iter().zip(reach) {
|
||||
@@ -790,7 +788,7 @@ impl Mapper {
|
||||
let blank = |cs: &[char]| cs.iter().all(|c| c.is_whitespace());
|
||||
// A piece's type draws when it shows: blanks only when underlined or struck through.
|
||||
let draws = |cs: &[char]| line.shows && (!blank(cs) || line.decorated);
|
||||
if !draws(chars) {
|
||||
if chars.is_empty() || !draws(chars) {
|
||||
return;
|
||||
}
|
||||
let db = vectorcraft_text::FontDb::global();
|
||||
@@ -957,26 +955,30 @@ fn cell_rect(x0: f64, x1: f64, y: f64, cell: (f64, f64)) -> BezPath {
|
||||
|
||||
/// The character cells of `laid` (the type `s` laid out on more than one line), `cell.0` above
|
||||
/// and `cell.1` below each baseline (points), as one rectangle a line: across the line as layout
|
||||
/// places it (its trailing blanks are left out of the width layout aligns), or for a line of
|
||||
/// blanks alone as wide as their advances, where `align` puts a line that long.
|
||||
/// places it and across its glyphs' advances, its trailing blanks included (layout leaves them
|
||||
/// out of the width it aligns), or for a line of blanks alone as wide as their advances, where
|
||||
/// `align` puts a line that long.
|
||||
fn cells(laid: &vectorcraft_text::TextLayout, s: &str, cell: (f64, f64), align: &HAlign) -> BezPath {
|
||||
// Each line's baseline, the sum of its glyphs' advances and whether they are all blanks.
|
||||
// Glyphs come line by line.
|
||||
let mut lines: Vec<(usize, f64, f64, bool)> = Vec::new();
|
||||
// Each line's baseline, the sum of its glyphs' advances, how far before and after the
|
||||
// reference point they reach, and whether they are all blanks. Glyphs come line by line.
|
||||
type Row = (usize, f64, f64, (f64, f64), bool);
|
||||
let mut lines: Vec<Row> = Vec::new();
|
||||
for g in &laid.glyphs {
|
||||
let blank = s.get(g.byte..).and_then(|r| r.chars().next()).is_none_or(char::is_whitespace);
|
||||
let (from, to) = (g.origin.x.min(g.origin.x + g.advance), g.origin.x.max(g.origin.x + g.advance));
|
||||
match lines.last_mut() {
|
||||
Some((line, _, width, all)) if *line == g.line => {
|
||||
Some((line, _, width, reach, all)) if *line == g.line => {
|
||||
*width += g.advance;
|
||||
*reach = (reach.0.min(from), reach.1.max(to));
|
||||
*all &= blank;
|
||||
}
|
||||
_ => lines.push((g.line, g.origin.y, g.advance, blank)),
|
||||
_ => lines.push((g.line, g.origin.y, g.advance, (from, to), blank)),
|
||||
}
|
||||
}
|
||||
let mut bp = BezPath::new();
|
||||
for (line, y, width, blank) in lines {
|
||||
for (line, y, width, reach, blank) in lines {
|
||||
let (x0, x1) = match laid.lines.get(line) {
|
||||
Some(l) if !blank => (l.x0, l.x1),
|
||||
Some(l) if !blank => (l.x0.min(reach.0), l.x1.max(reach.1)),
|
||||
_ => {
|
||||
let x0 = aligned(0.0, width, align);
|
||||
(x0, x0 + width)
|
||||
|
||||
@@ -1148,10 +1148,18 @@ fn opaque_backgrounds_reach_as_far_as_the_advances() {
|
||||
let b = layer[0].geometric_bounds().unwrap();
|
||||
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.
|
||||
// Layout aligns a line without its trailing spaces: right aligned at 40 mm, the space after
|
||||
// the "i" runs on past it. On more than one line, the cells also run across the trailing
|
||||
// spaces, as far as on one line.
|
||||
assert!(end > 41.0 * mm, "{end}");
|
||||
let b = aligned(2, 26, 4000, arial(), "Hi \nHi", &[])[0].geometric_bounds().unwrap();
|
||||
assert!((b.x1 - 40.0 * mm).abs() < 0.01, "{b:?}");
|
||||
assert!((b.x1 - end).abs() < 0.01, "{b:?} {end}");
|
||||
// Leading NULs on a line of their own fill their cells across their advances, before the
|
||||
// next line: from 10 to 30 mm.
|
||||
let layer = opaque(arial(), "\0\nA", &[2000, 0, 1000]);
|
||||
let first = layer[0].geometric_bounds().unwrap();
|
||||
assert!((first.x0 - 10.0 * mm).abs() < 0.01 && (first.x1 - 30.0 * mm).abs() < 0.01, "{first:?}");
|
||||
assert!(matches!(layer[1].kind, NodeKind::Path { .. }) && matches!(layer[2].kind, NodeKind::Text(_)), "{layer:?}");
|
||||
// NULs whose advances go forward and back again still fill as far as they reach.
|
||||
let layer = opaque(arial(), "\0\0", &[1000, -1000]);
|
||||
assert_eq!(layer.len(), 1);
|
||||
|
||||
Reference in New Issue
Block a user