From 852baac9a014ec70a61ed83303d6a7719e9739a1 Mon Sep 17 00:00:00 2001 From: KnifeLemon Date: Thu, 19 Nov 2026 14:17:25 +0900 Subject: [PATCH 1/8] Word: lay floating tables out as floating objects (#1194) A Word table with w:tblpPr floats: Word places it from the top of the paragraph after it (vertAnchor text) or against the margins or the page, and the text after it goes around it. Filee laid it out in the flow, so the report's figure captions (floating one-cell tables) took their own room and the empty paragraphs after them went under them instead of beside them. The DOCX reader reads tblpPr into HTable.Float (an HAnchor: horzAnchor / vertAnchor, tblpX / tblpY or tblpXSpec / tblpYSpec) and FloatDistance (the From-text distances); only the layout engine uses them. The page composer places such a table like a picture wrapped square: anchored to the next paragraph (laid out ahead, so it keeps its list number), never above the text before it, moved to the next column with that paragraph when it would run past the bottom, and drawn with the page's floating objects. A table taller than a column stays in the flow. Co-Authored-By: Claude Opus 5.5 --- .../Hwp/Hwpx/Docx/DocxReader.Tables.cs | 44 ++++++++++++++ src/Filee.Engines/Hwp/Hwpx/HwpxModel.cs | 9 +++ src/Filee.Engines/Typesetting/PageComposer.cs | 60 ++++++++++++++++++- .../Filee.Engines.Tests/ReportedCasesTests.cs | 43 +++++++++++++ 4 files changed, 154 insertions(+), 2 deletions(-) diff --git a/src/Filee.Engines/Hwp/Hwpx/Docx/DocxReader.Tables.cs b/src/Filee.Engines/Hwp/Hwpx/Docx/DocxReader.Tables.cs index 7ec47a69e..9d2415b40 100644 --- a/src/Filee.Engines/Hwp/Hwpx/Docx/DocxReader.Tables.cs +++ b/src/Filee.Engines/Hwp/Hwpx/Docx/DocxReader.Tables.cs @@ -43,6 +43,9 @@ private HTable ReadTable(XElement tbl) grid), }; + if (tblPr?.Element(W + "tblpPr") is { } position) + Float(table, position); + // Borders and cell margins: table style chain, then the table's own properties. var borders = new Dictionary(); HInsets? margins = null; @@ -160,6 +163,47 @@ [.. styleChain.Select(s => s.PPr).Concat(cellConditions.Select(c => c.Element(W return table; } + /// + /// A floating table's place (w:tblpPr): against the paper (page), the margins (margin) or, down, the paragraph after + /// the table and, across, the column (text); at tblpX / tblpY from their left and top, or aligned by tblpXSpec / + /// tblpYSpec. Text goes around it on both sides, the From-text distances away. + /// + private static void Float(HTable table, XElement position) + { + static string Area(string? anchor, string text) => anchor switch + { + "page" => "PAPER", + "margin" => "PAGE", + _ => text, + }; + static string Align(string? spec) => spec switch + { + "center" => "CENTER", + "right" => "RIGHT", + "inside" => "INSIDE", + "outside" => "OUTSIDE", + _ => "LEFT", + }; + // Word's defaults where the attributes are left out: across from the column, down from the margin. + var across = Area(Attr(position, "horzAnchor"), "COLUMN"); + var down = Area(Attr(position, "vertAnchor") ?? "margin", "PARA"); + var xSpec = Attr(position, "tblpXSpec"); + var ySpec = Attr(position, "tblpYSpec"); + table.Float = new HAnchor( + across, Align(xSpec), xSpec is null ? HwpxUnits.FromTwips(Twips(position, "tblpX") ?? 0) : 0, + down, ySpec switch + { + "center" => "CENTER", + "bottom" => "BOTTOM", + "inside" => "INSIDE", + "outside" => "OUTSIDE", + _ => "TOP", + }, ySpec is null or "inline" ? HwpxUnits.FromTwips(Twips(position, "tblpY") ?? 0) : 0, + "SQUARE", true); + int Distance(string name) => HwpxUnits.FromTwips(Math.Max(0, Twips(position, name) ?? 0)); + table.FloatDistance = new HInsets(Distance("leftFromText"), Distance("rightFromText"), Distance("topFromText"), Distance("bottomFromText")); + } + /// /// The conditional formatting of the table style (w:tblStylePr) that reaches each cell, given its row, grid column /// and span: banded rows and columns, first and last column, header and total row, then the corner cells, each diff --git a/src/Filee.Engines/Hwp/Hwpx/HwpxModel.cs b/src/Filee.Engines/Hwp/Hwpx/HwpxModel.cs index 0f5ddb7a4..32a1ff001 100644 --- a/src/Filee.Engines/Hwp/Hwpx/HwpxModel.cs +++ b/src/Filee.Engines/Hwp/Hwpx/HwpxModel.cs @@ -448,6 +448,15 @@ internal sealed class HTable : HBlock /// public HTableSizing? Sizing { get; set; } + /// + /// A Word floating table (w:tblpPr): placed by the anchor (PARA = the paragraph after the table), text going around + /// it, kept away from that text; null = in the flow. Only the layout engine uses it. + /// + public HAnchor? Float { get; set; } + + /// The distance a floating table keeps from the text around it (w:leftFromText ...). + public HInsets FloatDistance { get; set; } + public List Caption { get; } = []; } diff --git a/src/Filee.Engines/Typesetting/PageComposer.cs b/src/Filee.Engines/Typesetting/PageComposer.cs index 07e07c439..1f6265018 100644 --- a/src/Filee.Engines/Typesetting/PageComposer.cs +++ b/src/Filee.Engines/Typesetting/PageComposer.cs @@ -28,7 +28,8 @@ // the reader keeps in a text box placed for rhwp, goes with its equation instead (ParagraphLayout.Equations.cs). // // - Tables (TableLayout): rows bound by spanning cells move to a page together, header rows repeat, and a table splits -// by its page break mode. +// by its page break mode. A Word floating table (HTable.Float) is placed as a floating object instead, against the +// paragraph after it or the page, and the text after it goes around it (see Float). // - Columns: a section's newspaper columns, with their gap and separator line; what doesn't fit in a column goes on in // the next, and after the last on the next page. Columns change without a new page where the model says (Word's // continuous section break, 한글's 다단 설정 나누기); the columns before are balanced then, so they end at about the @@ -488,6 +489,8 @@ private void Body(List blocks) { var tableBox = next as TableBox ?? Tables.Layout(table, ColumnWidth); next = null; + if (holder is null && table.Float is { } anchor && Float(table, anchor, tableBox, blocks, i, ref next)) + continue; PlaceTable(table, tableBox, holder, holder is null ? TableLayout.Follower(blocks, i) : null); holder = null; continue; @@ -1096,6 +1099,56 @@ private List Anchor(HParagraph paragraph, float top, FuncA Word floating table laid out, placed as a floating object. + private sealed class FloatingTable(TableBox box) : HObject + { + public TableBox Box { get; } = box; + } + + /// + /// Places a Word floating table where its anchor puts it: down from the top of the paragraph after it (laid out + /// ahead into , so it keeps its list number), never above the text before it, or against + /// the page. Text after it goes around it (as around a picture wrapped square). One that moves with the text and + /// would run past the column's bottom goes to the next column with its paragraph. False for a table taller than a + /// column, which stays in the flow (split by its rows). + /// + private bool Float(HTable table, HAnchor anchor, TableBox box, List blocks, int index, ref object? next) + { + var (width, height) = (box.Width, box.Height); + if (height > ColumnHeight + Epsilon || width <= 0) + return false; + var withText = anchor.VerticalRelativeTo == "PARA"; + HParagraph? paragraph = null; + ParagraphBox? following = null; + if (index + 1 < blocks.Count && blocks[index + 1] is HParagraph after && !TableLayout.HoldsTable(blocks, index + 1)) + { + paragraph = composer.Resolve(after, index + 2 < blocks.Count ? blocks[index + 2] : null); + following = Layout(paragraph, ColumnWidth); + next = following; + } + SKRect area; + while (true) + { + var top = _y + (_columnEmpty || following is null ? 0 : following.SpaceBefore); + area = FloatingObjects.Place(anchor, width, height, Frame(top, paragraph)); + if (!withText) + break; + var y = Math.Max(_y, top + anchor.VerticalOffset / 100f); + area = new SKRect(area.Left, y, area.Right, y + height); + if (_columnEmpty || area.Bottom <= Bottom + Epsilon) + break; + NextColumn(); + _inStep = false; + } + Current.Objects.Add(FloatingObjects.Placed(new FloatingTable(box) { OuterMargin = table.FloatDistance }, anchor, area, _objects++)); + for (; _numberedChecked < _numbered.Count; _numberedChecked++) + { + if (!_notesPlaced.Contains(_numbered[_numberedChecked])) + AddNote(_numbered[_numberedChecked]); + } + return true; + } + /// Draws a page's floating objects, behind or in front of its text, lower z-order first. private void DrawObjects(Page page) { @@ -1104,7 +1157,10 @@ private void DrawObjects(Page page) foreach (var item in page.Objects.OrderBy(o => o.Order).ThenBy(o => o.Sequence)) { var layer = new PageContent(page.Content.Width, page.Content.Height); - FloatingObjects.Draw(layer, item.Object, item.Area, (blocks, width) => Tables.Stack(blocks, width)); + if (item.Object is FloatingTable table) + TableLayout.Draw(layer, table.Box.Rows, item.Area.Left, item.Area.Top); + else + FloatingObjects.Draw(layer, item.Object, item.Area, (blocks, width) => Tables.Stack(blocks, width)); page.Content.Add(layer, item.Behind); } } diff --git a/tests/Filee.Engines.Tests/ReportedCasesTests.cs b/tests/Filee.Engines.Tests/ReportedCasesTests.cs index 0a0eee6c7..a0da9bc9f 100644 --- a/tests/Filee.Engines.Tests/ReportedCasesTests.cs +++ b/tests/Filee.Engines.Tests/ReportedCasesTests.cs @@ -673,6 +673,49 @@ public async Task Issue1313_a_frame_sits_where_its_page_position_puts_it() Assert.True(check.Y > code[0].Y && check.Y < 720 + 98, $"second line at {check.Y}"); } + // ───────────────────────── Word: floating tables and paragraph spacing (#1194, #1152) ───────────────────────── + + /// A one-cell table (Table Grid) 2 inches wide holding , with in its properties. + private static string OneCellTable(string text, string position = "") => + $"{position}" + + $"{P(R(text))}"; + + private static SKPoint Where(PageContent page, string text) => Runs(page).First(r => r.Text.Contains(text, StringComparison.Ordinal)).Positions[0]; + + [Fact] + public async Task Issue1194_text_after_a_floating_table_goes_around_it() + { + // The report's figure captions are floating tables (w:tblpPr), placed from the top of the paragraph after them; + // the empty paragraphs after them sat beside the caption in Word and under it in Filee, a page longer in all. + const string Right = ""; + var floating = Single(await DocxAsync(Docx(P(R("Before")) + OneCellTable("Cell", Right) + P(R("Beside")) + P(R("Next"))))); + var flowing = Single(await DocxAsync(Docx(P(R("Before")) + OneCellTable("Cell") + P(R("Beside"))))); + + var (before, cell, beside, next) = (Where(floating, "Before"), Where(floating, "Cell"), Where(floating, "Beside"), Where(floating, "Next")); + // The table at the right margin, 10 pt below the top of the paragraph after it. + Assert.Equal(RightMargin - 144 + 5.4f, cell.X, 1f); + var pitch = LineHeight(RepositoryFonts.Latin, 10); + Assert.Equal(before.Y + pitch + 10, cell.Y, 1f); + // The paragraphs after it on their own lines at the left margin, beside it. + Assert.Equal(LeftMargin, beside.X, 0.5f); + Assert.Equal(before.Y + pitch, beside.Y, 0.5f); + Assert.Equal(beside.Y + pitch, next.Y, 0.5f); + // In the flow the paragraph goes under the table. + Assert.True(Where(flowing, "Beside").Y > Where(flowing, "Cell").Y + pitch - 0.5f); + } + + [Fact] + public async Task Issue1194_a_table_placed_on_the_page_leaves_the_text_where_it_is() + { + // A floating table 5 inches down the page (vertAnchor page) takes no room in the text above it. + const string Page = ""; + var page = Single(await DocxAsync(Docx(P(R("Before")) + OneCellTable("Cell", Page) + P(R("After"))))); + + var pitch = LineHeight(RepositoryFonts.Latin, 10); + Assert.Equal(Where(page, "Before").Y + pitch, Where(page, "After").Y, 0.5f); + Assert.InRange(Where(page, "Cell").Y, 360, 360 + pitch + 1); + } + // ───────────────────────── Word: right-to-left documents (#202, #216, #620) ───────────────────────── [Fact] From 46f000cec6c59f534e80207717f5bd50e132e4a7 Mon Sep 17 00:00:00 2001 From: KnifeLemon Date: Thu, 19 Nov 2026 14:17:32 +0900 Subject: [PATCH 2/8] Word: no 5 mm gap under a table (#1194) Tables read from other formats get the 5 mm the HWPX writer leaves under a table. Word puts the next paragraph right under its tables, so a Word table (one with Word's column sizing) now gets none; every table of the report took 14 pt more than in Word. Co-Authored-By: Claude Opus 5.5 --- src/Filee.Engines/Typesetting/PageComposer.cs | 5 +++-- tests/Filee.Engines.Tests/ReportedCasesTests.cs | 10 ++++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/Filee.Engines/Typesetting/PageComposer.cs b/src/Filee.Engines/Typesetting/PageComposer.cs index 1f6265018..6cd8bf80e 100644 --- a/src/Filee.Engines/Typesetting/PageComposer.cs +++ b/src/Filee.Engines/Typesetting/PageComposer.cs @@ -617,8 +617,9 @@ private void PlaceTable(HTable table, TableBox box, HParagraph? holder, HParagra // paragraph's text right under it, without the space before. _skipSpaceBefore = true; } - // A table of another format has the outer margin the HWPX writer gives it: 5 mm below unless it is tight. - var margin = table.OuterMargin ?? (composer._styles is not null && !table.Tight ? new HInsets(0, 0, 0, 1417) : default); + // A table of another format has the outer margin the HWPX writer gives it: 5 mm below unless it is tight. A + // Word table (sized by Word's rules) has none: Word puts the next paragraph right under it. + var margin = table.OuterMargin ?? (composer._styles is not null && !table.Tight && table.Sizing is null ? new HInsets(0, 0, 0, 1417) : default); _y += (table.VerticalOffset + margin.Top) / 100f; // Filee's rows are not 한글's unless the table is as high as 한글's (its line for it, else its stored size); // else the stored places hold again on 한글's next page. diff --git a/tests/Filee.Engines.Tests/ReportedCasesTests.cs b/tests/Filee.Engines.Tests/ReportedCasesTests.cs index a0da9bc9f..b4ffc7b1f 100644 --- a/tests/Filee.Engines.Tests/ReportedCasesTests.cs +++ b/tests/Filee.Engines.Tests/ReportedCasesTests.cs @@ -716,6 +716,16 @@ public async Task Issue1194_a_table_placed_on_the_page_leaves_the_text_where_it_ Assert.InRange(Where(page, "Cell").Y, 360, 360 + pitch + 1); } + [Fact] + public async Task Issue1194_a_word_table_has_no_space_below_it() + { + // The next paragraph sits right under a Word table; the 5 mm the HWPX writer leaves under one is not Word's. + var page = Single(await DocxAsync(Docx(OneCellTable("Cell") + P(R("After"))))); + + var pitch = LineHeight(RepositoryFonts.Latin, 10); + Assert.InRange(Where(page, "After").Y - Where(page, "Cell").Y, pitch, pitch + 1.5f); + } + // ───────────────────────── Word: right-to-left documents (#202, #216, #620) ───────────────────────── [Fact] From 408ad4d419ae6821200e2b27d16893c7cf7ae005 Mon Sep 17 00:00:00 2001 From: KnifeLemon Date: Thu, 19 Nov 2026 14:17:37 +0900 Subject: [PATCH 3/8] Word: line spacing does not stretch inline pictures (#1194) A line holding an inline picture in a paragraph with "multiple" line spacing was the picture's height times the multiple: a 330 pt figure on 1.1 lines left 33 pt of empty line, and the report's flowchart no longer fitted on its page. Word spaces only the line's text: a picture on a 1.5 line with 12 pt text is its height and half the text's line, as Word's own PDF of #356 has it. Equations are spaced like text, as before; lines without pictures are unchanged. Co-Authored-By: Claude Opus 5.5 --- .../Typesetting/ParagraphLayout.cs | 23 ++++++++++++++++--- .../Filee.Engines.Tests/ReportedCasesTests.cs | 17 ++++++++++++++ 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/src/Filee.Engines/Typesetting/ParagraphLayout.cs b/src/Filee.Engines/Typesetting/ParagraphLayout.cs index 1a748ef2b..6b6e2f69c 100644 --- a/src/Filee.Engines/Typesetting/ParagraphLayout.cs +++ b/src/Filee.Engines/Typesetting/ParagraphLayout.cs @@ -1169,7 +1169,7 @@ private LineBox Place(ParagraphText source, LineIndex ranges, int start, int end var (height, baseline, textHeight) = stored is var (segment, _) ? ((segment.VerticalSize + segment.Spacing) / 100f, segment.Baseline / 100f, segment.TextHeight / 100f) - : LineHeight(parts, settings, mark); + : LineHeight(parts, settings, mark, source); var bookmarks = ranges.Bookmarks(start, end, text.Length).Select(b => (lineLeft + offset, b.Name)).ToList(); return new LineBox { @@ -1234,12 +1234,14 @@ private void Decorate(Part part, float x0, float x1, List fills, Lis } /// The line's pitch, the distance from its top to the baseline and the height of its text. - private (float Height, float Baseline, float TextHeight) LineHeight(List parts, Settings settings, TextStyle mark) + private (float Height, float Baseline, float TextHeight) LineHeight(List parts, Settings settings, TextStyle mark, ParagraphText source) { var rule = settings.LineHeight; var fromFont = settings.FontLineHeight || rule.Rule == HLineHeightRule.Multiple; float objectAscent = 0, objectDescent = 0, textAscent = 0, textDescent = 0; - bool anyText = false, anyObject = false; + // Word's multiple spaces text and equations, not pictures: their extent apart. + float mathAscent = 0, mathDescent = 0; + bool anyText = false, anyObject = false, anyMath = false; foreach (var part in parts) { if (part.Item.Kind == ItemKind.Object) @@ -1248,6 +1250,12 @@ private void Decorate(Part part, float x0, float x1, List fills, Lis objectAscent = Math.Max(objectAscent, part.Size.Height); objectDescent = Math.Max(objectDescent, part.Size.Depth); anyObject = true; + if (source.Objects.GetValueOrDefault(part.Start) is HEquation) + { + mathAscent = Math.Max(mathAscent, part.Size.Height); + mathDescent = Math.Max(mathDescent, part.Size.Depth); + anyMath = true; + } continue; } if (part.Item.Kind != ItemKind.Text) @@ -1272,12 +1280,21 @@ private void Decorate(Part part, float x0, float x1, List fills, Lis // 한글's percentage adds its share of the text's size, not of an object's height: a line holding a 60 pt // picture at 160 % with 10 pt text is 66 pt high, as 한글 stores such lines. HLineHeightRule.Percent => textHeight + (fontAscent + fontDescent) * (rule.Value - 100) / 100f, + // Word's multiple spaces the line's text and equations; a picture taller than them adds its height unspaced: a + // picture on a 1.5 line with 12 pt text is its height and half the text's line, not 1.5 times the picture (as + // Word's own PDFs have such lines). A line of pictures only adds the paragraph mark's share, as 한글's does. + HLineHeightRule.Multiple when anyObject && (anyText || anyMath) => Spaced(Math.Max(textAscent, mathAscent), Math.Max(textDescent, mathDescent)), + HLineHeightRule.Multiple when anyObject => textHeight + (fontAscent + fontDescent) * (rule.Value - 240) / 240f, HLineHeightRule.Multiple => textHeight * rule.Value / 240f, HLineHeightRule.Exact => value, HLineHeightRule.AtLeast => Math.Max(value, textHeight), _ => textHeight + value, }; return (Math.Max(0, height), ascent, textHeight); + + // The text's extent spaced by the multiple, with what the pictures reach beyond it. + float Spaced(float textUp, float textDown) => + (textUp + textDown) * rule.Value / 240f + Math.Max(0, ascent - textUp) + Math.Max(0, descent - textDown); } /// Ascent and descent of a style: 한글's share of the size, or the font's own metrics. diff --git a/tests/Filee.Engines.Tests/ReportedCasesTests.cs b/tests/Filee.Engines.Tests/ReportedCasesTests.cs index b4ffc7b1f..80832ef44 100644 --- a/tests/Filee.Engines.Tests/ReportedCasesTests.cs +++ b/tests/Filee.Engines.Tests/ReportedCasesTests.cs @@ -726,6 +726,23 @@ public async Task Issue1194_a_word_table_has_no_space_below_it() Assert.InRange(Where(page, "After").Y - Where(page, "Cell").Y, pitch, pitch + 1.5f); } + [Fact] + public async Task Issue1194_line_spacing_does_not_stretch_a_picture() + { + // A 72 pt picture in a paragraph with 1.5 lines: Word adds half the paragraph's text line, not half the picture. + async Task After(string line) + { + var docx = Docx(""); + var id = docx.Image(HalfRedHalfBlue()); + docx.Paragraph(P(InlinePicture(id, 914400, 914400), $"") + P(R("After"))); + return Where(Single(await DocxAsync(docx)), "After").Y; + } + + var single = await After("240"); + var half = LineHeight(RepositoryFonts.Latin, 10) / 2; + Assert.Equal(single + half, await After("360"), 0.5f); + } + // ───────────────────────── Word: right-to-left documents (#202, #216, #620) ───────────────────────── [Fact] From dce1449e8842e4ff103666e066cc6b828c14e054 Mon Sep 17 00:00:00 2001 From: KnifeLemon Date: Thu, 19 Nov 2026 14:17:46 +0900 Subject: [PATCH 4/8] Word: the larger of two paragraphs' spaces goes between them (#1152) Word puts the larger of a paragraph's space after and the next one's space before between them, not their sum, unless the settings turn its HTML paragraph spacing off (w:doNotUseHTMLParagraphAutoSpacing). The headings on page 3 of #1152 have 6 pt after and 12 pt before: Word's PDF has 12 pt between them, Filee had 18. The reader now cuts the space after by the next paragraph's space before, as it drops contextual spacing; a paragraph that starts a page or column keeps it. Co-Authored-By: Claude Opus 5.5 --- src/Filee.Engines/Hwp/Hwpx/Docx/DocxReader.cs | 18 ++++++++++++++++++ .../Filee.Engines.Tests/ReportedCasesTests.cs | 17 +++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/src/Filee.Engines/Hwp/Hwpx/Docx/DocxReader.cs b/src/Filee.Engines/Hwp/Hwpx/Docx/DocxReader.cs index 78d498243..ceb7acbac 100644 --- a/src/Filee.Engines/Hwp/Hwpx/Docx/DocxReader.cs +++ b/src/Filee.Engines/Hwp/Hwpx/Docx/DocxReader.cs @@ -54,6 +54,9 @@ internal sealed partial class DocxReader private bool _bordersAroundHeader = true; private bool _bordersAroundFooter = true; + /// Neighbouring paragraphs keep the larger of their spaces between them, not both (see ). + private bool _collapseSpacing = true; + /// Width of each section's text column (HWPUNIT), for equations that don't fit a line. private readonly List _columnWidths = []; @@ -225,6 +228,7 @@ private HDocument ReadDocument() _evenAndOddHeaders = settings?.Element(W + "evenAndOddHeaders") is { } evenOdd && Val(evenOdd) is not ("0" or "false"); _bordersAroundHeader = !(settings?.Element(W + "bordersDoNotSurroundHeader") is { } noHeader && Val(noHeader) is not ("0" or "false")); _bordersAroundFooter = !(settings?.Element(W + "bordersDoNotSurroundFooter") is { } noFooter && Val(noFooter) is not ("0" or "false")); + _collapseSpacing = !(settings?.Element(W + "compat")?.Element(W + "doNotUseHTMLParagraphAutoSpacing") is { } sum && Val(sum) is not ("0" or "false")); _footnotesPart = RelatedPart(main, "/footnotes"); _endnotesPart = RelatedPart(main, "/endnotes"); foreach (var note in _footnotesPart?.Xml.Root?.Elements(W + "footnote") ?? []) @@ -523,6 +527,19 @@ private void ApplyContextualSpacing(HParagraph previous, HParagraph current, Par current.Format = current.Format with { Before = 0 }; } + /// + /// Word puts the larger of a paragraph's space below and the next one's space above between them, not both (its HTML + /// paragraph spacing, which only w:doNotUseHTMLParagraphAutoSpacing turns off): 6 pt below a heading and 12 pt above + /// the next is 12 pt, as Word's own PDFs have it (#1152). The space below the first gives way to the space above. + /// + private void CollapseSpacing(HParagraph previous, HParagraph current) + { + if (!_collapseSpacing || current.PageBreakBefore || current.ColumnBreakBefore) + return; + if (previous.Format.After is > 0 and var after && current.Format.Before is > 0 and var before) + previous.Format = previous.Format with { After = Math.Max(0, after - before) }; + } + /// /// Gives a paragraph with "multiple" line spacing the height its fonts have in Word: 한글's percentage is of the font /// size, and a single Calibri line is 122% of it where one in Times New Roman is 115%. @@ -973,6 +990,7 @@ private void Emit() { JoinBorders(previous, Current); _reader.ApplyContextualSpacing(previous, Current, _props); + _reader.CollapseSpacing(previous, Current); } _reader.RememberParagraphStyle(Current, _props); _output.Add(Current); diff --git a/tests/Filee.Engines.Tests/ReportedCasesTests.cs b/tests/Filee.Engines.Tests/ReportedCasesTests.cs index 80832ef44..f79b55b61 100644 --- a/tests/Filee.Engines.Tests/ReportedCasesTests.cs +++ b/tests/Filee.Engines.Tests/ReportedCasesTests.cs @@ -743,6 +743,23 @@ async Task After(string line) Assert.Equal(single + half, await After("360"), 0.5f); } + [Fact] + public async Task Issue1152_the_larger_of_the_spaces_between_two_paragraphs_counts() + { + // Word keeps the larger of a heading's 6 pt after and the next heading's 12 pt before between them (12 pt, not 18), + // unless its settings turn its HTML paragraph spacing off. + const string Spacing = ""; + var body = P(R("First"), Spacing) + P(R("Second"), Spacing); + var larger = Single(await DocxAsync(Docx(body))); + var both = Docx(body); + both.Settings = ""; + var summed = Single(await DocxAsync(both)); + + var single = LineHeight(RepositoryFonts.Latin, 10); + Assert.Equal(single + 12, Pitch(larger), 0.1f); + Assert.Equal(single + 18, Pitch(summed), 0.1f); + } + // ───────────────────────── Word: right-to-left documents (#202, #216, #620) ───────────────────────── [Fact] From d22985ac67a3bcf2acc0090cfa192e00c5e06a10 Mon Sep 17 00:00:00 2001 From: KnifeLemon Date: Thu, 19 Nov 2026 14:37:44 +0900 Subject: [PATCH 5/8] feat(udf): read UYAP documents into the document model (#1338) UYAP Document Editor files (.udf, Turkish courts and lawyers) are a ZIP with one content.xml: the text in a CDATA block and paragraphs whose runs point into it by offset. The new built-in UYAP reader turns them into the document model, so the Filee renderer writes PDF and the writers DOCX, ODT, HWPX, TXT and the rest. Fonts, sizes, bold/italic/underline, colours, Swing alignment, indents, spacing, tab sets, tables, Base64 pictures, page breaks, headers and footers and the page setup are read. Lines are as high as Java makes them and text has no kerning, so the reporter's petition breaks onto the same three pages as UYAP's own PDF. ".udf" is also a Universal Disk Format disc image. The content decides: a ZIP whose content.xml has a