API Reference (by levels)¶
This page documents the library by progressive learning levels.
Most users can simply import the flat API:
import mini_svg as minisvg
For teaching purposes, you can also import explicit levels:
from mini_svg import minisvg_basic
from mini_svg import minisvg_intermediate
from mini_svg import minisvg_advanced
Level 1 — Basic (minisvg_basic)¶
Core SVG building blocks:
- svg_begin
- svg_end
- save_svg
- line
- rect
- circle
mini_svg.basic ¶
mini_svg/basic.py¶
Level: Basic
Core SVG primitives for educational use.
This module provides: - SVG document helpers: svg_begin(), svg_end(), save_svg() - Basic shapes: line(), rect(), circle()
Style behavior
Most drawing functions accept style parameters (stroke, fill, etc.). If a style
parameter is omitted (passed as None), the global defaults from mini_svg.style
are used.
Recommended usage (flat API)
import mini_svg as minisvg
parts = [
minisvg.svg_begin(400, 300),
minisvg.circle(200, 150, 80, fill=minisvg.COLORS.SUNFLOWER),
minisvg.svg_end(),
]
minisvg.save_svg("out.svg", parts)
See also
mini_svg.stylefor COLORS, PALETTES and global style defaults.- The project examples folder for more complete scripts.
circle ¶
circle(
cx: float,
cy: float,
r: float,
*,
stroke: Optional[str] = None,
stroke_width: Optional[int] = None,
fill: Optional[str] = None,
opacity: Optional[float] = None,
) -> str
Draw a circle.
Parameters:
-
cx(float) –Center position. -
cy(float) –Center position. -
r(float) –Radius. -
stroke(Optional[str], default:None) –Stroke color. If None, uses global default `stroke`. -
stroke_width(Optional[int], default:None) –Stroke width in pixels. If None, uses global default `stroke_width`. -
fill(Optional[str], default:None) –Fill color. If None, uses global default `fill`. -
opacity(Optional[float], default:None) –Opacity 0.0..1.0. If None, uses global default `opacity`.
Returns:
-
str–SVG
fragment.
get_style ¶
get_style(field: str) -> object
Get one global default style field by name.
Parameters:
-
field(str) –Name of the field.
Returns:
-
object–The current value.
Raises:
-
KeyError–If field does not exist.
Examples:
import mini_svg as minisvg
current = minisvg.get_style("stroke_width")
line ¶
line(
x1: float,
y1: float,
x2: float,
y2: float,
*,
stroke: Optional[str] = None,
stroke_width: Optional[int] = None,
opacity: Optional[float] = None,
) -> str
Draw a straight line.
Parameters:
-
x1(float) –Endpoints of the line. -
y1(float) –Endpoints of the line. -
x2(float) –Endpoints of the line. -
y2(float) –Endpoints of the line. -
stroke(Optional[str], default:None) –Stroke color. If None, uses global default `stroke`. -
stroke_width(Optional[int], default:None) –Stroke width in pixels. If None, uses global default `stroke_width`. -
opacity(Optional[float], default:None) –Opacity 0.0..1.0. If None, uses global default `opacity`.
Returns:
-
str–SVG
fragment.
palette_pick ¶
palette_pick(palette: Tuple[str, ...], i: int) -> str
Pick a color from a palette using wrap-around indexing.
Parameters:
-
palette(Tuple[str, ...]) –A palette tuple, for example `PALETTES.WARHOL`. -
i(int) –Any integer index. The palette wraps around automatically.
Returns:
-
str–A color string.
Examples:
import mini_svg as minisvg
p = minisvg.PALETTES.WARHOL
c0 = minisvg.palette_pick(p, 0)
c1 = minisvg.palette_pick(p, 999) # wraps around
rect ¶
rect(
x: float,
y: float,
w: float,
h: float,
*,
stroke: Optional[str] = None,
stroke_width: Optional[int] = None,
fill: Optional[str] = None,
rx: Optional[float] = None,
ry: Optional[float] = None,
opacity: Optional[float] = None,
) -> str
Draw a rectangle.
Parameters:
-
x(float) –Top-left corner of the rectangle. -
y(float) –Top-left corner of the rectangle. -
w(float) –Width and height. -
h(float) –Width and height. -
stroke(Optional[str], default:None) –Stroke color. If None, uses global default `stroke`. -
stroke_width(Optional[int], default:None) –Stroke width in pixels. If None, uses global default `stroke_width`. -
fill(Optional[str], default:None) –Fill color. If None, uses global default `fill`. -
rx(Optional[float], default:None) –Optional corner radii (rounded corners). -
ry(Optional[float], default:None) –Optional corner radii (rounded corners). -
opacity(Optional[float], default:None) –Opacity 0.0..1.0. If None, uses global default `opacity`.
Returns:
-
str–SVG
fragment.
save_svg ¶
save_svg(filename: str, parts: List[str]) -> None
Save a list of SVG fragments into a file.
Parameters:
-
filename(str) –Output filename (e.g. "out.svg"). -
parts(List[str]) –List of SVG fragments (strings). Usually: [svg_begin(...), ..., svg_end()].
set_style ¶
set_style(
*,
stroke: Optional[str] = None,
fill: Optional[str] = None,
stroke_width: Optional[int] = None,
opacity: Optional[float] = None,
rotation_deg: Optional[float] = None,
font_size: Optional[int] = None,
) -> None
Set global default drawing style.
Any parameter left as None remains unchanged.
Parameters:
-
stroke(Optional[str], default:None) –Default stroke (outline) color. -
fill(Optional[str], default:None) –Default fill color. -
stroke_width(Optional[int], default:None) –Default stroke width (integer pixels). -
opacity(Optional[float], default:None) –Default opacity (0.0..1.0). -
rotation_deg(Optional[float], default:None) –Default rotation angle in degrees (used by polygons/stars). -
font_size(Optional[int], default:None) –Default font size (used by text()).
Notes
Drawing functions use these defaults when their style parameters are omitted.
Examples:
import mini_svg as minisvg
minisvg.set_style(stroke=minisvg.COLORS.DARK_GRAY, stroke_width=2, opacity=0.9)
svg_begin ¶
svg_begin(
width: int,
height: int,
*,
viewbox: Optional[Tuple[int, int, int, int]] = None,
) -> str
Start an SVG document and return the header fragment.
Parameters:
-
width(int) –Document dimensions in pixels. -
height(int) –Document dimensions in pixels. -
viewbox(Optional[Tuple[int, int, int, int]], default:None) –Optional SVG viewBox (x, y, w, h). If not provided, defaults to (0, 0, width, height).
Returns:
-
str–SVG header fragment including XML declaration and opening
Level 2 — Intermediate (minisvg_intermediate)¶
Builds on Level 1 and adds:
- text
- group
- regular_polygon
- star
- cubic_bezier
mini_svg.intermediate ¶
mini_svg/intermediate.py¶
Level: Intermediate
Builds on mini_svg.basic and adds:
- text(): draw text labels
- group(): group elements (like layers)
- regular_polygon(): regular polygon in a bounding box
- star(): star polygon in a bounding box
- cubic_bezier(): a single cubic Bézier curve as a path
Recommended usage (flat API)
import mini_svg as minisvg
parts = [minisvg.svg_begin(400, 300)]
parts.append(minisvg.text(200, 30, "Hello", anchor="middle"))
parts.append(minisvg.regular_polygon(6, (150, 80, 100, 100), fill=minisvg.COLORS.SUNFLOWER))
parts.append(minisvg.svg_end())
minisvg.save_svg("out.svg", parts)
See also
Look at the repository examples folder for complete scripts.
circle ¶
circle(
cx: float,
cy: float,
r: float,
*,
stroke: Optional[str] = None,
stroke_width: Optional[int] = None,
fill: Optional[str] = None,
opacity: Optional[float] = None,
) -> str
Draw a circle.
Parameters:
-
cx(float) –Center position. -
cy(float) –Center position. -
r(float) –Radius. -
stroke(Optional[str], default:None) –Stroke color. If None, uses global default `stroke`. -
stroke_width(Optional[int], default:None) –Stroke width in pixels. If None, uses global default `stroke_width`. -
fill(Optional[str], default:None) –Fill color. If None, uses global default `fill`. -
opacity(Optional[float], default:None) –Opacity 0.0..1.0. If None, uses global default `opacity`.
Returns:
-
str–SVG
fragment.
cubic_bezier ¶
cubic_bezier(
x0: float,
y0: float,
cx1: float,
cy1: float,
cx2: float,
cy2: float,
x: float,
y: float,
*,
stroke: Optional[str] = None,
stroke_width: Optional[int] = None,
fill: Optional[str] = None,
opacity: Optional[float] = None,
) -> str
Draw a single cubic Bézier curve using an SVG path.
Parameters:
-
x0(float) –Start point. -
y0(float) –Start point. -
cx1(float) –Control point 1. -
cy1(float) –Control point 1. -
cx2(float) –Control point 2. -
cy2(float) –Control point 2. -
x(float) –End point. -
y(float) –End point. -
stroke(Optional[str], default:None) –Style parameters. If omitted (None), use global defaults. -
stroke_width(Optional[str], default:None) –Style parameters. If omitted (None), use global defaults. -
fill(Optional[str], default:None) –Style parameters. If omitted (None), use global defaults. -
opacity(Optional[str], default:None) –Style parameters. If omitted (None), use global defaults.
Returns:
-
str–SVG
fragment.
get_style ¶
get_style(field: str) -> object
Get one global default style field by name.
Parameters:
-
field(str) –Name of the field.
Returns:
-
object–The current value.
Raises:
-
KeyError–If field does not exist.
Examples:
import mini_svg as minisvg
current = minisvg.get_style("stroke_width")
group ¶
group(
elements: List[str],
*,
transform: Optional[str] = None,
opacity: Optional[float] = None,
) -> str
Group multiple SVG fragments inside a
Parameters:
-
elements(List[str]) –List of SVG fragments. -
transform(Optional[str], default:None) –Raw SVG transform string (e.g. "rotate(30 200 200)"). -
opacity(Optional[float], default:None) –Group opacity. If None, uses global default `opacity`.
Returns:
-
str–SVG
fragment containing all elements.
line ¶
line(
x1: float,
y1: float,
x2: float,
y2: float,
*,
stroke: Optional[str] = None,
stroke_width: Optional[int] = None,
opacity: Optional[float] = None,
) -> str
Draw a straight line.
Parameters:
-
x1(float) –Endpoints of the line. -
y1(float) –Endpoints of the line. -
x2(float) –Endpoints of the line. -
y2(float) –Endpoints of the line. -
stroke(Optional[str], default:None) –Stroke color. If None, uses global default `stroke`. -
stroke_width(Optional[int], default:None) –Stroke width in pixels. If None, uses global default `stroke_width`. -
opacity(Optional[float], default:None) –Opacity 0.0..1.0. If None, uses global default `opacity`.
Returns:
-
str–SVG
fragment.
palette_pick ¶
palette_pick(palette: Tuple[str, ...], i: int) -> str
Pick a color from a palette using wrap-around indexing.
Parameters:
-
palette(Tuple[str, ...]) –A palette tuple, for example `PALETTES.WARHOL`. -
i(int) –Any integer index. The palette wraps around automatically.
Returns:
-
str–A color string.
Examples:
import mini_svg as minisvg
p = minisvg.PALETTES.WARHOL
c0 = minisvg.palette_pick(p, 0)
c1 = minisvg.palette_pick(p, 999) # wraps around
rect ¶
rect(
x: float,
y: float,
w: float,
h: float,
*,
stroke: Optional[str] = None,
stroke_width: Optional[int] = None,
fill: Optional[str] = None,
rx: Optional[float] = None,
ry: Optional[float] = None,
opacity: Optional[float] = None,
) -> str
Draw a rectangle.
Parameters:
-
x(float) –Top-left corner of the rectangle. -
y(float) –Top-left corner of the rectangle. -
w(float) –Width and height. -
h(float) –Width and height. -
stroke(Optional[str], default:None) –Stroke color. If None, uses global default `stroke`. -
stroke_width(Optional[int], default:None) –Stroke width in pixels. If None, uses global default `stroke_width`. -
fill(Optional[str], default:None) –Fill color. If None, uses global default `fill`. -
rx(Optional[float], default:None) –Optional corner radii (rounded corners). -
ry(Optional[float], default:None) –Optional corner radii (rounded corners). -
opacity(Optional[float], default:None) –Opacity 0.0..1.0. If None, uses global default `opacity`.
Returns:
-
str–SVG
fragment.
regular_polygon ¶
regular_polygon(
n_sides: int,
bbox: Tuple[float, float, float, float],
*,
rotation_deg: Optional[float] = None,
stroke: Optional[str] = None,
stroke_width: Optional[int] = None,
fill: Optional[str] = None,
opacity: Optional[float] = None,
) -> str
Draw a regular polygon inside a bounding box.
Parameters:
-
n_sides(int) –Number of sides (>= 3). -
bbox(Tuple[float, float, float, float]) –Bounding box (x, y, width, height). -
rotation_deg(Optional[float], default:None) –Rotation angle in degrees. If None, uses global default `rotation_deg`. -
stroke(Optional[str], default:None) –Style parameters. If omitted (None), use global defaults. -
stroke_width(Optional[str], default:None) –Style parameters. If omitted (None), use global defaults. -
fill(Optional[str], default:None) –Style parameters. If omitted (None), use global defaults. -
opacity(Optional[str], default:None) –Style parameters. If omitted (None), use global defaults.
Returns:
-
str–SVG
fragment.
Raises:
-
ValueError–If n_sides < 3.
save_svg ¶
save_svg(filename: str, parts: List[str]) -> None
Save a list of SVG fragments into a file.
Parameters:
-
filename(str) –Output filename (e.g. "out.svg"). -
parts(List[str]) –List of SVG fragments (strings). Usually: [svg_begin(...), ..., svg_end()].
set_style ¶
set_style(
*,
stroke: Optional[str] = None,
fill: Optional[str] = None,
stroke_width: Optional[int] = None,
opacity: Optional[float] = None,
rotation_deg: Optional[float] = None,
font_size: Optional[int] = None,
) -> None
Set global default drawing style.
Any parameter left as None remains unchanged.
Parameters:
-
stroke(Optional[str], default:None) –Default stroke (outline) color. -
fill(Optional[str], default:None) –Default fill color. -
stroke_width(Optional[int], default:None) –Default stroke width (integer pixels). -
opacity(Optional[float], default:None) –Default opacity (0.0..1.0). -
rotation_deg(Optional[float], default:None) –Default rotation angle in degrees (used by polygons/stars). -
font_size(Optional[int], default:None) –Default font size (used by text()).
Notes
Drawing functions use these defaults when their style parameters are omitted.
Examples:
import mini_svg as minisvg
minisvg.set_style(stroke=minisvg.COLORS.DARK_GRAY, stroke_width=2, opacity=0.9)
star ¶
star(
n_points: int,
bbox: Tuple[float, float, float, float],
*,
rotation_deg: Optional[float] = None,
inner_ratio: float = 0.5,
stroke: Optional[str] = None,
stroke_width: Optional[int] = None,
fill: Optional[str] = None,
opacity: Optional[float] = None,
) -> str
Draw a star inside a bounding box.
Parameters:
-
n_points(int) –Number of outer points (>= 3). -
bbox(Tuple[float, float, float, float]) –Bounding box (x, y, width, height). -
rotation_deg(Optional[float], default:None) –Rotation angle in degrees. If None, uses global default `rotation_deg`. -
inner_ratio(float, default:0.5) –Inner radius ratio (0..1). Typical values: 0.4..0.6. -
stroke(Optional[str], default:None) –Style parameters. If omitted (None), use global defaults. -
stroke_width(Optional[str], default:None) –Style parameters. If omitted (None), use global defaults. -
fill(Optional[str], default:None) –Style parameters. If omitted (None), use global defaults. -
opacity(Optional[str], default:None) –Style parameters. If omitted (None), use global defaults.
Returns:
-
str–SVG
fragment.
Raises:
-
ValueError–If n_points < 3 or inner_ratio not in (0, 1).
svg_begin ¶
svg_begin(
width: int,
height: int,
*,
viewbox: Optional[Tuple[int, int, int, int]] = None,
) -> str
Start an SVG document and return the header fragment.
Parameters:
-
width(int) –Document dimensions in pixels. -
height(int) –Document dimensions in pixels. -
viewbox(Optional[Tuple[int, int, int, int]], default:None) –Optional SVG viewBox (x, y, w, h). If not provided, defaults to (0, 0, width, height).
Returns:
-
str–SVG header fragment including XML declaration and opening
text ¶
text(
x: float,
y: float,
content: str,
*,
font_size: Optional[int] = None,
fill: Optional[str] = None,
anchor: str = "start",
opacity: Optional[float] = None,
) -> str
Draw text.
Parameters:
-
x(float) –Text position. -
y(float) –Text position. -
content(str) –Text content (will be XML-escaped). -
font_size(Optional[int], default:None) –Font size in pixels. If None, uses global default `font_size`. -
fill(Optional[str], default:None) –Text color. If None, defaults to COLORS.BLACK. -
anchor(str, default:'start') –SVG text-anchor: "start", "middle", or "end". -
opacity(Optional[float], default:None) –Opacity 0.0..1.0. If None, uses global default `opacity`.
Returns:
-
str–SVG
fragment.
Level 3 — Advanced (minisvg_advanced)¶
Builds on Level 2 and adds:
- bezier_path_cubic
mini_svg.advanced ¶
mini_svg/advanced.py¶
Level: Advanced
Builds on mini_svg.intermediate and adds:
- bezier_path_cubic(): multi-segment cubic Bézier path
This level introduces structured data: - a list of cubic segments (control points + end point)
Recommended usage (flat API)
import mini_svg as minisvg
parts = [minisvg.svg_begin(400, 300)]
path = minisvg.bezier_path_cubic(
(50, 150),
[
(100, 50, 150, 250, 200, 150),
(250, 50, 300, 250, 350, 150),
],
stroke=minisvg.COLORS.DARK_GRAY,
fill=minisvg.COLORS.TRANSPARENT,
stroke_width=3,
)
parts.append(path)
parts.append(minisvg.svg_end())
minisvg.save_svg("out.svg", parts)
bezier_path_cubic ¶
bezier_path_cubic(
start: Tuple[float, float],
segments: List[
Tuple[float, float, float, float, float, float]
],
*,
stroke: Optional[str] = None,
stroke_width: Optional[int] = None,
fill: Optional[str] = None,
opacity: Optional[float] = None,
) -> str
Draw a multi-segment cubic Bézier path.
Parameters:
-
start(Tuple[float, float]) –Start point (x0, y0). -
segments(List[Tuple[float, float, float, float, float, float]]) –List of cubic segments, each: (cx1, cy1, cx2, cy2, x, y) -
stroke(Optional[str], default:None) –Style parameters. If omitted (None), use global defaults. -
stroke_width(Optional[str], default:None) –Style parameters. If omitted (None), use global defaults. -
fill(Optional[str], default:None) –Style parameters. If omitted (None), use global defaults. -
opacity(Optional[str], default:None) –Style parameters. If omitted (None), use global defaults.
Returns:
-
str–SVG
fragment.
Notes
This function is "advanced" because it introduces lists and structured data.
circle ¶
circle(
cx: float,
cy: float,
r: float,
*,
stroke: Optional[str] = None,
stroke_width: Optional[int] = None,
fill: Optional[str] = None,
opacity: Optional[float] = None,
) -> str
Draw a circle.
Parameters:
-
cx(float) –Center position. -
cy(float) –Center position. -
r(float) –Radius. -
stroke(Optional[str], default:None) –Stroke color. If None, uses global default `stroke`. -
stroke_width(Optional[int], default:None) –Stroke width in pixels. If None, uses global default `stroke_width`. -
fill(Optional[str], default:None) –Fill color. If None, uses global default `fill`. -
opacity(Optional[float], default:None) –Opacity 0.0..1.0. If None, uses global default `opacity`.
Returns:
-
str–SVG
fragment.
cubic_bezier ¶
cubic_bezier(
x0: float,
y0: float,
cx1: float,
cy1: float,
cx2: float,
cy2: float,
x: float,
y: float,
*,
stroke: Optional[str] = None,
stroke_width: Optional[int] = None,
fill: Optional[str] = None,
opacity: Optional[float] = None,
) -> str
Draw a single cubic Bézier curve using an SVG path.
Parameters:
-
x0(float) –Start point. -
y0(float) –Start point. -
cx1(float) –Control point 1. -
cy1(float) –Control point 1. -
cx2(float) –Control point 2. -
cy2(float) –Control point 2. -
x(float) –End point. -
y(float) –End point. -
stroke(Optional[str], default:None) –Style parameters. If omitted (None), use global defaults. -
stroke_width(Optional[str], default:None) –Style parameters. If omitted (None), use global defaults. -
fill(Optional[str], default:None) –Style parameters. If omitted (None), use global defaults. -
opacity(Optional[str], default:None) –Style parameters. If omitted (None), use global defaults.
Returns:
-
str–SVG
fragment.
get_style ¶
get_style(field: str) -> object
Get one global default style field by name.
Parameters:
-
field(str) –Name of the field.
Returns:
-
object–The current value.
Raises:
-
KeyError–If field does not exist.
Examples:
import mini_svg as minisvg
current = minisvg.get_style("stroke_width")
group ¶
group(
elements: List[str],
*,
transform: Optional[str] = None,
opacity: Optional[float] = None,
) -> str
Group multiple SVG fragments inside a
Parameters:
-
elements(List[str]) –List of SVG fragments. -
transform(Optional[str], default:None) –Raw SVG transform string (e.g. "rotate(30 200 200)"). -
opacity(Optional[float], default:None) –Group opacity. If None, uses global default `opacity`.
Returns:
-
str–SVG
fragment containing all elements.
line ¶
line(
x1: float,
y1: float,
x2: float,
y2: float,
*,
stroke: Optional[str] = None,
stroke_width: Optional[int] = None,
opacity: Optional[float] = None,
) -> str
Draw a straight line.
Parameters:
-
x1(float) –Endpoints of the line. -
y1(float) –Endpoints of the line. -
x2(float) –Endpoints of the line. -
y2(float) –Endpoints of the line. -
stroke(Optional[str], default:None) –Stroke color. If None, uses global default `stroke`. -
stroke_width(Optional[int], default:None) –Stroke width in pixels. If None, uses global default `stroke_width`. -
opacity(Optional[float], default:None) –Opacity 0.0..1.0. If None, uses global default `opacity`.
Returns:
-
str–SVG
fragment.
palette_pick ¶
palette_pick(palette: Tuple[str, ...], i: int) -> str
Pick a color from a palette using wrap-around indexing.
Parameters:
-
palette(Tuple[str, ...]) –A palette tuple, for example `PALETTES.WARHOL`. -
i(int) –Any integer index. The palette wraps around automatically.
Returns:
-
str–A color string.
Examples:
import mini_svg as minisvg
p = minisvg.PALETTES.WARHOL
c0 = minisvg.palette_pick(p, 0)
c1 = minisvg.palette_pick(p, 999) # wraps around
rect ¶
rect(
x: float,
y: float,
w: float,
h: float,
*,
stroke: Optional[str] = None,
stroke_width: Optional[int] = None,
fill: Optional[str] = None,
rx: Optional[float] = None,
ry: Optional[float] = None,
opacity: Optional[float] = None,
) -> str
Draw a rectangle.
Parameters:
-
x(float) –Top-left corner of the rectangle. -
y(float) –Top-left corner of the rectangle. -
w(float) –Width and height. -
h(float) –Width and height. -
stroke(Optional[str], default:None) –Stroke color. If None, uses global default `stroke`. -
stroke_width(Optional[int], default:None) –Stroke width in pixels. If None, uses global default `stroke_width`. -
fill(Optional[str], default:None) –Fill color. If None, uses global default `fill`. -
rx(Optional[float], default:None) –Optional corner radii (rounded corners). -
ry(Optional[float], default:None) –Optional corner radii (rounded corners). -
opacity(Optional[float], default:None) –Opacity 0.0..1.0. If None, uses global default `opacity`.
Returns:
-
str–SVG
fragment.
regular_polygon ¶
regular_polygon(
n_sides: int,
bbox: Tuple[float, float, float, float],
*,
rotation_deg: Optional[float] = None,
stroke: Optional[str] = None,
stroke_width: Optional[int] = None,
fill: Optional[str] = None,
opacity: Optional[float] = None,
) -> str
Draw a regular polygon inside a bounding box.
Parameters:
-
n_sides(int) –Number of sides (>= 3). -
bbox(Tuple[float, float, float, float]) –Bounding box (x, y, width, height). -
rotation_deg(Optional[float], default:None) –Rotation angle in degrees. If None, uses global default `rotation_deg`. -
stroke(Optional[str], default:None) –Style parameters. If omitted (None), use global defaults. -
stroke_width(Optional[str], default:None) –Style parameters. If omitted (None), use global defaults. -
fill(Optional[str], default:None) –Style parameters. If omitted (None), use global defaults. -
opacity(Optional[str], default:None) –Style parameters. If omitted (None), use global defaults.
Returns:
-
str–SVG
fragment.
Raises:
-
ValueError–If n_sides < 3.
save_svg ¶
save_svg(filename: str, parts: List[str]) -> None
Save a list of SVG fragments into a file.
Parameters:
-
filename(str) –Output filename (e.g. "out.svg"). -
parts(List[str]) –List of SVG fragments (strings). Usually: [svg_begin(...), ..., svg_end()].
set_style ¶
set_style(
*,
stroke: Optional[str] = None,
fill: Optional[str] = None,
stroke_width: Optional[int] = None,
opacity: Optional[float] = None,
rotation_deg: Optional[float] = None,
font_size: Optional[int] = None,
) -> None
Set global default drawing style.
Any parameter left as None remains unchanged.
Parameters:
-
stroke(Optional[str], default:None) –Default stroke (outline) color. -
fill(Optional[str], default:None) –Default fill color. -
stroke_width(Optional[int], default:None) –Default stroke width (integer pixels). -
opacity(Optional[float], default:None) –Default opacity (0.0..1.0). -
rotation_deg(Optional[float], default:None) –Default rotation angle in degrees (used by polygons/stars). -
font_size(Optional[int], default:None) –Default font size (used by text()).
Notes
Drawing functions use these defaults when their style parameters are omitted.
Examples:
import mini_svg as minisvg
minisvg.set_style(stroke=minisvg.COLORS.DARK_GRAY, stroke_width=2, opacity=0.9)
star ¶
star(
n_points: int,
bbox: Tuple[float, float, float, float],
*,
rotation_deg: Optional[float] = None,
inner_ratio: float = 0.5,
stroke: Optional[str] = None,
stroke_width: Optional[int] = None,
fill: Optional[str] = None,
opacity: Optional[float] = None,
) -> str
Draw a star inside a bounding box.
Parameters:
-
n_points(int) –Number of outer points (>= 3). -
bbox(Tuple[float, float, float, float]) –Bounding box (x, y, width, height). -
rotation_deg(Optional[float], default:None) –Rotation angle in degrees. If None, uses global default `rotation_deg`. -
inner_ratio(float, default:0.5) –Inner radius ratio (0..1). Typical values: 0.4..0.6. -
stroke(Optional[str], default:None) –Style parameters. If omitted (None), use global defaults. -
stroke_width(Optional[str], default:None) –Style parameters. If omitted (None), use global defaults. -
fill(Optional[str], default:None) –Style parameters. If omitted (None), use global defaults. -
opacity(Optional[str], default:None) –Style parameters. If omitted (None), use global defaults.
Returns:
-
str–SVG
fragment.
Raises:
-
ValueError–If n_points < 3 or inner_ratio not in (0, 1).
svg_begin ¶
svg_begin(
width: int,
height: int,
*,
viewbox: Optional[Tuple[int, int, int, int]] = None,
) -> str
Start an SVG document and return the header fragment.
Parameters:
-
width(int) –Document dimensions in pixels. -
height(int) –Document dimensions in pixels. -
viewbox(Optional[Tuple[int, int, int, int]], default:None) –Optional SVG viewBox (x, y, w, h). If not provided, defaults to (0, 0, width, height).
Returns:
-
str–SVG header fragment including XML declaration and opening
text ¶
text(
x: float,
y: float,
content: str,
*,
font_size: Optional[int] = None,
fill: Optional[str] = None,
anchor: str = "start",
opacity: Optional[float] = None,
) -> str
Draw text.
Parameters:
-
x(float) –Text position. -
y(float) –Text position. -
content(str) –Text content (will be XML-escaped). -
font_size(Optional[int], default:None) –Font size in pixels. If None, uses global default `font_size`. -
fill(Optional[str], default:None) –Text color. If None, defaults to COLORS.BLACK. -
anchor(str, default:'start') –SVG text-anchor: "start", "middle", or "end". -
opacity(Optional[float], default:None) –Opacity 0.0..1.0. If None, uses global default `opacity`.
Returns:
-
str–SVG
fragment.