Skip to content

Commit

Permalink
Merge pull request #176 from googlefonts/features-infrastructure
Browse files Browse the repository at this point in the history
Adjust to OT features infrastructure
  • Loading branch information
justvanrossum authored Apr 30, 2024
2 parents 214214a + aed3e7e commit 6e25406
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 3 deletions.
30 changes: 29 additions & 1 deletion src/fontra_rcjk/backend_fs.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,14 @@

from fontra.backends.filewatcher import Change, FileWatcher
from fontra.backends.ufo_utils import extractGlyphNameAndCodePoints
from fontra.core.classes import Axes, Font, FontInfo, FontSource, VariableGlyph
from fontra.core.classes import (
Axes,
Font,
FontInfo,
FontSource,
OpenTypeFeatures,
VariableGlyph,
)
from fontra.core.instancer import mapLocationFromUserToSource
from fontra.core.protocols import WritableFontBackend
from fontTools.ufoLib.filenames import userNameToFileName
Expand All @@ -32,6 +39,7 @@

FILE_DELETED_TOKEN = object()
DS_FILENAME = "designspace.json"
FEA_FILENAME = "features.fea"
FONTLIB_FILENAME = "fontLib.json"


Expand Down Expand Up @@ -228,6 +236,26 @@ async def deleteGlyph(self, glyphName):

del self._glyphMap[glyphName]

async def getFeatures(self) -> OpenTypeFeatures:
featuresPath = self.path / FEA_FILENAME
featureText = (
featuresPath.read_text(encoding="utf-8") if featuresPath.is_file() else ""
)
return OpenTypeFeatures(text=featureText)

async def putFeatures(self, features: OpenTypeFeatures) -> None:
if features.language != "fea":
logger.warning(
f"skip writing features in unsupported language: {features.language!r}"
)
return

featuresPath = self.path / FEA_FILENAME
if features.text:
featuresPath.write_text(features.text, encoding="utf-8")
elif featuresPath.is_file():
featuresPath.unlink()

async def getCustomData(self) -> dict[str, Any]:
customData = {}
customDataPath = self.path / FONTLIB_FILENAME
Expand Down
20 changes: 20 additions & 0 deletions src/fontra_rcjk/backend_mysql.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
Font,
FontInfo,
FontSource,
OpenTypeFeatures,
VariableGlyph,
structure,
unstructure,
Expand Down Expand Up @@ -126,6 +127,9 @@ async def taskFunc():
self._tempFontItemsCache["designspace"] = structureDesignspaceData(
font_data["data"].get("designspace", {})
)
self._tempFontItemsCache["features"] = font_data["data"].get(
"features", ""
)
self._tempFontItemsCache["customData"] = (
font_data["data"].get("fontlib", {}) | standardCustomDataItems
)
Expand Down Expand Up @@ -195,6 +199,22 @@ async def putUnitsPerEm(self, value: int) -> None:
designspace.unitsPerEm = value
await self._writeDesignspace(designspace)

async def getFeatures(self) -> OpenTypeFeatures:
await self._getMiscFontItems()
featureText = self._tempFontItemsCache["features"]
return OpenTypeFeatures(text=featureText)

async def putFeatures(self, features: OpenTypeFeatures) -> None:
if features.language != "fea":
logger.warning(
f"skip writing features in unsupported language: {features.language!r}"
)
return

await self._getMiscFontItems()
self._tempFontItemsCache["features"] = features.text
_ = await self.client.font_update(self.fontUID, features=features.text)

async def getCustomData(self) -> dict[str, Any]:
customData = self._tempFontItemsCache.get("customData")
if customData is None:
Expand Down
4 changes: 2 additions & 2 deletions src/fontra_rcjk/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -544,8 +544,8 @@ def structureDesignspaceData(designspaceData: dict[str, Any]) -> Font:

def unstructureDesignspaceData(designspace: Font) -> dict[str, Any]:
designspaceData = unstructure(designspace)
del designspaceData["glyphs"]
del designspaceData["glyphMap"]
designspaceData.pop("glyphs", None)
designspaceData.pop("glyphMap", None)
return designspaceData


Expand Down
14 changes: 14 additions & 0 deletions tests/test_font.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
GlyphAxis,
GlyphSource,
Layer,
OpenTypeFeatures,
PackedPath,
StaticGlyph,
VariableGlyph,
Expand Down Expand Up @@ -1023,3 +1024,16 @@ async def test_putUnitsPerEm(writableTestFont):
assert 1000 == await writableTestFont.getUnitsPerEm()
await writableTestFont.putUnitsPerEm(2000)
assert 2000 == await writableTestFont.getUnitsPerEm()


async def test_getFeatures(writableTestFont):
font = getTestFont("rcjk")
features = await font.getFeatures()
assert "languagesystem DFLT dflt" in features.text


async def test_putFeatures(writableTestFont):
featureText = "# feature text"
async with contextlib.aclosing(writableTestFont):
await writableTestFont.putFeatures(OpenTypeFeatures(text=featureText))
assert (await writableTestFont.getFeatures()).text == featureText

0 comments on commit 6e25406

Please sign in to comment.