wasmhost: SVG icons with clip paths and masks (nanosvg drew them as black boxes)
CI / test (push) Failing after 18s

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DPL5hiNfjrw2edja7LiRLB
This commit is contained in:
Claude committed 2026-11-25 20:09:39 +00:00
1 parent b4b9ba0e10
commit 2774516abf
3 files changed
+262 -4

No files matched your search

+85 -1
View File
@@ -390,13 +390,97 @@ int32_t rs_image_svg(moonbit_bytes_t data) {
return id;
}
/* Rasterise just the shapes `list[0..n)` (relinked into a list for the call) into `dst`, which
* nanosvg clears first: straight (not premultiplied) RGBA. */
static void svg_raster_list(NSVGrasterizer *rast, NSVGimage *svg, NSVGshape **list, int n, float dx, float dy,
float k, uint8_t *dst, int w, int h) {
NSVGshape **next = malloc(sizeof *next * (size_t)(n ? n : 1));
for (int i = 0; i < n; i++) { next[i] = list[i]->next; list[i]->next = i + 1 < n ? list[i + 1] : NULL; }
NSVGimage part = { svg->width, svg->height, n ? list[0] : NULL };
nsvgRasterize(rast, &part, dx, dy, k, dst, w, h, w * 4);
for (int i = 0; i < n; i++) list[i]->next = next[i];
free(next);
}
/* The coverage of clip path or mask `id` as w * h alpha bytes: a clip path's shapes filled
* opaque; a mask's shapes as drawn, by luminance times alpha (content under a filter counts as
* white: the masks Inkscape writes turn theirs white with a colour-matrix filter). */
static uint8_t *svg_clip_mask(NSVGrasterizer *rast, NSVGimage *svg, NSVGshape **all, int n, const char *id,
float dx, float dy, float k, int w, int h) {
NSVGshape **clip = malloc(sizeof *clip * (size_t)n);
NSVGpaint *fill = malloc(sizeof *fill * (size_t)n), *stroke = malloc(sizeof *stroke * (size_t)n);
int m = 0;
for (int i = 0; i < n; i++)
if ((all[i]->flags & NSVG_FLAGS_CLIP) && strcmp(all[i]->clipOf, id) == 0) clip[m++] = all[i];
if (!m) { free(clip); free(fill); free(stroke); return NULL; } /* no such clip: not clipped */
unsigned char *flags = malloc((size_t)m + 1);
for (int i = 0; i < m; i++) {
fill[i] = clip[i]->fill; stroke[i] = clip[i]->stroke; flags[i] = clip[i]->flags;
int is_mask = clip[i]->flags & NSVG_FLAGS_MASK;
if (!is_mask || (clip[i]->flags & NSVG_FLAGS_FILTERED)) {
/* White, keeping a mask's alpha (a plain colour's; a gradient's counts as opaque). */
unsigned a = is_mask && clip[i]->fill.type == NSVG_PAINT_COLOR ? clip[i]->fill.color >> 24 : 0xff;
if (clip[i]->fill.type != NSVG_PAINT_NONE || !is_mask) { clip[i]->fill.type = NSVG_PAINT_COLOR; clip[i]->fill.color = (a << 24) | 0xffffffu; }
if (!is_mask) clip[i]->stroke.type = NSVG_PAINT_NONE;
else if (clip[i]->stroke.type == NSVG_PAINT_COLOR) clip[i]->stroke.color |= 0xffffffu;
}
clip[i]->flags = (unsigned char)(clip[i]->flags | NSVG_FLAGS_VISIBLE);
}
uint8_t *rgba = malloc((size_t)w * h * 4), *mask = malloc((size_t)w * h);
svg_raster_list(rast, svg, clip, m, dx, dy, k, rgba, w, h);
for (size_t i = 0; i < (size_t)w * h; i++) {
const uint8_t *q = rgba + i * 4;
unsigned lum = (54u * q[0] + 183u * q[1] + 19u * q[2]) >> 8; /* Rec. 709 luminance */
mask[i] = (uint8_t)(q[3] * lum / 255);
}
for (int i = 0; i < m; i++) { clip[i]->fill = fill[i]; clip[i]->stroke = stroke[i]; clip[i]->flags = flags[i]; }
free(flags);
free(rgba); free(clip); free(fill); free(stroke);
return mask;
}
/* `src` over `dst`, both straight RGBA, with src's alpha scaled by `mask` (NULL: none). */
static void svg_over(uint8_t *dst, const uint8_t *src, const uint8_t *mask, size_t npx) {
for (size_t i = 0; i < npx; i++) {
const uint8_t *s = src + i * 4;
uint8_t *d = dst + i * 4;
unsigned sa = mask ? (unsigned)s[3] * mask[i] / 255 : s[3];
if (!sa) continue;
unsigned da = d[3], oa = sa + da * (255 - sa) / 255;
for (int c = 0; c < 3; c++) d[c] = (uint8_t)((s[c] * sa + d[c] * da * (255 - sa) / 255) / oa);
d[3] = (uint8_t)oa;
}
}
/* The SVG drawn into a new w x h straight RGBA buffer, fitted and centred. Shapes under a
* clip-path (or mask) are drawn in runs, each cut to its clip's coverage (nanosvg itself has no
* clipping; the vendored parser collects the clip shapes, see NSVG_FLAGS_CLIP). */
static uint8_t *render_svg(NSVGimage *svg, int w, int h) {
static NSVGrasterizer *rast;
if (!rast) rast = nsvgCreateRasterizer();
uint8_t *out = calloc((size_t)w * h, 4);
float k = fminf((float)w / svg->width, (float)h / svg->height);
float dx = ((float)w - svg->width * k) / 2.0f, dy = ((float)h - svg->height * k) / 2.0f;
nsvgRasterize(rast, svg, dx, dy, k, out, w, h, w * 4); /* straight (not premultiplied) RGBA */
int n = 0, clipped = 0;
for (NSVGshape *sh = svg->shapes; sh; sh = sh->next) { n++; if (sh->clipId[0] && (sh->flags & NSVG_FLAGS_VISIBLE)) clipped = 1; }
if (!clipped) {
nsvgRasterize(rast, svg, dx, dy, k, out, w, h, w * 4);
return out;
}
NSVGshape **all = malloc(sizeof *all * (size_t)n);
int i = 0;
for (NSVGshape *sh = svg->shapes; sh; sh = sh->next) all[i++] = sh;
uint8_t *tmp = malloc((size_t)w * h * 4);
for (int a = 0; a < n;) {
int b = a + 1;
while (b < n && strcmp(all[b]->clipId, all[a]->clipId) == 0) b++;
svg_raster_list(rast, svg, all + a, b - a, dx, dy, k, tmp, w, h);
uint8_t *mask = all[a]->clipId[0] ? svg_clip_mask(rast, svg, all, n, all[a]->clipId, dx, dy, k, w, h) : NULL;
svg_over(out, tmp, mask, (size_t)w * h);
free(mask);
a = b;
}
free(tmp); free(all);
return out;
}