Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions napi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ pub struct TextItem {
pub width: f64,
pub height: f64,
pub font: String,
pub font_tag: String,
pub font_size: f64,
pub page: u32,
pub is_bold: bool,
Expand Down Expand Up @@ -505,6 +506,7 @@ pub fn extract_text_with_positions(
width: item.width as f64,
height: item.height as f64,
font: item.font,
font_tag: item.font_tag,
font_size: item.font_size as f64,
page: item.page,
is_bold: item.is_bold,
Expand Down
1 change: 1 addition & 0 deletions pdf_inspector.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ class TextItem:
width: float
height: float
font: str
font_tag: str
font_size: float
page: int
is_bold: bool
Expand Down
4 changes: 3 additions & 1 deletion src/bin/pdf2md.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,15 @@ fn format_items_json(items: &[TextItem]) -> String {
_ => String::new(),
};
format!(
r#"{{"text":"{}","page":{},"x":{:.2},"y":{:.2},"width":{:.2},"height":{:.2},"font":"{}","font_size":{:.2},"is_bold":{},"is_italic":{},"is_underline":{},"is_strikeout":{},"item_type":"{}","mcid":{}{}}}"#,
r#"{{"text":"{}","page":{},"x":{:.2},"y":{:.2},"width":{:.2},"height":{:.2},"font":"{}","font_tag":"{}","font_size":{:.2},"is_bold":{},"is_italic":{},"is_underline":{},"is_strikeout":{},"item_type":"{}","mcid":{}{}}}"#,
json_escape(&item.text),
item.page,
item.x,
item.y,
item.width,
item.height,
json_escape(&item.font),
json_escape(&item.font_tag),
item.font_size,
item.is_bold,
item.is_italic,
Expand Down Expand Up @@ -274,6 +275,7 @@ mod tests {
width: 23.456,
height: 9.876,
font: "F1".to_string(),
font_tag: String::new(),
font_size: 10.0,
page: 2,
is_bold: false,
Expand Down
17 changes: 17 additions & 0 deletions src/extractor/content_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,7 @@ pub(crate) fn extract_page_text_items(
base_font,
)
.to_string(),
font_tag: current_font.clone(),
Comment thread
abimaelmartell marked this conversation as resolved.
font_size: rendered_size,
page: page_num,
is_bold: is_bold_font(base_font) || desc_bold,
Expand Down Expand Up @@ -820,6 +821,7 @@ pub(crate) fn extract_page_text_items(
base_font,
)
.to_string(),
font_tag: current_font.clone(),
font_size: rendered_size,
page: page_num,
is_bold: is_bold_font(base_font) || desc_bold,
Expand Down Expand Up @@ -940,6 +942,7 @@ pub(crate) fn extract_page_text_items(
base_font,
)
.to_string(),
font_tag: current_font.clone(),
font_size: rendered_size,
page: page_num,
is_bold: is_bold_font(base_font) || desc_bold,
Expand Down Expand Up @@ -984,6 +987,7 @@ pub(crate) fn extract_page_text_items(
width,
height,
font: String::new(),
font_tag: String::new(),
font_size: 0.0,
page: page_num,
is_bold: false,
Expand Down Expand Up @@ -1101,6 +1105,7 @@ pub(crate) fn extract_page_text_items(
base_font,
)
.to_string(),
font_tag: current_font.clone(),
font_size: rendered_size,
page: page_num,
is_bold: is_bold_font(base_font) || desc_bold,
Expand Down Expand Up @@ -2056,6 +2061,18 @@ end"#;

const SHALOM_LOGICAL: &str = "\u{05E9}\u{05DC}\u{05D5}\u{05DD}"; // שלום

#[test]
fn items_carry_family_name_and_resource_tag() {
// `font` is the resolved /BaseFont family name; `font_tag` keeps the
// raw page resource tag so consumers can partition by font program
// even when two resources share a family.
let content = b"BT /F1 12 Tf 100 700 Tm <44434241> Tj ET";
let items = extract_hebrew_items(content);
assert_eq!(items.len(), 1);
assert_eq!(items[0].font, "TestHebrew");
assert_eq!(items[0].font_tag, "F1");
}

#[test]
fn visual_order_hebrew_ops_are_reversed() {
// Two show ops on one baseline painted left-to-right, each holding
Expand Down
2 changes: 2 additions & 0 deletions src/extractor/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3064,6 +3064,7 @@ mod tests {
height: 12.0,
font_size: 12.0,
font: String::new(),
font_tag: String::new(),
page,
is_bold: false,
is_italic: false,
Expand Down Expand Up @@ -3811,6 +3812,7 @@ mod tests {
height: 12.0,
font_size: 12.0,
font: String::new(),
font_tag: String::new(),
page,
is_bold: false,
is_italic: false,
Expand Down
2 changes: 2 additions & 0 deletions src/extractor/links.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ pub fn extract_page_links(doc: &Document, page_id: ObjectId, page_num: u32) -> V
width,
height,
font: String::new(),
font_tag: String::new(),
font_size: 0.0,
page: page_num,
is_bold: false,
Expand Down Expand Up @@ -447,6 +448,7 @@ pub(crate) fn walk_form_fields(
width,
height,
font: String::new(),
font_tag: String::new(),
font_size: 0.0,
page: page_num,
is_bold: false,
Expand Down
25 changes: 25 additions & 0 deletions src/extractor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1121,6 +1121,7 @@ pub(crate) fn merge_text_items(items: Vec<TextItem>) -> Vec<TextItem> {
width: end_x - first.x,
height: first.height,
font: first.font.clone(),
font_tag: first.font_tag.clone(),
font_size: first.font_size,
page: first.page,
is_bold: first.is_bold,
Expand Down Expand Up @@ -1424,6 +1425,7 @@ mod tests {
width,
height: 12.0,
font: "F1".into(),
font_tag: String::new(),
font_size: 12.0,
page: 1,
is_bold: false,
Expand Down Expand Up @@ -1682,6 +1684,7 @@ mod tests {
width: 50.0,
height: 12.0,
font: "F1".into(),
font_tag: String::new(),
font_size: 12.0,
page: 1,
is_bold: false,
Expand All @@ -1698,6 +1701,7 @@ mod tests {
width: 50.0,
height: 12.0,
font: "F1".into(),
font_tag: String::new(),
font_size: 12.0,
page: 1,
is_bold: false,
Expand All @@ -1714,6 +1718,7 @@ mod tests {
width: 80.0,
height: 12.0,
font: "F1".into(),
font_tag: String::new(),
font_size: 12.0,
page: 1,
is_bold: false,
Expand Down Expand Up @@ -2500,6 +2505,7 @@ mod tests {
width: 19.5,
height: 12.0,
font: "C2_0".into(),
font_tag: String::new(),
font_size: 12.0,
page: 1,
is_bold: false,
Expand All @@ -2516,6 +2522,7 @@ mod tests {
width: 42.0,
height: 12.0,
font: "C2_0".into(),
font_tag: String::new(),
font_size: 12.0,
page: 1,
is_bold: false,
Expand All @@ -2532,6 +2539,7 @@ mod tests {
width: 35.0,
height: 12.0,
font: "C2_0".into(),
font_tag: String::new(),
font_size: 12.0,
page: 1,
is_bold: false,
Expand Down Expand Up @@ -2559,6 +2567,7 @@ mod tests {
width: 8.0,
height: 12.0,
font: "F1".into(),
font_tag: String::new(),
font_size: 12.0,
page: 1,
is_bold: false,
Expand All @@ -2575,6 +2584,7 @@ mod tests {
width: 8.0,
height: 12.0,
font: "F1".into(),
font_tag: String::new(),
font_size: 12.0,
page: 1,
is_bold: false,
Expand All @@ -2591,6 +2601,7 @@ mod tests {
width: 8.0,
height: 12.0,
font: "F1".into(),
font_tag: String::new(),
font_size: 12.0,
page: 1,
is_bold: false,
Expand Down Expand Up @@ -2620,6 +2631,7 @@ mod tests {
width,
height: 13.3,
font: "F4".into(),
font_tag: String::new(),
font_size: 13.3,
page: 1,
is_bold: true,
Expand Down Expand Up @@ -2656,6 +2668,7 @@ mod tests {
width,
height: 13.3,
font: "F5".into(),
font_tag: String::new(),
font_size: 13.3,
page: 1,
is_bold: false,
Expand Down Expand Up @@ -2693,6 +2706,7 @@ mod tests {
width: 24.0,
height: 12.0,
font: "C2_0".into(),
font_tag: String::new(),
font_size: 12.0,
page: 1,
is_bold: false,
Expand All @@ -2709,6 +2723,7 @@ mod tests {
width: 32.0,
height: 12.0,
font: "C2_0".into(),
font_tag: String::new(),
font_size: 12.0,
page: 1,
is_bold: false,
Expand All @@ -2725,6 +2740,7 @@ mod tests {
width: 32.0,
height: 12.0,
font: "C2_0".into(),
font_tag: String::new(),
font_size: 12.0,
page: 1,
is_bold: false,
Expand All @@ -2749,6 +2765,7 @@ mod tests {
width,
height: 12.0,
font: "F1".into(),
font_tag: String::new(),
font_size: 12.0,
page: 1,
is_bold: false,
Expand Down Expand Up @@ -2888,6 +2905,7 @@ mod tests {
width: 10.0,
height: 12.0,
font: "F1".into(),
font_tag: String::new(),
font_size: 12.0,
page: 1,
is_bold: false,
Expand All @@ -2904,6 +2922,7 @@ mod tests {
width: 10.0,
height: 12.0,
font: "F1".into(),
font_tag: String::new(),
font_size: 12.0,
page: 1,
is_bold: false,
Expand All @@ -2930,6 +2949,7 @@ mod tests {
width: 50.0,
height: 12.0,
font: "F1".into(),
font_tag: String::new(),
font_size: 12.0,
page: 1,
is_bold: false,
Expand All @@ -2946,6 +2966,7 @@ mod tests {
width: 50.0,
height: 12.0,
font: "F1".into(),
font_tag: String::new(),
font_size: 12.0,
page: 1,
is_bold: false,
Expand Down Expand Up @@ -2988,6 +3009,7 @@ mod tests {
width: 100.0,
height: 12.0,
font: "F1".into(),
font_tag: String::new(),
font_size: 12.0,
page,
is_bold: false,
Expand Down Expand Up @@ -3034,6 +3056,7 @@ mod tests {
width: 100.0,
height: 12.0,
font: "F1".into(),
font_tag: String::new(),
font_size: 12.0,
page,
is_bold: false,
Expand Down Expand Up @@ -3080,6 +3103,7 @@ mod tests {
width: 100.0,
height: 12.0,
font: "F1".into(),
font_tag: String::new(),
font_size: 12.0,
page,
is_bold: false,
Expand Down Expand Up @@ -3119,6 +3143,7 @@ mod tests {
width,
height: font_size,
font: "F1".into(),
font_tag: String::new(),
font_size,
page: 1,
is_bold: false,
Expand Down
1 change: 1 addition & 0 deletions src/extractor/reading_order.rs
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,7 @@ mod tests {
width,
height: 11.0,
font: "F1".into(),
font_tag: String::new(),
font_size: 11.0,
page: 1,
is_bold: false,
Expand Down
1 change: 1 addition & 0 deletions src/extractor/underline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -704,6 +704,7 @@ mod tests {
width,
height: font_size,
font: "F1".to_string(),
font_tag: String::new(),
font_size,
page: 1,
is_bold: false,
Expand Down
14 changes: 14 additions & 0 deletions src/extractor/xobjects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,7 @@ fn extract_form_xobject_text_inner(
width,
height,
font: String::new(),
font_tag: String::new(),
font_size: 0.0,
page: page_num,
is_bold: false,
Expand Down Expand Up @@ -682,6 +683,7 @@ fn extract_form_xobject_text_inner(
base_font,
)
.to_string(),
font_tag: current_font.clone(),
font_size: rendered_size,
page: page_num,
is_bold: is_bold_font(base_font) || desc_bold,
Expand Down Expand Up @@ -885,6 +887,7 @@ fn extract_form_xobject_text_inner(
base_font,
)
.to_string(),
font_tag: current_font.clone(),
font_size: rendered_size,
page: page_num,
is_bold: is_bold_font(base_font) || desc_bold,
Expand Down Expand Up @@ -1067,6 +1070,17 @@ mod tests {
assert_eq!(items[0].text, "X");
}

#[test]
fn form_items_carry_family_name_and_resource_tag() {
// Parity with content_stream.rs: `font` is the /BaseFont family
// name, `font_tag` the raw resource tag, in both parsers.
let (doc, root) = form_dag(1, 2);
let items = extract_form(&doc, root, &mut FormWalkBudget::new());
assert_eq!(items.len(), 1);
assert_eq!(items[0].font, "Helvetica");
assert_eq!(items[0].font_tag, "F1");
}

#[test]
fn acyclic_form_dag_within_budget_keeps_all_leaves() {
// 4 sibling invocations across 4 nested levels → 4^4 leaf drawings.
Expand Down
Loading