Skip to content

Commit 7cc82e7

Browse files
jpc7k1claude
andcommitted
PDF: wrapped text breaks between words (on storytold#2282)
wrap_shaped broke lines between any two grapheme clusters, so Latin text wrapped mid-word in exported PDFs ("Regular mat / rix", "10/22 - 10/2 / 8") where Excel for Mac and GridCraft's base-14 path break at spaces. It now breaks at spaces, and only a word wider than the cell (or a run of text without spaces, as Thai is written) breaks between clusters, as before. Test: words wrap whole, an over-long word still breaks without losing text, paragraphs stay separate; the Thai tests still pass. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 758431f commit 7cc82e7

1 file changed

Lines changed: 46 additions & 6 deletions

File tree

‎crates/engine/src/cmd/print.rs‎

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1032,18 +1032,38 @@ fn wrap_shaped(
10321032
size: f32,
10331033
width: f32,
10341034
) -> std::result::Result<Vec<String>, String> {
1035+
// Word-wraps like Excel: lines break at spaces, and only a word (or a run of text without
1036+
// spaces, as Thai is written) wider than the cell breaks between grapheme clusters.
1037+
let fits = |s: &str| -> std::result::Result<bool, String> { Ok(shaped_width(catalog, family, bold, italic, s, size)? <= width) };
10351038
let mut out = Vec::new();
10361039
for paragraph in text.split('\n') {
10371040
let mut line = String::new();
1038-
for (cluster_index, (_, cluster)) in gridcraft_pdf::text_clusters(paragraph).enumerate() {
1039-
if cluster_index >= 8192 {
1040-
return Err("wrapped PDF text line exceeds the 8,192-grapheme layout limit".to_string());
1041+
for (word_index, word) in paragraph.split(' ').enumerate() {
1042+
if word_index >= 8192 {
1043+
return Err("wrapped PDF text line exceeds the 8,192-word layout limit".to_string());
10411044
}
1042-
let candidate = format!("{line}{cluster}");
1043-
if !line.is_empty() && shaped_width(catalog, family, bold, italic, &candidate, size)? > width {
1045+
let candidate = if line.is_empty() { word.to_string() } else { format!("{line} {word}") };
1046+
if line.is_empty() || fits(&candidate)? {
1047+
line = candidate;
1048+
} else {
10441049
out.push(std::mem::take(&mut line));
1050+
line = word.to_string();
1051+
}
1052+
// A word wider than the cell breaks between clusters.
1053+
if !fits(&line)? {
1054+
let mut piece = String::new();
1055+
for (cluster_index, (_, cluster)) in gridcraft_pdf::text_clusters(&std::mem::take(&mut line)).enumerate() {
1056+
if cluster_index >= 8192 {
1057+
return Err("wrapped PDF text line exceeds the 8,192-grapheme layout limit".to_string());
1058+
}
1059+
let candidate = format!("{piece}{cluster}");
1060+
if !piece.is_empty() && !fits(&candidate)? {
1061+
out.push(std::mem::take(&mut piece));
1062+
}
1063+
piece.push_str(cluster);
1064+
}
1065+
line = piece;
10451066
}
1046-
line.push_str(cluster);
10471067
if out.len() >= 2000 {
10481068
return Ok(out);
10491069
}
@@ -2236,6 +2256,26 @@ mod tests {
22362256
assert_eq!(advance_text_baseline(12.0, 25.0, advance, f32::NAN), (92.0, 25.0));
22372257
}
22382258

2259+
/// Wrapped text breaks between words, as Excel does ("Regular / matrix", not "Regular mat /
2260+
/// rix"); only a word wider than the cell breaks inside, between grapheme clusters.
2261+
#[test]
2262+
fn wrapped_text_breaks_between_words() {
2263+
let catalog = FontCatalog::empty();
2264+
let w = |t: &str| shaped_width(&catalog, "Calibri", false, false, t, 11.0).unwrap();
2265+
// Room for "Regular" and a bit, not for "Regular matrix".
2266+
let width = w("Regular m") + 1.0;
2267+
let lines = wrap_shaped("Regular matrix", &catalog, "Calibri", false, false, 11.0, width).unwrap();
2268+
assert_eq!(lines, vec!["Regular".to_string(), "matrix".to_string()]);
2269+
let lines = wrap_shaped("10/22 - 10/28", &catalog, "Calibri", false, false, 11.0, w("10/22 - 10/") + 1.0).unwrap();
2270+
assert_eq!(lines, vec!["10/22 -".to_string(), "10/28".to_string()]);
2271+
// A single word too wide for the cell still breaks, and no text is lost.
2272+
let long = "Supercalifragilistic";
2273+
let lines = wrap_shaped(long, &catalog, "Calibri", false, false, 11.0, w("Supercal") + 1.0).unwrap();
2274+
assert!(lines.len() > 1 && lines.concat() == long, "{lines:?}");
2275+
// Paragraphs stay separate.
2276+
assert_eq!(wrap_shaped("a\nb", &catalog, "Calibri", false, false, 11.0, 200.0).unwrap(), vec!["a".to_string(), "b".to_string()]);
2277+
}
2278+
22392279
#[test]
22402280
fn serialized_mixed_font_runs_share_rotated_baselines() {
22412281
let catalog = FontCatalog::system();

0 commit comments

Comments
 (0)