Skip to content

Commit

Permalink
merge master
Browse files Browse the repository at this point in the history
  • Loading branch information
ldevillez committed Oct 15, 2023
2 parents c00e975 + d7615e4 commit cb15466
Show file tree
Hide file tree
Showing 11 changed files with 471 additions and 25 deletions.
9 changes: 7 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,21 @@
- Adding logo for SVG2TikZ
- Adding theme for doc of SVG2TikZ
- Adding autodoc
- Adding basic switch tag handle
- Adding failsafe for non defined sys.stdout.buffer
- Adding list of tikz color
### Changed
- Using style from new inkex
- Using path from new inkex
- Using transform from new inkex
- Using colors from new inkex
- Using Vector2d from inkex instead of Point DataClass
- Removing input-options and using unit from viewbox
- Adding list of tikz color
- Correcting licence in the pyproject
- Unify conversion of coordinate: (x, y)
- Convert_file and convert_svg functions are now directly accesible from root
- Fixing the installation of svg2tikz as command line tool
- Try excepting non existing tags in a svg
- Changing the select file option in tikz effect to new file
### Deprecated
- Gradient are commented for the time being
### Removed
Expand All @@ -30,6 +33,8 @@
- License in the main file
### Fixed
- Transform working with --noreversey
- Fixing the installation of svg2tikz as command line tool
- Wrapping line now respect newline
### Security

## v2.1.0 - 2023/06/28
Expand Down
8 changes: 4 additions & 4 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

111 changes: 93 additions & 18 deletions svg2tikz/tikz_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@
from inkex.transforms import Vector2d
from lxml import etree

try:
SYS_OUTPUT_BUFFER = sys.stdout.buffer
except AttributeError:
logging.warning("Sys has no output buffer, redirecting to None")
SYS_OUTPUT_BUFFER = None

#### Utility functions and classes

