Changeset 318
- Timestamp:
- 01/15/09 02:31:17 (4 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
packages/defcon/branches/ufo2/Lib/defcon/objects/glyph.py
r314 r318 548 548 return pen.getResult() 549 549 550 def autoContourDirection(self, baseDirectionIsClockwise=False):551 """552 Try to set the "proper" contour direction in the glyph.553 554 Known Problems:555 556 * Speed. This could be sped up by not testing every557 point in overlapping contours, but that would558 result in less accurate results.559 * Contours with points that overlap, but lines560 which intersect with the other contour. Consider561 an "A" with the crossbar defined as a rectangle.562 """563 baseDirection = baseDirectionIsClockwise564 # if only one contour is present, set565 # the default direction and return566 contourCount = len(self._contours)567 if contourCount < 2:568 for contour in self._contours:569 contour.clockwise = baseDirection570 return571 #572 countIter = xrange(contourCount)573 intersections = {}574 for index1 in countIter:575 for index2 in countIter:576 # don't test the same contour with itself577 if index1 == index2:578 continue579 # test for intersection of the two contours580 bounds1 = self._contours[index1].bounds581 bounds2 = self._contours[index2].bounds582 intersects, position = arrayTools.sectRect(bounds1, bounds2)583 if intersects:584 # only flag the contours if they are complete overlaps585 combinedRect = arrayTools.unionRect(bounds1, bounds2)586 if combinedRect == bounds1 or combinedRect == bounds2:587 if index1 not in intersections:588 intersections[index1] = []589 intersections[index1].append(index2)590 for index in countIter:591 direction = baseDirection592 contour = self[index]593 intersectingContours = intersections.get(index)594 if intersectingContours:595 for otherContourIndex in intersectingContours:596 otherContour = self[otherContourIndex]597 foundPointOutside = False598 for point in contour:599 if not point.segmentType:600 continue601 pt = (point.x, point.y)602 if not otherContour.pointInside(pt):603 foundPointOutside = True604 break605 if foundPointOutside:606 continue607 direction += 1608 contour.clockwise = direction % 2609 610 550 # --------------- 611 551 # Representations … … 1114 1054 """ 1115 1055 1116 def _testAutoContourDirection():1117 """1118 >>> from defcon.test.testTools import getTestFontPath1119 >>> from defcon.objects.font import Font1120 >>> font = Font(getTestFontPath('TestContourDirection.ufo'))1121 >>> glyph = font['TestContourDirection1']1122 >>> for contour in glyph:1123 ... contour.clockwise = True1124 >>> glyph.autoContourDirection()1125 >>> [contour.clockwise for contour in glyph]1126 [False, True, False]1127 >>> glyph = font['TestContourDirection2']1128 >>> for contour in glyph:1129 ... contour.clockwise = True1130 >>> glyph.autoContourDirection()1131 >>> [contour.clockwise for contour in glyph]1132 [False, False, True]1133 >>> glyph = font['TestContourDirection3']1134 >>> for contour in glyph:1135 ... contour.clockwise = True1136 >>> glyph.autoContourDirection()1137 >>> [contour.clockwise for contour in glyph]1138 [False, True, True, True, True]1139 >>> glyph = font['TestContourDirection4']1140 >>> for contour in glyph:1141 ... contour.clockwise = True1142 >>> glyph.autoContourDirection()1143 >>> [contour.clockwise for contour in glyph]1144 [False, False, True]1145 """1146 1147 1056 if __name__ == "__main__": 1148 1057 import doctest
Note: See TracChangeset
for help on using the changeset viewer.
