Changeset 349
- Timestamp:
- 09/30/11 18:28:15 (20 months ago)
- Location:
- branches/ufo3k/Lib/ufoLib
- Files:
-
- 2 edited
-
glifLib.py (modified) (6 diffs)
-
test/test_GLIF1.py (modified) (9 diffs)
Legend:
- Unmodified
- Added
- Removed
-
branches/ufo3k/Lib/ufoLib/glifLib.py
r348 r349 937 937 raise GlifLibError("move can not have an offcurve.") 938 938 elif segmentType == "line": 939 raise GlifLibError(" move can not have an offcurve.")939 raise GlifLibError("line can not have an offcurve.") 940 940 elif segmentType == "curve": 941 941 if len(offCurves) > 2: … … 982 982 raise GlifLibError("Unknown point smooth value: %s" % smooth) 983 983 smooth = smooth == "yes" 984 if smooth and segmentType not in ["curve"]:984 if smooth and segmentType not in ("curve", "qcurve"): 985 985 raise GlifLibError("smooth attribute set in a %s point." % segmentType) 986 986 # name is not required … … 1133 1133 self.writer = xmlWriter 1134 1134 self.prevSegmentType = None 1135 self.prevOffCurveCount = 0 1135 1136 1136 1137 def beginPath(self, identifier=None, **kwargs): … … 1145 1146 self.writer.newline() 1146 1147 self.prevSegmentType = None 1148 self.prevOffCurveCount = 0 1147 1149 1148 1150 def endPath(self): … … 1150 1152 self.writer.newline() 1151 1153 self.prevSegmentType = None 1154 self.prevOffCurveCount = 0 1152 1155 1153 1156 def addPoint(self, pt, segmentType=None, smooth=None, name=None, identifier=None, **kwargs): … … 1163 1166 if segmentType == "move" and self.prevSegmentType is not None: 1164 1167 raise GlifLibError("move occurs after a point has already been added to the contour.") 1168 if segmentType in ("move", "line") and self.prevSegmentType == "offcurve": 1169 raise GlifLibError("offcurve occurs before %s point." % segmentType) 1170 if segmentType == "curve" and self.prevOffCurveCount > 2: 1171 raise GlifLibError("too many offcurve points before curve point.") 1165 1172 if segmentType is not None: 1166 1173 attrs.append(("type", segmentType)) 1174 else: 1175 segmentType = "offcurve" 1176 if segmentType == "offcurve": 1177 self.prevOffCurveCount += 1 1178 else: 1179 self.prevOffCurveCount = 0 1167 1180 self.prevSegmentType = segmentType 1168 1181 # smooth 1169 1182 if smooth: 1170 if segmentType not in ["curve"]:1183 if segmentType not in ("curve", "qcurve"): 1171 1184 raise GlifLibError("can't set smooth in a %s point." % segmentType) 1172 1185 attrs.append(("smooth", "yes")) -
branches/ufo3k/Lib/ufoLib/test/test_GLIF1.py
r345 r349 14 14 self.unicodes = None 15 15 self.note = None 16 self.lib = None 16 17 self.image = None 17 18 self.guidelines = None … … 19 20 20 21 def _writePointPenCommand(self, command, args, kwargs): 22 args = listToString(args) 23 kwargs = dictToString(kwargs) 21 24 if args and kwargs: 22 return "pointPen.%s(*%s, **%s)" % (command, listToString(args), dictToString(kwargs))23 elif args:24 return "pointPen.%s(*%s)" % (command, listToString(args))25 elif kwargs:26 return "pointPen.%s(**%s)" % (command, dictToString(kwargs))25 return "pointPen.%s(*%s, **%s)" % (command, args, kwargs) 26 elif len(args): 27 return "pointPen.%s(*%s)" % (command, args) 28 elif len(kwargs): 29 return "pointPen.%s(**%s)" % (command, kwargs) 27 30 else: 28 31 return "pointPen.%s()" % command 29 32 30 33 def beginPath(self, **kwargs): 31 self.outline.append(self._writePointPenCommand("beginPath", args))34 self.outline.append(self._writePointPenCommand("beginPath", [], kwargs)) 32 35 33 36 def endPath(self): 34 self.outline.append(self._writePointPenCommand("endPath" ))37 self.outline.append(self._writePointPenCommand("endPath", [], {})) 35 38 36 39 def addPoint(self, *args, **kwargs): … … 57 60 if self.note is not None: 58 61 text.append("glyph.note = \"%s\"" % self.note) 62 if self.lib is not None: 63 text.append("glyph.lib = %s" % dictToString(self.lib)) 59 64 if self.image is not None: 60 65 text.append("glyph.image = %s" % dictToString(self.image)) … … 62 67 text.append("glyph.guidelines = %s" % listToString(self.guidelines)) 63 68 if self.outline: 64 text.append("pointPen = glyph.getPointPen()")65 69 text += self.outline 66 70 return "\n".join(text) … … 83 87 value = "\"%s\"" % value 84 88 text.append("%s : %s" % (key, value)) 89 if not text: 90 return "" 85 91 return "{%s}" % ", ".join(text) 86 92 … … 99 105 value = "\"%s\"" % value 100 106 text.append(value) 107 if not text: 108 return "" 101 109 return "[%s]" % ", ".join(text) 102 110 … … 115 123 value = "\"%s\"" % value 116 124 text.append(value) 125 if not text: 126 return "" 117 127 return "(%s)" % ", ".join(text) 118 119 128 120 129 def stripText(text): … … 143 152 py = stripText(py) 144 153 glyph = Glyph() 145 exec py in {"glyph" : glyph }154 exec py in {"glyph" : glyph, "pointPen" : glyph} 146 155 glif = writeGlyphToString(glyph.name, glyphObject=glyph, drawPointsFunc=glyph.drawPoints, formatVersion=1) 147 156 glif = "\n".join(glif.splitlines()[1:]) … … 409 418 self.assertEqual(py, resultPy) 410 419 420 def testLib(self): 421 glif = """ 422 <glyph name="a" format="1"> 423 <outline> 424 </outline> 425 <lib> 426 <dict> 427 <key>dict</key> 428 <dict> 429 <key>hello</key> 430 <string>world</string> 431 </dict> 432 <key>float</key> 433 <real>2.5</real> 434 <key>int</key> 435 <integer>1</integer> 436 <key>list</key> 437 <array> 438 <string>a</string> 439 <string>b</string> 440 <integer>1</integer> 441 <real>2.5</real> 442 </array> 443 <key>string</key> 444 <string>a</string> 445 </dict> 446 </lib> 447 </glyph> 448 """ 449 py = """ 450 glyph.name = "a" 451 glyph.lib = {"dict" : {"hello" : "world"}, "float" : 2.5, "int" : 1, "list" : ["a", "b", 1, 2.5], "string" : "a"} 452 """ 453 resultGlif = self.glyphToGLIF(py) 454 resultPy = self.glifToPy(glif) 455 self.assertEqual(glif, resultGlif) 456 self.assertEqual(py, resultPy) 457 458 def testOutline(self): 459 # unknown element 460 glif = """ 461 <glyph name="a" format="1"> 462 <outline> 463 <unknown/> 464 </outline> 465 </glyph> 466 """ 467 self.assertRaises(GlifLibError, self.glifToPy, glif) 468 # content 469 glif = """ 470 <glyph name="a" format="1"> 471 <outline> 472 hello 473 </outline> 474 </glyph> 475 """ 476 self.assertRaises(GlifLibError, self.glifToPy, glif) 477 478 def testComponent(self): 479 # legal 480 glif = """ 481 <glyph name="a" format="1"> 482 <outline> 483 <component base="x" xScale="2" xyScale="3" yxScale="6" yScale="5" xOffset="1" yOffset="4"/> 484 </outline> 485 </glyph> 486 """ 487 py = """ 488 glyph.name = "a" 489 pointPen.addComponent(*["x", (2, 3, 6, 5, 1, 4)]) 490 """ 491 resultGlif = self.glyphToGLIF(py) 492 resultPy = self.glifToPy(glif) 493 self.assertEqual(glif, resultGlif) 494 self.assertEqual(py, resultPy) 495 # no base 496 glif = """ 497 <glyph name="a" format="1"> 498 <outline> 499 <component xScale="2" xyScale="3" yxScale="6" yScale="5" xOffset="1" yOffset="4"/> 500 </outline> 501 </glyph> 502 """ 503 self.assertRaises(GlifLibError, self.glifToPy, glif) 504 # bogus values in transformation 505 glif = """ 506 <glyph name="a" format="1"> 507 <outline> 508 <component base="x" xScale="a" xyScale="3" yxScale="6" yScale="5" xOffset="1" yOffset="4"/> 509 </outline> 510 </glyph> 511 """ 512 py = """ 513 glyph.name = "a" 514 pointPen.addComponent(*["x", ("a", 3, 6, 5, 1, 4)]) 515 """ 516 self.assertRaises(GlifLibError, self.glyphToGLIF, py) 517 self.assertRaises(GlifLibError, self.glifToPy, glif) 518 glif = """ 519 <glyph name="a" format="1"> 520 <outline> 521 <component base="x" xScale="a" xyScale="3" yxScale="6" yScale="5" xOffset="1" yOffset="4"/> 522 </outline> 523 </glyph> 524 """ 525 py = """ 526 glyph.name = "a" 527 pointPen.addComponent(*["x", (2, "a", 6, 5, 1, 4)]) 528 """ 529 self.assertRaises(GlifLibError, self.glyphToGLIF, py) 530 self.assertRaises(GlifLibError, self.glifToPy, glif) 531 glif = """ 532 <glyph name="a" format="1"> 533 <outline> 534 <component base="x" xScale="2" xyScale="3" yxScale="a" yScale="5" xOffset="1" yOffset="4"/> 535 </outline> 536 </glyph> 537 """ 538 py = """ 539 glyph.name = "a" 540 pointPen.addComponent(*["x", (2, 3, "a", 5, 1, 4)]) 541 """ 542 self.assertRaises(GlifLibError, self.glyphToGLIF, py) 543 self.assertRaises(GlifLibError, self.glifToPy, glif) 544 glif = """ 545 <glyph name="a" format="1"> 546 <outline> 547 <component base="x" xScale="2" xyScale="3" yxScale="6" yScale="a" xOffset="1" yOffset="4"/> 548 </outline> 549 </glyph> 550 """ 551 py = """ 552 glyph.name = "a" 553 pointPen.addComponent(*["x", (2, 3, 6, "a", 1, 4)]) 554 """ 555 self.assertRaises(GlifLibError, self.glyphToGLIF, py) 556 self.assertRaises(GlifLibError, self.glifToPy, glif) 557 glif = """ 558 <glyph name="a" format="1"> 559 <outline> 560 <component base="x" xScale="2" xyScale="3" yxScale="6" yScale="5" xOffset="a" yOffset="4"/> 561 </outline> 562 </glyph> 563 """ 564 py = """ 565 glyph.name = "a" 566 pointPen.addComponent(*["x", (2, 3, 6, 5, "a", 4)]) 567 """ 568 self.assertRaises(GlifLibError, self.glyphToGLIF, py) 569 self.assertRaises(GlifLibError, self.glifToPy, glif) 570 glif = """ 571 <glyph name="a" format="1"> 572 <outline> 573 <component base="x" xScale="2" xyScale="3" yxScale="6" yScale="5" xOffset="1" yOffset="a"/> 574 </outline> 575 </glyph> 576 """ 577 py = """ 578 glyph.name = "a" 579 pointPen.addComponent(*["x", (2, 3, 6, 5, 1, "a")]) 580 """ 581 self.assertRaises(GlifLibError, self.glyphToGLIF, py) 582 self.assertRaises(GlifLibError, self.glifToPy, glif) 583 584 def testContour(self): 585 # legal: one contour 586 glif = """ 587 <glyph name="a" format="1"> 588 <outline> 589 <contour> 590 </contour> 591 </outline> 592 </glyph> 593 """ 594 py = """ 595 glyph.name = "a" 596 pointPen.beginPath() 597 pointPen.endPath() 598 """ 599 resultGlif = self.glyphToGLIF(py) 600 resultPy = self.glifToPy(glif) 601 self.assertEqual(glif, resultGlif) 602 self.assertEqual(py, resultPy) 603 # legal: two contours 604 glif = """ 605 <glyph name="a" format="1"> 606 <outline> 607 <contour> 608 <point x="1" y="2" type="move"/> 609 </contour> 610 <contour> 611 <point x="1" y="2" type="move"/> 612 <point x="10" y="20" type="line"/> 613 </contour> 614 </outline> 615 </glyph> 616 """ 617 py = """ 618 glyph.name = "a" 619 pointPen.beginPath() 620 pointPen.addPoint(*[(1, 2)], **{"segmentType" : "move", "smooth" : False}) 621 pointPen.endPath() 622 pointPen.beginPath() 623 pointPen.addPoint(*[(1, 2)], **{"segmentType" : "move", "smooth" : False}) 624 pointPen.addPoint(*[(10, 20)], **{"segmentType" : "line", "smooth" : False}) 625 pointPen.endPath() 626 """ 627 resultGlif = self.glyphToGLIF(py) 628 resultPy = self.glifToPy(glif) 629 self.assertEqual(glif, resultGlif) 630 self.assertEqual(py, resultPy) 631 # unknown element 632 glif = """ 633 <glyph name="a" format="1"> 634 <outline> 635 <contour> 636 <unknown/> 637 </contour> 638 </outline> 639 </glyph> 640 """ 641 self.assertRaises(GlifLibError, self.glifToPy, glif) 642 643 def testPointCoordinates(self): 644 # legal: int 645 glif = """ 646 <glyph name="a" format="1"> 647 <outline> 648 <contour> 649 <point x="1" y="-2" type="move"/> 650 </contour> 651 </outline> 652 </glyph> 653 """ 654 py = """ 655 glyph.name = "a" 656 pointPen.beginPath() 657 pointPen.addPoint(*[(1, -2)], **{"segmentType" : "move", "smooth" : False}) 658 pointPen.endPath() 659 """ 660 resultGlif = self.glyphToGLIF(py) 661 resultPy = self.glifToPy(glif) 662 self.assertEqual(glif, resultGlif) 663 self.assertEqual(py, resultPy) 664 # legal: float 665 glif = """ 666 <glyph name="a" format="1"> 667 <outline> 668 <contour> 669 <point x="1.1" y="-2.2" type="move"/> 670 </contour> 671 </outline> 672 </glyph> 673 """ 674 py = """ 675 glyph.name = "a" 676 pointPen.beginPath() 677 pointPen.addPoint(*[(1.1, -2.2)], **{"segmentType" : "move", "smooth" : False}) 678 pointPen.endPath() 679 """ 680 resultGlif = self.glyphToGLIF(py) 681 resultPy = self.glifToPy(glif) 682 self.assertEqual(glif, resultGlif) 683 self.assertEqual(py, resultPy) 684 # legal: int 685 glif = """ 686 <glyph name="a" format="1"> 687 <outline> 688 <contour> 689 <point x="a" y="2" type="move"/> 690 </contour> 691 </outline> 692 </glyph> 693 """ 694 py = """ 695 glyph.name = "a" 696 pointPen.beginPath() 697 pointPen.addPoint(*[("a", 2)], **{"segmentType" : "move", "smooth" : False}) 698 pointPen.endPath() 699 """ 700 self.assertRaises(GlifLibError, self.glyphToGLIF, py) 701 self.assertRaises(GlifLibError, self.glifToPy, glif) 702 # legal: int 703 glif = """ 704 <glyph name="a" format="1"> 705 <outline> 706 <contour> 707 <point x="1" y="a" type="move"/> 708 </contour> 709 </outline> 710 </glyph> 711 """ 712 py = """ 713 glyph.name = "a" 714 pointPen.beginPath() 715 pointPen.addPoint(*[(1, "a")], **{"segmentType" : "move", "smooth" : False}) 716 pointPen.endPath() 717 """ 718 self.assertRaises(GlifLibError, self.glyphToGLIF, py) 719 self.assertRaises(GlifLibError, self.glifToPy, glif) 720 721 def testPointTypeMove(self): 722 # legal 723 glif = """ 724 <glyph name="a" format="1"> 725 <outline> 726 <contour> 727 <point x="1" y="-2" type="move"/> 728 <point x="3" y="-4" type="line"/> 729 </contour> 730 </outline> 731 </glyph> 732 """ 733 py = """ 734 glyph.name = "a" 735 pointPen.beginPath() 736 pointPen.addPoint(*[(1, -2)], **{"segmentType" : "move", "smooth" : False}) 737 pointPen.addPoint(*[(3, -4)], **{"segmentType" : "line", "smooth" : False}) 738 pointPen.endPath() 739 """ 740 resultGlif = self.glyphToGLIF(py) 741 resultPy = self.glifToPy(glif) 742 self.assertEqual(glif, resultGlif) 743 self.assertEqual(py, resultPy) 744 # illegal: smooth=True 745 glif = """ 746 <glyph name="a" format="1"> 747 <outline> 748 <contour> 749 <point x="1" y="-2" type="move" smooth="yes"/> 750 <point x="3" y="-4" type="line"/> 751 </contour> 752 </outline> 753 </glyph> 754 """ 755 py = """ 756 glyph.name = "a" 757 pointPen.beginPath() 758 pointPen.addPoint(*[(1, -2)], **{"segmentType" : "move", "smooth" : True}) 759 pointPen.addPoint(*[(3, -4)], **{"segmentType" : "line", "smooth" : False}) 760 pointPen.endPath() 761 """ 762 self.assertRaises(GlifLibError, self.glyphToGLIF, py) 763 self.assertRaises(GlifLibError, self.glifToPy, glif) 764 # illegal: not at start 765 glif = """ 766 <glyph name="a" format="1"> 767 <outline> 768 <contour> 769 <point x="3" y="-4" type="line"/> 770 <point x="1" y="-2" type="move"/> 771 </contour> 772 </outline> 773 </glyph> 774 """ 775 py = """ 776 glyph.name = "a" 777 pointPen.beginPath() 778 pointPen.addPoint(*[(3, -4)], **{"segmentType" : "line", "smooth" : False}) 779 pointPen.addPoint(*[(1, -2)], **{"segmentType" : "move", "smooth" : False}) 780 pointPen.endPath() 781 """ 782 self.assertRaises(GlifLibError, self.glyphToGLIF, py) 783 self.assertRaises(GlifLibError, self.glifToPy, glif) 784 785 def testPointTypeLine(self): 786 # legal 787 glif = """ 788 <glyph name="a" format="1"> 789 <outline> 790 <contour> 791 <point x="1" y="-2" type="move"/> 792 <point x="3" y="-4" type="line"/> 793 </contour> 794 </outline> 795 </glyph> 796 """ 797 py = """ 798 glyph.name = "a" 799 pointPen.beginPath() 800 pointPen.addPoint(*[(1, -2)], **{"segmentType" : "move", "smooth" : False}) 801 pointPen.addPoint(*[(3, -4)], **{"segmentType" : "line", "smooth" : False}) 802 pointPen.endPath() 803 """ 804 resultGlif = self.glyphToGLIF(py) 805 resultPy = self.glifToPy(glif) 806 self.assertEqual(glif, resultGlif) 807 self.assertEqual(py, resultPy) 808 # legal: start of contour 809 glif = """ 810 <glyph name="a" format="1"> 811 <outline> 812 <contour> 813 <point x="1" y="-2" type="line"/> 814 <point x="3" y="-4" type="line"/> 815 </contour> 816 </outline> 817 </glyph> 818 """ 819 py = """ 820 glyph.name = "a" 821 pointPen.beginPath() 822 pointPen.addPoint(*[(1, -2)], **{"segmentType" : "line", "smooth" : False}) 823 pointPen.addPoint(*[(3, -4)], **{"segmentType" : "line", "smooth" : False}) 824 pointPen.endPath() 825 """ 826 resultGlif = self.glyphToGLIF(py) 827 resultPy = self.glifToPy(glif) 828 self.assertEqual(glif, resultGlif) 829 self.assertEqual(py, resultPy) 830 # illegal: smooth=True 831 glif = """ 832 <glyph name="a" format="1"> 833 <outline> 834 <contour> 835 <point x="1" y="-2" type="move"/> 836 <point x="3" y="-4" type="line" smooth="yes"/> 837 </contour> 838 </outline> 839 </glyph> 840 """ 841 py = """ 842 glyph.name = "a" 843 pointPen.beginPath() 844 pointPen.addPoint(*[(1, -2)], **{"segmentType" : "move", "smooth" : False}) 845 pointPen.addPoint(*[(3, -4)], **{"segmentType" : "line", "smooth" : True}) 846 pointPen.endPath() 847 """ 848 self.assertRaises(GlifLibError, self.glyphToGLIF, py) 849 self.assertRaises(GlifLibError, self.glifToPy, glif) 850 851 def testPointTypeCurve(self): 852 # legal 853 glif = """ 854 <glyph name="a" format="1"> 855 <outline> 856 <contour> 857 <point x="0" y="0" type="move"/> 858 <point x="0" y="65"/> 859 <point x="65" y="200"/> 860 <point x="100" y="200" type="curve"/> 861 </contour> 862 </outline> 863 </glyph> 864 """ 865 py = """ 866 glyph.name = "a" 867 pointPen.beginPath() 868 pointPen.addPoint(*[(0, 0)], **{"segmentType" : "move", "smooth" : False}) 869 pointPen.addPoint(*[(0, 65)], **{"smooth" : False}) 870 pointPen.addPoint(*[(65, 200)], **{"smooth" : False}) 871 pointPen.addPoint(*[(100, 200)], **{"segmentType" : "curve", "smooth" : False}) 872 pointPen.endPath() 873 """ 874 resultGlif = self.glyphToGLIF(py) 875 resultPy = self.glifToPy(glif) 876 self.assertEqual(glif, resultGlif) 877 self.assertEqual(py, resultPy) 878 # legal: start of contour 879 glif = """ 880 <glyph name="a" format="1"> 881 <outline> 882 <contour> 883 <point x="100" y="200" type="curve"/> 884 <point x="0" y="65"/> 885 <point x="65" y="200"/> 886 </contour> 887 </outline> 888 </glyph> 889 """ 890 py = """ 891 glyph.name = "a" 892 pointPen.beginPath() 893 pointPen.addPoint(*[(100, 200)], **{"segmentType" : "curve", "smooth" : False}) 894 pointPen.addPoint(*[(0, 65)], **{"smooth" : False}) 895 pointPen.addPoint(*[(65, 200)], **{"smooth" : False}) 896 pointPen.endPath() 897 """ 898 resultGlif = self.glyphToGLIF(py) 899 resultPy = self.glifToPy(glif) 900 self.assertEqual(glif, resultGlif) 901 self.assertEqual(py, resultPy) 902 # legal: smooth=True 903 glif = """ 904 <glyph name="a" format="1"> 905 <outline> 906 <contour> 907 <point x="0" y="0" type="move"/> 908 <point x="0" y="65"/> 909 <point x="65" y="200"/> 910 <point x="100" y="200" type="curve" smooth="yes"/> 911 </contour> 912 </outline> 913 </glyph> 914 """ 915 py = """ 916 glyph.name = "a" 917 pointPen.beginPath() 918 pointPen.addPoint(*[(0, 0)], **{"segmentType" : "move", "smooth" : False}) 919 pointPen.addPoint(*[(0, 65)], **{"smooth" : False}) 920 pointPen.addPoint(*[(65, 200)], **{"smooth" : False}) 921 pointPen.addPoint(*[(100, 200)], **{"segmentType" : "curve", "smooth" : True}) 922 pointPen.endPath() 923 """ 924 resultGlif = self.glyphToGLIF(py) 925 resultPy = self.glifToPy(glif) 926 self.assertEqual(glif, resultGlif) 927 self.assertEqual(py, resultPy) 928 # legal: no off-curves 929 glif = """ 930 <glyph name="a" format="1"> 931 <outline> 932 <contour> 933 <point x="0" y="0" type="move"/> 934 <point x="100" y="200" type="curve"/> 935 </contour> 936 </outline> 937 </glyph> 938 """ 939 py = """ 940 glyph.name = "a" 941 pointPen.beginPath() 942 pointPen.addPoint(*[(0, 0)], **{"segmentType" : "move", "smooth" : False}) 943 pointPen.addPoint(*[(100, 200)], **{"segmentType" : "curve", "smooth" : False}) 944 pointPen.endPath() 945 """ 946 resultGlif = self.glyphToGLIF(py) 947 resultPy = self.glifToPy(glif) 948 self.assertEqual(glif, resultGlif) 949 self.assertEqual(py, resultPy) 950 # legal: 1 off-curve 951 glif = """ 952 <glyph name="a" format="1"> 953 <outline> 954 <contour> 955 <point x="0" y="0" type="move"/> 956 <point x="50" y="100"/> 957 <point x="100" y="200" type="curve"/> 958 </contour> 959 </outline> 960 </glyph> 961 """ 962 py = """ 963 glyph.name = "a" 964 pointPen.beginPath() 965 pointPen.addPoint(*[(0, 0)], **{"segmentType" : "move", "smooth" : False}) 966 pointPen.addPoint(*[(50, 100)], **{"smooth" : False}) 967 pointPen.addPoint(*[(100, 200)], **{"segmentType" : "curve", "smooth" : False}) 968 pointPen.endPath() 969 """ 970 resultGlif = self.glyphToGLIF(py) 971 resultPy = self.glifToPy(glif) 972 self.assertEqual(glif, resultGlif) 973 self.assertEqual(py, resultPy) 974 # illegal: 3 off-curves 975 glif = """ 976 <glyph name="a" format="1"> 977 <outline> 978 <contour> 979 <point x="0" y="0" type="move"/> 980 <point x="0" y="100"/> 981 <point x="35" y="125"/> 982 <point x="65" y="200"/> 983 <point x="100" y="200" type="curve"/> 984 </contour> 985 </outline> 986 </glyph> 987 """ 988 py = """ 989 glyph.name = "a" 990 pointPen.beginPath() 991 pointPen.addPoint(*[(0, 0)], **{"segmentType" : "move", "smooth" : False}) 992 pointPen.addPoint(*[(0, 100)], **{"smooth" : False}) 993 pointPen.addPoint(*[(35, 125)], **{"smooth" : False}) 994 pointPen.addPoint(*[(65, 200)], **{"smooth" : False}) 995 pointPen.addPoint(*[(100, 200)], **{"segmentType" : "curve", "smooth" : False}) 996 pointPen.endPath() 997 """ 998 self.assertRaises(GlifLibError, self.glyphToGLIF, py) 999 self.assertRaises(GlifLibError, self.glifToPy, glif) 1000 1001 def testPointQCurve(self): 1002 # legal 1003 glif = """ 1004 <glyph name="a" format="1"> 1005 <outline> 1006 <contour> 1007 <point x="0" y="0" type="move"/> 1008 <point x="0" y="65"/> 1009 <point x="65" y="200"/> 1010 <point x="100" y="200" type="qcurve"/> 1011 </contour> 1012 </outline> 1013 </glyph> 1014 """ 1015 py = """ 1016 glyph.name = "a" 1017 pointPen.beginPath() 1018 pointPen.addPoint(*[(0, 0)], **{"segmentType" : "move", "smooth" : False}) 1019 pointPen.addPoint(*[(0, 65)], **{"smooth" : False}) 1020 pointPen.addPoint(*[(65, 200)], **{"smooth" : False}) 1021 pointPen.addPoint(*[(100, 200)], **{"segmentType" : "qcurve", "smooth" : False}) 1022 pointPen.endPath() 1023 """ 1024 resultGlif = self.glyphToGLIF(py) 1025 resultPy = self.glifToPy(glif) 1026 self.assertEqual(glif, resultGlif) 1027 self.assertEqual(py, resultPy) 1028 # legal: start of contour 1029 glif = """ 1030 <glyph name="a" format="1"> 1031 <outline> 1032 <contour> 1033 <point x="100" y="200" type="qcurve"/> 1034 <point x="0" y="65"/> 1035 <point x="65" y="200"/> 1036 </contour> 1037 </outline> 1038 </glyph> 1039 """ 1040 py = """ 1041 glyph.name = "a" 1042 pointPen.beginPath() 1043 pointPen.addPoint(*[(100, 200)], **{"segmentType" : "qcurve", "smooth" : False}) 1044 pointPen.addPoint(*[(0, 65)], **{"smooth" : False}) 1045 pointPen.addPoint(*[(65, 200)], **{"smooth" : False}) 1046 pointPen.endPath() 1047 """ 1048 resultGlif = self.glyphToGLIF(py) 1049 resultPy = self.glifToPy(glif) 1050 self.assertEqual(glif, resultGlif) 1051 self.assertEqual(py, resultPy) 1052 # legal: smooth=True 1053 glif = """ 1054 <glyph name="a" format="1"> 1055 <outline> 1056 <contour> 1057 <point x="0" y="0" type="move"/> 1058 <point x="0" y="65"/> 1059 <point x="65" y="200"/> 1060 <point x="100" y="200" type="qcurve" smooth="yes"/> 1061 </contour> 1062 </outline> 1063 </glyph> 1064 """ 1065 py = """ 1066 glyph.name = "a" 1067 pointPen.beginPath() 1068 pointPen.addPoint(*[(0, 0)], **{"segmentType" : "move", "smooth" : False}) 1069 pointPen.addPoint(*[(0, 65)], **{"smooth" : False}) 1070 pointPen.addPoint(*[(65, 200)], **{"smooth" : False}) 1071 pointPen.addPoint(*[(100, 200)], **{"segmentType" : "qcurve", "smooth" : True}) 1072 pointPen.endPath() 1073 """ 1074 resultGlif = self.glyphToGLIF(py) 1075 resultPy = self.glifToPy(glif) 1076 self.assertEqual(glif, resultGlif) 1077 self.assertEqual(py, resultPy) 1078 # legal: no off-curves 1079 glif = """ 1080 <glyph name="a" format="1"> 1081 <outline> 1082 <contour> 1083 <point x="0" y="0" type="move"/> 1084 <point x="100" y="200" type="qcurve"/> 1085 </contour> 1086 </outline> 1087 </glyph> 1088 """ 1089 py = """ 1090 glyph.name = "a" 1091 pointPen.beginPath() 1092 pointPen.addPoint(*[(0, 0)], **{"segmentType" : "move", "smooth" : False}) 1093 pointPen.addPoint(*[(100, 200)], **{"segmentType" : "qcurve", "smooth" : False}) 1094 pointPen.endPath() 1095 """ 1096 resultGlif = self.glyphToGLIF(py) 1097 resultPy = self.glifToPy(glif) 1098 self.assertEqual(glif, resultGlif) 1099 self.assertEqual(py, resultPy) 1100 # legal: 1 off-curve 1101 glif = """ 1102 <glyph name="a" format="1"> 1103 <outline> 1104 <contour> 1105 <point x="0" y="0" type="move"/> 1106 <point x="50" y="100"/> 1107 <point x="100" y="200" type="qcurve"/> 1108 </contour> 1109 </outline> 1110 </glyph> 1111 """ 1112 py = """ 1113 glyph.name = "a" 1114 pointPen.beginPath() 1115 pointPen.addPoint(*[(0, 0)], **{"segmentType" : "move", "smooth" : False}) 1116 pointPen.addPoint(*[(50, 100)], **{"smooth" : False}) 1117 pointPen.addPoint(*[(100, 200)], **{"segmentType" : "qcurve", "smooth" : False}) 1118 pointPen.endPath() 1119 """ 1120 resultGlif = self.glyphToGLIF(py) 1121 resultPy = self.glifToPy(glif) 1122 self.assertEqual(glif, resultGlif) 1123 self.assertEqual(py, resultPy) 1124 # legal: 3 off-curves 1125 glif = """ 1126 <glyph name="a" format="1"> 1127 <outline> 1128 <contour> 1129 <point x="0" y="0" type="move"/> 1130 <point x="0" y="100"/> 1131 <point x="35" y="125"/> 1132 <point x="65" y="200"/> 1133 <point x="100" y="200" type="qcurve"/> 1134 </contour> 1135 </outline> 1136 </glyph> 1137 """ 1138 py = """ 1139 glyph.name = "a" 1140 pointPen.beginPath() 1141 pointPen.addPoint(*[(0, 0)], **{"segmentType" : "move", "smooth" : False}) 1142 pointPen.addPoint(*[(0, 100)], **{"smooth" : False}) 1143 pointPen.addPoint(*[(35, 125)], **{"smooth" : False}) 1144 pointPen.addPoint(*[(65, 200)], **{"smooth" : False}) 1145 pointPen.addPoint(*[(100, 200)], **{"segmentType" : "qcurve", "smooth" : False}) 1146 pointPen.endPath() 1147 """ 1148 resultGlif = self.glyphToGLIF(py) 1149 resultPy = self.glifToPy(glif) 1150 self.assertEqual(glif, resultGlif) 1151 self.assertEqual(py, resultPy) 1152 1153 def testPointTypeOffCurve(self): 1154 # legal 1155 glif = """ 1156 <glyph name="a" format="1"> 1157 <outline> 1158 <contour> 1159 <point x="0" y="0" type="move"/> 1160 <point x="0" y="65"/> 1161 <point x="65" y="200"/> 1162 <point x="100" y="200" type="curve"/> 1163 </contour> 1164 </outline> 1165 </glyph> 1166 """ 1167 py = """ 1168 glyph.name = "a" 1169 pointPen.beginPath() 1170 pointPen.addPoint(*[(0, 0)], **{"segmentType" : "move", "smooth" : False}) 1171 pointPen.addPoint(*[(0, 65)], **{"smooth" : False}) 1172 pointPen.addPoint(*[(65, 200)], **{"smooth" : False}) 1173 pointPen.addPoint(*[(100, 200)], **{"segmentType" : "curve", "smooth" : False}) 1174 pointPen.endPath() 1175 """ 1176 resultGlif = self.glyphToGLIF(py) 1177 resultPy = self.glifToPy(glif) 1178 self.assertEqual(glif, resultGlif) 1179 self.assertEqual(py, resultPy) 1180 # legal: start of contour 1181 glif = """ 1182 <glyph name="a" format="1"> 1183 <outline> 1184 <contour> 1185 <point x="0" y="65"/> 1186 <point x="65" y="200"/> 1187 <point x="100" y="200" type="curve"/> 1188 </contour> 1189 </outline> 1190 </glyph> 1191 """ 1192 py = """ 1193 glyph.name = "a" 1194 pointPen.beginPath() 1195 pointPen.addPoint(*[(0, 65)], **{"smooth" : False}) 1196 pointPen.addPoint(*[(65, 200)], **{"smooth" : False}) 1197 pointPen.addPoint(*[(100, 200)], **{"segmentType" : "curve", "smooth" : False}) 1198 pointPen.endPath() 1199 """ 1200 resultGlif = self.glyphToGLIF(py) 1201 resultPy = self.glifToPy(glif) 1202 self.assertEqual(glif, resultGlif) 1203 self.assertEqual(py, resultPy) 1204 # before move 1205 glif = """ 1206 <glyph name="a" format="1"> 1207 <outline> 1208 <contour> 1209 <point x="0" y="65"/> 1210 <point x="0" y="0" type="move"/> 1211 </contour> 1212 </outline> 1213 </glyph> 1214 """ 1215 py = """ 1216 glyph.name = "a" 1217 pointPen.beginPath() 1218 pointPen.addPoint(*[(0, 65)], **{"smooth" : False}) 1219 pointPen.addPoint(*[(0, 0)], **{"segmentType" : "move", "smooth" : False}) 1220 pointPen.endPath() 1221 """ 1222 self.assertRaises(GlifLibError, self.glyphToGLIF, py) 1223 self.assertRaises(GlifLibError, self.glifToPy, glif) 1224 # before line 1225 glif = """ 1226 <glyph name="a" format="1"> 1227 <outline> 1228 <contour> 1229 <point x="0" y="65"/> 1230 <point x="0" y="0" type="line"/> 1231 </contour> 1232 </outline> 1233 </glyph> 1234 """ 1235 py = """ 1236 glyph.name = "a" 1237 pointPen.beginPath() 1238 pointPen.addPoint(*[(0, 65)], **{"smooth" : False}) 1239 pointPen.addPoint(*[(0, 0)], **{"segmentType" : "line", "smooth" : False}) 1240 pointPen.endPath() 1241 """ 1242 self.assertRaises(GlifLibError, self.glyphToGLIF, py) 1243 self.assertRaises(GlifLibError, self.glifToPy, glif) 1244 # smooth=True 1245 glif = """ 1246 <glyph name="a" format="1"> 1247 <outline> 1248 <contour> 1249 <point x="0" y="65" smooth="yess"/> 1250 <point x="0" y="0" type="curve"/> 1251 </contour> 1252 </outline> 1253 </glyph> 1254 """ 1255 py = """ 1256 glyph.name = "a" 1257 pointPen.beginPath() 1258 pointPen.addPoint(*[(0, 65)], **{"smooth" : True}) 1259 pointPen.addPoint(*[(0, 0)], **{"segmentType" : "curve", "smooth" : False}) 1260 pointPen.endPath() 1261 """ 1262 self.assertRaises(GlifLibError, self.glyphToGLIF, py) 1263 self.assertRaises(GlifLibError, self.glifToPy, glif) 1264 411 1265 412 1266 if __name__ == "__main__":
Note: See TracChangeset
for help on using the changeset viewer.
