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
Binary file added assets/bugfixes/issue-204/after.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/bugfixes/issue-204/before.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/bugfixes/issue-204/gt.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
50 changes: 49 additions & 1 deletion crates/office2pdf/src/ir/elements.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,64 @@
use std::collections::BTreeMap;

use super::style::{Alignment, Color, ParagraphStyle, TextStyle};
use super::style::{Alignment, Color, ParagraphStyle, TabLeader, TextStyle};

/// Header or footer content for flow pages.
#[derive(Debug, Clone)]
pub struct HeaderFooter {
pub paragraphs: Vec<HeaderFooterParagraph>,
/// Distance in points from the page edge, as specified by the section page margins.
pub distance_from_edge: Option<f64>,
}

/// A paragraph within a header or footer.
#[derive(Debug, Clone)]
pub struct HeaderFooterParagraph {
pub style: ParagraphStyle,
pub elements: Vec<HFInline>,
pub border: Option<CellBorder>,
pub frame: Option<HeaderFooterFrame>,
}

/// Page- or margin-relative positioning for a header/footer paragraph frame.
#[derive(Debug, Clone, PartialEq)]
pub struct HeaderFooterFrame {
pub x: Option<f64>,
pub y: Option<f64>,
pub width: Option<f64>,
pub height: Option<f64>,
pub horizontal_anchor: FrameAnchor,
pub vertical_anchor: FrameAnchor,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum FrameAnchor {
Page,
Margin,
#[default]
Text,
}

/// A position-relative tab (`w:ptab`) inside header/footer content.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct PositionedTab {
pub alignment: PositionedTabAlignment,
pub relative_to: PositionedTabRelativeTo,
pub leader: TabLeader,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum PositionedTabAlignment {
Center,
#[default]
Left,
Right,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum PositionedTabRelativeTo {
Indent,
#[default]
Margin,
}

/// An inline element within a header or footer paragraph.
Expand All @@ -26,6 +72,8 @@ pub enum HFInline {
PageNumber,
/// Total page count field.
TotalPages,
/// Alignment tab positioned relative to the paragraph indent or page margin.
PositionedTab(PositionedTab),
}

/// Block-level content elements.
Expand Down
6 changes: 6 additions & 0 deletions crates/office2pdf/src/ir/elements_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ fn test_paragraph_with_runs() {
#[test]
fn test_header_footer_with_text() {
let hf = HeaderFooter {
distance_from_edge: None,
paragraphs: vec![HeaderFooterParagraph {
style: ParagraphStyle::default(),
elements: vec![HFInline::Run(Run {
Expand All @@ -195,6 +196,8 @@ fn test_header_footer_with_text() {
href: None,
footnote: None,
})],
border: None,
frame: None,
}],
};
assert_eq!(hf.paragraphs.len(), 1);
Expand All @@ -208,6 +211,7 @@ fn test_header_footer_with_text() {
#[test]
fn test_header_footer_with_page_number() {
let hf = HeaderFooter {
distance_from_edge: None,
paragraphs: vec![HeaderFooterParagraph {
style: ParagraphStyle::default(),
elements: vec![
Expand All @@ -219,6 +223,8 @@ fn test_header_footer_with_page_number() {
}),
HFInline::PageNumber,
],
border: None,
frame: None,
}],
};
assert_eq!(hf.paragraphs[0].elements.len(), 2);
Expand Down
6 changes: 6 additions & 0 deletions crates/office2pdf/src/lib_render_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,7 @@ fn test_render_document_with_header() {
}],
})],
header: Some(HeaderFooter {
distance_from_edge: None,
paragraphs: vec![HeaderFooterParagraph {
style: ParagraphStyle::default(),
elements: vec![HFInline::Run(Run {
Expand All @@ -542,6 +543,8 @@ fn test_render_document_with_header() {
href: None,
footnote: None,
})],
border: None,
frame: None,
}],
}),
footer: None,
Expand Down Expand Up @@ -572,6 +575,7 @@ fn test_render_document_with_page_number_footer() {
})],
header: None,
footer: Some(HeaderFooter {
distance_from_edge: None,
paragraphs: vec![HeaderFooterParagraph {
style: ParagraphStyle::default(),
elements: vec![
Expand All @@ -583,6 +587,8 @@ fn test_render_document_with_page_number_footer() {
}),
HFInline::PageNumber,
],
border: None,
frame: None,
}],
}),
columns: None,
Expand Down
134 changes: 127 additions & 7 deletions crates/office2pdf/src/parser/docx_sections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ use std::io::{Cursor, Read, Seek};

