Debian Codegraph @

Lib/fontMath/test/test_mathGlyph.py

raw symbols SCIP
1 from __future__ import division
2 import unittest
3 from fontTools.pens.pointPen import AbstractPointPen
4 from fontMath.mathFunctions import addPt, mulPt
5 from fontMath.mathGlyph import (
6 MathGlyph, MathGlyphPen, FilterRedundantPointPen,
7 _processMathOneContours, _processMathTwoContours, _anchorTree,
8 _pairAnchors, _processMathOneAnchors, _processMathTwoAnchors,
9 _pairComponents, _processMathOneComponents, _processMathTwoComponents,
10 _expandImage, _compressImage, _pairImages, _processMathOneImage,
11 _processMathTwoImage, _processMathOneTransformation,
12 _processMathTwoTransformation, _roundContours, _roundTransformation,
13 _roundImage, _roundComponents, _roundAnchors
14 )
15
16
17 try:
18 basestring, xrange
19 range = xrange
20 except NameError:
21 basestring = str
22
23
24 class MathGlyphTest(unittest.TestCase):
25 def __init__(self, methodName):
26 unittest.TestCase.__init__(self, methodName)
27
28 def _setupTestGlyph(self):
29 glyph = MathGlyph(None)
30 glyph.width = 0
31 glyph.height = 0
32 return glyph
33
34 def test__eq__(self):
35 glyph1 = self._setupTestGlyph()
36 glyph2 = self._setupTestGlyph()
37 self.assertEqual(glyph1, glyph2)
38
39 glyph2.width = 1
40 self.assertFalse(glyph1 == glyph2)
41
42 nonglyph = object()
43 self.assertFalse(glyph1 == nonglyph)
44
45 glyph1 = MathGlyph(None)
46 glyph1.name = 'space'
47 glyph1.width = 100
48
49 class MyGlyph(object):
50 pass
51 other = MyGlyph()
52 other.name = 'space'
53 other.width = 100
54 other.height = None
55 other.contours = []
56 other.components = []
57 other.anchors = []
58 other.guidelines = []
59 other.image = {'fileName': None,
60 'transformation': (1, 0, 0, 1, 0, 0),
61 'color': None}
62 other.lib = {}
63 other.unicodes = None
64 other.note = None
65 self.assertEqual(glyph1, other)
66
67 def test__ne__(self):
68 glyph1 = self._setupTestGlyph()
69 glyph2 = MathGlyph(None)
70 glyph2.width = 1
71 glyph2.name = 'a'
72 self.assertNotEqual(glyph1, glyph2)
73 self.assertNotEqual(glyph1, 'foo')
74
75 def test_width_add(self):
76 glyph1 = self._setupTestGlyph()
77 glyph1.width = 1
78 glyph2 = self._setupTestGlyph()
79 glyph2.width = 2
80 glyph3 = glyph1 + glyph2
81 self.assertEqual(glyph3.width, 3)
82
83 def test_width_sub(self):
84 glyph1 = self._setupTestGlyph()
85 glyph1.width = 3
86 glyph2 = self._setupTestGlyph()
87 glyph2.width = 2
88 glyph3 = glyph1 - glyph2
89 self.assertEqual(glyph3.width, 1)
90
91 def test_width_mul(self):
92 glyph1 = self._setupTestGlyph()
93 glyph1.width = 2
94 glyph2 = glyph1 * 3
95 self.assertEqual(glyph2.width, 6)
96 glyph1 = self._setupTestGlyph()
97 glyph1.width = 2
98 glyph2 = glyph1 * (3, 1)
99 self.assertEqual(glyph2.width, 6)
100
101 def test_width_div(self):
102 glyph1 = self._setupTestGlyph()
103 glyph1.width = 7
104 glyph2 = glyph1 / 2
105 self.assertEqual(glyph2.width, 3.5)
106 glyph1 = self._setupTestGlyph()
107 glyph1.width = 7
108 glyph2 = glyph1 / (2, 1)
109 self.assertEqual(glyph2.width, 3.5)
110
111 def test_width_round(self):
112 glyph1 = self._setupTestGlyph()
113 glyph1.width = 6.99
114 glyph2 = glyph1.round()
115 self.assertEqual(glyph2.width, 7)
116
117 def test_height_add(self):
118 glyph1 = self._setupTestGlyph()
119 glyph1.height = 1
120 glyph2 = self._setupTestGlyph()
121 glyph2.height = 2
122 glyph3 = glyph1 + glyph2
123 self.assertEqual(glyph3.height, 3)
124
125 def test_height_sub(self):
126 glyph1 = self._setupTestGlyph()
127 glyph1.height = 3
128 glyph2 = self._setupTestGlyph()
129 glyph2.height = 2
130 glyph3 = glyph1 - glyph2
131 self.assertEqual(glyph3.height, 1)
132
133 def test_height_mul(self):
134 glyph1 = self._setupTestGlyph()
135 glyph1.height = 2
136 glyph2 = glyph1 * 3
137 self.assertEqual(glyph2.height, 6)
138 glyph1 = self._setupTestGlyph()
139 glyph1.height = 2
140 glyph2 = glyph1 * (1, 3)
141 self.assertEqual(glyph2.height, 6)
142
143 def test_height_div(self):
144 glyph1 = self._setupTestGlyph()
145 glyph1.height = 7
146 glyph2 = glyph1 / 2
147 self.assertEqual(glyph2.height, 3.5)
148 glyph1 = self._setupTestGlyph()
149 glyph1.height = 7
150 glyph2 = glyph1 / (1, 2)
151 self.assertEqual(glyph2.height, 3.5)
152
153 def test_height_round(self):
154 glyph1 = self._setupTestGlyph()
155 glyph1.height = 6.99
156 glyph2 = glyph1.round()
157 self.assertEqual(glyph2.height, 7)
158
159 def test_contours_add(self):
160 glyph1 = self._setupTestGlyph()
161 glyph1.contours = [
162 dict(identifier="contour 1",
163 points=[("line", (0.55, 3.1), False, "test", "1")]),
164 dict(identifier="contour 1",
165 points=[("line", (0.55, 3.1), True, "test", "1")])
166 ]
167 glyph2 = self._setupTestGlyph()
168 glyph2.contours = [
169 dict(identifier="contour 1",
170 points=[("line", (1.55, 4.1), False, "test", "1")]),
171 dict(identifier="contour 1",
172 points=[("line", (1.55, 4.1), True, "test", "1")])
173 ]
174 glyph3 = glyph1 + glyph2
175 expected = [
176 dict(identifier="contour 1",
177 points=[("line", (0.55 + 1.55, 3.1 + 4.1), False, "test",
178 "1")]),
179 dict(identifier="contour 1",
180 points=[("line", (0.55 + 1.55, 3.1 + 4.1), True, "test",
181 "1")])
182 ]
183 self.assertEqual(glyph3.contours, expected)
184
185 def test_contours_sub(self):
186 glyph1 = self._setupTestGlyph()
187 glyph1.contours = [
188 dict(identifier="contour 1",
189 points=[("line", (0.55, 3.1), False, "test", "1")]),
190 dict(identifier="contour 1",
191 points=[("line", (0.55, 3.1), True, "test", "1")])
192 ]
193 glyph2 = self._setupTestGlyph()
194 glyph2.contours = [
195 dict(identifier="contour 1",
196 points=[("line", (1.55, 4.1), False, "test", "1")]),
197 dict(identifier="contour 1",
198 points=[("line", (1.55, 4.1), True, "test", "1")])
199 ]
200 glyph3 = glyph1 - glyph2
201 expected = [
202 dict(identifier="contour 1",
203 points=[("line", (0.55 - 1.55, 3.1 - 4.1), False, "test",
204 "1")]),
205 dict(identifier="contour 1",
206 points=[("line", (0.55 - 1.55, 3.1 - 4.1), True, "test",
207 "1")])
208 ]
209 self.assertEqual(glyph3.contours, expected)
210
211 def test_contours_mul(self):
212 glyph1 = self._setupTestGlyph()
213 glyph1.contours = [
214 dict(identifier="contour 1",
215 points=[("line", (0.55, 3.1), False, "test", "1")]),
216 dict(identifier="contour 1",
217 points=[("line", (0.55, 3.1), True, "test", "1")])
218 ]
219 glyph2 = glyph1 * 2
220 expected = [
221 dict(identifier="contour 1",
222 points=[("line", (0.55 * 2, 3.1 * 2), False, "test",
223 "1")]),
224 dict(identifier="contour 1",
225 points=[("line", (0.55 * 2, 3.1 * 2), True, "test",
226 "1")])
227 ]
228 self.assertEqual(glyph2.contours, expected)
229
230 def test_contours_div(self):
231 glyph1 = self._setupTestGlyph()
232 glyph1.contours = [
233 dict(identifier="contour 1",
234 points=[("line", (1, 3.4), False, "test", "1")]),
235 dict(identifier="contour 1",
236 points=[("line", (2, 3.1), True, "test", "1")])
237 ]
238 glyph2 = glyph1 / 2
239 expected = [
240 dict(identifier="contour 1",
241 points=[("line", (1/2, 3.4/2), False, "test",
242 "1")]),
243 dict(identifier="contour 1",
244 points=[("line", (2/2, 3.1/2), True, "test",
245 "1")])
246 ]
247 self.assertEqual(glyph2.contours, expected)
248
249 def test_contours_round(self):
250 glyph1 = self._setupTestGlyph()
251 glyph1.contours = [
252 dict(identifier="contour 1",
253 points=[("line", (0.55, 3.1), False, "test", "1")]),
254 dict(identifier="contour 1",
255 points=[("line", (0.55, 3.1), True, "test", "1")])
256 ]
257 glyph2 = glyph1.round()
258 expected = [
259 dict(identifier="contour 1",
260 points=[("line", (1, 3), False, "test", "1")]),
261 dict(identifier="contour 1",
262 points=[("line", (1, 3), True, "test", "1")])
263 ]
264 self.assertEqual(glyph2.contours, expected)
265
266 def test_components_round(self):
267 glyph1 = self._setupTestGlyph()
268 glyph1.components = [
269 dict(baseGlyph="A", transformation=(1, 2, 3, 4, 5.1, 5.99),
270 identifier="1"),
271 ]
272 glyph2 = glyph1.round()
273 expected = [
274 dict(baseGlyph="A", transformation=(1, 2, 3, 4, 5, 6),
275 identifier="1")
276 ]
277 self.assertEqual(glyph2.components, expected)
278
279 def test_guidelines_add_same_name_identifier_x_y_angle(self):
280 glyph1 = self._setupTestGlyph()
281 glyph1.guidelines = [
282 dict(name="foo", identifier="1", x=1, y=2, angle=1),
283 dict(name="foo", identifier="2", x=3, y=4, angle=2)
284 ]
285 glyph2 = self._setupTestGlyph()
286 glyph2.guidelines = [
287 dict(name="foo", identifier="2", x=3, y=4, angle=2),
288 dict(name="foo", identifier="1", x=1, y=2, angle=1)
289 ]
290 expected = [
291 dict(name="foo", identifier="1", x=2, y=4, angle=2),
292 dict(name="foo", identifier="2", x=6, y=8, angle=4)
293 ]
294 glyph3 = glyph1 + glyph2
295 self.assertEqual(glyph3.guidelines, expected)
296
297 def test_guidelines_add_same_name_identifier(self):
298 glyph1 = self._setupTestGlyph()
299 glyph1.guidelines = [
300 dict(name="foo", identifier="1", x=1, y=2, angle=1),
301 dict(name="foo", identifier="2", x=1, y=2, angle=2),
302 ]
303 glyph2 = self._setupTestGlyph()
304 glyph2.guidelines = [
305 dict(name="foo", identifier="2", x=3, y=4, angle=3),
306 dict(name="foo", identifier="1", x=3, y=4, angle=4)
307 ]
308 expected = [
309 dict(name="foo", identifier="1", x=4, y=6, angle=5),
310 dict(name="foo", identifier="2", x=4, y=6, angle=5)
311 ]
312 glyph3 = glyph1 + glyph2
313 self.assertEqual(glyph3.guidelines, expected)
314
315 def test_guidelines_add_same_name_x_y_angle(self):
316 glyph1 = self._setupTestGlyph()
317 glyph1.guidelines = [
318 dict(name="foo", identifier="1", x=1, y=2, angle=1),
319 dict(name="foo", identifier="2", x=3, y=4, angle=2),
320 ]
321 glyph2 = self._setupTestGlyph()
322 glyph2.guidelines = [
323 dict(name="foo", identifier="3", x=3, y=4, angle=2),
324 dict(name="foo", identifier="4", x=1, y=2, angle=1)
325 ]
326 expected = [
327 dict(name="foo", identifier="1", x=2, y=4, angle=2),
328 dict(name="foo", identifier="2", x=6, y=8, angle=4)
329 ]
330 glyph3 = glyph1 + glyph2
331 self.assertEqual(glyph3.guidelines, expected)
332
333 def test_guidelines_add_same_identifier_x_y_angle(self):
334 glyph1 = self._setupTestGlyph()
335 glyph1.guidelines = [
336 dict(name="foo", identifier="1", x=1, y=2, angle=1),
337 dict(name="bar", identifier="2", x=3, y=4, angle=2),
338 ]
339 glyph2 = self._setupTestGlyph()
340 glyph2.guidelines = [
341 dict(name="xxx", identifier="2", x=3, y=4, angle=2),
342 dict(name="yyy", identifier="1", x=1, y=2, angle=1)
343 ]
344 expected = [
345 dict(name="foo", identifier="1", x=2, y=4, angle=2),
346 dict(name="bar", identifier="2", x=6, y=8, angle=4)
347 ]
348 glyph3 = glyph1 + glyph2
349 self.assertEqual(glyph3.guidelines, expected)
350
351 def test_guidelines_add_same_name(self):
352 glyph1 = self._setupTestGlyph()
353 glyph1.guidelines = [
354 dict(name="foo", identifier="1", x=1, y=2, angle=1),
355 dict(name="bar", identifier="2", x=1, y=2, angle=2),
356 ]
357 glyph2 = self._setupTestGlyph()
358 glyph2.guidelines = [
359 dict(name="bar", identifier="3", x=3, y=4, angle=3),
360 dict(name="foo", identifier="4", x=3, y=4, angle=4)
361 ]
362 expected = [
363 dict(name="foo", identifier="1", x=4, y=6, angle=5),
364 dict(name="bar", identifier="2", x=4, y=6, angle=5)
365 ]
366 glyph3 = glyph1 + glyph2
367 self.assertEqual(glyph3.guidelines, expected)
368
369 def test_guidelines_add_same_identifier(self):
370 glyph1 = self._setupTestGlyph()
371 glyph1.guidelines = [
372 dict(name="foo", identifier="1", x=1, y=2, angle=1),
373 dict(name="bar", identifier="2", x=1, y=2, angle=2),
374 ]
375 glyph2 = self._setupTestGlyph()
376 glyph2.guidelines = [
377 dict(name="xxx", identifier="2", x=3, y=4, angle=3),
378 dict(name="yyy", identifier="1", x=3, y=4, angle=4)
379 ]
380 expected = [
381 dict(name="foo", identifier="1", x=4, y=6, angle=5),
382 dict(name="bar", identifier="2", x=4, y=6, angle=5)
383 ]
384 glyph3 = glyph1 + glyph2
385 self.assertEqual(glyph3.guidelines, expected)
386
387 def test_guidelines_mul(self):
388 glyph1 = self._setupTestGlyph()
389 glyph1.guidelines = [
390 dict(x=1, y=3, angle=5, name="test", identifier="1",
391 color="0,0,0,0")
392 ]
393 glyph2 = glyph1 * 3
394 expected = [
395 dict(x=1 * 3, y=3 * 3, angle=15, name="test", identifier="1",
396 color="0,0,0,0")
397 ]
398 self.assertEqual(glyph2.guidelines, expected)
399
400 def test_guidelines_round(self):
401 glyph1 = self._setupTestGlyph()
402 glyph1.guidelines = [
403 dict(x=1.99, y=3.01, angle=5, name="test", identifier="1",
404 color="0,0,0,0")
405 ]
406 glyph2 = glyph1.round()
407 expected = [
408 dict(x=2, y=3, angle=5, name="test", identifier="1",
409 color="0,0,0,0")
410 ]
411 self.assertEqual(glyph2.guidelines, expected)
412
413 def test_guidelines_valid_angle(self):
414 glyph1 = self._setupTestGlyph()
415 glyph1.guidelines = [
416 dict(name="foo", identifier="1", x=0, y=0, angle=1)
417 ]
418 glyph2 = self._setupTestGlyph()
419 glyph2.guidelines = [
420 dict(name="foo", identifier="1", x=0, y=0, angle=359)
421 ]
422
423 expected_add = [dict(name="foo", identifier="1", x=0, y=0, angle=0)]
424 glyph3 = glyph1 + glyph2
425 self.assertEqual(glyph3.guidelines, expected_add)
426
427 expected_sub = [dict(name="foo", identifier="1", x=0, y=0, angle=2)]
428 glyph4 = glyph1 - glyph2
429 self.assertEqual(glyph4.guidelines, expected_sub)
430
431 expected_mul = [dict(name="foo", identifier="1", x=0, y=0, angle=355)]
432 glyph5 = glyph2 * 5
433 self.assertEqual(glyph5.guidelines, expected_mul)
434
435 expected_div = [dict(name="foo", identifier="1", x=0, y=0, angle=71.8)]
436 glyph6 = glyph2 / 5
437 self.assertEqual(glyph6.guidelines, expected_div)
438
439 def test_anchors_add(self):
440 glyph1 = self._setupTestGlyph()
441 glyph1.anchors = [
442 dict(x=1, y=-2, name="foo", identifier="1", color="0,0,0,0")
443 ]
444 glyph2 = self._setupTestGlyph()
445 glyph2.anchors = [
446 dict(x=3, y=-4, name="foo", identifier="1", color="0,0,0,0")
447 ]
448 glyph3 = glyph1 + glyph2
449 expected = [
450 dict(x=4, y=-6, name="foo", identifier="1", color="0,0,0,0")
451 ]
452 self.assertEqual(glyph3.anchors, expected)
453
454 def test_anchors_sub(self):
455 glyph1 = self._setupTestGlyph()
456 glyph1.anchors = [
457 dict(x=1, y=-2, name="foo", identifier="1", color="0,0,0,0")
458 ]
459 glyph2 = self._setupTestGlyph()
460 glyph2.anchors = [
461 dict(x=3, y=-4, name="foo", identifier="1", color="0,0,0,0")
462 ]
463 glyph3 = glyph1 - glyph2
464 expected = [
465 dict(x=-2, y=2, name="foo", identifier="1", color="0,0,0,0")
466 ]
467 self.assertEqual(glyph3.anchors, expected)
468
469 def test_anchors_mul(self):
470 glyph1 = self._setupTestGlyph()
471 glyph1.anchors = [
472 dict(x=1, y=-2, name="foo", identifier="1", color="0,0,0,0")
473 ]
474 glyph2 = glyph1 * 2
475 expected = [
476 dict(x=2, y=-4, name="foo", identifier="1", color="0,0,0,0")
477 ]
478 self.assertEqual(glyph2.anchors, expected)
479
480 def test_anchors_div(self):
481 glyph1 = self._setupTestGlyph()
482 glyph1.anchors = [
483 dict(x=1, y=-2, name="foo", identifier="1", color="0,0,0,0")
484 ]
485 glyph2 = glyph1 / 2
486 expected = [
487 dict(x=0.5, y=-1, name="foo", identifier="1", color="0,0,0,0")
488 ]
489 self.assertEqual(glyph2.anchors, expected)
490
491 def test_anchors_round(self):
492 glyph1 = self._setupTestGlyph()
493 glyph1.anchors = [
494 dict(x=99.9, y=-100.1, name="foo", identifier="1", color="0,0,0,0")
495 ]
496 glyph2 = glyph1.round()
497 expected = [
498 dict(x=100, y=-100, name="foo", identifier="1", color="0,0,0,0")
499 ]
500 self.assertEqual(glyph2.anchors, expected)
501
502 def test_image_round(self):
503 glyph1 = self._setupTestGlyph()
504 glyph1.image = dict(fileName="foo",
505 transformation=(1, 2, 3, 4, 4.99, 6.01),
506 color="0,0,0,0")
507 expected = dict(fileName="foo", transformation=(1, 2, 3, 4, 5, 6),
508 color="0,0,0,0")
509 glyph2 = glyph1.round()
510 self.assertEqual(glyph2.image, expected)
511
512 def test_copy(self):
513 glyph1 = self._setupTestGlyph()
514 glyph1.unicodes = []
515 glyph2 = glyph1.copy()
516 self.assertEqual(glyph1, glyph2)
517
518
519 class MathGlyphPenTest(unittest.TestCase):
520 def __init__(self, methodName):
521 unittest.TestCase.__init__(self, methodName)
522
523 def test_pen_with_lines(self):
524 pen = MathGlyphPen()
525 pen.beginPath(identifier="contour 1")
526 pen.addPoint((0, 100), "line", smooth=False, name="name 1",
527 identifier="point 1")
528 pen.addPoint((100, 100), "line", smooth=False, name="name 2",
529 identifier="point 2")
530 pen.addPoint((100, 0), "line", smooth=False, name="name 3",
531 identifier="point 3")
532 pen.addPoint((0, 0), "line", smooth=False, name="name 4",
533 identifier="point 4")
534 pen.endPath()
535 expected = [
536 ("curve", (0, 100), False, "name 1", "point 1"),
537 (None, (0, 100), False, None, None),
538 (None, (100, 100), False, None, None),
539 ("curve", (100, 100), False, "name 2", "point 2"),
540 (None, (100, 100), False, None, None),
541 (None, (100, 0), False, None, None),
542 ("curve", (100, 0), False, "name 3", "point 3"),
543 (None, (100, 0), False, None, None),
544 (None, (0, 0), False, None, None),
545 ("curve", (0, 0), False, "name 4", "point 4"),
546 (None, (0, 0), False, None, None),
547 (None, (0, 100), False, None, None),
548 ]
549 self.assertEqual(pen.contours[-1]["points"], expected)
550 self.assertEqual(pen.contours[-1]["identifier"], 'contour 1')
551
552 def test_pen_with_lines_strict(self):
553 pen = MathGlyphPen(strict=True)
554 pen.beginPath(identifier="contour 1")
555 pen.addPoint((0, 100), "line", smooth=False, name="name 1",
556 identifier="point 1")
557 pen.addPoint((100, 100), "line", smooth=False, name="name 2",
558 identifier="point 2")
559 pen.addPoint((100, 0), "line", smooth=False, name="name 3",
560 identifier="point 3")
561 pen.addPoint((0, 0), "line", smooth=False, name="name 4",
562 identifier="point 4")
563 pen.endPath()
564 expected = [
565 ("line", (0, 100), False, "name 1", "point 1"),
566 ("line", (100, 100), False, "name 2", "point 2"),
567 ("line", (100, 0), False, "name 3", "point 3"),
568 ("line", (0, 0), False, "name 4", "point 4"),
569 ]
570 self.assertEqual(pen.contours[-1]["points"], expected)
571 self.assertEqual(pen.contours[-1]["identifier"], 'contour 1')
572
573 def test_pen_with_lines_and_curves(self):
574 pen = MathGlyphPen()
575 pen.beginPath(identifier="contour 1")
576 pen.addPoint((0, 50), "curve", smooth=False, name="name 1",
577 identifier="point 1")
578 pen.addPoint((50, 100), "line", smooth=False, name="name 2",
579 identifier="point 2")
580 pen.addPoint((75, 100), None)
581 pen.addPoint((100, 75), None)
582 pen.addPoint((100, 50), "curve", smooth=True, name="name 3",
583 identifier="point 3")
584 pen.addPoint((100, 25), None)
585 pen.addPoint((75, 0), None)
586 pen.addPoint((50, 0), "curve", smooth=False, name="name 4",
587 identifier="point 4")
588 pen.addPoint((25, 0), None)
589 pen.addPoint((0, 25), None)
590 pen.endPath()
591 expected = [
592 ("curve", (0, 50), False, "name 1", "point 1"),
593 (None, (0, 50), False, None, None),
594 (None, (50, 100), False, None, None),
595 ("curve", (50, 100), False, "name 2", "point 2"),
596 (None, (75, 100), False, None, None),
597 (None, (100, 75), False, None, None),
598 ("curve", (100, 50), True, "name 3", "point 3"),
599 (None, (100, 25), False, None, None),
600 (None, (75, 0), False, None, None),
601 ("curve", (50, 0), False, "name 4", "point 4"),
602 (None, (25, 0), False, None, None),
603 (None, (0, 25), False, None, None),
604 ]
605 self.assertEqual(pen.contours[-1]["points"], expected)
606 self.assertEqual(pen.contours[-1]["identifier"], 'contour 1')
607
608 def test_pen_with_lines_and_curves_strict(self):
609 pen = MathGlyphPen(strict=True)
610 pen.beginPath(identifier="contour 1")
611 pen.addPoint((0, 50), "curve", smooth=False, name="name 1",
612 identifier="point 1")
613 pen.addPoint((50, 100), "line", smooth=False, name="name 2",
614 identifier="point 2")
615 pen.addPoint((75, 100), None)
616 pen.addPoint((100, 75), None)
617 pen.addPoint((100, 50), "curve", smooth=True, name="name 3",
618 identifier="point 3")
619 pen.addPoint((100, 25), None)
620 pen.addPoint((75, 0), None)
621 pen.addPoint((50, 0), "curve", smooth=False, name="name 4",
622 identifier="point 4")
623 pen.addPoint((25, 0), None)
624 pen.addPoint((0, 25), None)
625 pen.endPath()
626 expected = [
627 ("curve", (0, 50), False, "name 1", "point 1"),
628 ("line", (50, 100), False, "name 2", "point 2"),
629 (None, (75, 100), False, None, None),
630 (None, (100, 75), False, None, None),
631 ("curve", (100, 50), True, "name 3", "point 3"),
632 (None, (100, 25), False, None, None),
633 (None, (75, 0), False, None, None),
634 ("curve", (50, 0), False, "name 4", "point 4"),
635 (None, (25, 0), False, None, None),
636 (None, (0, 25), False, None, None),
637 ]
638 self.assertEqual(pen.contours[-1]["points"], expected)
639 self.assertEqual(pen.contours[-1]["identifier"], 'contour 1')
640
641
642 class _TestPointPen(AbstractPointPen):
643
644 def __init__(self):
645 self._text = []
646
647 def dump(self):
648 return "\n".join(self._text)
649
650 def _prep(self, i):
651 if isinstance(i, basestring):
652 i = "\"%s\"" % i
653 return str(i)
654
655 def beginPath(self, identifier=None, **kwargs):
656 self._text.append("beginPath(identifier=%s)" % self._prep(identifier))
657
658 def addPoint(self, pt, segmentType=None, smooth=False, name=None,
659 identifier=None, **kwargs):
660 self._text.append(
661 "addPoint(%s, segmentType=%s, smooth=%s, name=%s, "
662 "identifier=%s)" % (
663 self._prep(pt),
664 self._prep(segmentType),
665 self._prep(smooth),
666 self._prep(name),
667 self._prep(identifier)
668 )
669 )
670
671 def endPath(self):
672 self._text.append("endPath()")
673
674 def addComponent(self, baseGlyph, transformation, identifier=None,
675 **kwargs):
676 self._text.append(
677 "addComponent(baseGlyph=%s, transformation=%s, identifier=%s)" % (
678 self._prep(baseGlyph),
679 self._prep(transformation),
680 self._prep(identifier)
681 )
682 )
683
684
685 class FilterRedundantPointPenTest(unittest.TestCase):
686 def __init__(self, methodName):
687 unittest.TestCase.__init__(self, methodName)
688
689 def test_flushContour(self):
690 points = [
691 ("curve", (0, 100), False, "name 1", "point 1"),
692 (None, (0, 100), False, None, None),
693 (None, (100, 100), False, None, None),
694 ("curve", (100, 100), False, "name 2", "point 2"),
695 (None, (100, 100), False, None, None),
696 (None, (100, 0), False, None, None),
697 ("curve", (100, 0), False, "name 3", "point 3"),
698 (None, (100, 0), False, None, None),
699 (None, (0, 0), False, None, None),
700 ("curve", (0, 0), False, "name 4", "point 4"),
701 (None, (0, 0), False, None, None),
702 (None, (0, 100), False, None, None),
703 ]
704 testPen = _TestPointPen()
705 filterPen = FilterRedundantPointPen(testPen)
706 filterPen.beginPath(identifier="contour 1")
707 for segmentType, pt, smooth, name, identifier in points:
708 filterPen.addPoint(pt, segmentType=segmentType, smooth=smooth,
709 name=name, identifier=identifier)
710 filterPen.endPath()
711 self.assertEqual(
712 testPen.dump(),
713 'beginPath(identifier="contour 1")\n'
714 'addPoint((0, 100), segmentType="line", smooth=False, '
715 'name="name 1", identifier="point 1")\n'
716 'addPoint((100, 100), segmentType="line", smooth=False, '
717 'name="name 2", identifier="point 2")\n'
718 'addPoint((100, 0), segmentType="line", smooth=False, '
719 'name="name 3", identifier="point 3")\n'
720 'addPoint((0, 0), segmentType="line", smooth=False, '
721 'name="name 4", identifier="point 4")\n'
722 'endPath()'
723 )
724
725
726 class PrivateFuncsTest(unittest.TestCase):
727 def __init__(self, methodName):
728 unittest.TestCase.__init__(self, methodName)
729
730 def test_processMathOneContours(self):
731 contours1 = [
732 dict(identifier="contour 1",
733 points=[("line", (1, 3), False, "test", "1")])
734 ]
735 contours2 = [
736 dict(identifier=None, points=[(None, (4, 6), True, None, None)])
737 ]
738 self.assertEqual(
739 _processMathOneContours(contours1, contours2, addPt),
740 [
741 dict(identifier="contour 1",
742 points=[("line", (5, 9), False, "test", "1")])
743 ]
744 )
745
746 def test_processMathTwoContours(self):
747 contours = [
748 dict(identifier="contour 1",
749 points=[("line", (1, 3), False, "test", "1")])
750 ]
751 self.assertEqual(
752 _processMathTwoContours(contours, (2, 1.5), mulPt),
753 [
754 dict(identifier="contour 1",
755 points=[("line", (2, 4.5), False, "test", "1")])
756 ]
757 )
758 True
759
760 def test_anchorTree(self):
761 anchors = [
762 dict(identifier="1", name="test", x=1, y=2, color=None),
763 dict(name="test", x=1, y=2, color=None),
764 dict(name="test", x=3, y=4, color=None),
765 dict(name="test", x=2, y=3, color=None),
766 dict(name="c", x=1, y=2, color=None),
767 dict(name="a", x=0, y=0, color=None),
768 ]
769 self.assertEqual(
770 list(_anchorTree(anchors).items()),
771 [
772 ("test", [
773 ("1", 1, 2, None),
774 (None, 1, 2, None),
775 (None, 3, 4, None),
776 (None, 2, 3, None)
777 ]),
778 ("c", [
779 (None, 1, 2, None)
780 ]),
781 ("a", [
782 (None, 0, 0, None)
783 ])
784 ]
785 )
786
787 def test_pairAnchors_matching_identifiers(self):
788 anchors1 = {
789 "test": [
790 (None, 1, 2, None),
791 ("identifier 1", 3, 4, None)
792 ]
793 }
794 anchors2 = {
795 "test": [
796 ("identifier 1", 1, 2, None),
797 (None, 3, 4, None)
798 ]
799 }
800 self.assertEqual(
801 _pairAnchors(anchors1, anchors2),
802 [
803 (
804 dict(name="test", identifier=None, x=1, y=2, color=None),
805 dict(name="test", identifier=None, x=3, y=4, color=None)
806 ),
807 (
808 dict(name="test", identifier="identifier 1", x=3, y=4,
809 color=None),
810 dict(name="test", identifier="identifier 1", x=1, y=2,
811 color=None)
812 )
813 ]
814 )
815
816 def test_pairAnchors_mismatched_identifiers(self):
817 anchors1 = {
818 "test": [
819 ("identifier 1", 3, 4, None)
820 ]
821 }
822 anchors2 = {
823 "test": [
824 ("identifier 2", 1, 2, None),
825 ]
826 }
827 self.assertEqual(
828 _pairAnchors(anchors1, anchors2),
829 [
830 (
831 dict(name="test", identifier="identifier 1", x=3, y=4,
832 color=None),
833 dict(name="test", identifier="identifier 2", x=1, y=2,
834 color=None)
835 )
836 ]
837 )
838
839 def test_processMathOneAnchors(self):
840 anchorPairs = [
841 (
842 dict(x=100, y=-100, name="foo", identifier="1",
843 color="0,0,0,0"),
844 dict(x=200, y=-200, name="bar", identifier="2",
845 color="1,1,1,1")
846 )
847 ]
848 self.assertEqual(
849 _processMathOneAnchors(anchorPairs, addPt),
850 [
851 dict(x=300, y=-300, name="foo", identifier="1",
852 color="0,0,0,0")
853 ]
854 )
855
856 def test_processMathTwoAnchors(self):
857 anchors = [
858 dict(x=100, y=-100, name="foo", identifier="1", color="0,0,0,0")
859 ]
860 self.assertEqual(
861 _processMathTwoAnchors(anchors, (2, 1.5), mulPt),
862 [
863 dict(x=200, y=-150, name="foo", identifier="1",
864 color="0,0,0,0")
865 ]
866 )
867
868 def test_pairComponents(self):
869 components1 = [
870 dict(baseGlyph="A", transformation=(0, 0, 0, 0, 0, 0),
871 identifier="1"),
872 dict(baseGlyph="B", transformation=(0, 0, 0, 0, 0, 0),
873 identifier="1"),
874 dict(baseGlyph="A", transformation=(0, 0, 0, 0, 0, 0),
875 identifier=None)
876 ]
877 components2 = [
878 dict(baseGlyph="A", transformation=(0, 0, 0, 0, 0, 0),
879 identifier=None),
880 dict(baseGlyph="B", transformation=(0, 0, 0, 0, 0, 0),
881 identifier="1"),
882 dict(baseGlyph="A", transformation=(0, 0, 0, 0, 0, 0),
883 identifier="1")
884 ]
885 self.assertEqual(
886 _pairComponents(components1, components2),
887 [
888 (
889 dict(baseGlyph="A", transformation=(0, 0, 0, 0, 0, 0),
890 identifier="1"),
891 dict(baseGlyph="A", transformation=(0, 0, 0, 0, 0, 0),
892 identifier="1")
893 ),
894 (
895 dict(baseGlyph="B", transformation=(0, 0, 0, 0, 0, 0),
896 identifier="1"),
897 dict(baseGlyph="B", transformation=(0, 0, 0, 0, 0, 0),
898 identifier="1")
899 ),
900 (
901 dict(baseGlyph="A", transformation=(0, 0, 0, 0, 0, 0),
902 identifier=None),
903 dict(baseGlyph="A", transformation=(0, 0, 0, 0, 0, 0),
904 identifier=None)
905 ),
906 ]
907 )
908
909 components1 = [
910 dict(baseGlyph="A", transformation=(0, 0, 0, 0, 0, 0),
911 identifier=None),
912 dict(baseGlyph="B", transformation=(0, 0, 0, 0, 0, 0),
913 identifier=None)
914 ]
915 components2 = [
916 dict(baseGlyph="B", transformation=(0, 0, 0, 0, 0, 0),
917 identifier=None),
918 dict(baseGlyph="A", transformation=(0, 0, 0, 0, 0, 0),
919 identifier=None)
920 ]
921 self.assertEqual(
922 _pairComponents(components1, components2),
923 [
924 (
925 dict(baseGlyph="A", transformation=(0, 0, 0, 0, 0, 0),
926 identifier=None),
927 dict(baseGlyph="A", transformation=(0, 0, 0, 0, 0, 0),
928 identifier=None)
929 ),
930 (
931 dict(baseGlyph="B", transformation=(0, 0, 0, 0, 0, 0),
932 identifier=None),
933 dict(baseGlyph="B", transformation=(0, 0, 0, 0, 0, 0),
934 identifier=None)
935 ),
936 ]
937 )
938
939 def test_processMathOneComponents(self):
940 components = [
941 (
942 dict(baseGlyph="A", transformation=(1, 3, 5, 7, 9, 11),
943 identifier="1"),
944 dict(baseGlyph="A", transformation=(12, 14, 16, 18, 20, 22),
945 identifier=None)
946 )
947 ]
948 self.assertEqual(
949 _processMathOneComponents(components, addPt),
950 [
951 dict(baseGlyph="A", transformation=(13, 17, 21, 25, 29, 33),
952 identifier="1")
953 ]
954 )
955
956 def test_processMathTwoComponents(self):
957 components = [
958 dict(baseGlyph="A", transformation=(1, 2, 3, 4, 5, 6), identifier="1")
959 ]
960 scaled_components = [
961 dict(baseGlyph="A", transformation=(2, 4, 4.5, 6, 10, 9), identifier="1")
962 ]
963 self.assertEqual(
964 _processMathTwoComponents(components, (2, 1.5), mulPt),
965 scaled_components
966 )
967 self.assertEqual(
968 _processMathTwoComponents(
969 components, (2, 1.5), mulPt, scaleComponentTransform=True
970 ),
971 scaled_components
972 )
973 self.assertEqual(
974 _processMathTwoComponents(
975 components, (2, 1.5), mulPt, scaleComponentTransform=False
976 ),
977 [
978 dict(
979 baseGlyph="A",
980 transformation=(1, 2, 3, 4, 10, 9),
981 identifier="1"
982 )
983 ],
984 )
985
986 def test_expandImage(self):
987 self.assertEqual(
988 _expandImage(None),
989 dict(fileName=None, transformation=(1, 0, 0, 1, 0, 0), color=None)
990 )
991 self.assertEqual(
992 _expandImage(dict(fileName="foo")),
993 dict(fileName="foo", transformation=(1, 0, 0, 1, 0, 0), color=None)
994 )
995
996 def test_compressImage(self):
997 self.assertEqual(
998 _compressImage(
999 dict(fileName="foo",
1000 transformation=(1, 0, 0, 1, 0, 0), color=None)),
1001 dict(fileName="foo", color=None, xScale=1,
1002 xyScale=0, yxScale=0, yScale=1, xOffset=0, yOffset=0)
1003 )
1004
1005 def test_pairImages(self):
1006 image1 = dict(fileName="foo", transformation=(1, 0, 0, 1, 0, 0),
1007 color=None)
1008 image2 = dict(fileName="foo", transformation=(2, 0, 0, 2, 0, 0),
1009 color="0,0,0,0")
1010 self.assertEqual(
1011 _pairImages(image1, image2),
1012 (image1, image2)
1013 )
1014
1015 image1 = dict(fileName="foo", transformation=(1, 0, 0, 1, 0, 0),
1016 color=None)
1017 image2 = dict(fileName="bar", transformation=(1, 0, 0, 1, 0, 0),
1018 color=None)
1019 self.assertEqual(
1020 _pairImages(image1, image2),
1021 ()
1022 )
1023
1024 def test_processMathOneImage(self):
1025 image1 = dict(fileName="foo", transformation=(1, 3, 5, 7, 9, 11),
1026 color="0,0,0,0")
1027 image2 = dict(fileName="bar", transformation=(12, 14, 16, 18, 20, 22),
1028 color=None)
1029 self.assertEqual(
1030 _processMathOneImage((image1, image2), addPt),
1031 dict(fileName="foo", transformation=(13, 17, 21, 25, 29, 33),
1032 color="0,0,0,0")
1033 )
1034
1035 def test_processMathTwoImage(self):
1036 image = dict(fileName="foo", transformation=(1, 2, 3, 4, 5, 6),
1037 color="0,0,0,0")
1038 self.assertEqual(
1039 _processMathTwoImage(image, (2, 1.5), mulPt),
1040 dict(fileName="foo", transformation=(2, 4, 4.5, 6, 10, 9),
1041 color="0,0,0,0")
1042 )
1043
1044 def test_processMathOneTransformation(self):
1045 transformation1 = (1, 3, 5, 7, 9, 11)
1046 transformation2 = (12, 14, 16, 18, 20, 22)
1047 self.assertEqual(
1048 _processMathOneTransformation(transformation1, transformation2,
1049 addPt),
1050 (13, 17, 21, 25, 29, 33)
1051 )
1052
1053 def test_processMathTwoTransformation(self):
1054 transformation = (1, 2, 3, 4, 5, 6)
1055 self.assertEqual(
1056 _processMathTwoTransformation(transformation, (2, 1.5), mulPt),
1057 (2, 4, 4.5, 6, 10, 9)
1058 )
1059 self.assertEqual(
1060 _processMathTwoTransformation(transformation, (2, 1.5), mulPt, doScale=True),
1061 (2, 4, 4.5, 6, 10, 9)
1062 )
1063 self.assertEqual(
1064 _processMathTwoTransformation(transformation, (2, 1.5), mulPt, doScale=False),
1065 (1, 2, 3, 4, 10, 9)
1066 )
1067
1068 def test_roundContours(self):
1069 contour = [
1070 dict(identifier="contour 1", points=[("line", (0.55, 3.1), False,
1071 "test", "1")]),
1072 dict(identifier="contour 1", points=[("line", (0.55, 3.1), True,
1073 "test", "1")])
1074 ]
1075 self.assertEqual(
1076 _roundContours(contour),
1077 [
1078 dict(identifier="contour 1", points=[("line", (1, 3), False,
1079 "test", "1")]),
1080 dict(identifier="contour 1", points=[("line", (1, 3), True,
1081 "test", "1")])
1082 ]
1083 )
1084
1085 def test_roundTransformation(self):
1086 transformation = (1, 2, 3, 4, 4.99, 6.01)
1087 self.assertEqual(
1088 _roundTransformation(transformation),
1089 (1, 2, 3, 4, 5, 6)
1090 )
1091
1092 def test_roundImage(self):
1093 image = dict(fileName="foo", transformation=(1, 2, 3, 4, 4.99, 6.01),
1094 color="0,0,0,0")
1095 self.assertEqual(
1096 _roundImage(image),
1097 dict(fileName="foo", transformation=(1, 2, 3, 4, 5, 6),
1098 color="0,0,0,0")
1099 )
1100
1101 def test_roundComponents(self):
1102 components = [
1103 dict(baseGlyph="A", transformation=(1, 2, 3, 4, 5.1, 5.99),
1104 identifier="1"),
1105 ]
1106 self.assertEqual(
1107 _roundComponents(components),
1108 [
1109 dict(baseGlyph="A", transformation=(1, 2, 3, 4, 5, 6),
1110 identifier="1")
1111 ]
1112 )
1113
1114 def test_roundAnchors(self):
1115 anchors = [
1116 dict(x=99.9, y=-100.1, name="foo", identifier="1",
1117 color="0,0,0,0")
1118 ]
1119 self.assertEqual(
1120 _roundAnchors(anchors),
1121 [
1122 dict(x=100, y=-100, name="foo", identifier="1",
1123 color="0,0,0,0")
1124 ]
1125 )
1126
1127 if __name__ == "__main__":
1128 unittest.main()