Skip to content
Open
Show file tree
Hide file tree
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
14 changes: 9 additions & 5 deletions mathics/autoload/formats/SVG/Export.m
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@


SVGExport[filename_, expr_, opts___] :=
Module[{strm, data},
Module[{strm, data, p, q, expr2},
strm = OpenWrite[filename];
If[strm === $Failed, Return[$Failed]];
If[System`$UseSansSerif,
data = StringTake[ToString[MathMLForm[expr]],{23,-8}],
data = StringTake[ToString[MathMLForm[expr]],{23,-8}]];
WriteString[strm, "<svg>" <> data <> "</svg>"];
expr2 = If[Head[expr]=!=System`Graphics, System`Graphics[{System`Inset[ToString[expr]]}], expr];
expr2= MathMLForm[expr2];
data=ToString[expr2];
p = StringPosition[data, "data:image/svg+xml;base64"][[1]][[2]];
(*Let's assume that the end of the string is reached just before the last quote. *)
q = StringPosition[data,"\""][[-1]][[-2]];
data = StringTake[data ,{p+2,q-1}];
WriteString[strm, System`Convert`B64Dump`B64Decode[data]];
Close[strm];
]

Expand Down
6 changes: 3 additions & 3 deletions mathics/builtin/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -707,13 +707,13 @@ def get_option_values(self, leaves, **options):
default[option] = parse_builtin_rule(value)
return default

def boxes_to_text(self, leaves, **options) -> str:
def _boxes_to_text(self, leaves, **options) -> str:
raise BoxConstructError

def boxes_to_xml(self, leaves, **options) -> str:
def _boxes_to_mathml(self, leaves, **options) -> str:
raise BoxConstructError

def boxes_to_tex(self, leaves, **options) -> str:
def _boxes_to_tex(self, leaves, **options) -> str:
raise BoxConstructError


Expand Down
6 changes: 3 additions & 3 deletions mathics/builtin/compilation.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,17 +193,17 @@ class CompiledCodeBox(BoxConstruct):
Used internally by <i>CompileCode[]</i>.
"""

def boxes_to_text(self, leaves=None, **options):
def _boxes_to_text(self, leaves=None, **options):
if leaves is None:
leaves = self._leaves
return leaves[0].value

def boxes_to_xml(self, leaves=None, **options):
def _boxes_to_mathml(self, leaves=None, **options):
if leaves is None:
leaves = self._leaves
return leaves[0].value

def boxes_to_tex(self, leaves=None, **options):
def _boxes_to_tex(self, leaves=None, **options):
if leaves is None:
leaves = self._leaves
return leaves[0].value
Expand Down
38 changes: 26 additions & 12 deletions mathics/builtin/graphics.py
Original file line number Diff line number Diff line change
Expand Up @@ -2577,7 +2577,7 @@ def init(self, graphics, style, item=None, content=None, pos=None, opos=(0, 0)):
self.content = content
self.pos = pos
self.opos = opos
self.content_text = self.content.boxes_to_text(
self.content_text = self.content._boxes_to_text(
evaluation=self.graphics.evaluation
)

Expand All @@ -2592,17 +2592,29 @@ def extent(self):

def to_svg(self):
x, y = self.pos.pos()
content = self.content.boxes_to_xml(evaluation=self.graphics.evaluation)
style = create_css(font_color=self.color)
svg = (
'<foreignObject x="%f" y="%f" ox="%f" oy="%f" style="%s">'
"<math>%s</math></foreignObject>"
) % (x, y, self.opos[0], self.opos[1], style, content)
content = self.content._boxes_to_text(evaluation=self.graphics.evaluation)
style = create_css(
font_color=self.color, edge_color=self.color, face_color=self.color
)

# content = self.content._boxes_to_mathml(evaluation=self.graphics.evaluation)
# style = create_css(font_color=self.color)
# svg = (
# '<foreignObject x="%f" y="%f" ox="%f" oy="%f" style="%s">'
# "<math>%s</math></foreignObject>")
svg = ('<text x="%f" y="%f" ox="%f" oy="%f" style="text-anchor:middle; dominant-baseline:middle; %s">' "%s" "</text>") % (
x,
y,
self.opos[0],
self.opos[1],
style,
content,
)
return svg

def to_asy(self):
x, y = self.pos.pos()
content = self.content.boxes_to_tex(evaluation=self.graphics.evaluation)
content = self.content._boxes_to_tex(evaluation=self.graphics.evaluation)
pen = create_pens(edge_color=self.color)
asy = 'label("$%s$", (%s,%s), (%s,%s), %s);' % (
content,
Expand Down Expand Up @@ -2933,7 +2945,7 @@ class GraphicsBox(BoxConstruct):

attributes = ("HoldAll", "ReadProtected")

def boxes_to_text(self, leaves=None, **options):
def _boxes_to_text(self, leaves=None, **options):
if not leaves:
leaves = self._leaves

Expand Down Expand Up @@ -3140,7 +3152,7 @@ def get_range(min, max):

return elements, calc_dimensions

def boxes_to_tex(self, leaves=None, **options):
def _boxes_to_tex(self, leaves=None, **options):
if not leaves:
leaves = self._leaves
elements, calc_dimensions = self._prepare_elements(
Expand Down Expand Up @@ -3195,7 +3207,7 @@ def boxes_to_tex(self, leaves=None, **options):

return tex

def boxes_to_xml(self, leaves=None, **options):
def _boxes_to_mathml(self, leaves=None, **options):
if not leaves:
leaves = self._leaves
elements, calc_dimensions = self._prepare_elements(leaves, options, neg_y=True)
Expand Down Expand Up @@ -3234,11 +3246,13 @@ def boxes_to_xml(self, leaves=None, **options):

# mglyph, which is what we have been using, is bad because MathML standard changed.
# metext does not work because the way in which we produce the svg images is also based on this outdated mglyph behaviour.
# template = "<mtext><img width="%dpx" height="%dpx" src="data:image/svg+xml;base64,%s"/></mtext>"
# template = '<mtext width="%dpx" height="%dpx"><img width="%dpx" height="%dpx" src="data:image/svg+xml;base64,%s"/></mtext>'
template = (
'<mglyph width="%dpx" height="%dpx" src="data:image/svg+xml;base64,%s"/>'
)
return template % (
# int(width),
# int(height),
int(width),
int(height),
base64.b64encode(svg_xml.encode("utf8")).decode("utf8"),
Expand Down
2 changes: 1 addition & 1 deletion mathics/builtin/graphics3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,7 @@ def boxes_to_tex(self, leaves=None, **options):
)
return tex

def boxes_to_xml(self, leaves=None, **options):
def _boxes_to_mathml(self, leaves=None, **options):
if not leaves:
leaves = self._leaves

Expand Down
6 changes: 3 additions & 3 deletions mathics/builtin/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -2102,10 +2102,10 @@ def test(self, expr):


class ImageBox(BoxConstruct):
def boxes_to_text(self, leaves=None, **options):
def _boxes_to_text(self, leaves=None, **options):
return "-Image-"

def boxes_to_xml(self, leaves=None, **options):
def _boxes_to_mathml(self, leaves=None, **options):
if leaves is None:
leaves = self._leaves
# see https://tools.ietf.org/html/rfc2397
Expand All @@ -2115,7 +2115,7 @@ def boxes_to_xml(self, leaves=None, **options):
leaves[2].get_int_value(),
)

def boxes_to_tex(self, leaves=None, **options):
def _boxes_to_tex(self, leaves=None, **options):
return "-Image-"


Expand Down
4 changes: 2 additions & 2 deletions mathics/builtin/importexport.py
Original file line number Diff line number Diff line change
Expand Up @@ -1917,8 +1917,8 @@ class ExportString(Builtin):
. 2,
. 3,
. 4,
>> ExportString[Integrate[f[x],{x,0,2}], "SVG"]
= ...
>> ExportString[Integrate[f[x],{x,0,2}], "SVG"]//Head
= String
"""

options = {
Expand Down
13 changes: 8 additions & 5 deletions mathics/builtin/inout.py
Original file line number Diff line number Diff line change
Expand Up @@ -845,7 +845,7 @@ def boxes_to_tex(self, leaves=None, **box_options) -> str:
result += r"\end{array}"
return result

def boxes_to_xml(self, leaves=None, **box_options) -> str:
def _boxes_to_mathml(self, leaves=None, **box_options) -> str:
if not leaves:
leaves = self._leaves
evaluation = box_options.get("evaluation")
Expand All @@ -872,7 +872,7 @@ def boxes_to_xml(self, leaves=None, **box_options) -> str:
for item in row:
result += "<mtd {0}>{1}</mtd>".format(
joined_attrs,
item.evaluate(evaluation).boxes_to_xml(**new_box_options),
item.evaluate(evaluation)._boxes_to_mathml(**new_box_options),
)
result += "</mtr>\n"
result += "</mtable>"
Expand Down Expand Up @@ -2084,20 +2084,23 @@ def apply_mathml(self, expr, evaluation) -> Expression:

boxes = MakeBoxes(expr).evaluate(evaluation)
try:
xml = boxes.boxes_to_xml(evaluation=evaluation)
xml = boxes._boxes_to_mathml(evaluation=evaluation)
except BoxError:
evaluation.message(
"General",
"notboxes",
Expression("FullForm", boxes).evaluate(evaluation),
)
xml = ""
is_a_picture = xml[:6] == "<mtext"

# mathml = '<math><mstyle displaystyle="true">%s</mstyle></math>' % xml
# #convert_box(boxes)
query = evaluation.parse("System`$UseSansSerif")
usesansserif = query.evaluate(evaluation).to_python()
if usesansserif:
xml = '<mstyle mathvariant="sans-serif">%s</mstyle>' % xml
if not is_a_picture:
if usesansserif:
xml = '<mstyle mathvariant="sans-serif">%s</mstyle>' % xml

mathml = '<math display="block">%s</math>' % xml # convert_box(boxes)
return Expression("RowBox", Expression(SymbolList, String(mathml)))
Expand Down
2 changes: 1 addition & 1 deletion mathics/builtin/strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -1645,7 +1645,7 @@ def apply(self, value, evaluation, **options):
"ToString[value_, OptionsPattern[ToString]]"
encoding = options["options"]["System`CharacterEncoding"]
text = value.format(evaluation, "System`OutputForm", encoding=encoding)
text = text.boxes_to_text(evaluation=evaluation)
text = text._boxes_to_text(evaluation=evaluation)
return String(text)


Expand Down
2 changes: 1 addition & 1 deletion mathics/core/evaluation.py
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ def format_output(self, expr, format=None):
raise ValueError

try:
boxes = result.boxes_to_text(evaluation=self)
boxes = Expression("ToString", result).evaluate(evaluation=self).value
except BoxError:
self.message(
"General", "notboxes", Expression("FullForm", result).evaluate(self)
Expand Down
Loading