now parameter supports units will specify if not given

pull/75/head
Andy Port 2018-10-13 21:40:35 -07:00
parent 95179a6bfa
commit aa03a4aecb
1 changed files with 30 additions and 22 deletions

View File

@ -9,6 +9,7 @@ from xml.dom.minidom import parse as md_xml_parse
from svgwrite import Drawing, text as txt from svgwrite import Drawing, text as txt
from time import time from time import time
from warnings import warn from warnings import warn
import re
# Internal dependencies # Internal dependencies
from .path import Path, Line, is_path_segment from .path import Path, Line, is_path_segment
@ -140,14 +141,19 @@ def disvg(paths=None, colors=None,
:param mindim - The minimum dimension (height or width) of the output :param mindim - The minimum dimension (height or width) of the output
SVG (default is 600). SVG (default is 600).
:param dimensions - The (x,y) display dimensions of the output SVG. :param dimensions - The (x,y) display dimensions of the output SVG.
Using this will override the `mindim` parameter. I.e. this specifies the `width` and `height` SVG attributes. Note that
these also can be used to specify units other than pixels. Using this
will override the `mindim` parameter.
:param viewbox - This specifies what rectangular patch of R^2 will be :param viewbox - This specifies the coordinated system used in the svg.
viewable through the outputSVG. It should be input as a 4-tuple, The SVG `viewBox` attribute works together with the the `height` and
(min_x, min_y, width, height), or a string "min_x min_y width height". `width` attrinutes. Using these three attributes allows for shifting
This is different from the display dimension of the svg, which can be and scaling of the SVG canvas without changing the any values other
set through mindim or dimensions. than those in `viewBox`, `height`, and `width`. `viewbox` should be
input as a 4-tuple, (min_x, min_y, width, height), or a string
"min_x min_y width height". Using this will override the `mindim`
parameter.
:param attributes - a list of dictionaries of attributes for the input :param attributes - a list of dictionaries of attributes for the input
paths. Note: This will override any other conflicting settings. paths. Note: This will override any other conflicting settings.
@ -233,9 +239,12 @@ def disvg(paths=None, colors=None,
if not isinstance(viewbox, str): if not isinstance(viewbox, str):
viewbox = '%s %s %s %s' % viewbox viewbox = '%s %s %s %s' % viewbox
if dimensions is None: if dimensions is None:
szx, szy = viewbox.split(' ')[2:4] dimensions = viewbox.split(' ')[2:4]
else: elif dimensions:
szx, szy = dimensions dimensions = tuple(map(str, dimensions))
def strip_units(s):
return re.search('\d?\.?\d?', s).group()
viewbox = '0 0 %s %s' % tuple(map(strip_units, dimensions))
else: else:
if paths: if paths:
stuff2bound += paths stuff2bound += paths
@ -282,25 +291,24 @@ def disvg(paths=None, colors=None,
dx += 2*margin_size*dx + extra_space_for_style dx += 2*margin_size*dx + extra_space_for_style
dy += 2*margin_size*dy + extra_space_for_style dy += 2*margin_size*dy + extra_space_for_style
viewbox = "%s %s %s %s" % (xmin, ymin, dx, dy) viewbox = "%s %s %s %s" % (xmin, ymin, dx, dy)
if dimensions:
szx, szy = dimensions if dx > dy:
szx = str(mindim) + 'px'
szy = str(int(ceil(mindim * dy / dx))) + 'px'
else: else:
if dx > dy: szx = str(int(ceil(mindim * dx / dy))) + 'px'
szx = str(mindim) + 'px' szy = str(mindim) + 'px'
szy = str(int(ceil(mindim * dy / dx))) + 'px' dimensions = szx, szy
else:
szx = str(int(ceil(mindim * dx / dy))) + 'px'
szy = str(mindim) + 'px'
# Create an SVG file # Create an SVG file
if svg_attributes is not None: if svg_attributes is not None:
szx = svg_attributes.get("width", szx) dimensions[0] = svg_attributes.get("width", dimensions[0])
szy = svg_attributes.get("height", szy) dimensions[1] = svg_attributes.get("height", dimensions[1])
debug = svg_attributes.get("debug", svgwrite_debug) debug = svg_attributes.get("debug", svgwrite_debug)
dwg = Drawing(filename=filename, size=(szx, szy), debug=debug, dwg = Drawing(filename=filename, size=dimensions, debug=debug,
**svg_attributes) **svg_attributes)
else: else:
dwg = Drawing(filename=filename, size=(szx, szy), debug=svgwrite_debug, dwg = Drawing(filename=filename, size=dimensions, debug=svgwrite_debug,
viewBox=viewbox) viewBox=viewbox)
# add paths # add paths