geomotif.io.svg
¶
Write a design as SVG, in pure standard library.
SVG is the format that goes everywhere from here: a browser, Illustrator or Inkscape, a laser cutter, a pen plotter's toolchain, and the gallery in these docs. Nothing is imported that is not already installed.
Two things about the output are worth knowing up front.
Y points down. SVG's origin is the top-left corner and y grows downward,
which is the opposite of the convention every motif here is written in. So
flip_y defaults to true and a design comes out the way you drew it. Turn it
off only if you are feeding the result to something that shares SVG's axes
already.
The coordinates are transformed, not the canvas. The design is fitted into
the canvas before anything is written, rather than being scaled by a
viewBox. That way stroke_width means the same thing whatever the design
measured -- one unit of the file you are looking at -- and rounding coordinates
to precision actually shrinks the file rather than throwing away detail
that a later scale would have magnified.
A design carrying styles (:mod:geomotif.core.style) writes its layers as the
labeled groups Inkscape and vpype read, and its colors as attributes on
the individual elements. A design carrying none writes exactly the file it
always did.
Functions:
| Name | Description |
|---|---|
to_svg |
Render a design as an SVG document. |
save_svg |
Write a design to an SVG file and return the path written. |
to_svg
¶
to_svg(design: Design, *, width: float | None = None, height: float | None = None, padding: float = 8.0, stroke: str = '#0b0b0b', stroke_width: float = 1.0, fill: str = 'none', background: str | None = None, dot_radius: float | None = None, flip_y: bool = True, precision: int = 3, group_by_path: bool = True, title: str | None = None, units: str = '') -> str
Render a design as an SVG document.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
design
|
Design
|
What to draw. Its strokes become |
required |
width
|
float
|
Canvas size in user units. Give both to fit the design into exactly
that rectangle; give one and the other follows from the design's own
proportions; give neither and the design keeps its own measurements,
with |
None
|
height
|
float
|
Canvas size in user units. Give both to fit the design into exactly
that rectangle; give one and the other follows from the design's own
proportions; give neither and the design keeps its own measurements,
with |
None
|
padding
|
float
|
Margin reserved on all four sides. |
8.0
|
stroke
|
(str, float, str)
|
Applied to the group holding the strokes. |
'#0b0b0b'
|
stroke_width
|
(str, float, str)
|
Applied to the group holding the strokes. |
'#0b0b0b'
|
fill
|
(str, float, str)
|
Applied to the group holding the strokes. |
'#0b0b0b'
|
background
|
str
|
Draw a filled rectangle behind everything. Omitted by default, which leaves the canvas transparent. |
None
|
dot_radius
|
float
|
Radius for the loose points. Defaults to |
None
|
flip_y
|
bool
|
Mirror vertically, so a design drawn y-up appears the right way up in SVG's y-down space. On by default. |
True
|
precision
|
int
|
Decimal places for coordinates. Trailing zeros are dropped, so a whole number costs one character rather than five. |
3
|
group_by_path
|
bool
|
Give every stroke its own |
True
|
title
|
str
|
The document's |
None
|
units
|
str
|
A physical unit for the document's |
''
|
Returns:
| Type | Description |
|---|---|
str
|
A complete SVG document, ending in a newline. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the design has no points, |
Source code in src/geomotif/io/svg.py
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 | |
save_svg
¶
save_svg(design: Design, path: str | PathLike[str], **kwargs: Any) -> Path
Write a design to an SVG file and return the path written.
Keyword arguments are passed straight through to :func:to_svg.