Smith.SVG (Smith v0.4.0)

Copy Markdown View Source

Deferred SVG artwork as planar CAD regions, with fills, strokes and holes.

load/1 snapshots a local file; new/2 records a recipe. layout/1 evaluates actual geometry and returns a result and revision-linked measurements. Extrude the recipe with Smith.extrude/2, then fuse raised artwork or cut an engraving. SVG's downward Y axis is converted to upward CAD Y.

Supported: paths (all commands), basic shapes, groups, local use references, affine transforms, viewBox, physical units, inherited presentation attributes and inline styles. Fill curves remain native curves; stroke centerlines are sampled with an explicit final-mm tolerance. Fill rules retain holes, self-intersections and disconnected islands.

Painted mode unions selected fill and stroke regions. Paint colors are metadata, not Boolean operations or material assignments. No scripts, DTDs, external resources, stylesheets, raster tracing, masks, filters, gradients, dashes or partial opacity are rendered. Unsupported visible content returns an element-specific error. Convert embedded text to paths or use Smith.Text.

Summary

Functions

Lists visible elements with stable indices, IDs, groups, and paint metadata for selection.

Uniformly fits artwork within {width,height} mm, with optional margin: 0. Preserves placement and selection.

Parses SVG bytes without native geometry or external resource access. Errors identify unsupported elements.

Evaluates positioned planar faces and returns their actual bounds, area, region count, source hash and geometry revision.

Reads and snapshots a local SVG (up to 4 MiB). Subsequent file edits do not change the asset.

Records an SVG recipe. Options

Serializes an evaluated layout as filled, even-odd SVG outlines in its local plane. Wires are sampled with tolerance: 0.005 mm; the output records the source geometry revision and is independent of fonts and external resources. This is a geometry export, not a round trip of source styles or groups.

Returns positioned native wire recipes for centerline sweeps or projection, retaining open/closed subpaths and element metadata.

Writes JSON measurements and PNG/SVG previews of the converted CAD geometry. Accepts a recipe or its evaluated layout.

Types

layout()

@type layout() :: %{result: Smith.Result.t(), report: map()}

t()

@type t() :: %Smith.SVG{asset: Smith.SVG.Asset.t(), options: keyword()}

Functions

elements(asset)

@spec elements(Smith.SVG.Asset.t()) :: [map()]

Lists visible elements with stable indices, IDs, groups, and paint metadata for selection.

fit(recipe, box, opts \\ [])

@spec fit(t(), {number(), number()}, keyword()) :: {:ok, t()} | {:error, term()}

Uniformly fits artwork within {width,height} mm, with optional margin: 0. Preserves placement and selection.

from_binary(bytes)

@spec from_binary(binary()) :: {:ok, Smith.SVG.Asset.t()} | {:error, term()}

Parses SVG bytes without native geometry or external resource access. Errors identify unsupported elements.

layout(arg1)

@spec layout(t()) :: {:ok, layout()} | {:error, term()}

Evaluates positioned planar faces and returns their actual bounds, area, region count, source hash and geometry revision.

load(path)

@spec load(String.t()) :: {:ok, Smith.SVG.Asset.t()} | {:error, term()}

Reads and snapshots a local SVG (up to 4 MiB). Subsequent file edits do not change the asset.

new(asset, opts \\ [])

@spec new(Smith.SVG.Asset.t(), keyword()) :: t()

Records an SVG recipe. Options:

  • mode: :painted (union of fills and strokes), :fill, or :strokes.
  • width: or height: in mm; both form a contain-fit box, never stretching.
  • bounds: :artwork (default) or :viewport determines sizing/alignment.
  • align: {:origin, :origin} preserves the source origin. Each axis also accepts :min, :center, :max. at: {0,0}, on: Plane.xy() place it.
  • select: :all, an ID/group-label string, a list of strings, an element index, or {:fill, color} / {:stroke, color} selects artwork.
  • reference: :document retains shared size/alignment across selections; use :selection to fit and center only the chosen elements.
  • stroke_width: overrides stroke width in final mm. Otherwise original stroke widths follow SVG transforms and artwork scaling.
  • tolerance: 0.01 controls stroke sampling in final mm (must exceed 1e-6).

Construction makes no native calls. Validation occurs during evaluation.

outline_svg(layout, opts \\ [])

@spec outline_svg(layout(), keyword()) :: {:ok, binary()} | {:error, term()}

Serializes an evaluated layout as filled, even-odd SVG outlines in its local plane. Wires are sampled with tolerance: 0.005 mm; the output records the source geometry revision and is independent of fonts and external resources. This is a geometry export, not a round trip of source styles or groups.

paths(arg1)

@spec paths(t()) :: {:ok, [map()]} | {:error, term()}

Returns positioned native wire recipes for centerline sweeps or projection, retaining open/closed subpaths and element metadata.

write(value, directory, opts \\ [])

@spec write(t() | layout(), String.t(), keyword()) :: {:ok, map()} | {:error, term()}

Writes JSON measurements and PNG/SVG previews of the converted CAD geometry. Accepts a recipe or its evaluated layout.