MCPcopy Create free account
hub / github.com/PageBot/PageBot / asFontPath

Function asFontPath

Lib/pagebot/fonttoolbox/objects/font.py:41–83  ·  view source on GitHub ↗

Answers if the path is a font path. Check if there is a matching problem by lower case/upper case. If matching then answer the real path. If no match exists for the defined fontPath, then answer None. Case matching is only tested on the file name, not the whole path. For now, PageBo

(fontPath)

Source from the content-addressed store, hash-verified

39from pagebot.toolbox.units import RelativeUnit, Unit, upt, isUnit
40
41def asFontPath(fontPath):
42 """Answers if the path is a font path. Check if there is a matching
43 problem by lower case/upper case. If matching then answer the real path.
44 If no match exists for the defined fontPath, then answer None.
45 Case matching is only tested on the file name, not the whole path.
46
47 For now, PageBot only supports ('ttf', 'otf')
48
49 >>> from pagebot.fonttoolbox.fontpaths import getTestFontsPath
50 >>> fontPath = getTestFontsPath()
51 >>> path1 = fontPath + '/fontbureau/Amstelvar-Roman-VF.ttf'
52 >>> path1 == asFontPath(path1) # Path with capitals, nothing changed.
53 True
54 >>> path1_lc = fontPath + '/fontbureau/amstelvar-roman-vf.ttf'
55 >>> path1 == asFontPath(path1_lc) # Matching witn lower case, answers the real path.
56 True
57 >>> path2 = fontPath + '/fontbureau/Amstelvar-Roman-VF_XXX.ttf'
58 >>> path2 == asFontPath(path2)
59 False
60 >>> path3 = fontPath + '/fontbureau/Amstelvar-Roman-VF.UFO'
61 >>> asFontPath(path3) is None # Font path does not exist
62 True
63 >>> asFontPath(None) is None # Not a valid font path
64 True
65 >>> asFontPath(123) is None # Not a valid font path
66 True
67 """
68 try:
69 if not path2Extension(fontPath) in ('ttf', 'otf'):
70 return None
71
72 pathParts = fontPath.split('/')
73 dirPath = '/'.join(pathParts[:-1]) + '/'
74 filePath = pathParts[-1].lower() # Match ignoring case
75 for fn in os.listdir(dirPath):
76 if fn.startswith('.'):
77 continue
78 if filePath == fn.lower():
79 return dirPath + fn
80 return None
81 except AttributeError:
82 pass
83 return None
84
85def getFont(fontOrPath, lazy=True):
86 """Answers the Font instance, that connects to the fontPath. Note that

Callers 1

getFontFunction · 0.85

Calls 1

path2ExtensionFunction · 0.90

Tested by

no test coverage detected