Skip to content

Fix excessive PDF size caused by ideographic space font fallback - #223

Open
mikairyuu wants to merge 319 commits into
mini-software:mainfrom
mikairyuu:patch-1
Open

mikairyuu wants to merge 319 commits into
mini-software:mainfrom
mikairyuu:patch-1

Conversation

@mikairyuu

@mikairyuu mikairyuu commented Nov 26, 2026 •

Copy link
Copy Markdown

Problem

HasUsableGlyph recognizes only three whitespace characters as valid glyphs without outlines. As a result, an ideographic space (U+3000) can trigger unnecessary font fallback even when the preferred TrueType font supports it.

In my case, converting a six-page Japanese Excel report to PDF produced a 14 MB file because MiniPdf embedded the entire Noto Sans CJK JP font alongside IPAexGothic and IPAexMincho.

Inspecting the embedded font's ToUnicode CMap confirmed that U+3000 was the only character assigned to Noto Sans CJK JP.

Solution

Replace the hardcoded whitespace check with char.IsWhiteSpace, allowing Unicode whitespace characters to use their existing glyphs without requiring visible outlines.

This preserves the intended font selection and avoids unnecessary font embedding.

Summary by CodeRabbit

  • Bug Fixes
    • PDF text output now recognizes a broader range of BMP whitespace characters as usable glyphs, including whitespace types beyond regular spaces. This can improve how text containing those characters is handled when generating PDFs. Characters that were previously recognized as whitespace remain supported.

shps951023 and others added 30 commits August 27, 2026 00:51
shps951023 and others added 27 commits November 14, 2026 14:23
Untrack selected project notes and local configuration, update README links, and retain the standard build and test jobs.
Keep local notes and configuration ignored; preserve .NET CI and license files.
Switch portable conversion to managed EMF and WMF rendering, resolve real font faces, preserve cropping, and remove incorporated Drawing sources and package notices.
Reuse the existing CJK font selection policy and report substitution rather than losing OLE previews when an exact family is unavailable.
…dering

Replace incorporated Drawing code with owned managed metafile rendering. Preserve the existing CJK fallback policy after Ubuntu CI verification.
…ftware#216) (mini-software#218)

* fix(xlsx): read SpreadsheetML parts that use a prefixed namespace

ExcelReader took the namespace of each part from the root's default
namespace (GetDefaultNamespace). Parts written with a prefixed namespace,
such as <x:workbook xmlns:x="...">, have no default namespace, so every
lookup used XNamespace.None and found no sheets, strings or cells. Use the
root element's own namespace instead, which is identical for default
namespace documents.

Fixes mini-software#216

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(xlsx): honor prefixed font flags and range bounds

* fix(xlsx): resolve cell references in between rules

* fix(xlsx): compare conditional rules using raw values

* fix(xlsx): preserve numeric operands in low-memory rules

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Wei Lin <shps951002@gmail.com>
…software#158) (mini-software#219)

* Resolve PAGE and NUMPAGES fields in header/footer tables

RenderHeaderFooterElementsOnPage resolved the {PAGE}/{NUMPAGES}
placeholders only for header/footer paragraphs. Paragraphs inside a
header/footer table were drawn with the raw placeholders (and wrapped
as such in narrow cells), so the footer showed "Page {PAGE} of
{NUMPAGES}". Resolve the placeholders in a copy of the table before it
is measured and drawn, and measure the reserved footer height with the
same page 1 of 1 convention as paragraphs.

Refs mini-software#158

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Fix DOCX page fields in header/footer tables

* Measure header/footer margins after pagination

* Honor section layouts for header and footer rendering

* Keep continuous-section footer margins across pages

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Wei Lin <shps951002@gmail.com>
…ini-software#221)

* fix(xlsx): keep the whole-table style when a table has a data format

A table can carry its own data format (dataDxfId) next to its table
style. The reader applied that format instead of the wholeTable element
of the style, so the fill of the table style was lost on every data row.
In "Simple invoice1" the table body was printed white on a tinted page.

Apply the wholeTable element first, then row stripes, then the table's
own data format, so each layer adds to the one below it.

The assertion for "Business expenses budget2" expected no fill on the
rows between stripes. The Excel reference shows the whole-table fill
there, so the assertion now expects it.

* docs(xlsx): state the full layer order for table data rows

* fix(xlsx): keep a cell's own fill on top of the table style

A fill set on a table body cell is direct formatting. The table style
layers replaced it, so an explicitly filled cell took the whole-table or
stripe fill.

Restore the cell's own fill after the table layers are applied. The
firstColumn element moves into the data-row loop so the same rule covers
it.

Font properties are not covered: the reader cannot yet tell an explicit
cell font from the workbook default.

* test(xlsx): cover a cell's own fill under row stripes and firstColumn

The firstColumn branch that moved into the data-row loop had no test. Add row stripes and a firstColumn element to the fixture behind a flag, and list firstColumn in the layer-order comment.

* Preserve empty and white table fills

---------

Co-authored-by: Wei Lin <shps951002@gmail.com>
Use char.IsWhiteSpace instead of a hardcoded list of whitespace
characters in HasUsableGlyph to prevent unnecessary font fallback
for characters without visible outlines, such as U+3000.
@coderabbitai

coderabbitai Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

PdfWriter.HasUsableGlyph now accepts any BMP character recognized by char.IsWhiteSpace without checking its glyph outline.

Changes: Glyph Handling

Layer / File(s) Summary
Whitespace character check
src/MiniPdf/PdfWriter.cs
HasUsableGlyph accepts BMP whitespace characters recognized by char.IsWhiteSpace. Previously, this exception applied only to space, nonbreaking space, and thin space.

Estimated code review effort: 2 (Simple) | ~5 minutes

Change: Bug fix

Suggested reviewers: shps951023

Merge Risk: Minimal · up to 4d918

This change stops ideographic space from triggering needless font fallback, which should keep PDFs from embedding large extra fonts. The change is small and easy to revert, and no concrete merge-blocking risk was found.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the main change: the excessive PDF size came from ideographic space (U+3000) font fallback. The raw summary and objectives confirm that HasUsableGlyph now uses char.IsWhiteSpace for wh…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the main change: the excessive PDF size came from ideographic space (U+3000) font fallback. The raw summary and objectives confirm that HasUsableGlyph now uses char.IsWhiteSpace for whitespace handling, which addresses that fallback. The title is specific and matches the changeset.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
Fix failing CI checks

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 21e61081-ef49-42ef-9396-65fbb871bca1
Commits

Reviewing files that changed from the base of the PR and between 549ec1b and 4d918c7.

Files selected for processing: 1

  • src/MiniPdf/PdfWriter.cs

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 3 remain after this review.

Comment thread src/MiniPdf/PdfWriter.cs
private static bool HasUsableGlyph(byte[] ttf, int codePoint, ushort gid)
{
if (codePoint is ' ' or 0x00A0 or 0x2009) return true;
if (codePoint <= char.MaxValue && char.IsWhiteSpace((char)codePoint)) return true;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we add a regression test for this? For example, a text block with U+3000 and a preferred font whose U+3000 glyph has no outline, asserting that only the preferred font is embedded. If there is no suitable font available in CI, a before/after file size in the PR description is fine too.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

10 participants