term: fullwidth and zerowidth characters
[fbpad.git] / pad.c
blob2d306c160b7ded26c25e3beeb9b11f843feaa275
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_B ? (fg) + 8 : (fg)) & 0x0f)
10 #define NCACHE (1 << 11)
11 #define MAXFBWIDTH (1 << 12)
13 static unsigned int cd[] = {
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 i;
25 char *paths[] = {FR, FI, FB};
26 if (fb_init())
27 return 1;
28 if (sizeof(fbval_t) != FBM_BPP(fb_mode())) {
29 fprintf(stderr, "pad_init: fbval_t doesn't match fb depth\n");
30 return 1;
32 for (i = 0; i < 3; i++)
33 fonts[i] = paths[i] ? font_open(paths[i]) : NULL;
34 if (!fonts[0]) {
35 fprintf(stderr, "pad: bad font <%s>\n", paths[0]);
36 return -1;
38 fnrows = font_rows(fonts[0]);
39 fncols = font_cols(fonts[0]);
40 rows = fb_rows() / fnrows;
41 cols = fb_cols() / fncols;
42 return 0;
45 void pad_free(void)
47 int i;
48 for (i = 0; i < 3; i++)
49 if (fonts[i])
50 font_free(fonts[i]);
51 fb_free();
54 #define CR(a) (((a) >> 16) & 0x0000ff)
55 #define CG(a) (((a) >> 8) & 0x0000ff)
56 #define CB(a) ((a) & 0x0000ff)
57 #define COLORMERGE(f, b, c) ((b) + (((f) - (b)) * (c) >> 8u))
59 static unsigned mixed_color(int fg, int bg, unsigned val)
61 unsigned int fore = cd[fg], back = cd[bg];
62 unsigned char r = COLORMERGE(CR(fore), CR(back), val);
63 unsigned char g = COLORMERGE(CG(fore), CG(back), val);
64 unsigned char b = COLORMERGE(CB(fore), CB(back), val);
65 return FB_VAL(r, g, b);
68 static unsigned color2fb(int c)
70 return FB_VAL(CR(cd[c]), CG(cd[c]), CB(cd[c]));
73 static fbval_t cache[NCACHE][MAXDOTS];
74 static struct glyph {
75 int c;
76 short fg, bg;
77 } glyphs[NCACHE];
79 static struct glyph *glyph_entry(int c, int fg, int bg)
81 return &glyphs[((c - 32) ^ (fg << 6) ^ (bg << 5)) & (NCACHE - 1)];
84 static fbval_t *glyph_cache(int c, short fg, short bg)
86 struct glyph *g = glyph_entry(c, fg, bg);
87 if (g->c == c && g->fg == fg && g->bg == bg)
88 return cache[g - glyphs];
89 return NULL;
92 static fbval_t *glyph_add(int c, short fg, short bg)
94 struct glyph *g = glyph_entry(c, fg, bg);
95 g->c = c;
96 g->fg = fg;
97 g->bg = bg;
98 return cache[g - glyphs];
101 static void bmp2fb(fbval_t *d, char *s, int fg, int bg, int nr, int nc)
103 int i, j;
104 for (i = 0; i < fnrows; i++) {
105 for (j = 0; j < fncols; j++) {
106 unsigned v = i < nr && j < nc ?
107 (unsigned char) s[i * nc + j] : 0;
108 d[i * fncols + j] = mixed_color(fg, bg, v);
113 static fbval_t *ch2fb(int fn, int c, short fg, short bg)
115 char bits[MAXDOTS];
116 fbval_t *fbbits;
117 if (c < 0 || (c < 128 && (!isprint(c) || isspace(c))))
118 return NULL;
119 if ((fbbits = glyph_cache(c, fg, bg)))
120 return fbbits;
121 if (font_bitmap(fonts[fn], bits, c))
122 return NULL;
123 fbbits = glyph_add(c, fg, bg);
124 bmp2fb(fbbits, bits, FN_C(fg), FN_C(bg),
125 font_rows(fonts[fn]), font_cols(fonts[fn]));
126 return fbbits;
129 static void fb_box(int sr, int sc, int er, int ec, fbval_t val)
131 static fbval_t line[MAXFBWIDTH];
132 int cn = ec - sc;
133 int i;
134 for (i = 0; i < cn; i++)
135 line[i] = val;
136 for (i = sr; i < er; i++)
137 fb_set(i, sc, line, cn);
140 static int fnsel(int fg, int bg)
142 if ((fg | bg) & FN_B)
143 return fonts[2] ? 2 : 0;
144 if ((fg | bg) & FN_I)
145 return fonts[1] ? 1 : 0;
146 return 0;
149 void pad_put(int ch, int r, int c, int fg, int bg)
151 int sr = fnrows * r;
152 int sc = fncols * c;
153 int i;
154 fbval_t *bits = ch2fb(fnsel(fg, bg), ch, fg, bg);
155 if (!bits)
156 bits = ch2fb(0, ch, fg, bg);
157 if (!bits)
158 fb_box(sr, sc, sr + fnrows, sc + fncols, color2fb(FN_C(bg)));
159 else
160 for (i = 0; i < fnrows; i++)
161 fb_set(sr + i, sc, bits + (i * fncols), fncols);
164 void pad_blank(int c)
166 fb_box(0, 0, fb_rows(), fb_cols(), color2fb(FN_C(c)));
169 void pad_blankrow(int r, int bg)
171 int sr = r * fnrows;
172 int er = r == rows - 1 ? fb_rows() : (r + 1) * fnrows;
173 fb_box(sr, 0, er, fb_cols(), color2fb(FN_C(bg)));
176 int pad_rows(void)
178 return rows;
181 int pad_cols(void)
183 return cols;