Scale Text Attributes: path type bakes only the uniform part of a scale

Type on a path sits at fractions of its path's length. Scaling the path
non-uniformly changes where a fraction lands (CodeRabbit's case: M0 0 L100 0
L100 100 under diag(2, 1) moves the start bracket from (200, 0) to (150, 0)).

Path type now bakes the vertical scale into the character sizes and scales
its path uniformly by it, which keeps every fraction where it was; the
horizontal stretch stays in the transform.
This commit is contained in:
Hamza
2026-11-17 14:17:49 +05:00
parent 586db4da06
commit 957cd66a82
2 changed files with 52 additions and 7 deletions
+19 -7
View File
@@ -27,7 +27,11 @@ impl TextObject {
/// Character attributes as presented by controls (stored runs stay local for layout
/// and rich-text clipboard operations). Explicit vertical character scale stays separate.
pub fn effective_char_style(&self, style: &CharStyle) -> Option<CharStyle> {
let scale = self.style_scale()?;
Self::scaled_char_style(style, self.style_scale()?)
}
/// `style` with `scale` taken into its attributes ([`Self::effective_char_style`]).
fn scaled_char_style(style: &CharStyle, scale: TextStyleScale) -> Option<CharStyle> {
let mut style = style.clone();
style.size *= scale.points;
style.leading = style.leading.map(|v| v * scale.points);
@@ -57,19 +61,27 @@ impl TextObject {
/// scale from `xf`, so panels show document-point sizes without a separate transform factor.
/// Returns `false` when there is nothing to bake (identity scale or an unrepresentable
/// transform).
///
/// Type on a path bakes only the uniform part of the scale (the vertical one) and keeps any
/// stretch in `xf`: its brackets and glyphs sit at fractions of the path's length, which a
/// non-uniform scale of the path would move, and a uniform one keeps.
pub fn scale_text_attributes(&mut self) -> bool {
let Some(scale) = self.style_scale() else { return false };
if (scale.points - 1.0).abs() <= 1e-12 && (scale.horizontal - 1.0).abs() <= 1e-12 {
let Some(mut scale) = self.style_scale() else { return false };
let [a, b, c, d, _, _] = self.xf.as_coeffs();
let (mut sx, sy) = (a.hypot(b), c.hypot(d));
if sx <= 1e-12 || sy <= 1e-12 {
return false;
}
let [a, b, c, d, _, _] = self.xf.as_coeffs();
let (sx, sy) = (a.hypot(b), c.hypot(d));
if sx <= 1e-12 || sy <= 1e-12 {
if matches!(self.kind, TextKind::OnPath { .. }) {
scale.horizontal = 1.0;
sx = sy;
}
if (scale.points - 1.0).abs() <= 1e-12 && (scale.horizontal - 1.0).abs() <= 1e-12 {
return false;
}
let mut baked = Vec::with_capacity(self.runs.len());
for run in &self.runs {
let Some(eff) = self.effective_char_style(&run.style) else { return false };
let Some(eff) = Self::scaled_char_style(&run.style, scale) else { return false };
baked.push(eff);
}
for (run, style) in self.runs.iter_mut().zip(baked) {
+33
View File
@@ -462,3 +462,36 @@ fn scale_text_attributes_keeps_a_path_types_path() {
near(text(&s, id).runs[0].style.size, 24.0);
same_place(&before, &placed(&s, id));
}
/// A non-uniform scale on type on a path: only the uniform part is baked and the stretch stays in
/// the transform, so the brackets, which sit at fractions of the path's length, stay where they
/// were. Baking the stretch into the path would move them: on this path, scaled 3 × 2, the length
/// grows from 200 to 500 and not in proportion, so the same fraction lands elsewhere.
#[test]
fn scale_text_attributes_keeps_path_type_on_a_stretched_path_where_it_was() {
let mut s = session();
let pid = s.execute("path.create", &json!({ "d": "M0 0 L100 0 L100 100" })).unwrap()["id"].as_u64().unwrap();
let id = NodeId(
s.execute("text.createInPath", &json!({"path": pid, "mode": "onPath", "text": "Corner", "size": 12, "at": [60, 0]})).unwrap()["id"]
.as_u64()
.unwrap(),
);
s.execute("select.set", &json!({"ids": [id.0]})).unwrap();
s.execute("object.scale", &json!({"sx": 300, "sy": 200, "origin": [0, 0]})).unwrap();
let before = placed(&s, id);
let brackets = |s: &Session| match text(s, id).kind {
vectorcraft_doc::TextKind::OnPath { start, end, .. } => (start, end),
k => panic!("not path type: {k:?}"),
};
let before_brackets = brackets(&s);
s.execute("type.scaleTextAttributes", &json!({})).unwrap();
near(text(&s, id).runs[0].style.size, 24.0);
near(text(&s, id).runs[0].style.h_scale, 100.0);
let left = text(&s, id).style_scale().unwrap();
near(left.points, 1.0);
near(left.horizontal, 1.5);
assert_eq!(brackets(&s), before_brackets);
same_place(&before, &placed(&s, id));
// Only the stretch is left: there is nothing more to bake.
assert!(s.execute("type.scaleTextAttributes", &json!({})).is_err());
}