Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CMAP decompile error #3256

Open
moi15moi opened this issue Aug 14, 2023 · 2 comments
Open

CMAP decompile error #3256

moi15moi opened this issue Aug 14, 2023 · 2 comments

Comments

@moi15moi
Copy link

moi15moi commented Aug 14, 2023

When I am trying to get an font cmap, I get an exception
You can download the font here: DFHSMincho-W9__DFPHSMincho-W9__DFGHSMincho-W9.zip

Sample

from fontTools.ttLib.ttFont import TTFont

ttFont = TTFont("DFHSMincho-W9__DFPHSMincho-W9__DFGHSMincho-W9.TTC", fontNumber=0)
cmap_table = ttFont["cmap"].getcmap(3,1)

print(cmap_table.cmap)

Which raise this exception:

Traceback (most recent call last):
  File "c:\Users\Admin\Desktop\import ctypes.py", line 6, in <module>
    print(cmap_table.cmap)
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python310\lib\site-packages\fontTools\ttLib\tables\_c_m_a_p.py", line 289, in __getattr__
    self.ensureDecompiled()
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python310\lib\site-packages\fontTools\ttLib\tables\_c_m_a_p.py", line 278, in ensureDecompiled
    self.decompile(None, None)  # use saved data.
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python310\lib\site-packages\fontTools\ttLib\tables\_c_m_a_p.py", line 918, in decompile
    assert index < lenGIArray, (
AssertionError: In format 4 cmap, range (165), the calculated index (22351) into the glyph index array is not less than the length of the array (22351) !

Why is it happening?
What can I do to avoid it.

Possible solution (line 923 of fontTools\ttLib\tables\_c_m_a_p.py) - Add an verification if the index is part of the glyphIndexArray:

if index < lenGIArray and glyphIndexArray[index] != 0:  # if not missing glyph
    glyphID = glyphIndexArray[index] + delta
else:
    glyphID = 0  # missing glyph
@justvanrossum
Copy link
Collaborator

The font is invalid; ots also rejects it.

@moi15moi
Copy link
Author

moi15moi commented Aug 15, 2023

I tested with libass which use freetype and it can correctly decode the cmap. GDI and DirectWrite also decode it correctly.
Why fonttools should reject it?

moi15moi added a commit to moi15moi/FontCollector that referenced this issue Aug 15, 2023
fontTools seems to have multiple issue to decode the cmap table.
- fonttools/fonttools#3256
- fonttools/fonttools#3060

So, we replace it by freetype
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

No branches or pull requests

2 participants