~ubuntu-branches/ubuntu/maverick/fonttools/maverick

« back to all changes in this revision

Viewing changes to Lib/fontTools/pens/boundsPen.py

  • Committer: Bazaar Package Importer
  • Author(s): Anthony Fok
  • Date: 2003-11-18 00:53:59 UTC
  • Revision ID: james.westby@ubuntu.com-20031118005359-pqirsxbgdz0f0xmx
Tags: upstream-1.99+2.0b1+cvs20031014
ImportĀ upstreamĀ versionĀ 1.99+2.0b1+cvs20031014

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from fontTools.pens.basePen import BasePen
 
2
from fontTools.misc.arrayTools import updateBounds, pointInRect, unionRect
 
3
from fontTools.misc.bezierTools import calcCubicBounds, calcQuadraticBounds
 
4
 
 
5
 
 
6
__all__ = ["BoundsPen", "ControlBoundsPen"]
 
7
 
 
8
 
 
9
class ControlBoundsPen(BasePen):
 
10
 
 
11
        """Pen to calculate the "control bounds" of a shape. This is the
 
12
        bounding box of all control points, so may be larger than the
 
13
        actual bounding box if there are curves that don't have points
 
14
        on their extremes.
 
15
 
 
16
        When the shape has been drawn, the bounds are available as the
 
17
        'bounds' attribute of the pen object. It's a 4-tuple:
 
18
                (xMin, yMin, xMax, yMax)
 
19
        """
 
20
 
 
21
        def __init__(self, glyphSet):
 
22
                BasePen.__init__(self, glyphSet)
 
23
                self.bounds = None
 
24
 
 
25
        def _moveTo(self, pt):
 
26
                bounds = self.bounds
 
27
                if bounds:
 
28
                        self.bounds = updateBounds(bounds, pt)
 
29
                else:
 
30
                        x, y = pt
 
31
                        self.bounds = (x, y, x, y)
 
32
 
 
33
        def _lineTo(self, pt):
 
34
                self.bounds = updateBounds(self.bounds, pt)
 
35
 
 
36
        def _curveToOne(self, bcp1, bcp2, pt):
 
37
                bounds = self.bounds
 
38
                bounds = updateBounds(bounds, bcp1)
 
39
                bounds = updateBounds(bounds, bcp2)
 
40
                bounds = updateBounds(bounds, pt)
 
41
                self.bounds = bounds
 
42
 
 
43
        def _qCurveToOne(self, bcp, pt):
 
44
                bounds = self.bounds
 
45
                bounds = updateBounds(bounds, bcp)
 
46
                bounds = updateBounds(bounds, pt)
 
47
                self.bounds = bounds
 
48
 
 
49
 
 
50
class BoundsPen(ControlBoundsPen):
 
51
 
 
52
        """Pen to calculate the bounds of a shape. It calculates the
 
53
        correct bounds even when the shape contains curves that don't
 
54
        have points on their extremes. This is somewhat slower to compute
 
55
        than the "control bounds".
 
56
 
 
57
        When the shape has been drawn, the bounds are available as the
 
58
        'bounds' attribute of the pen object. It's a 4-tuple:
 
59
                (xMin, yMin, xMax, yMax)
 
60
        """
 
61
 
 
62
        def _curveToOne(self, bcp1, bcp2, pt):
 
63
                bounds = self.bounds
 
64
                bounds = updateBounds(bounds, pt)
 
65
                if not pointInRect(bcp1, bounds) or not pointInRect(bcp2, bounds):
 
66
                        bounds = unionRect(bounds, calcCubicBounds(
 
67
                                        self._getCurrentPoint(), bcp1, bcp2, pt))
 
68
                self.bounds = bounds
 
69
 
 
70
        def _qCurveToOne(self, bcp, pt):
 
71
                bounds = self.bounds
 
72
                bounds = updateBounds(bounds, pt)
 
73
                if not pointInRect(bcp, bounds):
 
74
                        bounds = unionRect(bounds, calcQuadraticBounds(
 
75
                                        self._getCurrentPoint(), bcp, pt))
 
76
                self.bounds = bounds
 
77
 
 
78
 
 
79
if __name__ == "__main__":
 
80
        def draw(pen):
 
81
                pen.moveTo((0, 0))
 
82
                pen.lineTo((0, 100))
 
83
                pen.qCurveTo((50, 75), (60, 50), (50, 25), (0, 0))
 
84
                pen.curveTo((-50, 25), (-60, 50), (-50, 75), (0, 100))
 
85
                pen.closePath()
 
86
 
 
87
        pen = ControlBoundsPen(None)
 
88
        draw(pen)
 
89
        print pen.bounds
 
90
 
 
91
        pen = BoundsPen(None)
 
92
        draw(pen)
 
93
        print pen.bounds