File: cmap-format.py

package info (click to toggle)
fonttools 4.61.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 27,584 kB
  • sloc: python: 145,091; xml: 103; makefile: 24
file content (38 lines) | stat: -rwxr-xr-x 1,196 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#! /usr/bin/env python3

# Sample script to convert legacy cmap subtables to format-4
# subtables.  Note that this is rarely what one needs.  You
# probably need to just drop the legacy subtables if the font
# already has a format-4 subtable.
#
# Other times, you would need to convert a non-Unicode cmap
# legacy subtable to a Unicode one.  In those cases, use the
# getEncoding() of subtable and use that encoding to map the
# characters to Unicode...  TODO: Extend this script to do that.

from fontTools.ttLib import TTFont
from fontTools.ttLib.tables._c_m_a_p import CmapSubtable
import sys

if len(sys.argv) != 3:
    print("usage: cmap-format.py fontfile.ttf outfile.ttf")
    sys.exit(1)
fontfile = sys.argv[1]
outfile = sys.argv[2]
font = TTFont(fontfile)

cmap = font["cmap"]
outtables = []
for table in cmap.tables:
    if table.format in [4, 12, 13, 14]:
        outtables.append(table)
    # Convert ot format4
    newtable = CmapSubtable.newSubtable(4)
    newtable.platformID = table.platformID
    newtable.platEncID = table.platEncID
    newtable.language = table.language
    newtable.cmap = table.cmap
    outtables.append(newtable)
cmap.tables = outtables

font.save(outfile)