Bland.Svg (Elixir Technical Drawing v0.6.0)

Copy Markdown View Source

Low-level SVG element builders.

Each builder returns an iodata fragment. The renderer composes fragments and only flattens to a binary at the very end, which keeps large plots cheap to construct.

The helpers in this module do not know about data or scales — they speak only in pixel coordinates. Use Bland.Scale.project/2 first, then pass the result in here.

Summary

Functions

Renders a keyword list or map of attributes into an SVG attribute string suffix (" k1=\"v1\" k2=\"v2\""). Underscores in keys are converted to hyphens so stroke_width: 1 becomes stroke-width="1".

Circle with center and radius.

Defs block — for <pattern>, <marker>, <clipPath> etc.

Wraps a %Bland.Figure{}-sized viewBox around a body of SVG fragments.

XML-escapes a string for safe insertion into element text or attribute values.

Group element with attributes.

Line from (x1,y1) to (x2,y2).

Formats a millimetre length for an SVG length attribute — two decimals, trailing zeros trimmed, with the mm unit suffix.

Formats a numeric value with up to 3 decimals, trimming trailing zeros.

General path from an SVG d string.

Polygon from a list of {x, y} tuples.

Polyline from a list of {x, y} tuples.

Text element. content is written literally — escape it with escape/1 if it may contain user input.

Types

attrs()

@type attrs() :: Enumerable.t()

iodata_frag()

@type iodata_frag() :: iodata()

Functions

attrs(attrs)

@spec attrs(attrs()) :: iodata()

Renders a keyword list or map of attributes into an SVG attribute string suffix (" k1=\"v1\" k2=\"v2\""). Underscores in keys are converted to hyphens so stroke_width: 1 becomes stroke-width="1".

circle(cx, cy, r, attrs \\ [])

@spec circle(number(), number(), number(), attrs()) :: iodata()

Circle with center and radius.

defs(body)

@spec defs(iodata()) :: iodata()

Defs block — for <pattern>, <marker>, <clipPath> etc.

document(width, height, body, size_mm \\ nil)

@spec document(number(), number(), iodata(), {number(), number()} | nil) :: iodata()

Wraps a %Bland.Figure{}-sized viewBox around a body of SVG fragments.

When size_mm is a {width_mm, height_mm} tuple the width and height attributes are emitted as physical millimetres while viewBox stays in user units — so the figure prints at true size and imports into a page layout at the dimensions it was drawn for. Pass nil (the default) to emit user units in both.

escape(s)

@spec escape(String.t()) :: String.t()

XML-escapes a string for safe insertion into element text or attribute values.

g(attrs, body)

@spec g(attrs(), iodata()) :: iodata()

Group element with attributes.

line(x1, y1, x2, y2, attrs \\ [])

@spec line(number(), number(), number(), number(), attrs()) :: iodata()

Line from (x1,y1) to (x2,y2).

mm(v)

@spec mm(number()) :: String.t()

Formats a millimetre length for an SVG length attribute — two decimals, trailing zeros trimmed, with the mm unit suffix.

iex> {Bland.Svg.mm(210.0), Bland.Svg.mm(215.9)}
{"210mm", "215.9mm"}

num(v)

@spec num(number()) :: String.t()

Formats a numeric value with up to 3 decimals, trimming trailing zeros.

path(d, attrs \\ [])

@spec path(iodata(), attrs()) :: iodata()

General path from an SVG d string.

polygon(points, attrs \\ [])

@spec polygon([{number(), number()}], attrs()) :: iodata()

Polygon from a list of {x, y} tuples.

polyline(points, attrs \\ [])

@spec polyline([{number(), number()}], attrs()) :: iodata()

Polyline from a list of {x, y} tuples.

rect(x, y, w, h, attrs \\ [])

@spec rect(number(), number(), number(), number(), attrs()) :: iodata()

Rectangle.

text(x, y, content, attrs \\ [])

@spec text(number(), number(), iodata(), attrs()) :: iodata()

Text element. content is written literally — escape it with escape/1 if it may contain user input.