Bland.Svg (Elixir Technical Drawing v0.3.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 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)

@spec document(number(), number(), iodata()) :: iodata()

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

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).

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.