| 1 |
|
| 2 |
|
| 3 |
import fontforge |
| 4 |
|
| 5 |
co = fontforge.splineCorner |
| 6 |
cu = fontforge.splineCurve |
| 7 |
tn = fontforge.splineTangent |
| 8 |
hv = fontforge.splineHVCurve |
| 9 |
|
| 10 |
ff = fontforge.font() |
| 11 |
|
| 12 |
g = ff.createMappedChar('a') |
| 13 |
|
| 14 |
cl = [(0,0,True,hv),(0,0,False),(5,-1,False),(5,0,True,hv),(5,1,False),(5,3,False),(4,4,True,hv),(3,5,False),(0,3,False),(0,2,True,hv)] |
| 15 |
c = fontforge.contour() |
| 16 |
c[:] = cl |
| 17 |
c.closed = True |
| 18 |
|
| 19 |
l = fontforge.layer() |
| 20 |
l += c |
| 21 |
|
| 22 |
g.setLayer(l,1,('select_none','hvcurve')) |
| 23 |
cc = g.layers[1][0] |
| 24 |
for (i,t) in [(0,hv),(3,hv),(6,hv),(9,hv)]: |
| 25 |
if cc[i].type != t: |
| 26 |
raise ValueError("Type of point " + str(i) + " should be " + str(t) + " rather than " + str(cc[i].type)) |
| 27 |
|
| 28 |
g.setLayer(l,1,('select_all','hvcurve','force')) |
| 29 |
cc = g.layers[1][0] |
| 30 |
for (i,t) in [(1,False),(2,True),(4,True),(5,False),(7,False),(8,False)]: |
| 31 |
if (cc[i] == c[i]) != t: |
| 32 |
print(cc[i],c[i]) |
| 33 |
raise ValueError("Coordinates of point " + str(i) + " should have " + ("stayed the same" if t else "changed")) |
| 34 |
|
| 35 |
c[9].type = tn |
| 36 |
g.setLayer(l,1,('select_all','hvcurve','downgrade')) |
| 37 |
cc = g.layers[1][0] |
| 38 |
for (i,t) in [(0,co),(3,hv),(6,cu),(9,tn)]: |
| 39 |
if cc[i].type != t: |
| 40 |
raise ValueError("Type of point " + str(i) + " should be " + str(t) + " rather than " + str(cc[i].type)) |
| 41 |
|
| 42 |
c[9].type = cu |
| 43 |
c[3].type = tn |
| 44 |
c[6].type = co |
| 45 |
g.setLayer(l,1,('select_incompat','hvcurve','by_geom')) |
| 46 |
cc = g.layers[1][0] |
| 47 |
for (i,t) in [(0,co),(3,hv),(6,co),(9,tn)]: |
| 48 |
if cc[i].type != t: |
| 49 |
raise ValueError("Type of point " + str(i) + " should be " + str(t) + " rather than " + str(cc[i].type)) |
| 50 |
|
| 51 |
c[3].type = cu |
| 52 |
c[9].type = cu |
| 53 |
g.setLayer(l,1,('select_smooth','hvcurve','by_geom')) |
| 54 |
cc = g.layers[1][0] |
| 55 |
for (i,t) in [(0,co),(3,hv),(6,co),(9,tn)]: |
| 56 |
if cc[i].type != t: |
| 57 |
raise ValueError("Type of point " + str(i) + " should be " + str(t) + " rather than " + str(cc[i].type)) |
| 58 |
|
| 59 |
c[3].type = cu |
| 60 |
c[9].type = cu |
| 61 |
g.setLayer(l,1,('select_smooth','by_geom')) |
| 62 |
cc = g.layers[1][0] |
| 63 |
for (i,t) in [(0,co),(3,cu),(6,co),(9,tn)]: |
| 64 |
if cc[i].type != t: |
| 65 |
raise ValueError("Type of point " + str(i) + " should be " + str(t) + " rather than " + str(cc[i].type)) |
| 66 |
|
| 67 |
c[0].type = c[3].type = c[6].type = c[9].type = co |
| 68 |
g.setLayer(l,1,('select_all','by_geom')) |
| 69 |
cc = g.layers[1][0] |
| 70 |
for (i,t) in [(0,co),(3,cu),(6,cu),(9,tn)]: |
| 71 |
if cc[i].type != t: |
| 72 |
raise ValueError("Type of point " + str(i) + " should be " + str(t) + " rather than " + str(cc[i].type)) |
| 73 |
|
| 74 |
c[3].type = hv |
| 75 |
try: |
| 76 |
g.setLayer(l,1,('select_all','check')) |
| 77 |
except: |
| 78 |
raise ValueError("setLayer with 'check' failed when all types compatible with geometry") |
| 79 |
|
| 80 |
c[3].type = tn |
| 81 |
try: |
| 82 |
g.setLayer(l,1,('select_all','check')) |
| 83 |
except: |
| 84 |
pass |
| 85 |
else: |
| 86 |
raise ValueError("setLayer with 'check' did not raise error for incompatible type") |
| 87 |
|
| 88 |
try: |
| 89 |
g.setLayer(l,1,('select_all','select_none','by_geom')) |
| 90 |
except: |
| 91 |
pass |
| 92 |
else: |
| 93 |
raise ValueError("setLayer did not raise error for incompatible select flags") |
| 94 |
|
| 95 |
try: |
| 96 |
g.setLayer(l,1,('select_all','check','by_geom')) |
| 97 |
except: |
| 98 |
pass |
| 99 |
else: |
| 100 |
raise ValueError("setLayer did not raise error for incompatible conversion mode flags") |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
c2 = fontforge.contour() |
| 105 |
c3 = c2.simplify(1) |
| 106 |
|
| 107 |
c2[:] = [(0,0,True)] |
| 108 |
c3 = c2.simplify(1) |
| 109 |
|
| 110 |
c2[:] = [(0,0,False)] |
| 111 |
try: |
| 112 |
c3 = c2.simplify(1) |
| 113 |
except: |
| 114 |
pass |
| 115 |
else: |
| 116 |
raise ValueError("No error on round-trip for invalid contour") |
| 117 |
|
| 118 |
c2[:] = [(0,0,False),(1,1,False),(2,2,False),(3,3,True)] |
| 119 |
try: |
| 120 |
c3 = c2.simplify(1) |
| 121 |
except: |
| 122 |
pass |
| 123 |
else: |
| 124 |
raise ValueError("No error on round-trip for invalid contour") |
| 125 |
|
| 126 |
l = fontforge.layer() |
| 127 |
c[:] = cl |
| 128 |
l += c |
| 129 |
c2[:] = [] |
| 130 |
l += c2 |
| 131 |
g.setLayer(l,1) |
| 132 |
l2 = g.layers[1] |
| 133 |
if len(l) != 2 or len(l2) != 1: |
| 134 |
raise ValueError("Expected setLayer to drop empty contour.") |
| 135 |
|
| 136 |
l.correctDirection() |
| 137 |
if len(l) != 1: |
| 138 |
raise ValueError("Expected round trip to drop empty contour.") |
| 139 |
|
| 140 |
l += c2 |
| 141 |
c2[:] = [(0,0,True)] |
| 142 |
g.setLayer(l,1) |
| 143 |
l2 = g.layers[1] |
| 144 |
if len(l) != 2 or len(l2) != 2: |
| 145 |
raise ValueError("Setlayer lost open contour.") |
| 146 |
|
| 147 |
c2[:] = [(0,0,False)] |
| 148 |
try: |
| 149 |
l.correctDirection() |
| 150 |
except: |
| 151 |
pass |
| 152 |
else: |
| 153 |
raise ValueError("No error on round-trip for layer with invalid contour") |
| 154 |
|
| 155 |
p = l[1][0] |
| 156 |
if len(l) != 2 or p.x != 0 or p.y != 0 or p.on_curve: |
| 157 |
raise ValueError("Contours not retained on layer error.") |