mirror of https://github.com/tuhiproject/tuhi.git
Change SVG export to use paths instead of line segments
parent
1362b89394
commit
0b02f5f9c2
|
@ -82,27 +82,44 @@ class JsonSvg(ImageExportBase):
|
||||||
_base_pen_width = 0.4
|
_base_pen_width = 0.4
|
||||||
_pen_pressure_width_factor = 0.2
|
_pen_pressure_width_factor = 0.2
|
||||||
|
|
||||||
|
# Change this value down to reduce size, change it up to improve accuracy. measured in px
|
||||||
|
_width_precision = 10
|
||||||
|
|
||||||
def _convert(self):
|
def _convert(self):
|
||||||
|
|
||||||
width, height = self.output_dimensions
|
width, height = self.output_dimensions
|
||||||
size = width * mm, height * mm
|
size = width * mm, height * mm
|
||||||
svg = svgwrite.Drawing(filename=self.filename, size=size)
|
|
||||||
|
# Make sure to set viewBox here so mm doesn't have to be specified in all later parts
|
||||||
|
svg = svgwrite.Drawing(filename=self.filename, size=size, viewBox=(f'0 0 {width} {height}'))
|
||||||
|
|
||||||
g = svgwrite.container.Group(id='layer0')
|
g = svgwrite.container.Group(id='layer0')
|
||||||
for sk_num, stroke_points in enumerate(self.output_strokes):
|
for sk_num, stroke_points in enumerate(self.output_strokes):
|
||||||
lines = svgwrite.container.Group(id=f'sk_{sk_num}', stroke='black')
|
path = None
|
||||||
|
stroke_width_p = None
|
||||||
for i, (x, y, stroke_width) in enumerate(stroke_points):
|
for i, (x, y, stroke_width) in enumerate(stroke_points):
|
||||||
if i != 0:
|
if not x or not y:
|
||||||
xp, yp, stroke_width_p = stroke_points[i - 1]
|
continue
|
||||||
lines.add(
|
|
||||||
svg.line(
|
# Reduce precision of the width
|
||||||
start=(xp * mm, yp * mm),
|
stroke_width = int(stroke_width * self._width_precision) / self._width_precision
|
||||||
end=(x * mm, y * mm),
|
|
||||||
stroke_width=stroke_width,
|
# Create a new path per object and per unique width
|
||||||
style='fill:none'
|
if stroke_width_p != stroke_width:
|
||||||
)
|
if path:
|
||||||
)
|
g.add(path)
|
||||||
g.add(lines)
|
# Reduce width by mm to px at 96dpi (see SVG/CSS specification)
|
||||||
|
width_px = stroke_width * 0.26458
|
||||||
|
path = svg.path(id=f'sk_{sk_num}_{i}', style=f'fill:none;stroke:black;stroke-width:{width_px}')
|
||||||
|
stroke_width_p = stroke_width
|
||||||
|
path.push("M", f'{x:.2f}', f'{y:.2f}')
|
||||||
|
|
||||||
|
else:
|
||||||
|
# Continue writing segment line with next coords
|
||||||
|
path.push("L", f'{x:.2f}', f'{y:.2f}')
|
||||||
|
|
||||||
|
if path:
|
||||||
|
g.add(path)
|
||||||
|
|
||||||
svg.add(g)
|
svg.add(g)
|
||||||
svg.save()
|
svg.save()
|
||||||
|
|
Loading…
Reference in New Issue