Merge pull request #2822 from udifuchs/fix-wx-svg
Some checks failed
ci-build / build-source-dist (push) Has been cancelled
ci-build / Build wxPython documentation (push) Has been cancelled
ci-build / build-wheels (arm64, macos-14, 3.10) (push) Has been cancelled
ci-build / build-wheels (arm64, macos-14, 3.11) (push) Has been cancelled
ci-build / build-wheels (arm64, macos-14, 3.12) (push) Has been cancelled
ci-build / build-wheels (arm64, macos-14, 3.13) (push) Has been cancelled
ci-build / build-wheels (arm64, macos-14, 3.14) (push) Has been cancelled
ci-build / build-wheels (arm64, macos-14, 3.9) (push) Has been cancelled
ci-build / build-wheels (arm64, windows-11-arm, 3.11) (push) Has been cancelled
ci-build / build-wheels (arm64, windows-11-arm, 3.12) (push) Has been cancelled
ci-build / build-wheels (arm64, windows-11-arm, 3.13) (push) Has been cancelled
ci-build / build-wheels (arm64, windows-11-arm, 3.14) (push) Has been cancelled
ci-build / build-wheels (x64, macos-13, 3.10) (push) Has been cancelled
ci-build / build-wheels (x64, macos-13, 3.11) (push) Has been cancelled
ci-build / build-wheels (x64, macos-13, 3.12) (push) Has been cancelled
ci-build / build-wheels (x64, macos-13, 3.13) (push) Has been cancelled
ci-build / build-wheels (x64, macos-13, 3.14) (push) Has been cancelled
ci-build / build-wheels (x64, macos-13, 3.9) (push) Has been cancelled
ci-build / build-wheels (x64, ubuntu-22.04, 3.10) (push) Has been cancelled
ci-build / build-wheels (x64, ubuntu-22.04, 3.11) (push) Has been cancelled
ci-build / build-wheels (x64, ubuntu-22.04, 3.12) (push) Has been cancelled
ci-build / build-wheels (x64, ubuntu-22.04, 3.13) (push) Has been cancelled
ci-build / build-wheels (x64, ubuntu-22.04, 3.14) (push) Has been cancelled
ci-build / build-wheels (x64, ubuntu-22.04, 3.9) (push) Has been cancelled
ci-build / build-wheels (x64, windows-2022, 3.10) (push) Has been cancelled
ci-build / build-wheels (x64, windows-2022, 3.11) (push) Has been cancelled
ci-build / build-wheels (x64, windows-2022, 3.12) (push) Has been cancelled
ci-build / build-wheels (x64, windows-2022, 3.13) (push) Has been cancelled
ci-build / build-wheels (x64, windows-2022, 3.14) (push) Has been cancelled
ci-build / build-wheels (x64, windows-2022, 3.9) (push) Has been cancelled
ci-build / build-wheels (x86, windows-2022, 3.10) (push) Has been cancelled
ci-build / build-wheels (x86, windows-2022, 3.11) (push) Has been cancelled
ci-build / build-wheels (x86, windows-2022, 3.12) (push) Has been cancelled
ci-build / build-wheels (x86, windows-2022, 3.13) (push) Has been cancelled
ci-build / build-wheels (x86, windows-2022, 3.14) (push) Has been cancelled
ci-build / build-wheels (x86, windows-2022, 3.9) (push) Has been cancelled
ci-build / Publish Python distribution to PyPI (push) Has been cancelled
ci-build / Create GitHub Release and upload source (push) Has been cancelled
ci-build / Upload demo/docs to extras.wxpython.org (push) Has been cancelled
ci-build / Upload wheels to snapshot-builds on wxpython.org (push) Has been cancelled

Fix wx.svg to work with cython 3.1 generated code.
This commit is contained in:
Scott Talbert
2025-11-14 11:16:12 -05:00
committed by GitHub
3 changed files with 63 additions and 18 deletions

View File

