def read(cls, reader: UFOReader, lazy: bool = True) -> LayerSet: """Instantiates a LayerSet object from a :class:`fontTools.ufoLib.UFOReader`. Args: path: The path to the UFO to load. lazy: If True, load glyphs, data files and images as they are accessed. If False, load everything up front. """ layers: dict[str, Layer] = {} defaultLayer = None defaultLayerName = reader.getDefaultLayerName() for layerName in reader.getLayerNames(): isDefault = layerName == defaultLayerName if isDefault or not lazy: layer = cls._loadLayer(reader, layerName, lazy, isDefault) if isDefault: defaultLayer = layer layers[layerName] = layer else: layers[layerName] = _LAYER_NOT_LOADED assert defaultLayer is not None self = cls(layers=layers, defaultLayer=defaultLayer) if lazy: self._reader = reader return self
def reloadLayers(self, layerData): """ Reload the layers. This should not be called externally. """ reader = UFOReader(self.font.path, validate=self.font.ufoLibReadValidate) # handle the layers currentLayerOrder = self.layerOrder for layerName, l in layerData.get("layers", {}).items(): # new layer if layerName not in currentLayerOrder: glyphSet = reader.getGlyphSet(layerName, validateRead=self.ufoLibReadValidate, validateWrite=self.font.ufoLibWriteValidate) self.newLayer(layerName, glyphSet) # get the layer layer = self[layerName] # reload the layer info if l.get("info"): layer.color = None layer.lib.clear() layer._glyphSet.readLayerInfo(layer) self._stampLayerInfoDataState(layer) # reload the glyphs glyphNames = l.get("glyphNames", []) if glyphNames: layer.reloadGlyphs(glyphNames) # handle the order if layerData.get("order", False): newLayerOrder = reader.getLayerNames() for layerName in self.layerOrder: if layerName not in newLayerOrder: newLayerOrder.append(layerName) self.layerOrder = newLayerOrder # handle the default layer if layerData.get("default", False): newDefaultLayerName = reader.getDefaultLayerName() self.defaultLayer = self[newDefaultLayerName]
def reloadLayers(self, layerData): """ Reload the layers. This should not be called externally. """ reader = UFOReader(self.font.path, validate=self.font.ufoLibReadValidate) # handle the layers currentLayerOrder = self.layerOrder for layerName, l in layerData.get("layers", {}).items(): # new layer if layerName not in currentLayerOrder: glyphSet = reader.getGlyphSet( layerName, validateRead=self.ufoLibReadValidate, validateWrite=self.font.ufoLibWriteValidate) self.newLayer(layerName, glyphSet) # get the layer layer = self[layerName] # reload the layer info if l.get("info"): layer.color = None layer.lib.clear() layer._glyphSet.readLayerInfo(layer) self._stampLayerInfoDataState(layer) # reload the glyphs glyphNames = l.get("glyphNames", []) if glyphNames: layer.reloadGlyphs(glyphNames) # handle the order if layerData.get("order", False): newLayerOrder = reader.getLayerNames() for layerName in self.layerOrder: if layerName not in newLayerOrder: newLayerOrder.append(layerName) self.layerOrder = newLayerOrder # handle the default layer if layerData.get("default", False): newDefaultLayerName = reader.getDefaultLayerName() self.defaultLayer = self[newDefaultLayerName]