Skip to content

A fast, helpful, and open-source document parser

feat(raw_text): per-glyph loose and strict boxes on the glyph-widths entry point

Sebas14a9e4bf0a7b
diff --git a/crates/liteparse/src/lib.rs b/crates/liteparse/src/lib.rs
index 62b157e..72f384c 100644
--- a/crates/liteparse/src/lib.rs
+++ b/crates/liteparse/src/lib.rs
@@ -16,7 +16,7 @@ pub use font_db_resolver::FontDbResolver;
pub use glyph_resolver::{GLYPH_RESOLVER_FONT_SIZE, GlyphResolver};
pub use parser::{LiteParse, ParseBatch, ParseResult, ParseSession, ScreenshotResult};
pub use raw_text::{
- RawTextItem, append_raw_widget_text_items, extract_raw_text_items,
+ GlyphBoxes, RawTextItem, append_raw_widget_text_items, extract_raw_text_items,
extract_raw_text_items_with_glyph_widths,
};
pub use search::{SearchOptions, search_items};
diff --git a/crates/liteparse/src/raw_text.rs b/crates/liteparse/src/raw_text.rs
index ccf2866..02af94e 100644
--- a/crates/liteparse/src/raw_text.rs
+++ b/crates/liteparse/src/raw_text.rs
@@ -98,6 +98,9 @@ pub struct RawTextItem {
/// evenly across the chars it decodes to. `Some` only from
/// [`extract_raw_text_items_with_glyph_widths`].
pub glyph_widths: Option<Vec<f32>>,
+ /// One entry per glyph, parallel to `char_codes`: its loose and strict boxes.
+ /// `Some` only from [`extract_raw_text_items_with_glyph_widths`].
+ pub glyph_boxes: Option<Vec<GlyphBoxes>>,
pub x: f32,
pub y: f32,
pub width: f32,
@@ -133,6 +136,17 @@ pub struct RawTextItem {
pub stroke_color: Option<u32>,
}
+/// One glyph's boxes in viewport space, as pdfium reports them.
+#[derive(Debug, Clone, Copy, PartialEq)]
+pub struct GlyphBoxes {
+ /// The glyph's advance cell: its advance along the baseline, the font's
+ /// ascent to descent across it (`FPDFText_GetLooseCharBox`).
+ pub loose: RectF,
+ /// The glyph's ink (`FPDFText_GetCharBox`). Not meaningful for a generated glyph.
+ /// The chars a ligature decodes to each get the whole ligature's box.
+ pub strict: RectF,
+}
+
/// Everything read per glyph before items are formed.
struct Glyph {
index: i32,
@@ -163,7 +177,8 @@ pub fn extract_raw_text_items(
extract(page, text_page, view_box, glyph_resolver, false)
}
-/// [`extract_raw_text_items`] that also fills [`RawTextItem::glyph_widths`], for
+/// [`extract_raw_text_items`] that also fills [`RawTextItem::glyph_widths`] and
+/// [`RawTextItem::glyph_boxes`], for
/// callers that split an item's box per glyph.
pub fn extract_raw_text_items_with_glyph_widths(
page: &Page,
@@ -469,6 +484,15 @@ fn build_item(
angle_radians: first.angle,
text_width,
glyph_widths: keep_glyph_widths.then(|| split_ligature_advances(glyphs, widths)),
+ glyph_boxes: keep_glyph_widths.then(|| {
+ glyphs
+ .iter()
+ .map(|glyph| GlyphBoxes {
+ loose: glyph.loose,
+ strict: glyph.strict,
+ })
+ .collect()
+ }),
x: bounds.left,
y: bounds.top,
width: bounds.right - bounds.left,
@@ -1066,6 +1090,7 @@ mod tests {
angle_radians: angle,
text_width: 0.0,
glyph_widths: None,
+ glyph_boxes: None,
x,
y,
width,
diff --git a/crates/liteparse/tests/integration_test.rs b/crates/liteparse/tests/integration_test.rs
index 70b06c4..b995993 100644
--- a/crates/liteparse/tests/integration_test.rs
+++ b/crates/liteparse/tests/integration_test.rs
@@ -1059,10 +1059,12 @@ fn glyph_widths_are_opt_in_and_sum_to_text_width() {
let plain = extract_raw_text_items(&page, &text_page, &view_box, None);
let with_widths = extract_raw_text_items_with_glyph_widths(&page, &text_page, &view_box, None);
assert!(plain.iter().all(|item| item.glyph_widths.is_none()));
+ assert!(plain.iter().all(|item| item.glyph_boxes.is_none()));
assert_eq!(plain.len(), with_widths.len());
let mut uneven = false;
let mut ligature = false;
+ let mut tight = false;
for (a, b) in plain.iter().zip(&with_widths) {
let widths = b.glyph_widths.as_ref().expect("opt-in fills glyph_widths");
assert_eq!(widths.len(), b.char_codes.len());
@@ -1072,8 +1074,15 @@ fn glyph_widths_are_opt_in_and_sum_to_text_width() {
} else {
assert_eq!(sum.to_bits(), b.text_width.to_bits(), "{:?}", b.text);
}
+ let boxes = b.glyph_boxes.as_ref().expect("opt-in fills glyph_boxes");
+ assert_eq!(boxes.len(), b.char_codes.len());
+ // Ink is shorter than the ascent-to-descent cell for most letters.
+ tight |= boxes
+ .iter()
+ .any(|g| g.strict.bottom - g.strict.top < g.loose.bottom - g.loose.top - 0.5);
let mut b = b.clone();
b.glyph_widths = None;
+ b.glyph_boxes = None;
assert_eq!(a, &b, "opt-in changes nothing else");
uneven |= widths
.windows(2)
@@ -1084,6 +1093,7 @@ fn glyph_widths_are_opt_in_and_sum_to_text_width() {
"a proportional font gives glyphs different advances"
);
assert!(ligature, "sample.pdf has an item with a ligature");
+ assert!(tight, "strict boxes hug the ink inside the loose cells");
}
/// One page that shows `(\002ce) Tj` in 12 pt Helvetica, where char code 2 is the