def effect(self): fallback = False try: call('virtualenv', TARGET_DIR, p='python3') except CommandNotFound: fallback = True except ProgramRunError as err: raise inkex.AbortExtension( "There has been a problem creating the python environment:\n" + str(err)) if fallback: # Add a fallback for places like windows where python isn't available. if self.install_fallback(): return raise inkex.AbortExtension( "You must have the python-virtualenv package installed. This should have" " been included with Inkscape, but in some special cases it might not" " be. Please install this software externally and try again.") try: call(os.path.join(TARGET_DIR, 'bin', 'pip'), 'install', self.options.version) except CommandNotFound: raise inkex.AbortExtension( "Can't find pip program after environment initialisation!") except ProgramRunError as err: raise inkex.AbortExtension( "Error installing extension manager package:\n" + str(err))
def generate(self): tex_file = os.path.join(self.tempdir, 'input.tex') pdf_file = os.path.join(self.tempdir, 'input.pdf') # Auto-generate by pdflatex svg_file = os.path.join(self.tempdir, 'output.svg') with open(tex_file, 'w') as fhl: self.write_latex(fhl) call('pdflatex', tex_file,\ output_directory=self.tempdir,\ halt_on_error=True, oldie=True) inkscape(pdf_file, export_filename=svg_file, pdf_page=1, pdf_poppler=True, export_type="svg") if not os.path.isfile(svg_file): fn = os.path.basename(svg_file) if os.path.isfile(fn): # Inkscape bug detected, file got saved wrong svg_file = fn with open(svg_file, 'r') as fhl: svg = load_svg(fhl).getroot() svg.set_random_ids(backlinks=True) for child in svg: if isinstance(child, ShapeElement): yield child elif isinstance(child, Defs): for def_child in child: #def_child.set_random_id() self.svg.defs.append(def_child)
def call(self, input_file, output_file): cp = os.path.dirname( os.path.abspath(__file__)) + "/svg-embed-and-crop/*" java = "javaw -cp \"" command.call('javaw', '-cp', cp, 'edu.emory.cellbio.svg.EmbedAndCropInkscapeEntry', input_file, "-o", output_file) if not os.path.exists(output_file): raise inkex.AbortExtension("Plugin canceled") return output_file
def save(self, stream): scribus_version = call(SCRIBUS_EXE, '-g', '--version').decode('utf-8') version_match = VERSION_REGEX.search(scribus_version) if version_match is None: raise AbortExtension( f"Could not detect Scribus version ({scribus_version})") major = int(version_match.group(1)) minor = int(version_match.group(2)) point = int(version_match.group(3)) if (major < 1) or (major == 1 and minor < 5): raise AbortExtension( f"Found Scribus {version_match.group(0)}. This extension requires Scribus 1.5.x." ) input_file = self.options.input_file py_file = os.path.join(self.tempdir, 'scribus.py') svg_file = os.path.join(self.tempdir, 'in.svg') profiles = self.svg.defs.findall("svg:color-profile") if len(profiles) == 0: raise AbortExtension( "Please select a color profile in the document settings.") elif len(profiles) > 1: raise AbortExtension( "Please only link a single color profile in the document settings. No output generated." ) iccPath = profiles[0].get("xlink:href") with open(input_file) as f: with open(svg_file, "w") as f1: for line in f: f1.write(line) f.close() pdf_file = os.path.join(self.tempdir, 'out.pdf') width = self.svg.unittouu(self.svg.get('width')) height = self.svg.unittouu(self.svg.get('height')) with open(py_file, 'w') as fhl: self.generate_script(fhl, width, height, iccPath) call(SCRIBUS_EXE, '-g', '-py', py_file, svg_file, pdf_file) with open(pdf_file, 'rb') as fhl: stream.write(fhl.read())
def call(self, input_file, output_file): crop = '-dEPSCrop' if self.options.crop else None call('ps2pdf', crop, input_file, output_file)
def call(self, input_file, output_file): call('fig2dev', '-L', 'svg', input_file, output_file)
def test_binary_call(self): """Calls should allow binary stdin""" # https://gitlab.com/inkscape/extensions/-/commit/2e504f2a3f6bb627f17b267c5623a71005d7234d#note_164780678 binary = b'\x1f\x8b\x08\x00eh\xc3\\\x02\xffK\xcb\xcf\x07\x00!es\x8c\x03\x00\x00\x00' stdout = call('gunzip', stdin=binary)
def save(self, stream): pngs = OrderedDict() valid = False for node in self.svg.xpath("/svg:svg/*[name()='g' or @style][@id]"): if not len(node): # pylint: disable=len-as-condition # Ignore empty layers continue valid = True node_id = node.get('id') name = node.get("inkscape:label", node_id) pngs[name] = take_snapshot(self.document, dirname=self.tempdir, name=name, dpi=int(self.options.dpi), export_id=node_id, export_id_only=True, export_area_page=True, export_background_opacity=int( bool(self.options.background))) if not valid: inkex.errormsg( _('This extension requires at least one non empty layer.')) return xcf = os.path.join(self.tempdir, "{}.xcf".format(self.docname)) script_fu = """ (tracing 1) (define (png-to-layer img png_filename layer_name) (let* ( (png (car (file-png-load RUN-NONINTERACTIVE png_filename png_filename))) (png_layer (car (gimp-image-get-active-layer png))) (xcf_layer (car (gimp-layer-new-from-drawable png_layer img))) ) (gimp-image-add-layer img xcf_layer -1) (gimp-drawable-set-name xcf_layer layer_name) ) ) (let* ( (img (car (gimp-image-new 200 200 RGB))) ) (gimp-image-set-resolution img {dpi} {dpi}) (gimp-image-undo-disable img) (for-each (lambda (names) (png-to-layer img (car names) (cdr names)) ) (map cons '("{files}") '("{names}")) ) (gimp-image-resize-to-layers img) """.format(dpi=self.options.dpi, files='" "'.join(pngs.values()), names='" "'.join(list(pngs))) if self.options.guides: for dim, guides in self.get_guides(): script_fu += """ (for-each (lambda ({d}Guide) (gimp-image-add-{d}guide img {d}Guide) ) '({g}) )""".format(d=dim, g=guides) # Grid if self.options.grid: for fu_let in self.get_grid(): script_fu += "\n" + fu_let + "\n" script_fu += """ (gimp-image-undo-enable img) (gimp-file-save RUN-NONINTERACTIVE img (car (gimp-image-get-active-layer img)) "{xcf}" "{xcf}")) (gimp-quit 0) """.format(xcf=xcf) call('gimp', "-b", "-", i=True, batch_interpreter="plug-in-script-fu-eval", stdin=script_fu) with open(xcf, 'rb') as fhl: stream.write(fhl.read())