/* * svg_test.c - testa tinyblocks-svg: parsing de shapes, path e rasterizacao * (via tinyblocks-vg) e os blocos SVG.*. */ #include #include #include #include #include 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 = "" " " " " " " ""; 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 "); 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 = "" ""; 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; }