mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-12-05 13:32:45 -05:00
Type Tools: Make Numbers Sequence keeps adjacent glyph choices in one-pass runs
Pieces are joined only when edit::normalize would join them, so a second character with the same glyph alternate keeps it. The plain text is built only when the one-pass path is still possible.
This commit is contained in:
1 parent
74b3d5c557
commit
a0b0702a05
2 files changed
+6
-5
No files matched your search
@@ -309,10 +309,10 @@ pub(crate) fn replaced(text: &str, changes: &[Edit]) -> String {
|
||||
/// paragraphs, style runs and ruby that replacing each range by itself with
|
||||
/// [`edit::replace_range`] would give, in one pass over each.
|
||||
pub(crate) fn apply(t: &mut TextObject, changes: &[Edit]) {
|
||||
let text = t.plain_text();
|
||||
let newline = |(a, b, n): &Edit| n.contains('\n') || text.get(*a..*b).is_none_or(|s| s.contains('\n'));
|
||||
let annotated = !t.ruby.is_empty() || t.runs.iter().any(|r| r.inline.is_some());
|
||||
if changes.len() < 2 || annotated || changes.iter().any(newline) {
|
||||
if changes.len() < 2 || !t.ruby.is_empty() || t.runs.iter().any(|r| r.inline.is_some()) || {
|
||||
let text = t.plain_text();
|
||||
changes.iter().any(|(a, b, n)| n.contains('\n') || text.get(*a..*b).is_none_or(|s| s.contains('\n')))
|
||||
} {
|
||||
// One change (a whole text), one that adds or removes paragraphs, or text with ruby or
|
||||
// inline art, whose spans each replacement moves or drops in turn.
|
||||
for (a, b, n) in changes {
|
||||
@@ -371,7 +371,7 @@ fn replace_runs(runs: &mut Vec<TextRun>, changes: &[Edit]) {
|
||||
/// style is copied once for each change of style rather than once for each number.
|
||||
fn push(out: &mut Vec<TextRun>, text: &str, style: &CharStyle) {
|
||||
match out.last_mut() {
|
||||
Some(last) if last.inline.is_none() && last.style == *style => last.text.push_str(text),
|
||||
Some(last) if last.style == *style && last.inline.is_none() && style.glyph_alternate.is_none() => last.text.push_str(text),
|
||||
_ => out.push(TextRun { text: text.to_string(), style: style.clone(), inline: None }),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -378,6 +378,7 @@ fn numbers_made_in_one_pass_keep_the_styles_and_ruby_of_one_at_a_time() {
|
||||
(vec![("a12 b", 10.0, None), ("34 c5", 12.0, Some('3')), ("6 -7.8 9", 14.0, None)], vec![(1, 3), (0, 6), (7, 12), (12, 14), (16, 18)]),
|
||||
(vec![("1", 10.0, Some('1')), ("2", 11.0, Some('2')), (" x ", 12.0, None), ("333", 13.0, None)], vec![(0, 2), (2, 4), (5, 8)]),
|
||||
(vec![("no 1, 2 or 3", 10.0, None), ("", 11.0, None), ("44", 12.0, Some('4'))], vec![(3, 4), (4, 9), (13, 14)]),
|
||||
(vec![("a", 10.0, Some('a')), ("a", 10.0, Some('a')), (" 12 34", 10.0, None)], vec![(3, 5), (6, 8)]),
|
||||
];
|
||||
for (runs, spans) in cases {
|
||||
let mut t = base.clone();
|
||||
|
||||
Reference in new issue
Block a user