Skip to content

Commit 95f5575

Browse files
committed
Merge pull request #1385 from mathics/svg-linting
Small lint-like things Move _SVGTransform out of graphics
2 parents 9ecf464 + d70ac47 commit 95f5575

File tree

2 files changed

+36
-96
lines changed

2 files changed

+36
-96
lines changed

mathics/builtin/graphics.py

Lines changed: 0 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -335,29 +335,6 @@ def _extract_graphics(graphics, format, evaluation):
335335
return xmin, xmax, ymin, ymax, ox, oy, ex, ey, code
336336

337337

338-
class _SVGTransform:
339-
def __init__(self):
340-
self.transforms = []
341-
342-
def matrix(self, a, b, c, d, e, f):
343-
# a c e
344-
# b d f
345-
# 0 0 1
346-
self.transforms.append("matrix(%f, %f, %f, %f, %f, %f)" % (a, b, c, d, e, f))
347-
348-
def translate(self, x, y):
349-
self.transforms.append("translate(%f, %f)" % (x, y))
350-
351-
def scale(self, x, y):
352-
self.transforms.append("scale(%f, %f)" % (x, y))
353-
354-
def rotate(self, x):
355-
self.transforms.append("rotate(%f)" % x)
356-
357-
def apply(self, svg):
358-
return '<g transform="%s">%s</g>' % (" ".join(self.transforms), svg)
359-
360-
361338
class _ASYTransform:
362339
_template = """
363340
add(%s * (new picture() {
@@ -416,7 +393,6 @@ def apply(self, graphics, evaluation, options):
416393
new_leaves = []
417394
options_set = set(options.keys())
418395
for leaf in graphics.leaves:
419-
new_leaf = leaf
420396
leaf_name = leaf.get_head_name()
421397
if leaf_name == "System`Rule" and str(leaf.leaves[0]) in options_set:
422398
continue
@@ -1366,64 +1342,6 @@ def _arc_params(self):
13661342

13671343
return x, y, abs(rx), abs(ry), sx, sy, ex, ey, large_arc
13681344

1369-
# FIXME: Why do we need this? If so,
1370-
# figure out how to put in svg.py
1371-
# --------------------------------
1372-
# def to_svg(self, offset=None):
1373-
# if self.arc is None:
1374-
# return super(_ArcBox, self).to_svg(offset)
1375-
1376-
# x, y, rx, ry, sx, sy, ex, ey, large_arc = self._arc_params()
1377-
1378-
# format_fn = lookup_method(self, "svg")
1379-
# if format_fn is not None:
1380-
# return format_fn(self, offset)
1381-
# def path(closed):
1382-
# if closed:
1383-
# yield "M %f,%f" % (x, y)
1384-
# yield "L %f,%f" % (sx, sy)
1385-
# else:
1386-
# yield "M %f,%f" % (sx, sy)
1387-
1388-
# yield "A %f,%f,0,%d,0,%f,%f" % (rx, ry, large_arc, ex, ey)
1389-
1390-
# if closed:
1391-
# yield "Z"
1392-
1393-
# l = self.style.get_line_width(face_element=self.face_element)
1394-
# style = create_css(self.edge_color, self.face_color, stroke_width=l)
1395-
# return '<path d="%s" style="%s" />' % (" ".join(path(self.face_element)), style)
1396-
1397-
def to_asy(self):
1398-
if self.arc is None:
1399-
return super(_ArcBox, self).to_asy()
1400-
1401-
x, y, rx, ry, sx, sy, ex, ey, large_arc = self._arc_params()
1402-
1403-
def path(closed):
1404-
if closed:
1405-
yield "(%s,%s)--(%s,%s)--" % tuple(
1406-
asy_number(t) for t in (x, y, sx, sy)
1407-
)
1408-
1409-
yield "arc((%s,%s), (%s, %s), (%s, %s))" % tuple(
1410-
asy_number(t) for t in (x, y, sx, sy, ex, ey)
1411-
)
1412-
1413-
if closed:
1414-
yield "--cycle"
1415-
1416-
l = self.style.get_line_width(face_element=self.face_element)
1417-
pen = create_pens(
1418-
edge_color=self.edge_color,
1419-
face_color=self.face_color,
1420-
stroke_width=l,
1421-
is_face_element=self.face_element,
1422-
)
1423-
command = "filldraw" if self.face_element else "draw"
1424-
return "%s(%s, %s);" % (command, "".join(path(self.face_element)), pen)
1425-
1426-
14271345
class DiskBox(_ArcBox):
14281346
face_element = True
14291347

mathics/formatter/svg.py

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,35 @@
2020
RectangleBox,
2121
_RoundBox,
2222
_svg_bezier,
23-
_SVGTransform,
2423
)
2524

2625
from mathics.core.formatter import lookup_method, add_conversion_fn
2726

2827

28+
class _SVGTransform:
29+
def __init__(self):
30+
from trepan.api import debug; debug()
31+
self.transforms = []
32+
33+
def matrix(self, a, b, c, d, e, f):
34+
# a c e
35+
# b d f
36+
# 0 0 1
37+
self.transforms.append("matrix(%f, %f, %f, %f, %f, %f)" % (a, b, c, d, e, f))
38+
39+
def translate(self, x, y):
40+
self.transforms.append("translate(%f, %f)" % (x, y))
41+
42+
def scale(self, x, y):
43+
self.transforms.append("scale(%f, %f)" % (x, y))
44+
45+
def rotate(self, x):
46+
self.transforms.append("rotate(%f)" % x)
47+
48+
def apply(self, svg):
49+
return '<g transform="%s">%s</g>' % (" ".join(self.transforms), svg)
50+
51+
2952
def create_css(
3053
edge_color=None, face_color=None, stroke_width=None, font_color=None, opacity=1.0
3154
) -> str:
@@ -76,8 +99,8 @@ def polygon(points):
7699

77100

78101
def beziercurvebox(self, offset=None):
79-
l = self.style.get_line_width(face_element=False)
80-
style = create_css(edge_color=self.edge_color, stroke_width=l)
102+
line_width = self.style.get_line_width(face_element=False)
103+
style = create_css(edge_color=self.edge_color, stroke_width=line_width)
81104

82105
svg = ""
83106
for line in self.lines:
@@ -91,9 +114,9 @@ def beziercurvebox(self, offset=None):
91114

92115

93116
def filledcurvebox(self, offset=None):
94-
l = self.style.get_line_width(face_element=False)
117+
line_width = self.style.get_line_width(face_element=False)
95118
style = create_css(
96-
edge_color=self.edge_color, face_color=self.face_color, stroke_width=l
119+
edge_color=self.edge_color, face_color=self.face_color, stroke_width=line_width
97120
)
98121

99122
def components():
@@ -110,7 +133,6 @@ def components():
110133

111134
add_conversion_fn(FilledCurveBox)
112135

113-
# FIXME figure out how we can add this.
114136
def graphicsbox(self, leaves=None, **options) -> str:
115137
if not leaves:
116138
leaves = self._leaves
@@ -220,8 +242,8 @@ def insetbox(self, offset=None):
220242

221243

222244
def linebox(self, offset=None):
223-
l = self.style.get_line_width(face_element=False)
224-
style = create_css(edge_color=self.edge_color, stroke_width=l)
245+
line_width = self.style.get_line_width(face_element=False)
246+
style = create_css(edge_color=self.edge_color, stroke_width=line_width)
225247
svg = ""
226248
for line in self.lines:
227249
svg += '<polyline points="%s" style="%s" />' % (
@@ -261,13 +283,13 @@ def pointbox(self, offset=None):
261283

262284

263285
def polygonbox(self, offset=None):
264-
l = self.style.get_line_width(face_element=True)
286+
line_width = self.style.get_line_width(face_element=True)
265287
if self.vertex_colors is None:
266288
face_color = self.face_color
267289
else:
268290
face_color = None
269291
style = create_css(
270-
edge_color=self.edge_color, face_color=face_color, stroke_width=l
292+
edge_color=self.edge_color, face_color=face_color, stroke_width=line_width
271293
)
272294
svg = ""
273295
if self.vertex_colors is not None:
@@ -292,7 +314,7 @@ def polygonbox(self, offset=None):
292314

293315

294316
def rectanglebox(self, offset=None):
295-
l = self.style.get_line_width(face_element=True)
317+
line_width = self.style.get_line_width(face_element=True)
296318
x1, y1 = self.p1.pos()
297319
x2, y2 = self.p2.pos()
298320
xmin = min(x1, x2)
@@ -302,7 +324,7 @@ def rectanglebox(self, offset=None):
302324
if offset:
303325
x1, x2 = x1 + offset[0], x2 + offset[0]
304326
y1, y2 = y1 + offset[1], y2 + offset[1]
305-
style = create_css(self.edge_color, self.face_color, l)
327+
style = create_css(self.edge_color, self.face_color, line_width)
306328
return '<rect x="%f" y="%f" width="%f" height="%f" style="%s" />' % (
307329
xmin,
308330
ymin,
@@ -321,8 +343,8 @@ def _roundbox(self, offset=None):
321343
rx, ry = self.r.pos()
322344
rx -= x
323345
ry = y - ry
324-
l = self.style.get_line_width(face_element=self.face_element)
325-
style = create_css(self.edge_color, self.face_color, stroke_width=l)
346+
line_width = self.style.get_line_width(face_element=self.face_element)
347+
style = create_css(self.edge_color, self.face_color, stroke_width=line_width)
326348
return '<ellipse cx="%f" cy="%f" rx="%f" ry="%f" style="%s" />' % (
327349
x,
328350
y,

0 commit comments

Comments
 (0)