SVG export: a drawn repeat mark in scaled type keeps its character's width

In horizontally scaled type, live SVG text stretched the character a
repeat mark is drawn as (Kurikaeshi Moji Shori) to the mark's advance with
textLength, so a reader narrowed or widened it whenever the two glyphs'
widths differ. Its textLength now takes the width the canvas draws that
character at; the text after it is still placed anew.
This commit is contained in:
dotCooCoo
2026-11-15 10:54:05 -08:00
parent 9548a669a0
commit 831e539ecc
2 changed files with 63 additions and 3 deletions
+33 -3
View File
@@ -439,7 +439,8 @@ impl Writer<'_> {
// reader would draw elsewhere, the next segment is placed.
place = seg.brk || (!fewer && !base.anchored && positioned);
if (st.h_scale - st.v_scale).abs() > 1e-9 || seg.scale != 1.0 {
attrs.push_str(&format!(" textLength=\"{}\" lengthAdjust=\"spacingAndGlyphs\"", self.num(seg.advance * seg.scale)));
let length = seg.advance * seg.scale + seg.repeat_widen;
attrs.push_str(&format!(" textLength=\"{}\" lengthAdjust=\"spacingAndGlyphs\"", self.num(length)));
}
if st.rotation != 0.0 {
attrs.push_str(&format!(" rotate=\"{}\"", self.num(-st.rotation)));
@@ -716,6 +717,9 @@ struct Segment {
/// Glyph Scaling of the segment's one glyph (Justification…), which a reader stretches it to
/// (`textLength`); 1 for other segments.
scale: f64,
/// How much wider than the repeat mark the canvas draws the character the mark is drawn as
/// (Kurikaeshi Moji Shori), which a reader stretches it to (`textLength`); 0 for other segments.
repeat_widen: f64,
/// A list marker's characters and tab, written in
/// [`vectorcraft_doc::text::ParaList::marker_style`] of the run's style.
marker: bool,
@@ -736,6 +740,30 @@ fn shown_repeat(lay: &vectorcraft_text::TextLayout, i: usize, g: &vectorcraft_te
lay.repeat_marks.get(at).map(|&(_, c)| c)
}
/// Kurikaeshi Moji Shori: how much wider than repeat mark `cluster` the canvas draws glyph `g`, the
/// character the mark is drawn as in the mark's place (text space), or 0 when the face or the
/// mark's glyph is not found.
fn repeat_widen(g: &vectorcraft_text::PositionedGlyph, cluster: &str) -> f64 {
let Some(face) = FontDb::global().face_by_id(g.font_id) else { return 0.0 };
// ゝ or ヽ with a combining voicing mark is drawn as one glyph, the voiced mark's.
let mark = match cluster {
"ゝ\u{3099}" => 'ゞ',
"ヽ\u{3099}" => 'ヾ',
_ => match cluster.chars().next() {
Some(c) => c,
None => return 0.0,
},
};
let mark_gid = face.glyph_for(mark);
if mark_gid == 0 {
return 0.0;
}
// Font units along the baseline → text space (size, Horizontal Scale and Glyph Scaling).
let [a, b, ..] = g.xf.as_coeffs();
let widen = (face.advance(g.gid) - face.advance(mark_gid)) * a.hypot(b);
if widen.is_finite() { widen } else { 0.0 }
}
/// The laid-out lines of `t` as segments: a new segment at every style change, after tabs and,
/// on justified lines, after each word space (unless `fewer`). The text comes from the clusters
/// the layout placed: soft hyphens draw nothing, a line broken by hyphenation ends in `-`, all
@@ -829,6 +857,7 @@ fn text_lines(t: &TextObject, lay: &vectorcraft_text::TextLayout, fewer: bool) -
Some(c) => c.to_string(),
None => source(line.glyph_start + k, g).chars().filter(|c| *c != '\u{ad}').map(|c| run.style.digits.map(c)).collect(),
};
let widen = if repeat.is_some() { repeat_widen(g, source(line.glyph_start + k, g)) } else { 0.0 };
let piece = if run.style.all_caps { src.to_uppercase() } else { src };
// The character a repeat mark is drawn as takes the mark's width on the canvas, which a
// reader does not know: the text after it is placed anew.
@@ -846,7 +875,8 @@ fn text_lines(t: &TextObject, lay: &vectorcraft_text::TextLayout, fewer: bool) -
let marker = lay.marker_of(line.glyph_start + k).is_some();
let tab_apart = piece == "\t" && (run.style.h_scale - run.style.v_scale).abs() > 1e-9;
match segs.last_mut() {
Some(last) if last.run == g.run && !last.brk && !fresh && !moved && last.marker == marker && !tab_apart => {
// The character a repeat mark is drawn as is a segment of its own, stretched to its width.
Some(last) if last.run == g.run && !last.brk && !fresh && !moved && last.marker == marker && !tab_apart && repeat.is_none() => {
last.text.push_str(&piece);
last.advance += advance;
last.brk = brk;
@@ -856,7 +886,7 @@ fn text_lines(t: &TextObject, lay: &vectorcraft_text::TextLayout, fewer: bool) -
last.brk |= moved || last.marker != marker;
end_segment(last, tail, t, lay, &mut unkerned);
}
segs.push(Segment { run: g.run, x, y: g.origin.y, text: piece, advance, brk, scale, marker });
segs.push(Segment { run: g.run, x, y: g.origin.y, text: piece, advance, brk, scale, repeat_widen: widen, marker });
}
}
tail = set.map(|_| (g.byte, advance));
+30
View File
@@ -942,6 +942,36 @@ fn a_repeat_mark_at_a_line_start_is_written_as_the_character_it_draws() {
}
}
/// Kurikaeshi Moji Shori in horizontally scaled type: the character a repeat mark is drawn as is
/// stretched (`textLength`) to the width the canvas draws that character at, not to the mark's
/// advance, so a reader does not distort it when the two characters' widths differ.
#[test]
fn a_drawn_repeat_mark_in_scaled_type_keeps_the_width_of_its_character() {
use vectorcraft_text::test_fonts::{RARE_REPEAT_MARK_FAMILY, rare_repeat_mark_font};
vectorcraft_text::FontDb::global().add_font(rare_repeat_mark_font().unwrap());
// The test font draws 龘 and 々 with glyphs of different widths.
let st = CharStyle { font_family: RARE_REPEAT_MARK_FAMILY.into(), h_scale: 50.0, ..style(12.0) };
let mut t = TextObject::point(Point::new(20.0, 40.0), "龘\u{2028}々", st);
t.para.kurikaeshi_moji_shori = true;
let mut d = Document::new(300.0, 200.0);
let n = Node::new(d.alloc_id(), NodeKind::Text(Box::new(t)));
let l = d.layers[0].id;
d.insert(Some(l), 0, n).unwrap();
let svg = export(&d, &ExportOptions::default());
// The textLength of each `<tspan>` that holds 龘 alone: the one on the first line and the one
// the mark is drawn as on the second.
let lengths: Vec<&str> = svg
.match_indices(">龘<")
.map(|(at, _)| {
let tag = &svg[svg[..at].rfind('<').unwrap_or(0)..at];
let from = tag.find("textLength=\"").unwrap_or_else(|| panic!("{tag}: {svg}")) + "textLength=\"".len();
&tag[from..from + tag[from..].find('"').unwrap_or(0)]
})
.collect();
assert_eq!(lengths.len(), 2, "{svg}");
assert_eq!(lengths[0], lengths[1], "{svg}");
}
/// Kurikaeshi Moji Shori: a `<textPath>` cannot draw a character in another character's width, so
/// type on a path whose threaded frame starts with a repeat mark drawn as the character it repeats
/// is outlined, with a warning; with the setting off it stays live text.