add (0, 4) to default cmaps to search for; add getBestCmap() to TTFont as well; added test for the latter

This commit is contained in:
justvanrossum 2017-11-03 16:01:45 +01:00
parent 43a39588b3
commit 113f9cc498
3 changed files with 24 additions and 2 deletions

View File

@ -702,6 +702,16 @@ 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.
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):

View File

@ -38,12 +38,12 @@ class table__c_m_a_p(DefaultTable.DefaultTable):
return subtable
return None # not found
def getBestCmap(self, cmapPreferences=((3, 10), (3, 1), (0, 3))):
def getBestCmap(self, cmapPreferences=((3, 10), (0, 4), (3, 1), (0, 3))):
"""Return the 'best' unicode cmap dictionary available in the font.
By default it will search for the following (platformID, platEncID)
pairs:
(3, 10), (3, 1), (0, 3)
(3, 10), (0, 4), (3, 1), (0, 3)
But this can be customized via the cmapPreferences argument.
"""
for platformID, platEncID in cmapPreferences:

View File

@ -68,6 +68,18 @@ class CmapSubtableTest(unittest.TestCase):
self.assertEqual(cmap.getBestCmap(), {0x10314: 'u10314'})
self.assertEqual(cmap.getBestCmap(cmapPreferences=[(3, 1)]), {0x0041:'A', 0x0391:'A'})
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'})
if __name__ == "__main__":
import sys