draw: clean up draw.h interface
[fbpad.git] / pad.c
blob25bbfafb89e5e9b928c1ee4ce10556c946cbbe62
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 static unsigned int cd[256] = {
10 COLOR0, COLOR1, COLOR2, COLOR3,
11 COLOR4, COLOR5, COLOR6, COLOR7,
12 COLOR8, COLOR9, COLOR10, COLOR11,
13 COLOR12, COLOR13, COLOR14, COLOR15};
14 static int rows, cols;
15 static int fnrows, fncols;
16 static struct font *fonts[3];
18 int pad_init(void)
20 int r, g, b;
21 if (fb_init(FBDEV))
22 return 1;
23 if (sizeof(fbval_t) != FBM_BPP(fb_mode())) {
24 fprintf(stderr, "pad_init: fbval_t doesn't match fb depth\n");
25 return 1;
27 if (pad_font(FR, FI, FB) < 0)
28 return -1;
29 fnrows = font_rows(fonts[0]);
30 fncols = font_cols(fonts[0]);
31 rows = fb_rows() / fnrows;
32 cols = fb_cols() / fncols;
33 for (r = 0; r < 6; r++) {
34 for (g = 0; g < 6; g++) {
35 for (b = 0; b < 6; b++) {
36 int idx = 16 + r * 36 + g * 6 + b;
37 cd[idx] = ((r * 40 + 55) << 16) |
38 ((g * 40 + 55) << 8) |
39 (b * 40 + 55);
43 for (r = 0; r < 24; r++)
44 cd[232 + r] = ((r * 10 + 8) << 16) |
45 ((r * 10 + 8) << 8) | (r * 10 + 8);
46 return 0;
49 void pad_free(void)
51 int i;
52 for (i = 0; i < 3; i++)
53 if (fonts[i])
54 font_free(fonts[i]);
55 fb_free();
58 #define CR(a) (((a) >> 16) & 0x0000ff)
59 #define CG(a) (((a) >> 8) & 0x0000ff)
60 #define CB(a) ((a) & 0x0000ff)
61 #define COLORMERGE(f, b, c) ((b) + (((f) - (b)) * (c) >> 8u))
63 static unsigned mixed_color(int fg, int bg, unsigned val)
65 unsigned int fore = cd[fg], back = cd[bg];
66 unsigned char r = COLORMERGE(CR(fore), CR(back), val);
67 unsigned char g = COLORMERGE(CG(fore), CG(back), val);
68 unsigned char b = COLORMERGE(CB(fore), CB(back), val);
69 return FB_VAL(r, g, b);
72 static unsigned color2fb(int c)
74 return FB_VAL(CR(cd[c]), CG(cd[c]), CB(cd[c]));
77 /* glyph bitmap cache */
78 #define GCLCNT (1 << 7) /* glyph cache list count */
79 #define GCLLEN (1 << 4) /* glyph cache list length */
80 #define GCIDX(c) ((c) & (GCLCNT - 1))
82 static fbval_t gc_mem[GCLCNT][GCLLEN][NDOTS];
83 static int gc_next[GCLCNT];
84 static struct glyph {
85 int c;
86 short fg, bg;
87 } gc_info[GCLCNT][GCLLEN];
89 static fbval_t *gc_get(int c, short fg, short bg)
91 struct glyph *g = gc_info[GCIDX(c)];
92 int i;
93 for (i = 0; i < GCLLEN; i++)
94 if (g[i].c == c && g[i].fg == fg && g[i].bg == bg)
95 return gc_mem[GCIDX(c)][i];
96 return NULL;
99 static fbval_t *gc_put(int c, short fg, short bg)
101 int idx = GCIDX(c);
102 int pos = gc_next[idx]++;
103 struct glyph *g = &gc_info[idx][pos];
104 if (gc_next[idx] >= GCLLEN)
105 gc_next[idx] = 0;
106 g->c = c;
107 g->fg = fg;
108 g->bg = bg;
109 return gc_mem[idx][pos];
112 static void bmp2fb(fbval_t *d, char *s, int fg, int bg, int nr, int nc)
114 int i, j;
115 for (i = 0; i < fnrows; i++) {
116 for (j = 0; j < fncols; j++) {
117 unsigned v = i < nr && j < nc ?
118 (unsigned char) s[i * nc + j] : 0;
119 d[i * fncols + j] = mixed_color(fg, bg, v);
124 static fbval_t *ch2fb(int fn, int c, short fg, short bg)
126 char bits[NDOTS];
127 fbval_t *fbbits;
128 if (c < 0 || (c < 128 && (!isprint(c) || isspace(c))))
129 return NULL;
130 if ((fbbits = gc_get(c, fg, bg)))
131 return fbbits;
132 if (font_bitmap(fonts[fn], bits, c))
133 return NULL;
134 fbbits = gc_put(c, fg, bg);
135 bmp2fb(fbbits, bits, fg & FN_C, bg & FN_C,
136 font_rows(fonts[fn]), font_cols(fonts[fn]));
137 return fbbits;
140 static void fb_set(int r, int c, void *mem, int len)
142 int bpp = FBM_BPP(fb_mode());
143 memcpy(fb_mem(r) + c * bpp, mem, len * bpp);
146 static void fb_box(int sr, int er, int sc, int ec, fbval_t val)
148 static fbval_t line[32 * NCOLS];
149 int cn = ec - sc;
150 int i;
151 for (i = 0; i < cn; i++)
152 line[i] = val;
153 for (i = sr; i < er; i++)
154 fb_set(i, sc, line, cn);
157 static int fnsel(int fg, int bg)
159 if ((fg | bg) & FN_B)
160 return fonts[2] ? 2 : 0;
161 if ((fg | bg) & FN_I)
162 return fonts[1] ? 1 : 0;
163 return 0;
166 void pad_put(int ch, int r, int c, int fg, int bg)
168 int sr = fnrows * r;
169 int sc = fncols * c;
170 fbval_t *bits;
171 int i;
172 if ((fg & 0xfff8) == FN_B && !fonts[2])
173 fg |= 8; /* increase intensity of no FB */
174 bits = ch2fb(fnsel(fg, bg), ch, fg, bg);
175 if (!bits)
176 bits = ch2fb(0, ch, fg, bg);
177 if (!bits)
178 fb_box(sr, sr + fnrows, sc, sc + fncols, color2fb(bg & FN_C));
179 else
180 for (i = 0; i < fnrows; i++)
181 fb_set(sr + i, sc, bits + (i * fncols), fncols);
184 void pad_fill(int sr, int er, int sc, int ec, int c)
186 int fber = er >= 0 ? er * fnrows : fb_rows();
187 int fbec = ec >= 0 ? ec * fncols : fb_cols();
188 fb_box(sr * fnrows, fber, sc * fncols, fbec, color2fb(c & FN_C));
191 int pad_rows(void)
193 return rows;
196 int pad_cols(void)
198 return cols;
201 int pad_font(char *fr, char *fi, char *fb)
203 char *fns[] = {fr, fi, fb};
204 int i;
205 for (i = 0; i < 3; i++) {
206 if (fonts[i])
207 font_free(fonts[i]);
208 fonts[i] = fns[i] ? font_open(fns[i]) : NULL;
210 memset(gc_info, 0, sizeof(gc_info));
211 if (!fonts[0])
212 fprintf(stderr, "pad: bad font <%s>\n", fr);
213 return fonts[0] ? 0 : -1;