Files
tinyblocks/tests/opentype_test.c

89 lines
3.4 KiB
C

/*
* 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;
}