From be2c3981b86accc9b5dd803663a0819650730722 Mon Sep 17 00:00:00 2001 From: capellancitizen Date: Fri, 14 Jun 2024 14:45:51 -0400 Subject: Fix realistic stitch filter rendering (#2988) --- lib/svg/rendering.py | 96 +++++++++++++++++++++++++++------------------------- 1 file changed, 49 insertions(+), 47 deletions(-) (limited to 'lib') diff --git a/lib/svg/rendering.py b/lib/svg/rendering.py index a153ff95..a37073fb 100644 --- a/lib/svg/rendering.py +++ b/lib/svg/rendering.py @@ -56,50 +56,53 @@ stitch_path = ( "l-0.55,-0.1,0.55,0.1" # Bottom-left whisker "z") # return to start -# The filter needs the xmlns:inkscape declaration, or Inkscape will display a parse error -# "Namespace prefix inkscape for auto-region on filter is not defined" -# Even when the document itself has the namespace, go figure. -realistic_filter = """ - - - - - - - - -""" +def generate_realistic_filter() -> inkex.BaseElement: + """ + Return a copy of the realistic stitch filter, ready to add to svg defs. + """ + filter = inkex.Filter(attrib = { + "style": "color-interpolation-filters:sRGB", + "id": "realistic-stitch-filter", + "x": "0", + "width": "1", + "y": "0", + "height": "1", + inkex.addNS('auto-region', 'inkscape'): "false", + }) + + filter.add( + inkex.Filter.GaussianBlur(attrib = { + "edgeMode": "none", + "stdDeviation": "0.9", + "in": "SourceAlpha", + }), + inkex.Filter.SpecularLighting( + inkex.Filter.DistantLight(attrib = { + "azimuth": "-125", + "elevation": "20", + }), attrib = { + "result": "result2", + "surfaceScale": "1.5", + "specularConstant": "0.78", + "specularExponent": "2.5", + } + ), + inkex.Filter.Composite(attrib = { + "in2": "SourceAlpha", + "operator": "atop", + }), + inkex.Filter.Composite(attrib = { + "in2": "SourceGraphic", + "operator": "arithmetic", + "result": "result3", + "k1": "0", + "k2": "0.8", + "k3": "1.2", + "k4": "0", + }) + ) + + return filter def realistic_stitch(start, end): @@ -243,9 +246,8 @@ def render_stitch_plan(svg, stitch_plan, realistic=False, visual_commands=True, # Remove filter from defs, if any filter: inkex.BaseElement = svg.defs.findone("//*[@id='realistic-stitch-filter']") if filter is not None: - svg.defs.remove(filter) + filter.delete() - filter_document = inkex.load_svg(realistic_filter) - svg.defs.append(filter_document.getroot()) + svg.defs.append(generate_realistic_filter()) return layer -- cgit v1.2.3