question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Error for cmap in saving a subset font: "OverflowError: unsigned short is greater than maximum"

See original GitHub issue

I have a script that makes a “trial font” by subsetting a font, then assigning cut-out unicodes to a single glyph.

This has worked well for me many times, but I am currently confused by an error that is triggered when I try to save a modified font in the script:

Traceback (most recent call last):
  File "/Users/stephennixon/project/build/helpers/make-trial-font.py", line 234, in <module>
    main()
  File "/Users/stephennixon/project/build/helpers/make-trial-font.py", line 209, in main
    tempFont.save(tempFontPath.replace(f".temporary.{filetype}",f".{nameSuffix}.{filetype}"))
  File "/Users/stephennixon/project/venv/lib/python3.10/site-packages/fontTools/ttLib/ttFont.py", line 185, in save
    writer_reordersTables = self._save(tmp)
  File "/Users/stephennixon/project/venv/lib/python3.10/site-packages/fontTools/ttLib/ttFont.py", line 225, in _save
    self._writeTable(tag, writer, done, tableCache)
  File "/Users/stephennixon/project/venv/lib/python3.10/site-packages/fontTools/ttLib/ttFont.py", line 658, in _writeTable
    tabledata = self.getTableData(tag)
  File "/Users/stephennixon/project/venv/lib/python3.10/site-packages/fontTools/ttLib/ttFont.py", line 680, in getTableData
    return self.tables[tag].compile(self)
  File "/Users/stephennixon/project/venv/lib/python3.10/site-packages/fontTools/ttLib/tables/_c_m_a_p.py", line 184, in compile
    chunk = table.compile(ttFont)
  File "/Users/stephennixon/project/venv/lib/python3.10/site-packages/fontTools/ttLib/tables/_c_m_a_p.py", line 911, in compile
    charCodeArray = array.array("H", endCode + [0] + startCode)
OverflowError: unsigned short is greater than maximum

Other issues suggest that the fix might be something like setting the format of the post table, but this seems to be a problem relating to the cmap table. (Also, setting a format 3 post table doesn’t seem to make a difference, in my initial attempts.)

The cmap table in the current font is version 4, similar to other fonts I’ve successfully run this script on.

Is there anything else I might try to get past this?

Thanks so much for any advice!

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:19 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
justvanrossumcommented, Oct 29, 2022

Alternatively you could rebuild the cmap from scratch. FontBuilder.setupCharacterMap() makes this fairly trivial, and will add the right subtables depending on the character set. Something like:

from fontTools.fontBuilder import FontBuilder

newCmap = {....}  # code points => glyph names
fb = FontBuilder(font=<a-TTFont-instance>)
fb.setupCharacterMap(newCmap)
fb.save(<new-file-name>)
1reaction
anthrotypecommented, Oct 29, 2022

glad it worked

should I be calling the subsetter as a python module, somehow, and feeding in the TTFont?

up to you; in that case at the very least you’d need to create an subset.Options() passing in as keyword arguments all the options you want, then instantiate a subset.Subsetter(options), call populate method with the set of unicodes or glyphs to keep, and finally call its subset(font) method which will modify the TTFont instance in-place. Something like this

    from fontTools import subset
    options = subset.Options(glyph_names=True, name_IDs=["*"], notdef_outline=True)
    subsetter = subset.Subsetter(options)
    subsetter.populate(unicodes=["*"])
    subsetter.subset(font)
    font.save(output_file)
Read more comments on GitHub >

github_iconTop Results From Across the Web

otf2ttf OverflowError: unsigned short is greater than maximum
I want to convert the Noto CJK fonts from OTF to TTF (so they work better with Microsoft Word). Here's what happens with ......
Read more >
I keep getting overflowError: unsigned short is greater than ...
Sounds like somewhere an integer exceeds the maximum limit that it can hold. Without giving us an exact error code it'll be a...
Read more >
comparison of characters in C & python - Stack Overflow
it's actually giving: OverflowError: unsigned byte integer is greater than maximum. and it's because the %c format expects a single byte, ...
Read more >
fonttools · PyPI
fontTools is a library for manipulating fonts, written in Python. The project includes the TTX tool, that can convert TrueType and OpenType fonts...
Read more >
Diff - platform/external/fonttools - Google Git
TTX now makes more compact + data than in any shipping font I've tested it ... +- Fixed bug where glyphnames were not...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found