Skip to content

Commit

Permalink
Merge pull request #57 from BlackFoundryCom/fix56
Browse files Browse the repository at this point in the history
Reproduce and fix recursion crash
  • Loading branch information
justvanrossum authored Nov 27, 2021
2 parents ed8f326 + 8d81d02 commit 2e7ff27
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
9 changes: 8 additions & 1 deletion Lib/blackrenderer/font.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ def drawGlyph(self, glyphName, canvas, *, palette=None, textColor=(0, 0, 0, 1)):
palette = self.palettes[0]
self.currentPalette = palette
self.textColor = textColor
self._recursionCheck = set()

glyph = self.colrV1Glyphs.get(glyphName)
if glyph is not None:
Expand All @@ -151,7 +152,13 @@ def _drawGlyphCOLRv0(self, layers, canvas):
canvas.drawPathSolid(path, self._getColor(layer.colorID, 1))

def _drawGlyphCOLRv1(self, glyph, canvas):
self._drawPaint(glyph.Paint, canvas)
if glyph.BaseGlyph in self._recursionCheck:
raise RecursionError(f"Glyph '{glyph.BaseGlyph}' references itself")
self._recursionCheck.add(glyph.BaseGlyph)
try:
self._drawPaint(glyph.Paint, canvas)
finally:
self._recursionCheck.remove(glyph.BaseGlyph)

# COLRv1 Paint dispatch

Expand Down
Binary file added Tests/data/crash.subset.otf
Binary file not shown.
14 changes: 14 additions & 0 deletions Tests/test_glyph_render.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"mutator": dataDir / "MutatorSans.ttf",
"twemoji": dataDir / "TwemojiMozilla.subset.default.3299.ttf",
"more_samples": dataDir / "more_samples-glyf_colr_1.ttf",
"crash": dataDir / "crash.subset.otf",
}


Expand Down Expand Up @@ -176,3 +177,16 @@ def test_vectorBackends(backendName, imageSuffix):
# assert expectedPath.read_bytes() == outputPath.read_bytes()
diff = compareImages(expectedPath, outputPath)
assert diff < 0.00012, diff


def test_recursive():
# https://github.com/BlackFoundryCom/black-renderer/issues/56
# https://github.com/justvanrossum/fontgoggles/issues/213
glyphName = "hah-ar"
font = BlackRendererFont(testFonts["crash"])
boundingBox = font.getGlyphBounds(glyphName)
surfaceClass = getSurfaceClass("svg", ".svg")
surface = surfaceClass()
with surface.canvas(boundingBox) as canvas:
with pytest.raises(RecursionError):
font.drawGlyph(glyphName, canvas)

0 comments on commit 2e7ff27

Please sign in to comment.