term: reorder functions; move term_*() higher
[fbpad.git] / pad.c
bloba7f1cd2b7649ebe3e2db89e2a75b5f6c8fdbec08
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 "font.h"
8 #include "util.h"
9 #include "pad.h"
11 static unsigned int cd[] = {
12 COLOR0, COLOR1, COLOR2, COLOR3,
13 COLOR4, COLOR5, COLOR6, COLOR7,
14 COLOR8, COLOR9, COLOR10, COLOR11,
15 COLOR12, COLOR13, COLOR14, COLOR15};
16 static int rows, cols;
18 int pad_init(void)
20 if (fb_init())
21 return 1;
22 if (sizeof(fbval_t) != FBM_BPP(fb_mode())) {
23 fprintf(stderr, "pad_init: fbval_t doesn't match fb depth\n");
24 return 1;
26 if (font_init()) {
27 fprintf(stderr, "pad_init: loading font failed\n");
28 return 1;
30 rows = fb_rows() / font_rows();
31 cols = fb_cols() / font_cols();
32 return 0;
35 void pad_free(void)
37 font_free();
38 fb_free();
41 #define CR(a) (((a) >> 16) & 0x0000ff)
42 #define CG(a) (((a) >> 8) & 0x0000ff)
43 #define CB(a) ((a) & 0x0000ff)
44 #define COLORMERGE(f, b, c) ((b) + (((f) - (b)) * (c) >> 8u))
46 static unsigned mixed_color(int fg, int bg, unsigned char val)
48 unsigned int fore = cd[fg], back = cd[bg];
49 unsigned char r = COLORMERGE(CR(fore), CR(back), val);
50 unsigned char g = COLORMERGE(CG(fore), CG(back), val);
51 unsigned char b = COLORMERGE(CB(fore), CB(back), val);
52 return FB_VAL(r, g, b);
55 static unsigned color2fb(int c)
57 return FB_VAL(CR(cd[c]), CG(cd[c]), CB(cd[c]));
60 #define NCACHE (1 << 11)
61 static fbval_t cache[NCACHE * MAXDOTS];
62 static struct glyph {
63 int c;
64 short fg, bg;
65 } cacheid[NCACHE];
67 static int glyph_hash(int c, int fg, int bg)
69 return (c ^ (fg << 7) ^ (bg << 6)) & (NCACHE - 1);
72 static fbval_t *bitmap(int c, short fg, short bg)
74 unsigned char bits[MAXDOTS];
75 fbval_t *fbbits;
76 struct glyph *glyph;
77 int i;
78 int nbits = font_rows() * font_cols();
79 if (c < 0 || (c < 256 && (!isprint(c) || isspace(c))))
80 return NULL;
81 glyph = &cacheid[glyph_hash(c, fg, bg)];
82 fbbits = &cache[glyph_hash(c, fg, bg) * MAXDOTS];
83 if (glyph->c == c && glyph->fg == fg && glyph->bg == bg)
84 return fbbits;
85 if (font_bitmap(bits, c))
86 return NULL;
87 glyph->c = c;
88 glyph->fg = fg;
89 glyph->bg = bg;
90 for (i = 0; i < nbits; i++)
91 fbbits[i] = mixed_color(fg, bg, bits[i]);
92 return fbbits;
95 #define MAXFBWIDTH (1 << 12)
96 static void fb_box(int sr, int sc, int er, int ec, fbval_t val)
98 static fbval_t line[MAXFBWIDTH];
99 int cn = ec - sc;
100 int i;
101 for (i = 0; i < cn; i++)
102 line[i] = val;
103 for (i = sr; i < er; i++)
104 fb_set(i, sc, line, cn);
107 void pad_put(int ch, int r, int c, int fg, int bg)
109 int sr = font_rows() * r;
110 int sc = font_cols() * c;
111 int frows = font_rows(), fcols = font_cols();
112 int i;
113 fbval_t *bits = bitmap(ch, fg, bg);
114 if (!bits)
115 fb_box(sr, sc, sr + frows, sc + fcols, color2fb(bg));
116 else
117 for (i = 0; i < frows; i++)
118 fb_set(sr + i, sc, bits + (i * fcols), fcols);
121 void pad_blank(int c)
123 fb_box(0, 0, fb_rows(), fb_cols(), color2fb(c));
126 void pad_blankrow(int r, int bg)
128 int sr = r * font_rows();
129 int er = r == rows - 1 ? fb_rows() : (r + 1) * font_rows();
130 fb_box(sr, 0, er, fb_cols(), color2fb(bg));
133 int pad_rows(void)
135 return rows;
138 int pad_cols(void)
140 return cols;
143 void pad_shown(void)
145 fb_cmap();