mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-19 03:08:00 +00:00
M7/G4: retype keeps paint order, groups, transparency and font version (#4934)
An unmatched shape between glyphs ends the line, so the type after it is still painted over it. Glyphs of different groups, paint or transparency never share a line, and the new type takes the glyphs' own opacity and blend mode. The type names the font version that matched. The glyph budget holds inside a face too.
This commit is contained in:
@@ -47,23 +47,28 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
/// A shape to match: a path or compound path, in document space.
|
||||
struct Piece {
|
||||
id: NodeId,
|
||||
/// The group holding it (`None` for a selected shape at the top): glyphs of different
|
||||
/// groups, which may composite differently, never share a line.
|
||||
parent: Option<NodeId>,
|
||||
path: BezPath,
|
||||
appearance: Appearance,
|
||||
/// The shape's own transparency, which the new type takes.
|
||||
opacity: f32,
|
||||
blend: vectorcraft_color::BlendMode,
|
||||
}
|
||||
|
||||
fn collect(n: &Node, depth: usize, out: &mut Vec<Piece>) {
|
||||
fn collect(n: &Node, parent: Option<NodeId>, depth: usize, out: &mut Vec<Piece>) {
|
||||
if out.len() >= MAX_PIECES || depth > MAX_DEPTH {
|
||||
return;
|
||||
}
|
||||
let piece = |path: BezPath| Piece { id: n.id, parent, path, appearance: (*n.appearance).clone(), opacity: n.opacity, blend: n.blend };
|
||||
match &n.kind {
|
||||
NodeKind::Group { children, clip: false } => {
|
||||
for c in children {
|
||||
collect(c, depth + 1, out);
|
||||
collect(c, Some(n.id), depth + 1, out);
|
||||
}
|
||||
}
|
||||
NodeKind::Path { path, clipping: false, guide: false, .. } => {
|
||||
out.push(Piece { id: n.id, path: path.to_bezpath(), appearance: (*n.appearance).clone() })
|
||||
}
|
||||
NodeKind::Path { path, clipping: false, guide: false, .. } => out.push(piece(path.to_bezpath())),
|
||||
NodeKind::Compound { children, .. } => {
|
||||
let mut path = BezPath::new();
|
||||
for c in children {
|
||||
@@ -71,7 +76,7 @@ fn collect(n: &Node, depth: usize, out: &mut Vec<Piece>) {
|
||||
path.extend(p.to_bezpath());
|
||||
}
|
||||
}
|
||||
out.push(Piece { id: n.id, path, appearance: (*n.appearance).clone() });
|
||||
out.push(piece(path));
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
@@ -111,6 +116,9 @@ struct Line {
|
||||
origin: Point,
|
||||
end: Point,
|
||||
appearance: Appearance,
|
||||
parent: Option<NodeId>,
|
||||
opacity: f32,
|
||||
blend: vectorcraft_color::BlendMode,
|
||||
}
|
||||
|
||||
impl Line {
|
||||
@@ -118,13 +126,18 @@ impl Line {
|
||||
Vec2::new(self.angle.cos(), self.angle.sin())
|
||||
}
|
||||
|
||||
/// Does glyph `m` continue this line (same font, size and turn, on its baseline, after its end)?
|
||||
fn continues(&self, m: &GlyphMatch) -> bool {
|
||||
/// Does glyph `m` of shape `pc` continue this line (same group, paint and transparency, same
|
||||
/// font, size and turn, on its baseline, after its end)?
|
||||
fn continues(&self, pc: &Piece, m: &GlyphMatch) -> bool {
|
||||
let size = self.scale * self.face.units_per_em();
|
||||
let d = m.origin - self.origin;
|
||||
let dir = self.dir();
|
||||
let across = d.cross(dir).abs();
|
||||
m.face.id() == self.face.id()
|
||||
pc.parent == self.parent
|
||||
&& pc.appearance == self.appearance
|
||||
&& pc.opacity == self.opacity
|
||||
&& pc.blend == self.blend
|
||||
&& m.face.id() == self.face.id()
|
||||
&& (m.scale - self.scale).abs() <= self.scale * 0.005
|
||||
&& (m.angle - self.angle).abs() < 1e-3
|
||||
&& across < size * 0.05
|
||||
@@ -150,17 +163,20 @@ fn space_advance(face: &vectorcraft_text::FontFace) -> f64 {
|
||||
face.mapped_chars().iter().find(|(c, _)| *c == ' ').map(|(_, g)| face.advance(*g)).unwrap_or(0.0)
|
||||
}
|
||||
|
||||
fn new_line(id: NodeId, m: &GlyphMatch, appearance: &Appearance) -> Line {
|
||||
fn new_line(pc: &Piece, m: &GlyphMatch) -> Line {
|
||||
let dir = Vec2::new(m.angle.cos(), m.angle.sin());
|
||||
Line {
|
||||
pieces: vec![id],
|
||||
pieces: vec![pc.id],
|
||||
text: m.ch.to_string(),
|
||||
face: m.face.clone(),
|
||||
scale: m.scale,
|
||||
angle: m.angle,
|
||||
origin: m.origin,
|
||||
end: m.origin + dir * (m.face.advance(m.gid) * m.scale),
|
||||
appearance: appearance.clone(),
|
||||
appearance: pc.appearance.clone(),
|
||||
parent: pc.parent,
|
||||
opacity: pc.opacity,
|
||||
blend: pc.blend,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -180,7 +196,7 @@ fn retype(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let mut pieces = vec![];
|
||||
for r in &roots {
|
||||
if let Some(n) = d.node(*r) {
|
||||
collect(n, 0, &mut pieces);
|
||||
collect(n, None, 0, &mut pieces);
|
||||
}
|
||||
}
|
||||
if pieces.is_empty() {
|
||||
@@ -200,17 +216,21 @@ fn retype(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let rank = |face: u32| votes.iter().find(|(f, _)| *f == face).map_or(0, |v| v.1);
|
||||
let mut lines: Vec<Line> = vec![];
|
||||
let mut unmatched = vec![];
|
||||
// An unmatched shape between glyphs ends the line: the type after it is painted after it.
|
||||
let mut open = false;
|
||||
for (pc, ms) in pieces.iter().zip(&found) {
|
||||
// Earlier matches win ties: they are the closer fits.
|
||||
let best = ms.iter().enumerate().max_by_key(|(i, m)| (rank(m.face.id()), std::cmp::Reverse(*i))).map(|(_, m)| m);
|
||||
let Some(m) = best else {
|
||||
unmatched.push(pc.id);
|
||||
open = false;
|
||||
continue;
|
||||
};
|
||||
match lines.last_mut() {
|
||||
Some(l) if l.continues(m) => l.push(pc.id, m),
|
||||
_ => lines.push(new_line(pc.id, m, &pc.appearance)),
|
||||
Some(l) if open && l.continues(pc, m) => l.push(pc.id, m),
|
||||
_ => lines.push(new_line(pc, m)),
|
||||
}
|
||||
open = true;
|
||||
}
|
||||
(lines, unmatched)
|
||||
};
|
||||
@@ -227,6 +247,8 @@ fn retype(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let style = CharStyle {
|
||||
font_family: l.face.family.clone(),
|
||||
font_style: l.face.style.clone(),
|
||||
// The version that matched, when two of the family and style are loaded.
|
||||
font_version: (!l.face.version.is_empty()).then(|| l.face.version.clone()),
|
||||
size: l.scale * l.face.units_per_em(),
|
||||
fill: l.appearance.fill_paint(),
|
||||
stroke: l.appearance.stroke_paint(),
|
||||
@@ -241,7 +263,11 @@ fn retype(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
t.system_layout = system_layout;
|
||||
super::typecmd::refresh_bounds(&mut t);
|
||||
let id = d.alloc_id();
|
||||
d.insert(par, idx, Node::new(id, NodeKind::Text(Box::new(t))))?;
|
||||
let mut node = Node::new(id, NodeKind::Text(Box::new(t)));
|
||||
// The glyphs' own transparency (the group's, kept, still applies over it).
|
||||
node.opacity = l.opacity;
|
||||
node.blend = l.blend;
|
||||
d.insert(par, idx, node)?;
|
||||
for pc in &l.pieces {
|
||||
if let Some((Some(parent), _, _)) = d.position(*pc) {
|
||||
emptied.push(parent);
|
||||
|
||||
@@ -118,3 +118,51 @@ fn craft_fonts_glyphs_retype_too() {
|
||||
assert_eq!(r["texts"], json!(["漢字です"]), "{r}");
|
||||
assert_eq!(r["fonts"][0]["family"], json!(f.family));
|
||||
}
|
||||
|
||||
fn children_of(s: &Session, g: u64) -> Vec<u64> {
|
||||
s.doc().unwrap().doc.node(NodeId(g)).unwrap().children().unwrap().iter().map(|c| c.id.0).collect()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_shape_between_glyphs_ends_the_line_so_paint_order_holds() {
|
||||
let mut s = session();
|
||||
let g = outlined(&mut s, json!({"x": 40, "y": 100, "text": "AB", "size": 40}));
|
||||
let rect = s.execute("shape.rectangle", &json!({"x": 30, "y": 60, "width": 80, "height": 50})).unwrap()["id"].as_u64().unwrap();
|
||||
// The rectangle goes between A and B, inside the outlines' group.
|
||||
s.edit("test", |d, _| {
|
||||
let n = d.node(NodeId(rect)).unwrap().clone();
|
||||
d.remove(NodeId(rect))?;
|
||||
d.insert(Some(NodeId(g)), 1, n)?;
|
||||
Ok(())
|
||||
})
|
||||
.unwrap();
|
||||
s.execute("select.set", &json!({"ids": [g]})).unwrap();
|
||||
let r = s.execute("type.retype", &json!({})).unwrap();
|
||||
assert_eq!(r["texts"], json!(["A", "B"]), "{r}");
|
||||
assert_eq!(r["unmatched"], json!([rect]));
|
||||
let ids: Vec<u64> = r["ids"].as_array().unwrap().iter().map(|v| v.as_u64().unwrap()).collect();
|
||||
assert_eq!(children_of(&s, g), vec![ids[0], rect, ids[1]], "A under the rectangle, B over it");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn glyphs_of_different_groups_and_transparency_stay_apart() {
|
||||
let mut s = session();
|
||||
let a = outlined(&mut s, json!({"x": 40, "y": 100, "text": "ab", "size": 20}));
|
||||
let b = outlined(&mut s, json!({"x": 120, "y": 100, "text": "cd", "size": 20}));
|
||||
let first_d = children_of(&s, b)[1];
|
||||
s.edit("test", |d, _| {
|
||||
d.node_mut(NodeId(b)).unwrap().opacity = 0.5;
|
||||
d.node_mut(NodeId(first_d)).unwrap().opacity = 0.25;
|
||||
Ok(())
|
||||
})
|
||||
.unwrap();
|
||||
s.execute("select.set", &json!({"ids": [a, b]})).unwrap();
|
||||
let r = s.execute("type.retype", &json!({})).unwrap();
|
||||
assert_eq!(r["texts"], json!(["ab", "c", "d"]), "same baseline, other group; d has its own opacity: {r}");
|
||||
let ids: Vec<u64> = r["ids"].as_array().unwrap().iter().map(|v| v.as_u64().unwrap()).collect();
|
||||
let d = &s.doc().unwrap().doc;
|
||||
assert!(d.node(NodeId(a)).is_none(), "a plain group gives way to its type");
|
||||
assert_eq!(d.node(NodeId(b)).unwrap().opacity, 0.5, "the half-transparent group stays");
|
||||
assert_eq!(children_of(&s, b), vec![ids[1], ids[2]]);
|
||||
assert_eq!(s.doc().unwrap().doc.node(NodeId(ids[2])).unwrap().opacity, 0.25, "the glyph's own opacity carries over");
|
||||
}
|
||||
|
||||
@@ -106,6 +106,10 @@ impl RetypeIndex {
|
||||
let mut gids: Vec<(u32, char)> = by_gid.into_iter().collect();
|
||||
gids.sort_unstable();
|
||||
for (gid, ch) in gids {
|
||||
// The budget holds inside a face too: no glyph is loaded past it.
|
||||
if entries.len() >= MAX_GLYPHS {
|
||||
break;
|
||||
}
|
||||
let outline = db.outline(face, gid);
|
||||
if let Some(sig) = signature(&outline) {
|
||||
entries.push(Entry { face: fi, gid, ch, sig });
|
||||
|
||||
Reference in New Issue
Block a user