c1f139cc62a5389315f139c2f02d82a6611f3e62
[fbpad.git] / pad.c
blobc1f139cc62a5389315f139c2f02d82a6611f3e62
1 #include <ctype.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include "config.h"
6 #include "draw.h"
7 #include "fbpad.h"
9 #define FN_C(fg) ((fg) & ~(FN_I | FN_B))
10 #define NCACHE (1 << 11)
11 #define MAXFBWIDTH (1 << 12)
13 static unsigned int cd[256] = {
14 COLOR0, COLOR1, COLOR2, COLOR3,
15 COLOR4, COLOR5, COLOR6, COLOR7,
16 COLOR8, COLOR9, COLOR10, COLOR11,
17 COLOR12, COLOR13, COLOR14, COLOR15};
18 static int rows, cols;
19 static int fnrows, fncols;
20 static struct font *fonts[3];
22 int pad_init(void)
24 int r, g, b;
25 if (fb_init())
26 return 1;
27 if (sizeof(fbval_t) != FBM_BPP(fb_mode())) {
28 fprintf(stderr, "pad_init: fbval_t doesn't match fb depth\n");
29 return 1;
31 if (pad_font(FR, FI, FB) < 0)
32 return -1;
33 fnrows = font_rows(fonts[0]);
34 fncols = font_cols(fonts[0]);
35 rows = fb_rows() / fnrows;
36 cols = fb_cols() / fncols;
37 for (r = 0; r < 6; r++) {
38 for (g = 0; g < 6; g++) {
39 for (b = 0; b < 6; b++) {
40 int idx = 16 + r * 36 + g * 6 + b;
41 cd[idx] = ((r * 40 + 55) << 16) |
42 ((g * 40 + 55) << 8) |
43 (b * 40 + 55);
47 for (r = 0; r < 24; r++)
48 cd[232 + r] = ((r * 10 + 8) << 16) |
49 ((r * 10 + 8) << 8) | (r * 10 + 8);
50 return 0;
53 void pad_free(void)
55 int i;
56 for (i = 0; i < 3; i++)
57 if (fonts[i])
58 font_free(fonts[i]);
59 fb_free();
62 #define CR(a) (((a) >> 16) & 0x0000ff)
63 #define CG(a) (((a) >> 8) & 0x0000ff)
64 #define CB(a) ((a) & 0x0000ff)
65 #define COLORMERGE(f, b, c) ((b) + (((f) - (b)) * (c) >> 8u))
67 static unsigned mixed_color(int fg, int bg, unsigned val)
69 unsigned int fore = cd[fg], back = cd[bg];
70 unsigned char r = COLORMERGE(CR(fore), CR(back), val);
71 unsigned char g = COLORMERGE(CG(fore), CG(back), val);
72 unsigned char b = COLORMERGE(CB(fore), CB(back), val);
73 return FB_VAL(r, g, b);
76 static unsigned color2fb(int c)
78 return FB_VAL(CR(cd[c]), CG(cd[c]), CB(cd[c]));
81 static fbval_t cache[NCACHE][NDOTS];
82 static struct glyph {
83 int c;
84 short fg, bg;
85 } glyphs[NCACHE];
87 static struct glyph *glyph_entry(int c, int fg, int bg)
89 return &glyphs[((c - 32) ^ (fg << 6) ^ (bg << 5)) & (NCACHE - 1)];
92 static fbval_t *glyph_cache(int c, short fg, short bg)
94 struct glyph *g = glyph_entry(c, fg, bg);
95 if (g->c == c && g->fg == fg && g->bg == bg)
96 return cache[g - glyphs];
97 return NULL;
100 static fbval_t *glyph_add(int c, short fg, short bg)
102 struct glyph *g = glyph_entry(c, fg, bg);
103 g->c = c;
104 g->fg = fg;
105 g->bg = bg;
106 return cache[g - glyphs];
109 static void bmp2fb(fbval_t *d, char *s, int fg, int bg, int nr, int nc)
111 int i, j;
112 for (i = 0; i < fnrows; i++) {
113 for (j = 0; j < fncols; j++) {
114 unsigned v = i < nr && j < nc ?
115 (unsigned char) s[i * nc + j] : 0;
116 d[i * fncols + j] = mixed_color(fg, bg, v);
121 static fbval_t *ch2fb(int fn, int c, short fg, short bg)
123 char bits[NDOTS];
124 fbval_t *fbbits;
125 if (c < 0 || (c < 128 && (!isprint(c) || isspace(c))))
126 return NULL;
127 if ((fbbits = glyph_cache(c, fg, bg)))
128 return fbbits;
129 if (font_bitmap(fonts[fn], bits, c))
130 return NULL;
131 fbbits = glyph_add(c, fg, bg);
132 bmp2fb(fbbits, bits, FN_C(fg), FN_C(bg),
133 font_rows(fonts[fn]), font_cols(fonts[fn]));
134 return fbbits;
137 static void fb_box(int sr, int sc, int er, int ec, fbval_t val)
139 static fbval_t line[MAXFBWIDTH];
140 int cn = ec - sc;
141 int i;
142 for (i = 0; i < cn; i++)
143 line[i] = val;
144 for (i = sr; i < er; i++)
145 fb_set(i, sc, line, cn);
148 static int fnsel(int fg, int bg)
150 if ((fg | bg) & FN_B)
151 return fonts[2] ? 2 : 0;
152 if ((fg | bg) & FN_I)
153 return fonts[1] ? 1 : 0;
154 return 0;
157 void pad_put(int ch, int r, int c, int fg, int bg)
159 int sr = fnrows * r;
160 int sc = fncols * c;
161 int i;
162 fbval_t *bits = ch2fb(fnsel(fg, bg), ch, fg, bg);
163 if (!bits)
164 bits = ch2fb(0, ch, fg, bg);
165 if (!bits)
166 fb_box(sr, sc, sr + fnrows, sc + fncols, color2fb(FN_C(bg)));
167 else
168 for (i = 0; i < fnrows; i++)
169 fb_set(sr + i, sc, bits + (i * fncols), fncols);
172 void pad_blank(int c)
174 fb_box(0, 0, fb_rows(), fb_cols(), color2fb(FN_C(c)));
177 void pad_blankrow(int r, int bg)
179 int sr = r * fnrows;
180 int er = r == rows - 1 ? fb_rows() : (r + 1) * fnrows;
181 fb_box(sr, 0, er, fb_cols(), color2fb(FN_C(bg)));
184 int pad_rows(void)
186 return rows;
189 int pad_cols(void)
191 return cols;
194 int pad_font(char *fr, char *fi, char *fb)
196 char *fns[] = {fr, fi, fb};
197 int i;
198 for (i = 0; i < 3; i++) {
199 if (fonts[i])
200 font_free(fonts[i]);
201 fonts[i] = fns[i] ? font_open(fns[i]) : NULL;
203 memset(glyphs, 0, sizeof(glyphs));
204 if (!fonts[0])
205 fprintf(stderr, "pad: bad font <%s>\n", fr);
206 return fonts[0] ? 0 : -1;