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
51 changes: 31 additions & 20 deletions crates/office2pdf/src/parser/xlsx_drawing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -746,6 +746,35 @@ fn resolved_or_legacy(
})
}

/// Apply an `<a:rPr>` element's size and emphasis attributes to a run
/// style. Called from both the `Start` and `Empty` arms of the drawing
/// reader: Excel writes shape-label run properties as a self-closing
/// element, which quick-xml reports as `Empty`, and reading them on `Start`
/// alone dropped every attribute (issue #466).
fn apply_run_properties(
style: &mut crate::ir::TextStyle,
element: &quick_xml::events::BytesStart<'_>,
) {
for attr in element.attributes().flatten() {
match attr.key.local_name().as_ref() {
b"sz" => {
if let Ok(value) = attr.unescape_value()
&& let Ok(hundredths_pt) = value.parse::<f64>()
{
style.font_size = Some(hundredths_pt / 100.0);
}
}
b"b" if attr.unescape_value().ok().as_deref() == Some("1") => {
style.bold = Some(true);
}
b"i" if attr.unescape_value().ok().as_deref() == Some("1") => {
style.italic = Some(true);
}
_ => {}
}
}
}

/// Parse `<xdr:sp>` text boxes from a worksheet drawing, resolving scheme
/// colors against the workbook theme palette and run typefaces against its
/// font scheme.
Expand Down Expand Up @@ -846,26 +875,7 @@ pub(super) fn parse_drawing_text_boxes(
in_run = true;
current_style = TextStyle::default();
}
b"rPr" if in_run => {
for attr in e.attributes().flatten() {
match attr.key.local_name().as_ref() {
b"sz" => {
if let Ok(v) = attr.unescape_value()
&& let Ok(sz) = v.parse::<f64>()
{
current_style.font_size = Some(sz / 100.0);
}
}
b"b" if attr.unescape_value().ok().as_deref() == Some("1") => {
current_style.bold = Some(true);
}
b"i" if attr.unescape_value().ok().as_deref() == Some("1") => {
current_style.italic = Some(true);
}
_ => {}
}
}
}
b"rPr" if in_run => apply_run_properties(&mut current_style, e),
b"t" if in_run => in_text = true,
b"srgbClr" | b"schemeClr" => {
let parsed =
Expand Down Expand Up @@ -905,6 +915,7 @@ pub(super) fn parse_drawing_text_boxes(
current_style.font_family = Some(family);
}
}
b"rPr" if in_run => apply_run_properties(&mut current_style, e),
b"pPr" if current_para.is_some() => {
for attr in e.attributes().flatten() {
if attr.key.local_name().as_ref() == b"algn"
Expand Down
44 changes: 44 additions & 0 deletions crates/office2pdf/src/parser/xlsx_drawing_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,3 +261,47 @@ fn theme_font_scheme_ignores_empty_typefaces() {
let fonts = crate::parser::drawingml::parse_theme_font_scheme(theme_xml);
assert_eq!(fonts, ThemeFontScheme::default());
}

/// `(font_size, bold, italic)` of the single run the fixture produces.
fn run_emphasis(run_properties: &str) -> (Option<f64>, Option<bool>, Option<bool>) {
let boxes = parse_drawing_text_boxes(
&drawing_with_run_properties(run_properties),
&HashMap::new(),
&ThemeFontScheme::default(),
);
assert_eq!(boxes.len(), 1, "fixture should yield one text box");
let style = &boxes[0].paragraphs[0].runs[0].style;
(style.font_size, style.bold, style.italic)
}

#[test]
fn self_closing_run_properties_still_carry_size_and_emphasis() {
// Excel writes shape-label run properties as a self-closing element,
// which quick-xml reports as `Event::Empty`; reading them only on
// `Event::Start` dropped every attribute (issue #466).
assert_eq!(
run_emphasis(r#"<a:rPr lang="en-US" sz="1400" b="1" i="1"/>"#),
(Some(14.0), Some(true), Some(true))
);
}

#[test]
fn run_properties_with_children_keep_working() {
// Triangulation: the same attributes on an element that has children
// must resolve identically, so the fix cannot special-case one spelling.
assert_eq!(
run_emphasis(
r#"<a:rPr lang="en-US" sz="1400" b="1" i="1"><a:solidFill><a:srgbClr val="FF0000"/></a:solidFill></a:rPr>"#
),
(Some(14.0), Some(true), Some(true))
);
}

#[test]
fn run_properties_omit_emphasis_when_disabled() {
// `b="0"`/`i="0"` mean "not bold"/"not italic"; they must not set the flag.
assert_eq!(
run_emphasis(r#"<a:rPr lang="en-US" sz="900" b="0" i="0"/>"#),
(Some(9.0), None, None)
);
}
Loading