mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-12-04 19:41:36 -05:00
.ai/EPS import: area type grows until the file's lines fit, not only by a descent (#2769)
Area type of one frame whose lines are each a little taller here than in the file (a paragraph of CJK type in a fallback font) lost its last line: the drift adds up over the paragraph, so the last line ends more than a descent past the frame's bottom, and fit_source_lines, which grows the frame by the lines' deepest descent, can't bring it back. vectorcraft_text::fit_lines grows the frame from its bottom by the least that lays out as many lines as the file sets in it (or all of the text, when it needs fewer), found by bisection. Story::fit_last_lines tries fit_source_lines first, as before, then fit_lines; it still only fits a story's one area frame, and the warning is unchanged.
This commit is contained in:
@@ -1251,11 +1251,17 @@ impl Story {
|
||||
|
||||
/// Area type of one frame that the file lays more lines out in than fit here: in the fallback
|
||||
/// font, or a last line whose descent the file lets pass the frame's bottom. The frame is made
|
||||
/// taller from its bottom (`vectorcraft_text::fit_source_lines`) so the file's lines show.
|
||||
/// taller from its bottom (`vectorcraft_text::fit_source_lines`) so the file's lines show. When
|
||||
/// a descent isn't enough (each line a little taller here, as in a paragraph of CJK type in a
|
||||
/// fallback font, #2769), it grows by the least that lays them out (`vectorcraft_text::fit_lines`).
|
||||
/// Whether it was.
|
||||
pub(super) fn fit_last_lines(&self, t: &mut TextObject) -> bool {
|
||||
matches!(self.frames.as_slice(), [FrameKind::Area(..)])
|
||||
&& vectorcraft_text::fit_source_lines(vectorcraft_text::FontDb::global(), t, self.lines.len())
|
||||
if !matches!(self.frames.as_slice(), [FrameKind::Area(..)]) {
|
||||
return false;
|
||||
}
|
||||
let db = vectorcraft_text::FontDb::global();
|
||||
let deeper = vectorcraft_text::fit_source_lines(db, t, self.lines.len());
|
||||
vectorcraft_text::fit_lines(db, t, self.lines.len()) || deeper
|
||||
}
|
||||
|
||||
/// Were tab stops past [`MAX_STORY_TABS`] left out of its paragraphs (so far: making its text
|
||||
@@ -2613,6 +2619,45 @@ mod tests {
|
||||
assert!(!s.fit_last_lines(&mut t));
|
||||
}
|
||||
|
||||
/// #2769: the file sets three lines in a frame that is more than a descent too short for them
|
||||
/// here (each line a little taller, as CJK type in a fallback font is): the frame grows from its
|
||||
/// bottom until they all show, and no further.
|
||||
#[test]
|
||||
fn a_frame_more_than_a_descent_too_short_for_the_file_s_lines_is_made_taller() {
|
||||
let text = "one\rtwo\rthree\r";
|
||||
let db = vectorcraft_text::FontDb::global();
|
||||
let area = |height: f64| {
|
||||
let mut t = TextObject::point(Point::ZERO, "one\ntwo\nthree", CharStyle { size: 13.0, ..CharStyle::default() });
|
||||
t.kind = TextKind::Area { frame: vectorcraft_geom::shapes::rectangle(Rect::new(10.0, 20.0, 210.0, 20.0 + height)) };
|
||||
t
|
||||
};
|
||||
let height = |t: &TextObject| if let TextKind::Area { frame } = &t.kind { frame.bounds().map(|b| (b.y0, b.height())) } else { None };
|
||||
let lines = |t: &TextObject| vectorcraft_text::layout(db, t).lines.len();
|
||||
let one_frame = |ys: &[f64]| {
|
||||
let mut s = story(text, ys, &Para::default());
|
||||
s.frames = vec![FrameKind::Area(vec![], AreaFrame::default())];
|
||||
s
|
||||
};
|
||||
let s = one_frame(&[0.0, 15.6, 31.2]);
|
||||
// The least frame height (to a quarter point) that holds all three lines here, and the
|
||||
// third line's descent: the frame is short by more than that.
|
||||
let full = (1..400).map(|i| i as f64 * 0.25).find(|h| lines(&area(*h)) == 3).unwrap();
|
||||
let descent = vectorcraft_text::layout(db, &area(400.0)).lines[2].descent;
|
||||
let short = full - descent - 1.0;
|
||||
let mut t = area(short);
|
||||
assert_eq!(lines(&t), 2, "the last line is cut off");
|
||||
assert!(s.fit_last_lines(&mut t));
|
||||
let (y0, h) = height(&t).unwrap();
|
||||
assert_eq!(lines(&t), 3, "every line shows");
|
||||
assert!(y0 == 20.0 && h > full - 0.25 && h < full + 0.5, "taller from its bottom, by what the last line needs: {h} (fits at {full})");
|
||||
// A frame the file lays out only two lines in keeps its third line overset, as the file has it.
|
||||
let mut t = area(short);
|
||||
assert!(!one_frame(&[0.0, 15.6]).fit_last_lines(&mut t) && height(&t).unwrap().1 == short);
|
||||
// A frame that holds every line stays as it is.
|
||||
let mut t = area(full + 5.0);
|
||||
assert!(!s.fit_last_lines(&mut t) && height(&t).unwrap().1 == full + 5.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn area_type_s_first_line_goes_where_the_file_lays_it_out() {
|
||||
// GUARDS: area type's first baseline was the font's ascent below the frame's top, where the
|
||||
|
||||
@@ -39,7 +39,7 @@ pub(crate) use bidi::{is_rtl, para_bidi, starts_bidi};
|
||||
use breaking::tab_advance;
|
||||
#[cfg(test)]
|
||||
use breaking::{break_line, candidates};
|
||||
pub use driver::{decorations, drawn, fit_first_line, fit_source_lines, layout, layout_with, scaled};
|
||||
pub use driver::{decorations, drawn, fit_first_line, fit_lines, fit_source_lines, layout, layout_with, scaled};
|
||||
#[cfg(test)]
|
||||
pub(crate) use driver::{fit_passes, layout_faces_per_call};
|
||||
#[cfg(test)]
|
||||
|
||||
@@ -94,6 +94,72 @@ pub fn fit_source_lines(db: &FontDb, t: &mut TextObject, lines: usize) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
/// Area type whose source lays out `lines` lines in its frame, where fewer of them fit here even
|
||||
/// after [`fit_source_lines`] (#2769). Each line here can be a little taller than the source's (a
|
||||
/// font shown in a fallback, CJK line heights), so a paragraph's lines push the last one more than
|
||||
/// a descent past the frame's bottom, and a line that doesn't fit isn't drawn: it went missing
|
||||
/// without a sign. For importers: the frame is made taller, from its bottom, by the least that lays
|
||||
/// the source's lines out (or all of the text, when it has fewer), plus 0.05 pt for the rounding of
|
||||
/// the layout. Horizontal type only. Whether it was.
|
||||
pub fn fit_lines(db: &FontDb, t: &mut TextObject, lines: usize) -> bool {
|
||||
if t.vertical || lines == 0 {
|
||||
return false;
|
||||
}
|
||||
let TextKind::Area { frame } = &t.kind else { return false };
|
||||
let frame = frame.clone();
|
||||
let Some(b) = frame.bounds().filter(|b| b.height() > 1e-6 && b.height().is_finite()) else { return false };
|
||||
// Lines the source has but this layout doesn't, with text left over for them.
|
||||
let fits = |t: &TextObject| {
|
||||
let l = layout(db, t);
|
||||
l.lines.len() >= lines || !l.overflow
|
||||
};
|
||||
if fits(t) {
|
||||
return false;
|
||||
}
|
||||
let at = |height: f64| {
|
||||
let by = height / b.height();
|
||||
frame.transformed(Affine::translate((0.0, b.y0)) * Affine::scale_non_uniform(1.0, by) * Affine::translate((0.0, -b.y0)))
|
||||
};
|
||||
let mut probe = t.clone();
|
||||
let mut fits_at = |height: f64| {
|
||||
probe.kind = TextKind::Area { frame: at(height) };
|
||||
fits(&probe)
|
||||
};
|
||||
// The line pitch a frame of `lines` lines would have, to start from.
|
||||
let shown = layout(db, t).lines.len().max(1);
|
||||
let pitch = (b.height() / shown as f64).max(t.runs.iter().map(|r| r.style.size).fold(0.0, f64::max)).max(1.0);
|
||||
let (mut lo, mut hi) = (b.height(), b.height() + pitch * (lines - shown.min(lines) + 1) as f64);
|
||||
// Doubling what is added, a few times at most: a frame narrower than a letter never fits.
|
||||
let mut tries = 0;
|
||||
while !fits_at(hi) {
|
||||
tries += 1;
|
||||
if tries > 6 || !hi.is_finite() {
|
||||
return false;
|
||||
}
|
||||
lo = hi;
|
||||
hi = b.height() + (hi - b.height()) * 2.0;
|
||||
}
|
||||
// The least height that fits, to a hundredth of a point.
|
||||
for _ in 0..32 {
|
||||
if hi - lo < 0.01 {
|
||||
break;
|
||||
}
|
||||
let mid = (lo + hi) / 2.0;
|
||||
if fits_at(mid) {
|
||||
hi = mid;
|
||||
} else {
|
||||
lo = mid;
|
||||
}
|
||||
}
|
||||
let taller = TextKind::Area { frame: at(hi + 0.05) };
|
||||
let short = std::mem::replace(&mut t.kind, taller);
|
||||
if !fits(t) {
|
||||
t.kind = short;
|
||||
return false;
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
/// The underline and strikethrough bars of `t` laid out as `layout` (#847): per run and line, a
|
||||
/// bar from the stretch's first glyph to its last one's advance, placed and sized by the font's
|
||||
/// own underline and strikeout metrics through the glyph's transform (so the size, baseline shift
|
||||
|
||||
@@ -53,7 +53,7 @@ pub use svg_glyph::{SvgGlyphReader, set_svg_glyph_reader};
|
||||
|
||||
pub use kept::{KeptLayoutsFrame, LayoutStamp, keep_layout, take_kept_layout};
|
||||
use kurbo::{Affine, BezPath, Point, Rect, Shape, Vec2};
|
||||
pub use layout::{decorations, drawn, fit_first_line, fit_source_lines, layout, layout_with, scaled};
|
||||
pub use layout::{decorations, drawn, fit_first_line, fit_lines, fit_source_lines, layout, layout_with, scaled};
|
||||
pub use shape::{aki_space, inseparable, line_start_space, spacing_scale};
|
||||
pub use vectorcraft_doc::TextObject;
|
||||
|
||||
|
||||
@@ -515,6 +515,39 @@ fn fit_source_lines_keeps_the_last_lines_the_source_sets() {
|
||||
assert!(!fit_source_lines(db(), &mut p, 9));
|
||||
}
|
||||
|
||||
/// #2769: a source whose lines are each a little shorter than they are here sets a last line more
|
||||
/// than a descent past the frame's bottom: the frame grows from its bottom by the least that lays
|
||||
/// the source's lines out, and only as many as the source has.
|
||||
#[test]
|
||||
fn fit_lines_grows_the_frame_until_the_source_s_lines_fit() {
|
||||
let text = "one\ntwo\nthree";
|
||||
let tall = layout(db(), &area(text, style(12.0), Rect::new(0.0, 0.0, 200.0, 400.0), Justify::Left));
|
||||
let need = tall.lines[2].baseline + tall.lines[2].descent;
|
||||
// More than a descent short: fit_source_lines leaves it, fit_lines doesn't.
|
||||
let frame = Rect::new(0.0, 0.0, 200.0, tall.lines[1].baseline + tall.lines[1].descent + 1.0);
|
||||
let mut t = area(text, style(12.0), frame, Justify::Left);
|
||||
assert!(!fit_source_lines(db(), &mut t, 3));
|
||||
// A source with no more lines than fit leaves the frame as it is.
|
||||
assert!(!fit_lines(db(), &mut t, 2));
|
||||
assert!(fit_lines(db(), &mut t, 3));
|
||||
let fitted = layout(db(), &t);
|
||||
assert!(!fitted.overflow && fitted.lines.len() == 3);
|
||||
let TextKind::Area { frame: grown } = &t.kind else { panic!("area type") };
|
||||
let grown = grown.bounds().unwrap();
|
||||
assert!(grown.y0 == frame.y0 && grown.y1 > need - 0.5 && grown.y1 < need + 0.5, "{grown:?}, needs {need}");
|
||||
// Fitted, nothing more to do.
|
||||
assert!(!fit_lines(db(), &mut t, 3));
|
||||
// A source that sets two of the three lines keeps the third overset.
|
||||
let mut t = area(text, style(12.0), frame, Justify::Left);
|
||||
assert!(!fit_lines(db(), &mut t, 2));
|
||||
// Point type and vertical type aren't fitted.
|
||||
let mut p = point(text, style(12.0));
|
||||
assert!(!fit_lines(db(), &mut p, 9));
|
||||
let mut v = area(text, style(12.0), frame, Justify::Left);
|
||||
v.vertical = true;
|
||||
assert!(!fit_lines(db(), &mut v, 9));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn indents_and_paragraph_spacing() {
|
||||
let frame = Rect::new(0.0, 0.0, 200.0, 400.0);
|
||||
|
||||
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user