use crate::error::ConvertWarning;
use crate::ir::{
Block, ColumnLayout, FlowPage, HFInline, HeaderFooter, HeaderFooterParagraph, Margins,
PageSize, Run, TextDirection, TextStyle,
Block, BorderLineStyle, BorderSide, CellBorder, Color, ColumnLayout, FlowPage, FrameAnchor,
HFInline, HeaderFooter, HeaderFooterFrame, HeaderFooterParagraph, Margins, PageSize,
PositionedTab, PositionedTabAlignment, PositionedTabRelativeTo, Run, TabLeader, TextDirection,
TextStyle,
};

use super::contexts::WrapContext;
Expand All @@ -15,6 +17,7 @@ use super::{
merge_paragraph_style, read_zip_text,
};
use crate::parser::units::twips_to_pt;
use crate::parser::xml_util::parse_hex_color;

/// Parsed header/footer assets addressed by relationship ID.
#[derive(Default)]
Expand Down Expand Up @@ -418,12 +421,21 @@ pub(super) fn build_flow_page_from_section(
});
}

let mut header = extract_docx_header(section_prop, header_footer_assets);
if let Some(header) = &mut header {
header.distance_from_edge = Some(twips_to_pt(section_prop.page_margin.header));
}
let mut footer = extract_docx_footer(section_prop, header_footer_assets);
if let Some(footer) = &mut footer {
footer.distance_from_edge = Some(twips_to_pt(section_prop.page_margin.footer));
}

FlowPage {
size,
margins,
content,
header: extract_docx_header(section_prop, header_footer_assets),
footer: extract_docx_footer(section_prop, header_footer_assets),
header,
footer,
columns: column_layout
.or_else(|| extract_column_layout_from_section_property(section_prop)),
}
Expand Down Expand Up @@ -454,7 +466,10 @@ fn convert_docx_header(
if paragraphs.is_empty() {
return None;
}
Some(HeaderFooter { paragraphs })
Some(HeaderFooter {
paragraphs,
distance_from_edge: None,
})
}

fn convert_docx_footer(
Expand Down Expand Up @@ -483,7 +498,10 @@ fn convert_docx_footer(
if paragraphs.is_empty() {
return None;
}
Some(HeaderFooter { paragraphs })
Some(HeaderFooter {
paragraphs,
distance_from_edge: None,
})
}

/// Extract the header for a section, preferring the default variant and falling back to
Expand Down Expand Up @@ -634,7 +652,87 @@ fn convert_hf_paragraph(
}
}

HeaderFooterParagraph { style, elements }
HeaderFooterParagraph {
style,
elements,
border: extract_hf_paragraph_border(&paragraph.property),
frame: extract_hf_frame(&paragraph.property),
}
}

