Adiciona tinyblocks-vg e tinyblocks-svg (rasterizador + parser SVG) e specs no editor
This commit is contained in:
1 parent
f4e2556110
commit
714186a1a1
12 files changed
+1272
-6
No files matched your search
@@ -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)
|
||||
@@ -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 */
|
||||
@@ -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"]
|
||||
@@ -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;
|
||||
}
|
||||
Reference in new issue
Block a user