Skip to content

Commit

Permalink
Merge pull request #34 from googlefonts/sources-and-font-info
Browse files Browse the repository at this point in the history
Add some support for sources and font info
  • Loading branch information
justvanrossum authored Mar 18, 2024
2 parents a2cd7ef + 8962287 commit 7f0cab3
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 1 deletion.
40 changes: 40 additions & 0 deletions src/fontra_glyphs/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
import openstep_plist
from fontra.core.classes import (
Component,
FontInfo,
GlobalAxis,
GlobalDiscreteAxis,
GlobalSource,
Layer,
LocalAxis,
Source,
Expand All @@ -21,6 +23,26 @@
from glyphsLib.builder.axes import get_axis_definitions, to_designspace_axes
from glyphsLib.builder.smart_components import Pole

rootInfoNames = [
"familyName",
"versionMajor",
"versionMinor",
]


infoNamesMapping = [
# (Fontra, Glyphs)
("copyright", "copyrights"),
("designer", "designers"),
("designerURL", "designerURL"),
("licenseDescription", "licenses"),
# ("licenseInfoURL", "licensesURL"), # Not defined in glyphsLib
("manufacturer", "manufacturers"),
("manufacturerURL", "manufacturerURL"),
("trademark", "trademarks"),
("vendorID", "vendorID"),
]


class GlyphsBackend:
@classmethod
Expand Down Expand Up @@ -97,6 +119,24 @@ def _loadFiles(path: PathLike) -> tuple[dict[str, Any], list[Any]]:
async def getGlyphMap(self) -> dict[str, list[int]]:
return self.glyphMap

async def getFontInfo(self) -> FontInfo:
infoDict = {}
for name in rootInfoNames:
value = getattr(self.gsFont, name, None)
if value is not None:
infoDict[name] = value

properties = {p.key: p.value for p in self.gsFont.properties}
for fontraName, glyphsName in infoNamesMapping:
value = properties.get(glyphsName)
if value is not None:
infoDict[fontraName] = value

return FontInfo(**infoDict)

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

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

Expand Down
27 changes: 26 additions & 1 deletion tests/test_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import pytest
from fontra.backends import getFileSystemBackend
from fontra.core.classes import GlobalAxis, structure, unstructure
from fontra.core.classes import FontInfo, GlobalAxis, structure, unstructure
from fontTools.misc.filenames import userNameToFileName

dataDir = pathlib.Path(__file__).resolve().parent / "data"
Expand Down Expand Up @@ -73,6 +73,31 @@ async def test_glyphMap(testFont):
assert expectedGlyphMap == glyphMap


expectedFontInfo = FontInfo(
familyName="Glyphs Unit Test Sans",
versionMajor=1,
versionMinor=0,
copyright=None,
trademark=None,
description=None,
sampleText=None,
designer=None,
designerURL=None,
manufacturer=None,
manufacturerURL=None,
licenseDescription=None,
licenseInfoURL=None,
vendorID=None,
customData={},
)


@pytest.mark.asyncio
async def test_fontInfo(testFont):
fontInfo = await testFont.getFontInfo()
assert expectedFontInfo == fontInfo


@pytest.mark.asyncio
@pytest.mark.parametrize("glyphName", list(expectedGlyphMap))
async def test_glyphRead(testFont, glyphName):
Expand Down

0 comments on commit 7f0cab3

Please sign in to comment.