Skip to content

Commit

Permalink
fix(svg): improve border so that it does not rescale
Browse files Browse the repository at this point in the history
include the border in the image size and keep viewBox and w/h the same,
that way, no rescaling, reducing the quality, is needed.
  • Loading branch information
zsquareplusc committed Jun 1, 2017
1 parent 3f8e573 commit 45fea33
Showing 1 changed file with 27 additions and 26 deletions.
53 changes: 27 additions & 26 deletions aafigure/svg.py
Expand Up @@ -24,6 +24,7 @@ def __init__(self, options):
self.foreground = options['foreground']
self.background = options['background']
self.fillcolor = options['fill']
self.border = 3
self.indent = ''
# if front is given explicit, use it instead of textual/proportional flags
if 'font' in options:
Expand All @@ -38,42 +39,42 @@ def _num(self, number):
"""helper to scale numbers for svg output"""
return number * self.scale

def _coordinate(self, number):
"""helper to scale numbers for svg output"""
return self._num(number) + self.border

def get_size_attrs(self):
"""get image size as svg text"""
# this function is here beacuse of a hack. the rst2html converter
# has to know the size of the figure it inserts
return u'width="{}" height="{}"'.format(
self._num(self.width),
self._num(self.height))
self._num(self.width) + 2 * self.border,
self._num(self.height) + 2 * self.border)

def visit_image(self, aa_image, xml_header=True):
"""\
Process the given ASCIIArtFigure and output the shapes in
the SVG file
"""
self.aa_image = aa_image # save for later XXX not optimal to do it here
self.width = (aa_image.width + 1) * aa_image.nominal_size * aa_image.aspect_ratio
self.height = (aa_image.height + 1) * aa_image.nominal_size
self.width = aa_image.width * aa_image.nominal_size * aa_image.aspect_ratio
self.height = aa_image.height * aa_image.nominal_size
if xml_header:
self.file_like.write(
u'<?xml version="1.0" standalone="no"?>\n'
'<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" '
'"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">\n\n'
'<svg width="{w}" height="{h}" viewBox="-3 -3 {wb} {hb}" version="1.1" xmlns="http://www.w3.org/2000/svg" '
'<svg width="{w}" height="{h}" viewBox="0 0 {w} {h}" version="1.1" xmlns="http://www.w3.org/2000/svg" '
'xmlns:xlink="http://www.w3.org/1999/xlink">\n'
'<!-- automatically generated by aafigure -->'.format(
w=self._num(self.width),
h=self._num(self.height),
wb=self._num(self.width) + 3,
hb=self._num(self.height) + 3))
w=self._num(self.width) + 2 * self.border,
h=self._num(self.height) + 2 * self.border))
else:
self.file_like.write(
u'<svg width="{w}" height="{h}" viewBox="-3 -3 {wb} {hb}" version="1.1" '
u'<svg width="{w}" height="{h}" viewBox="0 0 {w} {h}" version="1.1" '
'xmlns="http://www.w3.org/2000/svg">\n'.format(
w=self._num(self.width),
h=self._num(self.height),
wb=self._num(self.width) + 3,
hb=self._num(self.height) + 3))
w=self._num(self.width) + 2 * self.border,
h=self._num(self.height) + 2 * self.border))
self.visit_shapes(aa_image.shapes)
self.file_like.write(u'</svg>\n')

Expand All @@ -93,10 +94,10 @@ def _line(self, x1, y1, x2, y2, thick):
u'{}<line x1="{}" y1="{}" x2="{}" y2="{}" stroke="{}" '
'stroke-width="{}" />\n'.format(
self.indent,
self._num(x1),
self._num(y1),
self._num(x2),
self._num(y2),
self._coordinate(x1),
self._coordinate(y1),
self._coordinate(x2),
self._coordinate(y2),
self.foreground,
self.line_width * (1 + bool(thick))))

Expand All @@ -113,7 +114,7 @@ def _rectangle(self, x1, y1, x2, y2, style=''):
u'{}<rect x="{}" y="{}" width="{}" height="{}" stroke="{}" '
'fill="{}" stroke-width="{}" style="{}" />'.format(
self.indent,
self._num(x1), self._num(y1),
self._coordinate(x1), self._coordinate(y1),
self._num(x2 - x1), self._num(y2 - y1),
self.fillcolor, # stroke:%s;
self.fillcolor,
Expand All @@ -127,7 +128,7 @@ def visit_point(self, point):
u'{}<circle cx="{}" cy="{}" r="{}" fill="{}" stroke="{}" '
'stroke-width="{}" />'.format(
self.indent,
self._num(point.x), self._num(point.y),
self._coordinate(point.x), self._coordinate(point.y),
self._num(0.2),
self.foreground, self.foreground,
self.line_width))
Expand All @@ -147,7 +148,7 @@ def visit_circle(self, circle):
u'{}<circle cx="{}" cy="{}" r="{}" stroke="{}" stroke-width="{}" '
'fill="{}" />'.format(
self.indent,
self._num(circle.center.x), self._num(circle.center.y),
self._coordinate(circle.center.x), self._coordinate(circle.center.y),
self._num(circle.radius),
self.foreground,
self.line_width,
Expand All @@ -159,7 +160,7 @@ def visit_label(self, label):
u'{}<text x="{}" y="{}" font-family="{}" font-size="{}" '
'fill="{}" >\n {}\n{}</text>\n'.format(
self.indent,
self._num(label.position.x), self._num(label.position.y - 0.3), # XXX static offset not good in all situations
self._coordinate(label.position.x), self._coordinate(label.position.y - 0.3), # XXX static offset not good in all situations
self.font,
self._num(self.aa_image.nominal_size),
self.foreground,
Expand All @@ -182,9 +183,9 @@ def visit_arc(self, arc):
u'{}<path d="M{},{} C{},{} {},{} {},{}" fill="none" '
'stroke="{}" stroke-width="{}" />'.format(
self.indent,
self._num(p1.x), self._num(p1.y),
self._num(c1.x), self._num(c1.y),
self._num(c2.x), self._num(c2.y),
self._num(p2.x), self._num(p2.y),
self._coordinate(p1.x), self._coordinate(p1.y),
self._coordinate(c1.x), self._coordinate(c1.y),
self._coordinate(c2.x), self._coordinate(c2.y),
self._coordinate(p2.x), self._coordinate(p2.y),
self.foreground,
self.line_width))

0 comments on commit 45fea33

Please sign in to comment.