A fast, helpful, and open-source document parser
Split a ligature's advance across the chars it decodes to
Sebas046aef1d906a
diff --git a/crates/liteparse/src/raw_text.rs b/crates/liteparse/src/raw_text.rsindex 6be85ba..ccf2866 100644--- a/crates/liteparse/src/raw_text.rs+++ b/crates/liteparse/src/raw_text.rs@@ -94,7 +94,8 @@ pub struct RawTextItem {/// 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+ /// `text_width` (0 where the font reports none). A ligature's advance is split+ /// evenly across the chars it decodes to. `Some` only from/// [`extract_raw_text_items_with_glyph_widths`].pub glyph_widths: Option<Vec<f32>>,pub x: f32,@@ -285,6 +286,22 @@ fn load_glyph(}}+/// pdfium decodes a ligature such as `fi` into one char per letter, each with+/// the ligature's char code, loose box and full advance. Split that one advance+/// evenly across its letters so the widths add up to the printed advance.+/// `text_width` keeps the repeated advances, as the C extractor does.+fn split_ligature_advances(glyphs: &[Glyph], mut widths: Vec<f32>) -> Vec<f32> {+ let mut start = 0;+ for run in glyphs.chunk_by(|a, b| {+ !a.generated && !b.generated && a.char_code == b.char_code && a.loose == b.loose+ }) {+ let share = widths[start] / run.len() as f32;+ widths[start..start + run.len()].fill(share);+ start += run.len();+ }+ widths+}+/// Counter-clockwise baseline rotation of a text matrix, in radians, from the/// direction of its first column. Formed the way the C extractor forms it — the/// degrees wrapped into `[0, 360)` first, then negated and converted — so an@@ -451,7 +468,7 @@ fn build_item(glyph_names,angle_radians: first.angle,text_width,- glyph_widths: keep_glyph_widths.then_some(widths),+ glyph_widths: keep_glyph_widths.then(|| split_ligature_advances(glyphs, widths)),x: bounds.left,y: bounds.top,width: bounds.right - bounds.left,diff --git a/crates/liteparse/tests/integration_test.rs b/crates/liteparse/tests/integration_test.rsindex d63b106..70b06c4 100644--- a/crates/liteparse/tests/integration_test.rs+++ b/crates/liteparse/tests/integration_test.rs@@ -1040,8 +1040,9 @@ fn raw_text_items_account_for_every_glyph() {);}-/// The opt-in entry point returns the same items plus one advance per glyph, and-/// those advances are the ones `text_width` sums.+/// The opt-in entry point returns the same items plus one advance per glyph.+/// Those advances add up to `text_width`, except in an item with a ligature+/// (sample.pdf prints "file" with an fi ligature), where they add up to less.#[test]#[serial]fn glyph_widths_are_opt_in_and_sum_to_text_width() {@@ -1061,11 +1062,16 @@ fn glyph_widths_are_opt_in_and_sum_to_text_width() {assert_eq!(plain.len(), with_widths.len());let mut uneven = false;+ let mut ligature = 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);+ if sum < b.text_width {+ ligature = true;+ } else {+ 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");@@ -1077,6 +1083,7 @@ fn glyph_widths_are_opt_in_and_sum_to_text_width() {uneven,"a proportional font gives glyphs different advances");+ assert!(ligature, "sample.pdf has an item with a ligature");}/// One page that shows `(\002ce) Tj` in 12 pt Helvetica, where char code 2 is the@@ -1126,7 +1133,8 @@ fn ligature_pdf() -> Vec<u8> {/// Without the opt-in, `text_width` is the Helvetica advances of the item's glyphs:/// a ligature glyph is read by its char code and each of its two chars carries its-/// whole advance, and a generated space with no font adds 0.+/// whole advance, and a generated space with no font adds 0. With the opt-in, the+/// two chars share the ligature's one advance.#[test]#[serial]fn text_width_reads_char_codes_and_counts_a_missing_width_as_zero() {@@ -1150,7 +1158,7 @@ fn text_width_reads_char_codes_and_counts_a_missing_width_as_zero() {let with_widths = extract_raw_text_items_with_glyph_widths(&page, &text_page, &view_box, None);assert_eq!(with_widths[0].glyph_widths,- Some(vec![6.0, 6.0, 6.0, 6.672])+ Some(vec![3.0, 3.0, 6.0, 6.672]));let document = lib