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];
27 if (sizeof(fbval_t
) != FBM_BPP(fb_mode())) {
28 fprintf(stderr
, "pad_init: fbval_t doesn't match fb depth\n");
31 if (pad_font(FR
, FI
, FB
) < 0)
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) |
47 for (r
= 0; r
< 24; r
++)
48 cd
[232 + r
] = ((r
* 10 + 8) << 16) |
49 ((r
* 10 + 8) << 8) | (r
* 10 + 8);
56 for (i
= 0; i
< 3; i
++)
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
];
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
];
100 static fbval_t
*glyph_add(int c
, short fg
, short bg
)
102 struct glyph
*g
= glyph_entry(c
, fg
, bg
);
106 return cache
[g
- glyphs
];
109 static void bmp2fb(fbval_t
*d
, char *s
, int fg
, int bg
, int nr
, int nc
)
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
)
125 if (c
< 0 || (c
< 128 && (!isprint(c
) || isspace(c
))))
127 if ((fbbits
= glyph_cache(c
, fg
, bg
)))
129 if (font_bitmap(fonts
[fn
], bits
, c
))
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
]));
137 static void fb_box(int sr
, int sc
, int er
, int ec
, fbval_t val
)
139 static fbval_t line
[MAXFBWIDTH
];
142 for (i
= 0; i
< cn
; i
++)
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;
157 void pad_put(int ch
, int r
, int c
, int fg
, int bg
)
162 fbval_t
*bits
= ch2fb(fnsel(fg
, bg
), ch
, fg
, bg
);
164 bits
= ch2fb(0, ch
, fg
, bg
);
166 fb_box(sr
, sc
, sr
+ fnrows
, sc
+ fncols
, color2fb(FN_C(bg
)));
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
)
180 int er
= r
== rows
- 1 ? fb_rows() : (r
+ 1) * fnrows
;
181 fb_box(sr
, 0, er
, fb_cols(), color2fb(FN_C(bg
)));
194 int pad_font(char *fr
, char *fi
, char *fb
)
196 char *fns
[] = {fr
, fi
, fb
};
198 for (i
= 0; i
< 3; i
++) {
201 fonts
[i
] = fns
[i
] ? font_open(fns
[i
]) : NULL
;
203 memset(glyphs
, 0, sizeof(glyphs
));
205 fprintf(stderr
, "pad: bad font <%s>\n", fr
);
206 return fonts
[0] ? 0 : -1;