libwebsockets
Lightweight C library for HTML5 websockets
Loading...
Searching...
No Matches
lws-svg.h
Go to the documentation of this file.
1/*
2 * lws svg
3 *
4 * Written in 2026 by Andy Green <andy@warmcat.com>
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22 * IN THE SOFTWARE.
23 *
24 * Stateful, linewise SVG renderer for the fill-only subset of SVG.
25 *
26 * The document is parsed streaming (the parser is not sensitive to the input
27 * chunk sizes) into a retained vector scene in a single lwsac. Curves are
28 * flattened adaptively at parse time, to a tolerance derived from the
29 * viewBox size. Individual raster lines are then produced on demand by
30 * scanline intersection against the retained geometry, so any line can be
31 * rendered at any time, in any order.
32 *
33 * The supported subset is solid fills with nonzero or evenodd winding,
34 * transforms, viewBox / preserveAspectRatio mapping, group fill property
35 * inheritance and opacity. Strokes, gradients, text and filters are not
36 * rendered in this phase of the work.
37 */
38
39typedef struct lws_svg lws_svg_t;
40
41/* rgba packing is r in bits 0-7, g in 8-15, b in 16-23, alpha in 24-31, so it
42 * matches lws_display_colour_t layout */
43
44#define LWS_SVG_RGBA(_r, _g, _b, _a) (uint32_t)( \
45 ((uint32_t)(_r) & 0xff) | \
46 (((uint32_t)(_g) & 0xff) << 8) | \
47 (((uint32_t)(_b) & 0xff) << 16) | \
48 (((uint32_t)(_a) & 0xff) << 24))
49
50#define LWS_SVG_ALPHA(_c) ((uint32_t)(((_c) >> 24) & 0xff))
51
52/*
53 * Render mapping policy from preserveAspectRatio, only meaningful when a
54 * viewBox exists
55 */
56
57typedef enum {
58 LWS_SVG_PAR_DEFAULT, /* xMidYMid meet */
59 LWS_SVG_PAR_NONE, /* stretch to fill */
61
70
80
109lws_svg_parse(lws_svg_t *svg, const uint8_t **buf, size_t *len,
110 char hold_at_metadata);
111
122LWS_VISIBLE LWS_EXTERN unsigned int
124
132LWS_VISIBLE LWS_EXTERN unsigned int
134
146
147/*
148 * Span callback for rendered lines. Filled spans are delivered in ascending
149 * x with \p x0 the first covered pixel and \p x1 one past the last covered
150 * pixel (so the span is half-open [x0, x1)). \p rgba is the span fill colour
151 * with composed-opacity alpha in the top byte.
152 *
153 * With ri->aa clear (the default), coverage is binary at pixel centres, and
154 * each shape produces spans of the same alpha. With ri->aa set, coverage is
155 * the exact covered fraction of each pixel (area antialiasing), spans are
156 * additionally split where the fraction changes, and the alpha byte is the
157 * fill alpha scaled by the pixel's coverage. Returning nonzero stops the
158 * line rendering, which then returns LWS_SRET_OK.
159 */
160typedef int (*lws_svg_span_cb_t)(void *user, int x0, int x1, uint32_t rgba);
161
162typedef struct lws_svg_render {
163 int w; /* output raster width in px */
164 int h; /* output raster height in px */
165 char aa; /* nonzero: area-antialiased coverage */
167
198 lws_svg_span_cb_t cb, void *user);
unsigned int uint32_t
#define LWS_EXTERN
unsigned char uint8_t
lws_stateful_ret_t
#define LWS_VISIBLE
LWS_VISIBLE LWS_EXTERN lws_stateful_ret_t lws_svg_parse(lws_svg_t *svg, const uint8_t **buf, size_t *len, char hold_at_metadata)
LWS_VISIBLE LWS_EXTERN unsigned int lws_svg_get_width(const lws_svg_t *svg)
LWS_VISIBLE LWS_EXTERN lws_stateful_ret_t lws_svg_render_line(lws_svg_t *svg, const lws_svg_render_t *ri, int y, lws_svg_span_cb_t cb, void *user)
LWS_VISIBLE LWS_EXTERN lws_svg_t * lws_svg_new(void)
int(* lws_svg_span_cb_t)(void *user, int x0, int x1, uint32_t rgba)
Definition lws-svg.h:160
lws_svg_par_t
Definition lws-svg.h:57
@ LWS_SVG_PAR_DEFAULT
Definition lws-svg.h:58
@ LWS_SVG_PAR_NONE
Definition lws-svg.h:59
LWS_VISIBLE LWS_EXTERN unsigned int lws_svg_get_height(const lws_svg_t *svg)
struct lws_svg lws_svg_t
Definition lws-svg.h:39
struct lws_svg_render lws_svg_render_t
LWS_VISIBLE LWS_EXTERN char lws_svg_get_doc_complete(const lws_svg_t *svg)
LWS_VISIBLE LWS_EXTERN void lws_svg_free(lws_svg_t **svg)