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