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

+98
View File
@@ -0,0 +1,98 @@
/*
* svg_test.c - testa tinyblocks-svg: parsing de shapes, path e rasterizacao
* (via tinyblocks-vg) e os blocos SVG.*.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <tinyblocks/tinyblocks.h>
#include <tb_svg.h>
static int fails = 0;
#define CHECK(c, m) do { if (c) printf("ok %s\n", m); else { printf("FAIL %s\n", m); fails++; } } while (0)
static const char* DOC =
"<svg width=\"64\" height=\"64\" xmlns=\"http://www.w3.org/2000/svg\">"
" <rect x=\"4\" y=\"4\" width=\"20\" height=\"20\" fill=\"#ff0000\"/>"
" <circle cx=\"44\" cy=\"44\" r=\"12\" fill=\"rgb(0,0,255)\"/>"
" <line x1=\"0\" y1=\"60\" x2=\"64\" y2=\"60\" stroke=\"green\" stroke-width=\"3\"/>"
"</svg>";
static void set_txt(TinyBlocks_Block* b, uint32_t i, const char* s) {
TinyBlocks_Value vv = tb_value_text(s);
tb_block_set_default(b, i, vv);
tb_value_free(&vv);
}
static void set_num(TinyBlocks_Block* b, uint32_t i, double v) {
TinyBlocks_Value vv = tb_value_real(v);
tb_block_set_default(b, i, vv);
tb_value_free(&vv);
}
int main(void) {
/* -- Parse --- */
int sw = 0, sh = 0;
TinyBlocks_VectorData* v = tb_svg_parse(DOC, &sw, &sh);
CHECK(v && sw == 64 && sh == 64, "tb_svg_parse le width/height do <svg>");
if (v) tb_vector_data_release(v);
/* -- Render --- */
TinyBlocks_Image* im = tb_svg_render(DOC, 64, 64);
CHECK(im && tb_image_width(im) == 64 && tb_image_height(im) == 64, "tb_svg_render 64x64");
uint8_t r, g, b, a;
if (im) {
tb_image_get_pixel(im, 10, 10, &r, &g, &b, &a);
CHECK(r > 200 && g < 60 && b < 60, "retangulo vermelho renderizado");
tb_image_get_pixel(im, 44, 44, &r, &g, &b, &a);
CHECK(b > 200 && r < 60, "circulo azul renderizado");
tb_image_get_pixel(im, 32, 60, &r, &g, &b, &a);
CHECK(g > 100 && r < 100 && b < 100, "linha verde renderizada");
tb_image_get_pixel(im, 32, 32, &r, &g, &b, &a);
CHECK(a == 0, "area vazia transparente");
}
if (im) tb_image_release(im);
/* -- Path com fill --- */
const char* tri = "<svg width=\"20\" height=\"20\">"
"<path d=\"M2 2 L18 2 L10 18 Z\" fill=\"#00ff00\"/></svg>";
TinyBlocks_Image* ti = tb_svg_render(tri, 20, 20);
if (ti) {
tb_image_get_pixel(ti, 10, 8, &r, &g, &b, &a);
CHECK(g > 150 && r < 80, "path preenchido (triangulo verde)");
tb_image_release(ti);
}
/* -- Blocos --- */
TinyBlocks* rt;
tinyblocks_init(&rt, NULL);
tb_library_register(rt, tb_svg_get_library());
CHECK(tb_rt_find_type(rt, "SVG.Parse") && tb_rt_find_type(rt, "SVG.Render") &&
tb_rt_find_type(rt, "SVG.Load"), "blocos SVG.* registrados");
{
TinyBlocks_TypeBlock* t = tb_rt_find_type(rt, "SVG.Render");
const TinyBlocks_Category* cat = tb_library_category((const TinyBlocks_LibraryBlock*)t->base.parent);
CHECK(cat && strcmp(cat->name, "SVG") == 0, "categoria 'SVG'");
}
TinyBlocks_Block* bp = tb_block_create(rt, tb_rt_find_type(rt, "SVG.Parse"));
set_txt(bp, 0, DOC);
tb_block_eval(bp, rt);
CHECK(bp->outputs[0].kind == TB_VALUE_VECTOR, "SVG.Parse devolve vetor");
TinyBlocks_Block* br = tb_block_create(rt, tb_rt_find_type(rt, "SVG.Render"));
set_txt(br, 0, DOC);
set_num(br, 1, 64); set_num(br, 2, 64);
tb_block_eval(br, rt);
CHECK(br->outputs[0].kind == TB_VALUE_IMAGE, "SVG.Render devolve imagem");
if (br->outputs[0].kind == TB_VALUE_IMAGE) {
tb_image_get_pixel(br->outputs[0].as.image, 10, 10, &r, &g, &b, &a);
CHECK(r > 200, "imagem do bloco SVG.Render tem o retangulo vermelho");
}
tb_block_destroy(bp, rt); tb_block_destroy(br, rt);
tinyblocks_deinit(&rt);
if (fails) printf("\n%d FALHA(S)\n", fails);
else printf("\nTUDO OK\n");
return fails != 0;
}
+113
View File
@@ -0,0 +1,113 @@
/*
* vg_test.c - testa tinyblocks-vg: construcao de desenho vetorial, rasterizacao
* e os blocos VG.*.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <tinyblocks/tinyblocks.h>
#include <tb_vg.h>
static int fails = 0;
#define CHECK(c, m) do { if (c) printf("ok %s\n", m); else { printf("FAIL %s\n", m); fails++; } } while (0)
static void set_num(TinyBlocks_Block* b, uint32_t i, double v) {
TinyBlocks_Value vv = tb_value_real(v);
tb_block_set_default(b, i, vv);
tb_value_free(&vv);
}
static void set_vec(TinyBlocks_Block* b, uint32_t i, TinyBlocks_VectorData* v) {
TinyBlocks_Value vv = tb_value_vector(v);
tb_block_set_default(b, i, vv);
tb_value_free(&vv);
}
int main(void) {
/* -- Rasterizacao direta --- */
TinyBlocks_VectorData* v = tb_vg_new();
CHECK(v != NULL, "tb_vg_new cria vetor");
tb_vg_rect(v, 4, 4, 8, 8, 1, 0, 0, 1); /* retangulo vermelho */
tb_vg_circle(v, 8, 16, 2, 0, 0, 1, 1); /* circulo azul */
TinyBlocks_Image* im = tb_vg_render(v, 20, 20, 1);
CHECK(im && tb_image_width(im) == 20 && tb_image_height(im) == 20, "tb_vg_render 20x20");
uint8_t r, g, b, a;
if (im) {
tb_image_get_pixel(im, 8, 8, &r, &g, &b, &a);
CHECK(r > 200 && g < 60 && b < 60 && a > 200, "retangulo pintado de vermelho");
tb_image_get_pixel(im, 8, 16, &r, &g, &b, &a);
CHECK(b > 200 && r < 60, "circulo pintado de azul");
tb_image_get_pixel(im, 0, 0, &r, &g, &b, &a);
CHECK(a == 0, "fundo transparente");
}
/* Poligono preenchido (triangulo verde) */
TinyBlocks_VectorData* p = tb_vg_new();
double tri[] = {2,2, 18,2, 10,18};
tb_vg_polygon(p, tri, 3, true, 0, 1, 0, 1);
TinyBlocks_Image* pi = tb_vg_render(p, 20, 20, 1);
if (pi) {
tb_image_get_pixel(pi, 10, 8, &r, &g, &b, &a);
CHECK(g > 150 && r < 80, "poligono preenchido de verde");
tb_image_release(pi);
}
tb_vector_data_release(p);
/* Linha espessa */
TinyBlocks_VectorData* l = tb_vg_new();
tb_vg_line(l, 2, 10, 18, 10, 1, 1, 1, 1, 4);
TinyBlocks_Image* li = tb_vg_render(l, 20, 20, 1);
if (li) {
tb_image_get_pixel(li, 10, 10, &r, &g, &b, &a);
CHECK(r > 200 && g > 200 && b > 200, "linha branca espessa");
tb_image_release(li);
}
tb_vector_data_release(l);
if (im) tb_image_release(im);
tb_vector_data_release(v);
/* -- Blocos --- */
TinyBlocks* rt;
tinyblocks_init(&rt, NULL);
tb_library_register(rt, tb_vg_get_library());
const char* names[] = {"VG.New","VG.Line","VG.Rect","VG.Circle","VG.Polygon","VG.Render"};
int all = 1;
for (size_t i = 0; i < sizeof(names)/sizeof(names[0]); i++)
if (!tb_rt_find_type(rt, names[i])) { all = 0; printf(" falta %s\n", names[i]); }
CHECK(all, "todos os blocos VG.* registrados");
{
TinyBlocks_TypeBlock* t = tb_rt_find_type(rt, "VG.New");
const TinyBlocks_Category* cat = tb_library_category((const TinyBlocks_LibraryBlock*)t->base.parent);
CHECK(cat && strcmp(cat->name, "Vetor") == 0, "categoria 'Vetor'");
}
TinyBlocks_Block* bn = tb_block_create(rt, tb_rt_find_type(rt, "VG.New"));
tb_block_eval(bn, rt);
CHECK(bn->outputs[0].kind == TB_VALUE_VECTOR, "VG.New devolve vetor");
TinyBlocks_Block* br = tb_block_create(rt, tb_rt_find_type(rt, "VG.Rect"));
set_vec(br, 0, bn->outputs[0].as.vector);
set_num(br, 1, 0); set_num(br, 2, 0); set_num(br, 3, 10); set_num(br, 4, 10);
set_num(br, 5, 1); set_num(br, 6, 0); set_num(br, 7, 1); /* magenta */
tb_block_eval(br, rt);
CHECK(br->outputs[0].kind == TB_VALUE_VECTOR, "VG.Rect devolve o mesmo vetor");
TinyBlocks_Block* bren = tb_block_create(rt, tb_rt_find_type(rt, "VG.Render"));
set_vec(bren, 0, br->outputs[0].as.vector);
set_num(bren, 1, 12); set_num(bren, 2, 12); set_num(bren, 3, 1);
tb_block_eval(bren, rt);
CHECK(bren->outputs[0].kind == TB_VALUE_IMAGE, "VG.Render devolve imagem");
if (bren->outputs[0].kind == TB_VALUE_IMAGE) {
tb_image_get_pixel(bren->outputs[0].as.image, 5, 5, &r, &g, &b, &a);
CHECK(r > 200 && b > 200, "retangulo magenta rasterizado pelo bloco");
}
tb_block_destroy(bn, rt); tb_block_destroy(br, rt); tb_block_destroy(bren, rt);
tinyblocks_deinit(&rt);
if (fails) printf("\n%d FALHA(S)\n", fails);
else printf("\nTUDO OK\n");
return fails != 0;
}