TIKZ_BASE_COLOR = [
Expand Down Expand Up @@ -175,7 +181,16 @@ def filter_tag(node):
# As it is done in lxml
if node.tag == etree.Comment:
return False
if node.TAG in ["desc", "namedview", "defs", "svg", "symbol", "title", "style"]:
if node.TAG in [
"desc",
"namedview",
"defs",
"svg",
"symbol",
"title",
"style",
"metadata",
]:
return False
return True

Expand Down Expand Up @@ -785,7 +800,11 @@ def style_to_tz(self, node=None):
options = []

# Stroke and fill
for use_path in [("stroke", "draw"), ("fill", "fill")]:
for use_path in (
[("fill", "text")]
if node.TAG == "text"
else [("stroke", "draw"), ("fill", "fill")]
):
value = style.get(use_path[0])
if value != "none" and value is not None:
options.append(
Expand Down Expand Up @@ -955,6 +974,53 @@ def _handle_group(self, groupnode):
s = code
return s

def _handle_switch(self, groupnode):
"""
Convert a svg switch to tikzcode
All the elements are returned for now
"""
options = self.trans_to_tz(groupnode)

old_indent = self.text_indent

if len(options) > 0:
self.text_indent += TEXT_INDENT

group_id = groupnode.get_id()
code = self._output_group(groupnode)

self.text_indent = old_indent

if code == "":
return ""

extra = ""
if self.options.verbose and group_id:
extra = f"%% {group_id}"

hide = "none" in options

s = ""
if len(options) > 0 or self.options.verbose:
# Remove it from the list
if hide or self.options.verbose:
options.remove("none")

pstyles = [",".join(options)]

if "opacity" in pstyles[0]:
pstyles.append("transparency group")

s += self.text_indent + "\\begin{scope}"
s += f"[{','.join(pstyles)}]{extra}\n{code}"
s += self.text_indent + "\\end{scope}\n"

if hide:
s = "%" + s.replace("\n", "\n%")[:-1]
else:
s = code
return s

def _handle_image(self, node):
"""Handles the image tag and returns tikz code"""
p = self.convert_unit_coord(Vector2d(node.left, node.top))
Expand Down Expand Up @@ -1179,26 +1245,30 @@ def _output_group(self, group):
The group is processed recursively if it contains sub groups.
"""
string = ""
# transform = []
for node in group:
if not filter_tag(node):
continue

if node.TAG == "use":
node = node.unlink()

if node.TAG == "switch":
string += self._handle_switch(node)
continue

if node.TAG == "g":
string += self._handle_group(node)
continue

goptions = self.style_to_tz(node) + self.trans_to_tz(node)

cmd = []
try:
goptions = self.style_to_tz(node) + self.trans_to_tz(node)
except AttributeError as msg:
attr = msg.args[0].split("attribute")[1].split(".")[0]
logging.warning("%s attribute cannot be represented", attr)

pathcode = ""

if self.options.verbose:
cmd.append(f"%{node.get_id()}\n")
string += self.text_indent + f"%{node.get_id()}\n"

if node.TAG == "path":
optionscode = f"[{','.join(goptions)}]" if len(goptions) > 0 else ""
Expand Down Expand Up @@ -1242,16 +1312,21 @@ def _output_group(self, group):
logging.debug("Unhandled element %s", node.tag)
continue

if pathcode != "":
cmd.append(pathcode)

cmd = [self.text_indent + c for c in cmd]
string += "\n".join(cmd) + ";\n\n\n\n"
if self.options.wrap:
string += "\n".join(
wrap(
self.text_indent + pathcode,
80,
subsequent_indent=" ",
break_long_words=False,
drop_whitespace=False,
replace_whitespace=False,
)
)
else:
string += self.text_indent + pathcode

if self.options.wrap:
string = "\n".join(
wrap(string, 80, subsequent_indent=" ", break_long_words=False)
)
string += ";\n\n\n\n"

return string

Expand Down Expand Up @@ -1333,7 +1408,7 @@ def save_raw(self, _):

self.options.output.write(out)

def run(self, args=None, output=sys.stdout.buffer):
def run(self, args=None, output=SYS_OUTPUT_BUFFER):
"""
Custom inkscape entry point to remove agr processing
"""
Expand Down
2 changes: 1 addition & 1 deletion svg2tikz/tikz_export_effect.inx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<param name="tab" type="notebook">
<page name="options" gui-text="Document">
<label appearance="header">Output file</label>
<param name="output" type="path" _gui-text="Output filename">none</param>
<param name="output" type="path" mode="file_new" _gui-text="Output filename">none</param>
<param name="clipboard" type="boolean" _gui-text="Export to clipboard">false</param>
<separator />
<label appearance="header">Tikz option</label>
Expand Down
15 changes: 15 additions & 0 deletions tests/test_complete_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,21 @@ def test_text(self):
filename = "text"
create_test_from_filename(filename, self)

def test_switch(self):
"""Test complete convert simple switch case"""
filename = "switch_simple"
create_test_from_filename(filename, self)

def test_text_fill_color(self):
"""Test complete convert text with color case"""
filename = "text_fill_color"
create_test_from_filename(filename, self)

def test_wrap(self):
"""Test complete convert wrap option"""
filename = "rectangle_wrap"
create_test_from_filename(filename, self, wrap=True)


if __name__ == "__main__":
unittest.main()
91 changes: 91 additions & 0 deletions tests/testfiles/rectangle_wrap.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
35 changes: 35 additions & 0 deletions tests/testfiles/rectangle_wrap.tex
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}

\begin{document}
\definecolor{cc2a2f2}{RGB}{194,162,242}
\definecolor{ccdf4c8}{RGB}{205,244,200}


\def \globalscale {1.000000}
\begin{tikzpicture}[y=1cm, x=1cm, yscale=\globalscale,xscale=\globalscale, inner sep=0pt, outer sep=0pt]
\path[draw=black,line cap=,line width=0.2cm] (0.7985, 28.9408) rectangle
(5.0753, 27.1111);



\path[draw=black,line cap=,line width=0.2cm,rounded corners=1.4343cm] (0.7985,
25.8962) rectangle (5.0753, 24.0665);



\path[draw=black,line cap=,line width=0.2cm,rounded corners=0.644cm] (7.5137,
28.9408) rectangle (11.7905, 27.1111);



\path[draw=cc2a2f2,fill=ccdf4c8,line cap=,line width=0.2cm] (7.5137, 25.8962)
rectangle (11.7905, 24.0665);




\end{tikzpicture}
\end{document}
Loading

0 comments on commit cb15466

Please sign in to comment.