• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#! /usr/bin/env python
2
3# Sample script to convert legacy cmap subtables to format-4
4# subtables.  Note that this is rarely what one needs.  You
5# probably need to just drop the legacy subtables if the font
6# already has a format-4 subtable.
7#
8# Other times, you would need to convert a non-Unicode cmap
9# legacy subtable to a Unicode one.  In those cases, use the
10# getEncoding() of subtable and use that encoding to map the
11# characters to Unicode...  TODO: Extend this script to do that.
12
13from __future__ import print_function, division, absolute_import
14from fontTools.misc.py23 import *
15from fontTools.ttLib import TTFont
16from fontTools.ttLib.tables._c_m_a_p import CmapSubtable
17import sys
18
19if len(sys.argv) != 3:
20	print("usage: cmap-format.py fontfile.ttf outfile.ttf")
21	sys.exit(1)
22fontfile = sys.argv[1]
23outfile = sys.argv[2]
24font = TTFont(fontfile)
25
26cmap = font['cmap']
27outtables = []
28for table in cmap.tables:
29	if table.format in [4, 12, 13, 14]:
30		outtables.append(table)
31	# Convert ot format4
32	newtable = CmapSubtable.newSubtable(4)
33	newtable.platformID = table.platformID
34	newtable.platEncID = table.platEncID
35	newtable.language = table.language
36	newtable.cmap = table.cmap
37	outtables.append(newtable)
38cmap.tables = outtables
39
40font.save(outfile)
41