source: toilet/trunk/src/mono9.c @ 1242

Last change on this file since 1242 was 1242, checked in by Sam Hocevar, 14 years ago
  • Factor code from renderers to main render.c functions.
  • Property svn:keywords set to Id
File size: 3.1 KB
Line 
1/*
2 *  TOIlet        The Other Implementation’s letters
3 *  Copyright (c) 2006 Sam Hocevar <sam@zoy.org>
4 *                All Rights Reserved
5 *
6 *  $Id: mono9.c 1242 2006-10-26 13:14:37Z sam $
7 *
8 *  This program is free software; you can redistribute it and/or
9 *  modify it under the terms of the Do What The Fuck You Want To
10 *  Public License, Version 2, as published by Sam Hocevar. See
11 *  http://sam.zoy.org/wtfpl/COPYING for more details.
12 */
13
14/*
15 * This file contains text to canvas rendering functions.
16 */
17
18#include "config.h"
19
20#if defined(HAVE_INTTYPES_H)
21#   include <inttypes.h>
22#endif
23#include <stdlib.h>
24#include <cucul.h>
25
26#include "toilet.h"
27#include "render.h"
28
29static int feed_big(context_t *, uint32_t);
30static int flush_big(context_t *);
31static int end_big(context_t *);
32
33int init_big(context_t *cx)
34{
35    char const * const * fonts;
36
37    fonts = cucul_get_font_list();
38    cx->f = cucul_load_font(fonts[0], 0);
39    cx->buf = malloc(4 * cucul_get_font_width(cx->f)
40                       * cucul_get_font_height(cx->f));
41    cx->onechar = cucul_create_canvas(1, 1);
42    cucul_set_color(cx->onechar, CUCUL_COLOR_WHITE, CUCUL_COLOR_BLACK);
43
44    cx->feed = feed_big;
45    cx->flush = flush_big;
46    cx->end = end_big;
47
48    return 0;
49}
50
51static int feed_big(context_t *cx, uint32_t ch)
52{
53    unsigned int w = cucul_get_font_width(cx->f);
54    unsigned int h = cucul_get_font_height(cx->f);
55    unsigned int x, y;
56
57    switch(ch)
58    {
59        case (uint32_t)'\r':
60            return 0;
61        case (uint32_t)'\n':
62            cx->x = 0;
63            cx->y += h;
64            return 0;
65        case (uint32_t)'\t':
66            cx->x = (((cx->x / w) & ~7) + 8) * w;
67            return 0;
68    }
69
70    /* Check whether we reached the end of the screen */
71    if(cx->x && cx->x + w > cx->term_width)
72    {
73        cx->x = 0;
74        cx->y += h;
75    }
76
77    /* Check whether the current canvas is large enough */
78    if(cx->x + w > cx->w)
79        cx->w = cx->x + w < cx->term_width ? cx->x + w : cx->term_width;
80
81    if(cx->y + h > cx->h)
82        cx->h = cx->y + h;
83
84    cucul_set_canvas_size(cx->cv, cx->w, cx->h);
85
86    /* Render our char */
87    cucul_putchar(cx->onechar, 0, 0, ch);
88    cucul_render_canvas(cx->onechar, cx->f, cx->buf, w, h, 4 * w);
89
90    for(y = 0; y < h; y++)
91       for(x = 0; x < w; x++)
92    {
93        unsigned char c = cx->buf[4 * (x + y * w) + 2];
94
95        if(c >= 0xa0)
96            cucul_putstr(cx->cv, cx->x + x, cx->y + y, "█");
97        else if(c >= 0x80)
98            cucul_putstr(cx->cv, cx->x + x, cx->y + y, "▓");
99        else if(c >= 0x40)
100            cucul_putstr(cx->cv, cx->x + x, cx->y + y, "▒");
101        else if(c >= 0x20)
102            cucul_putstr(cx->cv, cx->x + x, cx->y + y, "░");
103    }
104
105    /* Advance cursor */
106    cx->x += w;
107
108    return 0;
109}
110
111static int flush_big(context_t *cx)
112{
113    cx->torender = cx->cv;
114    cucul_set_canvas_size(cx->torender, cx->w, cx->h);
115
116    cx->x = cx->y = 0;
117    cx->w = cx->h = 0;
118    cx->cv = cucul_create_canvas(1, 1);
119
120    return 0;
121}
122
123static int end_big(context_t *cx)
124{
125    cucul_free_canvas(cx->onechar);
126    free(cx->buf);
127    cucul_free_font(cx->f);
128
129    return 0;
130}
131
Note: See TracBrowser for help on using the repository browser.