Merge pull request #1092 from fonttools/get-best-cmap
[ttLib] Add a convenience method to the cmap table to return the best available cmap
This commit is contained in:
commit
604059305b
@ -702,6 +702,17 @@ class TTFont(object):
|
||||
|
||||
return glyphs
|
||||
|
||||
def getBestCmap(self, cmapPreferences=((3, 10), (0, 4), (3, 1), (0, 3))):
|
||||
"""Return the 'best' unicode cmap dictionary available in the font,
|
||||
or None, if no unicode cmap subtable is available.
|
||||
|
||||
By default it will search for the following (platformID, platEncID)
|
||||
pairs:
|
||||
(3, 10), (0, 4), (3, 1), (0, 3)
|
||||
But this can be customized via the cmapPreferences argument.
|
||||
"""
|
||||
return self["cmap"].getBestCmap(cmapPreferences=cmapPreferences)
|
||||
|
||||
|
||||
class _TTGlyphSet(object):
|
||||
|
||||
|
@ -38,6 +38,21 @@ class table__c_m_a_p(DefaultTable.DefaultTable):
|
||||
return subtable
|
||||
return None # not found
|
||||
|
||||
def getBestCmap(self, cmapPreferences=((3, 10), (0, 4), (3, 1), (0, 3))):
|
||||
"""Return the 'best' unicode cmap dictionary available in the font,
|
||||
or None, if no unicode cmap subtable is available.
|
||||
|
||||
By default it will search for the following (platformID, platEncID)
|
||||
pairs:
|
||||
(3, 10), (0, 4), (3, 1), (0, 3)
|
||||
But this can be customized via the cmapPreferences argument.
|
||||
"""
|
||||
for platformID, platEncID in cmapPreferences:
|
||||
cmapSubtable = self.getcmap(platformID, platEncID)
|
||||
if cmapSubtable is not None:
|
||||
return cmapSubtable.cmap
|
||||
return None # None of the requested cmap subtables were found
|
||||
|
||||
def buildReversed(self):
|
||||
"""Returns a reverse cmap such as {'one':{0x31}, 'A':{0x41,0x391}}.
|
||||
|
||||
|
@ -58,6 +58,30 @@ class CmapSubtableTest(unittest.TestCase):
|
||||
cmap.tables = [c4, c12]
|
||||
self.assertEqual(cmap.buildReversed(), {'A':{0x0041, 0x0391}, 'u10314':{0x10314}})
|
||||
|
||||
def test_getBestCmap(self):
|
||||
c4 = self.makeSubtable(4, 3, 1, 0)
|
||||
c4.cmap = {0x0041:'A', 0x0391:'A'}
|
||||
c12 = self.makeSubtable(12, 3, 10, 0)
|
||||
c12.cmap = {0x10314: 'u10314'}
|
||||
cmap = table__c_m_a_p()
|
||||
cmap.tables = [c4, c12]
|
||||
self.assertEqual(cmap.getBestCmap(), {0x10314: 'u10314'})
|
||||
self.assertEqual(cmap.getBestCmap(cmapPreferences=[(3, 1)]), {0x0041:'A', 0x0391:'A'})
|
||||
self.assertEqual(cmap.getBestCmap(cmapPreferences=[(0, 4)]), None)
|
||||
|
||||
def test_font_getBestCmap(self):
|
||||
c4 = self.makeSubtable(4, 3, 1, 0)
|
||||
c4.cmap = {0x0041:'A', 0x0391:'A'}
|
||||
c12 = self.makeSubtable(12, 3, 10, 0)
|
||||
c12.cmap = {0x10314: 'u10314'}
|
||||
cmap = table__c_m_a_p()
|
||||
cmap.tables = [c4, c12]
|
||||
font = ttLib.TTFont()
|
||||
font["cmap"] = cmap
|
||||
self.assertEqual(font.getBestCmap(), {0x10314: 'u10314'})
|
||||
self.assertEqual(font.getBestCmap(cmapPreferences=[(3, 1)]), {0x0041:'A', 0x0391:'A'})
|
||||
self.assertEqual(font.getBestCmap(cmapPreferences=[(0, 4)]), None)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import sys
|
||||
|
Loading…
x
Reference in New Issue
Block a user