fn extract_hf_paragraph_border(property: &docx_rs::ParagraphProperty) -> Option<CellBorder> {
let borders = serde_json::to_value(property.borders.as_ref()?).ok()?;
let extract_side = |key: &str| -> Option<BorderSide> {
let side = borders.get(key)?.as_object()?;
let border_type = side
.get("borderType")
.or_else(|| side.get("val"))?
.as_str()?;
if matches!(border_type, "none" | "nil") {
return None;
}
let width = side.get("size")?.as_f64()? / 8.0;
let color = side
.get("color")
.and_then(serde_json::Value::as_str)
.filter(|value| *value != "auto")
.and_then(parse_hex_color)
.unwrap_or_else(Color::black);
let style = match border_type {
"dashed" | "dashSmallGap" => BorderLineStyle::Dashed,
"dotted" => BorderLineStyle::Dotted,
"dashDotStroked" | "dotDash" => BorderLineStyle::DashDot,
"dotDotDash" => BorderLineStyle::DashDotDot,
"double"
| "thinThickSmallGap"
| "thickThinSmallGap"
| "thinThickMediumGap"
| "thickThinMediumGap"
| "thinThickLargeGap"
| "thickThinLargeGap"
| "thinThickThinSmallGap"
| "thinThickThinMediumGap"
| "thinThickThinLargeGap"
| "triple" => BorderLineStyle::Double,
_ => BorderLineStyle::Solid,
};
Some(BorderSide {
width,
color,
style,
})
};
let border = CellBorder {
top: extract_side("top"),
bottom: extract_side("bottom"),
left: extract_side("left"),
right: extract_side("right"),
};
(border.top.is_some()
|| border.bottom.is_some()
|| border.left.is_some()
|| border.right.is_some())
.then_some(border)
}

fn extract_hf_frame(property: &docx_rs::ParagraphProperty) -> Option<HeaderFooterFrame> {
let frame = property.frame_property.as_ref()?;
Some(HeaderFooterFrame {
x: frame.x.map(twips_to_pt),
y: frame.y.map(twips_to_pt),
width: frame.w.map(|value| twips_to_pt(value as i32)),
height: frame.h.map(|value| twips_to_pt(value as i32)),
horizontal_anchor: frame_anchor(frame.h_anchor.as_deref()),
vertical_anchor: frame_anchor(frame.v_anchor.as_deref()),
})
}

fn frame_anchor(value: Option<&str>) -> FrameAnchor {
match value {
Some("page") => FrameAnchor::Page,
Some("margin") => FrameAnchor::Margin,
_ => FrameAnchor::Text,
}
}

fn append_simple_fields(
Expand Down Expand Up @@ -728,6 +826,28 @@ fn extract_hf_run_elements(
footnote: None,
}));
}
docx_rs::RunChild::PTab(tab) if !in_field => {
let alignment = match tab.alignment {
docx_rs::PositionalTabAlignmentType::Center => PositionedTabAlignment::Center,
docx_rs::PositionalTabAlignmentType::Right => PositionedTabAlignment::Right,
docx_rs::PositionalTabAlignmentType::Left => PositionedTabAlignment::Left,
};
let relative_to = match tab.relative_to {
docx_rs::PositionalTabRelativeTo::Indent => PositionedTabRelativeTo::Indent,
docx_rs::PositionalTabRelativeTo::Margin => PositionedTabRelativeTo::Margin,
};
let leader = match tab.leader {
docx_rs::TabLeaderType::Dot => TabLeader::Dot,
docx_rs::TabLeaderType::Hyphen => TabLeader::Hyphen,
docx_rs::TabLeaderType::Underscore => TabLeader::Underscore,
_ => TabLeader::None,
};
elements.push(HFInline::PositionedTab(PositionedTab {
alignment,
relative_to,
leader,
}));
}
_ => {}
}
}
Expand Down
7 changes: 6 additions & 1 deletion crates/office2pdf/src/parser/xlsx_hf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,19 @@ pub(super) fn parse_hf_format_string(format_str: &str) -> Option<HeaderFooter> {
..ParagraphStyle::default()
},
elements,
border: None,
frame: None,
});
}
}

if paragraphs.is_empty() {
None
} else {
Some(HeaderFooter { paragraphs })
Some(HeaderFooter {
paragraphs,
distance_from_edge: None,
})
}
}

Expand Down
5 changes: 4 additions & 1 deletion crates/office2pdf/src/render/font_subst.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,10 @@ fn visit_header_footer_fonts(
.map(str::trim)
.filter(|f| !f.is_empty())
.is_none_or(&mut *visitor),
HFInline::Image(_) | HFInline::PageNumber | HFInline::TotalPages => true,
HFInline::Image(_)
| HFInline::PageNumber
| HFInline::TotalPages
| HFInline::PositionedTab(_) => true,
})
})
}
Expand Down
Loading
Loading