Adiciona tinyblocks-vg e tinyblocks-svg (rasterizador + parser SVG) e specs no editor

This commit is contained in:
mguinhos committed 2026-10-20 21:13:53 -03:00
1 parent f4e2556110
commit 714186a1a1
12 files changed
+1272 -6

No files matched your search

+322
View File
@@ -0,0 +1,322 @@
/*
* TinyBlocks - tb_vg.c
* Categoria "Vetor": VG.New / VG.Line / VG.Rect / VG.Circle / VG.Polygon /
* VG.Render. Rasterizador de software simples (linhas, retangulos, circulos e
* poligonos preenchidos) usado pela tinyblocks-svg.
*/
#define _POSIX_C_SOURCE 200809L
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <tinyblocks/tinyblocks.h>
#include "tb_vg.h"
#define VG_MAX_PTS 4096
typedef struct { double x, y; } VgPt;
static void blend(TinyBlocks_Image* im, int x, int y, float r, float g, float b, float a) {
int w = tb_image_width(im), h = tb_image_height(im);
if (x < 0 || y < 0 || x >= w || y >= h || a <= 0.f) return;
uint8_t dr, dg, db, da;
if (!tb_image_get_pixel(im, x, y, &dr, &dg, &db, &da)) return;
float sr = r*255.f, sg = g*255.f, sb = b*255.f;
float daf = da / 255.f;
float oa = a + daf * (1.f - a);
if (oa <= 0.f) return;
float rr = (sr*a + dr*daf*(1.f-a)) / oa;
float gg = (sg*a + dg*daf*(1.f-a)) / oa;
float bb = (sb*a + db*daf*(1.f-a)) / oa;
tb_image_set_pixel(im, x, y,
(uint8_t)(rr + 0.5f), (uint8_t)(gg + 0.5f),
(uint8_t)(bb + 0.5f), (uint8_t)(oa*255.f + 0.5f));
}
static void fill_rect(TinyBlocks_Image* im, double x, double y, double w, double h,
float r, float g, float b, float a) {
int x0 = (int)floor(x), y0 = (int)floor(y);
int x1 = (int)ceil(x + w), y1 = (int)ceil(y + h);
for (int j = y0; j < y1; j++)
for (int i = x0; i < x1; i++) blend(im, i, j, r, g, b, a);
}
static void fill_circle(TinyBlocks_Image* im, double cx, double cy, double rad,
float r, float g, float b, float a) {
int x0 = (int)floor(cx - rad), x1 = (int)ceil(cx + rad);
int y0 = (int)floor(cy - rad), y1 = (int)ceil(cy + rad);
double r2 = rad * rad;
for (int j = y0; j <= y1; j++)
for (int i = x0; i <= x1; i++) {
double dx = i + 0.5 - cx, dy = j + 0.5 - cy;
if (dx*dx + dy*dy <= r2) blend(im, i, j, r, g, b, a);
}
}
static void stroke_seg(TinyBlocks_Image* im, double x0, double y0, double x1, double y1,
double wdt, float r, float g, float b, float a) {
if (wdt < 1.0) wdt = 1.0;
double hw = wdt * 0.5;
int bx0 = (int)floor(fmin(x0, x1) - hw), bx1 = (int)ceil(fmax(x0, x1) + hw);
int by0 = (int)floor(fmin(y0, y1) - hw), by1 = (int)ceil(fmax(y0, y1) + hw);
double vx = x1 - x0, vy = y1 - y0;
double len2 = vx*vx + vy*vy;
for (int j = by0; j <= by1; j++)
for (int i = bx0; i <= bx1; i++) {
double px = i + 0.5 - x0, py = j + 0.5 - y0;
double t = len2 > 1e-12 ? (px*vx + py*vy) / len2 : 0.0;
if (t < 0) t = 0;
if (t > 1) t = 1;
double dx = px - t*vx, dy = py - t*vy;
if (dx*dx + dy*dy <= hw*hw) blend(im, i, j, r, g, b, a);
}
}
static void fill_poly(TinyBlocks_Image* im, const VgPt* p, int n,
float r, float g, float b, float a) {
if (n < 3) return;
double miny = p[0].y, maxy = p[0].y;
for (int i = 1; i < n; i++) {
if (p[i].y < miny) miny = p[i].y;
if (p[i].y > maxy) maxy = p[i].y;
}
int y0 = (int)floor(miny), y1 = (int)ceil(maxy);
double xs[VG_MAX_PTS];
for (int y = y0; y <= y1; y++) {
double yc = y + 0.5;
int nx = 0;
for (int i = 0; i < n; i++) {
const VgPt* a = &p[i];
const VgPt* b = &p[(i + 1) % n];
if ((a->y <= yc && b->y > yc) || (b->y <= yc && a->y > yc)) {
double t = (yc - a->y) / (b->y - a->y);
if (nx < VG_MAX_PTS) xs[nx++] = a->x + t * (b->x - a->x);
}
}
for (int i = 1; i < nx; i++) { /* insertion sort */
double v = xs[i]; int j = i - 1;
while (j >= 0 && xs[j] > v) { xs[j+1] = xs[j]; j--; }
xs[j+1] = v;
}
for (int i = 0; i + 1 < nx; i += 2)
for (int x = (int)floor(xs[i]); x <= (int)ceil(xs[i+1]); x++)
blend(im, x, y, r, g, b, a);
}
}
TinyBlocks_Image* tb_vg_render(const TinyBlocks_VectorData* v, int w, int h, float scale) {
if (!v || w <= 0 || h <= 0) return NULL;
if (scale <= 0.f) scale = 1.f;
TinyBlocks_Image* im = tb_image_new(w, h, 4);
if (!im) return NULL;
VgPt path[VG_MAX_PTS];
int pn = 0;
bool closed = false;
float cr = 0, cg = 0, cb = 0, ca = 0, cw = 1;
for (uint32_t i = 0; i < v->count; i++) {
const TinyBlocks_VCmd* c = &v->cmds[i];
switch (c->kind) {
case TB_VCMD_BEGIN_PATH: pn = 0; closed = false; break;
case TB_VCMD_MOVE_TO:
pn = 0; closed = false;
if (pn < VG_MAX_PTS) path[pn++] = (VgPt){ c->args[0]*scale, c->args[1]*scale };
break;
case TB_VCMD_LINE_TO:
if (pn < VG_MAX_PTS) path[pn++] = (VgPt){ c->args[0]*scale, c->args[1]*scale };
break;
case TB_VCMD_CLOSE_PATH: closed = true; break;
case TB_VCMD_STROKE:
cr = c->args[0]; cg = c->args[1]; cb = c->args[2]; ca = c->args[3];
cw = c->args[4] > 0 ? c->args[4] : 1;
for (int k = 0; k + 1 < pn; k++)
stroke_seg(im, path[k].x, path[k].y, path[k+1].x, path[k+1].y, cw*scale, cr, cg, cb, ca);
if (closed && pn >= 2)
stroke_seg(im, path[pn-1].x, path[pn-1].y, path[0].x, path[0].y, cw*scale, cr, cg, cb, ca);
pn = 0; closed = false;
break;
case TB_VCMD_FILL:
cr = c->args[0]; cg = c->args[1]; cb = c->args[2]; ca = c->args[3];
if (pn >= 3) {
fill_poly(im, path, pn, cr, cg, cb, ca);
if (closed)
for (int k = 0; k < pn; k++)
stroke_seg(im, path[k].x, path[k].y, path[(k+1)%pn].x, path[(k+1)%pn].y, scale, cr, cg, cb, ca);
}
pn = 0; closed = false;
break;
case TB_VCMD_RECT:
fill_rect(im, c->args[0]*scale, c->args[1]*scale, c->args[2]*scale, c->args[3]*scale, cr, cg, cb, ca);
break;
case TB_VCMD_CIRCLE:
fill_circle(im, c->args[0]*scale, c->args[1]*scale, c->args[2]*scale, cr, cg, cb, ca);
break;
}
}
return im;
}
/* -- Helpers de construcao ------------------------------------------------ */
TinyBlocks_VectorData* tb_vg_new(void) { return tb_vector_data_create(); }
bool tb_vg_line(TinyBlocks_VectorData* v, float x1, float y1, float x2, float y2,
float r, float g, float b, float a, float width) {
if (!v) return false;
tb_vector_fill(v, r, g, b, a); /* define a cor atual */
tb_vector_begin_path(v);
tb_vector_move_to(v, x1, y1);
tb_vector_line_to(v, x2, y2);
return tb_vector_stroke(v, r, g, b, a, width > 0 ? width : 1.f);
}
bool tb_vg_rect(TinyBlocks_VectorData* v, float x, float y, float w, float h,
float r, float g, float b, float a) {
if (!v) return false;
tb_vector_fill(v, r, g, b, a);
return tb_vector_rect(v, x, y, w, h);
}
bool tb_vg_circle(TinyBlocks_VectorData* v, float cx, float cy, float rad,
float r, float g, float b, float a) {
if (!v) return false;
tb_vector_fill(v, r, g, b, a);
return tb_vector_circle(v, cx, cy, rad);
}
bool tb_vg_polygon(TinyBlocks_VectorData* v, const double* xy, int n, bool closed,
float r, float g, float b, float a) {
if (!v || !xy || n < 2) return false;
tb_vector_fill(v, r, g, b, a); /* define a cor atual */
tb_vector_begin_path(v);
tb_vector_move_to(v, (float)xy[0], (float)xy[1]);
for (int i = 1; i < n; i++)
tb_vector_line_to(v, (float)xy[2*i], (float)xy[2*i+1]);
if (closed) tb_vector_close_path(v);
return tb_vector_fill(v, r, g, b, a);
}
/* -- Blocos -------------------------------------------------------------- */
static double in_real(TinyBlocks_Block* self, TinyBlocks* rt, uint32_t i) {
TinyBlocks_Value v = tb_block_get_input(self, rt, i);
if (v.kind == TB_VALUE_REAL) return v.as.real;
if (v.kind == TB_VALUE_INTEGER) return (double)v.as.integer;
return 0.0;
}
static int in_int(TinyBlocks_Block* self, TinyBlocks* rt, uint32_t i) { return (int)in_real(self, rt, i); }
static TinyBlocks_VectorData* in_vec(TinyBlocks_Block* self, TinyBlocks* rt, uint32_t i) {
TinyBlocks_Value v = tb_block_get_input(self, rt, i);
return v.kind == TB_VALUE_VECTOR ? v.as.vector : NULL;
}
/* Devolve o mesmo vetor (mutado), retendo-o para o output. */
static void out_vec(TinyBlocks_Block* self, TinyBlocks_VectorData* v) {
tb_block_set_output(self, 0, tb_value_vector(v));
self->dirty = false;
}
static float f01(double v) { return v < 0 ? 0.f : v > 1 ? 1.f : (float)v; }
static void eval_new(TinyBlocks_Block* self, TinyBlocks* rt) {
(void)rt;
TinyBlocks_VectorData* v = tb_vg_new();
out_vec(self, v);
tb_vector_data_release(v);
}
static void eval_line(TinyBlocks_Block* self, TinyBlocks* rt) {
TinyBlocks_VectorData* v = in_vec(self, rt, 0);
if (v) tb_vg_line(v, in_real(self,rt,1), in_real(self,rt,2), in_real(self,rt,3), in_real(self,rt,4),
f01(in_real(self,rt,5)), f01(in_real(self,rt,6)), f01(in_real(self,rt,7)), 1.f,
in_real(self,rt,8));
out_vec(self, v);
}
static void eval_rect(TinyBlocks_Block* self, TinyBlocks* rt) {
TinyBlocks_VectorData* v = in_vec(self, rt, 0);
if (v) tb_vg_rect(v, in_real(self,rt,1), in_real(self,rt,2), in_real(self,rt,3), in_real(self,rt,4),
f01(in_real(self,rt,5)), f01(in_real(self,rt,6)), f01(in_real(self,rt,7)), 1.f);
out_vec(self, v);
}
static void eval_circle(TinyBlocks_Block* self, TinyBlocks* rt) {
TinyBlocks_VectorData* v = in_vec(self, rt, 0);
if (v) tb_vg_circle(v, in_real(self,rt,1), in_real(self,rt,2), in_real(self,rt,3),
f01(in_real(self,rt,4)), f01(in_real(self,rt,5)), f01(in_real(self,rt,6)), 1.f);
out_vec(self, v);
}
/* VG.Render (vetor, largura, altura, escala) -> imagem */
static void eval_render(TinyBlocks_Block* self, TinyBlocks* rt) {
TinyBlocks_VectorData* v = in_vec(self, rt, 0);
TinyBlocks_Image* im = tb_vg_render(v, in_int(self,rt,1), in_int(self,rt,2), (float)in_real(self,rt,3));
if (!im) { self->dirty = false; return; }
tb_block_set_output(self, 0, tb_value_image(im));
tb_image_release(im);
self->dirty = false;
}
/* VG.Polygon (vetor, pontos JSON, fechar?) -> vetor */
static void eval_polygon(TinyBlocks_Block* self, TinyBlocks* rt) {
TinyBlocks_VectorData* v = in_vec(self, rt, 0);
TinyBlocks_Value pv = tb_block_get_input(self, rt, 1);
if (v && pv.kind == TB_VALUE_JSON && pv.as.json) {
int n = (int)tb_json_array_length(pv.as.json);
double* xy = (double*)malloc(sizeof(double) * (size_t)n * 2);
int m = 0;
for (int i = 0; i < n; i++) {
TinyBlocks_JsonValue* p = tb_json_array_get(pv.as.json, (uint32_t)i);
if (p && tb_json_kind(p) == TB_JSON_ARRAY && tb_json_array_length(p) >= 2) {
TinyBlocks_JsonValue* jx = tb_json_array_get(p, 0);
TinyBlocks_JsonValue* jy = tb_json_array_get(p, 1);
xy[m++] = tb_json_as_number(jx, 0);
xy[m++] = tb_json_as_number(jy, 0);
tb_json_release(jx); tb_json_release(jy);
}
if (p) tb_json_release(p);
}
if (m >= 4)
tb_vg_polygon(v, xy, m/2, in_int(self, rt, 2) != 0,
f01(in_real(self,rt,3)), f01(in_real(self,rt,4)), f01(in_real(self,rt,5)), 1.f);
free(xy);
}
out_vec(self, v);
}
static bool reg(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib, const char* name,
const TinyBlocks_SlotDesc* in, uint32_t ni,
const TinyBlocks_SlotDesc* out, uint32_t no,
void (*fn)(TinyBlocks_Block*, TinyBlocks*)) {
TinyBlocks_TypeBlock* t = tb_type_block_create(rt, name, in, ni, out, no, fn);
return t && tb_library_add(lib, &t->base);
}
static bool vg_init(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
static const TinyBlocks_Category cat = {
"Vetor",
{ 171/255.f, 71/255.f, 188/255.f, 1.f},
{ 123/255.f, 31/255.f, 162/255.f, 1.f},
};
tb_library_set_category(lib, &cat);
static const TinyBlocks_SlotDesc out_vec[] = {{"vetor", TB_VALUE_VECTOR}};
static const TinyBlocks_SlotDesc out_img[] = {{"imagem", TB_VALUE_IMAGE}};
static const TinyBlocks_SlotDesc line_in[] = {{"vetor",TB_VALUE_VECTOR},{"x1",TB_VALUE_REAL},{"y1",TB_VALUE_REAL},
{"x2",TB_VALUE_REAL},{"y2",TB_VALUE_REAL},
{"r",TB_VALUE_REAL},{"g",TB_VALUE_REAL},{"b",TB_VALUE_REAL},{"espessura",TB_VALUE_REAL}};
static const TinyBlocks_SlotDesc rect_in[] = {{"vetor",TB_VALUE_VECTOR},{"x",TB_VALUE_REAL},{"y",TB_VALUE_REAL},
{"largura",TB_VALUE_REAL},{"altura",TB_VALUE_REAL},
{"r",TB_VALUE_REAL},{"g",TB_VALUE_REAL},{"b",TB_VALUE_REAL}};
static const TinyBlocks_SlotDesc circ_in[] = {{"vetor",TB_VALUE_VECTOR},{"cx",TB_VALUE_REAL},{"cy",TB_VALUE_REAL},{"raio",TB_VALUE_REAL},
{"r",TB_VALUE_REAL},{"g",TB_VALUE_REAL},{"b",TB_VALUE_REAL}};
static const TinyBlocks_SlotDesc rend_in[] = {{"vetor",TB_VALUE_VECTOR},{"largura",TB_VALUE_INTEGER},{"altura",TB_VALUE_INTEGER},{"escala",TB_VALUE_REAL}};
static const TinyBlocks_SlotDesc poly_in[] = {{"vetor",TB_VALUE_VECTOR},{"pontos",TB_VALUE_JSON},{"fechar",TB_VALUE_INTEGER},
{"r",TB_VALUE_REAL},{"g",TB_VALUE_REAL},{"b",TB_VALUE_REAL}};
return reg(rt, lib, "VG.New", NULL, 0, out_vec, 1, eval_new)
&& reg(rt, lib, "VG.Line", line_in, 9, out_vec, 1, eval_line)
&& reg(rt, lib, "VG.Rect", rect_in, 8, out_vec, 1, eval_rect)
&& reg(rt, lib, "VG.Circle", circ_in, 7, out_vec, 1, eval_circle)
&& reg(rt, lib, "VG.Polygon",poly_in, 6, out_vec, 1, eval_polygon)
&& reg(rt, lib, "VG.Render", rend_in, 4, out_img, 1, eval_render);
}
TINYBLOCKS_DEFINE_LIBRARY(vg, "tinyblocks-vg", "1.0.0", vg_init, NULL)
const TinyBlocks_LibraryDescriptor* tb_vg_get_library(void) {
return &_tb_lib_desc_vg;
}