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

SVG document walk, gradient definitions, and the public render API (#112). More...

#include <string.h>
#include "ra8_attributes.h"
#include "reflow_svg.h"
#include "reflow_svg_internal.h"
Include dependency graph for reflow_svg_doc.c:

Go to the source code of this file.

Functions

static bool internal_elem_at (const uint8_t *s, size_t len, size_t at, const char *name)
 Test whether an element keyword begins at s[at] with a valid delimiter.
static bool internal_tag_span (const uint8_t *s, size_t len, size_t from, const char *name, size_t *open, size_t *close)
 Locate the tag span [*open, *close) of element name at or after from.
static void internal_read_viewbox (const uint8_t *s, size_t len, svg_xform_t *t)
 Fill t's viewBox fields from the SVG root element.
static void internal_dispatch_shape (const uint8_t *s, size_t len, size_t at, size_t close, const svg_xform_t *t)
 Dispatch one element tag span to the appropriate shape-drawing function.
static void internal_xform_with_group (const svg_xform_t *t, svg_utf_t g, svg_xform_t *out)
 Copy the base transform t into out and replace the user affine with g.
static int32_t internal_group_open (const uint8_t *s, size_t i, size_t close, const svg_xform_t *t, svg_utf_t *gstk, int32_t gsp)
 Handle a 'g' open tag: compose its 'transform=' over the stack top and push.
static void internal_draw_shapes (const uint8_t *s, size_t len, const svg_xform_t *t)
 Walk every element in document order and draw the supported shapes.
static float internal_attr_numf (const uint8_t *s, size_t len, const char *name, float def)
 Read an SVG attribute as a floating-point number, returning a default when absent.
static void internal_copy_attr_id (const uint8_t *s, size_t len, char *dst)
 Copy the 'id' attribute value into dst as a NUL-terminated string.
static float internal_clamp01 (float v)
 Clamp a float value to the unit interval [0, 1].
static float internal_stop_offset (const uint8_t *tag, size_t tlen)
 Parse a gradient 'stop' element's 'offset' attribute.
static uint32_t internal_stop_color (const uint8_t *tag, size_t tlen)
 Parse a gradient 'stop' element's 'stop-color' attribute.
static void internal_parse_stops (const uint8_t *s, size_t from, size_t end, svg_grad_t *g)
 Parse the 'stop' child elements of a gradient definition into g.
static void internal_scan_grads (const uint8_t *s, size_t len, svg_grads_t *gs)
 Scan the document for gradient definitions and populate gs.
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.

Variables

static const float s_svg_unit = 1.0F
 Gradient parameter / offset upper clamp.

Detailed Description

SVG document walk, gradient definitions, and the public render API (#112).

The document-order element walk (with a bounded nested <g> transform stack), the <linearGradient> / <radialGradient> + <stop> gradient-definition scan, and the public ra8_svg_is_svg / ra8_svg_image_href / ra8_svg_size / ra8_svg_render entry points. Each shape is dispatched to its rasteriser in the sibling reflow_svg_*.c files. No DOM, no heap. See reflow_svg_internal.h for the shared geometry types and helper contracts.

[Ring 4 / Reflow] {World: NS}

Since
0.1.0

Definition in file reflow_svg_doc.c.

Function Documentation

◆ internal_attr_numf()

float internal_attr_numf ( const uint8_t * s,
size_t len,
const char * name,
float def )
static

Read an SVG attribute as a floating-point number, returning a default when absent.

Calls priv_ra8_svgp_attr to locate the attribute value slice, then forwards that slice to priv_ra8_svgp_numf starting at offset zero. Returns def when the attribute is absent. Used for reading gradient geometry attributes (x1, y1, x2, y2, cx, cy, r) and stop offsets that are specified as decimal fractions.

Parameters
[in]sByte buffer containing the SVG tag text; must not be NULL.
[in]lenTotal valid bytes in s.
[in]nameNUL-terminated ASCII attribute name; must not be NULL.
[in]defValue to return when the attribute is absent.
Returns
float The parsed floating-point value, or def when absent.
Return values
defAttribute name was not found.
nThe float value parsed from the attribute.
Precondition
s is a valid pointer to at least len bytes.
name is a valid NUL-terminated ASCII string.
Postcondition
s and name are not modified.
The return value equals def when priv_ra8_svgp_attr returns false.
Note
Not thread-safe in isolation; callers in this module are single-threaded during SVG render.
Since
0.1.0

Definition at line 429 of file reflow_svg_doc.c.

References priv_ra8_svgp_attr(), and priv_ra8_svgp_numf().

Referenced by internal_scan_grads().

◆ internal_clamp01()

float internal_clamp01 ( float v)
static

Clamp a float value to the unit interval [0, 1].

Returns 0.0F when v < 0.0F, returns s_svg_unit (1.0F) when v > s_svg_unit, and returns v unchanged otherwise. Used to clamp parsed gradient stop offsets to a valid range before they are stored in the gradient descriptor.

Parameters
[in]vFloat value to clamp.
Returns
float The value clamped to [0.0F, 1.0F].
Return values
0.0FWhen v < 0.0F.
s_svg_unitWhen v > s_svg_unit.
vWhen v is already in [0.0F, 1.0F].
Precondition
v is a finite float value.
s_svg_unit is initialised to 1.0F (module-scope constant).
Postcondition
The return value is in [0.0F, 1.0F].
v is not modified.
Note
Not thread-safe in isolation; callers in this module are single-threaded during SVG render.
Since
0.1.0

Definition at line 507 of file reflow_svg_doc.c.

References s_svg_unit.

Referenced by internal_parse_stops().

◆ internal_copy_attr_id()

void internal_copy_attr_id ( const uint8_t * s,
size_t len,
char * dst )
static

Copy the 'id' attribute value into dst as a NUL-terminated string.

Looks up the "id" attribute via priv_ra8_svgp_attr. If absent, writes a NUL byte at dst[0] and returns. Otherwise copies at most k_svg_grad_id - 1 bytes from the attribute value to dst and appends a NUL terminator. Used when scanning gradient definitions to store each gradient's id for later url(#id) resolution.

Parameters
[in]sByte buffer containing the SVG tag text; must not be NULL.
[in]lenTotal valid bytes in s.
[out]dstOutput buffer of at least k_svg_grad_id bytes; must not be NULL.
Precondition
s is a valid pointer to at least len bytes.
dst is a valid pointer to at least k_svg_grad_id bytes.
Postcondition
dst[0..n] is NUL-terminated where n <= k_svg_grad_id - 1.
s is not modified.
Note
Not thread-safe in isolation; callers in this module are single-threaded during SVG render.
Since
0.1.0

Definition at line 465 of file reflow_svg_doc.c.

References k_svg_grad_id, and priv_ra8_svgp_attr().

Referenced by internal_scan_grads().

◆ internal_dispatch_shape()

void internal_dispatch_shape ( const uint8_t * s,
size_t len,
size_t at,
size_t close,
const svg_xform_t * t )
static

Dispatch one element tag span to the appropriate shape-drawing function.

Slices s[at .. close) as the element tag, composes any element-level 'transform=' onto a local copy of t via priv_ra8_svgp_apply_xform, then selects the drawing function based on the element name: rect, circle, polygon, polyline, path, or line. Unsupported elements are silently ignored.

Parameters
[in]sFull SVG document byte buffer; must not be NULL.
[in]lenTotal valid bytes in s.
[in]atByte offset of the element opening tag in s.
[in]closeByte offset of the '>' terminator in s.
[in]tInherited coordinate transform (from group context); must not be NULL.
Precondition
s is a valid pointer to at least len bytes.
at <= close <= len.
Postcondition
The appropriate shape is drawn using the locally composed transform.
t is not modified; the local copy absorbs the element transform.
Note
Not thread-safe in isolation; callers in this module are single-threaded during SVG render.
Since
0.1.0

Definition at line 218 of file reflow_svg_doc.c.

References internal_elem_at(), priv_ra8_svgp_apply_xform(), priv_ra8_svgp_draw_circle(), priv_ra8_svgp_draw_line(), priv_ra8_svgp_draw_path(), priv_ra8_svgp_draw_polygon(), priv_ra8_svgp_draw_polyline(), and priv_ra8_svgp_draw_rect().

Referenced by internal_draw_shapes().

◆ internal_draw_shapes()

void internal_draw_shapes ( const uint8_t * s,
size_t len,
const svg_xform_t * t )
static

Walk every element in document order and draw the supported shapes.

Maintains a bounded group transform stack of depth k_svg_g_depth_max. Entering a 'g' element (via internal_group_open) composes its 'transform=' onto the current top and pushes. Encountering </g> pops. Each shape element inherits the top group transform via internal_xform_with_group, then composes its own element-level 'transform=' in internal_dispatch_shape. The scan is bounded: each iteration advances past one '<...>' tag or breaks.

Parameters
[in]sFull SVG document byte buffer; must not be NULL.
[in]lenTotal valid bytes in s.
[in]tBase coordinate transform (box, viewBox, gradients); must not be NULL.
Precondition
s is a valid pointer to at least len bytes.
t is a valid non-NULL pointer to an initialised svg_xform_t.
Postcondition
All recognisable shape elements in s are drawn to the framebuffer.
t is not modified; local copies absorb all transform composition.
Note
Not thread-safe in isolation; callers in this module are single-threaded during SVG render.
Since
0.1.0

Definition at line 364 of file reflow_svg_doc.c.

References internal_dispatch_shape(), internal_elem_at(), internal_group_open(), internal_xform_with_group(), k_svg_g_depth_max, svg_xform_t::ua, svg_xform_t::ub, svg_xform_t::uc, svg_xform_t::ud, svg_xform_t::ue, and svg_xform_t::uf.

Referenced by ra8_svg_render().

◆ internal_elem_at()

bool internal_elem_at ( const uint8_t * s,
size_t len,
size_t at,
const char * name )
static

Test whether an element keyword begins at s[at] with a valid delimiter.

Uses priv_ra8_svgp_starts_ci to match name at at, then verifies that the character immediately following the keyword (when within len) is either XML whitespace, '>', or '/'. A match at end-of-buffer is accepted as a valid (self-closing or run-to-EOF) element. Used to identify element tag kinds during the document walk without allocating strings.

Parameters
[in]sByte buffer containing the SVG text; must not be NULL.
[in]lenTotal valid bytes in s.
[in]atByte offset at which to test for the element name.
[in]nameNUL-terminated element keyword (including '<'); must not be NULL.
Returns
bool Whether name begins at at with a valid post-name delimiter.
Return values
truename matches at at and is followed by a valid delimiter.
falsename does not match or the following character is not a delimiter.
Precondition
s is a valid pointer to at least len bytes.
name is a valid NUL-terminated ASCII string.
Postcondition
s, len, at, and name are not modified.
The return value is deterministic given the same inputs.
Note
Not thread-safe in isolation; callers in this module are single-threaded during SVG render.
Since
0.1.0

Definition at line 65 of file reflow_svg_doc.c.

References priv_ra8_svgp_starts_ci(), priv_ra8_svgp_ws(), and strlen().

Referenced by internal_dispatch_shape(), and internal_draw_shapes().

◆ internal_group_open()

int32_t internal_group_open ( const uint8_t * s,
size_t i,
size_t close,
const svg_xform_t * t,
svg_utf_t * gstk,
int32_t gsp )
static

Handle a 'g' open tag: compose its 'transform=' over the stack top and push.

Builds a local transform by composing the current stack-top affine with any 'transform=' attribute on the 'g' element via priv_ra8_svgp_apply_xform. Self-closing 'g/' elements (detected by '/' before '>') and groups that would exceed k_svg_g_depth_max are not pushed; in those cases gsp is returned unchanged. Otherwise the new affine is pushed as gstk[gsp+1] and gsp+1 is returned.

Parameters
[in]sFull SVG document byte buffer; must not be NULL.
[in]iByte offset of the '<g' tag start.
[in]closeByte offset of the '>' terminator for the 'g' tag.
[in]tBase coordinate transform (box/viewBox); must not be NULL.
[in]gstkGroup transform stack of at least k_svg_g_depth_max+1 elements; must not be NULL.
[in]gspCurrent group stack pointer (index of the top entry).
Returns
int32_t The updated stack pointer after this open.
Return values
gspSelf-closing 'g' or depth-cap reached; stack unchanged.
gsp+1The new group's affine was pushed; stack pointer advanced.
Precondition
s is a valid pointer to at least close bytes.
gsp is in [0, k_svg_g_depth_max].
Postcondition
gstk[gsp+1] holds the composed affine when the return value is gsp+1.
gstk and t are not modified when gsp is returned.
Note
Not thread-safe in isolation; callers in this module are single-threaded during SVG render.
Since
0.1.0

Definition at line 310 of file reflow_svg_doc.c.

References internal_xform_with_group(), k_svg_g_depth_max, priv_ra8_svgp_apply_xform(), svg_xform_t::ua, svg_xform_t::ub, svg_xform_t::uc, svg_xform_t::ud, svg_xform_t::ue, and svg_xform_t::uf.

Referenced by internal_draw_shapes().

◆ internal_parse_stops()

void internal_parse_stops ( const uint8_t * s,
size_t from,
size_t end,
svg_grad_t * g )
static

Parse the 'stop' child elements of a gradient definition into g.

Scans s[from .. end) for '<stop' tags using priv_ra8_svgp_find_ci. For each found stop tag, extracts the offset via internal_stop_offset (clamped via internal_clamp01) and the colour via internal_stop_color, stores them in g->stops[g->nstops], and increments g->nstops. Bounded by k_svg_grad_stops; stops beyond the limit are silently dropped.

Parameters
[in]sFull SVG document byte buffer; must not be NULL.
[in]fromStart offset of the gradient body (after the opening tag).
[in]endEnd offset (exclusive; position of the closing tag).
[in,out]gGradient to populate; nstops and stops are updated.
Precondition
s is a valid pointer to at least end bytes.
g is a valid non-NULL pointer with g->nstops <= k_svg_grad_stops.
Postcondition
g->stops[0..nstops-1] hold the parsed stop data.
g->nstops <= k_svg_grad_stops.
Note
Not thread-safe in isolation; callers in this module are single-threaded during SVG render.
Since
0.1.0

Definition at line 625 of file reflow_svg_doc.c.

References svg_stop_t::col, internal_clamp01(), internal_stop_color(), internal_stop_offset(), k_svg_grad_stops, svg_grad_t::nstops, svg_stop_t::off, priv_ra8_svgp_find_ci(), and svg_grad_t::stops.

Referenced by internal_scan_grads().

◆ internal_read_viewbox()

void internal_read_viewbox ( const uint8_t * s,
size_t len,
svg_xform_t * t )
static

Fill t's viewBox fields from the SVG root element.

Searches for the first '<svg' tag and attempts to read a 'viewBox' attribute (four integers: min-x, min-y, width, height). If absent, falls back to 'width' and 'height' attributes. If those are also absent, the viewBox defaults to (0, 0, bw, bh) so that the SVG user space equals the target box with no scaling. Values with zero width or height are ignored.

Parameters
[in]sByte buffer containing the full SVG document; must not be NULL.
[in]lenTotal valid bytes in s.
[in,out]tTransform; vx, vy, vw, vh are written.
Precondition
s is a valid pointer to at least len bytes.
t is a valid non-NULL pointer with t->bw and t->bh set.
Postcondition
t->vx, t->vy, t->vw, t->vh reflect the parsed viewBox.
t->vw > 0 and t->vh > 0 on exit.
Note
Not thread-safe in isolation; callers in this module are single-threaded during SVG render.
Since
0.1.0

Definition at line 154 of file reflow_svg_doc.c.

References svg_xform_t::bh, svg_xform_t::bw, internal_tag_span(), k_svg_viewbox_n, priv_ra8_svgp_attr(), priv_ra8_svgp_attr_num(), priv_ra8_svgp_num(), svg_xform_t::vh, svg_xform_t::vw, svg_xform_t::vx, and svg_xform_t::vy.

Referenced by ra8_svg_render().

◆ internal_scan_grads()

void internal_scan_grads ( const uint8_t * s,
size_t len,
svg_grads_t * gs )
static

Scan the document for gradient definitions and populate gs.

Searches the document for '<linearGradient' and '<radialGradient' opening tags in document order. For each found gradient, reads the 'id', 'x1', 'y1', 'x2', 'y2' (linear) or 'cx', 'cy', 'r' (radial) attributes and populates a new entry in gs->g. Then locates the matching closing tag and calls internal_parse_stops to fill the stop list. Bounded by k_svg_grad_max; additional gradients are silently dropped.

Parameters
[in]sFull SVG document byte buffer; must not be NULL.
[in]lenTotal valid bytes in s.
[out]gsGradient set to populate; n and g are written.
Precondition
s is a valid pointer to at least len bytes.
gs is a valid non-NULL pointer to a writable svg_grads_t.
Postcondition
gs->n holds the count of parsed gradients (<= k_svg_grad_max).
gs->g[0..n-1] are valid, fully-populated svg_grad_t entries.
Note
Not thread-safe in isolation; callers in this module are single-threaded during SVG render.
Since
0.1.0

Definition at line 673 of file reflow_svg_doc.c.

References svg_grads::g, svg_grad_t::id, internal_attr_numf(), internal_copy_attr_id(), internal_parse_stops(), k_svg_grad_linear, k_svg_grad_max, k_svg_grad_radial, svg_grad_t::kind, svg_grads::n, svg_grad_t::nstops, priv_ra8_svgp_find_ci(), s_svg_unit, svg_grad_t::x1, svg_grad_t::x2, svg_grad_t::y1, and svg_grad_t::y2.

Referenced by ra8_svg_render().

◆ internal_stop_color()

uint32_t internal_stop_color ( const uint8_t * tag,
size_t tlen )
static

Parse a gradient 'stop' element's 'stop-color' attribute.

Reads the "stop-color" attribute from the stop tag via priv_ra8_svgp_attr and parses the value as a paint via priv_ra8_svgp_paint. If the attribute is absent or the paint parses to k_svg_no_paint (e.g. "none"), returns the SVG default fill colour k_svg_def_fill (black).

Parameters
[in]tagByte span of the 'stop' element tag; must not be NULL.
[in]tlenTotal valid bytes in tag.
Returns
uint32_t The stop colour in 0x00RRGGBB format.
Return values
(uint32_t)k_svg_def_fillAttribute absent or parses to no-paint.
0x000000..0xFFFFFFThe parsed solid stop colour.
Precondition
tag is a valid pointer to at least tlen bytes.
tlen > 0 when the tag is expected to contain attributes.
Postcondition
tag is not modified.
The return value has bits [31:24] clear.
Note
Not thread-safe in isolation; callers in this module are single-threaded during SVG render.
Since
0.1.0

Definition at line 588 of file reflow_svg_doc.c.

References k_svg_def_fill, k_svg_no_paint, priv_ra8_svgp_attr(), and priv_ra8_svgp_paint().

Referenced by internal_parse_stops().

◆ internal_stop_offset()

float internal_stop_offset ( const uint8_t * tag,
size_t tlen )
static

Parse a gradient 'stop' element's 'offset' attribute.

Reads the "offset" attribute from the stop tag via priv_ra8_svgp_attr and parses its value as a float via priv_ra8_svgp_numf. If a '' character is found anywhere in the attribute value span, the result is divided by the function-local 100.0F percentage scale to convert from percentage to fraction. Returns 0.0F when the attribute is absent. The caller is responsible for clamping via internal_clamp01.

Parameters
[in]tagByte span of the 'stop' element tag; must not be NULL.
[in]tlenTotal valid bytes in tag.
Returns
float The offset as a fraction in [0, 1] (unclamped), or 0.0F if absent.
Return values
0.0FAttribute "offset" is absent.
nParsed offset; divide by 100 if given as a percentage.
Precondition
tag is a valid pointer to at least tlen bytes.
tlen > 0 when the tag is expected to contain attributes.
Postcondition
tag is not modified.
The return value may be outside [0, 1] before the caller clamps it.
Note
Not thread-safe in isolation; callers in this module are single-threaded during SVG render.
Since
0.1.0

Definition at line 543 of file reflow_svg_doc.c.

References priv_ra8_svgp_attr(), and priv_ra8_svgp_numf().

Referenced by internal_parse_stops().

◆ internal_tag_span()

bool internal_tag_span ( const uint8_t * s,
size_t len,
size_t from,
const char * name,
size_t * open,
size_t * close )
static

Locate the tag span [*open, *close) of element name at or after from.

Calls priv_ra8_svgp_find_ci to find the first occurrence of name in s[from .. len). If found, scans forward for the next '>' character to determine the end of the tag. Sets *open to the start of the match and *close to the position of '>'. If name is not found, returns false.

Parameters
[in]sByte buffer containing the SVG text; must not be NULL.
[in]lenTotal valid bytes in s.
[in]fromStart offset for the search; must be <= len.
[in]nameNUL-terminated keyword to find; must not be NULL.
[out]openSet to the byte offset of the keyword match when found.
[out]closeSet to the byte offset of the '>' terminator when found.
Returns
bool Whether the element was found.
Return values
truename found; *open and *close are valid.
falsename not found in s[from .. len).
Precondition
s is a valid pointer to at least len bytes.
open and close are valid non-NULL output pointers.
Postcondition
On true: *open <= *close <= len.
On false: *open and *close are indeterminate.
Note
Not thread-safe in isolation; callers in this module are single-threaded during SVG render.
Since
0.1.0

Definition at line 109 of file reflow_svg_doc.c.

References priv_ra8_svgp_find_ci().

Referenced by internal_read_viewbox(), ra8_svg_image_href(), and ra8_svg_size().

◆ internal_xform_with_group()

void internal_xform_with_group ( const svg_xform_t * t,
svg_utf_t g,
svg_xform_t * out )
static

Copy the base transform t into out and replace the user affine with g.

Copies all fields of t (box, viewBox, gradient set) into out, then overwrites the six user-affine fields (ua .. uf) with the corresponding components of g. This produces a transform that has the same viewBox/box mapping as t but uses the group's accumulated affine.

Parameters
[in]tBase transform whose box/viewBox/grads are preserved; must not be NULL.
[in]gGroup affine to install in out.
[out]outTransform to write; must not be NULL; may alias t.
Precondition
t is a valid non-NULL pointer to an initialised svg_xform_t.
out is a valid non-NULL pointer to a writable svg_xform_t.
Postcondition
out->ua .. out->uf equal the fields of g.
All other fields of out equal those of t.
Note
Not thread-safe in isolation; callers in this module are single-threaded during SVG render.
Since
0.1.0

Definition at line 266 of file reflow_svg_doc.c.

References svg_utf_t::a, svg_utf_t::b, svg_utf_t::c, svg_utf_t::d, svg_utf_t::e, svg_utf_t::f, svg_xform_t::ua, svg_xform_t::ub, svg_xform_t::uc, svg_xform_t::ud, svg_xform_t::ue, and svg_xform_t::uf.

Referenced by internal_draw_shapes(), and internal_group_open().

◆ 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().

Variable Documentation

◆ s_svg_unit

const float s_svg_unit = 1.0F
static

Gradient parameter / offset upper clamp.

Definition at line 28 of file reflow_svg_doc.c.

Referenced by internal_clamp01(), and internal_scan_grads().