Skip to content

Commit

Permalink
Merge pull request #1011 from googlefonts/kw-only
Browse files Browse the repository at this point in the history
Make dataclass constructors kw-only
  • Loading branch information
justvanrossum authored Dec 3, 2023
2 parents a38a01d + df76c4c commit b134721
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 14 deletions.
8 changes: 5 additions & 3 deletions src/fontra/backends/designspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 = [
Expand Down Expand Up @@ -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


Expand Down
2 changes: 1 addition & 1 deletion src/fontra/backends/opentype.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ async def getGlyph(self, glyphName):
if glyphName not in self.glyphSet:
return None
defaultLayerName = "<default>"
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}
Expand Down
14 changes: 7 additions & 7 deletions src/fontra/core/classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -45,21 +45,21 @@ 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
defaultValue: float
maxValue: float


@dataclass(slots=True)
@dataclass(kw_only=True)
class VariableGlyph:
name: str
axes: list[LocalAxis] = field(default_factory=list)
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/fontra/core/instancer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
6 changes: 4 additions & 2 deletions src/fontra/core/path.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,15 +354,17 @@ 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
):
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 = {
Expand Down

0 comments on commit b134721

Please sign in to comment.