Macaw.Draw.Render.Raster (macaw_draw v0.1.0)

Copy Markdown View Source

The raster target (spec/10 §9), copied from visualize's Visualize.Render.Raster: the package's SVG rasterized to PNG through resvg run as a port (visualize D-121, D-213). It is the one module that runs resvg, so the rasterizer is replaceable in one place, and the one place the package holds a process: a render's own, for one call.

{:ok, png, []} = Macaw.Draw.Render.Raster.to_png(root, scale: 2, background: "#ffffff")

It takes its program and its fonts as options and reads no application environment (spec/06 §1.1), probing the binary's version on every call and caching nothing: no :persistent_term, no ETS, so a binary replaced in place is read on the next call. A render is two port opens, the version probe and the render (spec/12 §5.8).

OptionDefaultMeaningFlag
:resvgSystem.find_executable("resvg")the resvg program's path, expanded; a path that names no executable is :no_rasterizernone
:scale1the device pixel ratio, positive--zoom
:backgroundnila CSS color painted beneath; nil transparent--background
:fontsMacaw.Draw.Render.Fonts.default()the font configuration--use-fonts-dir, --skip-system-fonts, the --*-family flags
:font_familythe default theme's font listthe root's font-family unless it has onenone
:timeout30_000milliseconds before the render is killednone

Every render passes --resources-dir with System.tmp_dir!/0. An option of the wrong type is {:error, {:option, key, value}}, checked before anything else, so it is answered whether or not a binary is installed.

Input (§9.1): a :root node is rendered in :literal resolution and embedded, the rules its classes select in a <style> (resvg reads a document's <style>); a binary is an SVG document, embedded the same way, one already embedded taken as given; anything else is :not_a_root. An SVG holding var(-- is :css_references.

Results (§9.3): {:ok, png, warnings}, a warning {:missing_family, list} or {:rasterizer, line} (warnings/1), [] when there were none; or :no_rasterizer, {:rasterizer_version, found, required}, :css_references, :timeout, {:rasterizer, message}, {:option, key, value} or :not_a_root. There is no bang variant: a caller that needs a complete picture matches {:ok, png, []}.

The port (§9.5): the SVG goes in on stdin and the PNG out on stdout, no temp file; the port is owned by a process of the render's own, linked to the caller and trapping exits, so a program that ended before its input was written answers as a failed exit; the caller waits in a receive, holding no scheduler; the timeout kills the port's process group, repeated every 10 ms until the port reports its exit, every wait bounded.

Summary

Types

Why a render produced no PNG (spec/10 §9.3).

A warning of a render (spec/10 §9.7).

Functions

The PNG of a root node or an SVG document (spec/10 §9): to_png/3 with the binary's availability read from the options, the :resvg path or the PATH's resvg.

to_png/2 with the binary's availability as an argument, the seam a test uses to reach the absent branch (spec/10 §9.4): false answers :no_rasterizer once the options and the input are checked.

Types

failure()

@type failure() ::
  :no_rasterizer
  | {:rasterizer_version, String.t(), String.t()}
  | :css_references
  | :timeout
  | {:rasterizer, String.t()}
  | {:option, atom(), term()}
  | :not_a_root

Why a render produced no PNG (spec/10 §9.3).

warning()

@type warning() :: {:missing_family, String.t()} | {:rasterizer, String.t()}

A warning of a render (spec/10 §9.7).

Functions

to_png(input, opts \\ [])

@spec to_png(Macaw.Draw.IR.Element.t() | String.t(), keyword()) ::
  {:ok, binary(), [warning()]} | {:error, failure()}

The PNG of a root node or an SVG document (spec/10 §9): to_png/3 with the binary's availability read from the options, the :resvg path or the PATH's resvg.

to_png(input, opts, available)

@spec to_png(Macaw.Draw.IR.Element.t() | String.t(), keyword(), boolean()) ::
  {:ok, binary(), [warning()]} | {:error, failure()}

to_png/2 with the binary's availability as an argument, the seam a test uses to reach the absent branch (spec/10 §9.4): false answers :no_rasterizer once the options and the input are checked.