explicitClosingLinePen.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. from fontTools.pens.filterPen import ContourFilterPen
  2. class ExplicitClosingLinePen(ContourFilterPen):
  3. """A filter pen that adds an explicit lineTo to the first point of each closed
  4. contour if the end point of the last segment is not already the same as the first point.
  5. Otherwise, it passes the contour through unchanged.
  6. >>> from pprint import pprint
  7. >>> from fontTools.pens.recordingPen import RecordingPen
  8. >>> rec = RecordingPen()
  9. >>> pen = ExplicitClosingLinePen(rec)
  10. >>> pen.moveTo((0, 0))
  11. >>> pen.lineTo((100, 0))
  12. >>> pen.lineTo((100, 100))
  13. >>> pen.closePath()
  14. >>> pprint(rec.value)
  15. [('moveTo', ((0, 0),)),
  16. ('lineTo', ((100, 0),)),
  17. ('lineTo', ((100, 100),)),
  18. ('lineTo', ((0, 0),)),
  19. ('closePath', ())]
  20. >>> rec = RecordingPen()
  21. >>> pen = ExplicitClosingLinePen(rec)
  22. >>> pen.moveTo((0, 0))
  23. >>> pen.lineTo((100, 0))
  24. >>> pen.lineTo((100, 100))
  25. >>> pen.lineTo((0, 0))
  26. >>> pen.closePath()
  27. >>> pprint(rec.value)
  28. [('moveTo', ((0, 0),)),
  29. ('lineTo', ((100, 0),)),
  30. ('lineTo', ((100, 100),)),
  31. ('lineTo', ((0, 0),)),
  32. ('closePath', ())]
  33. >>> rec = RecordingPen()
  34. >>> pen = ExplicitClosingLinePen(rec)
  35. >>> pen.moveTo((0, 0))
  36. >>> pen.curveTo((100, 0), (0, 100), (100, 100))
  37. >>> pen.closePath()
  38. >>> pprint(rec.value)
  39. [('moveTo', ((0, 0),)),
  40. ('curveTo', ((100, 0), (0, 100), (100, 100))),
  41. ('lineTo', ((0, 0),)),
  42. ('closePath', ())]
  43. >>> rec = RecordingPen()
  44. >>> pen = ExplicitClosingLinePen(rec)
  45. >>> pen.moveTo((0, 0))
  46. >>> pen.curveTo((100, 0), (0, 100), (100, 100))
  47. >>> pen.lineTo((0, 0))
  48. >>> pen.closePath()
  49. >>> pprint(rec.value)
  50. [('moveTo', ((0, 0),)),
  51. ('curveTo', ((100, 0), (0, 100), (100, 100))),
  52. ('lineTo', ((0, 0),)),
  53. ('closePath', ())]
  54. >>> rec = RecordingPen()
  55. >>> pen = ExplicitClosingLinePen(rec)
  56. >>> pen.moveTo((0, 0))
  57. >>> pen.curveTo((100, 0), (0, 100), (0, 0))
  58. >>> pen.closePath()
  59. >>> pprint(rec.value)
  60. [('moveTo', ((0, 0),)),
  61. ('curveTo', ((100, 0), (0, 100), (0, 0))),
  62. ('closePath', ())]
  63. >>> rec = RecordingPen()
  64. >>> pen = ExplicitClosingLinePen(rec)
  65. >>> pen.moveTo((0, 0))
  66. >>> pen.closePath()
  67. >>> pprint(rec.value)
  68. [('moveTo', ((0, 0),)), ('closePath', ())]
  69. >>> rec = RecordingPen()
  70. >>> pen = ExplicitClosingLinePen(rec)
  71. >>> pen.closePath()
  72. >>> pprint(rec.value)
  73. [('closePath', ())]
  74. >>> rec = RecordingPen()
  75. >>> pen = ExplicitClosingLinePen(rec)
  76. >>> pen.moveTo((0, 0))
  77. >>> pen.lineTo((100, 0))
  78. >>> pen.lineTo((100, 100))
  79. >>> pen.endPath()
  80. >>> pprint(rec.value)
  81. [('moveTo', ((0, 0),)),
  82. ('lineTo', ((100, 0),)),
  83. ('lineTo', ((100, 100),)),
  84. ('endPath', ())]
  85. """
  86. def filterContour(self, contour):
  87. if (
  88. not contour
  89. or contour[0][0] != "moveTo"
  90. or contour[-1][0] != "closePath"
  91. or len(contour) < 3
  92. ):
  93. return
  94. movePt = contour[0][1][0]
  95. lastSeg = contour[-2][1]
  96. if lastSeg and movePt != lastSeg[-1]:
  97. contour[-1:] = [("lineTo", (movePt,)), ("closePath", ())]