Skip to content

Commit 346b19f

Browse files
committed
Relocate one more SVG to formatter space
1 parent 33fc83a commit 346b19f

2 files changed

Lines changed: 59 additions & 110 deletions

File tree

mathics/builtin/graphics.py

Lines changed: 4 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -3100,60 +3100,6 @@ def boxes_to_tex(self, leaves=None, **options):
31003100

31013101
return tex
31023102

3103-
# FIXME: figure out how to move this to the SVG formatter
3104-
def to_svg(self, leaves=None, **options):
3105-
if not leaves:
3106-
leaves = self._leaves
3107-
3108-
data = options.get("data", None)
3109-
if data:
3110-
elements, xmin, xmax, ymin, ymax, w, h, width, height = data
3111-
else:
3112-
elements, calc_dimensions = self._prepare_elements(
3113-
leaves, options, neg_y=True
3114-
)
3115-
xmin, xmax, ymin, ymax, w, h, width, height = calc_dimensions()
3116-
3117-
elements.view_width = w
3118-
3119-
format_fn = lookup_method(elements, "svg")
3120-
if format_fn is not None:
3121-
svg_body = format_fn(elements, offset=options.get("offset", None))
3122-
else:
3123-
svg_body = elements.to_svg(offset=options.get("offset", None))
3124-
3125-
if self.background_color is not None:
3126-
# Wrap svg_elements in a rectangle
3127-
svg_body = '<rect x="%f" y="%f" width="%f" height="%f" style="fill:%s"/>%s' % (
3128-
xmin,
3129-
ymin,
3130-
w,
3131-
h,
3132-
self.background_color.to_css()[0],
3133-
svg_body,
3134-
)
3135-
3136-
xmin -= 1
3137-
ymin -= 1
3138-
w += 2
3139-
h += 2
3140-
3141-
if options.get("noheader", False):
3142-
return svg_body
3143-
svg_main = """
3144-
<svg xmlns:svg="http://www.w3.org/2000/svg"
3145-
xmlns="http://www.w3.org/2000/svg"
3146-
version="1.1"
3147-
viewBox="%s">
3148-
%s
3149-
</svg>
3150-
""" % (
3151-
" ".join("%f" % t for t in (xmin, ymin, w, h)),
3152-
svg_body,
3153-
)
3154-
return svg_main # , width, height
3155-
3156-
# fixme: figure out how to move the svg-specific portions to the SVG formatter.
31573103
def boxes_to_mathml(self, leaves=None, **options) -> str:
31583104
if not leaves:
31593105
leaves = self._leaves
@@ -3162,7 +3108,10 @@ def boxes_to_mathml(self, leaves=None, **options) -> str:
31623108
xmin, xmax, ymin, ymax, w, h, width, height = calc_dimensions()
31633109
data = (elements, xmin, xmax, ymin, ymax, w, h, width, height)
31643110

3165-
svg_main = self.to_svg(leaves, data=data, **options)
3111+
# FIXME: SVG is the only thing we can convert MathML into.
3112+
# Handle other graphics formats.
3113+
format_fn = lookup_method(self, "svg")
3114+
svg_main = format_fn(self, leaves, data=data, **options)
31663115

31673116
# mglyph, which is what we have been using, is bad because MathML standard changed.
31683117
# metext does not work because the way in which we produce the svg images is also based on this outdated mglyph behaviour.

mathics/formatter/svg.py

Lines changed: 55 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
ArrowBox,
1111
BezierCurveBox,
1212
FilledCurveBox,
13-
# GraphicsBox,
13+
GraphicsBox,
1414
GraphicsElements,
1515
InsetBox,
1616
LineBox,
@@ -111,60 +111,60 @@ def components():
111111
add_conversion_fn(FilledCurveBox)
112112

113113
# FIXME figure out how we can add this.
114-
# def graphicsbox(self, leaves=None, **options) -> str:
115-
# if not leaves:
116-
# leaves = self._leaves
117-
#
118-
# data = options.get("data", None)
119-
# if data:
120-
# elements, xmin, xmax, ymin, ymax, w, h, width, height = data
121-
# else:
122-
# elements, calc_dimensions = self._prepare_elements(
123-
# leaves, options, neg_y=True
124-
# )
125-
# xmin, xmax, ymin, ymax, w, h, width, height = calc_dimensions()
126-
#
127-
# elements.view_width = w
128-
#
129-
# format_fn = lookup_method(elements, "svg")
130-
# if format_fn is not None:
131-
# svg_body = format_fn(elements, offset=options.get("offset", None))
132-
# else:
133-
# svg_body = elements.to_svg(offset=options.get("offset", None))
134-
#
135-
# if self.background_color is not None:
136-
# # Wrap svg_elements in a rectangle
137-
# svg_body = '<rect x="%f" y="%f" width="%f" height="%f" style="fill:%s"/>%s' % (
138-
# xmin,
139-
# ymin,
140-
# w,
141-
# h,
142-
# self.background_color.to_css()[0],
143-
# svg_body,
144-
# )
145-
#
146-
# xmin -= 1
147-
# ymin -= 1
148-
# w += 2
149-
# h += 2
150-
#
151-
# if options.get("noheader", False):
152-
# return svg_body
153-
# svg_main = """
154-
# <svg xmlns:svg="http://www.w3.org/2000/svg"
155-
# xmlns="http://www.w3.org/2000/svg"
156-
# version="1.1"
157-
# viewBox="%s">
158-
# %s
159-
# </svg>
160-
# """ % (
161-
# " ".join("%f" % t for t in (xmin, ymin, w, h)),
162-
# svg_body,
163-
# )
164-
# return svg_main # , width, height
165-
#
166-
#
167-
# add_conversion_fn(GraphicsBox)
114+
def graphicsbox(self, leaves=None, **options) -> str:
115+
if not leaves:
116+
leaves = self._leaves
117+
118+
data = options.get("data", None)
119+
if data:
120+
elements, xmin, xmax, ymin, ymax, w, h, width, height = data
121+
else:
122+
elements, calc_dimensions = self._prepare_elements(
123+
leaves, options, neg_y=True
124+
)
125+
xmin, xmax, ymin, ymax, w, h, width, height = calc_dimensions()
126+
127+
elements.view_width = w
128+
129+
format_fn = lookup_method(elements, "svg")
130+
if format_fn is not None:
131+
svg_body = format_fn(elements, offset=options.get("offset", None))
132+
else:
133+
svg_body = elements.to_svg(offset=options.get("offset", None))
134+
135+
if self.background_color is not None:
136+
# Wrap svg_elements in a rectangle
137+
svg_body = '<rect x="%f" y="%f" width="%f" height="%f" style="fill:%s"/>%s' % (
138+
xmin,
139+
ymin,
140+
w,
141+
h,
142+
self.background_color.to_css()[0],
143+
svg_body,
144+
)
145+
146+
xmin -= 1
147+
ymin -= 1
148+
w += 2
149+
h += 2
150+
151+
if options.get("noheader", False):
152+
return svg_body
153+
svg_main = """
154+
<svg xmlns:svg="http://www.w3.org/2000/svg"
155+
xmlns="http://www.w3.org/2000/svg"
156+
version="1.1"
157+
viewBox="%s">
158+
%s
159+
</svg>
160+
""" % (
161+
" ".join("%f" % t for t in (xmin, ymin, w, h)),
162+
svg_body,
163+
)
164+
return svg_main # , width, height
165+
166+
167+
add_conversion_fn(GraphicsBox)
168168

169169

170170
def graphicselements(self, offset=None):

0 commit comments

Comments
 (0)