Skip to content

SVG: raise FPDFUnicodeEncodingException for text a core font cannot encode - #2154

Open
RavSinghChandan wants to merge 1 commit into
py-pdf:masterfrom
RavSinghChandan:svg-text-core-font-encoding
Open

RavSinghChandan wants to merge 1 commit into
py-pdf:masterfrom
RavSinghChandan:svg-text-core-font-encoding

Conversation

@RavSinghChandan

Copy link
Copy Markdown

SVG text with a character that a core font does not have fails with a bare KeyError from the width lookup:

SVGObject('<svg xmlns="http://www.w3.org/2000/svg"><text x="1" y="10">a – b</text></svg>').draw_to_page(pdf)
# KeyError: '–'

The same text in pdf.cell() raises FPDFUnicodeEncodingException: Character "–" at index 2 in text is outside the range of characters supported by the font used: "helvetica". Please consider using a Unicode font. (from normalize_text()). En dashes, € and arrows are common in SVG diagrams.

Text.render() now checks the runs of a core font against its character widths and raises the same exception, with the same font suggestion from suggest_unicode_font_for_error(), before measuring the text.

Added test_svg_text_core_font_unsupported_character, which checks that the message equals the one of cell(); it fails on master. The full test suite passes except test_image_with_explicit_dimensions, which also fails on master in my environment; black, pylint, mypy and pyright pass

@RavSinghChandan
RavSinghChandan force-pushed the svg-text-core-font-encoding branch from 442c349 to de4c5ae Compare November 10, 2026 13:05

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