Adiciona tinyblocks-opentype (fontes OpenType via FreeType) com teste e bundling no AppImage
This commit is contained in:
1 parent
a921f6574d
commit
22ae18f508
6 files changed
+577
-3
No files matched your search
@@ -285,7 +285,7 @@ run-editor: editor ## Compila e abre o editor visual
|
|||||||
|
|
||||||
# ── Empacotamento AppImage ──────────────────────────────────────────────
|
# ── Empacotamento AppImage ──────────────────────────────────────────────
|
||||||
# Libraries com blocos (categoria) que vao empacotadas como extensoes.
|
# Libraries com blocos (categoria) que vao empacotadas como extensoes.
|
||||||
APPIMAGE_LIBS := $(CAT_LIBS) toml json xml csv blueprint base64 curves ppm bmp png image vg svg jpg
|
APPIMAGE_LIBS := $(CAT_LIBS) toml json xml csv blueprint base64 curves ppm bmp png image vg svg jpg syntax regex markdown opentype
|
||||||
|
|
||||||
APP_NAME := TinyBlocks
|
APP_NAME := TinyBlocks
|
||||||
APP_VER := 0.1.0
|
APP_VER := 0.1.0
|
||||||
@@ -529,7 +529,7 @@ PNG_CFLAGS := $(shell pkg-config --cflags libpng 2>/dev/null)
|
|||||||
PNG_LIBS := $(shell pkg-config --libs libpng 2>/dev/null)
|
PNG_LIBS := $(shell pkg-config --libs libpng 2>/dev/null)
|
||||||
|
|
||||||
FREETYPE_CFLAGS := $(shell pkg-config --cflags freetype2 2>/dev/null)
|
FREETYPE_CFLAGS := $(shell pkg-config --cflags freetype2 2>/dev/null)
|
||||||
FREETYPE_LIBS := $(shell pkg-config --libs freetype2 2>/dev/null)
|
FREETYPE_LIBS := $(shell pkg-config --libs freetype2 2>/dev/null) -lm
|
||||||
EXTRA_TEST_LIBS :=
|
EXTRA_TEST_LIBS :=
|
||||||
$(BUILD)/png_test: tests/png_test.c $(PNG_DIR)/src/tb_png.c static
|
$(BUILD)/png_test: tests/png_test.c $(PNG_DIR)/src/tb_png.c static
|
||||||
@mkdir -p $(BUILD)
|
@mkdir -p $(BUILD)
|
||||||
@@ -599,7 +599,7 @@ test-markdown: $(BUILD)/markdown_test ## Roda o teste da markdown (ASan/UBSan)
|
|||||||
|
|
||||||
$(BUILD)/opentype_test: tests/opentype_test.c $(OT_DIR)/src/tb_opentype.c static
|
$(BUILD)/opentype_test: tests/opentype_test.c $(OT_DIR)/src/tb_opentype.c static
|
||||||
@mkdir -p $(BUILD)
|
@mkdir -p $(BUILD)
|
||||||
$(CC) $(CFLAGS_DBG) -I$(OT_DIR)/include $(OT_DIR)/src/tb_opentype.c tests/opentype_test.c \
|
$(CC) $(CFLAGS_DBG) -I$(OT_DIR)/include $(FREETYPE_CFLAGS) $(OT_DIR)/src/tb_opentype.c tests/opentype_test.c \
|
||||||
-o $@ -Wl,--start-group $(LIB_A) -Wl,--end-group $(FREETYPE_LIBS) $(EXTRA_TEST_LIBS)
|
-o $@ -Wl,--start-group $(LIB_A) -Wl,--end-group $(FREETYPE_LIBS) $(EXTRA_TEST_LIBS)
|
||||||
|
|
||||||
test-opentype: $(BUILD)/opentype_test ## Roda o teste da opentype (ASan/UBSan)
|
test-opentype: $(BUILD)/opentype_test ## Roda o teste da opentype (ASan/UBSan)
|
||||||
|
|||||||
@@ -0,0 +1,31 @@
|
|||||||
|
# Makefile da library opentype (categoria "OpenType", via FreeType 2)
|
||||||
|
CC := gcc
|
||||||
|
BUILD := build
|
||||||
|
|
||||||
|
FT_CFLAGS := $(shell pkg-config --cflags freetype2 2>/dev/null)
|
||||||
|
FT_LIBS := $(shell pkg-config --libs freetype2 2>/dev/null) -lm
|
||||||
|
|
||||||
|
CFLAGS := -std=c11 -Wall -Wextra -pedantic -fvisibility=hidden \
|
||||||
|
-D_POSIX_C_SOURCE=200809L -I../../include -Iinclude $(FT_CFLAGS)
|
||||||
|
|
||||||
|
OBJ := $(BUILD)/tb_opentype.o
|
||||||
|
LIB_A := $(BUILD)/libtinyblocks-opentype.a
|
||||||
|
LIB_SO := $(BUILD)/libtinyblocks-opentype.so
|
||||||
|
|
||||||
|
.PHONY: all static shared clean
|
||||||
|
all: static shared
|
||||||
|
static: $(LIB_A)
|
||||||
|
shared: $(LIB_SO)
|
||||||
|
|
||||||
|
$(OBJ): src/tb_opentype.c include/tb_opentype.h
|
||||||
|
@mkdir -p $(BUILD)
|
||||||
|
$(CC) $(CFLAGS) -fPIC -c $< -o $@
|
||||||
|
|
||||||
|
$(LIB_A): $(OBJ)
|
||||||
|
ar rcs $@ $^
|
||||||
|
|
||||||
|
$(LIB_SO): $(OBJ)
|
||||||
|
$(CC) -shared -o $@ $^ $(FT_LIBS)
|
||||||
|
|
||||||
|
clean:
|
||||||
|
$(RM) -r $(BUILD)
|
||||||
@@ -0,0 +1,51 @@
|
|||||||
|
/*
|
||||||
|
* TinyBlocks - tb_opentype.h
|
||||||
|
* Library tinyblocks-opentype: leitura de fontes OpenType/TrueType (metricas,
|
||||||
|
* contornos de glifos e rasterizacao de texto).
|
||||||
|
*
|
||||||
|
* Observacao: nao existe uma biblioteca C "libopentype" de facto; este modulo
|
||||||
|
* usa a FreeType 2 (que faz o parsing de tabelas OpenType/TrueType: head, hhea,
|
||||||
|
* hmtx, cmap, glyf/CFF, ...). E' o parser OpenType usado pela industria.
|
||||||
|
*
|
||||||
|
* Independente do editor.
|
||||||
|
*/
|
||||||
|
#ifndef TINYBLOCKS_OPENTYPE_H
|
||||||
|
#define TINYBLOCKS_OPENTYPE_H
|
||||||
|
|
||||||
|
#include <tinyblocks/tinyblocks.h>
|
||||||
|
|
||||||
|
TB_API const TinyBlocks_LibraryDescriptor* tb_opentype_get_library(void);
|
||||||
|
|
||||||
|
/* -- API C --------------------------------------------------------------- */
|
||||||
|
|
||||||
|
/* Carrega uma fonte; devolve um handle (>0) ou 0 em erro. */
|
||||||
|
TB_API int tb_opentype_load(const char* path);
|
||||||
|
TB_API void tb_opentype_close(int handle);
|
||||||
|
|
||||||
|
TB_API int tb_opentype_units_per_em(int handle); /* 0 em erro */
|
||||||
|
TB_API int tb_opentype_glyph_count(int handle);
|
||||||
|
TB_API int tb_opentype_num_faces(int handle);
|
||||||
|
TB_API bool tb_opentype_has_char(int handle, long codepoint);
|
||||||
|
|
||||||
|
/* Metricas em pixels para um tamanho: ascent/descent/height. */
|
||||||
|
TB_API void tb_opentype_metrics(int handle, int pixel_size,
|
||||||
|
double* ascent, double* descent, double* height);
|
||||||
|
|
||||||
|
/* Largura (avanco total) de um texto UTF-8 em pixels. */
|
||||||
|
TB_API double tb_opentype_advance(int handle, const char* text, int pixel_size);
|
||||||
|
|
||||||
|
/* Contornos de um caractere (UTF-8) como lista de contornos, cada um uma
|
||||||
|
* lista de pontos [x,y] em pixels; origem no canto superior esquerdo.
|
||||||
|
* Devolve um array JSON (retido) ou NULL. */
|
||||||
|
TB_API TinyBlocks_JsonValue* tb_opentype_outline(int handle, const char* text, int pixel_size);
|
||||||
|
|
||||||
|
/* Rasteriza um texto numa imagem RGBA (fundo transparente). */
|
||||||
|
TB_API TinyBlocks_Image* tb_opentype_render(int handle, const char* text, int pixel_size,
|
||||||
|
int r, int g, int b);
|
||||||
|
|
||||||
|
/* Caminho padrao de fonte do sistema (para testes/demos) ou NULL. */
|
||||||
|
TB_API const char* tb_opentype_default_font(void);
|
||||||
|
|
||||||
|
TB_API const char* tb_opentype_last_error(void);
|
||||||
|
|
||||||
|
#endif /* TINYBLOCKS_OPENTYPE_H */
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
# Metadados da library opentype. Lido pela UI/build sem recompilar o .so.
|
||||||
|
[library]
|
||||||
|
name = "tinyblocks-opentype"
|
||||||
|
version = "1.0.0"
|
||||||
|
category = "OpenType"
|
||||||
|
description = "Fontes OpenType/TrueType: metricas, contornos e rasterizacao (FreeType)"
|
||||||
|
color = [63, 81, 181, 255]
|
||||||
|
dark = [40, 53, 147, 255]
|
||||||
|
|
||||||
|
[build]
|
||||||
|
sources = ["src/tb_opentype.c"]
|
||||||
|
includes = ["include"]
|
||||||
|
shared = "build/libtinyblocks-opentype.so"
|
||||||
|
static = "build/libtinyblocks-opentype.a"
|
||||||
|
depends = ["freetype2"]
|
||||||
@@ -0,0 +1,389 @@
|
|||||||
|
/*
|
||||||
|
* TinyBlocks - tb_opentype.c
|
||||||
|
* Categoria "OpenType": OpenType.Load / Metrics / Advance / Outline / Render.
|
||||||
|
* Parser de fontes OpenType/TrueType via FreeType 2.
|
||||||
|
*/
|
||||||
|
#define _POSIX_C_SOURCE 200809L
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <math.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
#include <ft2build.h>
|
||||||
|
#include FT_FREETYPE_H
|
||||||
|
#include FT_OUTLINE_H
|
||||||
|
|
||||||
|
#include <tinyblocks/tinyblocks.h>
|
||||||
|
#include <tinyblocks/tb_json_block.h>
|
||||||
|
#include "tb_opentype.h"
|
||||||
|
|
||||||
|
#define OT_MAX 32
|
||||||
|
|
||||||
|
static char s_err[200] = "";
|
||||||
|
const char* tb_opentype_last_error(void) { return s_err; }
|
||||||
|
|
||||||
|
static FT_Library s_ft = NULL;
|
||||||
|
static bool s_ft_tried = false;
|
||||||
|
|
||||||
|
static bool ft_init(void) {
|
||||||
|
if (s_ft) return true;
|
||||||
|
if (s_ft_tried) return false;
|
||||||
|
s_ft_tried = true;
|
||||||
|
if (FT_Init_FreeType(&s_ft) != 0) {
|
||||||
|
s_ft = NULL;
|
||||||
|
snprintf(s_err, sizeof(s_err), "falha ao inicializar a FreeType");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static struct {
|
||||||
|
FT_Face face;
|
||||||
|
int used;
|
||||||
|
} s_faces[OT_MAX];
|
||||||
|
|
||||||
|
static FT_Face face_of(int h) {
|
||||||
|
if (h <= 0 || h > OT_MAX) return NULL;
|
||||||
|
return s_faces[h - 1].used ? s_faces[h - 1].face : NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
int tb_opentype_load(const char* path) {
|
||||||
|
s_err[0] = 0;
|
||||||
|
if (!path) { snprintf(s_err, sizeof(s_err), "caminho nulo"); return 0; }
|
||||||
|
if (!ft_init()) return 0;
|
||||||
|
int slot = -1;
|
||||||
|
for (int i = 0; i < OT_MAX; i++) if (!s_faces[i].used) { slot = i; break; }
|
||||||
|
if (slot < 0) { snprintf(s_err, sizeof(s_err), "limite de fontes abertas (%d)", OT_MAX); return 0; }
|
||||||
|
FT_Face face = NULL;
|
||||||
|
FT_Error e = FT_New_Face(s_ft, path, 0, &face);
|
||||||
|
if (e != 0 || !face) {
|
||||||
|
snprintf(s_err, sizeof(s_err), "nao abriu '%s' (FreeType erro %d)", path, (int)e);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
s_faces[slot].face = face;
|
||||||
|
s_faces[slot].used = 1;
|
||||||
|
return slot + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
void tb_opentype_close(int handle) {
|
||||||
|
FT_Face f = face_of(handle);
|
||||||
|
if (!f) return;
|
||||||
|
FT_Done_Face(f);
|
||||||
|
s_faces[handle - 1].face = NULL;
|
||||||
|
s_faces[handle - 1].used = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int tb_opentype_units_per_em(int handle) {
|
||||||
|
FT_Face f = face_of(handle);
|
||||||
|
if (!f) { snprintf(s_err, sizeof(s_err), "fonte invalida"); return 0; }
|
||||||
|
return f->units_per_EM ? f->units_per_EM : (int)f->units_per_EM;
|
||||||
|
}
|
||||||
|
|
||||||
|
int tb_opentype_glyph_count(int handle) {
|
||||||
|
FT_Face f = face_of(handle);
|
||||||
|
if (!f) return 0;
|
||||||
|
return (int)f->num_glyphs;
|
||||||
|
}
|
||||||
|
|
||||||
|
int tb_opentype_num_faces(int handle) {
|
||||||
|
FT_Face f = face_of(handle);
|
||||||
|
if (!f) return 0;
|
||||||
|
return (int)f->num_faces;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool tb_opentype_has_char(int handle, long codepoint) {
|
||||||
|
FT_Face f = face_of(handle);
|
||||||
|
if (!f) return false;
|
||||||
|
return FT_Get_Char_Index(f, (FT_ULong)codepoint) != 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static size_t utf8_next(const char* s, unsigned* cp) {
|
||||||
|
const unsigned char* p = (const unsigned char*)s;
|
||||||
|
if (p[0] < 0x80) { *cp = p[0]; return 1; }
|
||||||
|
if ((p[0] & 0xE0) == 0xC0 && p[1]) { *cp = ((p[0] & 0x1F) << 6) | (p[1] & 0x3F); return 2; }
|
||||||
|
if ((p[0] & 0xF0) == 0xE0 && p[1] && p[2]) {
|
||||||
|
*cp = ((p[0] & 0x0F) << 12) | ((p[1] & 0x3F) << 6) | (p[2] & 0x3F); return 3;
|
||||||
|
}
|
||||||
|
if ((p[0] & 0xF8) == 0xF0 && p[1] && p[2] && p[3]) {
|
||||||
|
*cp = ((p[0] & 0x07) << 18) | ((p[1] & 0x3F) << 12) | ((p[2] & 0x3F) << 6) | (p[3] & 0x3F);
|
||||||
|
return 4;
|
||||||
|
}
|
||||||
|
*cp = p[0]; return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
void tb_opentype_metrics(int handle, int pixel_size, double* ascent, double* descent, double* height) {
|
||||||
|
if (ascent) *ascent = 0;
|
||||||
|
if (descent) *descent = 0;
|
||||||
|
if (height) *height = 0;
|
||||||
|
FT_Face f = face_of(handle);
|
||||||
|
if (!f || pixel_size <= 0) return;
|
||||||
|
FT_Set_Pixel_Sizes(f, 0, (FT_UInt)pixel_size);
|
||||||
|
double a = f->size->metrics.ascender / 64.0;
|
||||||
|
double d = f->size->metrics.descender / 64.0;
|
||||||
|
double h = f->size->metrics.height / 64.0;
|
||||||
|
if (ascent) *ascent = a;
|
||||||
|
if (descent) *descent = d;
|
||||||
|
if (height) *height = h;
|
||||||
|
}
|
||||||
|
|
||||||
|
double tb_opentype_advance(int handle, const char* text, int pixel_size) {
|
||||||
|
FT_Face f = face_of(handle);
|
||||||
|
if (!f || !text || pixel_size <= 0) return 0;
|
||||||
|
FT_Set_Pixel_Sizes(f, 0, (FT_UInt)pixel_size);
|
||||||
|
double w = 0;
|
||||||
|
const char* p = text;
|
||||||
|
while (*p) {
|
||||||
|
unsigned cp;
|
||||||
|
p += utf8_next(p, &cp);
|
||||||
|
if (FT_Load_Char(f, cp, FT_LOAD_DEFAULT) == 0)
|
||||||
|
w += f->glyph->advance.x / 64.0;
|
||||||
|
}
|
||||||
|
return w;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void push_point(TinyBlocks_JsonValue* arr, double x, double y) {
|
||||||
|
TinyBlocks_JsonValue* pt = tb_json_array();
|
||||||
|
if (!pt) return;
|
||||||
|
TinyBlocks_JsonValue* nx = tb_json_number(x);
|
||||||
|
TinyBlocks_JsonValue* ny = tb_json_number(y);
|
||||||
|
if (nx) { tb_json_array_push(pt, nx); tb_json_release(nx); }
|
||||||
|
if (ny) { tb_json_array_push(pt, ny); tb_json_release(ny); }
|
||||||
|
tb_json_array_push(arr, pt);
|
||||||
|
tb_json_release(pt);
|
||||||
|
}
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
TinyBlocks_JsonValue* root;
|
||||||
|
TinyBlocks_JsonValue* cur;
|
||||||
|
double px, py; /* ponto atual */
|
||||||
|
double sx, sy; /* inicio do contorno */
|
||||||
|
int err;
|
||||||
|
} OutCtx;
|
||||||
|
|
||||||
|
static int ot_move(const FT_Vector* to, void* user) {
|
||||||
|
OutCtx* c = (OutCtx*)user;
|
||||||
|
c->cur = tb_json_array();
|
||||||
|
if (!c->cur) { c->err = 1; return 1; }
|
||||||
|
tb_json_array_push(c->root, c->cur);
|
||||||
|
tb_json_release(c->cur);
|
||||||
|
c->px = c->sx = to->x / 64.0;
|
||||||
|
c->py = c->sy = -to->y / 64.0; /* y para baixo */
|
||||||
|
push_point(c->cur, c->px, c->py);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
static int ot_line(const FT_Vector* to, void* user) {
|
||||||
|
OutCtx* c = (OutCtx*)user;
|
||||||
|
c->px = to->x / 64.0; c->py = -to->y / 64.0;
|
||||||
|
push_point(c->cur, c->px, c->py);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
static int ot_conic(const FT_Vector* ctl, const FT_Vector* to, void* user) {
|
||||||
|
OutCtx* c = (OutCtx*)user;
|
||||||
|
double x0 = c->px, y0 = c->py;
|
||||||
|
double cx = ctl->x / 64.0, cy = -ctl->y / 64.0;
|
||||||
|
double x1 = to->x / 64.0, y1 = -to->y / 64.0;
|
||||||
|
for (int i = 1; i <= 10; i++) {
|
||||||
|
double t = i / 10.0, u = 1 - t;
|
||||||
|
double x = u*u*x0 + 2*u*t*cx + t*t*x1;
|
||||||
|
double y = u*u*y0 + 2*u*t*cy + t*t*y1;
|
||||||
|
push_point(c->cur, x, y);
|
||||||
|
}
|
||||||
|
c->px = x1; c->py = y1;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
static int ot_cubic(const FT_Vector* c1, const FT_Vector* c2, const FT_Vector* to, void* user) {
|
||||||
|
OutCtx* c = (OutCtx*)user;
|
||||||
|
double x0 = c->px, y0 = c->py;
|
||||||
|
double a1x = c1->x / 64.0, a1y = -c1->y / 64.0;
|
||||||
|
double a2x = c2->x / 64.0, a2y = -c2->y / 64.0;
|
||||||
|
double x1 = to->x / 64.0, y1 = -to->y / 64.0;
|
||||||
|
for (int i = 1; i <= 12; i++) {
|
||||||
|
double t = i / 12.0, u = 1 - t;
|
||||||
|
double x = u*u*u*x0 + 3*u*u*t*a1x + 3*u*t*t*a2x + t*t*t*x1;
|
||||||
|
double y = u*u*u*y0 + 3*u*u*t*a1y + 3*u*t*t*a2y + t*t*t*y1;
|
||||||
|
push_point(c->cur, x, y);
|
||||||
|
}
|
||||||
|
c->px = x1; c->py = y1;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
TinyBlocks_JsonValue* tb_opentype_outline(int handle, const char* text, int pixel_size) {
|
||||||
|
s_err[0] = 0;
|
||||||
|
FT_Face f = face_of(handle);
|
||||||
|
if (!f) { snprintf(s_err, sizeof(s_err), "fonte invalida"); return NULL; }
|
||||||
|
if (!text || !*text || pixel_size <= 0) return tb_json_array();
|
||||||
|
FT_Set_Pixel_Sizes(f, 0, (FT_UInt)pixel_size);
|
||||||
|
unsigned cp;
|
||||||
|
utf8_next(text, &cp);
|
||||||
|
if (FT_Load_Char(f, cp, FT_LOAD_NO_BITMAP) != 0) {
|
||||||
|
snprintf(s_err, sizeof(s_err), "nao carregou o glifo");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
if ((f->glyph->format != FT_GLYPH_FORMAT_OUTLINE)) {
|
||||||
|
snprintf(s_err, sizeof(s_err), "glifo sem contorno (fonte bitmap?)");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
OutCtx c; memset(&c, 0, sizeof(c));
|
||||||
|
c.root = tb_json_array();
|
||||||
|
if (!c.root) return NULL;
|
||||||
|
FT_Outline_Funcs funcs = { ot_move, ot_line, ot_conic, ot_cubic, 0, 0 };
|
||||||
|
FT_Outline_Decompose(&f->glyph->outline, &funcs, &c);
|
||||||
|
if (c.err) { tb_json_release(c.root); return NULL; }
|
||||||
|
return c.root;
|
||||||
|
}
|
||||||
|
|
||||||
|
TinyBlocks_Image* tb_opentype_render(int handle, const char* text, int pixel_size,
|
||||||
|
int r, int g, int b) {
|
||||||
|
s_err[0] = 0;
|
||||||
|
FT_Face f = face_of(handle);
|
||||||
|
if (!f) { snprintf(s_err, sizeof(s_err), "fonte invalida"); return NULL; }
|
||||||
|
if (!text) text = "";
|
||||||
|
if (pixel_size <= 0) pixel_size = 16;
|
||||||
|
FT_Set_Pixel_Sizes(f, 0, (FT_UInt)pixel_size);
|
||||||
|
double asc = f->size->metrics.ascender / 64.0;
|
||||||
|
double dsc = f->size->metrics.descender / 64.0;
|
||||||
|
int W = (int)ceil(tb_opentype_advance(handle, text, pixel_size)) + 2;
|
||||||
|
int H = (int)ceil(asc - dsc) + 2;
|
||||||
|
if (W < 1) W = 1;
|
||||||
|
if (H < 1) H = 1;
|
||||||
|
TinyBlocks_Image* img = tb_image_new(W, H, 4);
|
||||||
|
if (!img) { snprintf(s_err, sizeof(s_err), "sem memoria"); return NULL; }
|
||||||
|
/* fundo transparente */
|
||||||
|
memset(tb_image_data(img), 0, (size_t)W * (size_t)H * 4);
|
||||||
|
|
||||||
|
double penx = 1, baseline = 1 + asc;
|
||||||
|
const char* p = text;
|
||||||
|
while (*p) {
|
||||||
|
unsigned cp;
|
||||||
|
p += utf8_next(p, &cp);
|
||||||
|
if (FT_Load_Char(f, cp, FT_LOAD_RENDER) != 0) continue;
|
||||||
|
FT_GlyphSlot s = f->glyph;
|
||||||
|
int gx = (int)(penx + s->bitmap_left);
|
||||||
|
int gy = (int)(baseline - s->bitmap_top);
|
||||||
|
const unsigned char* buf = s->bitmap.buffer;
|
||||||
|
for (int yy = 0; yy < (int)s->bitmap.rows; yy++) {
|
||||||
|
int dy = gy + yy;
|
||||||
|
if (dy < 0 || dy >= H) continue;
|
||||||
|
for (int xx = 0; xx < (int)s->bitmap.width; xx++) {
|
||||||
|
int dx = gx + xx;
|
||||||
|
if (dx < 0 || dx >= W) continue;
|
||||||
|
unsigned cov = buf[yy * s->bitmap.pitch + xx];
|
||||||
|
if (!cov) continue;
|
||||||
|
uint8_t* px = tb_image_data(img) + ((size_t)dy * W + dx) * 4;
|
||||||
|
px[0] = (uint8_t)r; px[1] = (uint8_t)g; px[2] = (uint8_t)b;
|
||||||
|
px[3] = (uint8_t)cov;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
penx += s->advance.x / 64.0;
|
||||||
|
}
|
||||||
|
return img;
|
||||||
|
}
|
||||||
|
|
||||||
|
const char* tb_opentype_default_font(void) {
|
||||||
|
static const char* cands[] = {
|
||||||
|
"/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf",
|
||||||
|
"/usr/share/fonts/truetype/dejavu/DejaVuSansMono.ttf",
|
||||||
|
"/usr/share/fonts/truetype/liberation/LiberationSans-Regular.ttf",
|
||||||
|
"/usr/share/fonts/truetype/freefont/FreeSans.ttf",
|
||||||
|
"/usr/share/fonts/TTF/DejaVuSans.ttf",
|
||||||
|
NULL
|
||||||
|
};
|
||||||
|
for (int i = 0; cands[i]; i++) if (access(cands[i], R_OK) == 0) return cands[i];
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ---------------------------------------------------------------- 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void eval_load(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||||
|
int h = tb_opentype_load(in_text(self, rt, 0));
|
||||||
|
if (!h) tinyblocks_set_error(rt, "%s", tb_opentype_last_error());
|
||||||
|
tb_block_set_output(self, 0, tb_value_integer(h));
|
||||||
|
self->dirty = false;
|
||||||
|
}
|
||||||
|
static void eval_upem(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||||
|
tb_block_set_output(self, 0, tb_value_integer(tb_opentype_units_per_em((int)in_real(self, rt, 0))));
|
||||||
|
self->dirty = false;
|
||||||
|
}
|
||||||
|
static void eval_glyphs(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||||
|
tb_block_set_output(self, 0, tb_value_integer(tb_opentype_glyph_count((int)in_real(self, rt, 0))));
|
||||||
|
self->dirty = false;
|
||||||
|
}
|
||||||
|
static void eval_advance(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||||
|
tb_block_set_output(self, 0, tb_value_real(
|
||||||
|
tb_opentype_advance((int)in_real(self, rt, 0), in_text(self, rt, 1), (int)in_real(self, rt, 2))));
|
||||||
|
self->dirty = false;
|
||||||
|
}
|
||||||
|
static void eval_outline(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||||
|
TinyBlocks_JsonValue* j = tb_opentype_outline((int)in_real(self, rt, 0), in_text(self, rt, 1),
|
||||||
|
(int)in_real(self, rt, 2));
|
||||||
|
if (j) {
|
||||||
|
tb_block_set_output(self, 0, tb_value_json(j));
|
||||||
|
tb_json_release(j);
|
||||||
|
} else {
|
||||||
|
tinyblocks_set_error(rt, "%s", tb_opentype_last_error());
|
||||||
|
}
|
||||||
|
self->dirty = false;
|
||||||
|
}
|
||||||
|
static void eval_render(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||||
|
TinyBlocks_Image* img = tb_opentype_render((int)in_real(self, rt, 0), in_text(self, rt, 1),
|
||||||
|
(int)in_real(self, rt, 2),
|
||||||
|
(int)in_real(self, rt, 3), (int)in_real(self, rt, 4),
|
||||||
|
(int)in_real(self, rt, 5));
|
||||||
|
if (!img) { tinyblocks_set_error(rt, "%s", tb_opentype_last_error()); self->dirty = false; return; }
|
||||||
|
tb_block_set_output(self, 0, tb_value_image(img));
|
||||||
|
tb_image_release(img);
|
||||||
|
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 opentype_init(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
|
||||||
|
static const TinyBlocks_Category cat = {
|
||||||
|
"OpenType",
|
||||||
|
{ 63/255.f, 81/255.f, 181/255.f, 1.f},
|
||||||
|
{ 40/255.f, 53/255.f, 147/255.f, 1.f},
|
||||||
|
};
|
||||||
|
tb_library_set_category(lib, &cat);
|
||||||
|
|
||||||
|
static const TinyBlocks_SlotDesc load_in[] = {{"caminho", TB_VALUE_TEXT}};
|
||||||
|
static const TinyBlocks_SlotDesc font_in[] = {{"fonte", TB_VALUE_REAL}};
|
||||||
|
static const TinyBlocks_SlotDesc adv_in[] = {{"fonte", TB_VALUE_REAL}, {"texto", TB_VALUE_TEXT}, {"tamanho", TB_VALUE_REAL}};
|
||||||
|
static const TinyBlocks_SlotDesc out_in[] = {{"fonte", TB_VALUE_REAL}, {"caractere", TB_VALUE_TEXT}, {"tamanho", TB_VALUE_REAL}};
|
||||||
|
static const TinyBlocks_SlotDesc rnd_in[] = {{"fonte", TB_VALUE_REAL}, {"texto", TB_VALUE_TEXT}, {"tamanho", TB_VALUE_REAL},
|
||||||
|
{"r", TB_VALUE_REAL}, {"g", TB_VALUE_REAL}, {"b", TB_VALUE_REAL}};
|
||||||
|
static const TinyBlocks_SlotDesc out_num[] = {{"numero", TB_VALUE_INTEGER}};
|
||||||
|
static const TinyBlocks_SlotDesc out_real[] = {{"numero", TB_VALUE_REAL}};
|
||||||
|
static const TinyBlocks_SlotDesc out_json[] = {{"contornos", TB_VALUE_JSON}};
|
||||||
|
static const TinyBlocks_SlotDesc out_img[] = {{"imagem", TB_VALUE_IMAGE}};
|
||||||
|
|
||||||
|
return reg(rt, lib, "OpenType.Load", load_in, 1, out_num, 1, eval_load)
|
||||||
|
&& reg(rt, lib, "OpenType.UnitsPerEm", font_in, 1, out_num, 1, eval_upem)
|
||||||
|
&& reg(rt, lib, "OpenType.GlyphCount", font_in, 1, out_num, 1, eval_glyphs)
|
||||||
|
&& reg(rt, lib, "OpenType.Advance", adv_in, 3, out_real, 1, eval_advance)
|
||||||
|
&& reg(rt, lib, "OpenType.Outline", out_in, 3, out_json, 1, eval_outline)
|
||||||
|
&& reg(rt, lib, "OpenType.Render", rnd_in, 6, out_img, 1, eval_render);
|
||||||
|
}
|
||||||
|
|
||||||
|
TINYBLOCKS_DEFINE_LIBRARY(opentype, "tinyblocks-opentype", "1.0.0", opentype_init, NULL)
|
||||||
|
|
||||||
|
const TinyBlocks_LibraryDescriptor* tb_opentype_get_library(void) {
|
||||||
|
return &_tb_lib_desc_opentype;
|
||||||
|
}
|
||||||
@@ -0,0 +1,88 @@
|
|||||||
|
/*
|
||||||
|
* opentype_test.c - testa tinyblocks-opentype (via FreeType).
|
||||||
|
*/
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <math.h>
|
||||||
|
#include <tinyblocks/tinyblocks.h>
|
||||||
|
#include <tb_opentype.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)
|
||||||
|
|
||||||
|
int main(void) {
|
||||||
|
const char* font = tb_opentype_default_font();
|
||||||
|
if (!font) {
|
||||||
|
printf("sem fonte do sistema (instale fonts-dejavu) - pulando\n\nTUDO OK\n");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
printf("# fonte: %s\n", font);
|
||||||
|
|
||||||
|
int h = tb_opentype_load(font);
|
||||||
|
CHECK(h > 0, "carrega a fonte");
|
||||||
|
if (h <= 0) { printf("FAIL: %s\n", tb_opentype_last_error()); return 1; }
|
||||||
|
|
||||||
|
CHECK(tb_opentype_units_per_em(h) == 2048, "units_per_em = 2048 (DejaVu)");
|
||||||
|
CHECK(tb_opentype_glyph_count(h) > 100, "glyph_count > 100");
|
||||||
|
CHECK(tb_opentype_num_faces(h) >= 1, "num_faces >= 1");
|
||||||
|
CHECK(tb_opentype_has_char(h, 'A'), "tem 'A'");
|
||||||
|
CHECK(!tb_opentype_has_char(h, 0x10FFFD), "nao tem glifo invalido");
|
||||||
|
|
||||||
|
double a = 0, d = 0, ht = 0;
|
||||||
|
tb_opentype_metrics(h, 32, &a, &d, &ht);
|
||||||
|
CHECK(a > 0 && d < 0 && ht > 0, "metricas ascent>0, descent<0");
|
||||||
|
|
||||||
|
double w1 = tb_opentype_advance(h, "iiii", 32);
|
||||||
|
double w2 = tb_opentype_advance(h, "WWWW", 32);
|
||||||
|
CHECK(w1 > 0 && w2 > 0, "avanco positivo");
|
||||||
|
CHECK(w2 > w1, "avanco de 'W' > 'i'");
|
||||||
|
double w0 = tb_opentype_advance(h, "", 32);
|
||||||
|
CHECK(w0 == 0, "avanco de texto vazio = 0");
|
||||||
|
double w16 = tb_opentype_advance(h, "Hello", 16);
|
||||||
|
double w32 = tb_opentype_advance(h, "Hello", 32);
|
||||||
|
CHECK(w32 > w16 * 1.7 && w32 < w16 * 2.3, "avanco escala com o tamanho");
|
||||||
|
|
||||||
|
TinyBlocks_JsonValue* out = tb_opentype_outline(h, "A", 48);
|
||||||
|
CHECK(out != NULL, "contorno de 'A' gerado");
|
||||||
|
if (out) {
|
||||||
|
TinyBlocks_JsonValue* c0 = tb_json_array_get(out, 0);
|
||||||
|
CHECK(c0 != NULL, "contorno tem ao menos 1 contorno");
|
||||||
|
uint32_t n0 = c0 ? tb_json_array_length(c0) : 0;
|
||||||
|
CHECK(n0 >= 3, "contorno com >= 3 pontos");
|
||||||
|
if (c0) tb_json_release(c0);
|
||||||
|
tb_json_release(out);
|
||||||
|
}
|
||||||
|
|
||||||
|
TinyBlocks_Image* img = tb_opentype_render(h, "Hi", 32, 0, 0, 0);
|
||||||
|
CHECK(img != NULL, "render gera imagem");
|
||||||
|
if (img) {
|
||||||
|
CHECK(tb_image_width(img) > 10 && tb_image_height(img) > 10, "imagem com dimensoes uteis");
|
||||||
|
int painted = 0;
|
||||||
|
for (int y = 0; y < tb_image_height(img); y++)
|
||||||
|
for (int x = 0; x < tb_image_width(img); x++) {
|
||||||
|
uint8_t r, g, b, al;
|
||||||
|
tb_image_get_pixel(img, x, y, &r, &g, &b, &al);
|
||||||
|
if (al > 0) painted++;
|
||||||
|
}
|
||||||
|
CHECK(painted > 20, "render pinta pixels");
|
||||||
|
tb_image_release(img);
|
||||||
|
}
|
||||||
|
|
||||||
|
CHECK(tb_opentype_load("/nao/existe.ttf") == 0, "fonte inexistente -> erro");
|
||||||
|
tb_opentype_close(h);
|
||||||
|
|
||||||
|
TinyBlocks* rt;
|
||||||
|
tinyblocks_init(&rt, NULL);
|
||||||
|
TinyBlocks_LibraryBlock* lib = tb_library_register(rt, tb_opentype_get_library());
|
||||||
|
CHECK(tb_rt_find_type(rt, "OpenType.Load") && tb_rt_find_type(rt, "OpenType.Outline") &&
|
||||||
|
tb_rt_find_type(rt, "OpenType.Render") && tb_rt_find_type(rt, "OpenType.Advance"),
|
||||||
|
"blocos OpenType.* registrados");
|
||||||
|
const TinyBlocks_Category* cat = lib ? tb_library_category(lib) : NULL;
|
||||||
|
CHECK(cat && !strcmp(cat->name, "OpenType"), "categoria 'OpenType'");
|
||||||
|
tinyblocks_deinit(&rt);
|
||||||
|
|
||||||
|
if (fails) printf("\n%d FALHA(S)\n", fails);
|
||||||
|
else printf("\nTUDO OK\n");
|
||||||
|
return fails != 0;
|
||||||
|
}
|
||||||
Reference in new issue
Block a user