Type: outlined strokes on unevenly scaled type keep their thickness in each direction

Create Outlines, Object > Expand and geometry effects scaled a character
stroke on unevenly scaled, sheared or flattened type by the transform's
mean scale, so a stroke scaled 300% across came out about 1.7 times as
thick everywhere, while the canvas strokes the glyphs in text space and
then transforms them.

Under such a transform each run's stroke now becomes a path filled with
the stroke's paint: the area the stroke paints around the glyphs in text
space (dashes, caps, joins and inside or outside alignment), transformed
with them. Moved, rotated, reflected and uniformly scaled type keeps a
live stroke with its weight and dashes scaled. object.expand.info reports
a gradient on such a filled stroke.
This commit is contained in:
dotCooCoo
2026-11-14 18:49:02 -08:00
parent 6016d6200f
commit dfde48c181
9 changed files with 299 additions and 24 deletions
+8 -5
View File
@@ -265,18 +265,21 @@ fn gradient_expands(d: &Document, l: &Node) -> bool {
/// Whether type `t` has a gradient fill that Fill expands once Object has outlined it: one of the
/// paths `type.createOutlines` makes of its glyphs, ruby glyphs and underline and strikethrough
/// bars, each painted as its run with its unplaced gradients fitted to the layout's bounds, has one
/// bars, each painted as its run with its unplaced gradients fitted to the layout's bounds, or the
/// path filled with its run's stroke under a transform that is not a similarity, has one
/// ([`gradient_expands`] on that path). Inline graphics and color glyphs become other art. Leaves
/// the document as it is and stops at the first such path.
fn text_gradient_expands(d: &Document, t: &TextObject) -> bool {
if !t.runs.iter().any(|r| matches!(r.style.fill, Paint::Gradient(_))) {
let gradient =
|st: &vectorcraft_doc::CharStyle| matches!(st.fill, Paint::Gradient(_)) || (st.has_stroke() && matches!(st.stroke, Paint::Gradient(_)));
if !t.runs.iter().any(|r| gradient(&r.style)) {
return false;
}
let lay = vectorcraft_text::layout(vectorcraft_text::FontDb::global(), t);
let first = t.first_style();
let expands = |run: usize, outline: &BezPath| {
let style = t.runs.get(run).map_or(&first, |r| &r.style);
if !matches!(style.fill, Paint::Gradient(_)) {
if !gradient(style) {
return false;
}
let path = PathData::from_bezpath(outline);
@@ -284,8 +287,8 @@ fn text_gradient_expands(d: &Document, t: &TextObject) -> bool {
return false;
}
let mut piece = Node::path(NodeId(u64::MAX), path, Appearance::default());
vectorcraft_render::effects::paint_type_piece(&mut piece, style, lay.bounds, t.xf);
gradient_expands(d, &piece)
let stroke = vectorcraft_render::effects::paint_type_piece(&mut piece, style, lay.bounds, t.xf);
gradient_expands(d, &piece) || stroke.is_some_and(|s| gradient_expands(d, &s))
};
let inline = |gi: usize| lay.inlines.iter().find(|i| i.glyph == gi).is_some_and(|i| t.runs.get(i.run).is_some_and(|r| r.inline.is_some()));
lay.glyphs.iter().enumerate().any(|(gi, g)| !inline(gi) && g.image.is_none() && g.colour.is_none() && expands(g.run, &g.outline))
+18 -14
View File
@@ -41,7 +41,7 @@ pub fn specs() -> Vec<CommandSpec> {
"Create Outlines",
["Type"],
Some("Cmd+Shift+O"),
"{} convert the selected text to groups of compound paths (one per glyph); the type object's own fills and strokes (not its characters') go on paths of its whole outline, below or above the glyphs as its Characters row places them; a frame of threaded text leaves its thread with the text it shows; a dimension whose label is outlined becomes a plain group of its art → {ids}",
"{} convert the selected text to groups of compound paths (one per glyph); on type scaled unevenly, sheared or flattened, each glyph's stroke becomes a filled path over it, shaped as the canvas strokes it; the type object's own fills and strokes (not its characters') go on paths of its whole outline, below or above the glyphs as its Characters row places them; a frame of threaded text leaves its thread with the text it shows; a dimension whose label is outlined becomes a plain group of its art → {ids}",
has_selection,
create_outlines
),
@@ -619,16 +619,25 @@ fn scale_text_attributes(s: &mut Session, p: &Value) -> Result<Value> {
/// A path (or compound path) of `outline`, a piece of type `t`'s outline in text space, painted as
/// run `run` and placed in the document as the canvas paints it ([`paint_type_piece`]: unplaced
/// gradients fitted to `bounds`, the layout's bounds, stroke weights scaled by the type's
/// transform); `None` when the piece has no path.
fn type_piece(d: &mut Document, t: &TextObject, bounds: kurbo::Rect, run: usize, outline: &kurbo::BezPath) -> Option<Node> {
/// transform), followed by a path (or compound path) filled with the run's stroke when the type's
/// transform is not a similarity; empty when the piece has no path.
fn type_piece(d: &mut Document, t: &TextObject, bounds: kurbo::Rect, run: usize, outline: &kurbo::BezPath) -> Vec<Node> {
let path = PathData::from_bezpath(outline);
if path.is_empty() {
return None;
return vec![];
}
let st = t.runs.get(run).map(|r| r.style.clone()).unwrap_or_else(|| t.first_style());
let mut node = shape_node(d, path, None);
vectorcraft_render::effects::paint_type_piece(&mut node, &st, bounds, t.xf);
Some(node)
let stroke = vectorcraft_render::effects::paint_type_piece(&mut node, &st, bounds, t.xf);
let mut out = vec![node];
if let Some(s) = stroke
&& let Some(path) = s.path_data()
{
let mut filled = shape_node(d, path.clone(), None);
filled.appearance = s.appearance.clone();
out.push(filled);
}
out
}
fn create_outlines(s: &mut Session, _: &Value) -> Result<Value> {
@@ -675,9 +684,7 @@ fn create_outlines(s: &mut Session, _: &Value) -> Result<Value> {
children.push(Arc::new(d.reid(&art)));
break 'glyph;
}
if let Some(node) = type_piece(d, t, lay.bounds, g.run, &g.outline) {
children.push(Arc::new(node));
}
children.extend(type_piece(d, t, lay.bounds, g.run, &g.outline).into_iter().map(Arc::new));
}
run_end.insert(g.run, children.len());
}
@@ -686,10 +693,9 @@ fn create_outlines(s: &mut Session, _: &Value) -> Result<Value> {
// nothing there either).
let mut ruby: Vec<(usize, Arc<Node>)> = vec![];
for g in &lay.ruby {
let Some(node) = type_piece(d, t, lay.bounds, g.run, &g.outline) else { continue };
// After the glyphs of its run and of the runs before it.
let at = run_end.range(..=g.run).map(|(_, end)| *end).max().unwrap_or(0);
ruby.push((at, Arc::new(node)));
ruby.extend(type_piece(d, t, lay.bounds, g.run, &g.outline).into_iter().map(|node| (at, Arc::new(node))));
}
ruby.sort_by_key(|(at, _)| *at);
let mut ruby = ruby.into_iter().peekable();
@@ -711,9 +717,7 @@ fn create_outlines(s: &mut Session, _: &Value) -> Result<Value> {
// Underline and strikethrough bars become paths of their run's paint too (#847).
for (run, bar) in vectorcraft_text::decorations(&lay, vectorcraft_text::FontDb::global(), t) {
all.extend(bar.iter());
if let Some(node) = type_piece(d, t, lay.bounds, run, &bar) {
children.push(Arc::new(node));
}
children.extend(type_piece(d, t, lay.bounds, run, &bar).into_iter().map(Arc::new));
}
// The object's own fills and strokes (not its characters') paint the whole outline:
// those below the Characters row under the characters, the others over them, with
+71
View File
@@ -188,6 +188,77 @@ fn align_stroke_reaches_the_characters_stroke() {
assert!(aligns.contains(&StrokeAlign::Inside) && aligns.contains(&StrokeAlign::Outside), "{aligns:?}");
}
/// The bounds of type `t`'s glyph outlines in text space.
fn glyph_bounds(t: &TextObject) -> kurbo::Rect {
let lay = vectorcraft_text::layout(vectorcraft_text::FontDb::global(), t);
let mut all = kurbo::BezPath::new();
for g in &lay.glyphs {
all.extend(g.outline.iter());
}
kurbo::Shape::bounding_box(&all)
}
/// Create Outlines on type scaled 300% across and 100% down: the canvas strokes the characters in
/// text space and then scales them, so the outlined stroke is filled art three times as thick
/// across upright stems as across level bars, not a live stroke of one mean weight.
#[test]
fn create_outlines_keeps_the_character_strokes_thickness_under_an_uneven_scale() {
let mut s = session();
let id = stroked_text(&mut s);
run(&mut s, "stroke.set", json!({"weight": 4, "join": "round", "cap": "round"}));
run(&mut s, "object.scale", json!({"ids": [id.0], "sx": 300, "sy": 100}));
let t = text(&s, id);
let [a, b, c, d, ..] = t.xf.as_coeffs();
assert!((a - 3.0).abs() < 1e-9 && b.abs() < 1e-9 && c.abs() < 1e-9 && (d - 1.0).abs() < 1e-9, "{:?}", t.xf);
let w = t.runs[0].style.stroke_width;
let gb = glyph_bounds(&t);
run(&mut s, "select.set", json!({ "ids": [id.0] }));
let g = run(&mut s, "type.createOutlines", json!({}))["ids"][0].as_u64().unwrap();
let doc = &s.doc().unwrap().doc;
let group = doc.node(NodeId(g)).unwrap();
let mut strokes: Option<kurbo::Rect> = None;
for c in group.children().unwrap() {
assert!(c.appearance.stroke().is_none(), "no live stroke of one weight is left");
if c.appearance.fill_paint() == Paint::solid(Color::rgb8(255, 0, 0)) {
let cb = c.geometric_bounds().unwrap();
strokes = Some(strokes.map_or(cb, |s| s.union(cb)));
}
}
// Round joins and caps reach half the weight past the glyphs in text space, then scale.
let want = t.xf.transform_rect_bbox(gb.inflate(w / 2.0, w / 2.0));
let got = strokes.expect("filled character strokes");
assert!((got.width() - want.width()).abs() < 0.1 && (got.height() - want.height()).abs() < 0.1, "{got:?} for {want:?}");
assert!((got.height() - (gb.height() + w)).abs() < 0.1, "level bars keep the weight: {got:?}");
}
/// Under a uniform scale and a rotation the outlined characters keep a live stroke, its weight
/// and dashes scaled with them.
#[test]
fn create_outlines_keeps_a_live_character_stroke_under_a_uniform_scale_and_rotation() {
let mut s = session();
let id = stroked_text(&mut s);
run(&mut s, "stroke.set", json!({"weight": 4, "dash": [6, 2]}));
run(&mut s, "object.scale", json!({"ids": [id.0], "sx": 200, "sy": 200}));
run(&mut s, "object.rotate", json!({"angle": 30}));
let t = text(&s, id);
let k = t.xf.determinant().abs().sqrt();
assert!((k - 2.0).abs() < 1e-9, "{:?}", t.xf);
let st = t.runs[0].style.clone();
let dash = st.stroke_dash.as_ref().unwrap().pattern.clone();
let glyphs = vectorcraft_text::layout(vectorcraft_text::FontDb::global(), &t).glyphs.len();
run(&mut s, "select.set", json!({ "ids": [id.0] }));
let g = run(&mut s, "type.createOutlines", json!({}))["ids"][0].as_u64().unwrap();
let doc = &s.doc().unwrap().doc;
let pieces = doc.node(NodeId(g)).unwrap().children().unwrap().to_vec();
assert_eq!(pieces.len(), glyphs, "one path for each glyph, no filled strokes");
for p in &pieces {
let ps = p.appearance.stroke().expect("a live stroke");
assert!((ps.width - st.stroke_width * k).abs() < 1e-6, "{} for {}", ps.width, st.stroke_width * k);
let got = &ps.dash.as_ref().unwrap().pattern;
assert!(got.iter().zip(&dash).all(|(g, d)| (g - d * k).abs() < 1e-6), "{got:?} for {dash:?}");
}
}
/// Copying type's appearance onto a path keeps its characters' stroke alignment (the Eyedropper's
/// own stroke attributes have none, on paths as on type).
#[test]
+23
View File
@@ -717,6 +717,29 @@ fn type_reports_a_gradient_when_any_glyph_expands() {
assert_eq!(gradients_and_type(&s), (1, 0), "the large glyph keeps its gradient");
}
/// Type scaled unevenly outlines its characters' stroke as a filled path, so a gradient on that
/// stroke becomes a gradient fill: the query reports it, and Expand turns it into art.
#[test]
fn type_reports_a_stroke_gradient_its_uneven_scale_turns_into_a_fill() {
let mut s = session();
let t = run(&mut s, "text.create", json!({"x": 50, "y": 100, "text": "Hi", "size": 48}))["id"].as_u64().unwrap();
run(&mut s, "select.set", json!({"ids": [t]}));
run(
&mut s,
"paint.setStroke",
json!({"gradient": {"kind": "linear", "spread": "repeat", "repeats": 100, "start": [50, 100], "end": [70, 100],
"stops": [{"offset": 0, "color": "#ffff00"}, {"offset": 1, "color": "#004080"}]}}),
);
run(&mut s, "stroke.set", json!({"weight": 4}));
let info = run(&mut s, "object.expand.info", json!({}));
assert_eq!(info["gradient"], json!(false), "a live stroke's gradient is not a fill");
run(&mut s, "object.scale", json!({"ids": [t], "sx": 300, "sy": 100}));
let info = run(&mut s, "object.expand.info", json!({}));
assert_eq!((info["gradient"].clone(), info["fill"].clone()), (json!(true), json!(true)));
run(&mut s, "object.expand", json!({}));
assert_eq!(gradients_and_type(&s), (0, 0), "the filled strokes' gradients became art");
}
/// The same rule for type in the art of a symbol instance that Object expands. The symbol's art
/// is the type scaled down (its "i" about 2.5 pt wide), so its glyphs need more runs than are
/// written with 0.002 pt runs (over a 0.2 pt vector).