diff options
Diffstat (limited to 'Lib/fontTools/designspaceLib/__init__.py')
-rw-r--r-- | Lib/fontTools/designspaceLib/__init__.py | 54 |
1 files changed, 48 insertions, 6 deletions
diff --git a/Lib/fontTools/designspaceLib/__init__.py b/Lib/fontTools/designspaceLib/__init__.py index 4c5be8b4..e40f5edf 100644 --- a/Lib/fontTools/designspaceLib/__init__.py +++ b/Lib/fontTools/designspaceLib/__init__.py @@ -755,7 +755,6 @@ class BaseDocReader(LogMixin): axisObject.labelNames[lang] = tounicode(labelNameElement.text) self.documentObject.axes.append(axisObject) self.axisDefaults[axisObject.name] = axisObject.default - self.documentObject.defaultLoc = self.axisDefaults def readSources(self): for sourceCount, sourceElement in enumerate(self.root.findall(".sources/source")): @@ -998,7 +997,6 @@ class DesignSpaceDocument(LogMixin, AsDictMixin): self.axes = [] self.rules = [] self.default = None # name of the default master - self.defaultLoc = None self.lib = {} """Custom data associated with the whole document.""" @@ -1188,10 +1186,7 @@ class DesignSpaceDocument(LogMixin, AsDictMixin): # Convert the default location from user space to design space before comparing # it against the SourceDescriptor locations (always in design space). - default_location_design = { - axis.name: axis.map_forward(self.defaultLoc[axis.name]) - for axis in self.axes - } + default_location_design = self.newDefaultLocation() for sourceDescriptor in self.sources: if sourceDescriptor.location == default_location_design: @@ -1267,3 +1262,50 @@ class DesignSpaceDocument(LogMixin, AsDictMixin): newConditions.append(dict(name=cond['name'], minimum=minimum, maximum=maximum)) newConditionSets.append(newConditions) rule.conditionSets = newConditionSets + + def loadSourceFonts(self, opener, **kwargs): + """Ensure SourceDescriptor.font attributes are loaded, and return list of fonts. + + Takes a callable which initializes a new font object (e.g. TTFont, or + defcon.Font, etc.) from the SourceDescriptor.path, and sets the + SourceDescriptor.font attribute. + If the font attribute is already not None, it is not loaded again. + Fonts with the same path are only loaded once and shared among SourceDescriptors. + + For example, to load UFO sources using defcon: + + designspace = DesignSpaceDocument.fromfile("path/to/my.designspace") + designspace.loadSourceFonts(defcon.Font) + + Or to load masters as FontTools binary fonts, including extra options: + + designspace.loadSourceFonts(ttLib.TTFont, recalcBBoxes=False) + + Args: + opener (Callable): takes one required positional argument, the source.path, + and an optional list of keyword arguments, and returns a new font object + loaded from the path. + **kwargs: extra options passed on to the opener function. + + Returns: + List of font objects in the order they appear in the sources list. + """ + # we load fonts with the same source.path only once + loaded = {} + fonts = [] + for source in self.sources: + if source.font is not None: # font already loaded + fonts.append(source.font) + continue + if source.path in loaded: + source.font = loaded[source.path] + else: + if source.path is None: + raise DesignSpaceDocumentError( + "Designspace source '%s' has no 'path' attribute" + % (source.name or "<Unknown>") + ) + source.font = opener(source.path, **kwargs) + loaded[source.path] = source.font + fonts.append(source.font) + return fonts |