Skip to content

Commit 38b6d63

Browse files
committed
SVG adjustment for faulty PointSize calculation
This should really be fixed in the the routine _prepare_elements and calc_dimensions, but this entagles us too much. Also this causes axes to extend below 0, but this is a more minor effect, I think.
1 parent 399d561 commit 38b6d63

1 file changed

Lines changed: 43 additions & 34 deletions

File tree

mathics/formatter/svg.py

Lines changed: 43 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,19 @@ def matrix(self, a, b, c, d, e, f):
3333
# a c e
3434
# b d f
3535
# 0 0 1
36-
self.transforms.append("matrix(%f, %f, %f, %f, %f, %f)" % (a, b, c, d, e, f))
36+
self.transforms.append(f"matrix({a:f}, {b:f}, {c:f}, {d:f}, {e:f}, {e:f})")
3737

3838
def translate(self, x, y):
39-
self.transforms.append("translate(%f, %f)" % (x, y))
39+
self.transforms.append(f"translate({x:f}, {y:f})")
4040

4141
def scale(self, x, y):
42-
self.transforms.append("scale(%f, %f)" % (x, y))
42+
self.transforms.append(f"scale({x:f}, {y:f})")
4343

4444
def rotate(self, x):
45-
self.transforms.append("rotate(%f)" % x)
45+
self.transforms.append(f"rotate({x:f})")
4646

4747
def apply(self, svg):
48-
return '<g transform="%s">%s</g>' % (" ".join(self.transforms), svg)
48+
return f"""<g transform="{' '.join(self.transforms)}">{svg}</g>"""
4949

5050

