Skip to content

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

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

mikairyuu wants to merge 1 commit 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.

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.

2 participants