1 | /* |
---|
2 | * TOIlet The Other Implementation’s letters |
---|
3 | * Copyright (c) 2006 Sam Hocevar <sam@zoy.org> |
---|
4 | * All Rights Reserved |
---|
5 | * |
---|
6 | * $Id: term.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 | |
---|
29 | static int feed_tiny(context_t *, uint32_t); |
---|
30 | static int flush_tiny(context_t *); |
---|
31 | static int end_tiny(context_t *); |
---|
32 | |
---|
33 | int init_tiny(context_t *cx) |
---|
34 | { |
---|
35 | cx->ew = 16; |
---|
36 | cx->eh = 2; |
---|
37 | |
---|
38 | cx->feed = feed_tiny; |
---|
39 | cx->flush = flush_tiny; |
---|
40 | cx->end = end_tiny; |
---|
41 | |
---|
42 | return 0; |
---|
43 | } |
---|
44 | |
---|
45 | static int feed_tiny(context_t *cx, uint32_t ch) |
---|
46 | { |
---|
47 | switch(ch) |
---|
48 | { |
---|
49 | case (uint32_t)'\r': |
---|
50 | return 0; |
---|
51 | case (uint32_t)'\n': |
---|
52 | cx->x = 0; |
---|
53 | cx->y++; |
---|
54 | return 0; |
---|
55 | case (uint32_t)'\t': |
---|
56 | cx->x = (cx->x & ~7) + 8; |
---|
57 | return 0; |
---|
58 | } |
---|
59 | |
---|
60 | /* Check whether we reached the end of the screen */ |
---|
61 | if(cx->x && cx->x + 1 > cx->term_width) |
---|
62 | { |
---|
63 | cx->x = 0; |
---|
64 | cx->y++; |
---|
65 | } |
---|
66 | |
---|
67 | /* Check whether the current canvas is large enough */ |
---|
68 | if(cx->x + 1 > cx->w) |
---|
69 | { |
---|
70 | cx->w = cx->x + 1 < cx->term_width ? cx->x + 1 : cx->term_width; |
---|
71 | if(cx->w > cx->ew) |
---|
72 | cx->ew = cx->ew + cx->ew / 2; |
---|
73 | } |
---|
74 | |
---|
75 | if(cx->y + 1 > cx->h) |
---|
76 | { |
---|
77 | cx->h = cx->y + 1; |
---|
78 | if(cx->h > cx->eh) |
---|
79 | cx->eh = cx->eh + cx->eh / 2; |
---|
80 | } |
---|
81 | |
---|
82 | cucul_set_canvas_size(cx->cv, cx->ew, cx->eh); |
---|
83 | |
---|
84 | cucul_putchar(cx->cv, cx->x, cx->y, ch); |
---|
85 | cx->x++; |
---|
86 | |
---|
87 | return 0; |
---|
88 | } |
---|
89 | |
---|
90 | static int flush_tiny(context_t *cx) |
---|
91 | { |
---|
92 | cx->torender = cx->cv; |
---|
93 | cucul_set_canvas_size(cx->torender, cx->w, cx->h); |
---|
94 | |
---|
95 | cx->ew = 16; |
---|
96 | cx->eh = 2; |
---|
97 | cx->x = cx->y = 0; |
---|
98 | cx->w = cx->h = 0; |
---|
99 | cx->cv = cucul_create_canvas(cx->ew, cx->eh); |
---|
100 | |
---|
101 | return 0; |
---|
102 | } |
---|
103 | |
---|
104 | static int end_tiny(context_t *cx) |
---|
105 | { |
---|
106 | return 0; |
---|
107 | } |
---|
108 | |
---|