* Removed ugly hooks from test/font.c.tags/v0.99.beta14
@@ -190,7 +190,7 @@ void cucul_free_dither(struct cucul_dither *); | |||||
* | * | ||||
* @{ */ | * @{ */ | ||||
struct cucul_font; | struct cucul_font; | ||||
struct cucul_font *cucul_load_font(void *, unsigned int); | |||||
struct cucul_font *cucul_load_font(void const *, unsigned int); | |||||
unsigned int cucul_get_font_width(struct cucul_font *); | unsigned int cucul_get_font_width(struct cucul_font *); | ||||
unsigned int cucul_get_font_height(struct cucul_font *); | unsigned int cucul_get_font_height(struct cucul_font *); | ||||
void cucul_render_canvas(cucul_t *, struct cucul_font *, unsigned char *, | void cucul_render_canvas(cucul_t *, struct cucul_font *, unsigned char *, | ||||
@@ -88,13 +88,33 @@ DECLARE_UNPACKGLYPH(4) | |||||
DECLARE_UNPACKGLYPH(2) | DECLARE_UNPACKGLYPH(2) | ||||
DECLARE_UNPACKGLYPH(1) | DECLARE_UNPACKGLYPH(1) | ||||
struct cucul_font *cucul_load_font(void *data, unsigned int size) | |||||
/** \brief Load a font from memory for future use. | |||||
* | |||||
* This function loads a font and returns a handle to its internal | |||||
* structure. The handle can then be used with \e cucul_render_canvas() | |||||
* for bitmap output. | |||||
* | |||||
* Internal fonts can also be loaded: if \e size is set to 0, \e data must | |||||
* be a string containing the internal font name. | |||||
* | |||||
* If \e size is non-zero, the \e size bytes of memory at address \e data | |||||
* are loaded as a font. This memory are must not be freed by the calling | |||||
* program until the font handle has been freed with \e cucul_free_font(). | |||||
* | |||||
* \param data The memory area containing the font or its name. | |||||
* \param size The size of the memory area, or 0 if the font name is given. | |||||
* \return A font handle or NULL in case of error. | |||||
*/ | |||||
struct cucul_font *cucul_load_font(void const *data, unsigned int size) | |||||
{ | { | ||||
struct cucul_font *f; | struct cucul_font *f; | ||||
unsigned int i; | unsigned int i; | ||||
if(size == 0) | |||||
return cucul_load_font(font_monospace9, 150000); /* FIXME */ | |||||
f = malloc(sizeof(struct cucul_font)); | f = malloc(sizeof(struct cucul_font)); | ||||
f->private = data; | |||||
f->private = (void *)(uintptr_t)data; | |||||
memcpy(&f->header, f->private + 8, sizeof(struct font_header)); | memcpy(&f->header, f->private + 8, sizeof(struct font_header)); | ||||
f->header.control_size = htonl(f->header.control_size); | f->header.control_size = htonl(f->header.control_size); | ||||
@@ -27,8 +27,6 @@ typedef unsigned int uint32_t; | |||||
#include "cucul.h" | #include "cucul.h" | ||||
extern uint8_t font_monospace9[]; | |||||
int main(int argc, char *argv[]) | int main(int argc, char *argv[]) | ||||
{ | { | ||||
cucul_t *qq; | cucul_t *qq; | ||||
@@ -36,28 +34,40 @@ int main(int argc, char *argv[]) | |||||
unsigned char *buf; | unsigned char *buf; | ||||
unsigned int x, y, w, h; | unsigned int x, y, w, h; | ||||
qq = cucul_create(5, 2); | |||||
/* Create a canvas */ | |||||
qq = cucul_create(8, 2); | |||||
/* Draw stuff on our canvas */ | |||||
cucul_set_color(qq, CUCUL_COLOR_WHITE, CUCUL_COLOR_BLACK); | cucul_set_color(qq, CUCUL_COLOR_WHITE, CUCUL_COLOR_BLACK); | ||||
cucul_putstr(qq, 0, 0, "ABcde"); | |||||
cucul_putstr(qq, 0, 1, "&$âøÿ"); | |||||
cucul_putstr(qq, 0, 0, "ABcde\\o/"); | |||||
cucul_putstr(qq, 0, 1, "&$âøÿ░▒█"); | |||||
f = cucul_load_font(font_monospace9, 700000); | |||||
/* Load a libcucul internal font */ | |||||
f = cucul_load_font("Monospace 9", 0); | |||||
/* Create our bitmap buffer (32-bit ARGB) */ | |||||
w = cucul_get_width(qq) * cucul_get_font_width(f); | w = cucul_get_width(qq) * cucul_get_font_width(f); | ||||
h = cucul_get_height(qq) * cucul_get_font_height(f); | h = cucul_get_height(qq) * cucul_get_font_height(f); | ||||
buf = malloc(4 * w * h); | buf = malloc(4 * w * h); | ||||
/* Render the canvas onto our image buffer */ | |||||
cucul_render_canvas(qq, f, buf, w, h, 4 * w); | cucul_render_canvas(qq, f, buf, w, h, 4 * w); | ||||
/* Just for fun, output the image on the terminal using ASCII art */ | |||||
for(y = 0; y < h; y++) | for(y = 0; y < h; y++) | ||||
{ | { | ||||
for(x = 0; x < w; x++) | for(x = 0; x < w; x++) | ||||
{ | { | ||||
printf("%.02x", buf[4 * (y * w + x) + 3]); | |||||
static const char list[] = { | |||||
' ', '.', ':', 't', 'S', 'R', '#', '@' | |||||
}; | |||||
printf("%c", list[buf[4 * (y * w + x) + 3] / 0x20]); | |||||
} | } | ||||
printf("\n"); | printf("\n"); | ||||
} | } | ||||
/* Free everything */ | |||||
free(buf); | free(buf); | ||||
cucul_free_font(f); | cucul_free_font(f); | ||||
cucul_free(qq); | cucul_free(qq); | ||||