Skip to content

Commit faf0528

Browse files
committed
Test Point and Ellipse
1 parent 346b19f commit faf0528

2 files changed

Lines changed: 92 additions & 6 deletions

File tree

mathics/builtin/inout.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2063,25 +2063,25 @@ def apply_mathml(self, expr, evaluation) -> Expression:
20632063

20642064
boxes = MakeBoxes(expr).evaluate(evaluation)
20652065
try:
2066-
xml = boxes.boxes_to_mathml(evaluation=evaluation)
2066+
mathml = boxes.boxes_to_mathml(evaluation=evaluation)
20672067
except BoxError:
20682068
evaluation.message(
20692069
"General",
20702070
"notboxes",
20712071
Expression("FullForm", boxes).evaluate(evaluation),
20722072
)
2073-
xml = ""
2074-
is_a_picture = xml[:6] == "<mtext"
2073+
mathml = ""
2074+
is_a_picture = mathml[:6] == "<mtext"
20752075

2076-
# mathml = '<math><mstyle displaystyle="true">%s</mstyle></math>' % xml
2076+
# mathml = '<math><mstyle displaystyle="true">%s</mstyle></math>' % mathml
20772077
# #convert_box(boxes)
20782078
query = evaluation.parse("Settings`$UseSansSerif")
20792079
usesansserif = query.evaluate(evaluation).to_python()
20802080
if not is_a_picture:
20812081
if isinstance(usesansserif, bool) and usesansserif:
2082-
xml = '<mstyle mathvariant="sans-serif">%s</mstyle>' % xml
2082+
mathml = '<mstyle mathvariant="sans-serif">%s</mstyle>' % mathml
20832083

2084-
mathml = '<math display="block">%s</math>' % xml # convert_box(boxes)
2084+
mathml = '<math display="block">%s</math>' % mathml # convert_box(boxes)
20852085
return Expression("RowBox", Expression(SymbolList, String(mathml)))
20862086

20872087

test/test_formatter.py

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import re
2+
from mathics.core.expression import Symbol, Integer0, Expression
3+
from mathics.core.evaluation import Evaluation
4+
from mathics.session import MathicsSession
5+
from mathics.builtin.inout import MakeBoxes
6+
from mathics.core.formatter import lookup_method
7+
8+
session = MathicsSession(add_builtin=True, catch_interrupt=False)
9+
evaluation = Evaluation(session.definitions)
10+
11+
GraphicsSymbol = Symbol("Graphics")
12+
ListSymbol = Symbol("List")
13+
14+
svg_wrapper_pat = r"""^\s*
15+
\s*<svg xmlns:svg="http://www.w3.org/2000/svg"
16+
\s*xmlns="http://www.w3.org/2000/svg"
17+
\s*version="1\.1"
18+
"""
19+
20+
21+
def extract_svg_body(svg):
22+
matches = re.match(svg_wrapper_pat, svg)
23+
body = svg[len(matches.group(0)) :]
24+
assert matches
25+
view_inner_match = re.match(r"^\s+viewBox=.*\n\s+(.*)", body)
26+
assert view_inner_match
27+
inner_svg = view_inner_match.group(1)
28+
print(inner_svg)
29+
return inner_svg
30+
31+
32+
def get_svg(expression):
33+
options = {}
34+
boxes = MakeBoxes(expression).evaluate(evaluation)
35+
36+
# Would be nice to DRY this boilerplate from boxes_to_mathml
37+
38+
leaves = boxes._leaves
39+
elements, calc_dimensions = boxes._prepare_elements(
40+
leaves, options=options, neg_y=True
41+
)
42+
xmin, xmax, ymin, ymax, w, h, width, height = calc_dimensions()
43+
data = (elements, xmin, xmax, ymin, ymax, w, h, width, height)
44+
45+
format_fn = lookup_method(boxes, "svg")
46+
return format_fn(boxes, leaves, data=data, options=options)
47+
48+
49+
def test_svg_circle():
50+
expression = Expression(
51+
GraphicsSymbol,
52+
Expression("Circle", Expression(ListSymbol, Integer0, Integer0)),
53+
)
54+
55+
svg = get_svg(expression)
56+
inner_svg = extract_svg_body(svg)
57+
58+
# Circles are implemented as ellipses with equal major and minor axes.
59+
# Check for that.
60+
matches = re.match(
61+
r'^<ellipse cx="(\S+)" cy="(\S+)" rx="(\S+)" ry="(\S+)" .*/>', inner_svg
62+
)
63+
assert matches
64+
assert matches.group(1) == matches.group(2) == matches.group(3)
65+
66+
def test_svg_point():
67+
expression = Expression(
68+
GraphicsSymbol,
69+
Expression("Point", Expression(ListSymbol, Integer0, Integer0)),
70+
)
71+
72+
svg = get_svg(expression)
73+
inner_svg = extract_svg_body(svg)
74+
75+
# Circles are implemented as ellipses with equal major and minor axes.
76+
# Check for that.
77+
print(inner_svg)
78+
matches = re.match(
79+
r'^<circle cx="(\S+)" cy="(\S+)" r="(\S+)" .*/>', inner_svg
80+
)
81+
assert matches
82+
assert matches.group(1) == matches.group(2)
83+
84+
85+
if __name__ == "__main__":
86+
test_svg_point()

0 commit comments

Comments
 (0)