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;
}