Skip to content

A fast, helpful, and open-source document parser

wip

Logan Markewich4ae46b74eb35
diff --git a/crates/liteparse/src/markdown_layout/classify.rs b/crates/liteparse/src/markdown_layout/classify.rs
index 7648426..62a007d 100644
--- a/crates/liteparse/src/markdown_layout/classify.rs
+++ b/crates/liteparse/src/markdown_layout/classify.rs
@@ -2,9 +2,9 @@ use crate::types::{OutlineTarget, ParsedPage, ProjectedLine};
use super::blocks::{Block, paragraph_from_accum};
use super::headings::{
- HEADING_MAX_TEXT_CHARS, MAX_HEADING_LEVELS, heading_level_for, is_caption_line, is_toc_title,
- looks_like_bold_heading, looks_like_numbered_bold_heading, outline_heading_level, page_is_toc,
- struct_heading_level,
+ HEADING_MAX_TEXT_CHARS, MAX_HEADING_LEVELS, heading_level_for, heading_size_of,
+ is_caption_line, is_toc_title, looks_like_bold_heading, looks_like_numbered_bold_heading,
+ outline_heading_level, page_is_toc, struct_heading_level,
};
use super::hr::detect_horizontal_rules;
use super::inline::{
@@ -605,7 +605,7 @@ fn classify_region(
let size_level = if is_caption_line(text) || toc_suppress {
None
} else {
- heading_level_for(line.dominant_font_size, heading_map)
+ heading_level_for(heading_size_of(line), heading_map)
};
// Guard against height-jitter false headings: a line that flows from
// the previous line (same paragraph) AND starts lowercase is a
diff --git a/crates/liteparse/src/markdown_layout/headings.rs b/crates/liteparse/src/markdown_layout/headings.rs
index 964f013..d8b15c9 100644
--- a/crates/liteparse/src/markdown_layout/headings.rs
+++ b/crates/liteparse/src/markdown_layout/headings.rs
@@ -562,7 +562,7 @@ pub fn compute_body_size(pages: &[ParsedPage]) -> f32 {
if is_rotated_line(line) {
continue;
}
- let size = line.dominant_font_size;
+ let size = heading_size_of(line);
if size <= 0.0 {
continue;
}
@@ -634,7 +634,7 @@ pub fn build_heading_map(pages: &[ParsedPage], body_size: f32) -> Vec<(f32, u8)>
if is_caption_line(&line.text) {
continue;
}
- let size = line.dominant_font_size;
+ let size = heading_size_of(line);
let margin = if line.font_size_is_estimated {
ESTIMATED_HEADING_SIZE_MARGIN
} else {
@@ -708,6 +708,14 @@ fn alpha_ratio(text: &str) -> f32 {
alpha as f32 / total as f32
}
+/// Size a line presents to heading detection: the precise matrix-derived
+/// `heading_font_size` when available (matrix-baked-size lines), else the
+/// `dominant_font_size`. Used by `compute_body_size`, `build_heading_map`, and
+/// the per-line heading-level lookup — NOT by table/paragraph grouping.
+pub(super) fn heading_size_of(line: &ProjectedLine) -> f32 {
+ line.heading_font_size.unwrap_or(line.dominant_font_size)
+}
+
pub(super) fn heading_level_for(size: f32, heading_map: &[(f32, u8)]) -> Option<u8> {
for (s, level) in heading_map {
if (size - *s).abs() < FONT_SIZE_HEADING_TOLERANCE {
diff --git a/crates/liteparse/src/markdown_layout/test_helpers.rs b/crates/liteparse/src/markdown_layout/test_helpers.rs
index d733fc9..dc95741 100644
--- a/crates/liteparse/src/markdown_layout/test_helpers.rs
+++ b/crates/liteparse/src/markdown_layout/test_helpers.rs
@@ -15,6 +15,7 @@ pub(crate) fn line(text: &str, x: f32, y: f32, h: f32, size: f32) -> ProjectedLi
indent_x: x,
dominant_font_size: size,
font_size_is_estimated: false,
+ heading_font_size: None,
dominant_font_name: Some("Arial".into()),
all_bold: false,
all_italic: false,
@@ -85,6 +86,7 @@ pub(crate) fn line_with_spans(cells: &[(&str, f32)], y: f32, size: f32) -> Proje
indent_x: min_x,
dominant_font_size: size,
font_size_is_estimated: false,
+ heading_font_size: None,
dominant_font_name: Some("Arial".into()),
all_bold: false,
all_italic: false,
@@ -134,6 +136,7 @@ pub(crate) fn styled_line(spans: &[(&str, f32, Option<&str>)], y: f32, size: f32
indent_x: min_x,
dominant_font_size: size,
font_size_is_estimated: false,
+ heading_font_size: None,
dominant_font_name: Some("Arial".into()),
all_bold: false,
all_italic: false,
diff --git a/crates/liteparse/src/output/markdown.rs b/crates/liteparse/src/output/markdown.rs
index c4a8d48..1ebd8d2 100644
--- a/crates/liteparse/src/output/markdown.rs
+++ b/crates/liteparse/src/output/markdown.rs
@@ -100,6 +100,7 @@ mod tests {
indent_x: x,
dominant_font_size: size,
font_size_is_estimated: false,
+ heading_font_size: None,
dominant_font_name: Some("Arial".into()),
all_bold: false,
all_italic: false,
diff --git a/crates/liteparse/src/projection.rs b/crates/liteparse/src/projection.rs
index e33d33d..4d8d883 100644
--- a/crates/liteparse/src/projection.rs
+++ b/crates/liteparse/src/projection.rs
@@ -1,5 +1,12 @@
use crate::types::*;
use std::collections::{BTreeMap, HashMap, HashSet};
+use std::sync::LazyLock;
+
+/// Kill-switch for using the matrix-derived `font_height` as the size signal
+/// on matrix-baked-size lines (raw `font_size ≈ 1.0`). When set, fall back to
+/// the legacy char-weighted bbox-height estimate.
+static DISABLE_FONT_HEIGHT_SIZE: LazyLock<bool> =
+ LazyLock::new(|| std::env::var("LITEPARSE_DISABLE_FONT_HEIGHT_SIZE").is_ok());
const FLOATING_SPACES: usize = 2;
const COLUMN_SPACES: usize = 4;
@@ -4604,6 +4611,10 @@ fn build_one_line(
let mut size_weights: HashMap<u32, (f32, usize)> = HashMap::new();
let mut height_weights: HashMap<u32, (f32, usize)> = HashMap::new();
+ // Matrix-derived true on-page size (`Tf_size × text_matrix_scale`, computed
+ // in extract.rs). For matrix-baked-size fonts this is the precise size the
+ // raw `font_size` (≈1.0) hides; jitter-free, unlike bbox height.
+ let mut font_height_weights: HashMap<u32, (f32, usize)> = HashMap::new();
let mut name_weights: HashMap<String, usize> = HashMap::new();
let mut bold_chars: usize = 0;
let mut italic_chars: usize = 0;
@@ -4644,6 +4655,13 @@ fn build_one_line(
let h_key = (it.height.max(0.0) * 100.0).round() as u32;
let e = height_weights.entry(h_key).or_insert((it.height, 0));
e.1 += n;
+ if let Some(fh) = it.font_height
+ && fh > 1.5
+ {
+ let fh_key = (fh * 100.0).round() as u32;
+ let e = font_height_weights.entry(fh_key).or_insert((fh, 0));
+ e.1 += n;
+ }
if let Some(name) = &it.font_name {
*name_weights.entry(name.clone()).or_insert(0) += n;
@@ -4684,8 +4702,9 @@ fn build_one_line(
.map(|(_, (s, _))| *s)
.unwrap_or(0.0);
// Fallback: when PDFium reports font_size ≤ 1.5 (size baked into the text
- // matrix), use char-weighted bbox height instead so heading detection has
- // something to chew on. Documented in MARKDOWN_PROGRESS.md.
+ // matrix), use char-weighted bbox height so the size-dependent grouping
+ // (tables, paragraphs) keeps its well-tuned behavior. Documented in
+ // MARKDOWN_PROGRESS.md.
let (dominant_font_size, font_size_is_estimated) = if dominant_size_from_font > 1.5 {
(dominant_size_from_font, false)
} else {
@@ -4697,6 +4716,21 @@ fn build_one_line(
(h, true)
};
+ // Precise size for *heading detection only*: when the raw size is baked,
+ // the matrix-derived `font_height` (Tf_size × text_matrix_scale) is the
+ // jitter-free on-page size, far better than the bbox-height estimate above
+ // for separating headings from body. Routed only to body-size + heading-map
+ // (see `heading_font_size` doc on ProjectedLine); table/paragraph grouping
+ // deliberately keeps the bbox-height value to avoid regressing TEDS.
+ let heading_font_size = if dominant_size_from_font > 1.5 || *DISABLE_FONT_HEIGHT_SIZE {
+ None
+ } else {
+ font_height_weights
+ .iter()
+ .max_by_key(|(k, (_, n))| (*n, **k))
+ .map(|(_, (h, _))| *h)
+ };
+
let dominant_font_name = name_weights
.iter()
.max_by_key(|(name, n)| (**n, *name))
@@ -4741,6 +4775,7 @@ fn build_one_line(
indent_x: bbox.x,
dominant_font_size,
font_size_is_estimated,
+ heading_font_size,
dominant_font_name,
all_bold: majority(bold_chars),
all_italic: majority(italic_chars),
diff --git a/crates/liteparse/src/types.rs b/crates/liteparse/src/types.rs
index 9fe4a97..38baea4 100644
--- a/crates/liteparse/src/types.rs
+++ b/crates/liteparse/src/types.rs
@@ -252,6 +252,13 @@ pub struct ProjectedLine {
/// based on glyph content (descenders, parens, capitals), so heading
/// detection must use a wider margin over body for these lines.
pub font_size_is_estimated: bool,
+ /// Precise matrix-derived size (`Tf_size × text_matrix_scale`) for
+ /// matrix-baked-size lines, when a glyph exposed a text matrix. Used
+ /// *only* by heading detection (body-size + heading map), where the
+ /// jitter-free value beats the bbox-height estimate in `dominant_font_size`.
+ /// Deliberately NOT consumed by table/paragraph grouping, which stay on
+ /// `dominant_font_size` to avoid perturbing well-tuned line grouping.
+ pub heading_font_size: Option<f32>,
pub dominant_font_name: Option<String>,
pub all_bold: bool,
pub all_italic: bool,