Skip to content

Commit

Permalink
Add type annotations, and handle invalid input better
Browse files Browse the repository at this point in the history
  • Loading branch information
justvanrossum committed Dec 16, 2023
1 parent f6e2e5f commit e5766b9
Showing 1 changed file with 23 additions and 11 deletions.
34 changes: 23 additions & 11 deletions src/fontra/core/clipboard.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,33 @@
from xml.etree.ElementTree import ParseError

from fontTools.pens.boundsPen import ControlBoundsPen
from fontTools.pens.pointPen import GuessSmoothPointPen, SegmentToPointPen
from fontTools.pens.recordingPen import RecordingPen
from fontTools.pens.transformPen import TransformPointPen
from fontTools.svgLib import SVGPath
from fontTools.ufoLib.errors import GlifLibError
from fontTools.ufoLib.glifLib import readGlyphFromString, writeGlyphToString

from ..backends.designspace import UFOGlyph, populateUFOLayerGlyph, readGlyphOrCreate
from .classes import StaticGlyph
from .path import PackedPathPointPen


def parseClipboard(data):
def parseClipboard(data: str) -> StaticGlyph | None:
if "<svg" in data:
return parseSVG(data)
if "<?xml" in data and "<glyph " in data:
return parseGLIF(data)
return None


def parseSVG(data):
data = data.encode("utf-8")
svgPath = SVGPath.fromstring(data, transform=(1, 0, 0, -1, 0, 0))
def parseSVG(data: str) -> StaticGlyph | None:
try:
svgPath = SVGPath.fromstring(
data.encode("utf-8"), transform=(1, 0, 0, -1, 0, 0)
)
except ParseError:
return None
recPen = RecordingPen()
svgPath.draw(recPen)
boundsPen = ControlBoundsPen(None)
Expand All @@ -36,20 +43,25 @@ def parseSVG(data):
return StaticGlyph(path=pen.getPath(), xAdvance=xMax)


def parseGLIF(data):
def parseGLIF(data: str) -> StaticGlyph | None:
pen = PackedPathPointPen()
ufoGlyph = UFOGlyph()
readGlyphFromString(
data,
glyphObject=ufoGlyph,
pointPen=pen,
)
try:
readGlyphFromString(
data,
glyphObject=ufoGlyph,
pointPen=pen,
)
except GlifLibError:
return None
return StaticGlyph(
path=pen.getPath(), components=pen.components, xAdvance=ufoGlyph.width
)


def serializeStaticGlyphAsGLIF(glyphName, staticGlyph, unicodes):
def serializeStaticGlyphAsGLIF(
glyphName: str, staticGlyph: StaticGlyph, unicodes: list[int]
) -> str:
layerGlyph = readGlyphOrCreate({}, glyphName, unicodes)
drawPointsFunc = populateUFOLayerGlyph(layerGlyph, staticGlyph)
return writeGlyphToString(glyphName, layerGlyph, drawPointsFunc, validate=False)

0 comments on commit e5766b9

Please sign in to comment.