xref: /aosp_15_r20/external/fonttools/Lib/fontTools/pens/quartzPen.py (revision e1fe3e4ad2793916b15cccdc4a7da52a7e1dd0e9)
1*e1fe3e4aSElliott Hughesfrom fontTools.pens.basePen import BasePen
2*e1fe3e4aSElliott Hughes
3*e1fe3e4aSElliott Hughesfrom Quartz.CoreGraphics import CGPathCreateMutable, CGPathMoveToPoint
4*e1fe3e4aSElliott Hughesfrom Quartz.CoreGraphics import CGPathAddLineToPoint, CGPathAddCurveToPoint
5*e1fe3e4aSElliott Hughesfrom Quartz.CoreGraphics import CGPathAddQuadCurveToPoint, CGPathCloseSubpath
6*e1fe3e4aSElliott Hughes
7*e1fe3e4aSElliott Hughes
8*e1fe3e4aSElliott Hughes__all__ = ["QuartzPen"]
9*e1fe3e4aSElliott Hughes
10*e1fe3e4aSElliott Hughes
11*e1fe3e4aSElliott Hughesclass QuartzPen(BasePen):
12*e1fe3e4aSElliott Hughes    """A pen that creates a CGPath
13*e1fe3e4aSElliott Hughes
14*e1fe3e4aSElliott Hughes    Parameters
15*e1fe3e4aSElliott Hughes    - path: an optional CGPath to add to
16*e1fe3e4aSElliott Hughes    - xform: an optional CGAffineTransform to apply to the path
17*e1fe3e4aSElliott Hughes    """
18*e1fe3e4aSElliott Hughes
19*e1fe3e4aSElliott Hughes    def __init__(self, glyphSet, path=None, xform=None):
20*e1fe3e4aSElliott Hughes        BasePen.__init__(self, glyphSet)
21*e1fe3e4aSElliott Hughes        if path is None:
22*e1fe3e4aSElliott Hughes            path = CGPathCreateMutable()
23*e1fe3e4aSElliott Hughes        self.path = path
24*e1fe3e4aSElliott Hughes        self.xform = xform
25*e1fe3e4aSElliott Hughes
26*e1fe3e4aSElliott Hughes    def _moveTo(self, pt):
27*e1fe3e4aSElliott Hughes        x, y = pt
28*e1fe3e4aSElliott Hughes        CGPathMoveToPoint(self.path, self.xform, x, y)
29*e1fe3e4aSElliott Hughes
30*e1fe3e4aSElliott Hughes    def _lineTo(self, pt):
31*e1fe3e4aSElliott Hughes        x, y = pt
32*e1fe3e4aSElliott Hughes        CGPathAddLineToPoint(self.path, self.xform, x, y)
33*e1fe3e4aSElliott Hughes
34*e1fe3e4aSElliott Hughes    def _curveToOne(self, p1, p2, p3):
35*e1fe3e4aSElliott Hughes        (x1, y1), (x2, y2), (x3, y3) = p1, p2, p3
36*e1fe3e4aSElliott Hughes        CGPathAddCurveToPoint(self.path, self.xform, x1, y1, x2, y2, x3, y3)
37*e1fe3e4aSElliott Hughes
38*e1fe3e4aSElliott Hughes    def _qCurveToOne(self, p1, p2):
39*e1fe3e4aSElliott Hughes        (x1, y1), (x2, y2) = p1, p2
40*e1fe3e4aSElliott Hughes        CGPathAddQuadCurveToPoint(self.path, self.xform, x1, y1, x2, y2)
41*e1fe3e4aSElliott Hughes
42*e1fe3e4aSElliott Hughes    def _closePath(self):
43*e1fe3e4aSElliott Hughes        CGPathCloseSubpath(self.path)
44