@@ -1,7 +1,7 @@
import unittest
from unittests import wtc
import wx
import sys
import wx.svg
import os
fileName = 'svgtest.svg'
@@ -15,8 +15,44 @@ class SvgDCTests(wtc.WidgetTestCase):
dc.DrawLine(0,0, 50,50)
del dc
self.img = wx.svg.SVGimage.CreateFromFile(fileName)
dc = wx.ClientDC(self.frame)
dc.SetBackground(wx.Brush('white'))
dc.Clear()
dcdim = min(self.frame.Size.width, self.frame.Size.height)
imgdim = min(self.img.width, self.img.height)
scale = dcdim / imgdim
ctx = wx.GraphicsContext.Create(dc)
self.img.RenderToGC(ctx, scale)
os.remove(fileName)
def test_SvgDC2(self):
assert wx.svg.SVGpaintType.SVG_PAINT_NONE == wx.svg.SVG_PAINT_NONE
assert wx.svg.SVGpaintType.SVG_PAINT_COLOR == wx.svg.SVG_PAINT_COLOR
assert wx.svg.SVGpaintType.SVG_PAINT_LINEAR_GRADIENT == wx.svg.SVG_PAINT_LINEAR_GRADIENT
assert wx.svg.SVGpaintType.SVG_PAINT_RADIAL_GRADIENT == wx.svg.SVG_PAINT_RADIAL_GRADIENT
assert wx.svg.SVGspreadType.SVG_SPREAD_PAD == wx.svg.SVG_SPREAD_PAD
assert wx.svg.SVGspreadType.SVG_SPREAD_REFLECT == wx.svg.SVG_SPREAD_REFLECT
assert wx.svg.SVGspreadType.SVG_SPREAD_REPEAT == wx.svg.SVG_SPREAD_REPEAT
assert wx.svg.SVGlineJoin.SVG_JOIN_MITER == wx.svg.SVG_JOIN_MITER
assert wx.svg.SVGlineJoin.SVG_JOIN_ROUND == wx.svg.SVG_JOIN_ROUND
assert wx.svg.SVGlineJoin.SVG_JOIN_BEVEL == wx.svg.SVG_JOIN_BEVEL
assert wx.svg.SVGlineCap.SVG_CAP_BUTT == wx.svg.SVG_CAP_BUTT
assert wx.svg.SVGlineCap.SVG_CAP_ROUND == wx.svg.SVG_CAP_ROUND
assert wx.svg.SVGlineCap.SVG_CAP_SQUARE == wx.svg.SVG_CAP_SQUARE
assert wx.svg.SVGfillRule.SVG_FILLRULE_NONZERO == wx.svg.SVG_FILLRULE_NONZERO
assert wx.svg.SVGfillRule.SVG_FILLRULE_EVENODD == wx.svg.SVG_FILLRULE_EVENODD
assert wx.svg.SVGflags.SVG_FLAGS_VISIBLE == wx.svg.SVG_FLAGS_VISIBLE
#---------------------------------------------------------------------------

View File

