#include "tkc/mem.h" #include "tkc/utils.h" #include "gtest/gtest.h" #include "svg/svg_to_bsvg.h" static const char* s_template = "%s"; static char s_buff[1024]; TEST(SVGToBSVG, text) { bsvg_t svg; uint32_t* out = NULL; uint32_t out_length = 0; const svg_shape_t* shape = NULL; const svg_shape_text_t* shape_text = NULL; const char* text = NULL; const char* content = "abc"; tk_snprintf(s_buff, sizeof(s_buff) - 1, s_template, content); svg_to_bsvg(s_buff, strlen(s_buff), &out, &out_length); bsvg_init(&svg, out, out_length); shape_text = (const svg_shape_text_t*)bsvg_get_first_shape(&svg); shape = (const svg_shape_t*)shape_text; ASSERT_EQ(shape->type, SVG_SHAPE_TEXT); ASSERT_EQ(shape->fill_type, SVG_COLOR_SOLID); ASSERT_EQ(shape->stroke_type, SVG_COLOR_NULL); ASSERT_EQ(svg_shape_get_fill(shape)->solid.color, 0xff000000u); ASSERT_EQ(shape_text->x, 10.0f); ASSERT_EQ(shape_text->y, 10.0f); text = svg_shape_text_get_text(shape); ASSERT_STREQ(text, "abc"); TKMEM_FREE(out); } TEST(SVGToBSVG, path) { bsvg_t svg; uint32_t* out = NULL; uint32_t out_length = 0; const svg_shape_t* shape = NULL; const char* content = ""; tk_snprintf(s_buff, sizeof(s_buff) - 1, s_template, content); svg_to_bsvg(s_buff, strlen(s_buff), &out, &out_length); bsvg_init(&svg, out, out_length); shape = bsvg_get_first_shape(&svg); ASSERT_EQ(shape->type, SVG_SHAPE_PATH); TKMEM_FREE(out); } TEST(SVGToBSVG, rect) { bsvg_t svg; uint32_t* out = NULL; uint32_t out_length = 0; const svg_shape_t* shape = NULL; const char* content = ""; tk_snprintf(s_buff, sizeof(s_buff) - 1, s_template, content); svg_to_bsvg(s_buff, strlen(s_buff), &out, &out_length); bsvg_init(&svg, out, out_length); shape = bsvg_get_first_shape(&svg); ASSERT_EQ(shape->type, SVG_SHAPE_PATH); ASSERT_EQ(shape->fill_type, SVG_COLOR_NULL); ASSERT_EQ(shape->stroke_type, SVG_COLOR_SOLID); ASSERT_EQ(svg_shape_get_stroke(shape)->solid.color, 0xff000000u); ASSERT_EQ(shape->stroke_width, 5.0f); TKMEM_FREE(out); } TEST(SVGToBSVG, line) { bsvg_t svg; uint32_t* out = NULL; uint32_t out_length = 0; const svg_shape_t* shape = NULL; const char* content = ""; tk_snprintf(s_buff, sizeof(s_buff) - 1, s_template, content); svg_to_bsvg(s_buff, strlen(s_buff), &out, &out_length); bsvg_init(&svg, out, out_length); shape = bsvg_get_first_shape(&svg); ASSERT_EQ(shape->type, SVG_SHAPE_PATH); ASSERT_EQ(shape->fill_type, SVG_COLOR_SOLID); ASSERT_EQ(shape->stroke_type, SVG_COLOR_SOLID); ASSERT_EQ(svg_shape_get_fill(shape)->solid.color, 0xff0000ffu); ASSERT_EQ(svg_shape_get_stroke(shape)->solid.color, 0xff000000u); ASSERT_EQ(shape->stroke_width, 5.0f); TKMEM_FREE(out); } TEST(SVGToBSVG, circle) { bsvg_t svg; uint32_t* out = NULL; uint32_t out_length = 0; const svg_shape_t* shape = NULL; const char* content = ""; tk_snprintf(s_buff, sizeof(s_buff) - 1, s_template, content); svg_to_bsvg(s_buff, strlen(s_buff), &out, &out_length); bsvg_init(&svg, out, out_length); shape = bsvg_get_first_shape(&svg); ASSERT_EQ(shape->type, SVG_SHAPE_PATH); ASSERT_EQ(shape->fill_type, SVG_COLOR_SOLID); ASSERT_EQ(shape->stroke_type, SVG_COLOR_SOLID); ASSERT_EQ(svg_shape_get_fill(shape)->solid.color, 0xff0000ffu); ASSERT_EQ(svg_shape_get_stroke(shape)->solid.color, 0xff000000u); ASSERT_EQ(shape->stroke_width, 5.0f); TKMEM_FREE(out); } TEST(SVGToBSVG, polygon) { bsvg_t svg; uint32_t* out = NULL; uint32_t out_length = 0; const svg_shape_t* shape = NULL; const char* content = ""; tk_snprintf(s_buff, sizeof(s_buff) - 1, s_template, content); svg_to_bsvg(s_buff, strlen(s_buff), &out, &out_length); bsvg_init(&svg, out, out_length); shape = bsvg_get_first_shape(&svg); ASSERT_EQ(shape->type, SVG_SHAPE_PATH); ASSERT_EQ(shape->fill_type, SVG_COLOR_SOLID); ASSERT_EQ(shape->stroke_type, SVG_COLOR_SOLID); ASSERT_EQ(svg_shape_get_fill(shape)->solid.color, 0xff0000ffu); ASSERT_EQ(svg_shape_get_stroke(shape)->solid.color, 0xff000000u); ASSERT_EQ(shape->stroke_width, 5.0f); TKMEM_FREE(out); } TEST(SVGToBSVG, polyline) { bsvg_t svg; uint32_t* out = NULL; uint32_t out_length = 0; const svg_shape_t* shape = NULL; const char* content = ""; tk_snprintf(s_buff, sizeof(s_buff) - 1, s_template, content); svg_to_bsvg(s_buff, strlen(s_buff), &out, &out_length); bsvg_init(&svg, out, out_length); shape = bsvg_get_first_shape(&svg); ASSERT_EQ(shape->type, SVG_SHAPE_PATH); ASSERT_EQ(shape->fill_type, SVG_COLOR_SOLID); ASSERT_EQ(shape->stroke_type, SVG_COLOR_SOLID); ASSERT_EQ(svg_shape_get_fill(shape)->solid.color, 0xff0000ffu); ASSERT_EQ(svg_shape_get_stroke(shape)->solid.color, 0xff000000u); ASSERT_EQ(shape->stroke_width, 5.0f); TKMEM_FREE(out); } TEST(SVGToBSVG, polygon_lr) { bsvg_t svg; uint32_t* out = NULL; uint32_t out_length = 0; const svg_shape_t* shape = NULL; const char* content = ""; tk_snprintf(s_buff, sizeof(s_buff) - 1, s_template, content); svg_to_bsvg(s_buff, strlen(s_buff), &out, &out_length); bsvg_init(&svg, out, out_length); shape = bsvg_get_first_shape(&svg); ASSERT_EQ(shape->type, SVG_SHAPE_PATH); ASSERT_EQ(shape->fill_type, SVG_COLOR_SOLID); ASSERT_EQ(shape->stroke_type, SVG_COLOR_SOLID); ASSERT_EQ(svg_shape_get_fill(shape)->solid.color, 0xff0000ffu); ASSERT_EQ(svg_shape_get_stroke(shape)->solid.color, 0xff000000u); ASSERT_EQ(shape->stroke_width, 5.0f); TKMEM_FREE(out); } TEST(SVGToBSVG, polygon_no_fill1) { bsvg_t svg; uint32_t* out = NULL; uint32_t out_length = 0; const svg_shape_t* shape = NULL; const char* content = ""; tk_snprintf(s_buff, sizeof(s_buff) - 1, s_template, content); svg_to_bsvg(s_buff, strlen(s_buff), &out, &out_length); bsvg_init(&svg, out, out_length); shape = bsvg_get_first_shape(&svg); ASSERT_EQ(shape->type, SVG_SHAPE_PATH); ASSERT_EQ(shape->fill_type, SVG_COLOR_NULL); ASSERT_EQ(shape->stroke_type, SVG_COLOR_SOLID); ASSERT_EQ(svg_shape_get_stroke(shape)->solid.color, 0xff000000u); ASSERT_EQ(shape->stroke_width, 5.0f); TKMEM_FREE(out); } TEST(SVGToBSVG, polygon_no_fill2) { bsvg_t svg; uint32_t* out = NULL; uint32_t out_length = 0; const svg_shape_t* shape = NULL; const char* content = ""; tk_snprintf(s_buff, sizeof(s_buff) - 1, s_template, content); svg_to_bsvg(s_buff, strlen(s_buff), &out, &out_length); bsvg_init(&svg, out, out_length); shape = bsvg_get_first_shape(&svg); ASSERT_EQ(shape->type, SVG_SHAPE_PATH); ASSERT_EQ(shape->fill_type, SVG_COLOR_NULL); ASSERT_EQ(shape->stroke_type, SVG_COLOR_SOLID); ASSERT_EQ(svg_shape_get_stroke(shape)->solid.color, 0xff000000u); ASSERT_EQ(shape->stroke_width, 5.0f); TKMEM_FREE(out); } TEST(SVGToBSVG, polygon_no_fill3) { bsvg_t svg; uint32_t* out = NULL; uint32_t out_length = 0; const svg_shape_t* shape = NULL; const char* content = ""; tk_snprintf(s_buff, sizeof(s_buff) - 1, s_template, content); svg_to_bsvg(s_buff, strlen(s_buff), &out, &out_length); bsvg_init(&svg, out, out_length); shape = bsvg_get_first_shape(&svg); ASSERT_EQ(shape->type, SVG_SHAPE_PATH); ASSERT_EQ(shape->fill_type, SVG_COLOR_NULL); ASSERT_EQ(shape->stroke_type, SVG_COLOR_SOLID); ASSERT_EQ(svg_shape_get_stroke(shape)->solid.color, 0xff000000u); ASSERT_EQ(shape->stroke_width, 5.0f); TKMEM_FREE(out); } TEST(SVGToBSVG, polygon_no_stroke1) { bsvg_t svg; uint32_t* out = NULL; uint32_t out_length = 0; const svg_shape_t* shape = NULL; const char* content = ""; tk_snprintf(s_buff, sizeof(s_buff) - 1, s_template, content); svg_to_bsvg(s_buff, strlen(s_buff), &out, &out_length); bsvg_init(&svg, out, out_length); shape = bsvg_get_first_shape(&svg); ASSERT_EQ(shape->stroke_type, SVG_COLOR_NULL); TKMEM_FREE(out); } TEST(SVGToBSVG, polygon_no_stroke2) { bsvg_t svg; uint32_t* out = NULL; uint32_t out_length = 0; const svg_shape_t* shape = NULL; const char* content = ""; tk_snprintf(s_buff, sizeof(s_buff) - 1, s_template, content); svg_to_bsvg(s_buff, strlen(s_buff), &out, &out_length); bsvg_init(&svg, out, out_length); shape = bsvg_get_first_shape(&svg); ASSERT_EQ(shape->stroke_type, SVG_COLOR_NULL); TKMEM_FREE(out); } TEST(SVGToBSVG, polygon_no_stroke3) { bsvg_t svg; uint32_t* out = NULL; uint32_t out_length = 0; const svg_shape_t* shape = NULL; const char* content = ""; tk_snprintf(s_buff, sizeof(s_buff) - 1, s_template, content); svg_to_bsvg(s_buff, strlen(s_buff), &out, &out_length); bsvg_init(&svg, out, out_length); shape = bsvg_get_first_shape(&svg); ASSERT_EQ(shape->stroke_type, SVG_COLOR_NULL); TKMEM_FREE(out); } TEST(SVGToBSVG, polygon_gradient_fill) { bsvg_t svg; uint32_t* out = NULL; uint32_t out_length = 0; const svg_shape_t* shape = NULL; const svg_color_t* fill = NULL; const char* content = "" "" "" "fill=\"url(#gradient)\">"; tk_snprintf(s_buff, sizeof(s_buff) - 1, s_template, content); svg_to_bsvg(s_buff, strlen(s_buff), &out, &out_length); bsvg_init(&svg, out, out_length); shape = bsvg_get_first_shape(&svg); ASSERT_EQ(shape->fill_type, SVG_COLOR_LINEAR_GRADIENT); fill = svg_shape_get_fill(shape); ASSERT_EQ(fill->linear_gradient.icolor.color, color_init(0xff, 0x00, 0x00, 0xff).color); ASSERT_EQ(fill->linear_gradient.ocolor.color, color_init(0x00, 0x00, 0xff, 0xff).color); TKMEM_FREE(out); } #include "tkc/fs.h" TEST(SVGToBSVG, file) { bsvg_t svg; uint32_t* out = NULL; uint32_t out_length = 0; const svg_shape_t* shape = NULL; const char* content = ""; tk_snprintf(s_buff, sizeof(s_buff) - 1, s_template, content); file_write("test.svg", s_buff, strlen(s_buff)); svg_file_to_bsvg("test.svg", "test.bsvg"); out = (uint32_t*)file_read("test.bsvg", &out_length); file_remove("test.svg"); file_remove("test.bsvg"); bsvg_init(&svg, out, out_length); shape = bsvg_get_first_shape(&svg); ASSERT_EQ(shape->stroke_type, SVG_COLOR_NULL); TKMEM_FREE(out); }