Skip to content

Instantly share code, notes, and snippets.

@typemytype
Last active August 20, 2018 08:31
Show Gist options
  • Save typemytype/ada7589bd2a564cefac681f45c3374e5 to your computer and use it in GitHub Desktop.
Save typemytype/ada7589bd2a564cefac681f45c3374e5 to your computer and use it in GitHub Desktop.
visual tests for pathops
import pathops
from booleanOperations.booleanOperationManager import BooleanOperationManager
import time
import math
print("pathops version:", pathops.__version__)
f = CurrentFont()
def allPoints(g, rounding=5):
points = []
for c in g:
for p in c.points:
points.append((math.floor(round(p.x, rounding)), math.floor(round(p.y, rounding))))
return points
def compareResults(g1, g2):
points1 = allPoints(g1)
points2 = allPoints(g2)
pointsCount1 = len(points1)
pointsCount2 = len(points2)
if len(g1) != len(g2):
smallest = min([len(g1), len(g2)])
smallestGlyph = g1
if len(g1) < len(g2):
smallestGlyph = g2
path = BezierPath()
for i in range(smallest):
smallestGlyph[i].draw(path)
fill(1, 0, 0, .3)
drawPath(path)
if pointsCount1 != pointsCount2:
oeps = set(points1) - set(points2)
oeps ^= set(points2) - set(points1)
fill(1, .5, 0, .3)
stroke(None)
s = 20
for x, y in oeps:
rect(x-s, y-s, s*2, s*2)
def previewGlyph(glyph):
start = 10
s = start * .5
stroke(None)
for contour in glyph:
points = contour.points
foundStartPoint = False
stroke(.5, 1, 0)
for bpoint in contour.bPoints:
x, y = bpoint.anchor
bcpIn = bpoint.bcpIn
bcpOut = bpoint.bcpOut
if bcpIn != (0, 0):
ix, iy = bcpIn
line((x, y), (x+ix, y+iy))
if bcpOut != (0, 0):
ox, oy = bcpOut
line((x, y), (x+ox, y+oy))
stroke(None)
for point in points:
if not foundStartPoint and point.type != "offcurve":
fill(1, 0, 0)
oval(point.x-start, point.y-start, start*2, start*2)
foundStartPoint = True
if point.type == "offcurve":
fill(.5, 1, 0)
else:
fill(1, 0, 1)
oval(point.x-s, point.y-s, s*2, s*2)
fill(0, 1, 0, .2)
stroke(0)
drawGlyph(glyph)
booleanTime = 0
skiaTime = 0
manager = BooleanOperationManager()
border = 50
for glyph in f:
bounds = glyph.bounds
if bounds is None:
continue
minx, miny, maxx, maxy = bounds
w = maxx - minx
h = maxy - miny
newPage(w*3+border*4, h+border*2)
text(glyph.name, (10, 10))
translate(-minx, -miny)
translate(border, border)
glyph = glyph.getLayer("foreground")
boolean = glyph.getLayer("booleanResult")
boolean.clear()
t = time.time()
manager.union(glyph, boolean.getPointPen())
booleanTime += time.time() - t
skia = glyph.getLayer("result")
skia.clear()
t = time.time()
pathops.union(glyph, skia.getPen())
skiaTime += time.time() - t
for g in glyph, boolean, skia:
previewGlyph(g)
translate(w, 0)
translate(border, 0)
fill(0)
translate(-border, 0)
translate(-w, 0)
compareResults(boolean, skia)
print(len(f))
print("booleanTime: %s" % booleanTime)
print("skiaTime: %s" % skiaTime)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment