Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7143dd6

Browse files
committed
fix: use valid CID font names for Chinese text rendering (fixes HKUDS#24)
UnicodeCIDFont only supports a fixed set of CID font names: STSong-Light, MSung-Light, HeiseiMin-W3, HeiseiKakuGo-W5, HYSMyeongJo-Medium The old code used system font names (SimSun, SimHei, STHeiti) which are NOT valid CID names, causing silent font registration failures on Windows and partial failures on macOS. Chinese text would render as black squares or fallback to default Latin fonts. Fix: - Replace platform-specific invalid font name lists with STSong-Light, the standard cross-platform CID font for Simplified Chinese - Only apply fallback font when WenQuanYi (Linux) is not already set - Add tests verifying CID font name validity Co-Authored-By: OpenClaw
1 parent 4069e17 commit 7143dd6

2 files changed

Lines changed: 59 additions & 31 deletions

File tree

raganything/parser.py

Lines changed: 15 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -346,38 +346,22 @@ def convert_text_to_pdf(
346346
heading_style.fontName = "WenQuanYi"
347347

348348
# Try to register a font that supports Chinese characters
349+
# UnicodeCIDFont only supports specific CID font names:
350+
# STSong-Light (Chinese), MSung-Light (Chinese Traditional),
351+
# HeiseiMin-W3 / HeiseiKakuGo-W5 (Japanese),
352+
# HYSMyeongJo-Medium (Korean)
353+
# System font names like "SimSun", "SimHei", "STHeiti" are
354+
# NOT valid CID names and silently fail (#24).
349355
try:
350-
# Try to use system fonts that support Chinese
351-
import platform
352-
353-
system = platform.system()
354-
if system == "Windows":
355-
# Try common Windows fonts
356-
for font_name in ["SimSun", "SimHei", "Microsoft YaHei"]:
357-
try:
358-
from reportlab.pdfbase.cidfonts import (
359-
UnicodeCIDFont,
360-
)
361-
362-
pdfmetrics.registerFont(UnicodeCIDFont(font_name))
363-
normal_style.fontName = font_name
364-
heading_style.fontName = font_name
365-
break
366-
except Exception:
367-
continue
368-
elif system == "Darwin": # macOS
369-
for font_name in ["STSong-Light", "STHeiti"]:
370-
try:
371-
from reportlab.pdfbase.cidfonts import (
372-
UnicodeCIDFont,
373-
)
374-
375-
pdfmetrics.registerFont(UnicodeCIDFont(font_name))
376-
normal_style.fontName = font_name
377-
heading_style.fontName = font_name
378-
break
379-
except Exception:
380-
continue
356+
from reportlab.pdfbase.cidfonts import UnicodeCIDFont
357+
358+
# STSong-Light is the standard CID font for Simplified
359+
# Chinese and works cross-platform (reportlab ships the
360+
# required CID resources internally).
361+
pdfmetrics.registerFont(UnicodeCIDFont("STSong-Light"))
362+
if not support_chinese:
363+
normal_style.fontName = "STSong-Light"
364+
heading_style.fontName = "STSong-Light"
381365
except Exception:
382366
pass # Use default fonts if Chinese font setup fails
383367

tests/test_chinese_cid_font.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
"""Tests for Chinese CID font registration (issue #24).
2+
3+
Verifies that STSong-Light is registered as the cross-platform Chinese CID
4+
font instead of invalid system font names like SimSun/SimHei.
5+
"""
6+
7+
import sys
8+
from pathlib import Path
9+
10+
import pytest
11+
12+
sys.path.insert(0, str(Path(__file__).parent.parent))
13+
14+
15+
class TestChineseCIDFont:
16+
"""Test CID font registration for Chinese text rendering."""
17+
18+
def test_stsong_light_is_valid_cid_font(self):
19+
"""STSong-Light should register successfully as a CID font."""
20+
from reportlab.pdfbase.cidfonts import UnicodeCIDFont
21+
from reportlab.pdfbase import pdfmetrics
22+
23+
font = UnicodeCIDFont("STSong-Light")
24+
pdfmetrics.registerFont(font)
25+
# No exception means it worked
26+
27+
def test_invalid_cid_font_names_raise(self):
28+
"""Font names used in the old code should fail as CID fonts."""
29+
from reportlab.pdfbase.cidfonts import UnicodeCIDFont
30+
31+
invalid_names = ["SimSun", "SimHei", "Microsoft YaHei", "STHeiti"]
32+
for name in invalid_names:
33+
with pytest.raises(Exception):
34+
UnicodeCIDFont(name)
35+
36+
def test_all_valid_cid_cjk_font_names(self):
37+
"""Verify the set of valid CJK CID font names in reportlab."""
38+
from reportlab.pdfbase.cidfonts import UnicodeCIDFont
39+
from reportlab.pdfbase import pdfmetrics
40+
41+
valid_cjk = ["STSong-Light", "MSung-Light", "HeiseiMin-W3", "HeiseiKakuGo-W5", "HYSMyeongJo-Medium"]
42+
for name in valid_cjk:
43+
font = UnicodeCIDFont(name)
44+
pdfmetrics.registerFont(font)

0 commit comments

Comments
 (0)