Skip to content

Commit 3fa3268

Browse files
authored
Merge pull request #206 from mini-software/fix/docx-price-list-font-layout
Fix DOCX font measurement and table layout to match Word
2 parents 7c0c0ac + a22ecb2 commit 3fa3268

8 files changed

Lines changed: 895 additions & 65 deletions

File tree

‎.github/workflows/ci.yml‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,38 @@ jobs:
6161
name: test-results
6262
path: '**/test-results.trx'
6363

64+
docx-font-tests:
65+
runs-on: windows-latest
66+
67+
steps:
68+
- name: Checkout
69+
uses: actions/checkout@v4
70+
71+
- name: Setup .NET
72+
uses: actions/setup-dotnet@v4
73+
with:
74+
dotnet-version: '9.0.x'
75+
76+
- name: Verify required DOCX fonts
77+
shell: pwsh
78+
run: |
79+
$fontDirectory = Join-Path $env:WINDIR 'Fonts'
80+
foreach ($font in @('cambria.ttc', 'cambriab.ttf', 'cambriaz.ttf', 'seguisym.ttf', 'seguiemj.ttf')) {
81+
if (-not (Test-Path -LiteralPath (Join-Path $fontDirectory $font))) {
82+
throw "Required DOCX test font is unavailable: $font"
83+
}
84+
}
85+
86+
- name: Test DOCX rendering with system fonts
87+
run: dotnet test tests/MiniPdf.Tests/MiniPdf.Tests.csproj --configuration Release --filter "FullyQualifiedName~Docx|FullyQualifiedName~SystemFontTheoryAttributeTests" --verbosity normal --logger "trx;LogFileName=docx-font-results.trx"
88+
89+
- name: Upload DOCX test results
90+
uses: actions/upload-artifact@v4
91+
if: always()
92+
with:
93+
name: docx-font-test-results
94+
path: '**/docx-font-results.trx'
95+
6496
ai-security-scan:
6597
if: github.event_name == 'pull_request'
6698
runs-on: ubuntu-latest

‎CONTRIBUTING.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ dotnet build --configuration Release
3737
dotnet test tests/MiniPdf.Tests --configuration Release
3838
```
3939

40+
DOCX tests that measure installed Cambria or Segoe fonts report an explicit skip
41+
when their required fonts are unavailable. CI runs the portable suite on Ubuntu
42+
and a separate DOCX suite on Windows, where it verifies the required fonts
43+
before testing. Install these fonts through a licensed system/Office installation
44+
to run the font-dependent checks locally; do not add font binaries to the repository.
45+
4046
### Rust
4147

4248
Install the stable Rust toolchain, then run:

‎FEATURE_PARITY.md‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,24 @@ Word compatibility modes up to 14 align implicit left indents with first-cell
149149
content; modern implicit indents continue to align the table border.
150150
Inline images in table cells honor the paragraph's left, center, or right
151151
alignment in both body and header/footer tables.
152+
Cambria table cells use the same preferred-font advances for wrapping and
153+
row-height calculation, including header/footer tables. Auto-spaced body
154+
paragraphs do not carry the next text-line ascent into a following table.
155+
Large first-paragraph runs advance subsequent content from their rendered
156+
baseline rather than the smaller inherited paragraph-mark baseline.
157+
Cell line height distinguishes explicit paragraph-mark sizing from inherited
158+
defaults. Collapsed table borders contribute half their stroke widths to natural
159+
row heights and text insets; center/bottom alignment uses the remaining content
160+
area. Cambria/Calibri paragraph flow tracks font ascents across text, empty marks,
161+
and inline-image hosts. Auto-spaced image-only paragraphs add proportional font
162+
leading without scaling image height. Hard section starts collapse paragraph
163+
space-before against the closing section's space-after.
164+
Image-only paragraphs reclaim the next text ascent before body tables, while
165+
border-only empty marks reset text-ascent tracking. Courier transitions use
166+
Courier font metrics. Symbol cells account for the Segoe fallback's ascent and
167+
line height when the PDF writer selects that font.
168+
Mixed-format Cambria paragraphs measure each run's bold/italic font advances and
169+
spaces consistently for wrapping, run positioning, alignment, and run shading.
152170

153171
## XLSX Rendering Gaps
154172

‎src/MiniPdf/DocxReader.cs‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -912,6 +912,7 @@ private static float ParseVmlPointLength(Dictionary<string, string> style, strin
912912
float spacingAfter = -1;
913913
bool spacingAfterExplicit = false;
914914
bool spacingBeforeExplicit = false;
915+
float paragraphMarkFontSize = 0;
915916
float lineSpacing = 0;
916917
bool lineSpacingAbsolute = false;
917918
bool lineSpacingExact = false;
@@ -1183,7 +1184,10 @@ private static float ParseVmlPointLength(Dictionary<string, string> style, strin
11831184
{
11841185
var sz = rPr.Element(W + "sz")?.Attribute(W + "val")?.Value;
11851186
if (float.TryParse(sz, out var s))
1187+
{
11861188
fontSize = s / 2f; // half-points to points
1189+
paragraphMarkFontSize = fontSize;
1190+
}
11871191
// Paragraph-level character spacing
11881192
var spacingEl2 = rPr.Element(W + "spacing");
11891193
if (spacingEl2 != null && int.TryParse(spacingEl2.Attribute(W + "val")?.Value, out var pcs))
@@ -1509,7 +1513,8 @@ private static float ParseVmlPointLength(Dictionary<string, string> style, strin
15091513
ListSuff: listSuff,
15101514
ListTabStop: listTabStop,
15111515
OutlineLevel: ReadOutlineLevel(pPr),
1512-
StyleIndentFirstLine: styleIndentFirstLineForListLabel);
1516+
StyleIndentFirstLine: styleIndentFirstLineForListLabel,
1517+
ParagraphMarkFontSize: paragraphMarkFontSize);
15131518
}
15141519

15151520
private static void EmitPageFieldPlaceholder(string instruction, List<DocxRun> runs, bool bold, bool italic, float fontSize, PdfColor? color)
@@ -5286,7 +5291,8 @@ internal sealed record DocxParagraph(
52865291
// not position the automatic-numbering suffix.
52875292
float? ListTabStop = null,
52885293
int OutlineLevel = -1,
5289-
float StyleIndentFirstLine = 0
5294+
float StyleIndentFirstLine = 0,
5295+
float ParagraphMarkFontSize = 0
52905296
) : DocxElement;
52915297

52925298
/// <summary>Represents a single border edge. Width=0 is a sentinel for an

0 commit comments

Comments
 (0)