Skip to content

Commit

Permalink
No need for a method, just use a function
Browse files Browse the repository at this point in the history
  • Loading branch information
justvanrossum committed Nov 28, 2023
1 parent ce2ce94 commit 618399f
Showing 1 changed file with 46 additions and 47 deletions.
93 changes: 46 additions & 47 deletions src/fontra_rcjk/backend_fs.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,59 +122,13 @@ def _populateGlyphCache(self, glyphName):
for layerName, glifData in layerGLIFData:
layerGlyphs[layerName] = GLIFGlyph.fromGLIFData(glifData)

layerGlyphs = self._fudgeLayerNames(glyphName, layerGlyphs)
layerGlyphs = _fudgeLayerNames(glyphName, layerGlyphs)

self._tempGlyphCache[glyphName] = layerGlyphs

for compoName in layerGlyphs["foreground"].getComponentNames():
self._populateGlyphCache(compoName)

def _fudgeLayerNames(self, glyphName, layerGlyphs):
#
# The rcjk format does not play well with case-insensitive file systems:
# layer names become folder names, and to read layer names we read folder
# names. Since layer names are global per font, case differences in layer
# names can cause ambiguities. For example, if a layer "s2" exists, a
# folder named "s2" will be written. If "S2" (cap S!) *also* exists, its
# data will be happily written to the "s2" folder on a case-insensitive
# file system. When reading back the project, we find "s2", but not "S2".
# The code below tries to detect that situation and work around it. This
# works as long as the layer names *within a single glyph* are not
# ambiguous: "S1" and "s1" should not co-exist in the same glyph.
#
usedLayerNames = set()
for varData in layerGlyphs["foreground"].lib.get("robocjk.variationGlyphs", []):
layerName = varData.get("layerName")
if layerName:
usedLayerNames.add(layerName)
missingLayerNames = usedLayerNames - set(layerGlyphs)

if not missingLayerNames:
return layerGlyphs

if len(usedLayerNames) != len(
{layerName.casefold() for layerName in usedLayerNames}
):
logger.warn(
f"Possible layer name conflict on case-insensitive file system ({glyphName})"
)
return layerGlyphs

renameMap = {}
availableLayerNames = {
layerName.casefold(): layerName for layerName in layerGlyphs
}
for missingLayerName in missingLayerNames:
folded = missingLayerName.casefold()
fudged = availableLayerNames.get(folded)
if fudged:
renameMap[fudged] = missingLayerName
if renameMap:
logger.warn(f"fudging layer names for {glyphName}: {renameMap}")
layerGlyphs = {renameMap.get(k, k): v for k, v in layerGlyphs.items()}

return layerGlyphs

def _getLayerGLIFData(self, glyphName):
for gs, _ in self._iterGlyphSets():
if glyphName in gs:
Expand Down Expand Up @@ -347,3 +301,48 @@ def deleteGlyph(self, glyphName):
layerPath.unlink()
self.registerWrittenPath(layerPath, deleted=True)
del self.glyphMap[glyphName]


def _fudgeLayerNames(glyphName, layerGlyphs):
#
# The rcjk format does not play well with case-insensitive file systems:
# layer names become folder names, and to read layer names we read folder
# names. Since layer names are global per font, case differences in layer
# names can cause ambiguities. For example, if a layer "s2" exists, a
# folder named "s2" will be written. If "S2" (cap S!) *also* exists, its
# data will be happily written to the "s2" folder on a case-insensitive
# file system. When reading back the project, we find "s2", but not "S2".
# The code below tries to detect that situation and work around it. This
# works as long as the layer names *within a single glyph* are not
# ambiguous: "S1" and "s1" should not co-exist in the same glyph.
#
usedLayerNames = set()
for varData in layerGlyphs["foreground"].lib.get("robocjk.variationGlyphs", []):
layerName = varData.get("layerName")
if layerName:
usedLayerNames.add(layerName)
missingLayerNames = usedLayerNames - set(layerGlyphs)

if not missingLayerNames:
return layerGlyphs

if len(usedLayerNames) != len(
{layerName.casefold() for layerName in usedLayerNames}
):
logger.warn(
f"Possible layer name conflict on case-insensitive file system ({glyphName})"
)
return layerGlyphs

renameMap = {}
availableLayerNames = {layerName.casefold(): layerName for layerName in layerGlyphs}
for missingLayerName in missingLayerNames:
folded = missingLayerName.casefold()
fudged = availableLayerNames.get(folded)
if fudged:
renameMap[fudged] = missingLayerName
if renameMap:
logger.warn(f"fudging layer names for {glyphName}: {renameMap}")
layerGlyphs = {renameMap.get(k, k): v for k, v in layerGlyphs.items()}

return layerGlyphs

0 comments on commit 618399f

Please sign in to comment.