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

Copy Markdown View Source

The binary canvas encoder (spec/10 §4.2–§4.6), copied from visualize's Visualize.Backend.CanvasBinary, .Opcodes and .PathEncoder: a scene packed into a compact little-endian stream of records the FrameBinary hook replays (spec/10 §7.9).

The stream is a concatenation of records, each beginning with a one-byte opcode that fixes its layout, so a stream is self-delimiting and two streams concatenated are one stream. Multi-byte integers are little-endian unsigned; coordinates little-endian IEEE-754, f64 unless a record says f32; color components single bytes.

OpcodeByteVersionRecord
version0x002the version of the records after it
path0x011polyline, f64
path_cubic0x021command stream, f64
circles0x031circle list
rects0x041rectangle list
path320x051polyline, f32
path_cubic320x061command stream, f32
cubic_run0x071a start point and a run of cubics, f32
style0x101fill, stroke, width, alpha
transform0x201a transform
save0x301ctx.save()
restore0x311ctx.restore()
fill0x401fill the current path
stroke0x411stroke the current path
paint0x502a paint defined for the stream
box0x512the box the next draw's paint is built over
effect0x522a shadow or a blur
motion0x532a motion span begins
motion_end0x542the span ends
paint_state0x553; 5 for the dash offseta paint's own alpha, cap, join, dash and the dash's offset
clip0x564the current path becomes the clip
blend0x574the compositing operation
incremental0x60—the incremental record, enclosing streams
clearing0x61—the clearing incremental record

opcodes/0 answers this table. Versions (visualize D-175, D-180, D-208, D-325): a stream of version 1 records alone has no version record; one using any later record begins with 0x00 v, v the highest it uses: 5 for a dash offset, else 4 for a clip or a blend, else 3 for a paint state, else 2 for a paint, an effect or a motion span. Each distinct paint is written once after the version record, indexed from 0 in first-use order.

Encoding the scene (§4.4): a :path, :circle or :rect writes its transform, its style, its geometry record and its draws (fill where it fills?/2, stroke where it strokes?/2), scoped by save/restore when it writes a transform or a style (visualize D-179); a :line the path M L and only a stroke; :polyline, :polygon the path of their points; :ellipse four cubic quarter-arcs with κ = 0.5522847; a :group save, transform, style, children, restore; the root its children; a clipped node save, its transform, the shape's geometry, clip, its records without its transform, restore. Text, images, titles, descriptions and definitions write nothing (§4.6): a binary layer's text is the Frame's text layer, its tiles the backdrop. A node's alpha is its composed opacity (§2.3, D-304). A color term is written from its channels; text is parsed by Macaw.Layers.Color.parse/1, one it cannot read and :current_color opaque black.

Path forms and precision (§4.5): after expand_smooth/1, one M then Ls is path32 (:f32) or path (:f64); one M then Cs cubic_run (:f32) or path_cubic (:f64); anything else path_cubic32 or path_cubic. precision: is :f32 by default, since the coordinates are pixels, and governs path records alone. No tensor library: every record is written by the binary constructor; a value beyond f32's range is written at its bound, which :f64 holds exactly. The SVG builder's elements are Macaw.Draw.Render.Binary.SVG's (§4.7); the reference decoder is Macaw.Draw.Render.Binary.Decoder (§4.8).

FunctionOptionDefaultMeaning
every encoder:precision:f32the path records' coordinate width
the target's callbacks:style%{}the style a path alone is drawn in

Summary

Types

A refusal: faults with their paths.

The coordinate width of the path records.

Functions

A scene's stream (§4.4), or a tree of the SVG builder's elements (Macaw.Draw.Render.Binary.SVG, §4.7). A scene is checked as the SVG renderer checks it, every fault at once; an option it does not take is {:bad_option, key, value}.

A path's record (§4.5) under the :precision option.

The opcode table of §4.2: each record's name, its byte, the version it needs (nil for the incremental records, which enclose streams) and what it is.

A node and everything under it: encode/2.

A path alone, drawn in the :style option's style: {:ok, binary} or its faults.

A list of nodes, one stream: the nodes as the children of one root.

Types

error()

@type error() :: {:error, [Macaw.Base.Fault.t()]}

A refusal: faults with their paths.

precision()

@type precision() :: :f32 | :f64

The coordinate width of the path records.

Functions

encode(node, opts)

@spec encode(Macaw.Draw.IR.Element.t() | Macaw.Draw.SVG.Element.t(), keyword()) ::
  {:ok, binary()} | error()

A scene's stream (§4.4), or a tree of the SVG builder's elements (Macaw.Draw.Render.Binary.SVG, §4.7). A scene is checked as the SVG renderer checks it, every fault at once; an option it does not take is {:bad_option, key, value}.

iex> Macaw.Draw.Render.Binary.encode(Macaw.Draw.IR.Element.rect(0, 0, 1, 1), [])
{:ok, <<0x04, 1::little-32, 0.0::little-float-64, 0.0::little-float-64, 1.0::little-float-64, 1.0::little-float-64>>}

encode_path(path, opts)

@spec encode_path(Macaw.Draw.IR.Path.t(), keyword()) :: {:ok, binary()} | error()

A path's record (§4.5) under the :precision option.

iex> path = Macaw.Draw.IR.Path.from_commands([{:M, 0, 0}, {:L, 1, 2}])
iex> Macaw.Draw.Render.Binary.encode_path(path, [])
{:ok, <<0x05, 2::little-32, 0.0::little-float-32, 0.0::little-float-32, 1.0::little-float-32, 2.0::little-float-32>>}

opcodes()

@spec opcodes() :: [
  %{
    name: atom(),
    byte: byte(),
    version: pos_integer() | nil,
    record: String.t()
  }
]

The opcode table of §4.2: each record's name, its byte, the version it needs (nil for the incremental records, which enclose streams) and what it is.

iex> Enum.find(Macaw.Draw.Render.Binary.opcodes(), &(&1.name == :clip)).byte
0x56

render_node(node, opts)

@spec render_node(Macaw.Draw.IR.Element.t(), keyword()) :: {:ok, binary()} | error()

A node and everything under it: encode/2.

render_path(path, opts)

@spec render_path(Macaw.Draw.IR.Path.t(), keyword()) :: {:ok, binary()} | error()

A path alone, drawn in the :style option's style: {:ok, binary} or its faults.

render_scene(nodes, opts)

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

A list of nodes, one stream: the nodes as the children of one root.