Skip to content

Commit

Permalink
These can be moved to base as well.
Browse files Browse the repository at this point in the history
  • Loading branch information
michielkauwatjoe committed Jan 12, 2021
1 parent d1264bb commit 25b2dc4
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 84 deletions.
82 changes: 65 additions & 17 deletions Lib/pagebot/contexts/basecontext/basebezierpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,37 +221,86 @@ def addComponent(self, glyphName, transformation):

# Shapes.

def arc(self, center, radius, startAngle, endAngle, clockwise):
"""Arc with `center` and a given `radius`, from `startAngle` to
`endAngle`, going clockwise if `clockwise` is True and counter
clockwise if `clockwise` is False."""
raise NotImplementedError

def arcTo(self, point1, point2, radius):
"""Arc defined by a circle inscribed inside the angle specified by
three points: the current point, `point1`, and `point2`. The arc is
drawn between the two points of the circle that are tangent to the two
legs of the angle."""
raise NotImplementedError

def rect(self, x, y, w, h):
"""Adds a rectangle at position `x`, `y` with a size of `w`, `h`."""
raise NotImplementedError
x1 = x + w
y1 = y + h
p0 = (x, y)
p1 = (x1, y)
p2 = (x1, y1)
p3 = (x, y1)
self.moveTo(p0)
self.lineTo(p1)
self.lineTo(p2)
self.lineTo(p3)
self.closePath()

def oval(self, x, y, w, h):
"""Adds an oval at position `x`, `y` with a size of `w`, `h`"""
raise NotImplementedError
# Control point offsets.
kappa = .5522848
offsetX = (w / 2) * kappa
offsetY = (h / 2) * kappa

# Middle and other extreme points.
x0 = x + (w / 2)
y0 = y + (h / 2)
x1 = x + w
y1 = y + h

self.moveTo((x0, y0))

cp1 = (x, y0 - offsetY)
cp2 = (x0 - offsetX, y)
p = (x1, y0)
self.curveTo(cp1, cp2, p)

cp1 = (x0 + offsetX, y)
cp2 = (x1, y0 - offsetY)
p = (x1, y0)
self.curveTo(cp1, cp2, p)

cp1 = (x1, y0 + offsetY)
cp2 = (x0 + offsetX, y1)
p = (x0, y1)
self.curveTo(cp1, cp2, p)

cp1 = (x0 - offsetX, y1)
cp2 = (x, y0 + offsetY)
p = (x, y0)
self.curveTo(cp1, cp2, p)

def line(self, point1, point2):
"""Adds a line between two given points."""
raise NotImplementedError
self.moveTo(point1)
self.lineTo(point2)

def polygon(self, *points, **kwargs):
"""Draws a polygon with `n` points. Optionally a `close` argument can
be provided to open or close the path. By default a `polygon` is a
closed path."""
self.moveTo(points[0])

for point in points[1:]:
self.lineTo(point)

# TODO: optionally close.

def arc(self, center, radius, startAngle, endAngle, clockwise):
"""Arc with `center` and a given `radius`, from `startAngle` to
`endAngle`, going clockwise if `clockwise` is True and counter
clockwise if `clockwise` is False."""
raise NotImplementedError

def arcTo(self, point1, point2, radius):
"""Arc defined by a circle inscribed inside the angle specified by
three points: the current point, `point1`, and `point2`. The arc is
drawn between the two points of the circle that are tangent to the two
legs of the angle."""
raise NotImplementedError

# Text.

def text(self, txt, offset=None, font=_FALLBACKFONT, fontSize=10,
align=None):
"""Draws a `txt` with a `font` and `fontSize` at an `offset` in the
Expand Down Expand Up @@ -284,7 +333,6 @@ def textBox(self, txt, box, font=None, fontSize=10, align=None,
raise NotImplementedError

# Path operations.

# These are specific for a DrawBot path, dropping from interface.

#def getNSBezierPath(self):
Expand Down
67 changes: 0 additions & 67 deletions Lib/pagebot/contexts/flatcontext/flatbezierpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,73 +97,6 @@ def appendPath(self, path):
# ...
self.commands += path.commands

# Shapes.

def rect(self, x, y, w, h):
"""Adds a rectangle at position `x`, `y` with a size of `w`, `h`."""
x1 = x + w
y1 = y + h
p0 = (x, y)
p1 = (x1, y)
p2 = (x1, y1)
p3 = (x, y1)
self.moveTo(p0)
self.lineTo(p1)
self.lineTo(p2)
self.lineTo(p3)
self.closePath()

def oval(self, x, y, w, h):
"""Adds an oval at position `x`, `y` with a size of `w`, `h`"""
# Control point offsets.
kappa = .5522848
offsetX = (w / 2) * kappa
offsetY = (h / 2) * kappa

# Middle and other extreme points.
x0 = x + (w / 2)
y0 = y + (h / 2)
x1 = x + w
y1 = y + h

self.moveTo((x0, y0))

cp1 = (x, y0 - offsetY)
cp2 = (x0 - offsetX, y)
p = (x1, y0)
self.curveTo(cp1, cp2, p)

cp1 = (x0 + offsetX, y)
cp2 = (x1, y0 - offsetY)
p = (x1, y0)
self.curveTo(cp1, cp2, p)

cp1 = (x1, y0 + offsetY)
cp2 = (x0 + offsetX, y1)
p = (x0, y1)
self.curveTo(cp1, cp2, p)

cp1 = (x0 - offsetX, y1)
cp2 = (x, y0 + offsetY)
p = (x, y0)
self.curveTo(cp1, cp2, p)

def line(self, point1, point2):
"""Adds a line between two given points."""
self.moveTo(point1)
self.lineTo(point2)

def polygon(self, *points, **kwargs):
"""Draws a polygon with `n` points. Optionally a `close` argument can
be provided to open or close the path. By default a `polygon` is a
closed path."""
self.moveTo(points[0])

for point in points[1:]:
self.lineTo(point)

# TODO: optionally close.

def text(self, txt, offset=None, font=None, fontSize=10, align=None):
"""Draws a `txt` with a `font` and `fontSize` at an `offset` in the
Bézier path. If a font path is given the font will be installed and
Expand Down

0 comments on commit 25b2dc4

Please sign in to comment.