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];
23 if (sizeof(fbval_t
) != FBM_BPP(fb_mode())) {
24 fprintf(stderr
, "pad_init: fbval_t doesn't match fb depth\n");
27 if (pad_font(FR
, FI
, FB
) < 0)
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) |
43 for (r
= 0; r
< 24; r
++)
44 cd
[232 + r
] = ((r
* 10 + 8) << 16) |
45 ((r
* 10 + 8) << 8) | (r
* 10 + 8);
52 for (i
= 0; i
< 3; i
++)
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
];
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
)];
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
];
99 static fbval_t
*gc_put(int c
, short fg
, short bg
)
102 int pos
= gc_next
[idx
]++;
103 struct glyph
*g
= &gc_info
[idx
][pos
];
104 if (gc_next
[idx
] >= GCLLEN
)
109 return gc_mem
[idx
][pos
];
112 static void bmp2fb(fbval_t
*d
, char *s
, int fg
, int bg
, int nr
, int nc
)
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
)
128 if (c
< 0 || (c
< 128 && (!isprint(c
) || isspace(c
))))
130 if ((fbbits
= gc_get(c
, fg
, bg
)))
132 if (font_bitmap(fonts
[fn
], bits
, c
))
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
]));
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
];
151 for (i
= 0; i
< cn
; i
++)
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;
166 void pad_put(int ch
, int r
, int c
, int fg
, int bg
)
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
);
176 bits
= ch2fb(0, ch
, fg
, bg
);
178 fb_box(sr
, sr
+ fnrows
, sc
, sc
+ fncols
, color2fb(bg
& FN_C
));
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
));
201 int pad_font(char *fr
, char *fi
, char *fb
)
203 char *fns
[] = {fr
, fi
, fb
};
205 for (i
= 0; i
< 3; i
++) {
208 fonts
[i
] = fns
[i
] ? font_open(fns
[i
]) : NULL
;
210 memset(gc_info
, 0, sizeof(gc_info
));
212 fprintf(stderr
, "pad: bad font <%s>\n", fr
);
213 return fonts
[0] ? 0 : -1;