@@ -174,15 +174,15 @@ class SVGimage(SVGimageBase):
ctx.Scale(sx, sy)
for shape in self.shapes:
if not shape.flags & SVG_FLAGS_VISIBLE:
if not shape.flags & SVGflags.SVG_FLAGS_VISIBLE:
continue
if shape.opacity != 1.0:
ctx.BeginLayer(shape.opacity)
brush = self._makeBrush(ctx, shape)
pen = self._makePen(ctx, shape)
rule = { SVG_FILLRULE_NONZERO : wx.WINDING_RULE,
SVG_FILLRULE_EVENODD : wx.ODDEVEN_RULE }.get(shape.fillRule, 0)
rule = { SVGfillRule.SVG_FILLRULE_NONZERO : wx.WINDING_RULE,
SVGfillRule.SVG_FILLRULE_EVENODD : wx.ODDEVEN_RULE }.get(shape.fillRule, 0)
# The shape's path is comprised of one or more subpaths, collect
# and accumulate them in a new GraphicsPath
@@ -237,16 +237,16 @@ class SVGimage(SVGimageBase):
# set up a brush from the shape.fill (SVGpaint) object
# no brush
if shape.fill.type == SVG_PAINT_NONE:
if shape.fill.type == SVGpaintType.SVG_PAINT_NONE:
brush = wx.NullGraphicsBrush
# brush with a solid color
elif shape.fill.type == SVG_PAINT_COLOR:
elif shape.fill.type == SVGpaintType.SVG_PAINT_COLOR:
r,g,b,a = shape.fill.color_rgba
brush = ctx.CreateBrush(wx.Brush(wx.Colour(r,g,b,a)))
# brush with a linear gradient
elif shape.fill.type == SVG_PAINT_LINEAR_GRADIENT:
elif shape.fill.type == SVGpaintType.SVG_PAINT_LINEAR_GRADIENT:
# NanoSVG gives gradients their own transform which normalizes the
# linear gradients to go from (0, 0) to (0,1) in the transformed
# space. So once we have the transform set we can use those points
@@ -271,7 +271,7 @@ class SVGimage(SVGimageBase):
brush = ctx.CreateLinearGradientBrush(x1,y1, x2,y2, stops, matrix)
# brush with a radial gradient
elif shape.fill.type == SVG_PAINT_RADIAL_GRADIENT:
elif shape.fill.type == SVGpaintType.SVG_PAINT_RADIAL_GRADIENT:
# Likewise, NanoSVG normalizes radial gradients with a transform
# that puts the center (cx, cy) at (0,0) and the radius has a length
# of 1.
@@ -299,24 +299,24 @@ class SVGimage(SVGimageBase):
def _makePen(self, ctx, shape):
# set up a pen from the shape.stroke (SVGpaint) object
width = shape.strokeWidth
join = { SVG_JOIN_MITER : wx.JOIN_MITER,
SVG_JOIN_ROUND : wx.JOIN_ROUND,
SVG_JOIN_BEVEL : wx.JOIN_BEVEL}.get(shape.strokeLineJoin, 0)
cap = { SVG_CAP_BUTT : wx.CAP_BUTT,
SVG_CAP_ROUND : wx.CAP_ROUND,
SVG_CAP_SQUARE : wx.CAP_PROJECTING}.get(shape.strokeLineCap, 0)
join = { SVGlineJoin.SVG_JOIN_MITER : wx.JOIN_MITER,
SVGlineJoin.SVG_JOIN_ROUND : wx.JOIN_ROUND,
SVGlineJoin.SVG_JOIN_BEVEL : wx.JOIN_BEVEL}.get(shape.strokeLineJoin, 0)
cap = { SVGlineCap.SVG_CAP_BUTT : wx.CAP_BUTT,
SVGlineCap.SVG_CAP_ROUND : wx.CAP_ROUND,
SVGlineCap.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:
if shape.stroke.type == SVGpaintType.SVG_PAINT_NONE:
pen = wx.NullGraphicsPen
elif shape.stroke.type == SVG_PAINT_COLOR:
elif shape.stroke.type == SVGpaintType.SVG_PAINT_COLOR:
info.Colour(shape.stroke.color_rgba)
pen = ctx.CreatePen(info)
elif shape.stroke.type == SVG_PAINT_LINEAR_GRADIENT:
elif shape.stroke.type == SVGpaintType.SVG_PAINT_LINEAR_GRADIENT:
x1, y1, = (0.0, 0.0)
x2, y2, = (0.0, 1.0)
gradient = shape.stroke.gradient
@@ -333,7 +333,7 @@ class SVGimage(SVGimageBase):
info.LinearGradient(x1,y1, x2,y2, stops, matrix)
pen = ctx.CreatePen(info)
elif shape.stroke.type == SVG_PAINT_RADIAL_GRADIENT:
elif shape.stroke.type == SVGpaintType.SVG_PAINT_RADIAL_GRADIENT:
cx, cy = (0.0, 0.0)
radius = 1
gradient = shape.stroke.gradient

View File

@@ -77,6 +77,15 @@ cpdef enum SVGfillRule:
cpdef enum SVGflags:
SVG_FLAGS_VISIBLE = NSVG_FLAGS_VISIBLE
# Populate the module namespace with enum values to keep compatibility with packages
# compiled with cython > 3.1 (see https://github.com/cython/cython/issues/4571)
globals().update(getattr(SVGpaintType, "__members__"))
globals().update(getattr(SVGspreadType, "__members__"))
globals().update(getattr(SVGlineJoin, "__members__"))
globals().update(getattr(SVGlineCap, "__members__"))
globals().update(getattr(SVGfillRule, "__members__"))
globals().update(getattr(SVGflags, "__members__"))
#----------------------------------------------------------------------------
# Cython classes for wrapping the nanosvg structs