* Make filters line-feed aware.

This commit is contained in:
Sam Hocevar 2006-10-26 13:30:14 +00:00 committed by sam
parent 711021abc7
commit b337442c2c
4 changed files with 61 additions and 56 deletions

View file

@ -28,17 +28,17 @@
#include "toilet.h"
#include "filter.h"
static void filter_crop(cucul_canvas_t *);
static void filter_gay(cucul_canvas_t *);
static void filter_metal(cucul_canvas_t *);
static void filter_flip(cucul_canvas_t *);
static void filter_flop(cucul_canvas_t *);
static void filter_rotate(cucul_canvas_t *);
static void filter_crop(context_t *);
static void filter_gay(context_t *);
static void filter_metal(context_t *);
static void filter_flip(context_t *);
static void filter_flop(context_t *);
static void filter_rotate(context_t *);
struct
{
char const *name;
void (*function)(cucul_canvas_t *);
void (*function)(context_t *);
}
const lookup[] =
{
@ -92,7 +92,7 @@ int filter_do(context_t *cx)
unsigned int i;
for(i = 0; i < cx->nfilters; i++)
cx->filters[i](cx->torender);
cx->filters[i](cx);
return 0;
}
@ -104,20 +104,20 @@ int filter_end(context_t *cx)
return 0;
}
static void filter_crop(cucul_canvas_t *cv)
static void filter_crop(context_t *cx)
{
unsigned int x, y, w, h;
unsigned int xmin, xmax, ymin, ymax;
xmin = w = cucul_get_canvas_width(cv);
xmin = w = cucul_get_canvas_width(cx->torender);
xmax = 0;
ymin = h = cucul_get_canvas_height(cv);
ymin = h = cucul_get_canvas_height(cx->torender);
ymax = 0;
for(y = 0; y < h; y++)
for(x = 0; x < w; x++)
{
unsigned long int ch = cucul_getchar(cv, x, y);
unsigned long int ch = cucul_getchar(cx->torender, x, y);
if(ch != (unsigned char)' ')
{
if(x < xmin)
@ -134,11 +134,11 @@ static void filter_crop(cucul_canvas_t *cv)
if(xmax < xmin || ymax < ymin)
return;
cucul_set_canvas_boundaries(cv, xmin, ymin,
cucul_set_canvas_boundaries(cx->torender, xmin, ymin,
xmax - xmin + 1, ymax - ymin + 1);
}
static void filter_metal(cucul_canvas_t *cv)
static void filter_metal(context_t *cx)
{
static unsigned char const palette[] =
{
@ -150,25 +150,25 @@ static void filter_metal(cucul_canvas_t *cv)
unsigned int x, y, w, h;
w = cucul_get_canvas_width(cv);
h = cucul_get_canvas_height(cv);
w = cucul_get_canvas_width(cx->torender);
h = cucul_get_canvas_height(cx->torender);
for(y = 0; y < h; y++)
for(x = 0; x < w; x++)
{
unsigned long int ch = cucul_getchar(cv, x, y);
unsigned long int ch = cucul_getchar(cx->torender, x, y);
int i;
if(ch == (unsigned char)' ')
continue;
i = y * 4 / h;
cucul_set_color(cv, palette[i], CUCUL_COLOR_TRANSPARENT);
cucul_putchar(cv, x, y, ch);
i = ((cx->lines + y + x / 8) / 2) % 4;
cucul_set_color(cx->torender, palette[i], CUCUL_COLOR_TRANSPARENT);
cucul_putchar(cx->torender, x, y, ch);
}
}
static void filter_gay(cucul_canvas_t *cv)
static void filter_gay(context_t *cx)
{
static unsigned char const rainbow[] =
{
@ -181,34 +181,34 @@ static void filter_gay(cucul_canvas_t *cv)
};
unsigned int x, y, w, h;
w = cucul_get_canvas_width(cv);
h = cucul_get_canvas_height(cv);
w = cucul_get_canvas_width(cx->torender);
h = cucul_get_canvas_height(cx->torender);
for(y = 0; y < h; y++)
for(x = 0; x < w; x++)
{
unsigned long int ch = cucul_getchar(cv, x, y);
unsigned long int ch = cucul_getchar(cx->torender, x, y);
if(ch != (unsigned char)' ')
{
cucul_set_color(cv, rainbow[(x / 2 + y) % 6],
cucul_set_color(cx->torender, rainbow[(x / 2 + y + cx->lines) % 6],
CUCUL_COLOR_TRANSPARENT);
cucul_putchar(cv, x, y, ch);
cucul_putchar(cx->torender, x, y, ch);
}
}
}
static void filter_flip(cucul_canvas_t *cv)
static void filter_flip(context_t *cx)
{
cucul_flip(cv);
cucul_flip(cx->torender);
}
static void filter_flop(cucul_canvas_t *cv)
static void filter_flop(context_t *cx)
{
cucul_flop(cv);
cucul_flop(cx->torender);
}
static void filter_rotate(cucul_canvas_t *cv)
static void filter_rotate(context_t *cx)
{
cucul_rotate(cv);
cucul_rotate(cx->torender);
}

View file

@ -29,6 +29,8 @@
#include "render.h"
#include "filter.h"
static int render_flush(context_t *);
int render_init(context_t *cx)
{
cx->x = cx->y = 0;
@ -66,7 +68,7 @@ int render_stdin(context_t *cx)
i = 0;
if(ch == '\n')
render_line(cx);
render_flush(cx);
}
return 0;
@ -91,27 +93,7 @@ int render_list(context_t *cx, unsigned int argc, char *argv[])
}
}
render_line(cx);
return 0;
}
int render_line(context_t *cx)
{
cucul_buffer_t *buffer;
/* Flush current line */
cx->flush(cx);
/* Apply optional effects to our string */
filter_do(cx);
/* Output line */
buffer = cucul_export_canvas(cx->torender, cx->export);
fwrite(cucul_get_buffer_data(buffer),
cucul_get_buffer_size(buffer), 1, stdout);
cucul_free_buffer(buffer);
cucul_free_canvas(cx->torender);
render_flush(cx);
return 0;
}
@ -124,3 +106,27 @@ int render_end(context_t *cx)
return 0;
}
/* XXX: Following functions are local */
static int render_flush(context_t *cx)
{
cucul_buffer_t *buffer;
/* Flush current line */
cx->flush(cx);
/* Apply optional effects to our string */
filter_do(cx);
cx->lines += cucul_get_canvas_height(cx->torender);
/* Output line */
buffer = cucul_export_canvas(cx->torender, cx->export);
fwrite(cucul_get_buffer_data(buffer),
cucul_get_buffer_size(buffer), 1, stdout);
cucul_free_buffer(buffer);
cucul_free_canvas(cx->torender);
return 0;
}

View file

@ -22,6 +22,5 @@ extern int init_figlet(context_t *);
extern int render_init(context_t *);
extern int render_stdin(context_t *);
extern int render_list(context_t *, unsigned int, char *[]);
extern int render_line(context_t *);
extern int render_end(context_t *);

View file

@ -47,7 +47,7 @@ struct toilet_context
unsigned int *lookup;
/* Render filters */
void (**filters)(cucul_canvas_t *);
void (**filters)(struct toilet_context *);
unsigned int nfilters;
};