5151
def create_css(
@@ -57,22 +57,22 @@ def create_css(
5757
css = []
5858
if edge_color is not None:
5959
color, stroke_opacity = edge_color.to_css()
60-
css.append("stroke: %s" % color)
61-
css.append("stroke-opacity: %s" % stroke_opacity)
60+
css.append(f"stroke: {color}")
61+
css.append(f"stroke-opacity: {stroke_opacity}")
6262
else:
6363
css.append("stroke: none")
6464
if stroke_width is not None:
65-
css.append("stroke-width: %fpx" % stroke_width)
65+
css.append(f"stroke-width: {stroke_width:f}px")
6666
if face_color is not None:
6767
color, fill_opacity = face_color.to_css()
68-
css.append("fill: %s" % color)
69-
css.append("fill-opacity: %s" % fill_opacity)
68+
css.append(f"fill: {color}")
69+
css.append(f"fill-opacity: {fill_opacity}")
7070
else:
7171
css.append("fill: none")
7272
if font_color is not None:
7373
color, _ = font_color.to_css()
74-
css.append("color: %s" % color)
75-
css.append("opacity: %s" % opacity)
74+
css.append(f"color: {color}")
75+
css.append(f"opacity: {opacity}")
7676
return "; ".join(css)
7777

7878

@@ -86,7 +86,7 @@ def arrow_box(self, **options):
8686
def polygon(points):
8787
yield '<polygon points="'
8888
yield " ".join("%f,%f" % xy for xy in points)
89-
yield '" style="%s" />' % arrow_style
89+
yield f'" style="{arrow_style}" />'
9090

9191
extent = self.graphics.view_width or 0
9292
default_arrow = self._default_arrow(polygon)
@@ -104,7 +104,7 @@ def beziercurvebox(self, **options):
104104
svg = ""
105105
for line in self.lines:
106106
s = " ".join(_svg_bezier((self.spline_degree, [xy.pos() for xy in line])))
107-
svg += '<path d="%s" style="%s"/>' % (s, style)
107+
svg += f'<path d="{s}" style="{style}"/>'
108108
# print("XXX bezier", svg)
109109
return svg
110110

@@ -164,24 +164,31 @@ def graphics_box(self, leaves=None, **options) -> str:
164164
svg_body,
165165
)
166166

167-
xmin -= 1
168-
ymin -= 1
169-
w += 2
170-
h += 2
167+
168+
# FIXME:
169+
# Length calculation with PointBox is off by PointSize
170+
# point_size, _ = self.style.get_style(PointSize, face_element=False)
171+
# For others, I guess we just have this extra margin around the edge.
172+
point_size = 14.06 # Really 14.05333..5
173+
xmin -= point_size
174+
ymin -= point_size
175+
w += 2 * point_size
176+
h += 2 * point_size
171177

172178
if options.get("noheader", False):
173179
return svg_body
174180
svg_main = """
175-
<svg xmlns:svg="http://www.w3.org/2000/svg"
181+
<svg xmlns:svg="http://www.w3.org/2000/svg"
176182
xmlns="http://www.w3.org/2000/svg"
177183
version="1.1"
178184
viewBox="%s">
179185
%s
180-
</svg>
181-
""" % (
186+
</svg>
187+
""" % (
182188
" ".join("%f" % t for t in (xmin, ymin, w, h)),
183189
svg_body,
184190
)
191+
# print("svg_main", svg_main)
185192
return svg_main # , width, height
186193

187194

@@ -200,7 +207,9 @@ def graphics_elements(self, **options)->str:
200207
else:
201208
result.append(format_fn(element, **options))
202209

203-
return "\n".join(result)
210+
svg = "\n".join(result)
211+
# print("graphics_elements", svg)
212+
return svg
204213

205214

206215
add_conversion_fn(GraphicsElements, graphics_elements)
@@ -256,7 +265,7 @@ def line_box(self, **options)->str:
256265
" ".join(["%f,%f" % coords.pos() for coords in line]),
257266
style,
258267
)
259-
# print("XXX linebox", svg)
268+
# print("LineBox", svg)
260269
return svg
261270

262271

@@ -275,13 +284,10 @@ def pointbox(self, **options)->str:
275284
svg = ""
276285
for line in self.lines:
277286
for coords in line:
278-
svg += '<circle cx="%f" cy="%f" r="%f" style="%s" />' % (
279-
coords.pos()[0],
280-
coords.pos()[1],
281-
size,
282-
style,
283-
)
284-
# print("XXX PointBox", svg)
287+
svg += f"""
288+
<circle cx="{coords.pos()[0]:f}" cy="{coords.pos()[1]:f}"
289+
r="{size:f}" style="{style}"/>"""
290+
# print("PointBox", svg)
285291
return svg
286292

287293

@@ -312,7 +318,7 @@ def polygonbox(self, **options):
312318
" ".join("%f,%f" % coords.pos() for coords in line),
313319
style,
314320
)
315-
# print("XXX PolygonBox", svg)
321+
print("XXX PolygonBox", svg)
316322
return svg
317323

318324

@@ -331,14 +337,15 @@ def rectanglebox(self, **options):
331337
x1, x2 = x1 + offset[0], x2 + offset[0]
332338
y1, y2 = y1 + offset[1], y2 + offset[1]
333339
style = create_css(self.edge_color, self.face_color, line_width)
334-
return '<rect x="%f" y="%f" width="%f" height="%f" style="%s" />' % (
340+
svg = '<rect x="%f" y="%f" width="%f" height="%f" style="%s" />' % (
335341
xmin,
336342
ymin,
337343
w,
338344
h,
339345
style,
340346
)
341-
"\n".join(element.to_svg() for element in self.elements)
347+
# print("RectangleBox", svg)
348+
return svg
342349

343350

344351
add_conversion_fn(RectangleBox)
@@ -351,13 +358,15 @@ def _roundbox(self, **options):
351358
ry = y - ry
352359
line_width = self.style.get_line_width(face_element=self.face_element)
353360
style = create_css(self.edge_color, self.face_color, stroke_width=line_width)
354-
return '<ellipse cx="%f" cy="%f" rx="%f" ry="%f" style="%s" />' % (
361+
svg = '<ellipse cx="%f" cy="%f" rx="%f" ry="%f" style="%s" />' % (
355362
x,
356363
y,
357364
rx,
358365
ry,
359366
style,
360367
)
368+
# print("_RoundBox", svg)
369+
return svg
361370

362371

363372
add_conversion_fn(_RoundBox)

0 commit comments

Comments
 (0)