D E P R E C A T E D Use pagebot.fonttoolbox.objects.font.instantiateVariableFont instead (calling fontTools) Instantiate an instance of a variable font at the specified location. Keyword arguments: varfilename -- a variable font file path location -- a dictionary of ax
(variableFontPath, location, targetDirectory,
normalize=True, cached=True, lazy=True)
| 194 | |
| 195 | |
| 196 | def generateInstance(variableFontPath, location, targetDirectory, |
| 197 | normalize=True, cached=True, lazy=True): |
| 198 | """ |
| 199 | D E P R E C A T E D |
| 200 | |
| 201 | Use pagebot.fonttoolbox.objects.font.instantiateVariableFont instead |
| 202 | (calling fontTools) |
| 203 | |
| 204 | Instantiate an instance of a variable font at the specified location. |
| 205 | Keyword arguments: |
| 206 | varfilename -- a variable font file path |
| 207 | location -- a dictionary of axis tag and value {"wght": 0.75, "wdth": |
| 208 | -0.5} |
| 209 | """ |
| 210 | # make a custom file name from the location e.g. VariableFont-wghtXXX-wdthXXX.ttf |
| 211 | instanceName = "" |
| 212 | |
| 213 | for k, v in sorted(location.items()): |
| 214 | # TODO better way to normalize the location name to (0, 1000) |
| 215 | v = min(v, 1000) |
| 216 | v = max(v, 0) |
| 217 | instanceName += "-%s%s" % (k, v) |
| 218 | |
| 219 | targetFileName = '.'.join(variableFontPath.split('/')[-1].split('.')[:-1]) + instanceName + '.ttf' |
| 220 | |
| 221 | if not targetDirectory.endswith('/'): |
| 222 | targetDirectory += '/' |
| 223 | if not os.path.exists(targetDirectory): |
| 224 | os.makedirs(targetDirectory) |
| 225 | outFile = targetDirectory + targetFileName |
| 226 | |
| 227 | if not cached or not os.path.exists(outFile): |
| 228 | # Instance does not exist as file. Create it. |
| 229 | |
| 230 | # print("Loading GX font") |
| 231 | varfont = TTFont(variableFontPath, lazy=lazy) |
| 232 | |
| 233 | # Set the instance name IDs in the name table |
| 234 | platforms=((1, 0, 0), (3, 1, 0x409)) # Macintosh and Windows |
| 235 | for platformID, platEncID, langID in platforms: |
| 236 | familyName = varfont['name'].getName(1, platformID, platEncID, langID) # 1 Font Family name |
| 237 | if not familyName: |
| 238 | continue |
| 239 | familyName = familyName.toUnicode() # NameRecord to unicode string |
| 240 | styleName = instanceName # TODO make sure this works in any case |
| 241 | fullFontName = " ".join([familyName, styleName]) |
| 242 | postscriptName = fullFontName.replace(" ", "-") |
| 243 | varfont['name'].setName(styleName, 2, platformID, platEncID, langID) # 2 Font Subfamily name |
| 244 | varfont['name'].setName(fullFontName, 4, platformID, platEncID, langID) # 4 Full font name |
| 245 | varfont['name'].setName(postscriptName, 6, platformID, platEncID, langID) # 6 Postscript name for the font |
| 246 | # Other important name IDs |
| 247 | # 3 Unique font identifier (e.g. Version 0.000;NONE;Promise Bold Regular) |
| 248 | # 25 Variables PostScript Name Prefix |
| 249 | |
| 250 | fvar = varfont['fvar'] |
| 251 | axes = {a.axisTag:(a.minValue,a.defaultValue,a.maxValue) for a in fvar.axes} |
| 252 | # TODO Apply avar |
| 253 | # TODO Round to F2Dot14? |
no test coverage detected