From df76c4cecc6214afcfe300d3a0f04c3040f308e2 Mon Sep 17 00:00:00 2001 From: Just van Rossum Date: Sun, 3 Dec 2023 11:40:07 +0100 Subject: [PATCH] Make dataclass constructors kw-only --- src/fontra/backends/designspace.py | 8 +++++--- src/fontra/backends/opentype.py | 2 +- src/fontra/core/classes.py | 14 +++++++------- src/fontra/core/instancer.py | 2 +- src/fontra/core/path.py | 6 ++++-- 5 files changed, 18 insertions(+), 14 deletions(-) diff --git a/src/fontra/backends/designspace.py b/src/fontra/backends/designspace.py index e6ffb6e60..ea184fc78 100644 --- a/src/fontra/backends/designspace.py +++ b/src/fontra/backends/designspace.py @@ -201,7 +201,7 @@ async def getGlyph(self, glyphName): ) sourceNameMapping = ufoGlyph.lib.get(SOURCE_NAME_MAPPING_LIB_KEY, {}) layerNameMapping = ufoGlyph.lib.get(LAYER_NAME_MAPPING_LIB_KEY, {}) - layers[ufoLayer.fontraLayerName] = Layer(staticGlyph) + layers[ufoLayer.fontraLayerName] = Layer(glyph=staticGlyph) # When a glyph has axes with names that also exist as global axes, we need # to make sure our source locations use the *local* default values. We do @@ -235,7 +235,7 @@ async def getGlyph(self, glyphName): for source in sources: source.name = sourceNameMapping.get(source.name, source.name) - return VariableGlyph(glyphName, axes=axes, sources=sources, layers=layers) + return VariableGlyph(name=glyphName, axes=axes, sources=sources, layers=layers) def _unpackLocalDesignSpace(self, dsDict, defaultLayerName): axes = [ @@ -900,7 +900,9 @@ def unpackVariableComponents(lib): transformationDict = componentDict.get("transformation", {}) transformation = DecomposedTransform(**transformationDict) location = componentDict.get("location", {}) - components.append(Component(glyphName, transformation, location)) + components.append( + Component(name=glyphName, transformation=transformation, location=location) + ) return components diff --git a/src/fontra/backends/opentype.py b/src/fontra/backends/opentype.py index 31cedcbea..c67c19160 100644 --- a/src/fontra/backends/opentype.py +++ b/src/fontra/backends/opentype.py @@ -41,7 +41,7 @@ async def getGlyph(self, glyphName): if glyphName not in self.glyphSet: return None defaultLayerName = "" - glyph = VariableGlyph(glyphName) + glyph = VariableGlyph(name=glyphName) staticGlyph = buildStaticGlyph(self.glyphSet, glyphName) layers = {defaultLayerName: Layer(glyph=staticGlyph)} defaultLocation = {axis.name: 0 for axis in self.globalAxes} diff --git a/src/fontra/core/classes.py b/src/fontra/core/classes.py index 4d6eb187e..816248a9c 100644 --- a/src/fontra/core/classes.py +++ b/src/fontra/core/classes.py @@ -14,14 +14,14 @@ CustomData = dict[str, Any] -@dataclass +@dataclass(kw_only=True) class Component: name: str transformation: DecomposedTransform = field(default_factory=DecomposedTransform) location: Location = field(default_factory=Location) -@dataclass +@dataclass(kw_only=True) class StaticGlyph: path: Union[PackedPath, Path] = field(default_factory=PackedPath) components: list[Component] = field(default_factory=list) @@ -36,7 +36,7 @@ def convertToPaths(self): return replace(self, path=self.path.asPath()) -@dataclass +@dataclass(kw_only=True) class Source: name: str layerName: str @@ -45,13 +45,13 @@ class Source: customData: CustomData = field(default_factory=CustomData) -@dataclass +@dataclass(kw_only=True) class Layer: glyph: StaticGlyph customData: CustomData = field(default_factory=CustomData) -@dataclass +@dataclass(kw_only=True) class LocalAxis: name: str minValue: float @@ -59,7 +59,7 @@ class LocalAxis: maxValue: float -@dataclass(slots=True) +@dataclass(kw_only=True) class VariableGlyph: name: str axes: list[LocalAxis] = field(default_factory=list) @@ -124,7 +124,7 @@ class GlobalDiscreteAxis: GlyphMap = dict[str, list[int]] -@dataclass +@dataclass(kw_only=True) class Font: unitsPerEm: int = 1000 glyphs: GlyphSet = field(default_factory=GlyphSet) diff --git a/src/fontra/core/instancer.py b/src/fontra/core/instancer.py index aa44ab8ce..3bd25ed00 100644 --- a/src/fontra/core/instancer.py +++ b/src/fontra/core/instancer.py @@ -215,7 +215,7 @@ def combinedAxes(self) -> list[LocalAxis]: continue combinedAxes.append( LocalAxis( - axis.name, + name=axis.name, minValue=mapFunc(axis.minValue), defaultValue=mapFunc(axis.defaultValue), maxValue=mapFunc(axis.maxValue), diff --git a/src/fontra/core/path.py b/src/fontra/core/path.py index 92bf6d66e..53bd2fa48 100644 --- a/src/fontra/core/path.py +++ b/src/fontra/core/path.py @@ -354,7 +354,7 @@ def addComponent(self, glyphName, transformation, **kwargs): from .classes import Component transformation = DecomposedTransform.fromTransform(transformation) - self.components.append(Component(glyphName, transformation)) + self.components.append(Component(name=glyphName, transformation=transformation)) def addVarComponent( self, glyphName, transformation, location, identifier=None, **kwargs @@ -362,7 +362,9 @@ def addVarComponent( from .classes import Component transformation = copy(transformation) - self.components.append(Component(glyphName, transformation, location)) + self.components.append( + Component(name=glyphName, transformation=transformation, location=location) + ) _pointToSegmentType = {