From 7841bb6b50b001acc693d179defe0e1b8a5e6012 Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Sat, 22 Apr 2006 19:10:41 +0000 Subject: [PATCH] * Added a simple "text" importer, until the cool ANSI importer arrives. --- cucul/import.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/cucul/import.c b/cucul/import.c index d1d9742..1639722 100644 --- a/cucul/import.c +++ b/cucul/import.c @@ -27,6 +27,7 @@ #include "cucul_internals.h" static cucul_canvas_t *import_caca(void const *, unsigned int); +static cucul_canvas_t *import_text(void const *, unsigned int); /** \brief Import a buffer into a canvas * @@ -48,6 +49,8 @@ cucul_canvas_t * cucul_import_canvas(void const *data, unsigned int size, { if(!strcasecmp("caca", format)) return import_caca(data, size); + if(!strcasecmp("text", format)) + return import_text(data, size); /* FIXME: Try to autodetect */ if(!strcasecmp("", format)) @@ -70,6 +73,7 @@ char const * const * cucul_get_import_list(void) static char const * const list[] = { "", "autodetect", + "text", "plain text", "caca", "native libcaca format", NULL, NULL }; @@ -127,3 +131,45 @@ static cucul_canvas_t *import_caca(void const *data, unsigned int size) return cv; } +static cucul_canvas_t *import_text(void const *data, unsigned int size) +{ + cucul_canvas_t *cv; + char const *text = (char const *)data; + unsigned int width = 1, height = 1, x = 0, y = 0, i; + + cv = cucul_create_canvas(width, height); + cucul_set_color(cv, CUCUL_COLOR_DEFAULT, CUCUL_COLOR_TRANSPARENT); + + for(i = 0; i < size; i++) + { + unsigned char ch = *text++; + + if(ch == '\r') + continue; + + if(ch == '\n') + { + x = 0; + y++; + continue; + } + + while(x >= width) + { + width++; + cucul_set_canvas_size(cv, width, height); + } + + while(y >= height) + { + height++; + cucul_set_canvas_size(cv, width, height); + } + + cucul_putchar(cv, x, y, ch); + x++; + } + + return cv; +} +