mirror of
https://github.com/storytold/designcraft.git
synced 2026-10-14 19:27:07 -04:00
Compose: don't panic when a hyphenated paragraph starts with a no-break letter (#115)
This commit is contained in:
1 parent
aeabc82ca6
commit
07b95eb7b3
2 files changed
+17
No files matched your search
@@ -2080,6 +2080,11 @@ fn hyphenation_points(text: &str, glyphs: &[Glyph], pp: &ParaProps, exceptions:
|
||||
while j < glyphs.len() && (glyphs[j].is_letter() || glyphs[j].len == 0 || glyphs[j].ch == shape::SOFT_HYPHEN) && !glyphs[j].no_break {
|
||||
j += 1;
|
||||
}
|
||||
// A no-break letter can't start a hyphenatable word.
|
||||
if j == i {
|
||||
i += 1;
|
||||
continue;
|
||||
}
|
||||
// A discretionary hyphen in a word (or right before it) replaces automatic hyphenation.
|
||||
let discretionary = glyphs[i..j].iter().any(|g| g.ch == shape::SOFT_HYPHEN) || (i > 0 && glyphs[i - 1].ch == shape::SOFT_HYPHEN);
|
||||
let (a, b) = (glyphs[i].byte, glyphs[j - 1].byte + glyphs[j - 1].len);
|
||||
|
||||
@@ -313,6 +313,18 @@ fn discretionary_hyphen_replaces_automatic_points() {
|
||||
assert!(lines.iter().filter(|l| l.hyphenated).count() <= 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hyphenation_skips_a_no_break_word_at_the_start() {
|
||||
// Used to index before the first glyph and panic.
|
||||
let text = "Extraordinarily long words wrap in a narrow column";
|
||||
let (mut d, sid, _) = doc_with(text, Rect::new(0.0, 0.0, 60.0, 1000.0), ParaAttrs::default());
|
||||
d.story_mut(sid).unwrap().format_chars(0..15, |f| f.over.no_break = Some(true));
|
||||
let cs = compose_story(&d, sid, &ComposeOptions::default());
|
||||
// The no-break word stays whole on the first line.
|
||||
let first = all_lines(&cs)[0];
|
||||
assert!(!first.hyphenated && first.range.end >= 15, "{:?}", &text[first.range.clone()]);
|
||||
}
|
||||
|
||||
/// Story of `n` one-line filler paragraphs followed by `extra` paragraphs, in a 2-column frame.
|
||||
fn keep_doc(fillers: usize, extra: &[&str], height: f64) -> (Document, StoryId, Vec<usize>) {
|
||||
let mut parts: Vec<String> = (0..fillers).map(|k| format!("Filler {k}")).collect();
|
||||
|
||||
Reference in new issue
Block a user