Skip to content

A fast, helpful, and open-source document parser

wip

Logan Markewichfe72f540d280
diff --git a/crates/liteparse/src/extract.rs b/crates/liteparse/src/extract.rs
index fb5ee35..b30adc0 100644
--- a/crates/liteparse/src/extract.rs
+++ b/crates/liteparse/src/extract.rs
@@ -94,6 +94,7 @@ pub(crate) fn extract_pages_and_images(
assign_links(&mut text_items, &page.links(&view_box));
}
let graphics = extract_page_graphics(&page, &view_box);
+ assign_strikethrough(&mut text_items, &graphics);
let struct_nodes = extract_page_struct_nodes(&page, &view_box);
let image_refs = extract_page_image_refs(&page, page_number);
@@ -159,6 +160,70 @@ fn assign_links(items: &mut [TextItem], links: &[PdfLink]) {
}
}
+/// Max thickness (pt) for a stroke/rect to count as a strikethrough line.
+const STRIKE_MAX_THICKNESS_PT: f32 = 2.0;
+/// A strike line must horizontally cover at least this fraction of the item.
+const STRIKE_MIN_COVER_FRACTION: f32 = 0.6;
+
+/// Mark text items whose vertical *middle* band is crossed by a thin horizontal
+/// line (a strikethrough). The line may be drawn as a `Stroke` or as a thin
+/// filled `Rect`. Underlines (near the baseline) and overlines (near the top)
+/// are excluded by the band check; table rules / HRs almost never pass through
+/// the middle of a glyph run, and the per-item width-coverage gate keeps long
+/// dividers from tagging incidental text they happen to cross.
+fn assign_strikethrough(items: &mut [TextItem], graphics: &[GraphicPrimitive]) {
+ // Reduce graphics to horizontal segments: (xmin, xmax, y_center).
+ let mut segs: Vec<(f32, f32, f32)> = Vec::new();
+ for g in graphics {
+ match g {
+ GraphicPrimitive::Stroke {
+ x1,
+ y1,
+ x2,
+ y2,
+ width,
+ ..
+ } => {
+ let dy = (y1 - y2).abs();
+ let dx = (x1 - x2).abs();
+ if dy <= STRIKE_MAX_THICKNESS_PT && *width <= STRIKE_MAX_THICKNESS_PT && dx > dy {
+ segs.push((x1.min(*x2), x1.max(*x2), (y1 + y2) * 0.5));
+ }
+ }
+ GraphicPrimitive::Rect { bbox, .. } => {
+ // A thin, wide filled rect acts as a line.
+ if bbox.height <= STRIKE_MAX_THICKNESS_PT && bbox.width > bbox.height {
+ segs.push((bbox.x, bbox.x + bbox.width, bbox.y + bbox.height * 0.5));
+ }
+ }
+ }
+ }
+ if segs.is_empty() {
+ return;
+ }
+
+ for item in items.iter_mut() {
+ if item.width <= 0.0 || item.height <= 0.0 || item.text.trim().is_empty() {
+ continue;
+ }
+ // Viewport space is top-left origin, so `y` is the top edge. The middle
+ // band sits below the top and above the baseline, excluding over/underlines.
+ let band_top = item.y + item.height * 0.20;
+ let band_bot = item.y + item.height * 0.65;
+ let (ix0, ix1) = (item.x, item.x + item.width);
+ for &(sx0, sx1, sy) in &segs {
+ if sy < band_top || sy > band_bot {
+ continue;
+ }
+ let overlap = (ix1.min(sx1) - ix0.max(sx0)).max(0.0);
+ if overlap >= item.width * STRIKE_MIN_COVER_FRACTION {
+ item.strike = true;
+ break;
+ }
+ }
+ }
+}
+
/// Walk the document outline (bookmarks). Returns entries in pre-order.
/// Empty when the PDF has no outline.
pub(crate) fn extract_outline(document: &Document) -> Vec<OutlineTarget> {
@@ -1538,6 +1603,7 @@ impl SegmentBuilder {
stroke_color: self.stroke_color.clone(),
confidence: None,
link: None,
+ strike: false,
});
}
@@ -1550,6 +1616,52 @@ mod tests {
use super::*;
use std::f32::consts::PI;
+ fn strike_item() -> TextItem {
+ TextItem {
+ text: "word".to_string(),
+ x: 100.0,
+ y: 100.0,
+ width: 40.0,
+ height: 10.0,
+ ..Default::default()
+ }
+ }
+
+ fn h_stroke(x1: f32, x2: f32, y: f32) -> GraphicPrimitive {
+ GraphicPrimitive::Stroke {
+ x1,
+ y1: y,
+ x2,
+ y2: y,
+ color: None,
+ width: 0.5,
+ }
+ }
+
+ #[test]
+ fn strike_midline_stroke_detected() {
+ let mut items = [strike_item()];
+ // Line through the vertical middle (y≈105) spanning the item width.
+ assign_strikethrough(&mut items, &[h_stroke(100.0, 140.0, 105.0)]);
+ assert!(items[0].strike);
+ }
+
+ #[test]
+ fn strike_underline_not_detected() {
+ let mut items = [strike_item()];
+ // Line near the baseline (bottom, y≈110) is an underline, not a strike.
+ assign_strikethrough(&mut items, &[h_stroke(100.0, 140.0, 110.0)]);
+ assert!(!items[0].strike);
+ }
+
+ #[test]
+ fn strike_short_line_not_detected() {
+ let mut items = [strike_item()];
+ // Mid-band but only covers ~25% of the item width.
+ assign_strikethrough(&mut items, &[h_stroke(100.0, 110.0, 105.0)]);
+ assert!(!items[0].strike);
+ }
+
fn ti(text: &str, x: f32, y: f32, w: f32, h: f32) -> TextItem {
TextItem {
text: text.to_string(),
diff --git a/crates/liteparse/src/markdown_layout/classify.rs b/crates/liteparse/src/markdown_layout/classify.rs
index e2d091b..b649856 100644
--- a/crates/liteparse/src/markdown_layout/classify.rs
+++ b/crates/liteparse/src/markdown_layout/classify.rs
@@ -905,7 +905,12 @@ fn classify_region(
state.reset_list();
let inline = render_line_inline(line);
let raw = collapse_whitespace(text);
- let uniform = line_uniform_style(line).map(|s| (s.bold, s.italic));
+ // Strike has no block-level Paragraph flag, so a struck line
+ // must keep the per-line `inline` rendering (which emits `~~…~~`)
+ // rather than take the uniform raw-text fast path.
+ let uniform = line_uniform_style(line)
+ .filter(|s| !s.strike)
+ .map(|s| (s.bold, s.italic));
state.paragraph = Some(ParaAccum {
raw,
inline,
diff --git a/crates/liteparse/src/markdown_layout/inline.rs b/crates/liteparse/src/markdown_layout/inline.rs
index b3a39a6..e0c1e0d 100644
--- a/crates/liteparse/src/markdown_layout/inline.rs
+++ b/crates/liteparse/src/markdown_layout/inline.rs
@@ -1,4 +1,4 @@
-use crate::projection::{is_bold_item, is_italic_item, is_mono_item};
+use crate::projection::{is_bold_item, is_italic_item, is_mono_item, is_strike_item};
use crate::types::{ProjectedLine, TextItem};
use super::paragraphs::{collapse_whitespace, dehyphenate_join};
@@ -9,6 +9,7 @@ pub(super) struct SpanStyle {
pub(super) bold: bool,
pub(super) italic: bool,
pub(super) mono: bool,
+ pub(super) strike: bool,
}
impl SpanStyle {
@@ -17,11 +18,12 @@ impl SpanStyle {
bold: is_bold_item(item),
italic: is_italic_item(item),
mono: is_mono_item(item),
+ strike: is_strike_item(item),
}
}
pub(super) fn is_plain(self) -> bool {
- !self.bold && !self.italic && !self.mono
+ !self.bold && !self.italic && !self.mono && !self.strike
}
}
@@ -57,19 +59,28 @@ fn apply_link(inner: &str, url: &str) -> String {
/// inline code (`` `…` ``) doesn't compose with emphasis in CommonMark, so when
/// a span is mono we drop the `**/*` wrap. Bold + italic → `***…***`.
fn apply_style(inner: &str, style: SpanStyle) -> String {
- if style.mono {
+ let styled = if style.mono {
// Use backticks; if inner already contains backticks, switch to a
// longer fence (pair of backticks plus a space buffer) per CommonMark.
if inner.contains('`') {
- return format!("`` {} ``", inner);
+ format!("`` {} ``", inner)
+ } else {
+ format!("`{}`", inner)
}
- return format!("`{}`", inner);
- }
- match (style.bold, style.italic) {
- (true, true) => format!("***{}***", inner),
- (true, false) => format!("**{}**", inner),
- (false, true) => format!("*{}*", inner),
- (false, false) => inner.to_string(),
+ } else {
+ match (style.bold, style.italic) {
+ (true, true) => format!("***{}***", inner),
+ (true, false) => format!("**{}**", inner),
+ (false, true) => format!("*{}*", inner),
+ (false, false) => inner.to_string(),
+ }
+ };
+ // Strikethrough (GFM `~~…~~`) wraps outermost so it composes with emphasis
+ // and inline code (`~~**foo**~~`, `~~`bar`~~`).
+ if style.strike {
+ format!("~~{}~~", styled)
+ } else {
+ styled
}
}
@@ -361,6 +372,32 @@ mod tests {
assert_eq!(out, "[link](<https://example.com/a b>)");
}
+ #[test]
+ fn render_line_inline_strike_span() {
+ // A struck-through span mid-line → `~~word~~`, plain neighbors untouched.
+ let mut l = styled_line(
+ &[
+ ("keep this", 50.0, Some("Arial")),
+ ("removed", 150.0, Some("Arial")),
+ ],
+ 100.0,
+ 10.0,
+ );
+ l.spans[1].strike = true;
+ let out = render_line_inline(&l);
+ assert!(out.contains("keep this"), "got: {out}");
+ assert!(out.contains("~~removed~~"), "got: {out}");
+ }
+
+ #[test]
+ fn render_line_inline_strike_composes_with_bold() {
+ // Strike wraps outside emphasis: `~~**word**~~`.
+ let mut l = styled_line(&[("gone", 50.0, Some("Arial-Bold"))], 100.0, 10.0);
+ l.spans[0].strike = true;
+ let out = render_line_inline(&l);
+ assert_eq!(out, "~~**gone**~~");
+ }
+
#[test]
fn render_line_inline_mono_span() {
let l = styled_line(
diff --git a/crates/liteparse/src/markdown_layout/paragraphs.rs b/crates/liteparse/src/markdown_layout/paragraphs.rs
index 09a2d65..73bec51 100644
--- a/crates/liteparse/src/markdown_layout/paragraphs.rs
+++ b/crates/liteparse/src/markdown_layout/paragraphs.rs
@@ -249,7 +249,9 @@ pub(super) fn append_to_paragraph(accum: &mut ParaAccum, next_line: &ProjectedLi
return;
}
let next_inline = render_line_inline(next_line);
- let next_uniform: Option<SpanStyle> = line_uniform_style(next_line);
+ // A struck line has no block-level flag, so it can't use the uniform
+ // raw-text fast path — drop it to `None` to force the `inline` rendering.
+ let next_uniform: Option<SpanStyle> = line_uniform_style(next_line).filter(|s| !s.strike);
if accum.raw.is_empty() {
accum.raw.push_str(&next_raw);
diff --git a/crates/liteparse/src/projection.rs b/crates/liteparse/src/projection.rs
index 1e2e791..5b6329a 100644
--- a/crates/liteparse/src/projection.rs
+++ b/crates/liteparse/src/projection.rs
@@ -2951,6 +2951,10 @@ pub(crate) fn is_italic_item(item: &TextItem) -> bool {
false
}
+pub(crate) fn is_strike_item(item: &TextItem) -> bool {
+ item.strike
+}
+
pub(crate) fn is_mono_item(item: &TextItem) -> bool {
if let Some(flags) = item.font_flags
&& flags & PDF_FONT_FLAG_FIXED_PITCH != 0
diff --git a/crates/liteparse/src/types.rs b/crates/liteparse/src/types.rs
index 38baea4..0067a98 100644
--- a/crates/liteparse/src/types.rs
+++ b/crates/liteparse/src/types.rs
@@ -62,6 +62,11 @@ pub struct TextItem {
/// rectangle. Populated in `extract.rs`; consumed by the markdown emitter.
#[serde(skip_serializing_if = "Option::is_none")]
pub link: Option<String>,
+ /// Whether a thin horizontal stroke/rect crosses this item's vertical middle
+ /// band (a strikethrough line). Populated in `extract.rs`; consumed by the
+ /// markdown emitter to wrap the text in `~~…~~`.
+ #[serde(skip_serializing_if = "std::ops::Not::not")]
+ pub strike: bool,
}
#[doc(hidden)]