@@ -3145,7 +3145,7 @@ def get_range(min, max):
31453145
31463146 return elements , calc_dimensions
31473147
3148- def boxes_to_tex (self , leaves = None , ** options ):
3148+ def boxes_to_tex (self , leaves = None , forxml = False , ** options ):
31493149 if not leaves :
31503150 leaves = self ._leaves
31513151 elements , calc_dimensions = self ._prepare_elements (
@@ -3181,14 +3181,12 @@ def boxes_to_tex(self, leaves=None, **options):
31813181 asy_background = ""
31823182
31833183 tex = r"""
3184- \begin{asy}
31853184usepackage("amsmath");
31863185size(%scm, %scm);
31873186%s
31883187%s
31893188clip(%s);
31903189%s
3191- \end{asy}
31923190""" % (
31933191 asy_number (width / 60 ),
31943192 asy_number (height / 60 ),
@@ -3198,11 +3196,58 @@ def boxes_to_tex(self, leaves=None, **options):
31983196 asy_completely_visible ,
31993197 )
32003198
3201- return tex
3199+ if forxml :
3200+ return (tex , width , height )
3201+ else :
3202+ return "\n \\ begin{asy}\n " + tex + "\n \\ end{asy}\n "
32023203
32033204 def boxes_to_xml (self , leaves = None , ** options ):
32043205 if not leaves :
32053206 leaves = self ._leaves
3207+ evaluation = options .get ("evaluation" , None )
3208+ check_asy = False
3209+ if evaluation :
3210+ check_asy = evaluation .definitions .get_ownvalue ("Settings`UseAsyForGraphics2D" )
3211+ if check_asy :
3212+ check_asy = check_asy .replace .is_true ()
3213+ if check_asy :
3214+ import os
3215+ from subprocess import DEVNULL , STDOUT , check_call
3216+ import tempfile
3217+ try :
3218+ check_call (['asy' , '--version' ], stdout = DEVNULL , stderr = DEVNULL )
3219+ except :
3220+ check_asy = False
3221+
3222+ if check_asy :
3223+ asy , width , height = self .boxes_to_tex (leaves , forxml = True , ** options )
3224+ fin = os .path .join (tempfile ._get_default_tempdir (), next (tempfile ._get_candidate_names ()))
3225+ fout = fin + ".svg"
3226+ with open (fin , 'w+' ) as borrador :
3227+ borrador .write (asy )
3228+
3229+ try :
3230+ check_call (['asy' , '-f' , 'svg' , '--svgemulation' ,'-o' , fout , fin ], stdout = DEVNULL , stderr = DEVNULL )
3231+ except :
3232+ print ("Asy failed to build a svg" )
3233+ check_asy = False
3234+
3235+ if check_asy :
3236+ with open (fout , 'rt' ) as ff :
3237+ svg = ff .read ()
3238+
3239+ svg = svg [svg .find ("<svg " ):]
3240+ return (
3241+ '<img width="%dpx" height="%dpx" src="data:image/svg+xml;base64,%s"/>'
3242+ % (
3243+ int (width ),
3244+ int (height ),
3245+ base64 .b64encode (svg .encode ("utf8" )).decode ("utf8" ),
3246+ )
3247+ )
3248+ else :
3249+ print ("Asy not available. Continue with standard" )
3250+
32063251 elements , calc_dimensions = self ._prepare_elements (leaves , options , neg_y = True )
32073252
32083253 xmin , xmax , ymin , ymax , w , h , width , height = calc_dimensions ()
0 commit comments