Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding an option for use Asymptote to build svg graphics #1145

Closed
wants to merge 17 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 63 additions & 4 deletions mathics/builtin/graphics.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
Real,
String,
Symbol,
SymbolTrue,
SymbolFalse,
strip_context,
system_symbols,
system_symbols_dict,
Expand Down Expand Up @@ -2936,6 +2938,12 @@ class GraphicsBox(BoxConstruct):

attributes = ("HoldAll", "ReadProtected")

messages = {
"asynotav": 'Asymptote is not available in this system. Using the buggy backend.',
"noasyfile": 'Asy requires write permisions over a temporary file, but it was not available. Using the buggy backend',
"asyfail": 'Asymptote failed building the svg picture. Using the buggy backend.',
}

def boxes_to_text(self, leaves, **options):
self._prepare_elements(leaves, options) # to test for Box errors
return "-Graphics-"
Expand Down Expand Up @@ -3176,14 +3184,12 @@ def boxes_to_tex(self, leaves, **options):
asy_background = ""

tex = r"""
\begin{asy}
usepackage("amsmath");
size(%scm, %scm);
%s
%s
clip(%s);
%s
\end{asy}
""" % (
asy_number(width / 60),
asy_number(height / 60),
Expand All @@ -3192,10 +3198,63 @@ def boxes_to_tex(self, leaves, **options):
asy_box,
asy_completely_visible,
)

return tex
forxml = options.get("forxml", None)
if forxml:
return (tex, width, height)
else:
return "\n\\begin{asy}\n" + tex + "\n\\end{asy}\n"

def boxes_to_xml(self, leaves, **options):
evaluation = options.get("evaluation", None)
check_asy = False
if evaluation:
check_asy = evaluation.definitions.get_ownvalue("Settings`UseAsyForGraphics2D")
if check_asy:
check_asy = check_asy.replace.is_true()

if check_asy:
import os
from subprocess import DEVNULL, STDOUT, check_call
import tempfile
try:
check_call(['asy', '--version'], stdout=DEVNULL, stderr=DEVNULL)
except:
check_asy = False
evaluation.message("GraphicsBox", "asynotav")
Expression("Set", Symbol("Settings`UseAsyForGraphics2D"), SymbolFalse).evaluate(evaluation)

if check_asy:
asy, width, height = self.boxes_to_tex(leaves, forxml=True, **options)
fin = os.path.join(tempfile._get_default_tempdir(), next(tempfile._get_candidate_names()))
fout = fin + ".svg"
try:
with open(fin, 'w+') as borrador:
borrador.write(asy)
except:
evaluation.message("GraphicsBox", "noasyfile")
check_asy = False

if check_asy:
try:
check_call(['asy', '-f', 'svg', '--svgemulation' ,'-o', fout, fin], stdout=DEVNULL, stderr=DEVNULL)
except:
evaluation.message("GraphicsBox", "asyfail")
check_asy = False

if check_asy:
with open(fout, 'rt') as ff:
svg = ff.read()

svg = svg[svg.find("<svg "):]
return (
'<img width="%dpx" height="%dpx" src="data:image/svg+xml;base64,%s"/>'
% (
int(width),
int(height),
base64.b64encode(svg.encode("utf8")).decode("utf8"),
)
)
# Not using asymptote. Continue with the buggy backend...
elements, calc_dimensions = self._prepare_elements(leaves, options, neg_y=True)

xmin, xmax, ymin, ymax, w, h, width, height = calc_dimensions()
Expand Down