diff options
| author | Lex Neva <github.com@lexneva.name> | 2018-05-26 21:26:40 -0400 |
|---|---|---|
| committer | Lex Neva <github.com@lexneva.name> | 2018-05-29 20:04:30 -0400 |
| commit | 4c986117bfe1f2caa8280d4b0ddbeec69f41b18d (patch) | |
| tree | 76e076b6bc1e331d63252ff30917f0475f24c962 /lib/svg/svg.py | |
| parent | d50c7e1b99b570ec5300de39c2966ac864700138 (diff) | |
first attempt at realistic rendering
Diffstat (limited to 'lib/svg/svg.py')
| -rw-r--r-- | lib/svg/svg.py | 157 |
1 files changed, 137 insertions, 20 deletions
diff --git a/lib/svg/svg.py b/lib/svg/svg.py index 852215f2..147fdc84 100644 --- a/lib/svg/svg.py +++ b/lib/svg/svg.py @@ -1,9 +1,124 @@ -import simpletransform, simplestyle, inkex +import math +import simpletransform, simplestyle, simplepath, inkex -from .units import get_viewbox_transform -from .tags import SVG_GROUP_TAG, INKSCAPE_LABEL, INKSCAPE_GROUPMODE, SVG_PATH_TAG +from .units import get_viewbox_transform, PIXELS_PER_MM +from .tags import SVG_GROUP_TAG, INKSCAPE_LABEL, INKSCAPE_GROUPMODE, SVG_PATH_TAG, SVG_DEFS_TAG from ..i18n import _ -from ..utils import cache +from ..utils import cache, Point + + +# The stitch vector path looks like this: +# _______ +# (_______) +# +# It's 0.4mm high, which is the approximate thickness of common machine embroidery threads. + +# 1.52 pixels = 0.4mm +stitch_height = 1.52 + +# This vector path starts at the origin and contains a placeholder (%s) for the stitch length. +stitch_path = "M0,0c0.386,0,0.417,0.378,0.428,0.759c0.012,0.382,-0.048,0.754,-0.428,0.759h-%sc-0.357,-0.003,-0.399,-0.376,-0.413,-0.759c-0.014,-0.382,0.067,-0.759,0.413,-0.759z" + +# This filter makes the above stitch path look like a real stitch with lighting. +realistic_filter = """ + <filter + style="color-interpolation-filters:sRGB" + id="realistic-stitch-filter" + x="-0.40000001" + width="1.8" + y="-0.40000001" + height="1.8"> + <feGaussianBlur + stdDeviation="1.5" + id="feGaussianBlur1542-6" + in="SourceAlpha" /> + <feComponentTransfer + id="feComponentTransfer1544-7" + result="result1"> + <feFuncR + id="feFuncR1546-5" + type="identity" /> + <feFuncG + id="feFuncG1548-3" + type="identity" /> + <feFuncB + id="feFuncB1550-5" + type="identity" + slope="4.5300000000000002" /> + <feFuncA + id="feFuncA1552-6" + type="gamma" + slope="0.14999999999999999" + intercept="0" + amplitude="3.1299999999999999" + offset="-0.33000000000000002" /> + </feComponentTransfer> + <feComposite + in2="SourceAlpha" + id="feComposite1558-2" + operator="in" /> + <feGaussianBlur + stdDeviation="0.089999999999999997" + id="feGaussianBlur1969" /> + <feMorphology + id="feMorphology1971" + operator="dilate" + radius="0.10000000000000001" /> + <feSpecularLighting + id="feSpecularLighting1973" + result="result2" + specularConstant="0.70899999" + surfaceScale="33.81000137"> + <fePointLight + id="fePointLight1975" + z="10" /> + </feSpecularLighting> + <feGaussianBlur + stdDeviation="0.040000000000000001" + id="feGaussianBlur1979" /> + <feComposite + in2="SourceGraphic" + id="feComposite1977" + operator="arithmetic" + k2="1" + k3="1" + result="result3" + k1="0" + k4="0" /> + <feComposite + in2="SourceAlpha" + id="feComposite1981" + operator="in" /> + </filter> +""" + +def realistic_stitch(start, end): + """Generate a stitch vector path given a start and end point.""" + + end = Point(*end) + start = Point(*start) + + stitch_length = (end - start).length() + stitch_center = (end + start) / 2.0 + stitch_direction = (end - start) + stitch_angle = math.atan2(stitch_direction.y, stitch_direction.x) + + stitch_length = max(0, stitch_length - 0.2 * PIXELS_PER_MM) + + # create the path by filling in the length in the template + path = simplepath.parsePath(stitch_path % stitch_length) + + simplepath.scalePath(path, 1, 0.8) + + # rotate the path to match the stitch + rotation_center_x = -stitch_length / 2.0 + rotation_center_y = stitch_height / 2.0 + simplepath.rotatePath(path, stitch_angle, cx=rotation_center_x, cy=rotation_center_y) + + # move the path to the location of the stitch + simplepath.translatePath(path, stitch_center.x, stitch_center.y) + + return simplepath.formatPath(path) def color_block_to_point_lists(color_block): @@ -37,22 +152,21 @@ def color_block_to_paths(color_block, svg): # We could emit just a single path with one subpath per point list, but # emitting multiple paths makes it easier for the user to manipulate them. for point_list in color_block_to_point_lists(color_block): - color = color_block.color.visible_on_white.to_hex_str() - paths.append(inkex.etree.Element( - SVG_PATH_TAG, - {'style': simplestyle.formatStyle( - {'stroke': color, - 'stroke-width': "0.4", - 'fill': 'none'}), - 'd': "M" + " ".join(" ".join(str(coord) for coord in point) for point in point_list), - 'transform': get_correction_transform(svg), - 'embroider_manual_stitch': 'true', - 'embroider_trim_after': 'true', - })) - - # no need to trim at the end of a thread color - if paths: - paths[-1].attrib.pop('embroider_trim_after') + color = color_block.color.visible_on_white.darker.to_hex_str() + start = point_list[0] + for point in point_list[1:]: + paths.append(inkex.etree.Element( + SVG_PATH_TAG, + {'style': simplestyle.formatStyle( + { + 'fill': color, + 'stroke': 'none', + 'filter': 'url(#realistic-stitch-filter)' + }), + 'd': realistic_stitch(start, point), + 'transform': get_correction_transform(svg) + })) + start = point return paths @@ -78,4 +192,7 @@ def render_stitch_plan(svg, stitch_plan): INKSCAPE_LABEL: "color block %d" % (i + 1)}) group.extend(color_block_to_paths(color_block, svg)) + defs = svg.find(SVG_DEFS_TAG) + defs.append(inkex.etree.fromstring(realistic_filter)) + svg.append(layer) |
