Skip to content
Permalink
Browse files

begin support for gradient pens

  • Loading branch information...
RobinD42 committed Jul 31, 2019
1 parent 43370db commit 6a144a3654d2684fcb99923eb289a4d1e244a00b
Showing with 41 additions and 24 deletions.
  1. +4 −0 docs/sphinx/itemToModuleMap.json
  2. +1 −1 ext/wxWidgets
  3. +36 −23 wx/svg/__init__.py
@@ -1308,6 +1308,9 @@
"GLCanvas":"wx.glcanvas.",
"GLContext":"wx.glcanvas.",
"GLContextAttrs":"wx.glcanvas.",
"GRADIENT_LINEAR":"wx.",
"GRADIENT_NONE":"wx.",
"GRADIENT_RADIAL":"wx.",
"GREEN":"wx.",
"GREEN_BRUSH":"wx.",
"GREEN_PEN":"wx.",
@@ -1395,6 +1398,7 @@
"GetUserId":"wx.",
"GetUserName":"wx.",
"Getenv":"wx.",
"GradientType":"wx.",
"GraphicsBitmap":"wx.",
"GraphicsBrush":"wx.",
"GraphicsContext":"wx.",
@@ -89,15 +89,13 @@ def _makeSubPath(self, ctx, svg_path):
return path


def _makeGradientStops(self, gradient, prnt=False):
def _makeGradientStops(self, gradient):
stops = [stop for stop in gradient.stops]
first = stops[0]
last = stops[-1]
gcstops = wx.GraphicsGradientStops(wx.Colour(*first.color_rgba),
wx.Colour(*last.color_rgba))
for stop in stops:
if prnt:
print('stop: ', stop.offset, stop.color_rgba)
color = wx.Colour(*stop.color_rgba)
gcstop = wx.GraphicsGradientStop(color, stop.offset)
gcstops.Add(gcstop)
@@ -125,9 +123,7 @@ def _makeBrush(self, ctx, shape):
elif shape.fill.type == SVG_PAINT_LINEAR_GRADIENT:
gradient = shape.fill.gradient
(x1, y1), (x2, y2) = gradient.linearPoints
print('shape: ', shape.bounds)
print('lingrad:', (x1, y1), (x2, y2))
stops = self._makeGradientStops(gradient, True)
stops = self._makeGradientStops(gradient)
brush = ctx.CreateLinearGradientBrush(x1, y1, x2, y2, stops)

# brush with a radial gradient
@@ -159,31 +155,48 @@ def _makePen(self, ctx, shape):
SVG_CAP_SQUARE : wx.CAP_PROJECTING}.get(shape.strokeLineCap, 0)
# TODO: handle dashes

info = wx.GraphicsPenInfo(wx.BLACK).Width(width).Join(join).Cap(cap)

if shape.stroke.type == SVG_PAINT_NONE:
pen = wx.NullGraphicsPen

elif shape.stroke.type == SVG_PAINT_COLOR:
r,g,b,a = shape.stroke.color_rgba
pen = ctx.CreatePen(
wx.GraphicsPenInfo(wx.Colour(r,g,b,a)).Width(width).Join(join).Cap(cap))
info.Colour(shape.stroke.color_rgba)
pen = ctx.CreatePen(info)

elif shape.stroke.type == SVG_PAINT_LINEAR_GRADIENT:
# print("TODO: linear GradientPen")
# TODO: wxWidgets can't do gradient pens (yet?)
# Just average the stops to use as an approximation
colors = self._getGradientColors(shape.stroke.gradient)
ave = [round(sum(x)/len(x)) for x in zip(*colors)]
pen = ctx.CreatePen(
wx.GraphicsPenInfo(wx.Colour(*ave)).Width(width).Join(join).Cap(cap))
# # print("TODO: linear GradientPen")
# # TODO: wxWidgets can't do gradient pens (yet?)
# # Just average the stops to use as an approximation
# colors = self._getGradientColors(shape.stroke.gradient)
# ave = [round(sum(x)/len(x)) for x in zip(*colors)]
# pen = ctx.CreatePen(
# wx.GraphicsPenInfo(wx.Colour(*ave)).Width(width).Join(join).Cap(cap))
gradient = shape.stroke.gradient
(x1, y1), (x2, y2) = gradient.linearPoints
stops = self._makeGradientStops(gradient)
info.LinearGradient(x1, y1, x2, y2, stops)
pen = ctx.CreatePen(info)

elif shape.stroke.type == SVG_PAINT_RADIAL_GRADIENT:
# print("TODO: radial GradientPen")
# TODO: wxWidgets can't do gradient pens (yet?)
# Just average the stops to use as an approximation
colors = self._getGradientColors(shape.stroke.gradient)
ave = [round(sum(x)/len(x)) for x in zip(*colors)]
pen = ctx.CreatePen(
wx.GraphicsPenInfo(wx.Colour(*ave)).Width(width).Join(join).Cap(cap))
# # print("TODO: radial GradientPen")
# # TODO: wxWidgets can't do gradient pens (yet?)
# # Just average the stops to use as an approximation
# colors = self._getGradientColors(shape.stroke.gradient)
# ave = [round(sum(x)/len(x)) for x in zip(*colors)]
# pen = ctx.CreatePen(
# wx.GraphicsPenInfo(wx.Colour(*ave)).Width(width).Join(join).Cap(cap))
# pass
gradient = shape.stroke.gradient
(cx, cy), radius = gradient.radialPointRadius
#print('1: (cx, cy, radius) (fx, fy):', (cx, cy, radius, gradient.fx, gradient.fy))
stops = self._makeGradientStops(gradient)

# FIXME: *2 seems to be close for this test case, but it is surely
# wrong generally... figure out what needs to be done in
# gradient.radialPointRadius to get the correct value.
info.RadialGradient(cx, cy, cx, cy, radius*3, stops)
pen = ctx.CreatePen(info)

else:
raise ValueError("Unknown stroke type")

0 comments on commit 6a144a3

Please sign in to comment.
You can’t perform that action at this time.