|
| 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