From b1b15a4c495dd8c3a506fb9a68e0ceeb266f1145 Mon Sep 17 00:00:00 2001 From: mguinhos Date: Tue, 20 Oct 2026 20:59:17 -0300 Subject: [PATCH] Adiciona tinyblocks-base64 e tinyblocks-curves (listas de pontos) --- Makefile | 39 ++- libraries/tinyblocks-base64/Makefile | 29 ++ .../tinyblocks-base64/include/tb_base64.h | 23 ++ libraries/tinyblocks-base64/library.toml | 15 + libraries/tinyblocks-base64/src/tb_base64.c | 141 ++++++++++ libraries/tinyblocks-curves/Makefile | 29 ++ .../tinyblocks-curves/include/tb_curves.h | 36 +++ libraries/tinyblocks-curves/library.toml | 15 + libraries/tinyblocks-curves/src/tb_curves.c | 257 ++++++++++++++++++ libraries/tinyblocks-editor/src/tb_editor.c | 25 +- tests/base64_test.c | 91 +++++++ tests/curves_test.c | 124 +++++++++ 12 files changed, 815 insertions(+), 9 deletions(-) create mode 100644 libraries/tinyblocks-base64/Makefile create mode 100644 libraries/tinyblocks-base64/include/tb_base64.h create mode 100644 libraries/tinyblocks-base64/library.toml create mode 100644 libraries/tinyblocks-base64/src/tb_base64.c create mode 100644 libraries/tinyblocks-curves/Makefile create mode 100644 libraries/tinyblocks-curves/include/tb_curves.h create mode 100644 libraries/tinyblocks-curves/library.toml create mode 100644 libraries/tinyblocks-curves/src/tb_curves.c create mode 100644 tests/base64_test.c create mode 100644 tests/curves_test.c diff --git a/Makefile b/Makefile index 8d3c16d..ff73145 100644 --- a/Makefile +++ b/Makefile @@ -24,6 +24,8 @@ JSON_DIR := $(LIBDIR)/tinyblocks-json XML_DIR := $(LIBDIR)/tinyblocks-xml CSV_DIR := $(LIBDIR)/tinyblocks-csv BP_DIR := $(LIBDIR)/tinyblocks-blueprint +B64_DIR := $(LIBDIR)/tinyblocks-base64 +CV_DIR := $(LIBDIR)/tinyblocks-curves API_DIR := $(LIBDIR)/tinyblocks-api LIBM_DIR := $(LIBDIR)/tinyblocks-libman ED_DIR := $(LIBDIR)/tinyblocks-editor @@ -78,12 +80,12 @@ LIB_LDFLAGS := -L$(ED_DIR)/build -L$(GX_DIR)/build -L$(SD_DIR)/build \ .PHONY: all help debug release static shared example sdl2 clean run python-lib mcp-lib json \ editor run-editor run-sdl2 mylib vector customappearance graphics sdl2-backend test-editor \ appdir appimage run-appimage icon \ - test-toml test-plugin test-blueprint \ + test-toml test-plugin test-blueprint test-base64 test-curves \ category-libs category-sos libs \ lib-events lib-control lib-operators lib-variables lib-appearance lib-pen lib-fs \ lib-environ \ lib-editor lib-graphics lib-sdl2 lib-python lib-mcp lib-toml \ - lib-json lib-xml lib-csv lib-blueprint lib-api lib-libman \ + lib-json lib-xml lib-csv lib-blueprint lib-base64 lib-curves lib-api lib-libman \ test-libman test-environ all: static example ## Compila a biblioteca estática e o exemplo hello (padrão) @@ -159,6 +161,12 @@ lib-csv: lib-blueprint: $(MAKE) -C $(BP_DIR) +lib-base64: + $(MAKE) -C $(B64_DIR) + +lib-curves: + $(MAKE) -C $(CV_DIR) + lib-api: $(MAKE) -C $(API_DIR) @@ -179,8 +187,8 @@ lib-mcp: libs: static lib-events lib-control lib-operators lib-variables \ lib-appearance lib-pen lib-fs lib-environ lib-graphics lib-sdl2 lib-toml \ - lib-json lib-xml lib-csv lib-blueprint lib-api lib-libman lib-editor - @echo "Libraries OK: category + graphics + sdl2 + toml + json + xml + csv + blueprint + api + libman + editor" + lib-json lib-xml lib-csv lib-blueprint lib-base64 lib-curves lib-api lib-libman lib-editor + @echo "Libraries OK: category + graphics + sdl2 + toml + json + xml + csv + blueprint + base64 + curves + api + libman + editor" category-libs: static $(foreach l,$(CAT_LIBS),lib-$(l)) ## Compila as libraries de categoria (.a/.so) @echo "Build category libs OK" @@ -231,7 +239,7 @@ run-editor: editor ## Compila e abre o editor visual # ── Empacotamento AppImage ────────────────────────────────────────────── # Libraries com blocos (categoria) que vao empacotadas como extensoes. -APPIMAGE_LIBS := $(CAT_LIBS) toml json xml csv blueprint +APPIMAGE_LIBS := $(CAT_LIBS) toml json xml csv blueprint base64 curves APP_NAME := TinyBlocks APP_VER := 0.1.0 @@ -424,6 +432,26 @@ $(BUILD)/environ_test: tests/environ_test.c $(EN_DIR)/src/tb_environ.c static test-environ: $(BUILD)/environ_test ## Roda o teste da environ (ASan/UBSan) ./$(BUILD)/environ_test +# -- Teste da library base64 --- +$(BUILD)/base64_test: tests/base64_test.c $(B64_DIR)/src/tb_base64.c static + @mkdir -p $(BUILD) + $(CC) $(CFLAGS_DBG) -I$(B64_DIR)/include $(B64_DIR)/src/tb_base64.c \ + tests/base64_test.c -o $@ \ + -Wl,--start-group $(LIB_A) -Wl,--end-group + +test-base64: $(BUILD)/base64_test ## Roda o teste da base64 (ASan/UBSan) + ./$(BUILD)/base64_test + +# -- Teste da library curves --- +$(BUILD)/curves_test: tests/curves_test.c $(CV_DIR)/src/tb_curves.c static + @mkdir -p $(BUILD) + $(CC) $(CFLAGS_DBG) -I$(CV_DIR)/include $(CV_DIR)/src/tb_curves.c \ + tests/curves_test.c -o $@ \ + -Wl,--start-group $(LIB_A) -Wl,--end-group -lm + +test-curves: $(BUILD)/curves_test ## Roda o teste da curves (ASan/UBSan) + ./$(BUILD)/curves_test + # -- Teste de plugin .so standalone --- FS_SO := $(FS_DIR)/build/libtinyblocks-fs.so @@ -440,6 +468,7 @@ clean: ## Remove os diretórios build/ (raiz e libraries) $(RM) -r $(BUILD) @for d in $(EV_DIR) $(CT_DIR) $(OP_DIR) $(VR_DIR) $(AP_DIR) $(PN_DIR) $(FS_DIR) \ $(TOML_DIR) $(JSON_DIR) $(XML_DIR) $(CSV_DIR) $(BP_DIR) \ + $(B64_DIR) $(CV_DIR) \ $(API_DIR) $(LIBM_DIR) \ $(ED_DIR) $(GX_DIR) $(SD_DIR) $(PY_DIR) $(MC_DIR); do \ $(MAKE) -C $$d clean >/dev/null 2>&1 || true; \ diff --git a/libraries/tinyblocks-base64/Makefile b/libraries/tinyblocks-base64/Makefile new file mode 100644 index 0000000..4a487ca --- /dev/null +++ b/libraries/tinyblocks-base64/Makefile @@ -0,0 +1,29 @@ +# Makefile da library base64 (categoria "Base64") +# Constroi build/libtinyblocks-base64.a e build/libtinyblocks-base64.so +CC := gcc +BUILD := build + +CFLAGS := -std=c11 -Wall -Wextra -pedantic -fvisibility=hidden \ + -D_POSIX_C_SOURCE=200809L -I../../include -Iinclude + +OBJ := $(BUILD)/tb_base64.o +LIB_A := $(BUILD)/libtinyblocks-base64.a +LIB_SO := $(BUILD)/libtinyblocks-base64.so + +.PHONY: all static shared clean +all: static shared +static: $(LIB_A) +shared: $(LIB_SO) + +$(OBJ): src/tb_base64.c include/tb_base64.h + @mkdir -p $(BUILD) + $(CC) $(CFLAGS) -fPIC -c $< -o $@ + +$(LIB_A): $(OBJ) + ar rcs $@ $^ + +$(LIB_SO): $(OBJ) + $(CC) -shared -o $@ $^ + +clean: + $(RM) -r $(BUILD) diff --git a/libraries/tinyblocks-base64/include/tb_base64.h b/libraries/tinyblocks-base64/include/tb_base64.h new file mode 100644 index 0000000..456bb17 --- /dev/null +++ b/libraries/tinyblocks-base64/include/tb_base64.h @@ -0,0 +1,23 @@ +/* + * TinyBlocks - tb_base64.h + * Library tinyblocks-base64: codifica/decodifica Base64 (categoria "Base64"). + * Independente do editor. + */ +#ifndef TINYBLOCKS_BASE64_H +#define TINYBLOCKS_BASE64_H + +#include + +TB_API const TinyBlocks_LibraryDescriptor* tb_base64_get_library(void); + +/* -- API C: buffers alocados com malloc (libere com free) --- */ +/* Codifica n bytes em texto Base64 terminado em '\0'. */ +TB_API char* tb_base64_encode(const void* data, size_t n); +/* Decodifica texto Base64; escreve o tamanho em *out_len (se != NULL). */ +TB_API unsigned char* tb_base64_decode(const char* text, size_t* out_len); +/* Atalhos para strings (o decode devolve texto terminado em '\0'). */ +TB_API char* tb_base64_encode_str(const char* s); +TB_API char* tb_base64_decode_str(const char* s, size_t* out_len); +TB_API const char* tb_base64_last_error(void); + +#endif /* TINYBLOCKS_BASE64_H */ diff --git a/libraries/tinyblocks-base64/library.toml b/libraries/tinyblocks-base64/library.toml new file mode 100644 index 0000000..a3eb896 --- /dev/null +++ b/libraries/tinyblocks-base64/library.toml @@ -0,0 +1,15 @@ +# Metadados da library base64. Lido pela UI/build sem recompilar o .so. +[library] +name = "tinyblocks-base64" +version = "1.0.0" +category = "Base64" +description = "Codifica e decodifica Base64" +color = [0, 150, 136, 255] +dark = [0, 121, 107, 255] + +[build] +sources = ["src/tb_base64.c"] +includes = ["include"] +shared = "build/libtinyblocks-base64.so" +static = "build/libtinyblocks-base64.a" +depends = [] diff --git a/libraries/tinyblocks-base64/src/tb_base64.c b/libraries/tinyblocks-base64/src/tb_base64.c new file mode 100644 index 0000000..137b352 --- /dev/null +++ b/libraries/tinyblocks-base64/src/tb_base64.c @@ -0,0 +1,141 @@ +/* + * TinyBlocks - tb_base64.c + * Categoria "Base64": Base64.Encode / Base64.Decode. + */ +#define _POSIX_C_SOURCE 200809L +#include +#include +#include + +#include +#include "tb_base64.h" + +static const char B64_ALPHA[] = + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + +static char s_err[160] = ""; + +const char* tb_base64_last_error(void) { return s_err; } + +char* tb_base64_encode(const void* data, size_t n) { + s_err[0] = 0; + const unsigned char* p = (const unsigned char*)data; + size_t out = ((n + 2) / 3) * 4; + char* buf = (char*)malloc(out + 1); + if (!buf) { snprintf(s_err, sizeof(s_err), "sem memoria"); return NULL; } + size_t o = 0; + for (size_t i = 0; i < n; i += 3) { + unsigned v = (unsigned)p[i] << 16; + if (i + 1 < n) v |= (unsigned)p[i + 1] << 8; + if (i + 2 < n) v |= (unsigned)p[i + 2]; + buf[o++] = B64_ALPHA[(v >> 18) & 63]; + buf[o++] = B64_ALPHA[(v >> 12) & 63]; + buf[o++] = (i + 1 < n) ? B64_ALPHA[(v >> 6) & 63] : '='; + buf[o++] = (i + 2 < n) ? B64_ALPHA[v & 63] : '='; + } + buf[o] = 0; + return buf; +} + +static int b64_val(int c) { + if (c >= 'A' && c <= 'Z') return c - 'A'; + if (c >= 'a' && c <= 'z') return c - 'a' + 26; + if (c >= '0' && c <= '9') return c - '0' + 52; + if (c == '+') return 62; + if (c == '/') return 63; + return -1; +} + +unsigned char* tb_base64_decode(const char* text, size_t* out_len) { + s_err[0] = 0; + if (out_len) *out_len = 0; + if (!text) { snprintf(s_err, sizeof(s_err), "entrada nula"); return NULL; } + size_t n = strlen(text); + unsigned char* buf = (unsigned char*)malloc(n / 4 * 3 + 4); + if (!buf) { snprintf(s_err, sizeof(s_err), "sem memoria"); return NULL; } + size_t o = 0; + int quad[4], qn = 0; + for (size_t i = 0; i < n; i++) { + unsigned char c = (unsigned char)text[i]; + if (c == '\n' || c == '\r' || c == ' ' || c == '\t') continue; + if (c == '=') { quad[qn++] = -2; } + else { + int v = b64_val(c); + if (v < 0) { free(buf); snprintf(s_err, sizeof(s_err), "caractere invalido '%c'", c); return NULL; } + quad[qn++] = v; + } + if (qn == 4) { + int q0 = quad[0] < 0 ? 0 : quad[0]; + int q1 = quad[1] < 0 ? 0 : quad[1]; + int q2 = quad[2] < 0 ? 0 : quad[2]; + int q3 = quad[3] < 0 ? 0 : quad[3]; + if (quad[0] >= 0) buf[o++] = (unsigned char)((q0 << 2) | ((q1 & 0x30) >> 4)); + if (quad[2] >= 0) buf[o++] = (unsigned char)(((q1 & 0x0F) << 4) | ((q2 & 0x3C) >> 2)); + if (quad[3] >= 0) buf[o++] = (unsigned char)(((q2 & 0x03) << 6) | (q3 & 0x3F)); + qn = 0; + } + } + buf[o] = 0; + if (out_len) *out_len = o; + return buf; +} + +char* tb_base64_encode_str(const char* s) { + return tb_base64_encode(s ? s : "", s ? strlen(s) : 0); +} + +char* tb_base64_decode_str(const char* s, size_t* out_len) { + return (char*)tb_base64_decode(s, out_len); +} + +/* -- 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 void eval_encode(TinyBlocks_Block* self, TinyBlocks* rt) { + char* s = tb_base64_encode_str(in_text(self, rt, 0)); + tb_block_set_output(self, 0, tb_value_text(s ? s : "")); + free(s); + self->dirty = false; +} + +static void eval_decode(TinyBlocks_Block* self, TinyBlocks* rt) { + char* s = tb_base64_decode_str(in_text(self, rt, 0), NULL); + tb_block_set_output(self, 0, tb_value_text(s ? s : "")); + free(s); + self->dirty = false; +} + +/* -- Registro ------------------------------------------------------------ */ + +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 base64_init(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) { + static const TinyBlocks_Category cat = { + "Base64", + { 0/255.f, 150/255.f, 136/255.f, 1.f}, + { 0/255.f, 121/255.f, 107/255.f, 1.f}, + }; + tb_library_set_category(lib, &cat); + + static const TinyBlocks_SlotDesc in_text_s[] = {{"texto", TB_VALUE_TEXT}}; + static const TinyBlocks_SlotDesc out_text_s[] = {{"texto", TB_VALUE_TEXT}}; + + return reg(rt, lib, "Base64.Encode", in_text_s, 1, out_text_s, 1, eval_encode) + && reg(rt, lib, "Base64.Decode", in_text_s, 1, out_text_s, 1, eval_decode); +} + +TINYBLOCKS_DEFINE_LIBRARY(base64, "tinyblocks-base64", "1.0.0", base64_init, NULL) + +const TinyBlocks_LibraryDescriptor* tb_base64_get_library(void) { + return &_tb_lib_desc_base64; +} diff --git a/libraries/tinyblocks-curves/Makefile b/libraries/tinyblocks-curves/Makefile new file mode 100644 index 0000000..57ea30f --- /dev/null +++ b/libraries/tinyblocks-curves/Makefile @@ -0,0 +1,29 @@ +# Makefile da library curves (categoria "Curvas") +# Constroi build/libtinyblocks-curves.a e build/libtinyblocks-curves.so +CC := gcc +BUILD := build + +CFLAGS := -std=c11 -Wall -Wextra -pedantic -fvisibility=hidden \ + -D_POSIX_C_SOURCE=200809L -I../../include -Iinclude + +OBJ := $(BUILD)/tb_curves.o +LIB_A := $(BUILD)/libtinyblocks-curves.a +LIB_SO := $(BUILD)/libtinyblocks-curves.so + +.PHONY: all static shared clean +all: static shared +static: $(LIB_A) +shared: $(LIB_SO) + +$(OBJ): src/tb_curves.c include/tb_curves.h + @mkdir -p $(BUILD) + $(CC) $(CFLAGS) -fPIC -c $< -o $@ + +$(LIB_A): $(OBJ) + ar rcs $@ $^ + +$(LIB_SO): $(OBJ) + $(CC) -shared -o $@ $^ -lm + +clean: + $(RM) -r $(BUILD) diff --git a/libraries/tinyblocks-curves/include/tb_curves.h b/libraries/tinyblocks-curves/include/tb_curves.h new file mode 100644 index 0000000..6f4717d --- /dev/null +++ b/libraries/tinyblocks-curves/include/tb_curves.h @@ -0,0 +1,36 @@ +/* + * TinyBlocks - tb_curves.h + * Library tinyblocks-curves: curvas (reta, bezier, quadratica, arco, circulo). + * Cada curva devolve uma LISTA DE PONTOS como array JSON: [[x,y],[x,y],...]. + * Independente do editor. + */ +#ifndef TINYBLOCKS_CURVES_H +#define TINYBLOCKS_CURVES_H + +#include +#include + +TB_API const TinyBlocks_LibraryDescriptor* tb_curves_get_library(void); + +/* -- API C: cada funcao retorna um JSON array novo (libere com tb_json_release). + * "steps" e o numero de segmentos (>=1); a lista sai com steps+1 pontos. */ +TB_API TinyBlocks_JsonValue* tb_curves_line(double x0, double y0, + double x1, double y1, int steps); +TB_API TinyBlocks_JsonValue* tb_curves_bezier(double x0, double y0, + double cx1, double cy1, + double cx2, double cy2, + double x1, double y1, int steps); +TB_API TinyBlocks_JsonValue* tb_curves_quadratic(double x0, double y0, + double cx, double cy, + double x1, double y1, int steps); +TB_API TinyBlocks_JsonValue* tb_curves_arc(double cx, double cy, double r, + double a0_deg, double a1_deg, int steps); +TB_API TinyBlocks_JsonValue* tb_curves_circle(double cx, double cy, double r, + int steps); + +/* -- Leitura de uma lista de pontos --- */ +TB_API int tb_curves_count(const TinyBlocks_JsonValue* pts); +TB_API bool tb_curves_point(const TinyBlocks_JsonValue* pts, int i, + double* x, double* y); + +#endif /* TINYBLOCKS_CURVES_H */ diff --git a/libraries/tinyblocks-curves/library.toml b/libraries/tinyblocks-curves/library.toml new file mode 100644 index 0000000..b890de2 --- /dev/null +++ b/libraries/tinyblocks-curves/library.toml @@ -0,0 +1,15 @@ +# Metadados da library curves. Lido pela UI/build sem recompilar o .so. +[library] +name = "tinyblocks-curves" +version = "1.0.0" +category = "Curvas" +description = "Curvas: reta, bezier, quadratica, arco e circulo (lista de pontos)" +color = [233, 30, 99, 255] +dark = [194, 24, 91, 255] + +[build] +sources = ["src/tb_curves.c"] +includes = ["include"] +shared = "build/libtinyblocks-curves.so" +static = "build/libtinyblocks-curves.a" +depends = [] diff --git a/libraries/tinyblocks-curves/src/tb_curves.c b/libraries/tinyblocks-curves/src/tb_curves.c new file mode 100644 index 0000000..645b090 --- /dev/null +++ b/libraries/tinyblocks-curves/src/tb_curves.c @@ -0,0 +1,257 @@ +/* + * TinyBlocks - tb_curves.c + * Categoria "Curvas": Curvas.Line / Bezier / Quadratic / Arc / Circle / Count / + * PointX / PointY. As curvas retornam uma lista de pontos [[x,y],...] (JSON). + */ +#define _POSIX_C_SOURCE 200809L +#include +#include +#include +#include + +#include +#include +#include "tb_curves.h" + +#ifndef M_PI +#define M_PI 3.14159265358979323846 +#endif + +#define CURVES_MAX_STEPS 100000 + +static int clamp_steps(int steps) { + if (steps < 1) return 1; + if (steps > CURVES_MAX_STEPS) return CURVES_MAX_STEPS; + return steps; +} + +static void push_number(TinyBlocks_JsonValue* arr, double v) { + TinyBlocks_JsonValue* n = tb_json_number(v); + if (n) { tb_json_array_push(arr, n); tb_json_release(n); } +} + +/* Adiciona o ponto [x,y] ao array. */ +static void push_point(TinyBlocks_JsonValue* arr, double x, double y) { + TinyBlocks_JsonValue* pt = tb_json_array(); + if (!pt) return; + push_number(pt, x); + push_number(pt, y); + tb_json_array_push(arr, pt); + tb_json_release(pt); +} + +/* -- Curvas -------------------------------------------------------------- */ + +TinyBlocks_JsonValue* tb_curves_line(double x0, double y0, double x1, double y1, + int steps) { + steps = clamp_steps(steps); + TinyBlocks_JsonValue* a = tb_json_array(); + if (!a) return NULL; + for (int i = 0; i <= steps; i++) { + double t = (double)i / steps; + push_point(a, x0 + (x1 - x0) * t, y0 + (y1 - y0) * t); + } + return a; +} + +TinyBlocks_JsonValue* tb_curves_bezier(double x0, double y0, + double cx1, double cy1, + double cx2, double cy2, + double x1, double y1, int steps) { + steps = clamp_steps(steps); + TinyBlocks_JsonValue* a = tb_json_array(); + if (!a) return NULL; + for (int i = 0; i <= steps; i++) { + double t = (double)i / steps, u = 1.0 - t; + double b0 = u*u*u, b1 = 3*u*u*t, b2 = 3*u*t*t, b3 = t*t*t; + push_point(a, + b0*x0 + b1*cx1 + b2*cx2 + b3*x1, + b0*y0 + b1*cy1 + b2*cy2 + b3*y1); + } + return a; +} + +TinyBlocks_JsonValue* tb_curves_quadratic(double x0, double y0, + double cx, double cy, + double x1, double y1, int steps) { + steps = clamp_steps(steps); + TinyBlocks_JsonValue* a = tb_json_array(); + if (!a) return NULL; + for (int i = 0; i <= steps; i++) { + double t = (double)i / steps, u = 1.0 - t; + push_point(a, + u*u*x0 + 2*u*t*cx + t*t*x1, + u*u*y0 + 2*u*t*cy + t*t*y1); + } + return a; +} + +TinyBlocks_JsonValue* tb_curves_arc(double cx, double cy, double r, + double a0_deg, double a1_deg, int steps) { + steps = clamp_steps(steps); + TinyBlocks_JsonValue* a = tb_json_array(); + if (!a) return NULL; + double a0 = a0_deg * M_PI / 180.0, a1 = a1_deg * M_PI / 180.0; + for (int i = 0; i <= steps; i++) { + double t = (double)i / steps; + double ang = a0 + (a1 - a0) * t; + push_point(a, cx + r*cos(ang), cy + r*sin(ang)); + } + return a; +} + +TinyBlocks_JsonValue* tb_curves_circle(double cx, double cy, double r, int steps) { + return tb_curves_arc(cx, cy, r, 0.0, 360.0, steps); +} + +/* -- Leitura ------------------------------------------------------------- */ + +int tb_curves_count(const TinyBlocks_JsonValue* pts) { + if (!pts || tb_json_kind(pts) != TB_JSON_ARRAY) return 0; + return (int)tb_json_array_length(pts); +} + +bool tb_curves_point(const TinyBlocks_JsonValue* pts, int i, double* x, double* y) { + if (!pts || tb_json_kind(pts) != TB_JSON_ARRAY) return false; + if (i < 0 || (uint32_t)i >= tb_json_array_length(pts)) return false; + TinyBlocks_JsonValue* p = tb_json_array_get(pts, (uint32_t)i); + if (!p) return false; + bool ok = tb_json_kind(p) == TB_JSON_ARRAY && tb_json_array_length(p) >= 2; + if (ok) { + TinyBlocks_JsonValue* px = tb_json_array_get(p, 0); + TinyBlocks_JsonValue* py = tb_json_array_get(p, 1); + if (x) *x = tb_json_as_number(px, 0.0); + if (y) *y = tb_json_as_number(py, 0.0); + tb_json_release(px); tb_json_release(py); + } + tb_json_release(p); + return ok; +} + +/* -- Helpers de entrada -------------------------------------------------- */ + +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; + if (v.kind == TB_VALUE_TEXT && v.as.text) return atof(v.as.text); + return 0.0; +} + +static int64_t in_int(TinyBlocks_Block* self, TinyBlocks* rt, uint32_t i) { + TinyBlocks_Value v = tb_block_get_input(self, rt, i); + if (v.kind == TB_VALUE_INTEGER) return v.as.integer; + if (v.kind == TB_VALUE_REAL) return (int64_t)v.as.real; + return 0; +} + +static TinyBlocks_JsonValue* in_pts(TinyBlocks_Block* self, TinyBlocks* rt, uint32_t i) { + TinyBlocks_Value v = tb_block_get_input(self, rt, i); + return (v.kind == TB_VALUE_JSON) ? v.as.json : NULL; +} + +static void out_pts(TinyBlocks_Block* self, TinyBlocks_JsonValue* pts) { + tb_block_set_output(self, 0, tb_value_json(pts)); + tb_json_release(pts); + self->dirty = false; +} + +/* -- Blocos -------------------------------------------------------------- */ + +static void eval_line(TinyBlocks_Block* self, TinyBlocks* rt) { + out_pts(self, tb_curves_line(in_real(self,rt,0), in_real(self,rt,1), + in_real(self,rt,2), in_real(self,rt,3), + (int)in_int(self,rt,4))); +} +static void eval_bezier(TinyBlocks_Block* self, TinyBlocks* rt) { + out_pts(self, tb_curves_bezier(in_real(self,rt,0), in_real(self,rt,1), + in_real(self,rt,2), in_real(self,rt,3), + in_real(self,rt,4), in_real(self,rt,5), + in_real(self,rt,6), in_real(self,rt,7), + (int)in_int(self,rt,8))); +} +static void eval_quadratic(TinyBlocks_Block* self, TinyBlocks* rt) { + out_pts(self, tb_curves_quadratic(in_real(self,rt,0), in_real(self,rt,1), + in_real(self,rt,2), in_real(self,rt,3), + in_real(self,rt,4), in_real(self,rt,5), + (int)in_int(self,rt,6))); +} +static void eval_arc(TinyBlocks_Block* self, TinyBlocks* rt) { + out_pts(self, tb_curves_arc(in_real(self,rt,0), in_real(self,rt,1), + in_real(self,rt,2), in_real(self,rt,3), + in_real(self,rt,4), (int)in_int(self,rt,5))); +} +static void eval_circle(TinyBlocks_Block* self, TinyBlocks* rt) { + out_pts(self, tb_curves_circle(in_real(self,rt,0), in_real(self,rt,1), + in_real(self,rt,2), (int)in_int(self,rt,3))); +} +static void eval_count(TinyBlocks_Block* self, TinyBlocks* rt) { + tb_block_set_output(self, 0, tb_value_integer(tb_curves_count(in_pts(self,rt,0)))); + self->dirty = false; +} +static void eval_pointx(TinyBlocks_Block* self, TinyBlocks* rt) { + double x = 0, y = 0; + tb_curves_point(in_pts(self,rt,0), (int)in_int(self,rt,1), &x, &y); + tb_block_set_output(self, 0, tb_value_real(x)); + self->dirty = false; +} +static void eval_pointy(TinyBlocks_Block* self, TinyBlocks* rt) { + double x = 0, y = 0; + tb_curves_point(in_pts(self,rt,0), (int)in_int(self,rt,1), &x, &y); + tb_block_set_output(self, 0, tb_value_real(y)); + self->dirty = false; +} + +/* -- Registro ------------------------------------------------------------ */ + +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 curves_init(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) { + static const TinyBlocks_Category cat = { + "Curvas", + { 233/255.f, 30/255.f, 99/255.f, 1.f}, + { 194/255.f, 24/255.f, 91/255.f, 1.f}, + }; + tb_library_set_category(lib, &cat); + + static const TinyBlocks_SlotDesc pts_out[] = {{"pontos", TB_VALUE_JSON}}; + static const TinyBlocks_SlotDesc num_out[] = {{"valor", TB_VALUE_REAL}}; + static const TinyBlocks_SlotDesc int_out[] = {{"n", TB_VALUE_INTEGER}}; + + TinyBlocks_SlotDesc line_in[5], bez_in[9], quad_in[7], arc_in[6], circ_in[4]; + TinyBlocks_SlotDesc cnt_in[1], pt_in[2]; + const char* ln[5] = {"x0","y0","x1","y1","passos"}; + const char* bn[9] = {"x0","y0","cx1","cy1","cx2","cy2","x1","y1","passos"}; + const char* qn[7] = {"x0","y0","cx","cy","x1","y1","passos"}; + const char* an[6] = {"cx","cy","raio","ang0","ang1","passos"}; + const char* cn[4] = {"cx","cy","raio","passos"}; + for (int i = 0; i < 5; i++) line_in[i] = (TinyBlocks_SlotDesc){ln[i], TB_VALUE_NONE}; + for (int i = 0; i < 9; i++) bez_in[i] = (TinyBlocks_SlotDesc){bn[i], TB_VALUE_NONE}; + for (int i = 0; i < 7; i++) quad_in[i] = (TinyBlocks_SlotDesc){qn[i], TB_VALUE_NONE}; + for (int i = 0; i < 6; i++) arc_in[i] = (TinyBlocks_SlotDesc){an[i], TB_VALUE_NONE}; + for (int i = 0; i < 4; i++) circ_in[i] = (TinyBlocks_SlotDesc){cn[i], TB_VALUE_NONE}; + cnt_in[0] = (TinyBlocks_SlotDesc){"pontos", TB_VALUE_JSON}; + pt_in[0] = (TinyBlocks_SlotDesc){"pontos", TB_VALUE_JSON}; + pt_in[1] = (TinyBlocks_SlotDesc){"indice", TB_VALUE_INTEGER}; + + return reg(rt, lib, "Curves.Line", line_in, 5, pts_out, 1, eval_line) + && reg(rt, lib, "Curves.Bezier", bez_in, 9, pts_out, 1, eval_bezier) + && reg(rt, lib, "Curves.Quadratic", quad_in, 7, pts_out, 1, eval_quadratic) + && reg(rt, lib, "Curves.Arc", arc_in, 6, pts_out, 1, eval_arc) + && reg(rt, lib, "Curves.Circle", circ_in, 4, pts_out, 1, eval_circle) + && reg(rt, lib, "Curves.Count", cnt_in, 1, int_out, 1, eval_count) + && reg(rt, lib, "Curves.PointX", pt_in, 2, num_out, 1, eval_pointx) + && reg(rt, lib, "Curves.PointY", pt_in, 2, num_out, 1, eval_pointy); +} + +TINYBLOCKS_DEFINE_LIBRARY(curves, "tinyblocks-curves", "1.0.0", curves_init, NULL) + +const TinyBlocks_LibraryDescriptor* tb_curves_get_library(void) { + return &_tb_lib_desc_curves; +} diff --git a/libraries/tinyblocks-editor/src/tb_editor.c b/libraries/tinyblocks-editor/src/tb_editor.c index a35fa5d..868fd33 100644 --- a/libraries/tinyblocks-editor/src/tb_editor.c +++ b/libraries/tinyblocks-editor/src/tb_editor.c @@ -1221,17 +1221,34 @@ static int hit_slot(Tab* T, int bi, float x, float y) { /* === * Renderização de blocos * === */ +/* Campo de nome de variavel (slot 0 de Variables.Get/Set/Change): + * desenhado laranja (cor da categoria), como no Scratch. */ +static bool slot_is_var_name(const TinyBlocks_TypeBlock* t, int s) { + if (!t || !t->type_name || s != 0) return false; + const char* n = t->type_name; + return strcmp(n, "Variables.Get") == 0 || + strcmp(n, "Variables.Set") == 0 || + strcmp(n, "Variables.Change") == 0; +} + static void draw_slot(TinyBlocks_RenderableBlock* rb, const EdBlock* b, int s, float x, float y, Col dark, bool editing) { float sx = x + b->slot_x[s], sy = y + b->slot_y[s]; float sw = b->slot_w[s], sh = b->slot_h[s]; if (slot_is_bool(b->type, s)) { draw_hex(rb, sx, sy, sw, sh, col_mul(dark, 0.9f)); return; } + bool varname = slot_is_var_name(b->type, s); + /* Laranja num tom mais escuro que o bloco, para o campo aparecer + * (como o "vinco" do dropdown de variavel do Scratch). */ + Col border = varname ? col_mul(COL_VAR, 0.60f) : COLA(0,0,0,40); + Col fill = varname ? col_mul(COL_VAR, 0.82f) : COL_WHITE; + Col txt = varname ? COL_WHITE : COL_TEXT; + const char* src = slot_src(b, s); int lines = text_lines(src); float rad = lines > 1 ? 4.f : sh*0.5f; - rb_fill_rrect(rb, sx, sy, sw, sh, rad, editing ? COL_MENU : COLA(0,0,0,40)); - rb_fill_rrect(rb, sx+1, sy+1, sw-2, sh-2, rad-1, COL_WHITE); + rb_fill_rrect(rb, sx, sy, sw, sh, rad, editing ? COL_MENU : border); + rb_fill_rrect(rb, sx+1, sy+1, sw-2, sh-2, rad-1, fill); char line[64]; float ly = sy + (sh - lines*SLOT_LINE_H)*0.5f + SLOT_LINE_H*0.5f; @@ -1240,11 +1257,11 @@ static void draw_slot(TinyBlocks_RenderableBlock* rb, const EdBlock* b, int s, text_line(src, l, line, sizeof(line)); float lw = tw(line, FS); float lx = lines > 1 ? sx + 7 : sx + (sw - lw)*0.5f; - rb_text_c(rb, lx, ly, line, COL_TEXT, FS); + rb_text_c(rb, lx, ly, line, txt, FS); end_x = lx + lw; ly += SLOT_LINE_H; } - if (editing) rb_fill_rect(rb, end_x + 1, ly - SLOT_LINE_H - 7, 1.5f, 14, COL_TEXT); + if (editing) rb_fill_rect(rb, end_x + 1, ly - SLOT_LINE_H - 7, 1.5f, 14, txt); } static void render_chain(Ed* ed, TinyBlocks_RenderableBlock* rb, Tab* T, int head, float ox, float oy); diff --git a/tests/base64_test.c b/tests/base64_test.c new file mode 100644 index 0000000..7a3be4c --- /dev/null +++ b/tests/base64_test.c @@ -0,0 +1,91 @@ +/* + * base64_test.c - testa tinyblocks-base64: encode/decode (RFC 4648) e os + * blocos Base64.Encode / Base64.Decode. + */ +#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 void set_in(TinyBlocks_Block* b, uint32_t i, const char* v) { + TinyBlocks_Value vv = tb_value_text(v); + tb_block_set_default(b, i, vv); + tb_value_free(&vv); +} + +int main(void) { + /* -- API C --- */ + char* e = tb_base64_encode_str("Ola"); + CHECK(e && strcmp(e, "T2xh") == 0, "encode 'Ola' -> 'T2xh'"); + free(e); + + e = tb_base64_encode_str(""); + CHECK(e && strcmp(e, "") == 0, "encode vazio -> vazio"); + free(e); + + e = tb_base64_encode("f", 1); /* 1 byte -> 2 chars + '==' */ + CHECK(e && strcmp(e, "Zg==") == 0, "encode 1 byte -> 'Zg=='"); + free(e); + + e = tb_base64_encode("fo", 2); + CHECK(e && strcmp(e, "Zm8=") == 0, "encode 2 bytes -> 'Zm8='"); + free(e); + + char* d = tb_base64_decode_str("T2xh", NULL); + CHECK(d && strcmp(d, "Ola") == 0, "decode 'T2xh' -> 'Ola'"); + free(d); + + size_t n = 0; + unsigned char* raw = tb_base64_decode("Zm9vYmFy", &n); /* "foobar" */ + CHECK(raw && n == 6 && memcmp(raw, "foobar", 6) == 0, "decode 'Zm9vYmFy' -> 'foobar'"); + free(raw); + + /* round-trip de bytes arbitrarios (0..255) */ + unsigned char orig[256]; + for (int i = 0; i < 256; i++) orig[i] = (unsigned char)i; + e = tb_base64_encode(orig, sizeof(orig)); + raw = tb_base64_decode(e, &n); + CHECK(raw && n == 256 && memcmp(raw, orig, 256) == 0, "round-trip 256 bytes"); + free(raw); free(e); + + /* entrada invalida */ + CHECK(tb_base64_decode("@@@", NULL) == NULL, "decode invalido -> NULL"); + CHECK(strlen(tb_base64_last_error()) > 0, "mensagem de erro presente"); + + /* -- Blocos --- */ + TinyBlocks* rt; + tinyblocks_init(&rt, NULL); + tb_library_register(rt, tb_base64_get_library()); + + TinyBlocks_TypeBlock* tenc = tb_rt_find_type(rt, "Base64.Encode"); + TinyBlocks_TypeBlock* tdec = tb_rt_find_type(rt, "Base64.Decode"); + CHECK(tenc && tdec, "blocos Base64.Encode/Decode registrados"); + const TinyBlocks_Category* cat = tb_library_category((const TinyBlocks_LibraryBlock*)tenc->base.parent); + CHECK(cat && strcmp(cat->name, "Base64") == 0, "categoria 'Base64'"); + + if (tenc) { + TinyBlocks_Block* b = tb_block_create(rt, tenc); + set_in(b, 0, "TinyBlocks"); + tb_block_eval(b, rt); + CHECK(b->outputs[0].kind == TB_VALUE_TEXT && b->outputs[0].as.text && + strcmp(b->outputs[0].as.text, "VGlueUJsb2Nrcw==") == 0, "bloco Encode('TinyBlocks')"); + tb_block_destroy(b, rt); + } + if (tdec) { + TinyBlocks_Block* b = tb_block_create(rt, tdec); + set_in(b, 0, "VGlueUJsb2Nrcw=="); + tb_block_eval(b, rt); + CHECK(b->outputs[0].kind == TB_VALUE_TEXT && b->outputs[0].as.text && + strcmp(b->outputs[0].as.text, "TinyBlocks") == 0, "bloco Decode(...) -> 'TinyBlocks'"); + tb_block_destroy(b, rt); + } + + tinyblocks_deinit(&rt); + if (fails) printf("\n%d FALHA(S)\n", fails); + else printf("\nTUDO OK\n"); + return fails != 0; +} diff --git a/tests/curves_test.c b/tests/curves_test.c new file mode 100644 index 0000000..89ebab5 --- /dev/null +++ b/tests/curves_test.c @@ -0,0 +1,124 @@ +/* + * curves_test.c - testa tinyblocks-curves: retorno de listas de pontos + * ([[x,y],...]) e os blocos Curves.Line / Bezier / Quadratic / Arc / Circle / + * Count / PointX / PointY. + */ +#include +#include +#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 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_pts(TinyBlocks_Block* b, uint32_t i, TinyBlocks_JsonValue* pts) { + TinyBlocks_Value vv = tb_value_json(pts); + tb_block_set_default(b, i, vv); + tb_value_free(&vv); +} + +static bool near(double a, double b) { return fabs(a - b) < 1e-6; } + +int main(void) { + /* -- API C: reta ---- */ + TinyBlocks_JsonValue* ln = tb_curves_line(0, 0, 10, 20, 4); + CHECK(tb_curves_count(ln) == 5, "reta com 4 passos -> 5 pontos"); + double x, y; + CHECK(tb_curves_point(ln, 2, &x, &y) && near(x, 5) && near(y, 10), "reta: ponto do meio (5,10)"); + CHECK(tb_curves_point(ln, 4, &x, &y) && near(x, 10) && near(y, 20), "reta: ponto final (10,20)"); + CHECK(!tb_curves_point(ln, 5, &x, &y), "reta: indice fora -> false"); + + /* -- Bezier cubica: extremos e simetria --- */ + TinyBlocks_JsonValue* bz = tb_curves_bezier(0, 0, 0, 10, 10, 10, 10, 0, 8); + CHECK(tb_curves_count(bz) == 9, "bezier com 8 passos -> 9 pontos"); + tb_curves_point(bz, 0, &x, &y); + CHECK(near(x, 0) && near(y, 0), "bezier: comeca em (0,0)"); + tb_curves_point(bz, 8, &x, &y); + CHECK(near(x, 10) && near(y, 0), "bezier: termina em (10,0)"); + /* t=0.5 -> (5, 7.5) para essa curva */ + tb_curves_point(bz, 4, &x, &y); + CHECK(near(x, 5) && near(y, 7.5), "bezier: ponto medio (5,7.5)"); + + /* -- Quadratica --- */ + TinyBlocks_JsonValue* qd = tb_curves_quadratic(0, 0, 5, 10, 10, 0, 2); + tb_curves_point(qd, 1, &x, &y); + CHECK(near(x, 5) && near(y, 5), "quadratica: topo (5,5)"); + + /* -- Arco / circulo --- */ + TinyBlocks_JsonValue* ar = tb_curves_arc(0, 0, 10, 0, 90, 2); + tb_curves_point(ar, 0, &x, &y); + CHECK(near(x, 10) && near(y, 0), "arco: comeca em (10,0)"); + tb_curves_point(ar, 2, &x, &y); + CHECK(near(x, 0) && near(y, 10), "arco: termina em (0,10)"); + + TinyBlocks_JsonValue* ci = tb_curves_circle(0, 0, 5, 4); + CHECK(tb_curves_count(ci) == 5, "circulo fechado com 4 passos -> 5 pontos"); + tb_curves_point(ci, 0, &x, &y); + tb_curves_point(ci, 4, &x, &y); + CHECK(near(x, 5) && near(y, 0), "circulo: fecha no ponto inicial"); + + /* limites de passos */ + TinyBlocks_JsonValue* z = tb_curves_line(0, 0, 1, 1, 0); + CHECK(tb_curves_count(z) == 2, "passos=0 vira 1 -> 2 pontos"); + tb_json_release(z); + + tb_json_release(ln); tb_json_release(bz); tb_json_release(qd); + tb_json_release(ar); tb_json_release(ci); + + /* -- Blocos --- */ + TinyBlocks* rt; + tinyblocks_init(&rt, NULL); + tb_library_register(rt, tb_curves_get_library()); + + const char* names[] = {"Curves.Line","Curves.Bezier","Curves.Quadratic", + "Curves.Arc","Curves.Circle","Curves.Count", + "Curves.PointX","Curves.PointY"}; + 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 Curves.* registrados"); + + TinyBlocks_TypeBlock* tl = tb_rt_find_type(rt, "Curves.Line"); + const TinyBlocks_Category* cat = tb_library_category((const TinyBlocks_LibraryBlock*)tl->base.parent); + CHECK(cat && strcmp(cat->name, "Curvas") == 0, "categoria 'Curvas'"); + + /* Curves.Line via bloco */ + TinyBlocks_Block* bl = tb_block_create(rt, tl); + set_num(bl, 0, 0); set_num(bl, 1, 0); + set_num(bl, 2, 8); set_num(bl, 3, 4); + set_num(bl, 4, 2); + tb_block_eval(bl, rt); + CHECK(bl->outputs[0].kind == TB_VALUE_JSON, "Curves.Line devolve JSON"); + CHECK(tb_curves_count(bl->outputs[0].as.json) == 3, "Curves.Line devolve 3 pontos"); + + /* Curves.Count recebe a lista do bloco anterior */ + TinyBlocks_TypeBlock* tc = tb_rt_find_type(rt, "Curves.Count"); + TinyBlocks_Block* bc = tb_block_create(rt, tc); + set_pts(bc, 0, bl->outputs[0].as.json); + tb_block_eval(bc, rt); + CHECK(bc->outputs[0].as.integer == 3, "Curves.Count -> 3"); + + /* Curves.PointX(2) da lista */ + TinyBlocks_TypeBlock* tpx = tb_rt_find_type(rt, "Curves.PointX"); + TinyBlocks_Block* bpx = tb_block_create(rt, tpx); + set_pts(bpx, 0, bl->outputs[0].as.json); + TinyBlocks_Value idx = tb_value_integer(2); + tb_block_set_default(bpx, 1, idx); + tb_block_eval(bpx, rt); + CHECK(near(bpx->outputs[0].as.real, 8.0), "Curves.PointX(2) -> 8"); + + tb_block_destroy(bl, rt); tb_block_destroy(bc, rt); tb_block_destroy(bpx, rt); + tinyblocks_deinit(&rt); + + if (fails) printf("\n%d FALHA(S)\n", fails); + else printf("\nTUDO OK\n"); + return fails != 0; +}