SVGBuilder Module

build SVG files

class knittingpattern.convert.SVGBuilder.SVGBuilder[source]

Bases: object

This class builds an SVG to a file.

The class itself does not know what the objects look like. It offers a more convinient interface to build SVG files.

__init__()[source]

Initialize this object without arguments.

__weakref__

list of weak references to the object (if defined)

bounding_box

the bounding box of this SVG (min_x, min_y, max_x, max_y).

svg_builder10x10.bounding_box = (0, 0, 10, 10)
assert svg_builder10x10.bounding_box == (0, 0, 10, 10)

viewBox, width and height are computed from this.

If the bounding box was never set, the result is a tuple of four None.

get_svg_dict()[source]

Return the SVG structure generated.

insert_defs(defs)[source]

Adds the defs to the SVG structure.

Parameters:defs – a list of SVG dictionaries, which contain the defs, which should be added to the SVG structure.
place(x, y, svg, layer_id)[source]

Place the svg content at (x, y) position in the SVG, in a layer with the id layer_id.

Parameters:
  • x (float) – the x position of the svg
  • y (float) – the y position of the svg
  • svg (str) – the SVG to place at (x, y)
  • layer_id (str) – the id of the layer that this svg should be placed inside
place_svg_dict(x, y, svg_dict, layer_id, group=None)[source]

Same as place() but with a dictionary as svg_dict.

Parameters:
  • svg_dict (dict) – a dictionary returned by xmltodict.parse()
  • group (dict) – a dictionary of values to add to the group the svg_dict will be added to or None if nothing should be added
place_svg_use(symbol_id, layer_id, group=None)[source]

Same as place_svg_use_coords().

With implicit x and y which are set to 0 in this method and then place_svg_use_coords() is called.

place_svg_use_coords(x, y, symbol_id, layer_id, group=None)[source]

Similar to place() but with an id as symbol_id.

Parameters:
  • symbol_id (str) – an id which identifies an svg object defined in the defs
  • group (dict) – a dictionary of values to add to the group the use statement will be added to or None if nothing should be added
write_to_file(file)[source]

Writes the current SVG to the file.

Parameters:file – a file-like object
knittingpattern.convert.SVGBuilder.SVG_FILE = '\n<svg\n xmlns:ns="http://PURL.org/dc/elements/1.1/"\n xmlns:dc="http://purl.org/dc/elements/1.1/"\n xmlns:cc="http://creativecommons.org/ns#"\n xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"\n xmlns:svg="http://www.w3.org/2000/svg"\n xmlns="http://www.w3.org/2000/svg"\n xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"\n xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"\n xmlns:xlink="http://www.w3.org/1999/xlink">\n <title>knittingpattern</title>\n <defs></defs>\n</svg>\n'

an empty svg file as a basis