Skip to content

Commit

Permalink
Add typing, add missing 'put' methods
Browse files Browse the repository at this point in the history
  • Loading branch information
justvanrossum committed Dec 12, 2023
1 parent ee571bb commit d360a95
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 16 deletions.
21 changes: 17 additions & 4 deletions src/fontra/backends/designspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,12 @@ def updateGlyphSetContents(self, glyphSet):
for glyphName, fileName in glyphSet.contents.items():
glifFileNames[fileName] = glyphName

async def getGlyphMap(self):
async def getGlyphMap(self) -> dict[str, list[int]]:
return dict(self.glyphMap)

async def putGlyphMap(self, value: dict[str, list[int]]) -> None:
pass

async def getGlyph(self, glyphName: str) -> VariableGlyph | None:
if glyphName not in self.glyphMap:
return None
Expand Down Expand Up @@ -538,7 +541,7 @@ async def deleteGlyph(self, glyphName):
del self.glyphMap[glyphName]
self.savedGlyphModificationTimes[glyphName] = None

async def getGlobalAxes(self):
async def getGlobalAxes(self) -> list[GlobalAxis | GlobalDiscreteAxis]:
return self.axes

async def putGlobalAxes(self, axes):
Expand All @@ -561,10 +564,20 @@ async def putGlobalAxes(self, axes):
self.updateAxisInfo()
self.loadUFOLayers()

async def getUnitsPerEm(self):
async def getUnitsPerEm(self) -> int:
return self.defaultFontInfo.unitsPerEm

async def getCustomData(self):
async def putUnitsPerEm(self, value: int) -> None:
del self.defaultFontInfo
ufoPaths = sorted(set(self.ufoLayers.iterAttrs("path")))
for ufoPath in ufoPaths:
reader = self.ufoManager.getReader(ufoPath)
info = UFOFontInfo()
reader.readInfo(info)
info.unitsPerEm = value
reader.writeInfo(info)

async def getCustomData(self) -> dict[str, Any]:
return deepcopy(self.dsDoc.lib)

async def putCustomData(self, lib):
Expand Down
24 changes: 17 additions & 7 deletions src/fontra/backends/fontra.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,16 @@
from copy import deepcopy
from dataclasses import dataclass, field
from os import PathLike
from typing import Callable

from fontra.core.classes import Font, VariableGlyph, structure, unstructure
from typing import Any, Callable

from fontra.core.classes import (
Font,
GlobalAxis,
GlobalDiscreteAxis,
VariableGlyph,
structure,
unstructure,
)
from fontra.core.protocols import WritableFontBackend

from .filenames import stringToFileName
Expand Down Expand Up @@ -66,16 +73,19 @@ def close(self):
def flush(self):
self._scheduler.flush()

async def getUnitsPerEm(self):
async def getUnitsPerEm(self) -> int:
return self.fontData.unitsPerEm

async def putUnitsPerEm(self, unitsPerEm):
self.fontData.unitsPerEm = unitsPerEm
self._scheduler.schedule(self._writeFontData)

async def getGlyphMap(self):
async def getGlyphMap(self) -> dict[str, list[int]]:
return dict(self.glyphMap)

async def putGlyphMap(self, value: dict[str, list[int]]) -> None:
pass

async def getGlyph(self, glyphName: str) -> VariableGlyph | None:
try:
jsonSource = self.getGlyphData(glyphName)
Expand All @@ -95,14 +105,14 @@ async def putGlyph(
async def deleteGlyph(self, glyphName):
self.glyphMap.pop(glyphName, None)

async def getGlobalAxes(self):
async def getGlobalAxes(self) -> list[GlobalAxis | GlobalDiscreteAxis]:
return deepcopy(self.fontData.axes)

async def putGlobalAxes(self, axes):
self.fontData.axes = deepcopy(axes)
self._scheduler.schedule(self._writeFontData)

async def getCustomData(self):
async def getCustomData(self) -> dict[str, Any]:
return deepcopy(self.fontData.customData)

async def putCustomData(self, customData):
Expand Down
18 changes: 13 additions & 5 deletions src/fontra/backends/opentype.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
from os import PathLike
from typing import Any

from fontTools.misc.psCharStrings import SimpleT2Decompiler
from fontTools.pens.pointPen import GuessSmoothPointPen
from fontTools.ttLib import TTFont

from fontra.core.protocols import ReadableFontBackend

from ..core.classes import GlobalAxis, Layer, Source, StaticGlyph, VariableGlyph
from ..core.classes import (
GlobalAxis,
GlobalDiscreteAxis,
Layer,
Source,
StaticGlyph,
VariableGlyph,
)
from ..core.path import PackedPath, PackedPathPointPen


Expand Down Expand Up @@ -39,7 +47,7 @@ def __init__(self, *, path):
def close(self):
self.font.close()

async def getGlyphMap(self):
async def getGlyphMap(self) -> dict[str, list[int]]:
return self.glyphMap

async def getGlyph(self, glyphName: str) -> VariableGlyph | None:
Expand Down Expand Up @@ -100,13 +108,13 @@ def _getGlyphVariationLocations(self, glyphName):
}
return [dict(loc) for loc in sorted(locations)]

async def getGlobalAxes(self):
async def getGlobalAxes(self) -> list[GlobalAxis | GlobalDiscreteAxis]:
return self.globalAxes

async def getUnitsPerEm(self):
async def getUnitsPerEm(self) -> int:
return self.font["head"].unitsPerEm

async def getCustomData(self):
async def getCustomData(self) -> dict[str, Any]:
return {}


Expand Down

0 comments on commit d360a95

Please sign in to comment.