1 | /* |
---|
2 | * TOIlet The Other Implementation’s letters |
---|
3 | * Copyright (c) 2006 Sam Hocevar <sam@zoy.org> |
---|
4 | * All Rights Reserved |
---|
5 | * |
---|
6 | * $Id: render.c 1112 2006-09-26 21:51:13Z 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 "render.h" |
---|
27 | |
---|
28 | cucul_canvas_t *render_big(uint32_t const *string, unsigned int length) |
---|
29 | { |
---|
30 | cucul_canvas_t *cv; |
---|
31 | cucul_font_t *f; |
---|
32 | char const * const * fonts; |
---|
33 | unsigned char *buf; |
---|
34 | unsigned int w, h, x, y, miny, maxy; |
---|
35 | |
---|
36 | cv = cucul_create_canvas(length, 1); |
---|
37 | cucul_set_color(cv, CUCUL_COLOR_WHITE, CUCUL_COLOR_BLACK); |
---|
38 | for(x = 0; x < length; x++) |
---|
39 | cucul_putchar(cv, x, 0, string[x]); |
---|
40 | |
---|
41 | fonts = cucul_get_font_list(); |
---|
42 | f = cucul_load_font(fonts[0], 0); |
---|
43 | |
---|
44 | /* Create our bitmap buffer (32-bit ARGB) */ |
---|
45 | w = cucul_get_canvas_width(cv) * cucul_get_font_width(f); |
---|
46 | h = cucul_get_canvas_height(cv) * cucul_get_font_height(f); |
---|
47 | buf = malloc(4 * w * h); |
---|
48 | |
---|
49 | /* Render the canvas onto our image buffer */ |
---|
50 | cucul_render_canvas(cv, f, buf, w, h, 4 * w); |
---|
51 | |
---|
52 | /* Free our canvas, and allocate a bigger one */ |
---|
53 | cucul_free_font(f); |
---|
54 | cucul_free_canvas(cv); |
---|
55 | cv = cucul_create_canvas(w, h); |
---|
56 | |
---|
57 | /* Render the image buffer on the canvas */ |
---|
58 | cucul_set_color(cv, CUCUL_COLOR_WHITE, CUCUL_COLOR_TRANSPARENT); |
---|
59 | cucul_clear_canvas(cv); |
---|
60 | |
---|
61 | miny = h; maxy = 0; |
---|
62 | |
---|
63 | for(y = 0; y < h; y++) |
---|
64 | for(x = 0; x < w; x++) |
---|
65 | { |
---|
66 | unsigned char c = buf[4 * (x + y * w) + 2]; |
---|
67 | |
---|
68 | if(c >= 0xa0) |
---|
69 | cucul_putstr(cv, x, y, "█"); |
---|
70 | else if(c >= 0x80) |
---|
71 | cucul_putstr(cv, x, y, "▓"); |
---|
72 | else if(c >= 0x40) |
---|
73 | cucul_putstr(cv, x, y, "▒"); |
---|
74 | else if(c >= 0x20) |
---|
75 | cucul_putstr(cv, x, y, "░"); |
---|
76 | } |
---|
77 | |
---|
78 | free(buf); |
---|
79 | |
---|
80 | return cv; |
---|
81 | } |
---|
82 | |
---|
83 | cucul_canvas_t *render_tiny(uint32_t const *string, unsigned int length) |
---|
84 | { |
---|
85 | unsigned int x; |
---|
86 | cucul_canvas_t *cv = cucul_create_canvas(length, 1); |
---|
87 | |
---|
88 | for(x = 0; x < length; x++) |
---|
89 | cucul_putchar(cv, x, 0, string[x]); |
---|
90 | |
---|
91 | return cv; |
---|
92 | } |
---|
93 | |
---|