Skip to content

Commit

Permalink
Merge pull request #155 from googlefonts/issue-1105-refactoring-rcjk
Browse files Browse the repository at this point in the history
Refactoring unicode to codePoint
  • Loading branch information
justvanrossum authored Mar 8, 2024
2 parents cad28c5 + ed78349 commit fc57f95
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 29 deletions.
24 changes: 12 additions & 12 deletions src/fontra_rcjk/backend_fs.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import watchfiles
from fontra.backends.designspace import cleanupWatchFilesChanges
from fontra.backends.ufo_utils import extractGlyphNameAndUnicodes
from fontra.backends.ufo_utils import extractGlyphNameAndCodePoints
from fontra.core.classes import (
GlobalAxis,
GlobalDiscreteAxis,
Expand Down Expand Up @@ -78,11 +78,11 @@ def __init__(self, path: PathLike, *, create: bool = False):
self._glyphMap: dict[str, list[int]] = {}
for gs, hasEncoding in self._iterGlyphSets():
glyphMap = gs.getGlyphMap(not hasEncoding)
for glyphName, unicodes in glyphMap.items():
for glyphName, codePoints in glyphMap.items():
assert glyphName not in self._glyphMap
if not hasEncoding:
assert not unicodes
self._glyphMap[glyphName] = unicodes
assert not codePoints
self._glyphMap[glyphName] = codePoints

self._recentlyWrittenPaths: dict[str, Any] = {}
self._tempGlyphCache = TimedCache()
Expand Down Expand Up @@ -184,7 +184,7 @@ def _getLayerGLIFData(self, glyphName):
return None

async def putGlyph(
self, glyphName: str, glyph: VariableGlyph, unicodes: list[int]
self, glyphName: str, glyph: VariableGlyph, codePoints: list[int]
) -> None:
if glyphName not in self._glyphMap:
existingLayerGlyphs = {}
Expand All @@ -194,11 +194,11 @@ async def putGlyph(
axis.name: axis.defaultValue for axis in glyph.axes
}
layerGlyphs = buildLayerGlyphsFromVariableGlyph(
glyphName, glyph, unicodes, localDefaultLocation, existingLayerGlyphs
glyphName, glyph, codePoints, localDefaultLocation, existingLayerGlyphs
)
glyphSet = self.getGlyphSetForGlyph(glyphName)
glyphSet.putGlyphLayerData(glyphName, layerGlyphs.items())
self._glyphMap[glyphName] = unicodes
self._glyphMap[glyphName] = codePoints
self._tempGlyphCache[glyphName] = layerGlyphs

async def deleteGlyph(self, glyphName):
Expand Down Expand Up @@ -276,17 +276,17 @@ def setupLayers(self):
if glifPaths:
self.layers[layerDir.name] = glifPaths

def getGlyphMap(self, ignoreUnicodes=False):
def getGlyphMap(self, ignoreCodePoints=False):
if self.glyphMap is None:
glyphMap = {}
for path in self.path.glob("*.glif"):
with open(path, "rb") as f:
# assuming all unicodes are in the first 1024 bytes of the file
data = f.read(1024)
glyphName, unicodes = extractGlyphNameAndUnicodes(data, path.name)
if ignoreUnicodes:
unicodes = []
glyphMap[glyphName] = unicodes
glyphName, codePoints = extractGlyphNameAndCodePoints(data, path.name)
if ignoreCodePoints:
codePoints = []
glyphMap[glyphName] = codePoints
self.contents[glyphName] = path
self.glifFileNames[path.name] = glyphName
self.glyphMap = glyphMap
Expand Down
26 changes: 13 additions & 13 deletions src/fontra_rcjk/backend_mysql.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ async def _ensureGlyphMapTask(self) -> None:
self._lastPolledForChanges = response["server_datetime"]
for typeCode, typeName in _glyphTypes:
for glyphInfo in response["data"][typeName]:
glyphMap[glyphInfo["name"]] = _unicodesFromGlyphInfo(glyphInfo)
glyphMap[glyphInfo["name"]] = _codePointsFromGlyphInfo(glyphInfo)
rcjkGlyphInfo[glyphInfo["name"]] = RCJKGlyphInfo(
typeCode, glyphInfo["id"], getUpdatedTimeStamp(glyphInfo)
)
Expand Down Expand Up @@ -272,23 +272,23 @@ async def putGlyph(
logger.info(f"Done writing {glyphName}")

async def _putGlyph(
self, glyphName: str, glyph: VariableGlyph, unicodes: list[int]
self, glyphName: str, glyph: VariableGlyph, codePoints: list[int]
) -> None:
defaultLocation = await self.getDefaultLocation()

if glyphName not in self._rcjkGlyphInfo:
await self._newGlyph(glyphName, unicodes)
await self._newGlyph(glyphName, codePoints)
existingLayerGlyphs = {}
else:
existingLayerGlyphs = await self._getLayerGlyphs(glyphName)

existingLayerData = {k: v.asGLIFData() for k, v in existingLayerGlyphs.items()}

layerGlyphs = buildLayerGlyphsFromVariableGlyph(
glyphName, glyph, unicodes, defaultLocation, existingLayerGlyphs
glyphName, glyph, codePoints, defaultLocation, existingLayerGlyphs
)

self._glyphMap[glyphName] = unicodes
self._glyphMap[glyphName] = codePoints

lockResponse = await self._callGlyphMethod(glyphName, "lock", return_data=False)

Expand Down Expand Up @@ -342,7 +342,7 @@ async def _putGlyph(
self._rcjkGlyphInfo[glyphName].updated = timeStamp
self._writeGlyphToCacheDir(glyphName, glyph)

async def _newGlyph(self, glyphName: str, unicodes: list[int]) -> None:
async def _newGlyph(self, glyphName: str, codePoints: list[int]) -> None:
# In _newGlyph() we create a new character glyph in the database.
# _putGlyph will immediately overwrite it with the real glyph data,
# with a lock acquired. Our dummy glyph has to have a glyph name, but
Expand All @@ -351,14 +351,14 @@ async def _newGlyph(self, glyphName: str, unicodes: list[int]) -> None:
logger.info(f"Creating new glyph '{glyphName}'")
dummyGlyph = GLIFGlyph()
dummyGlyph.name = glyphName
dummyGlyph.unicodes = unicodes
dummyGlyph.unicodes = codePoints
dummyGlyph.width = 314 # arbitrary positive value
xmlData = dummyGlyph.asGLIFData()
response = await self.client.character_glyph_create(
self.fontUID, xmlData, return_data=False
)
glyphID = response["data"]["id"]
self._glyphMap[glyphName] = unicodes
self._glyphMap[glyphName] = codePoints
timeStamp = getUpdatedTimeStamp(response["data"])
self._rcjkGlyphInfo[glyphName] = RCJKGlyphInfo("CG", glyphID, timeStamp)
self._glyphTimeStamps[glyphName] = timeStamp
Expand Down Expand Up @@ -491,10 +491,10 @@ async def _pollOnceForChanges(self) -> tuple[Any, Any]:
typeCode, glyphInfo["id"], glyphUpdatedAt
)

unicodes = _unicodesFromGlyphInfo(glyphInfo)
if unicodes != self._glyphMap.get(glyphName):
self._glyphMap[glyphName] = unicodes
glyphMapUpdates[glyphName] = unicodes
codePoints = _codePointsFromGlyphInfo(glyphInfo)
if codePoints != self._glyphMap.get(glyphName):
self._glyphMap[glyphName] = codePoints
glyphMapUpdates[glyphName] = codePoints

glyphNames.add(glyphName)
self._glyphCache.pop(glyphName, None)
Expand All @@ -509,7 +509,7 @@ async def _pollOnceForChanges(self) -> tuple[Any, Any]:
return externalChange, reloadPattern


def _unicodesFromGlyphInfo(glyphInfo: dict) -> list[int]:
def _codePointsFromGlyphInfo(glyphInfo: dict) -> list[int]:
return glyphInfo.get("unicodes", [])


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 @@ -254,7 +254,7 @@ def convertTransformation(rcjkTransformation):


def buildLayerGlyphsFromVariableGlyph(
glyphName, glyph, unicodes, defaultLocation, existingLayerGlyphs
glyphName, glyph, codePoints, defaultLocation, existingLayerGlyphs
):
fontraLayerNameMapping = {}
defaultLayerName = None
Expand Down Expand Up @@ -284,7 +284,7 @@ def buildLayerGlyphsFromVariableGlyph(
fontraLayerNameMapping[safeLayerName] = layerName

layerGlyphs[layerName] = layerGlyph
layerGlyphs[layerName].unicodes = unicodes
layerGlyphs[layerName].unicodes = codePoints
defaultGlyph = layerGlyphs["foreground"]

if glyph.axes:
Expand Down
4 changes: 2 additions & 2 deletions tests/test_font.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,8 +389,8 @@ async def test_getGlyphMap(backendName, numGlyphs, testMapping):
with contextlib.closing(font):
glyphMap = await font.getGlyphMap()
assert numGlyphs == len(glyphMap)
for glyphName, unicodes in testMapping.items():
assert glyphMap[glyphName] == unicodes
for glyphName, codePoints in testMapping.items():
assert glyphMap[glyphName] == codePoints


@pytest.mark.asyncio
Expand Down

0 comments on commit fc57f95

Please sign in to comment.