ra8-firmware 0.1.0
Bare-metal firmware for the Renesas RA8 family (RA8D2 / RA8P1)
Loading...
Searching...
No Matches
reflow_svg.h File Reference

Minimal SVG handling for the reflow ereader (#112). More...

#include <stddef.h>
#include <stdint.h>
#include "ra8_err.h"
Include dependency graph for reflow_svg.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

bool ra8_svg_is_svg (const uint8_t *bytes, size_t len)
 Heuristically decide whether bytes are an SVG document.
ra8_err_t ra8_svg_image_href (const uint8_t *svg, size_t len, size_t *out_off, size_t *out_len)
 Extract the href of a cover-wrapper <svg><image .../></svg>.
ra8_err_t ra8_svg_size (const uint8_t *svg, size_t len, int32_t *out_w, int32_t *out_h)
 Report an SVG's intrinsic size (width/height, else viewBox dimensions).
ra8_err_t ra8_svg_render (const uint8_t *svg, size_t len, int32_t x, int32_t y, int32_t w, int32_t h)
 Render an SVG's vector shapes into a box.

Detailed Description

Minimal SVG handling for the reflow ereader (#112).

A small, hand-written, zero-allocation SVG subset for the two forms that actually appear in EPUBs:

  1. Cover wrappers – a <svg> whose only content is a single <image xlink:href="cover.jpg"/>. ra8_svg_image_href returns that href so the caller can decode the referenced raster through the existing ra8_img_decode_blit path (no new decoder needed).
  2. Simple in-content vector graphics<rect>, <circle>, <line>, <polygon> (scanline-filled), <polyline> (stroked), and <path> (filled; M/L/H/V/Z exact, the C/S/Q/T Bezier curves and the A elliptical arc flattened to line segments), with solid fill / stroke. A A full 2x3 affine transform= (translate / scale / rotate / skewX / skewY / matrix) – on a shape or a <g> group (nested, bounded depth) – repositions, resizes, rotates, and shears the result; axis-aligned shapes keep the exact ra8_gfx fast-path while rotated / sheared shapes are mapped through the affine and scanline-polygon-filled. ra8_svg_render maps them from the SVG viewBox (or width/height) into a caller box and draws them through the bound ra8_gfx framebuffer.

Out of scope (documented limitations, tracked as follow-ups): gradient fills (linearGradient / radialGradient), opacity, CSS styling, stroke width, and fractional coordinates (truncated to integers).

The parser is pure string scanning over the caller's byte buffer (no DOM, no heap); ra8_svg_render's only side effect is drawing through ra8_gfx.

[Ring 4 / Reflow] {World: NS}

Since
0.1.0

Definition in file reflow_svg.h.

Function Documentation

◆ ra8_svg_image_href()

ra8_err_t ra8_svg_image_href ( const uint8_t * svg,
size_t len,
size_t * out_off,
size_t * out_len )
nodiscard

Extract the href of a cover-wrapper <svg><image .../></svg>.

Scans for the first <image> element and returns its xlink:href / href attribute value as a slice into svg. The caller resolves + decodes that raster through its normal image path.

Parameters
[in]svgSVG document bytes.
[in]lenLength of svg.
[out]out_offReceives the href slice offset into svg.
[out]out_lenReceives the href slice length, bytes.
Returns
ra8_err_t
Return values
k_ra8_okAn <image> href was found.
k_ra8_err_null_ptrA required pointer is NULL.
k_ra8_err_not_foundNo <image> with an href was present.
Precondition
svg, out_off, out_len non-NULL.
None.
Postcondition
On success the outputs slice svg.
On failure the outputs are unchanged.
Note
Pure; thread-safe.
Since
0.1.0

Definition at line 741 of file reflow_svg_doc.c.

References internal_tag_span(), k_ra8_err_not_found, k_ra8_err_null_ptr, k_ra8_ok, and priv_ra8_svgp_attr().

Referenced by internal_render_svg().

◆ ra8_svg_is_svg()

bool ra8_svg_is_svg ( const uint8_t * bytes,
size_t len )
nodiscard

Heuristically decide whether bytes are an SVG document.

Skips a leading UTF-8 BOM + whitespace and checks for <?xml or a <svg element start (case-insensitive). Used by the image path to route SVG bytes here instead of to the raster decoder.

Parameters
[in]bytesCandidate image bytes.
[in]lenLength of bytes.
Returns
true iff bytes look like SVG.
Return values
falsebytes is NULL/empty or does not begin like SVG.
Precondition
None.
None.
Postcondition
No state modified.
Note
Pure; thread-safe.
Since
0.1.0

Definition at line 723 of file reflow_svg_doc.c.

References k_svg_bom0, k_svg_bom1, k_svg_bom2, k_svg_bom_len, priv_ra8_svgp_starts_ci(), and priv_ra8_svgp_ws().

Referenced by internal_render_one_image(), and ra8_img_probe_size().

◆ ra8_svg_render()

ra8_err_t ra8_svg_render ( const uint8_t * svg,
size_t len,
int32_t x,
int32_t y,
int32_t w,
int32_t h )
nodiscard

Render an SVG's vector shapes into a box.

Establishes a coordinate transform from the SVG viewBox (or its width/height, defaulting to the box) onto the caller box (x,y,w,h), then walks the supported shapes (<rect> / <circle> / <polygon> filled, <line> / <polyline> stroked) and draws each through the bound ra8_gfx framebuffer with its fill or stroke colour. fill="none" shapes are skipped. Coordinates outside the box are clipped by ra8_gfx.

Parameters
[in]svgSVG document bytes.
[in]lenLength of svg.
[in]xBox left, framebuffer pixels.
[in]yBox top, framebuffer pixels.
[in]wBox width, pixels (> 0).
[in]hBox height, pixels (> 0).
Returns
ra8_err_t
Return values
k_ra8_okRendered (possibly zero shapes).
k_ra8_err_null_ptrsvg is NULL.
k_ra8_err_invalid_argw or h is non-positive.
Precondition
svg non-NULL; ra8_gfx_init() has bound a framebuffer.
w > 0 and h > 0.
Postcondition
Shapes are drawn into the bound framebuffer within (x,y,w,h).
No allocation performed.
Note
Not thread-safe relative to the bound ra8_gfx framebuffer.
Since
0.1.0

Definition at line 799 of file reflow_svg_doc.c.

References svg_xform_t::grads, internal_draw_shapes(), internal_read_viewbox(), internal_scan_grads(), k_ra8_err_invalid_arg, k_ra8_err_null_ptr, and k_ra8_ok.

Referenced by es_render_svg_or_halt(), and internal_render_svg().

◆ ra8_svg_size()

ra8_err_t ra8_svg_size ( const uint8_t * svg,
size_t len,
int32_t * out_w,
int32_t * out_h )
nodiscard

Report an SVG's intrinsic size (width/height, else viewBox dimensions).

Lets the layout reserve an <img src="*.svg"> box without a raster decode. Prefers the <svg> width/height attributes (units ignored), and falls back to the viewBox width/height.

Parameters
[in]svgSVG document bytes.
[in]lenLength of svg.
[out]out_wReceives the intrinsic width, pixels.
[out]out_hReceives the intrinsic height, pixels.
Returns
ra8_err_t
Return values
k_ra8_okA positive size was determined.
k_ra8_err_null_ptrA required pointer is NULL.
k_ra8_err_not_foundNo <svg> or no positive size present.
Precondition
svg, out_w, out_h non-NULL.
None.
Postcondition
On success *out_w > 0 and *out_h > 0.
On failure the outputs are unchanged.
Note
Pure; thread-safe.
Since
0.1.0

Definition at line 764 of file reflow_svg_doc.c.

References internal_tag_span(), k_ra8_err_not_found, k_ra8_err_null_ptr, k_ra8_ok, k_svg_viewbox_n, priv_ra8_svgp_attr(), priv_ra8_svgp_attr_num(), and priv_ra8_svgp_num().

Referenced by es_render_svg_or_halt(), and ra8_img_probe_size().