mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-21 18:04:03 +01:00
Retype: a pair of lines' glyphs takes the tracking of the lines around it, and a wrap object below all area type makes no outline
A leading of exactly a quarter of the size joins lines.
This commit is contained in:
1 parent
0288d44e04
commit
1800ca6949
4 files changed
+93
-3
No files matched your search
@@ -669,6 +669,34 @@ fn area_type() -> TextObject {
|
||||
t
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_wrap_object_below_every_area_type_object_makes_no_outline() {
|
||||
use crate::{Appearance, Document, Node, NodeKind};
|
||||
let mut d = Document::new(300.0, 300.0);
|
||||
let l = d.layers[0].id;
|
||||
let wrap = |d: &mut Document, r: Rect| {
|
||||
let id = d.alloc_id();
|
||||
let mut n = Node::path(id, vectorcraft_geom::shapes::rectangle(r), Appearance::default_art());
|
||||
n.extras_mut().wrap = Some(TextWrap::default());
|
||||
n
|
||||
};
|
||||
// Bottom to top: a wrap object, the area type, then a wrap object.
|
||||
let below = wrap(&mut d, Rect::new(0.0, 0.0, 250.0, 250.0));
|
||||
d.insert(Some(l), 9, below).unwrap();
|
||||
let id = d.alloc_id();
|
||||
d.insert(Some(l), 9, Node::new(id, NodeKind::Text(Box::new(area_type())))).unwrap();
|
||||
let above = wrap(&mut d, Rect::new(60.0, 40.0, 80.0, 60.0));
|
||||
d.insert(Some(l), 9, above).unwrap();
|
||||
wrap::OUTLINES_MADE.with(|c| c.set(0));
|
||||
let mut sources = 0;
|
||||
wrap_sources(&d, |_, objects, shapes| {
|
||||
sources += 1;
|
||||
assert_eq!((objects.len(), shapes.len()), (1, 1));
|
||||
});
|
||||
assert_eq!(sources, 1);
|
||||
assert_eq!(wrap::OUTLINES_MADE.with(|c| c.get()), 1, "only the wrap object above the type is outlined");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn area_resizes_its_frame_and_keeps_its_type() {
|
||||
let mut t = area_type();
|
||||
|
||||
@@ -7,6 +7,12 @@ use vectorcraft_geom::{PathData, shapes};
|
||||
use super::{TextKind, TextObject, WrapShape};
|
||||
use crate::{Document, Node, NodeId, NodeKind};
|
||||
|
||||
#[cfg(test)]
|
||||
thread_local! {
|
||||
/// How many wrap outlines [`each_text`] has made on this thread (for tests).
|
||||
pub(super) static OUTLINES_MADE: std::cell::Cell<usize> = const { std::cell::Cell::new(0) };
|
||||
}
|
||||
|
||||
/// Outline of a wrap object in document coordinates: its paths, or its bounds for other art.
|
||||
fn outline(n: &Node, out: &mut PathData) {
|
||||
match &n.kind {
|
||||
@@ -133,9 +139,15 @@ fn each_text<'a>(layer: &'a Node, mut f: impl FnMut(&'a Node, &[(&'a Node, WrapS
|
||||
let mut items = vec![];
|
||||
collect(layer, &mut items);
|
||||
let mut above: Vec<(&Node, WrapShape)> = vec![];
|
||||
for it in items.iter().rev() {
|
||||
// A wrap object below every area text object of the layer wraps none of them: its outline is
|
||||
// not made.
|
||||
let first_text = items.iter().position(|it| matches!(it, Item::Text(_)));
|
||||
for (i, it) in items.iter().enumerate().rev() {
|
||||
match it {
|
||||
Item::Wrap(_) if first_text.is_none_or(|t| i < t) => {}
|
||||
Item::Wrap(n) => {
|
||||
#[cfg(test)]
|
||||
OUTLINES_MADE.with(|c| c.set(c.get() + 1));
|
||||
let mut pd = PathData::default();
|
||||
outline(n, &mut pd);
|
||||
if !pd.is_empty() {
|
||||
|
||||
@@ -821,7 +821,8 @@ impl Block {
|
||||
let n = self.lines.len() as f64;
|
||||
// The mean distance between the baselines so far, which this line's must continue.
|
||||
let leading = if self.lines.len() == 1 { down } else { self.leading };
|
||||
if !(leading > size * 0.25 && leading <= size * MAX_LEADING) || (down - n * leading).abs() > tol {
|
||||
// A quarter of the size is the least leading; the outlines' positions are rounded.
|
||||
if !(leading >= size * 0.25 * (1.0 - 1e-6) && leading <= size * MAX_LEADING) || (down - n * leading).abs() > tol {
|
||||
return Some(l);
|
||||
}
|
||||
let leading = down / n;
|
||||
@@ -935,6 +936,32 @@ impl Block {
|
||||
}
|
||||
}
|
||||
|
||||
/// A line of two untracked glyphs has one gap, too few to measure a tracking by
|
||||
/// ([`Line::tracked`]). It is set at the tracking of the nearest tracked line of the same font,
|
||||
/// size and turn, so the layout check ([`verified`]) tries it as that line is set: the check falls
|
||||
/// back to the line untracked, and then splits it, when the pair is not spaced so. A pair that
|
||||
/// untracked type already draws in place keeps its text, and negative tracking goes only to a pair
|
||||
/// that paints the same as one object ([`paints_as_one`]).
|
||||
fn share_tracking(lines: &mut [Line], system_layout: bool) {
|
||||
for i in 0..lines.len() {
|
||||
let Some(l) = lines.get(i).filter(|l| l.pieces.len() == 2 && l.tracking == 0.0) else { continue };
|
||||
if l.first_mismatch(&l.probe(system_layout)).is_none() {
|
||||
continue;
|
||||
}
|
||||
let like = |o: &Line| {
|
||||
o.tracking != 0.0
|
||||
&& o.parent == l.parent
|
||||
&& o.face.id() == l.face.id()
|
||||
&& (o.scale - l.scale).abs() <= l.scale * 1e-4
|
||||
&& ((o.angle - l.angle + PI).rem_euclid(TAU) - PI).abs() < 1e-5
|
||||
};
|
||||
let nearest = lines.iter().enumerate().filter(|(_, o)| like(o)).min_by_key(|(j, _)| j.abs_diff(i)).map(|(_, o)| o.tracking);
|
||||
if let Some(p) = nearest.filter(|t| *t > 0.0 || paints_as_one(l)).and_then(|t| l.respaced(0, 2, t)) {
|
||||
lines[i] = p;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// `lines`, in paint order, as the objects Retype makes: runs of lines that stack as the lines of
|
||||
/// one paragraph do become one object ([`Block`]). `follows(a, b)`: shape `b` comes right after
|
||||
/// shape `a`, with nothing between them in paint order. `budget`: see [`Block::made`].
|
||||
@@ -1147,7 +1174,8 @@ fn retype(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
// glyphs), so every line has what its measure can take.
|
||||
let mut budget =
|
||||
lines.iter().map(|l| l.text.chars().count().saturating_add(l.pieces.len().saturating_mul(393))).fold(256usize, usize::saturating_add);
|
||||
let lines = lines.into_iter().map(|l| l.tracked(system_layout, &mut budget)).collect();
|
||||
let mut lines: Vec<Line> = lines.into_iter().map(|l| l.tracked(system_layout, &mut budget)).collect();
|
||||
share_tracking(&mut lines, system_layout);
|
||||
let mut lines = verified(lines, system_layout, &mut unmatched);
|
||||
if pieces.len() > 1 {
|
||||
lines.retain(|l| {
|
||||
|
||||
@@ -597,6 +597,28 @@ fn several_lines_become_one_area_type_object() {
|
||||
assert_same_places(&before, &t);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn lines_a_quarter_of_the_size_apart_join_and_closer_ones_do_not() {
|
||||
// At 20 pt, 5 pt is the least leading that joins lines.
|
||||
// 4.9999999 pt is 5 pt to the rounding of the outlines' positions.
|
||||
for (leading, objects) in [(5.0, 1), (4.9999999, 1), (4.9, 2)] {
|
||||
let mut s = session();
|
||||
let text = "One\nTwo";
|
||||
let (_, r) = retyped(&mut s, json!({"x": 40, "y": 100, "text": text, "size": 20}), &[json!({"leading": leading})]);
|
||||
assert_eq!(r["texts"].as_array().unwrap().len(), objects, "{leading}: {r}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_two_glyph_line_takes_the_tracking_of_the_lines_around_it() {
|
||||
let mut s = session();
|
||||
let text = "MMM\nMM\nMMM";
|
||||
let (before, r) = retyped(&mut s, json!({"x": 40, "y": 100, "text": text, "size": 24}), &[json!({"tracking": 50})]);
|
||||
assert_eq!(r["texts"], json!([text]), "{r}");
|
||||
assert_eq!(r["fonts"][0]["tracking"], json!(50.0), "{r}");
|
||||
assert_same_places(&before, &text_of(&s, r["ids"][0].as_u64().unwrap()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn lines_keep_their_leading_and_alignment() {
|
||||
for justify in ["left", "center", "right"] {
|
||||
|
||||
Reference in new issue
Block a user