A fast, helpful, and open-source document parser
Add opt-in per-glyph advance widths to raw text items
Sebas547c16220d46
diff --git a/crates/liteparse/src/lib.rs b/crates/liteparse/src/lib.rsindex 1719d80..62b157e 100644--- a/crates/liteparse/src/lib.rs+++ b/crates/liteparse/src/lib.rs@@ -15,7 +15,10 @@ pub use error::LiteParseError;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};+pub use raw_text::{+ RawTextItem, append_raw_widget_text_items, extract_raw_text_items,+ extract_raw_text_items_with_glyph_widths,+};pub use search::{SearchOptions, search_items};pub use types::{DocumentMetadata, ParsedPage, TextItem, WordBox};diff --git a/crates/liteparse/src/raw_text.rs b/crates/liteparse/src/raw_text.rsindex d6f45d3..ad952cd 100644--- a/crates/liteparse/src/raw_text.rs+++ b/crates/liteparse/src/raw_text.rs@@ -93,6 +93,10 @@ pub struct RawTextItem {pub angle_radians: f32,/// Sum of the glyphs' advance widths in text space.pub text_width: f32,+ /// One advance width per glyph, parallel to `char_codes`, in the units of+ /// `text_width` (0 where the font reports none). `Some` only from+ /// [`extract_raw_text_items_with_glyph_widths`].+ pub glyph_widths: Option<Vec<f32>>,pub x: f32,pub y: f32,pub width: f32,@@ -154,6 +158,27 @@ pub fn extract_raw_text_items(text_page: &TextPage,view_box: &RectF,glyph_resolver: Option<&dyn GlyphResolver>,+) -> Vec<RawTextItem> {+ extract(page, text_page, view_box, glyph_resolver, false)+}++/// [`extract_raw_text_items`] that also fills [`RawTextItem::glyph_widths`], for+/// callers that split an item's box per glyph.+pub fn extract_raw_text_items_with_glyph_widths(+ page: &Page,+ text_page: &TextPage,+ view_box: &RectF,+ glyph_resolver: Option<&dyn GlyphResolver>,+) -> Vec<RawTextItem> {+ extract(page, text_page, view_box, glyph_resolver, true)+}++fn extract(+ page: &Page,+ text_page: &TextPage,+ view_box: &RectF,+ glyph_resolver: Option<&dyn GlyphResolver>,+ keep_glyph_widths: bool,) -> Vec<RawTextItem> {let char_count = text_page.char_count();let page_rotation = page.rotation();@@ -176,6 +201,7 @@ pub fn extract_raw_text_items(run,glyph_resolver,&mut outline_cache,+ keep_glyph_widths,) {items.push(item);}@@ -321,6 +347,7 @@ fn build_item(glyphs: &[Glyph],glyph_resolver: Option<&dyn GlyphResolver>,outline_cache: &mut OutlineSupportCache,+ keep_glyph_widths: bool,) -> Option<RawTextItem> {let first = glyphs.first()?;let first_char = text_page.char_at_unchecked(first.index);@@ -330,7 +357,6 @@ fn build_item(.and_then(|obj| unsafe { Font::from_text_object(obj) });let mut bounds = first.loose;- let mut text_width = 0.0f32;let mut font_name = String::new();let mut font_ascent = 0.0f32;let mut font_descent = 0.0f32;@@ -342,9 +368,6 @@ fn build_item(font_descent = font.descent(font_size).unwrap_or(0.0);font_is_embedded = font.is_embedded();font_is_buggy = font_is_embedded && is_buggy_font(&font_name, font.font_type());- if let Some(width) = font.glyph_width_from_char_code(first.char_code, font_size) {- text_width += width;- }}let font_height = if first.text_object.is_some() {let scale_y = first_char@@ -361,17 +384,23 @@ fn build_item(bounds.top = bounds.top.min(glyph.loose.top);bounds.right = bounds.right.max(glyph.loose.right);bounds.bottom = bounds.bottom.max(glyph.loose.bottom);- if let Some(font) = &font {- let width = if glyph.generated {+ }+ // The first glyph is always read by char code, as `text_width` has always been.+ let widths: Vec<f32> = glyphs+ .iter()+ .enumerate()+ .map(|(i, glyph)| {+ let font = font.as_ref()?;+ if i > 0 && glyph.generated {font.glyph_width(glyph.unicode, font_size)} else {font.glyph_width_from_char_code(glyph.char_code, font_size)- };- if let Some(width) = width {- text_width += width;}- }- }+ })+ .map(|width| width.unwrap_or(0.0))+ .collect();+ // Summed in glyph order; adding a 0 for a missing width leaves the sum unchanged.+ let text_width = widths.iter().fold(0.0f32, |sum, width| sum + width);// A control / private-use decode in an embedded font flags the whole item.// `unicode` is 0 for an unmapped glyph, so those count too.if font_is_embedded && !font_is_buggy {@@ -422,6 +451,7 @@ fn build_item(glyph_names,angle_radians: first.angle,text_width,+ glyph_widths: keep_glyph_widths.then_some(widths),x: bounds.left,y: bounds.top,width: bounds.right - bounds.left,@@ -1018,6 +1048,7 @@ mod tests {glyph_names: None,angle_radians: angle,text_width: 0.0,+ glyph_widths: None,x,y,width,diff --git a/crates/liteparse/tests/integration_test.rs b/crates/liteparse/tests/integration_test.rsindex 1c78a34..9c8405f 100644--- a/crates/liteparse/tests/integration_test.rs+++ b/crates/liteparse/tests/integration_test.rs@@ -989,6 +989,45 @@ async fn test_ocr_rounds_fill_across_sparse_pages() {/// items add up to the page's char count minus the generated line breaks it/// drops, items keep their trailing whitespace, and a generated glyph only/// ever closes an item.+/// The opt-in entry point returns the same items plus one advance per glyph, and+/// those advances are the ones `text_width` sums.+#[test]+#[serial]+fn glyph_widths_are_opt_in_and_sum_to_text_width() {+ use liteparse::{extract_raw_text_items, extract_raw_text_items_with_glyph_widths};++ let lib = pdfium::Library::init();+ let document = lib+ .load_document("../../integration_tests_data/sample.pdf", None)+ .expect("sample.pdf loads");+ let page = document.page(0).expect("page 0 loads");+ let text_page = page.text().expect("text page loads");+ let view_box = page.view_box().expect("page has a bounding box");++ 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_eq!(plain.len(), with_widths.len());++ let mut uneven = 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());+ let sum = widths.iter().fold(0.0f32, |sum, w| sum + w);+ assert_eq!(sum.to_bits(), b.text_width.to_bits(), "{:?}", b.text);+ let mut b = b.clone();+ b.glyph_widths = None;+ assert_eq!(a, &b, "opt-in changes nothing else");+ uneven |= widths+ .windows(2)+ .any(|w| w[0] > 0.0 && w[1] > 0.0 && w[0] != w[1]);+ }+ assert!(+ uneven,+ "a proportional font gives glyphs different advances"+ );+}+#[test]#[serial]fn raw_text_items_account_for_every_glyph() {