Skip to content

PdfCraft: An open-source PDF reader and editor written by scratch in pure Rust

Merge PR #4217: M7: edited text keeps every space typed (#4199, #3797)

aiongg5cb74eeb6977
diff --cc crates/edit/src/tests.rs
index 5308ea7c7,918a843da..192acf8a7
--- a/crates/edit/src/tests.rs
+++ b/crates/edit/src/tests.rs
@@@ -6639,237 -6572,183 +6639,418 @@@ fn a_flatten_refused_on_a_later_page_ch
assert_eq!((*doc.get(first)).clone(), before, "the first page keeps its comment and content");
}
+/// How each glyph-showing operator on page 0 is painted, read the way a renderer reads the page:
+/// its text, `Tr`, stroke colour operators, line width (from `w` or an ExtGState's `/LW`) and
+/// the ExtGStates selected before it, following `q`/`Q`.
+#[derive(Clone, Debug, PartialEq)]
+struct Painted {
+ text: String,
+ render_mode: i64,
+ stroke: Vec<String>,
+ line_width: f64,
+ gs: Vec<String>,
+}
+
+fn painted_shows(doc: &Document) -> Vec<Painted> {
+ let (_, ops) = joined_ops(doc);
+ let p = &pdfcraft_model::pages(doc)[0];
+ let res = doc.resolve(p.dict.get(b"Resources").unwrap());
+ let ext = res.as_dict().unwrap().get(b"ExtGState").map(|g| doc.resolve(g).as_dict().cloned().unwrap()).unwrap_or_default();
+ let mut state = Painted { text: String::new(), render_mode: 0, stroke: Vec::new(), line_width: 1.0, gs: Vec::new() };
+ let mut stack = Vec::new();
+ let mut out = Vec::new();
+ let show = |op: &pdfcraft_content::Op| String::from_utf8_lossy(&pdfcraft_content::serialize_ops(std::slice::from_ref(op))).trim().to_string();
+ for op in ops {
+ match op.op.as_slice() {
+ b"q" => stack.push(state.clone()),
+ b"Q" => state = stack.pop().unwrap(),
+ b"Tr" => state.render_mode = op.operands[0].as_int().unwrap(),
+ b"G" | b"RG" | b"K" | b"CS" => state.stroke = vec![show(&op)],
+ b"SC" | b"SCN" => state.stroke.push(show(&op)),
+ b"w" => state.line_width = op.num(0).unwrap(),
+ b"gs" => {
+ let name = op.name(0).unwrap();
+ state.gs.push(String::from_utf8_lossy(name).into_owned());
+ let lw = ext.get(name).and_then(|g| doc.resolve(g).as_dict().and_then(|g| g.get(b"LW")).and_then(|w| w.as_f64()));
+ state.line_width = lw.unwrap_or(state.line_width);
+ }
+ b"Tj" => {
+ let Some(Object::String(s)) = op.operands.last() else { continue };
+ if !s.bytes.is_empty() {
+ out.push(Painted { text: String::from_utf8_lossy(&s.bytes).into_owned(), ..state.clone() });
+ }
+ }
+ _ => {}
+ }
+ }
+ out
+}
+
+/// A page with Helvetica as /F1, /GS1, an ExtGState that paints at 20 % opacity, /GS2, one that
+/// sets the line width to 4, /GS3, another at 20 % opacity, and /GS4, at 20 % opacity and dashed.
+fn faint_page(content: &str) -> Document {
+ build(&[
+ "<< /Type /Catalog /Pages 2 0 R >>",
+ "<< /Type /Pages /Kids [3 0 R] /Count 1 >>",
+ "<< /Type /Page /Parent 2 0 R /MediaBox [0 0 600 800] /Contents 4 0 R /Resources << /Font << /F1 5 0 R >> /ExtGState << /GS1 6 0 R /GS2 7 0 R /GS3 8 0 R /GS4 9 0 R >> >> >>",
+ &format!("<< /Length {} >>\nstream\n{content}\nendstream", content.len()),
+ "<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica /Encoding /WinAnsiEncoding >>",
+ "<< /Type /ExtGState /ca 0.2 >>",
+ "<< /Type /ExtGState /LW 4 >>",
+ "<< /Type /ExtGState /ca 0.2 >>",
+ "<< /Type /ExtGState /ca 0.2 /D [[6 3] 0] >>",
+ ])
+}
+
+/// Rewriting a paragraph drew it before its text object, restating only the font, spacing and
+/// fill: a `1 Tr 1 0 0 RG 2 w` set inside `BT` was lost, and red outline text came back filled
+/// black, even with the text unchanged (#3508). Set before `BT` (the control), it already worked.
+#[test]
+fn a_rewritten_paragraph_keeps_the_rendering_mode_stroke_and_width_set_inside_its_text_object() {
+ let outline = |text: &str| Painted { text: text.into(), render_mode: 1, stroke: vec!["1 0 0 RG".into()], line_width: 2.0, gs: Vec::new() };
+ for content in ["BT /F1 72 Tf 1 Tr 1 0 0 RG 2 w 40 400 Td (OUTLINE) Tj ET\n", "1 Tr 1 0 0 RG 2 w BT /F1 72 Tf 40 400 Td (OUTLINE) Tj ET\n"] {
+ assert_eq!(painted_shows(&text_page(content)), [outline("OUTLINE")], "the fixture: {content}");
+ for new in ["OUTLINE", "OUTLINED"] {
+ let mut doc = text_page(content);
+ text::rewrite_block(&mut doc, 0, 0, Some(new), &BlockStyle::default()).unwrap();
+ let doc = reopen(&doc);
+ assert_eq!(painted_shows(&doc), [outline(new)], "{content} -> {new}: {}", streams(&doc, 0).join("\n"));
+ }
+ }
+}
+
+/// The same for transparency: `/GS1 gs` (`/ca 0.2`) inside `BT` was not in force for the
+/// rewritten paragraph, so 20 % blue text came back opaque (#3509).
+#[test]
+fn a_rewritten_paragraph_keeps_the_extgstate_selected_inside_its_text_object() {
+ for content in ["BT /GS1 gs /F1 72 Tf 0 0 1 rg 40 400 Td (FAINT) Tj ET\n", "/GS1 gs BT /F1 72 Tf 0 0 1 rg 40 400 Td (FAINT) Tj ET\n"] {
+ for new in ["FAINT", "FAINTER"] {
+ let mut doc = faint_page(content);
+ text::rewrite_block(&mut doc, 0, 0, Some(new), &BlockStyle::default()).unwrap();
+ let doc = reopen(&doc);
+ let shows = painted_shows(&doc);
+ assert_eq!(shows.len(), 1, "{content} -> {new}: {shows:?}");
+ assert_eq!(
+ (shows[0].text.as_str(), shows[0].gs.as_slice()),
+ (new, ["GS1".to_string()].as_slice()),
+ "{content} -> {new}: {}",
+ streams(&doc, 0).join("\n")
+ );
+ }
+ }
+}
+
+/// A paragraph of differently coloured runs goes through the styled rewrite, which draws each
+/// run in its own text object: each keeps the state its source set inside `BT`.
+#[test]
+fn a_styled_rewrite_keeps_the_paint_state_set_inside_the_text_object() {
+ let content = "BT /GS1 gs 1 Tr 1 0 0 RG 2 w /F1 72 Tf 40 400 Td (FAI) Tj 0 0 1 rg (NT) Tj ET\n";
+ let mut doc = faint_page(content);
+ assert_eq!(text::text_blocks(&doc, 0).unwrap()[0].rows[0].len(), 2, "two runs on one row");
+ text::rewrite_block(&mut doc, 0, 0, Some("FAINTS"), &BlockStyle::default()).unwrap();
+ let doc = reopen(&doc);
+ let shows = painted_shows(&doc);
+ let joined = streams(&doc, 0).join("\n");
+ assert_eq!(shows.iter().map(|s| s.text.as_str()).collect::<String>(), "FAINTS", "{joined}");
+ for s in &shows {
+ assert_eq!((s.render_mode, s.stroke.as_slice(), s.line_width), (1, ["1 0 0 RG".to_string()].as_slice(), 2.0), "{s:?}: {joined}");
+ assert_eq!(s.gs, ["GS1"], "{s:?}: {joined}");
+ }
+}
+
+/// A `gs` has no inverse, so one run's ExtGState stayed in force for the runs the styled rewrite
+/// drew after it. Two ExtGStates at the same opacity paint the same (see `Paint`), so their runs
+/// share a paragraph, but either may set what that doesn't compare (a dash, a line cap): each run
+/// is drawn with only the ones its source selected.
+#[test]
+fn a_styled_rewrite_does_not_carry_one_runs_extgstate_into_the_next() {
+ // "FAI" ends at 40 + 72 * (0.611 + 0.667 + 0.278) = 152.032; "NT" is blue, so another run.
+ let content = "q BT /GS3 gs /F1 72 Tf 40 400 Td (FAI) Tj ET Q BT /GS1 gs /F1 72 Tf 0 0 1 rg 152.032 400 Td (NT) Tj ET
+";
+ for new in ["FAINT", "FAINTS"] {
+ let mut doc = faint_page(content);
+ assert_eq!(text::text_blocks(&doc, 0).unwrap()[0].rows[0].len(), 2, "two runs on one row");
+ text::rewrite_block(&mut doc, 0, 0, Some(new), &BlockStyle::default()).unwrap();
+ let doc = reopen(&doc);
+ let shows = painted_shows(&doc);
+ let joined = streams(&doc, 0).join(
+ "
+",
+ );
+ assert_eq!(shows.iter().map(|s| s.text.as_str()).collect::<String>(), new, "{joined}");
+ let gs: Vec<_> = shows.iter().map(|s| (s.text.as_str(), s.gs.as_slice())).collect();
+ assert_eq!(gs.first(), Some(&("FAI", ["GS3".to_string()].as_slice())), "{gs:?}: {joined}");
+ assert!(gs.iter().skip(1).all(|(_, gs)| *gs == ["GS1"]), "{gs:?}: {joined}");
+ }
+}
+
+/// An ExtGState's `/LW` sets the line width as `w` does: `2 w /GS2 gs` (`/LW 4`) outlines at 4,
+/// and the rewrite replayed `/GS2 gs` and then `2 w`. `/GS2 gs 1 w` outlines at 1, and the
+/// rewrite replayed only `/GS2 gs`, since 1 is the initial width.
+#[test]
+fn a_rewritten_paragraph_keeps_the_line_width_an_extgstate_set() {
+ for (content, width) in [
+ ("BT /F1 72 Tf 1 Tr 2 w /GS2 gs 40 400 Td (OUTLINE) Tj ET\n", 4.0),
+ ("BT /F1 72 Tf 1 Tr /GS2 gs 2 w 40 400 Td (OUTLINE) Tj ET\n", 2.0),
+ ("BT /F1 72 Tf 1 Tr /GS2 gs 1 w 40 400 Td (OUTLINE) Tj ET\n", 1.0),
+ ] {
+ assert_eq!(painted_shows(&faint_page(content))[0].line_width, width, "the fixture: {content}");
+ for new in ["OUTLINE", "OUTLINED"] {
+ let mut doc = faint_page(content);
+ text::rewrite_block(&mut doc, 0, 0, Some(new), &BlockStyle::default()).unwrap();
+ let doc = reopen(&doc);
+ let shows = painted_shows(&doc);
+ assert_eq!(
+ shows.iter().map(|s| (s.text.as_str(), s.render_mode, s.line_width)).collect::<Vec<_>>(),
+ [(new, 1, width)],
+ "{content} -> {new}: {}",
+ streams(&doc, 0).join("\n")
+ );
+ }
+ }
+}
+
+/// A line is rewritten in the state of its first run, so outline text partway through a text
+/// object came back filled: "FAI" filled and "NT" a red outline came back all filled. The change
+/// of `Tr` now starts a run. (A `gs` partway already starts a paragraph; see `Paint`.)
+#[test]
+fn a_rendering_mode_partway_through_a_line_starts_a_run_of_its_own() {
+ let content = "BT /F1 72 Tf 40 400 Td (FAI) Tj 1 Tr 1 0 0 RG 2 w (NT) Tj ET
+";
+ for new in ["FAINT", "FAINTS"] {
+ let mut doc = faint_page(content);
+ assert_eq!(text::text_blocks(&doc, 0).unwrap()[0].rows[0].len(), 2, "two runs on one row");
+ text::rewrite_block(&mut doc, 0, 0, Some(new), &BlockStyle::default()).unwrap();
+ let doc = reopen(&doc);
+ let shows = painted_shows(&doc);
+ let joined = streams(&doc, 0).join(
+ "
+",
+ );
+ assert_eq!(shows.iter().map(|s| s.text.as_str()).collect::<String>(), new, "{joined}");
+ let modes: Vec<_> = shows.iter().map(|s| (s.text.as_str(), s.render_mode, s.stroke.as_slice(), s.line_width)).collect();
+ assert_eq!(modes.first().map(|m| (m.0, m.1)), Some(("FAI", 0)), "{modes:?}: {joined}");
+ let outline = ["1 0 0 RG".to_string()];
+ assert!(modes.iter().skip(1).all(|m| (m.1, m.2, m.3) == (1, outline.as_slice(), 2.0)), "{modes:?}: {joined}");
+ }
+}
+
+/// Two ExtGStates at the same opacity paint text the same (see `Paint`), but one of them dashes
+/// its outline: a dashed outline after a solid one in the same text object joined its line and
+/// came back solid, drawn with the first run's selections. A different dash now starts a run.
+#[test]
+fn a_dash_partway_through_an_outlined_line_starts_a_run_of_its_own() {
+ let content = "BT /GS3 gs /F1 72 Tf 1 Tr 1 0 0 RG 2 w 40 400 Td (FAI) Tj /GS4 gs (NT) Tj ET
+";
+ for new in ["FAINT", "FAINTS"] {
+ let mut doc = faint_page(content);
+ assert_eq!(text::text_blocks(&doc, 0).unwrap()[0].rows[0].len(), 2, "two runs on one row");
+ text::rewrite_block(&mut doc, 0, 0, Some(new), &BlockStyle::default()).unwrap();
+ let doc = reopen(&doc);
+ let shows = painted_shows(&doc);
+ let joined = streams(&doc, 0).join(
+ "
+",
+ );
+ assert_eq!(shows.iter().map(|s| s.text.as_str()).collect::<String>(), new, "{joined}");
+ let gs: Vec<_> = shows.iter().map(|s| (s.text.as_str(), s.gs.as_slice())).collect();
+ assert_eq!(gs.first(), Some(&("FAI", ["GS3".to_string()].as_slice())), "{gs:?}: {joined}");
+ assert!(gs.iter().skip(1).all(|(_, gs)| gs.last().is_some_and(|g| g == "GS4")), "{gs:?}: {joined}");
+ }
+}
+
+/// The dash and cap are compared as the operators set them too: `d` or `J` partway through an
+/// outlined line starts a run, and a malformed one (too long, not numbers, out of range) is ignored.
+#[test]
+fn a_dash_or_cap_operator_partway_through_an_outlined_line_starts_a_run() {
+ let long = format!("[{}] 0 d", "1 ".repeat(40));
+ for (change, runs) in [("[6 3] 0 d", 2), ("1 J", 2), (long.as_str(), 1), ("[(a) 3] 0 d", 1), ("[6 3] /x d", 1), ("7 J", 1), ("J", 1)] {
+ let content = format!("BT /F1 72 Tf 1 Tr 1 0 0 RG 2 w 40 400 Td (FAI) Tj {change} (NT) Tj ET\n");
+ let mut doc = faint_page(&content);
+ assert_eq!(text::text_blocks(&doc, 0).unwrap()[0].rows[0].len(), runs, "{change}");
+ text::rewrite_block(&mut doc, 0, 0, Some("FAINTS"), &BlockStyle::default()).unwrap();
+ let shows = painted_shows(&reopen(&doc));
+ assert_eq!(shows.iter().map(|s| s.text.as_str()).collect::<String>(), "FAINTS", "{change}");
+ }
+}
++
+ /// #4199, #3797: every space typed into a paragraph is saved, not folded into one.
+ #[test]
+ fn an_edited_paragraph_keeps_every_space_typed() {
+ for typed in ["Hello world", "Hello world end", " indented and trailing "] {
+ let mut doc = text_page("BT /F1 12 Tf 72 700 Td (Hello world) Tj ET");
+ assert_eq!(text::replace_block(&mut doc, 0, 0, typed).unwrap().substituted, None);
+ let doc = reopen(&doc);
+ let content = String::from_utf8_lossy(&page_content_bytes(&doc, 0)).into_owned();
+ assert!(content.contains(&format!("({typed}) Tj")), "{typed:?}: {content}");
+ assert_eq!(text::text_lines(&doc, 0).unwrap()[0].text, typed);
+ }
+ }
+
+ /// Spaces after a row's last word are kept but take no part in its alignment: a justified row
+ /// still reaches the margin, and a centred or right-aligned one sits where it would without them.
+ #[test]
+ fn spaces_after_a_rows_last_word_do_not_move_its_alignment() {
+ let shown = |text: &str, align: added::Align| {
+ let mut doc = text_page("BT /F1 12 Tf 72 700 Td (Hello world) Tj ET");
+ let style =
+ text::BlockStyle { family: Some((added::Family::Helvetica, false, false)), align: Some(align), width: Some(300.0), ..Default::default() };
+ text::rewrite_block(&mut doc, 0, 0, Some(text), &style).unwrap();
+ String::from_utf8_lossy(&page_content_bytes(&doc, 0)).into_owned()
+ };
+ for (plain, trailing, align) in [
+ ("aaa bbb ccc\nddd eee", "aaa bbb ccc \nddd eee", added::Align::Justify),
+ ("Title", "Title ", added::Align::Center),
+ ("Title", "Title ", added::Align::Right),
+ ] {
+ let first = trailing.split('\n').next().unwrap();
+ let expected = shown(plain, align);
+ let actual = shown(trailing, align).replace(&format!("({first})"), &format!("({})", first.trim_end()));
+ assert_eq!(actual, expected, "{trailing:?} {align:?}");
+ }
+ }
+
+ /// A justified row runs from the paragraph's left edge to the margin. Word spacing reaches only
+ /// the spaces between its words: the spaces before its first word and after its last are left
+ /// out of a stretched row (as before #4199), so the reopened rows stay inside the paragraph and
+ /// can be edited again in place.
+ #[test]
+ fn a_justified_row_keeps_its_indent_and_ends_at_the_margin() {
+ let style = text::BlockStyle {
+ family: Some((added::Family::Helvetica, false, false)),
+ align: Some(added::Align::Justify),
+ width: Some(300.0),
+ ..Default::default()
+ };
+ for typed in [" Pack my box with five dozen jugs\nof liquor", " Pack my box with five dozen jugs \nof liquor"] {
+ let mut doc = text_page("BT /F1 12 Tf 72 700 Td (Hello world) Tj ET");
+ text::rewrite_block(&mut doc, 0, 0, Some(typed), &style).unwrap();
+ let mut doc = reopen(&doc);
+ let lines = text::text_lines(&doc, 0).unwrap();
+ let first = lines.iter().find(|l| l.text.contains("Pack")).unwrap();
+ assert_eq!(first.text, "Pack my box with five dozen jugs", "{typed:?}");
+ assert!((first.rect[0] - 72.0).abs() < 0.5, "{typed:?}: {:?}", first.rect);
+ assert!(lines.iter().all(|l| l.rect[0] >= 72.0 - 0.5 && l.rect[2] <= 372.5), "{typed:?}: {lines:?}");
+ let blocks = text::text_blocks(&doc, 0).unwrap();
+ assert_eq!(blocks.len(), 1, "{typed:?}: {blocks:?}");
+ text::replace_block(&mut doc, 0, 0, "Edited again").unwrap();
+ let again = text::text_lines(&reopen(&doc), 0).unwrap();
+ assert!(again.iter().all(|l| l.rect[0] >= 0.0 && l.rect[2] <= 600.0), "{typed:?}: {again:?}");
+ }
+ }
+
+ /// A justified paragraph whose first row was typed with an indent is edited again through the
+ /// styled path (a draft with a line break): its rows keep their scaling and stay one paragraph
+ /// inside its width.
+ #[test]
+ fn a_justified_paragraph_with_an_indent_edits_again_at_its_own_scale() {
+ let style = text::BlockStyle {
+ family: Some((added::Family::Helvetica, false, false)),
+ align: Some(added::Align::Justify),
+ width: Some(300.0),
+ ..Default::default()
+ };
+ let mut doc = text_page("BT /F1 12 Tf 72 700 Td (Hello world) Tj ET");
+ text::rewrite_block(&mut doc, 0, 0, Some(" aaa bbb ccc \nddd"), &style).unwrap();
+ let mut doc = reopen(&doc);
+ let draft = text::text_blocks(&doc, 0).unwrap()[0].editor_text.replacen("ccc", "cc", 1);
+ assert!(draft.contains('\n'), "{draft:?}");
+ text::replace_block(&mut doc, 0, 0, &draft).unwrap();
+ let content = String::from_utf8_lossy(&page_content_bytes(&doc, 0)).into_owned();
+ let scales: Vec<&str> = content.lines().filter(|l| l.ends_with(" Tz")).collect();
+ assert!(scales.iter().all(|l| *l == "100 Tz"), "{scales:?}: {content}");
+ let doc = reopen(&doc);
+ let blocks = text::text_blocks(&doc, 0).unwrap();
+ assert_eq!(blocks.len(), 1, "{blocks:?}");
+ let lines = text::text_lines(&doc, 0).unwrap();
+ assert!(lines.iter().all(|l| l.rect[0] >= 72.0 - 0.5 && l.rect[2] <= 372.5), "{lines:?}");
+ }
+
+ /// A paragraph drawn at `0 Tz` has no width to justify into: nothing is divided by it. The
+ /// writer writes a number that isn't finite as 0, so the test looks for the word spacing and
+ /// the move a division by zero would add, rather than for `NaN`.
+ #[test]
+ fn justifying_text_at_zero_horizontal_scaling_divides_by_nothing() {
+ let mut doc = text_page("BT /F1 12 Tf 0 Tz 72 700 Td (Hello world) Tj ET");
+ let style = text::BlockStyle { family: Some((added::Family::Helvetica, false, false)), align: Some(added::Align::Justify), ..Default::default() };
+ text::rewrite_block(&mut doc, 0, 0, Some(" aaa bbb\nccc"), &style).unwrap();
+ let content = String::from_utf8_lossy(&page_content_bytes(&doc, 0)).into_owned();
+ let block = content.split("ET").next().unwrap();
+ assert_eq!(block.matches(" Tw").count(), 1, "only the paragraph's own 0 Tw: {content}");
+ let first_row = block.split(" Tm").nth(1).and_then(|rest| rest.split(" Tj").next()).unwrap();
+ assert!(!first_row.contains(" Td"), "no move before the first row: {content}");
+ }
+
+ /// The preview's box is the box of the rows Apply writes, measured to their last glyphs.
+ #[test]
+ fn a_drafts_box_is_the_box_apply_draws() {
+ let doc = text_page("BT /F1 12 Tf 14 TL 72 700 Td [(First) -2000 (row here )] TJ T* (Second row ) Tj ET");
+ let source = text::text_blocks(&doc, 0).unwrap().swap_remove(0);
+ let draft = source.editor_text.replacen("Second", "Third", 1);
+ let layout = text::text_edit_layout(&doc, 0, 0, &draft, &text::BlockStyle::default()).unwrap();
+ let mut applied = doc.clone();
+ text::replace_block(&mut applied, 0, 0, &draft).unwrap();
+ let boxed = text::text_lines(&applied, 0)
+ .unwrap()
+ .into_iter()
+ .map(|l| {
+ let end = l.ink.iter().map(|r| r[2]).fold(f64::NEG_INFINITY, f64::max);
+ [l.rect[0], l.rect[1], if end.is_finite() { end.min(l.rect[2]) } else { l.rect[2] }, l.rect[3]]
+ })
+ .reduce(|a, r| [a[0].min(r[0]), a[1].min(r[1]), a[2].max(r[2]), a[3].max(r[3])]);
+ let (d, b) = (layout.draft_rect.unwrap(), boxed.unwrap());
+ assert!(d.iter().zip(b).all(|(x, y)| (x - y).abs() < 0.01), "{draft:?}: preview {d:?}, applied {b:?}");
+ }
+
+ /// The draft's box is measured to its last glyph, not over the spaces typed after it.
+ #[test]
+ fn a_drafts_box_leaves_out_spaces_after_the_last_word() {
+ let doc = text_page("BT /F1 12 Tf 72 700 Td (Hello world) Tj ET");
+ let layout = |text: &str| text::text_edit_layout(&doc, 0, 0, text, &text::BlockStyle::default()).unwrap().draft_rect;
+ assert_eq!(layout("Hello there "), layout("Hello there"));
+ }
+
+ /// A lone carriage return (an old Mac line ending) breaks the row, as `\n` and `\r\n` do.
+ #[test]
+ fn a_lone_carriage_return_breaks_an_edited_paragraph() {
+ let mut doc = text_page("BT /F1 12 Tf 72 700 Td (Hello world) Tj ET");
+ text::replace_block(&mut doc, 0, 0, "one\rtwo").unwrap();
+ let lines: Vec<String> = text::text_lines(&reopen(&doc), 0).unwrap().into_iter().map(|l| l.text).collect();
+ assert_eq!(lines, ["one", "two"]);
+ }
+
+ /// On the styled rewrite path too, `\r\n` is one row break and a lone `\r` another, and no
+ /// carriage return reaches the page: with no other formatting, and with a part set in red.
+ #[test]
+ fn carriage_returns_break_rows_on_the_styled_path() {
+ let draft = "one\r\ntwo\rthree";
+ let red = text::RangeStyle {
+ range: 0..3,
+ family: None,
+ resource: None,
+ size: None,
+ color: Some([1.0, 0.0, 0.0]),
+ underline: None,
+ char_spacing: None,
+ scale: None,
+ };
+ for style in [text::BlockStyle::default(), text::BlockStyle { ranges: vec![red], ..Default::default() }] {
+ let mut doc = text_page("BT /F1 12 Tf 72 700 Td (Hello world) Tj ET");
+ text::rewrite_block(&mut doc, 0, 0, Some(draft), &style).unwrap();
+ let doc = reopen(&doc);
+ let content = page_content_bytes(&doc, 0);
+ assert!(!content.contains(&b'\r') && !content.windows(2).any(|w| w == b"\\r"), "{}", String::from_utf8_lossy(&content));
+ let lines: Vec<String> = text::text_lines(&doc, 0).unwrap().into_iter().map(|l| l.text).collect();
+ assert_eq!(lines, ["one", "two", "three"], "{style:?}");
+ }
+ }
+
+ /// A subset with no space glyph shows each space as a positioning gap one space wide, so a run
+ /// of spaces still takes a run's width (#4199).
+ #[test]
+ fn a_run_of_spaces_in_a_subset_without_a_space_glyph_keeps_its_width() {
+ let mut doc = text_page("BT /F2 12 Tf 72 700 Td (ab) Tj /F1 12 Tf ( ) Tj /F2 12 Tf (ba) Tj ET");
+ assert_eq!(text::replace_block(&mut doc, 0, 0, "ab ba ab").unwrap().substituted, None);
+ let content = String::from_utf8_lossy(&page_content_bytes(&reopen(&doc), 0)).into_owned();
+ assert_eq!(content.matches("[-330] TJ").count(), 5, "{content}");
+ }