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

@@ -258,6 +258,33 @@ static const BlockSpec s_specs[] = {
{"PPM.Height", "altura de %1", NULL},
{"PPM.SetPixel", "pinte %1 em (%2,%3) de r %4 g %5 b %6", "|0|0|0|0|0"},
{"PPM.GetPixel", "cor de %1 em (%2,%3)", "|0|0"},
{"BMP.Load", "carregue o BMP %1", "imagem.bmp"},
{"BMP.Save", "grave o BMP %1 em %2", "|saida.bmp"},
{"PNG.Load", "carregue o PNG %1", "imagem.png"},
{"PNG.Save", "grave o PNG %1 em %2", "|saida.png"},
/* Imagem (ops genericas) */
{"Image.New", "nova imagem(%1 x %2) cor r %3 g %4 b %5", "32|32|255|0|0"},
{"Image.Width", "largura da imagem %1", NULL},
{"Image.Height", "altura da imagem %1", NULL},
{"Image.GetPixel", "cor da imagem %1 em (%2,%3)", "|0|0"},
{"Image.SetPixel", "pinte a imagem %1 em (%2,%3) com r %4 g %5 b %6", "|0|0|0|0|0"},
{"Image.Fill", "preencha a imagem %1 com r %2 g %3 b %4", "|255|255|255"},
{"Image.Clone", "copie a imagem %1", NULL},
{"Image.Crop", "recorte da imagem %1 em (%2,%3) tamanho %4 x %5", "|0|0|16|16"},
{"Image.Scale", "escale a imagem %1 para %2 x %3", "|64|64"},
{"Image.Load", "carregue a imagem %1", "imagem.png"},
{"Image.Save", "grave a imagem %1 em %2", "|saida.png"},
/* Vetor (vg) */
{"VG.New", "novo desenho vetorial", NULL},
{"VG.Line", "linha do vetor %1 de (%2,%3) a (%4,%5) cor r %6 g %7 b %8 esp %9", "|0|0|50|50|0|0|0|2"},
{"VG.Rect", "retangulo no vetor %1 em (%2,%3) %4 x %5 cor r %6 g %7 b %8", "|0|0|40|40|255|0|0"},
{"VG.Circle", "circulo no vetor %1 em (%2,%3) raio %4 cor r %5 g %6 b %7", "|20|20|15|0|0|255"},
{"VG.Polygon", "poligono no vetor %1 pontos %2 fechar %3 cor r %4 g %5 b %6", "||1|0|255|0"},
{"VG.Render", "rasterize o vetor %1 em %2 x %3 escala %4", "|64|64|1"},
/* SVG */
{"SVG.Parse", "interprete o SVG %1", NULL},
{"SVG.Render", "desenhe o SVG %1 em %2 x %3", "|64|64"},
{"SVG.Load", "carregue o SVG %1", "desenho.svg"},
};
/* ===
+34
View File
@@ -0,0 +1,34 @@
# Makefile da library svg (categoria "SVG"); depende de vg e base64.
CC := gcc
BUILD := build
VG_LIB := ../tinyblocks-vg/build
B64_LIB := ../tinyblocks-base64/build
CFLAGS := -std=c11 -Wall -Wextra -pedantic -fvisibility=hidden \
-D_POSIX_C_SOURCE=200809L -I../../include -Iinclude \
-I../tinyblocks-vg/include -I../tinyblocks-base64/include
OBJ := $(BUILD)/tb_svg.o
LIB_A := $(BUILD)/libtinyblocks-svg.a
LIB_SO := $(BUILD)/libtinyblocks-svg.so
.PHONY: all static shared clean
all: static shared
static: $(LIB_A)
shared: $(LIB_SO)
$(OBJ): src/tb_svg.c include/tb_svg.h
@mkdir -p $(BUILD)
$(CC) $(CFLAGS) -fPIC -c $< -o $@
$(LIB_A): $(OBJ)
ar rcs $@ $^
$(LIB_SO): $(OBJ) $(VG_LIB)/libtinyblocks-vg.so $(B64_LIB)/libtinyblocks-base64.so
$(CC) -shared -o $@ $< -L$(VG_LIB) -ltinyblocks-vg \
-L$(B64_LIB) -ltinyblocks-base64 \
-Wl,-rpath,'$$ORIGIN' -lm
clean:
$(RM) -r $(BUILD)
+22
View File
@@ -0,0 +1,22 @@
/*
* TinyBlocks - tb_svg.h
* Library tinyblocks-svg: le um subconjunto de SVG (retangulos, circulos,
* linhas, polilinhas/poligonos e paths com M/L/H/V/C/Q/Z) e produz um desenho
* vetorial (tinyblocks-vg) ou uma imagem rasterizada (tinyblocks-image).
* Suporta <image> com data URI base64 (PPM) via tinyblocks-base64.
*/
#ifndef TINYBLOCKS_SVG_H
#define TINYBLOCKS_SVG_H
#include <tinyblocks/tinyblocks.h>
TB_API const TinyBlocks_LibraryDescriptor* tb_svg_get_library(void);
/* Interpreta o SVG e devolve o desenho vetorial (TB_VALUE_VECTOR).
* out_w/out_h recebem as dimensoes intrinsecas declaradas no SVG. */
TB_API TinyBlocks_VectorData* tb_svg_parse(const char* svg, int* out_w, int* out_h);
/* Interpreta e rasteriza para uma imagem w x h (ajustando a escala). */
TB_API TinyBlocks_Image* tb_svg_render(const char* svg, int w, int h);
TB_API const char* tb_svg_last_error(void);
#endif /* TINYBLOCKS_SVG_H */
+15
View File
@@ -0,0 +1,15 @@
# Metadados da library svg. Lido pela UI/build sem recompilar o .so.
[library]
name = "tinyblocks-svg"
version = "1.0.0"
category = "SVG"
description = "SVG (subconjunto) -> desenho vetorial ou imagem"
color = [255, 213, 79, 255]
dark = [255, 160, 0, 255]
[build]
sources = ["src/tb_svg.c"]
includes = ["include"]
shared = "build/libtinyblocks-svg.so"
static = "build/libtinyblocks-svg.a"
depends = ["vg", "image", "base64"]
+529
View File
@@ -0,0 +1,529 @@
/*
* TinyBlocks - tb_svg.c
* Categoria "SVG": SVG.Load / SVG.Parse / SVG.Render. Subconjunto de SVG:
* rect, circle, ellipse, line, polyline, polygon, path (M/L/H/V/C/Q/Z) e
* image com data URI base64 (PPM). Preenchimento e contorno por cores.
*/
#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#include <tinyblocks/tinyblocks.h>
#include <tb_base64.h>
#include <tb_vg.h>
#include "tb_svg.h"
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
static char s_err[200] = "";
const char* tb_svg_last_error(void) { return s_err; }
/* -- Utilitarios de parsing ---------------------------------------------- */
static void skip_ws(const char** p) {
while (**p && (isspace((unsigned char)**p) || **p == ',')) (*p)++;
}
static double read_num(const char** p, bool* ok) {
skip_ws(p);
char* end = NULL;
double v = strtod(*p, &end);
if (end == *p) { if (ok) *ok = false; return 0; }
*p = end;
if (ok) *ok = true;
return v;
}
/* Copia o valor do atributo name de uma tag XML (ja' sem '<'/'>'). */
static bool attr(const char* tag, size_t len, const char* name, char* out, size_t cap) {
size_t nl = strlen(name);
for (size_t i = 0; i + nl + 1 < len; i++) {
if ((i == 0 || isspace((unsigned char)tag[i-1]) || tag[i-1] == '<') &&
strncmp(tag + i, name, nl) == 0) {
size_t j = i + nl;
while (j < len && isspace((unsigned char)tag[j])) j++;
if (j >= len || tag[j] != '=') continue;
j++;
while (j < len && isspace((unsigned char)tag[j])) j++;
if (j >= len) return false;
char q = tag[j];
if (q == '"' || q == '\'') {
j++;
size_t k = 0;
while (j < len && tag[j] != q && k + 1 < cap) out[k++] = tag[j++];
out[k] = 0;
return true;
}
}
}
return false;
}
static int hexd(int c) {
if (c >= '0' && c <= '9') return c - '0';
if (c >= 'a' && c <= 'f') return c - 'a' + 10;
if (c >= 'A' && c <= 'F') return c - 'A' + 10;
return -1;
}
static bool parse_color(const char* s, float* r, float* g, float* b, float* a) {
if (!s || !s[0] || strcmp(s, "none") == 0 || strcmp(s, "transparent") == 0) return false;
*a = 1.f;
if (s[0] == '#') {
size_t n = strlen(s + 1);
if (n >= 6) {
int rr = hexd(s[1])*16 + hexd(s[2]);
int gg = hexd(s[3])*16 + hexd(s[4]);
int bb = hexd(s[5])*16 + hexd(s[6]);
if (rr < 0 || gg < 0 || bb < 0) return false;
*r = rr/255.f; *g = gg/255.f; *b = bb/255.f; return true;
} else if (n >= 3) {
int rr = hexd(s[1]), gg = hexd(s[2]), bb = hexd(s[3]);
if (rr < 0 || gg < 0 || bb < 0) return false;
*r = (rr*17)/255.f; *g = (gg*17)/255.f; *b = (bb*17)/255.f; return true;
}
return false;
}
if (strncmp(s, "rgb(", 4) == 0) {
int rr=0, gg=0, bb=0;
sscanf(s + 4, "%d , %d , %d", &rr, &gg, &bb);
*r = rr/255.f; *g = gg/255.f; *b = bb/255.f; return true;
}
struct { const char* n; int r, g, b; } names[] = {
{"black",0,0,0},{"white",255,255,255},{"red",255,0,0},{"green",0,128,0},
{"lime",0,255,0},{"blue",0,0,255},{"yellow",255,255,0},{"cyan",0,255,255},
{"magenta",255,0,255},{"gray",128,128,128},{"grey",128,128,128},
{"orange",255,165,0},{"purple",128,0,128},{"pink",255,192,203},
{"brown",165,42,42},{"navy",0,0,128},{"teal",0,128,128},{"silver",192,192,192},
{"gold",255,215,0},{"violet",238,130,238},{"indigo",75,0,130},
};
for (size_t i = 0; i < sizeof(names)/sizeof(names[0]); i++)
if (strcmp(s, names[i].n) == 0) {
*r = names[i].r/255.f; *g = names[i].g/255.f; *b = names[i].b/255.f; return true;
}
return false;
}
/* -- Cotas de imagens embutidas ------------------------------------------ */
typedef struct { double x, y, w, h; TinyBlocks_Image* img; } SvgImg;
#define SVG_MAX_IMGS 16
static TinyBlocks_Image* decode_data_uri(const char* uri) {
const char* b64 = strstr(uri, "base64,");
if (!b64) return NULL;
b64 += 7;
size_t n = 0;
unsigned char* raw = tb_base64_decode(b64, &n);
if (!raw) return NULL;
TinyBlocks_Image* img = NULL;
if (n > 2 && raw[0] == 'P' && (raw[1] == '6' || raw[1] == '3')) {
/* PPM P6/P3 simples */
int w=0, h=0, maxv=0, i=2;
int vals[3] = {0,0,0}, vi = 0;
while (vi < 3 && i < (int)n) {
while (i < (int)n && (isspace(raw[i]))) i++;
if (i < (int)n && raw[i] == '#') { while (i < (int)n && raw[i] != '\n') i++; continue; }
int v = 0;
while (i < (int)n && isdigit(raw[i])) { v = v*10 + (raw[i]-'0'); i++; }
vals[vi++] = v;
}
w = vals[0]; h = vals[1]; maxv = vals[2];
(void)maxv;
if (w > 0 && h > 0) {
img = tb_image_new(w, h, 3);
if (img) {
while (i < (int)n && isspace(raw[i])) i++;
if (raw[1] == '6') {
for (size_t k = 0; k < (size_t)w*h*3 && i < (int)n; k++)
tb_image_data(img)[k] = raw[i++];
} else {
for (size_t k = 0; k < (size_t)w*h*3 && i < (int)n; k++) {
int v = 0;
while (i < (int)n && isspace(raw[i])) i++;
while (i < (int)n && isdigit(raw[i])) v = v*10 + (raw[i++]-'0');
tb_image_data(img)[k] = (uint8_t)v;
}
}
}
}
}
free(raw);
return img;
}
/* -- Parsing de shapes ---------------------------------------------------- */
typedef struct { double* xy; int n, cap; } Pts;
static void pts_add(Pts* p, double x, double y) {
if (p->n * 2 + 2 > p->cap) {
int cap = p->cap ? p->cap * 2 : 32;
double* np = (double*)realloc(p->xy, sizeof(double) * (size_t)cap * 2);
if (!np) return;
p->xy = np; p->cap = cap;
}
p->xy[2*p->n] = x;
p->xy[2*p->n + 1] = y;
p->n++;
}
static void pts_free(Pts* p) { free(p->xy); p->xy = NULL; p->n = p->cap = 0; }
/* Le lista "x,y x,y ..." para Pts */
static void parse_points(const char* s, Pts* p) {
const char* q = s;
for (;;) {
bool ok1 = false, ok2 = false;
double x = read_num(&q, &ok1);
double y = read_num(&q, &ok2);
if (!ok1 || !ok2) break;
pts_add(p, x, y);
const char* save = q;
skip_ws(&q);
if (!*q) break;
if (!isdigit((unsigned char)*q) && *q != '-' && *q != '+' && *q != '.') { q = save; break; }
}
}
/* Aproxima uma Bezier cubica em segmentos (adiciona em Pts) */
static void sample_cubic(Pts* p, double x0,double y0,double x1,double y1,
double x2,double y2,double x3,double y3) {
int steps = 16;
for (int i = 1; i <= steps; i++) {
double t = (double)i/steps, u = 1-t;
double b0=u*u*u, b1=3*u*u*t, b2=3*u*t*t, b3=t*t*t;
pts_add(p, b0*x0+b1*x1+b2*x2+b3*x3, b0*y0+b1*y1+b2*y2+b3*y3);
}
}
static void sample_quad(Pts* p, double x0,double y0,double cx,double cy,double x1,double y1) {
int steps = 12;
for (int i = 1; i <= steps; i++) {
double t = (double)i/steps, u = 1-t;
pts_add(p, u*u*x0 + 2*u*t*cx + t*t*x1, u*u*y0 + 2*u*t*cy + t*t*y1);
}
}
/* Interpreta "d" de um <path> adicionando pontos e tracando contornos. */
static void parse_path(TinyBlocks_VectorData* v, const char* d, bool do_fill, bool do_stroke,
float r, float g, float b, float sw, float scale) {
Pts poly = {0};
const char* p = d;
char cmd = 0;
double cx = 0, cy = 0, sx = 0, sy = 0;
bool started = false;
while (*p) {
skip_ws(&p);
if (!*p) break;
if (isalpha((unsigned char)*p)) { cmd = *p++; skip_ws(&p); }
else if (!cmd) break;
bool rel = islower((unsigned char)cmd);
char C = (char)toupper((unsigned char)cmd);
bool ok = true;
if (C == 'M' || C == 'L') {
double x = read_num(&p,&ok), y = read_num(&p,&ok);
if (!ok) break;
if (rel) { x += cx; y += cy; }
if (C == 'M' && !started) { sx = x; sy = y; }
if (C == 'M') { started = true; }
if (do_stroke && C == 'L' && poly.n >= 1)
tb_vg_line(v, (float)(poly.xy[2*(poly.n-1)]*scale), (float)(poly.xy[2*(poly.n-1)+1]*scale),
(float)(x*scale), (float)(y*scale), r,g,b,1, sw);
pts_add(&poly, x, y);
cx = x; cy = y;
if (C == 'M') cmd = rel ? 'l' : 'L';
} else if (C == 'H') {
double x = read_num(&p,&ok); if (!ok) break;
if (rel) x += cx;
if (do_stroke && poly.n >= 1)
tb_vg_line(v, (float)(poly.xy[2*(poly.n-1)]*scale), (float)(poly.xy[2*(poly.n-1)+1]*scale),
(float)(x*scale), (float)(cy*scale), r,g,b,1, sw);
pts_add(&poly, x, cy); cx = x;
} else if (C == 'V') {
double y = read_num(&p,&ok); if (!ok) break;
if (rel) y += cy;
if (do_stroke && poly.n >= 1)
tb_vg_line(v, (float)(poly.xy[2*(poly.n-1)]*scale), (float)(poly.xy[2*(poly.n-1)+1]*scale),
(float)(cx*scale), (float)(y*scale), r,g,b,1, sw);
pts_add(&poly, cx, y); cy = y;
} else if (C == 'C') {
double x1=read_num(&p,&ok), y1=read_num(&p,&ok);
double x2=read_num(&p,&ok), y2=read_num(&p,&ok);
double x=read_num(&p,&ok), y=read_num(&p,&ok);
if (!ok) break;
if (rel) { x1+=cx; y1+=cy; x2+=cx; y2+=cy; x+=cx; y+=cy; }
sample_cubic(&poly, cx, cy, x1,y1, x2,y2, x,y);
cx = x; cy = y;
} else if (C == 'Q') {
double x1=read_num(&p,&ok), y1=read_num(&p,&ok);
double x=read_num(&p,&ok), y=read_num(&p,&ok);
if (!ok) break;
if (rel) { x1+=cx; y1+=cy; x+=cx; y+=cy; }
sample_quad(&poly, cx, cy, x1,y1, x,y);
cx = x; cy = y;
} else if (C == 'Z') {
if (do_fill && poly.n >= 3 && started) {
double* out = (double*)malloc(sizeof(double)*2*(size_t)poly.n);
for (int i = 0; i < poly.n; i++) { out[2*i] = poly.xy[2*i]*scale; out[2*i+1] = poly.xy[2*i+1]*scale; }
tb_vg_polygon(v, out, poly.n, true, r,g,b,1);
free(out);
}
pts_free(&poly);
cx = sx; cy = sy;
} else {
break;
}
}
/* fill para paths sem 'Z' */
if (do_fill && poly.n >= 3) {
double* out = (double*)malloc(sizeof(double)*2*(size_t)poly.n);
for (int i = 0; i < poly.n; i++) { out[2*i] = poly.xy[2*i]*scale; out[2*i+1] = poly.xy[2*i+1]*scale; }
tb_vg_polygon(v, out, poly.n, true, r,g,b,1);
free(out);
}
pts_free(&poly);
}
/* -- Parse principal ------------------------------------------------------ */
static TinyBlocks_VectorData* svg_parse_internal(const char* svg, int* out_w, int* out_h,
SvgImg* imgs, int* img_n) {
s_err[0] = 0;
if (out_w) *out_w = 0;
if (out_h) *out_h = 0;
TinyBlocks_VectorData* v = tb_vector_data_create();
if (!v) { snprintf(s_err, sizeof(s_err), "sem memoria"); return NULL; }
if (!svg) return v;
const char* p = svg;
while ((p = strchr(p, '<')) != NULL) {
if (p[1] == '/' || p[1] == '!' || p[1] == '?') { p++; continue; }
const char* end = strchr(p, '>');
if (!end) break;
size_t len = (size_t)(end - p + 1);
char nm[24] = "";
size_t k = 1;
while (k < len && !isspace((unsigned char)p[k]) && p[k] != '>' && p[k] != '/') {
if (k - 1 < sizeof(nm) - 1) nm[k-1] = p[k];
k++;
}
char val[512], val2[512], val3[512];
if (strcmp(nm, "svg") == 0) {
char wbuf[32] = "", hbuf[32] = "";
if (attr(p, len, "width", wbuf, sizeof(wbuf)) && out_w) *out_w = atoi(wbuf);
if (attr(p, len, "height", hbuf, sizeof(hbuf)) && out_h) *out_h = atoi(hbuf);
} else if (strcmp(nm, "rect") == 0) {
double x = attr(p,len,"x",val,sizeof(val)) ? atof(val) : 0;
double y = attr(p,len,"y",val,sizeof(val)) ? atof(val) : 0;
double w = attr(p,len,"width",val,sizeof(val)) ? atof(val) : 0;
double h = attr(p,len,"height",val,sizeof(val)) ? atof(val) : 0;
float cr,cg,cb,ca;
if (attr(p,len,"fill",val,sizeof(val)) && parse_color(val,&cr,&cg,&cb,&ca))
tb_vg_rect(v, (float)x,(float)y,(float)w,(float)h, cr,cg,cb,ca);
if (attr(p,len,"stroke",val,sizeof(val)) && parse_color(val,&cr,&cg,&cb,&ca)) {
double sw = attr(p,len,"stroke-width",val2,sizeof(val2)) ? atof(val2) : 1;
tb_vg_line(v,(float)x,(float)y,(float)(x+w),(float)y, cr,cg,cb,ca,(float)sw);
tb_vg_line(v,(float)(x+w),(float)y,(float)(x+w),(float)(y+h), cr,cg,cb,ca,(float)sw);
tb_vg_line(v,(float)(x+w),(float)(y+h),(float)x,(float)(y+h), cr,cg,cb,ca,(float)sw);
tb_vg_line(v,(float)x,(float)(y+h),(float)x,(float)y, cr,cg,cb,ca,(float)sw);
}
} else if (strcmp(nm, "circle") == 0) {
double x = attr(p,len,"cx",val,sizeof(val)) ? atof(val) : 0;
double y = attr(p,len,"cy",val,sizeof(val)) ? atof(val) : 0;
double r = attr(p,len,"r",val,sizeof(val)) ? atof(val) : 0;
float cr,cg,cb,ca;
if (attr(p,len,"fill",val,sizeof(val)) && parse_color(val,&cr,&cg,&cb,&ca))
tb_vg_circle(v,(float)x,(float)y,(float)r, cr,cg,cb,ca);
} else if (strcmp(nm, "ellipse") == 0) {
double x = attr(p,len,"cx",val,sizeof(val)) ? atof(val) : 0;
double y = attr(p,len,"cy",val,sizeof(val)) ? atof(val) : 0;
double rx = attr(p,len,"rx",val,sizeof(val)) ? atof(val) : 0;
double ry = attr(p,len,"ry",val2,sizeof(val2)) ? atof(val2) : 0;
float cr,cg,cb,ca;
if (attr(p,len,"fill",val,sizeof(val)) && parse_color(val,&cr,&cg,&cb,&ca) && rx>0 && ry>0) {
Pts pts = {0};
for (int i = 0; i < 40; i++) {
double a = 2*M_PI*i/40;
pts_add(&pts, x + rx*cos(a), y + ry*sin(a));
}
tb_vg_polygon(v, pts.xy, pts.n, true, cr,cg,cb,ca);
pts_free(&pts);
}
} else if (strcmp(nm, "line") == 0) {
double x1 = attr(p,len,"x1",val,sizeof(val)) ? atof(val) : 0;
double y1 = attr(p,len,"y1",val,sizeof(val)) ? atof(val) : 0;
double x2 = attr(p,len,"x2",val,sizeof(val)) ? atof(val) : 0;
double y2 = attr(p,len,"y2",val,sizeof(val)) ? atof(val) : 0;
float cr=0,cg=0,cb=0,ca=1;
parse_color(attr(p,len,"stroke",val,sizeof(val)) ? val : "black", &cr,&cg,&cb,&ca);
double sw = attr(p,len,"stroke-width",val2,sizeof(val2)) ? atof(val2) : 1;
tb_vg_line(v,(float)x1,(float)y1,(float)x2,(float)y2, cr,cg,cb,ca,(float)sw);
} else if (strcmp(nm, "polyline") == 0 || strcmp(nm, "polygon") == 0) {
if (attr(p,len,"points",val,sizeof(val))) {
Pts pts = {0};
parse_points(val, &pts);
float cr,cg,cb,ca;
bool stroke = attr(p,len,"stroke",val2,sizeof(val2)) && parse_color(val2,&cr,&cg,&cb,&ca);
bool fill = attr(p,len,"fill",val3,sizeof(val3)) && parse_color(val3,&cr,&cg,&cb,&ca);
if (fill && pts.n >= 3)
tb_vg_polygon(v, pts.xy, pts.n, true, cr,cg,cb,ca);
if (stroke && pts.n >= 2) {
double sw = attr(p,len,"stroke-width",val,sizeof(val)) ? atof(val) : 1;
for (int i = 0; i + 1 < pts.n; i++)
tb_vg_line(v,(float)pts.xy[2*i],(float)pts.xy[2*i+1],
(float)pts.xy[2*i+2],(float)pts.xy[2*i+3], cr,cg,cb,ca,(float)sw);
}
pts_free(&pts);
}
} else if (strcmp(nm, "path") == 0) {
if (attr(p,len,"d",val,sizeof(val))) {
float cr,cg,cb,ca;
bool fill = attr(p,len,"fill",val2,sizeof(val2)) && parse_color(val2,&cr,&cg,&cb,&ca);
bool stroke = attr(p,len,"stroke",val3,sizeof(val3)) && parse_color(val3,&cr,&cg,&cb,&ca);
double sw = attr(p,len,"stroke-width",val,sizeof(val)) ? atof(val) : 1;
parse_path(v, val, fill, stroke, cr, cg, cb, (float)sw, 1.0f);
}
} else if (strcmp(nm, "image") == 0 && imgs && img_n && *img_n < SVG_MAX_IMGS) {
char href[1024];
if ((attr(p,len,"href",href,sizeof(href)) || attr(p,len,"xlink:href",href,sizeof(href))) &&
strstr(href, "base64,")) {
TinyBlocks_Image* im = decode_data_uri(href);
if (im) {
SvgImg* si = &imgs[*img_n];
si->x = attr(p,len,"x",val,sizeof(val)) ? atof(val) : 0;
si->y = attr(p,len,"y",val,sizeof(val)) ? atof(val) : 0;
si->w = attr(p,len,"width",val,sizeof(val)) ? atof(val) : tb_image_width(im);
si->h = attr(p,len,"height",val,sizeof(val)) ? atof(val) : tb_image_height(im);
si->img = im;
(*img_n)++;
}
}
}
p = end + 1;
}
return v;
}
TinyBlocks_VectorData* tb_svg_parse(const char* svg, int* out_w, int* out_h) {
return svg_parse_internal(svg, out_w, out_h, NULL, NULL);
}
TinyBlocks_Image* tb_svg_render(const char* svg, int w, int h) {
if (w <= 0 || h <= 0) return NULL;
int sw = 0, sh = 0;
SvgImg imgs[SVG_MAX_IMGS];
int img_n = 0;
memset(imgs, 0, sizeof(imgs));
TinyBlocks_VectorData* v = svg_parse_internal(svg, &sw, &sh, imgs, &img_n);
if (!v) return NULL;
float scale = 1.f;
if (sw > 0 && sh > 0) {
float fx = (float)w / sw, fy = (float)h / sh;
scale = fx < fy ? fx : fy;
}
TinyBlocks_Image* im = tb_vg_render(v, w, h, scale);
/* composicao das imagens embutidas */
if (im) {
for (int i = 0; i < img_n; i++) {
SvgImg* si = &imgs[i];
int iw = (int)(si->w * scale), ih = (int)(si->h * scale);
if (iw <= 0 || ih <= 0) continue;
for (int y = 0; y < ih; y++)
for (int x = 0; x < iw; x++) {
int dx = (int)(si->x * scale) + x, dy = (int)(si->y * scale) + y;
int sxp = x * tb_image_width(si->img) / iw;
int syp = y * tb_image_height(si->img) / ih;
uint8_t r,g,b,a;
if (tb_image_get_pixel(si->img, sxp, syp, &r,&g,&b,&a) && a > 0)
tb_image_set_pixel(im, dx, dy, r,g,b,a);
}
}
}
for (int i = 0; i < img_n; i++) tb_image_release(imgs[i].img);
tb_vector_data_release(v);
return im;
}
/* -- Blocos -------------------------------------------------------------- */
static const char* in_text(TinyBlocks_Block* self, TinyBlocks* rt, uint32_t i) {
TinyBlocks_Value v = tb_block_get_input(self, rt, i);
return (v.kind == TB_VALUE_TEXT && v.as.text) ? v.as.text : "";
}
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 void eval_parse(TinyBlocks_Block* self, TinyBlocks* rt) {
TinyBlocks_VectorData* v = tb_svg_parse(in_text(self, rt, 0), NULL, NULL);
tb_block_set_output(self, 0, tb_value_vector(v));
tb_vector_data_release(v);
self->dirty = false;
}
static void eval_render(TinyBlocks_Block* self, TinyBlocks* rt) {
TinyBlocks_Image* im = tb_svg_render(in_text(self, rt, 0), in_int(self, rt, 1), in_int(self, rt, 2));
if (!im) { tinyblocks_set_error(rt, "%s", tb_svg_last_error()); self->dirty = false; return; }
tb_block_set_output(self, 0, tb_value_image(im));
tb_image_release(im);
self->dirty = false;
}
static void eval_load(TinyBlocks_Block* self, TinyBlocks* rt) {
const char* path = in_text(self, rt, 0);
FILE* f = path && path[0] ? fopen(path, "rb") : NULL;
if (!f) { tinyblocks_set_error(rt, "nao abriu '%s'", path); self->dirty = false; return; }
fseek(f, 0, SEEK_END); long n = ftell(f); fseek(f, 0, SEEK_SET);
char* buf = (char*)malloc((size_t)n + 1);
if (!buf) { fclose(f); self->dirty = false; return; }
size_t rd = fread(buf, 1, (size_t)n, f);
buf[rd] = 0; fclose(f);
int sw = 0, sh = 0;
TinyBlocks_VectorData* v = tb_svg_parse(buf, &sw, &sh);
free(buf);
tb_block_set_output(self, 0, tb_value_vector(v));
tb_vector_data_release(v);
self->dirty = false;
}
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 svg_init(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
static const TinyBlocks_Category cat = {
"SVG",
{ 255/255.f, 213/255.f, 79/255.f, 1.f},
{ 255/255.f, 160/255.f, 0/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 parse_in[] = {{"svg", TB_VALUE_TEXT}};
static const TinyBlocks_SlotDesc rend_in[] = {{"svg", TB_VALUE_TEXT},{"largura",TB_VALUE_INTEGER},{"altura",TB_VALUE_INTEGER}};
static const TinyBlocks_SlotDesc load_in[] = {{"caminho", TB_VALUE_TEXT}};
return reg(rt, lib, "SVG.Parse", parse_in, 1, out_vec, 1, eval_parse)
&& reg(rt, lib, "SVG.Render", rend_in, 3, out_img, 1, eval_render)
&& reg(rt, lib, "SVG.Load", load_in, 1, out_vec, 1, eval_load);
}
TINYBLOCKS_DEFINE_LIBRARY(svg, "tinyblocks-svg", "1.0.0", svg_init, NULL)
const TinyBlocks_LibraryDescriptor* tb_svg_get_library(void) {
return &_tb_lib_desc_svg;
}
+28
View File
@@ -0,0 +1,28 @@
# Makefile da library vg (categoria "Vetor")
CC := gcc
BUILD := build
CFLAGS := -std=c11 -Wall -Wextra -pedantic -fvisibility=hidden \
-D_POSIX_C_SOURCE=200809L -I../../include -Iinclude
OBJ := $(BUILD)/tb_vg.o
LIB_A := $(BUILD)/libtinyblocks-vg.a
LIB_SO := $(BUILD)/libtinyblocks-vg.so
.PHONY: all static shared clean
all: static shared
static: $(LIB_A)
shared: $(LIB_SO)
$(OBJ): src/tb_vg.c include/tb_vg.h
@mkdir -p $(BUILD)
$(CC) $(CFLAGS) -fPIC -c $< -o $@
$(LIB_A): $(OBJ)
ar rcs $@ $^
$(LIB_SO): $(OBJ)
$(CC) -shared -o $@ $^ -lm
clean:
$(RM) -r $(BUILD)
+34
View File
@@ -0,0 +1,34 @@
/*
* TinyBlocks - tb_vg.h
* Library tinyblocks-vg: construcao de desenhos vetoriais (TinyBlocks_VectorData)
* e rasterizacao para TinyBlocks_Image. Base de renderizacao do tinyblocks-svg.
*
* Cada helper gera comandos autocontidos (incluindo a cor), de modo que o
* documento pode ser rasterizado pela tinyblocks-vg e tambem reproduzido no
* palco por tb_draw_ctx_play.
*/
#ifndef TINYBLOCKS_VG_H
#define TINYBLOCKS_VG_H
#include <tinyblocks/tinyblocks.h>
TB_API const TinyBlocks_LibraryDescriptor* tb_vg_get_library(void);
/* -- API C --- */
TB_API TinyBlocks_VectorData* tb_vg_new(void);
/* Rasteriza o documento numa imagem w x h. "scale" amplia as coordenadas
* (0 ou negativo = 1). O fundo e' transparente. */
TB_API TinyBlocks_Image* tb_vg_render(const TinyBlocks_VectorData* v,
int w, int h, float scale);
TB_API 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);
TB_API bool tb_vg_rect (TinyBlocks_VectorData* v, float x, float y, float w, float h,
float r, float g, float b, float a);
TB_API bool tb_vg_circle (TinyBlocks_VectorData* v, float cx, float cy, float rad,
float r, float g, float b, float a);
TB_API bool tb_vg_polygon(TinyBlocks_VectorData* v, const double* xy, int n, bool closed,
float r, float g, float b, float a);
#endif /* TINYBLOCKS_VG_H */
+15
View File
@@ -0,0 +1,15 @@
# Metadados da library vg. Lido pela UI/build sem recompilar o .so.
[library]
name = "tinyblocks-vg"
version = "1.0.0"
category = "Vetor"
description = "Desenho vetorial 2D e rasterizacao para imagem"
color = [171, 71, 188, 255]
dark = [123, 31, 162, 255]
[build]
sources = ["src/tb_vg.c"]
includes = ["include"]
shared = "build/libtinyblocks-vg.so"
static = "build/libtinyblocks-vg.a"
depends = []
+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;
}