2024-06-26 20:51:15 +00:00
|
|
|
# Authors: see git history
|
|
|
|
#
|
|
|
|
# Copyright (c) 2024 Authors
|
|
|
|
# Licensed under the GNU GPL version 3.0 or later. See the file LICENSE for details.
|
|
|
|
|
|
|
|
import sys
|
|
|
|
from tempfile import TemporaryDirectory
|
|
|
|
|
|
|
|
from inkex.units import convert_unit
|
|
|
|
|
|
|
|
from ..stitch_plan import stitch_groups_to_stitch_plan
|
|
|
|
from ..svg import render_stitch_plan
|
|
|
|
from ..threads import ThreadCatalog
|
|
|
|
from ..utils.svg_data import get_pagecolor
|
|
|
|
from .base import InkstitchExtension
|
|
|
|
from .utils.inkex_command import inkscape
|
|
|
|
|
|
|
|
|
|
|
|
class PngSimple(InkstitchExtension):
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
InkstitchExtension.__init__(self)
|
|
|
|
|
2024-07-03 05:09:06 +00:00
|
|
|
self.arg_parser.add_argument('--notebook')
|
|
|
|
self.arg_parser.add_argument('--line_width', type=float, default=0.3, dest='line_width')
|
|
|
|
self.arg_parser.add_argument('--dpi', type=int, default=300, dest='dpi')
|
2024-06-26 20:51:15 +00:00
|
|
|
|
|
|
|
def effect(self):
|
|
|
|
if not self.get_elements():
|
|
|
|
return
|
|
|
|
|
|
|
|
self.metadata = self.get_inkstitch_metadata()
|
|
|
|
collapse_len = self.metadata['collapse_len_mm']
|
|
|
|
min_stitch_len = self.metadata['min_stitch_len_mm']
|
|
|
|
stitch_groups = self.elements_to_stitch_groups(self.elements)
|
|
|
|
stitch_plan = stitch_groups_to_stitch_plan(stitch_groups, collapse_len=collapse_len, min_stitch_len=min_stitch_len)
|
|
|
|
ThreadCatalog().match_and_apply_palette(stitch_plan, self.get_inkstitch_metadata()['thread-palette'])
|
|
|
|
|
|
|
|
line_width = convert_unit(f"{self.options.line_width}mm", self.svg.document_unit)
|
|
|
|
layer = render_stitch_plan(self.svg, stitch_plan, False, visual_commands=False,
|
|
|
|
render_jumps=False, line_width=line_width)
|
|
|
|
|
2024-07-03 05:09:06 +00:00
|
|
|
write_png_output(self.svg, layer, self.options.dpi)
|
2024-06-26 20:51:15 +00:00
|
|
|
|
|
|
|
# don't let inkex output the SVG!
|
|
|
|
sys.exit(0)
|
|
|
|
|
|
|
|
|
2024-07-03 05:09:06 +00:00
|
|
|
def write_png_output(svg, layer, dpi):
|
2024-06-26 20:51:15 +00:00
|
|
|
with TemporaryDirectory() as tempdir:
|
|
|
|
# Inkex's command functionality also writes files to temp directories like this.
|
|
|
|
temp_svg_path = f"{tempdir}/temp.svg"
|
|
|
|
temp_png_path = f"{tempdir}/temp.png"
|
|
|
|
with open(temp_svg_path, "wb") as f:
|
|
|
|
f.write(svg.tostring())
|
|
|
|
|
2024-07-03 05:09:06 +00:00
|
|
|
generate_png(svg, layer, temp_svg_path, temp_png_path, dpi)
|
2024-06-26 20:51:15 +00:00
|
|
|
|
|
|
|
# inkscape will read the file contents from stdout and copy
|
|
|
|
# to the destination file that the user chose
|
|
|
|
with open(temp_png_path, 'rb') as output_file:
|
|
|
|
sys.stdout.buffer.write(output_file.read())
|
|
|
|
|
|
|
|
|
2024-07-03 05:09:06 +00:00
|
|
|
def generate_png(svg, layer, input_path, output_path, dpi):
|
2024-06-26 20:51:15 +00:00
|
|
|
inkscape(input_path, actions="; ".join([
|
2024-07-03 05:09:06 +00:00
|
|
|
f"export-id: {layer.get_id()}",
|
2024-06-26 20:51:15 +00:00
|
|
|
"export-id-only",
|
|
|
|
"export-type:png",
|
2024-07-03 05:09:06 +00:00
|
|
|
f"export-dpi: {dpi}",
|
|
|
|
f"export-filename: {output_path}",
|
|
|
|
f"export-background: {get_pagecolor(svg.namedview)}",
|
2024-06-26 20:51:15 +00:00
|
|
|
"export-do" # Inkscape docs say this should be implicit at the end, but it doesn't seem to be.
|
|
|
|
]))
|