A drawing as keyed segments (spec/10 §6), copied from visualize's
Visualize.Render.Segments: a change sends only what it touched (visualize spec/09
§1.4, D-203).
What a segment is. A group and the root are their start, their children's segments and their end; any other node, and a group that names its identity, is clipped or moves (its motion wraps it), is one segment, its text what the target writes for it in the style its groups give it. A segment with no text is left out.
The texts are the output. svg/2's texts joined are the SVG renderer's output byte
for byte; json/2's joined by commas and bracketed are the :json canvas commands;
binary/2's concatenated are the binary stream, its head (the version and paint
records) first, keyed "head". Each target cuts its own output
(Macaw.Draw.Render.SVG, Canvas and Binary's segment steps), so a segment is
never a second serializer.
The binary pieces are raw bytes (macaw_frame#63): a live update carries them as
binary frames, never as text. Only where the target is markup (a first render, a page
with no stream) are they written as Base64, by base64/1 and to_base64/1 here, the
one place a stream becomes text.
The keys. A node with an identity ("data-mw-id") is "id:" <> identity; a
group's start and end are its path from the root (each node's index among its parent's
children, "r/2/1") then < or >; any other node is its group's path, ~ and a hash
of the node and its inherited style, so a node keeps its key when one before it comes or
goes. A repeated key gains #2, #3.
The cache holds the last drawing's nodes. A node whose key, node and inherited
style are equal to the last drawing's, compared whole, never by the hash, reuses the
text written then. It is an economy only: the segments are the same with %{}. A cache
of another target, or of a binary stream of other paints, is not read.
iex> tree = Macaw.Draw.IR.Element.group([Macaw.Draw.IR.Element.rect(0, 0, 10, 10, %{fill: "red"})])
iex> {segments, _cache} = Macaw.Draw.Render.Segments.svg(tree, %{})
iex> Enum.map(segments, &elem(&1, 1))
["<g>", ~s(<rect fill="red" height="10" width="10" x="0" y="0"/>), "</g>"]
iex> Macaw.Draw.Render.Segments.text(segments, :svg) == elem(Macaw.Draw.Render.to_string(tree, :svg, []), 1)
true
Summary
Types
What the last call wrote, by key: opaque.
A refusal: faults with their paths.
A segment's key: an identity's, or a path's.
The segments of a drawing, in drawing order.
Functions
Binary segments as markup carries them: each piece as padded standard Base64
(to_base64/1), keys and order kept. The one step that writes a stream's segments as
text, taken only where the target is markup (spec/10 §6): a live update carries the raw
pieces. A segment that is no {key, binary} is {:error, [{[i], {:type, :segment}}]}.
The node's binary canvas stream as segments, each a piece of the stream as raw bytes,
the stream's head (its version and paint records) first under "head", a segment of
no records left out: concatenated they are Macaw.Draw.Render.Binary.encode(node, [])'s
stream. Markup writes them through base64/1.
The node's :json canvas commands as segments, each its nodes' commands as JSON
objects joined by commas, a segment of no commands left out: joined by commas and put
in brackets they are Macaw.Draw.Render.render(node, :canvas, format: :json).
The node's SVG markup as segments: their texts concatenated are
Macaw.Draw.Render.to_string(node, :svg, []). A node the renderer refuses is its
faults.
The output the segments are of: the SVG markup, the :json commands, or the binary
stream's bytes. A binary segment that is no binary is
{:error, [{[i], {:type, :binary}}]}.
A stream as padded standard Base64, what markup carries (spec/10 §4.2, §6): the one
place a stream is written as text, the binary canvas's segments (base64/1) and the
incremental canvas's records alike.
Types
@type cache() :: map()
What the last call wrote, by key: opaque.
@type error() :: {:error, [Macaw.Base.Fault.t()]}
A refusal: faults with their paths.
@type key() :: String.t()
A segment's key: an identity's, or a path's.
The segments of a drawing, in drawing order.
Functions
Binary segments as markup carries them: each piece as padded standard Base64
(to_base64/1), keys and order kept. The one step that writes a stream's segments as
text, taken only where the target is markup (spec/10 §6): a live update carries the raw
pieces. A segment that is no {key, binary} is {:error, [{[i], {:type, :segment}}]}.
iex> Macaw.Draw.Render.Segments.base64([{"head", <<0, 4>>}, {"r~a", <<0x30>>}])
[{"head", "AAQ="}, {"r~a", "MA=="}]
@spec binary(Macaw.Draw.IR.Element.t(), cache()) :: {t(), cache()} | error()
The node's binary canvas stream as segments, each a piece of the stream as raw bytes,
the stream's head (its version and paint records) first under "head", a segment of
no records left out: concatenated they are Macaw.Draw.Render.Binary.encode(node, [])'s
stream. Markup writes them through base64/1.
@spec json(Macaw.Draw.IR.Element.t(), cache()) :: {t(), cache()} | error()
The node's :json canvas commands as segments, each its nodes' commands as JSON
objects joined by commas, a segment of no commands left out: joined by commas and put
in brackets they are Macaw.Draw.Render.render(node, :canvas, format: :json).
@spec svg(Macaw.Draw.IR.Element.t(), cache()) :: {t(), cache()} | error()
The node's SVG markup as segments: their texts concatenated are
Macaw.Draw.Render.to_string(node, :svg, []). A node the renderer refuses is its
faults.
The output the segments are of: the SVG markup, the :json commands, or the binary
stream's bytes. A binary segment that is no binary is
{:error, [{[i], {:type, :binary}}]}.
A stream as padded standard Base64, what markup carries (spec/10 §4.2, §6): the one
place a stream is written as text, the binary canvas's segments (base64/1) and the
incremental canvas's records alike.
iex> Macaw.Draw.Render.Segments.to_base64(<<0x30, 0x31>>)
"MDE="