Skip to content

Commit 5df6d84

Browse files
committed
interface updates
1 parent 6193716 commit 5df6d84

File tree

4 files changed

+42
-43
lines changed

4 files changed

+42
-43
lines changed

svgen/app.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,10 @@
1515

1616
# internal
1717
from svgen import PKG_NAME
18-
from svgen.attribute.viewbox import ViewBox
1918
from svgen.color.theme.manager import DEFAULT_THEME, THEMES
20-
from svgen.element.svg import Svg, add_background_grid
19+
from svgen.element.svg import Svg
2120
from svgen.generation.images import generate_images
2221
from svgen.script import invoke_script
23-
from svgen.shapes.border import compose_borders
2422

2523

2624
def generate(
@@ -41,12 +39,7 @@ def generate(
4139
if cwd_str not in path:
4240
path.append(cwd_str)
4341

44-
doc = Svg(ViewBox.from_dict(cast(GenericStrDict, config)))
45-
add_background_grid(doc, config["background"], config["grid"])
46-
if "border" in config:
47-
doc.children.extend(compose_borders(doc.viewbox, config["border"]))
48-
if "opacity" in config:
49-
doc["opacity"] = config["opacity"]
42+
doc = Svg.app(cast(GenericStrDict, config))
5043

5144
# Compose the document, via the external script.
5245
for script in list(scripts) + [Path(x) for x in config["scripts"]]:

svgen/element/path.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@
44

55
# built-in
66
from enum import StrEnum
7+
from typing import Any
78

89
# internal
10+
from svgen.attribute.viewbox import ViewBox
911
from svgen.cartesian.mutate import Translation
1012
from svgen.cartesian.point import Point
13+
from svgen.color.resolve import get_color
1114
from svgen.element import Element
1215

1316

@@ -63,3 +66,25 @@ def translation(self, translation: Translation, cmd: PathCmd) -> None:
6366
def path(self, **kwargs) -> Path:
6467
"""Get a path element based on this builder's state."""
6568
return Path.create(*self.cmds, **kwargs)
69+
70+
71+
def compose_borders(viewbox: ViewBox, config: dict[str, Any]) -> list[Element]:
72+
"""An example function for composing a document."""
73+
74+
builder = PathBuilder()
75+
builder.point(viewbox.box.top_left, PathCmd.MOVE)
76+
77+
builder.horizontal(viewbox.width)
78+
builder.vertical(viewbox.height)
79+
builder.horizontal(-viewbox.width)
80+
builder.close()
81+
82+
data = {
83+
"fill": "none",
84+
"stroke-width": config.get("stroke_width", 2),
85+
"stroke": get_color(config["color"]),
86+
}
87+
if "opacity" in config:
88+
data["stroke-opacity"] = config["opacity"]
89+
90+
return [builder.path(attrib=data)]

svgen/element/svg.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from svgen.attribute import XMLNS, Attribute
1010
from svgen.attribute.viewbox import ViewBox
1111
from svgen.element import Element
12+
from svgen.element.path import compose_borders
1213
from svgen.element.rect import Rect
1314

1415

@@ -26,6 +27,20 @@ def __init__(
2627
attrs.append(XMLNS)
2728
super().__init__(attrib=attrs, **extra)
2829

30+
@staticmethod
31+
def app(config: GenericStrDict) -> "Svg":
32+
"""Get an application SVG document."""
33+
34+
doc = Svg(ViewBox.from_dict(config))
35+
36+
add_background_grid(doc, config["background"], config["grid"])
37+
if "border" in config:
38+
doc.children.extend(compose_borders(doc.viewbox, config["border"]))
39+
if "opacity" in config:
40+
doc["opacity"] = config["opacity"]
41+
42+
return doc
43+
2944

3045
def add_background_grid(
3146
svg: Svg, background: GenericStrDict, grid: GenericStrDict

svgen/shapes/border.py

Lines changed: 0 additions & 34 deletions
This file was deleted.

0 commit comments

Comments
 (0)