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-288/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-288/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-288/gt.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
42 changes: 42 additions & 0 deletions crates/office2pdf/src/parser/docx_style_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,3 +300,45 @@ fn test_direct_jc_center_applied_to_paragraph() {

assert_eq!(para.style.alignment, Some(Alignment::Center));
}

#[test]
fn test_default_paragraph_style_applies_without_pstyle() {
// w:default="1" marks the style that paragraphs without an explicit
// pStyle inherit (issue #288): its spacing must survive the cascade.
let mut normal = docx_rs::Style::new("Normal", docx_rs::StyleType::Paragraph)
.name("Normal")
.size(24)
.line_spacing(
docx_rs::LineSpacing::new()
.after(160)
.line(360)
.line_rule(docx_rs::LineSpacingType::Auto),
);
normal.default = true;

let data = build_docx_bytes_with_styles(
vec![docx_rs::Paragraph::new().add_run(docx_rs::Run::new().add_text("본문 문단"))],
vec![normal],
);

let parser = DocxParser;
let (doc, _warnings) = parser.parse(&data, &ConvertOptions::default()).unwrap();

let Page::Flow(flow) = &doc.pages[0] else {
panic!("expected flow page");
};
let Block::Paragraph(paragraph) = flow
.content
.iter()
.find(|b| matches!(b, Block::Paragraph(_)))
.expect("paragraph")
else {
unreachable!()
};
assert_eq!(
paragraph.style.space_after,
Some(8.0),
"default style spacing (160 twips = 8pt) must apply to pStyle-less paragraphs"
);
assert_eq!(paragraph.runs[0].style.font_size, Some(12.0));
}
21 changes: 21 additions & 0 deletions crates/office2pdf/src/parser/docx_styles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,27 @@ pub(super) fn build_style_map(styles: &docx_rs::Styles) -> StyleMap {
}
}

// Paragraphs without an explicit pStyle inherit the default paragraph
// style (w:default="1", normally "Normal"), not just the bare document
// defaults — fold it into the synthetic doc-default entry so its spacing,
// line spacing, and text properties survive the cascade (issue #288).
let default_paragraph_style_id: Option<String> = styles
.styles
.iter()
.find(|style| style.default && style.style_type == docx_rs::StyleType::Paragraph)
.map(|style| style.style_id.clone());
if let Some(style_id) = default_paragraph_style_id
&& let Some(default_style) = map.get(&style_id)
{
let merged = ResolvedStyle {
text: default_style.text.clone(),
paragraph: default_style.paragraph.clone(),
paragraph_tab_overrides: default_style.paragraph_tab_overrides.clone(),
heading_level: None,
};
map.insert(DOC_DEFAULT_STYLE_ID.to_string(), merged);
}

map
}

Expand Down
Loading