aesthetic cleanup

vectorize-path-point
Andrew Port 2020-12-01 20:00:59 -08:00
parent 0f4c9c598a
commit 5aeb6e3bf7
1 changed files with 40 additions and 21 deletions

View File

@ -1,5 +1,7 @@
"""This submodule contains tools for creating svg files from paths and path """This submodule: basic tools for creating svg files from path data.
segments."""
See also the document.py submodule.
"""
# External dependencies: # External dependencies:
from __future__ import division, absolute_import, print_function from __future__ import division, absolute_import, print_function
@ -16,7 +18,7 @@ import re
from .path import Path, Line, is_path_segment from .path import Path, Line, is_path_segment
from .misctools import open_in_browser from .misctools import open_in_browser
# Used to convert a string colors (identified by single chars) to a list. # color shorthand for inputting color list as string of chars.
color_dict = {'a': 'aqua', color_dict = {'a': 'aqua',
'b': 'blue', 'b': 'blue',
'c': 'cyan', 'c': 'cyan',
@ -59,8 +61,16 @@ def is3tuple(c):
def big_bounding_box(paths_n_stuff): def big_bounding_box(paths_n_stuff):
"""Finds a BB containing a collection of paths, Bezier path segments, and """returns minimal upright bounding box.
points (given as complex numbers)."""
Args:
paths_n_stuff: iterable of Paths, Bezier path segments, and
points (given as complex numbers).
Returns:
extrema of bounding box, (xmin, xmax, ymin, ymax)
"""
bbs = [] bbs = []
for thing in paths_n_stuff: for thing in paths_n_stuff:
if is_path_segment(thing) or isinstance(thing, Path): if is_path_segment(thing) or isinstance(thing, Path):
@ -73,9 +83,9 @@ def big_bounding_box(paths_n_stuff):
bbs.append((complexthing.real, complexthing.real, bbs.append((complexthing.real, complexthing.real,
complexthing.imag, complexthing.imag)) complexthing.imag, complexthing.imag))
except ValueError: except ValueError:
raise TypeError( raise TypeError("paths_n_stuff can only contains Path, "
"paths_n_stuff can only contains Path, CubicBezier, " "CubicBezier, QuadraticBezier, Line, "
"QuadraticBezier, Line, and complex objects.") "and complex objects.")
xmins, xmaxs, ymins, ymaxs = list(zip(*bbs)) xmins, xmaxs, ymins, ymaxs = list(zip(*bbs))
xmin = min(xmins) xmin = min(xmins)
xmax = max(xmaxs) xmax = max(xmaxs)
@ -91,7 +101,8 @@ def disvg(paths=None, colors=None, filename=None, stroke_widths=None,
text_path=None, font_size=None, attributes=None, text_path=None, font_size=None, attributes=None,
svg_attributes=None, svgwrite_debug=False, svg_attributes=None, svgwrite_debug=False,
paths2Drawing=False): paths2Drawing=False):
"""Takes in a list of paths and creates an SVG file containing said paths. """Creates (and optionally displays) an SVG file.
REQUIRED INPUTS: REQUIRED INPUTS:
:param paths - a list of paths :param paths - a list of paths
@ -188,6 +199,9 @@ def disvg(paths=None, colors=None, filename=None, stroke_widths=None,
svgviewer/browser will likely fail to load some of the SVGs in time. svgviewer/browser will likely fail to load some of the SVGs in time.
To fix this, use the timestamp attribute, or give the files unique To fix this, use the timestamp attribute, or give the files unique
names, or use a pause command (e.g. time.sleep(1)) between uses. names, or use a pause command (e.g. time.sleep(1)) between uses.
SEE ALSO:
* document.py
""" """
_default_relative_node_radius = 5e-3 _default_relative_node_radius = 5e-3
@ -412,12 +426,13 @@ def disvg(paths=None, colors=None, filename=None, stroke_widths=None,
print(filename) print(filename)
def wsvg(paths=None, colors=None, filename=None, def wsvg(paths=None, colors=None, filename=None, stroke_widths=None,
stroke_widths=None, nodes=None, node_colors=None, node_radii=None, nodes=None, node_colors=None, node_radii=None,
openinbrowser=False, timestamp=False, openinbrowser=False, timestamp=False, margin_size=0.1,
margin_size=0.1, mindim=600, dimensions=None, mindim=600, dimensions=None, viewbox=None, text=None,
viewbox=None, text=None, text_path=None, font_size=None, text_path=None, font_size=None, attributes=None,
attributes=None, svg_attributes=None, svgwrite_debug=False, paths2Drawing=False): svg_attributes=None, svgwrite_debug=False,
paths2Drawing=False):
"""Create SVG and write to disk. """Create SVG and write to disk.
Note: This is identical to `disvg()` except that `openinbrowser` Note: This is identical to `disvg()` except that `openinbrowser`
@ -431,10 +446,12 @@ def wsvg(paths=None, colors=None, filename=None,
stroke_widths=stroke_widths, nodes=nodes, stroke_widths=stroke_widths, nodes=nodes,
node_colors=node_colors, node_radii=node_radii, node_colors=node_colors, node_radii=node_radii,
openinbrowser=openinbrowser, timestamp=timestamp, openinbrowser=openinbrowser, timestamp=timestamp,
margin_size=margin_size, mindim=mindim, dimensions=dimensions, margin_size=margin_size, mindim=mindim,
viewbox=viewbox, text=text, text_path=text_path, font_size=font_size, dimensions=dimensions, viewbox=viewbox, text=text,
text_path=text_path, font_size=font_size,
attributes=attributes, svg_attributes=svg_attributes, attributes=attributes, svg_attributes=svg_attributes,
svgwrite_debug=svgwrite_debug, paths2Drawing=paths2Drawing) svgwrite_debug=svgwrite_debug,
paths2Drawing=paths2Drawing)
def paths2Drawing(paths=None, colors=None, filename=None, def paths2Drawing(paths=None, colors=None, filename=None,
@ -456,7 +473,9 @@ def paths2Drawing(paths=None, colors=None, filename=None,
stroke_widths=stroke_widths, nodes=nodes, stroke_widths=stroke_widths, nodes=nodes,
node_colors=node_colors, node_radii=node_radii, node_colors=node_colors, node_radii=node_radii,
openinbrowser=openinbrowser, timestamp=timestamp, openinbrowser=openinbrowser, timestamp=timestamp,
margin_size=margin_size, mindim=mindim, dimensions=dimensions, margin_size=margin_size, mindim=mindim,
viewbox=viewbox, text=text, text_path=text_path, font_size=font_size, dimensions=dimensions, viewbox=viewbox, text=text,
text_path=text_path, font_size=font_size,
attributes=attributes, svg_attributes=svg_attributes, attributes=attributes, svg_attributes=svg_attributes,
svgwrite_debug=svgwrite_debug, paths2Drawing=paths2Drawing) svgwrite_debug=svgwrite_debug,
paths2Drawing=paths2Drawing)