pad: a better hash function
[fbpad.git] / pad.c
blob6eb245268b37d88a8a7f9514101de2691bafbf18
1 #include <ctype.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <wctype.h>
6 #include "config.h"
7 #include "draw.h"
8 #include "font.h"
9 #include "util.h"
10 #include "pad.h"
12 static unsigned int cd[] = {
13 COLOR0, COLOR1, COLOR2, COLOR3,
14 COLOR4, COLOR5, COLOR6, COLOR7,
15 COLOR8, COLOR9, COLOR10, COLOR11,
16 COLOR12, COLOR13, COLOR14, COLOR15};
17 static int rows, cols;
19 void pad_init(void)
21 fb_init();
22 font_init();
23 rows = fb_rows() / font_rows();
24 cols = fb_cols() / font_cols();
27 void pad_free(void)
29 fb_free();
32 #define CR(a) (((a) >> 16) & 0x0000ff)
33 #define CG(a) (((a) >> 8) & 0x0000ff)
34 #define CB(a) ((a) & 0x0000ff)
35 #define COLORMERGE(f, b, c) ((b) + (((f) - (b)) * (c) >> 8u))
37 static fbval_t mixed_color(int fg, int bg, unsigned char val)
39 unsigned int fore = cd[fg], back = cd[bg];
40 unsigned char r = COLORMERGE(CR(fore), CR(back), val);
41 unsigned char g = COLORMERGE(CG(fore), CG(back), val);
42 unsigned char b = COLORMERGE(CB(fore), CB(back), val);
43 return fb_color(r, g, b);
46 static fbval_t color2fb(int c)
48 return fb_color(CR(cd[c]), CG(cd[c]), CB(cd[c]));
51 #define NCACHE (1 << 11)
52 static fbval_t cache[NCACHE * MAXDOTS];
53 static struct glyph {
54 int c;
55 short fg, bg;
56 } cacheid[NCACHE];
58 static int glyph_hash(struct glyph *g)
60 return (g->c ^ (g->fg << 7) ^ (g->bg << 6)) & (NCACHE - 1);
63 static fbval_t *bitmap(int c, short fg, short bg)
65 unsigned char *bits;
66 fbval_t *fbbits;
67 struct glyph glyph = {0};
68 int hash;
69 int i;
70 int nbits = font_rows() * font_cols();
71 if (c < 0 || (c < 256 && (!isprint(c) || isspace(c))))
72 return NULL;
73 glyph.c = c;
74 glyph.fg = fg;
75 glyph.bg = bg;
76 hash = glyph_hash(&glyph);
77 fbbits = &cache[hash * MAXDOTS];
78 if (!memcmp(&glyph, &cacheid[hash], sizeof(glyph)))
79 return fbbits;
80 bits = font_bitmap(c);
81 if (!bits)
82 return NULL;
83 memcpy(&cacheid[hash], &glyph, sizeof(glyph));
84 for (i = 0; i < nbits; i++)
85 fbbits[i] = mixed_color(fg, bg, bits[i]);
86 return fbbits;
89 #define MAXFBWIDTH (1 << 12)
90 void fb_box(int sr, int sc, int er, int ec, fbval_t val)
92 static fbval_t line[MAXFBWIDTH];
93 int cn = ec - sc;
94 int i;
95 for (i = 0; i < cn; i++)
96 line[i] = val;
97 for (i = sr; i < er; i++)
98 fb_set(i, sc, line, cn);
101 void pad_put(int ch, int r, int c, int fg, int bg)
103 int sr = font_rows() * r;
104 int sc = font_cols() * c;
105 int frows = font_rows(), fcols = font_cols();
106 int i;
107 fbval_t *bits = bitmap(ch, fg, bg);
108 if (!bits)
109 fb_box(sr, sc, sr + frows, sc + fcols, color2fb(bg));
110 else
111 for (i = 0; i < frows; i++)
112 fb_set(sr + i, sc, bits + (i * fcols), fcols);
115 void pad_blank(int c)
117 fb_box(0, 0, fb_rows(), fb_cols(), color2fb(c));
120 void pad_blankrow(int r, int bg)
122 int sr = r * font_rows();
123 int er = r == rows - 1 ? fb_rows() : (r + 1) * font_rows();
124 fb_box(sr, 0, er, fb_cols(), color2fb(bg));
127 int pad_rows(void)
129 return rows;
132 int pad_cols(void)
134 return cols;
137 void pad_shown(void)
139 fb_cmap();