Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

## [FontTools-checkins] CVS: fonttools/Lib/fontTools/misc bezierTools.py,1.4,1.5

 [FontTools-checkins] CVS: fonttools/Lib/fontTools/misc bezierTools.py,1.4,1.5 From: Just van Rossum - 2005-02-25 10:11:53 ```Update of /cvsroot/fonttools/fonttools/Lib/fontTools/misc In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv22117 Modified Files: bezierTools.py Log Message: some refactoring, some doctests Index: bezierTools.py =================================================================== RCS file: /cvsroot/fonttools/fonttools/Lib/fontTools/misc/bezierTools.py,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** bezierTools.py 26 Aug 2003 19:20:33 -0000 1.4 --- bezierTools.py 25 Feb 2005 10:11:30 -0000 1.5 *************** *** 12,24 **** def calcQuadraticBounds(pt1, pt2, pt3): """Return the bounding rectangle for a qudratic bezier segment. ! pt1 and pt3 are the "anchor" points, pt2 is the "handle".""" ! # convert points to Numeric arrays ! pt1, pt2, pt3 = Numeric.array((pt1, pt2, pt3)) ! ! # calc quadratic parameters ! c = pt1 ! b = (pt2 - c) * 2.0 ! a = pt3 - c - b # calc first derivative ax, ay = a * 2 --- 12,23 ---- def calcQuadraticBounds(pt1, pt2, pt3): """Return the bounding rectangle for a qudratic bezier segment. ! pt1 and pt3 are the "anchor" points, pt2 is the "handle". + >>> calcQuadraticBounds((0, 0), (50, 100), (100, 0)) + (0.0, 0.0, 100.0, 50.0) + >>> calcQuadraticBounds((0, 0), (100, 0), (100, 100)) + (0.0, 0.0, 100.0, 100.0) + """ + a, b, c = calcQuadraticParameters(pt1, pt2, pt3) # calc first derivative ax, ay = a * 2 *************** *** 35,48 **** def calcCubicBounds(pt1, pt2, pt3, pt4): """Return the bounding rectangle for a cubic bezier segment. ! pt1 and pt4 are the "anchor" points, pt2 and pt3 are the "handles".""" ! # convert points to Numeric arrays ! pt1, pt2, pt3, pt4 = Numeric.array((pt1, pt2, pt3, pt4)) ! ! # calc cubic parameters ! d = pt1 ! c = (pt2 - d) * 3.0 ! b = (pt3 - pt2) * 3.0 - c ! a = pt4 - d - c - b ! # calc first derivative ax, ay = a * 3.0 --- 34,47 ---- def calcCubicBounds(pt1, pt2, pt3, pt4): """Return the bounding rectangle for a cubic bezier segment. ! pt1 and pt4 are the "anchor" points, pt2 and pt3 are the "handles". ! ! >>> calcCubicBounds((0, 0), (25, 100), (75, 100), (100, 0)) ! (0.0, 0.0, 100.0, 75.0) ! >>> calcCubicBounds((0, 0), (50, 0), (100, 50), (100, 100)) ! (0.0, 0.0, 100.0, 100.0) ! >>> calcCubicBounds((50, 0), (0, 100), (100, 100), (50, 0)) ! (35.566243270259356, 0.0, 64.433756729740679, 75.0) ! """ ! a, b, c, d = calcCubicParameters(pt1, pt2, pt3, pt4) # calc first derivative ax, ay = a * 3.0 *************** *** 62,66 **** isHorizontal is True. Return a list of two line segments if the line was successfully split, or a list containing the original ! line.""" pt1, pt2 = Numeric.array((pt1, pt2)) a = (pt2 - pt1) --- 61,75 ---- isHorizontal is True. Return a list of two line segments if the line was successfully split, or a list containing the original ! line. ! ! >>> _tuplify(splitLine((0, 0), (100, 100), 50, True)) ! (((0, 0), (50.0, 50.0)), ((50.0, 50.0), (100, 100))) ! >>> _tuplify(splitLine((0, 0), (100, 100), 100, True)) ! (((0, 0), (100, 100)),) ! >>> _tuplify(splitLine((0, 0), (100, 100), 0, True)) ! (((0, 0), (0.0, 0.0)), ((0.0, 0.0), (100, 100))) ! >>> _tuplify(splitLine((0, 0), (100, 100), 0, False)) ! (((0, 0), (0.0, 0.0)), ((0.0, 0.0), (100, 100))) ! """ pt1, pt2 = Numeric.array((pt1, pt2)) a = (pt2 - pt1) *************** *** 80,88 **** """Split the quadratic curve between pt1, pt2 and pt3 at position 'where', which is an x coordinate if isHorizontal is False, a y coordinate if ! isHorizontal is True. Return a list of curve segments.""" ! pt1, pt2, pt3 = Numeric.array((pt1, pt2, pt3)) ! c = pt1 ! b = (pt2 - c) * 2.0 ! a = pt3 - c - b solutions = solveQuadratic(a[isHorizontal], b[isHorizontal], c[isHorizontal] - where) --- 89,107 ---- """Split the quadratic curve between pt1, pt2 and pt3 at position 'where', which is an x coordinate if isHorizontal is False, a y coordinate if ! isHorizontal is True. Return a list of curve segments. ! ! >>> splitQuadratic((0, 0), (50, 100), (100, 0), 150, False) ! [((0, 0), (50, 100), (100, 0))] ! >>> _tuplify(splitQuadratic((0, 0), (50, 100), (100, 0), 50, False)) ! (((0.0, 0.0), (25.0, 50.0), (50.0, 50.0)), ((50.0, 50.0), (75.0, 50.0), (100.0, 0.0))) ! >>> _tuplify(splitQuadratic((0, 0), (50, 100), (100, 0), 25, False)) ! (((0.0, 0.0), (12.5, 25.0), (25.0, 37.5)), ((25.0, 37.5), (62.5, 75.0), (100.0, 0.0))) ! >>> _tuplify(splitQuadratic((0, 0), (50, 100), (100, 0), 25, True)) ! (((0.0, 0.0), (7.3223304703363103, 14.644660940672621), (14.644660940672621, 24.999999999999996)), ((14.644660940672621, 24.999999999999996), (49.999999999999993, 75.0), (85.355339059327363, 25.000000000000025)), ((85.355339059327378, 25.0), (92.677669529663689, 14.644660940672621), (100.0, -7.1054273576010019e-15))) ! >>> # XXX I'm not at all sure it the following behavior is desirable: ! >>> _tuplify(splitQuadratic((0, 0), (50, 100), (100, 0), 50, True)) ! (((0.0, 0.0), (25.0, 50.0), (50.0, 50.0)), ((50.0, 50.0), (50.0, 50.0), (50.0, 50.0)), ((50.0, 50.0), (75.0, 50.0), (100.0, 0.0))) ! """ ! a, b, c = calcQuadraticParameters(pt1, pt2, pt3) solutions = solveQuadratic(a[isHorizontal], b[isHorizontal], c[isHorizontal] - where) *************** *** 115,124 **** which is an x coordinate if isHorizontal is False, a y coordinate if isHorizontal is True. Return a list of curve segments.""" ! pt1, pt2, pt3, pt4 = Numeric.array((pt1, pt2, pt3, pt4)) ! d = pt1 ! c = (pt2 - d) * 3.0 ! b = (pt3 - pt2) * 3.0 - c ! a = pt4 - d - c - b ! solutions = solveCubic(a[isHorizontal], b[isHorizontal], c[isHorizontal], d[isHorizontal] - where) --- 134,138 ---- which is an x coordinate if isHorizontal is False, a y coordinate if isHorizontal is True. Return a list of curve segments.""" ! a, b, c, d = calcCubicParameters(pt1, pt2, pt3, pt4) solutions = solveCubic(a[isHorizontal], b[isHorizontal], c[isHorizontal], d[isHorizontal] - where) *************** *** 225,226 **** --- 239,275 ---- x = x - a1/3.0 return [x] + + + def calcQuadraticParameters(pt1, pt2, pt3): + pt1, pt2, pt3 = Numeric.array((pt1, pt2, pt3)) + c = pt1 + b = (pt2 - c) * 2.0 + a = pt3 - c - b + return a, b, c + + + def calcCubicParameters(pt1, pt2, pt3, pt4): + pt1, pt2, pt3, pt4 = Numeric.array((pt1, pt2, pt3, pt4)) + d = pt1 + c = (pt2 - d) * 3.0 + b = (pt3 - pt2) * 3.0 - c + a = pt4 - d - c - b + return a, b, c, d + + + def _tuplify(obj): + """ + >>> _tuplify([1, [2, 3], [], [[2, [3, 4]]]]) + (1, (2, 3), (), ((2, (3, 4)),)) + """ + try: + it = iter(obj) + except TypeError: + return obj + else: + return tuple([_tuplify(x) for x in it]) + + + if __name__ == "__main__": + import doctest + doctest.testmod() ```

 [FontTools-checkins] CVS: fonttools/Lib/fontTools/misc bezierTools.py,1.4,1.5 From: Just van Rossum - 2005-02-25 10:11:53 ```Update of /cvsroot/fonttools/fonttools/Lib/fontTools/misc In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv22117 Modified Files: bezierTools.py Log Message: some refactoring, some doctests Index: bezierTools.py =================================================================== RCS file: /cvsroot/fonttools/fonttools/Lib/fontTools/misc/bezierTools.py,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** bezierTools.py 26 Aug 2003 19:20:33 -0000 1.4 --- bezierTools.py 25 Feb 2005 10:11:30 -0000 1.5 *************** *** 12,24 **** def calcQuadraticBounds(pt1, pt2, pt3): """Return the bounding rectangle for a qudratic bezier segment. ! pt1 and pt3 are the "anchor" points, pt2 is the "handle".""" ! # convert points to Numeric arrays ! pt1, pt2, pt3 = Numeric.array((pt1, pt2, pt3)) ! ! # calc quadratic parameters ! c = pt1 ! b = (pt2 - c) * 2.0 ! a = pt3 - c - b # calc first derivative ax, ay = a * 2 --- 12,23 ---- def calcQuadraticBounds(pt1, pt2, pt3): """Return the bounding rectangle for a qudratic bezier segment. ! pt1 and pt3 are the "anchor" points, pt2 is the "handle". + >>> calcQuadraticBounds((0, 0), (50, 100), (100, 0)) + (0.0, 0.0, 100.0, 50.0) + >>> calcQuadraticBounds((0, 0), (100, 0), (100, 100)) + (0.0, 0.0, 100.0, 100.0) + """ + a, b, c = calcQuadraticParameters(pt1, pt2, pt3) # calc first derivative ax, ay = a * 2 *************** *** 35,48 **** def calcCubicBounds(pt1, pt2, pt3, pt4): """Return the bounding rectangle for a cubic bezier segment. ! pt1 and pt4 are the "anchor" points, pt2 and pt3 are the "handles".""" ! # convert points to Numeric arrays ! pt1, pt2, pt3, pt4 = Numeric.array((pt1, pt2, pt3, pt4)) ! ! # calc cubic parameters ! d = pt1 ! c = (pt2 - d) * 3.0 ! b = (pt3 - pt2) * 3.0 - c ! a = pt4 - d - c - b ! # calc first derivative ax, ay = a * 3.0 --- 34,47 ---- def calcCubicBounds(pt1, pt2, pt3, pt4): """Return the bounding rectangle for a cubic bezier segment. ! pt1 and pt4 are the "anchor" points, pt2 and pt3 are the "handles". ! ! >>> calcCubicBounds((0, 0), (25, 100), (75, 100), (100, 0)) ! (0.0, 0.0, 100.0, 75.0) ! >>> calcCubicBounds((0, 0), (50, 0), (100, 50), (100, 100)) ! (0.0, 0.0, 100.0, 100.0) ! >>> calcCubicBounds((50, 0), (0, 100), (100, 100), (50, 0)) ! (35.566243270259356, 0.0, 64.433756729740679, 75.0) ! """ ! a, b, c, d = calcCubicParameters(pt1, pt2, pt3, pt4) # calc first derivative ax, ay = a * 3.0 *************** *** 62,66 **** isHorizontal is True. Return a list of two line segments if the line was successfully split, or a list containing the original ! line.""" pt1, pt2 = Numeric.array((pt1, pt2)) a = (pt2 - pt1) --- 61,75 ---- isHorizontal is True. Return a list of two line segments if the line was successfully split, or a list containing the original ! line. ! ! >>> _tuplify(splitLine((0, 0), (100, 100), 50, True)) ! (((0, 0), (50.0, 50.0)), ((50.0, 50.0), (100, 100))) ! >>> _tuplify(splitLine((0, 0), (100, 100), 100, True)) ! (((0, 0), (100, 100)),) ! >>> _tuplify(splitLine((0, 0), (100, 100), 0, True)) ! (((0, 0), (0.0, 0.0)), ((0.0, 0.0), (100, 100))) ! >>> _tuplify(splitLine((0, 0), (100, 100), 0, False)) ! (((0, 0), (0.0, 0.0)), ((0.0, 0.0), (100, 100))) ! """ pt1, pt2 = Numeric.array((pt1, pt2)) a = (pt2 - pt1) *************** *** 80,88 **** """Split the quadratic curve between pt1, pt2 and pt3 at position 'where', which is an x coordinate if isHorizontal is False, a y coordinate if ! isHorizontal is True. Return a list of curve segments.""" ! pt1, pt2, pt3 = Numeric.array((pt1, pt2, pt3)) ! c = pt1 ! b = (pt2 - c) * 2.0 ! a = pt3 - c - b solutions = solveQuadratic(a[isHorizontal], b[isHorizontal], c[isHorizontal] - where) --- 89,107 ---- """Split the quadratic curve between pt1, pt2 and pt3 at position 'where', which is an x coordinate if isHorizontal is False, a y coordinate if ! isHorizontal is True. Return a list of curve segments. ! ! >>> splitQuadratic((0, 0), (50, 100), (100, 0), 150, False) ! [((0, 0), (50, 100), (100, 0))] ! >>> _tuplify(splitQuadratic((0, 0), (50, 100), (100, 0), 50, False)) ! (((0.0, 0.0), (25.0, 50.0), (50.0, 50.0)), ((50.0, 50.0), (75.0, 50.0), (100.0, 0.0))) ! >>> _tuplify(splitQuadratic((0, 0), (50, 100), (100, 0), 25, False)) ! (((0.0, 0.0), (12.5, 25.0), (25.0, 37.5)), ((25.0, 37.5), (62.5, 75.0), (100.0, 0.0))) ! >>> _tuplify(splitQuadratic((0, 0), (50, 100), (100, 0), 25, True)) ! (((0.0, 0.0), (7.3223304703363103, 14.644660940672621), (14.644660940672621, 24.999999999999996)), ((14.644660940672621, 24.999999999999996), (49.999999999999993, 75.0), (85.355339059327363, 25.000000000000025)), ((85.355339059327378, 25.0), (92.677669529663689, 14.644660940672621), (100.0, -7.1054273576010019e-15))) ! >>> # XXX I'm not at all sure it the following behavior is desirable: ! >>> _tuplify(splitQuadratic((0, 0), (50, 100), (100, 0), 50, True)) ! (((0.0, 0.0), (25.0, 50.0), (50.0, 50.0)), ((50.0, 50.0), (50.0, 50.0), (50.0, 50.0)), ((50.0, 50.0), (75.0, 50.0), (100.0, 0.0))) ! """ ! a, b, c = calcQuadraticParameters(pt1, pt2, pt3) solutions = solveQuadratic(a[isHorizontal], b[isHorizontal], c[isHorizontal] - where) *************** *** 115,124 **** which is an x coordinate if isHorizontal is False, a y coordinate if isHorizontal is True. Return a list of curve segments.""" ! pt1, pt2, pt3, pt4 = Numeric.array((pt1, pt2, pt3, pt4)) ! d = pt1 ! c = (pt2 - d) * 3.0 ! b = (pt3 - pt2) * 3.0 - c ! a = pt4 - d - c - b ! solutions = solveCubic(a[isHorizontal], b[isHorizontal], c[isHorizontal], d[isHorizontal] - where) --- 134,138 ---- which is an x coordinate if isHorizontal is False, a y coordinate if isHorizontal is True. Return a list of curve segments.""" ! a, b, c, d = calcCubicParameters(pt1, pt2, pt3, pt4) solutions = solveCubic(a[isHorizontal], b[isHorizontal], c[isHorizontal], d[isHorizontal] - where) *************** *** 225,226 **** --- 239,275 ---- x = x - a1/3.0 return [x] + + + def calcQuadraticParameters(pt1, pt2, pt3): + pt1, pt2, pt3 = Numeric.array((pt1, pt2, pt3)) + c = pt1 + b = (pt2 - c) * 2.0 + a = pt3 - c - b + return a, b, c + + + def calcCubicParameters(pt1, pt2, pt3, pt4): + pt1, pt2, pt3, pt4 = Numeric.array((pt1, pt2, pt3, pt4)) + d = pt1 + c = (pt2 - d) * 3.0 + b = (pt3 - pt2) * 3.0 - c + a = pt4 - d - c - b + return a, b, c, d + + + def _tuplify(obj): + """ + >>> _tuplify([1, [2, 3], [], [[2, [3, 4]]]]) + (1, (2, 3), (), ((2, (3, 4)),)) + """ + try: + it = iter(obj) + except TypeError: + return obj + else: + return tuple([_tuplify(x) for x in it]) + + + if __name__ == "__main__": + import doctest + doctest.testmod() ```