From 4e8295920a61c1e7247f18368e105d677f5e8156 Mon Sep 17 00:00:00 2001 From: justvanrossum Date: Fri, 3 Nov 2017 16:19:48 +0100 Subject: [PATCH] return None if no unicode cmap subtables were found --- Lib/fontTools/ttLib/__init__.py | 3 ++- Lib/fontTools/ttLib/tables/_c_m_a_p.py | 5 +++-- Tests/ttLib/tables/_c_m_a_p_test.py | 2 ++ 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/Lib/fontTools/ttLib/__init__.py b/Lib/fontTools/ttLib/__init__.py index 75b8ddcfc..a729d5610 100644 --- a/Lib/fontTools/ttLib/__init__.py +++ b/Lib/fontTools/ttLib/__init__.py @@ -703,7 +703,8 @@ 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. + """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: diff --git a/Lib/fontTools/ttLib/tables/_c_m_a_p.py b/Lib/fontTools/ttLib/tables/_c_m_a_p.py index ba8c97a13..dea7e410d 100644 --- a/Lib/fontTools/ttLib/tables/_c_m_a_p.py +++ b/Lib/fontTools/ttLib/tables/_c_m_a_p.py @@ -39,7 +39,8 @@ class table__c_m_a_p(DefaultTable.DefaultTable): 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. + """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: @@ -50,7 +51,7 @@ class table__c_m_a_p(DefaultTable.DefaultTable): cmapSubtable = self.getcmap(platformID, platEncID) if cmapSubtable is not None: return cmapSubtable.cmap - raise ValueError("None of the requested cmap subtables were found") + return None # None of the requested cmap subtables were found def buildReversed(self): """Returns a reverse cmap such as {'one':{0x31}, 'A':{0x41,0x391}}. diff --git a/Tests/ttLib/tables/_c_m_a_p_test.py b/Tests/ttLib/tables/_c_m_a_p_test.py index a6f91b7b0..664734580 100644 --- a/Tests/ttLib/tables/_c_m_a_p_test.py +++ b/Tests/ttLib/tables/_c_m_a_p_test.py @@ -67,6 +67,7 @@ class CmapSubtableTest(unittest.TestCase): 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) @@ -79,6 +80,7 @@ class CmapSubtableTest(unittest.TestCase): 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__":