Skip to content

DesignCraft: An open-source page and layout design editor written by scratch in pure Rust

Merge pdf-rtl-text: PDF: right-to-left lines carry no line-wide ActualText

aionggeda793e300d0
diff --cc crates/pdf/src/rtl_tests.rs
index 82dd97542,c2ec5bb96..724eb1308
--- a/crates/pdf/src/rtl_tests.rs
+++ b/crates/pdf/src/rtl_tests.rs
@@@ -186,21 -178,51 +178,67 @@@ fn arabic_ligatures_extract_their_lette
}
let bytes = export_pdf(&d, &Cache::new(), &PdfOptions::default()).unwrap();
assert_eq!(extract_text(&bytes)[0], text);
- assert_eq!(line_actual_texts(&bytes), [text]);
+ }
+
+ /// How many marked-content sections the first page opens while an ActualText span is open.
+ /// Viewers such as poppler drop the text before an ActualText span nested in another.
+ fn opened_inside_actual_text(bytes: &[u8]) -> usize {
+ use hayro_syntax::content::ops::TypedInstruction;
+ use hayro_syntax::object::Object;
+ let pdf = hayro_syntax::Pdf::new(bytes.to_vec()).expect("parse");
+ let page = &pdf.pages()[0];
+ let (mut open, mut nested) = (Vec::new(), 0);
+ let mut ops = page.typed_operations();
+ while let Some(op) = ops.next() {
+ let actual = match op {
+ TypedInstruction::BeginMarkedContentWithProperties(op) => matches!(op.1, Object::Dict(d) if d.contains_key(b"ActualText")),
+ TypedInstruction::BeginMarkedContent(_) => false,
+ TypedInstruction::EndMarkedContent(_) => {
+ open.pop();
+ continue;
+ }
+ _ => continue,
+ };
+ nested += usize::from(open.contains(&true));
+ open.push(actual);
+ }
+ nested
+ }
+
+ #[test]
+ fn right_to_left_lines_carry_actual_text_only_on_their_clusters() {
+ // Shadda clusters on contextual forms and pointed letters, in a system font with Arabic and
+ // Hebrew (skipped without one): krilla gives such clusters an ActualText span of their own,
+ // which must not nest in another (poppler drops the text before a nested one).
+ let lines = ["سيّد الكلّ مُحَمَّد", "ישראל אַבְרָהָם יִצְחָק"];
+ let mut d = doc(&lines.join("\n"), designcraft_fonts::DEFAULT_FAMILY, Rect::new(36.0, 36.0, 576.0, 300.0), rtl());
+ d.settings.glyph_fallback = true;
+ let sid = *d.stories.keys().next().unwrap();
+ let cs = compose_story(&d, sid, &ComposeOptions::default());
+ let glyphs: Vec<_> = cs.frames[0].lines.iter().flat_map(|l| &l.glyphs).collect();
+ if glyphs.iter().any(|g| g.gid == 0) || !glyphs.windows(2).any(|w| w[1].len == 0 && w[1].byte == w[0].byte) {
+ return;
+ }
+ for tagged in [false, true] {
+ let bytes = export_pdf(&d, &Cache::new(), &PdfOptions { tagged, ..PdfOptions::default() }).unwrap();
+ assert_eq!(opened_inside_actual_text(&bytes), 0, "tagged: {tagged}");
+ let spans = actual_texts(&bytes);
+ assert!(!spans.is_empty() && spans.iter().all(|t| !t.contains(' ') && t.chars().count() <= 3), "tagged: {tagged}: {spans:?}");
+ }
}
+
+/// A slanted (shatai) glyph in a right-to-left line is drawn inside the line's span, not in a span
+/// of its own (marked content can't nest).
+#[test]
+fn shatai_glyphs_in_right_to_left_lines_stay_in_the_line_span() {
+ hebrew_font();
+ let text = "דוד & שרה";
+ let mut d = doc(text, HEBREW, Rect::new(36.0, 36.0, 576.0, 300.0), rtl());
+ let sid = *d.stories.keys().next().unwrap();
+ d.story_mut(sid).unwrap().format_chars(0.."דוד".len(), |f| f.over.shatai_magnification = Some(30.0));
+ for tagged in [false, true] {
+ let bytes = export_pdf(&d, &Cache::new(), &PdfOptions { tagged, ..PdfOptions::default() }).unwrap();
+ assert_eq!(line_actual_texts(&bytes), [text], "tagged: {tagged}");
+ assert_eq!(extract_text(&bytes)[0], text, "tagged: {tagged}");
+ }
+}