fpdf2 icon indicating copy to clipboard operation
fpdf2 copied to clipboard

Crashes when adding fonts with no Unicode cmap subtable, like Window's symbol.ttf

Open jasonfevangsafe opened this issue 9 months ago • 2 comments

When adding C:/windows/fonts/symbol.ttf, fpdf throws as it assumes all fonts have a cmap, but symbol.ttf doesn't. Perhaps there are other fonts without cmap subtables, I'm not sure.

I'm not allowed to attach .ttf files to the issue, also may be legal issues with that? A quick google search brought up this site(lol) with a font exhibiting the same issue

Error details

Traceback (most recent call last):
  File "C:\code\fpdf2_fork\test.py", line 2, in <module>
    FPDF().add_font(
  File "C:\code\fpdf2_fork\fpdf\fpdf.py", line 2144, in add_font
    self.fonts[fontkey] = TTFFont(self, font_file_path, fontkey, style)
                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\code\fpdf2_fork\fpdf\fonts.py", line 363, in __init__
    for char in self.cmap:
TypeError: 'NoneType' object is not iterable

Minimal code Please include some minimal Python code reproducing your issue:

from fpdf import FPDF
FPDF().add_font(
    family="symbol_notcore",
    fname="C:/Windows/fonts/symbol.ttf",
)

Environment Please provide the following information:

  • Operating System: Windows
  • Python version: 3.12.2
    • fpdf2 version used: 2.8.2

jasonfevangsafe avatar Mar 13 '25 22:03 jasonfevangsafe

Hi @jasonfevangsafe 👋

Thank you for this bug report. I was able to reproduce this problem on my computer.

Seems like this font file has no Unicode cmap subtable, which is a case we currently do not support, hence the crash.

For now I just added an explicit error message: https://github.com/py-pdf/fpdf2/commit/1d2eb99ba27a8f63744dcf4c07fa0c1f860495d0

But we should try to support this case, and Pull Requests are welcome to improve this!

As a workaround for now @jasonfevangsafe, you should be able to use some tool (online or offline) to convert font files, in order to use a modified version of this Symbol font converted to a more "modern" version with a cmap table.

Also note that you can use the builtin Symbol font without any external font file: https://py-pdf.github.io/fpdf2/EmojisSymbolsDingbats.html#symbols

Lucas-C avatar Mar 14 '25 14:03 Lucas-C

Thanks for the suggestions!

jasonfevangsafe avatar Mar 17 '25 23:03 jasonfevangsafe