1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
|
/*
* text rendering for the framebuffer console
* pick fonts from X11 font server or
* use linux consolefont psf files.
* (c) 2001 Gerd Knorr <kraxel@bytesex.org>
*/
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <linux/fb.h>
#include "fbtools.h"
#include "fs.h"
/* ------------------------------------------------------------------ */
#define BIT_ORDER BitmapFormatBitOrderMSB
#ifdef BYTE_ORDER
#undef BYTE_ORDER
#endif
#define BYTE_ORDER BitmapFormatByteOrderMSB
#define SCANLINE_UNIT BitmapFormatScanlineUnit8
#define SCANLINE_PAD BitmapFormatScanlinePad8
#define EXTENTS BitmapFormatImageRectMin
#define SCANLINE_PAD_BYTES 1
#define GLWIDTHBYTESPADDED(bits, nBytes) \
((nBytes) == 1 ? (((bits) + 7) >> 3) /* pad to 1 byte */\
:(nBytes) == 2 ? ((((bits) + 15) >> 3) & ~1) /* pad to 2 bytes */\
:(nBytes) == 4 ? ((((bits) + 31) >> 3) & ~3) /* pad to 4 bytes */\
:(nBytes) == 8 ? ((((bits) + 63) >> 3) & ~7) /* pad to 8 bytes */\
: 0)
static const unsigned fs_masktab[] = {
(1 << 7), (1 << 6), (1 << 5), (1 << 4),
(1 << 3), (1 << 2), (1 << 1), (1 << 0),
};
/* ------------------------------------------------------------------ */
#ifndef X_DISPLAY_MISSING
static FSServer *svr;
#endif
static unsigned int bpp,black,white;
static void (*setpixel)(void *ptr, unsigned int color);
static void setpixel1(void *ptr, unsigned int color)
{
unsigned char *p = ptr;
*p = color;
}
static void setpixel2(void *ptr, unsigned int color)
{
unsigned short *p = ptr;
*p = color;
}
static void setpixel3(void *ptr, unsigned int color)
{
unsigned char *p = ptr;
*(p++) = (color >> 16) & 0xff;
*(p++) = (color >> 8) & 0xff;
*(p++) = color & 0xff;
}
static void setpixel4(void *ptr, unsigned int color)
{
unsigned long *p = ptr;
*p = color;
}
int fs_init_fb(int white8)
{
switch (fb_var.bits_per_pixel) {
case 8:
white = white8; black = 0; bpp = 1;
setpixel = setpixel1;
break;
case 15:
case 16:
if (fb_var.green.length == 6)
white = 0xffff;
else
white = 0x7fff;
black = 0; bpp = 2;
setpixel = setpixel2;
break;
case 24:
white = 0xffffff; black = 0; bpp = fb_var.bits_per_pixel/8;
setpixel = setpixel3;
break;
case 32:
white = 0xffffff; black = 0; bpp = fb_var.bits_per_pixel/8;
setpixel = setpixel4;
break;
default:
fprintf(stderr, "Oops: %i bit/pixel ???\n",
fb_var.bits_per_pixel);
return -1;
}
return 0;
}
void fs_render_fb(unsigned char *ptr, int pitch,
FSXCharInfo *charInfo, unsigned char *data)
{
int row,bit,bpr,x;
bpr = GLWIDTHBYTESPADDED((charInfo->right - charInfo->left),
SCANLINE_PAD_BYTES);
for (row = 0; row < (charInfo->ascent + charInfo->descent); row++) {
for (x = 0, bit = 0; bit < (charInfo->right - charInfo->left); bit++) {
if (data[bit>>3] & fs_masktab[bit&7])
setpixel(ptr+x,white);
x += bpp;
}
data += bpr;
ptr += pitch;
}
}
int fs_puts(struct fs_font *f, int x, int y, unsigned char *str)
{
unsigned char *pos,*start;
int i,c,j,w;
pos = fb_mem+fb_mem_offset;
pos += fb_fix.line_length * y;
for (i = 0; str[i] != '\0'; i++) {
c = str[i];
if (NULL == f->eindex[c])
continue;
/* clear with bg color */
start = pos + x*bpp + f->fontHeader.max_bounds.descent * fb_fix.line_length;
w = (f->eindex[c]->width+1)*bpp;
for (j = 0; j < f->height; j++) {
memset(start,0,w);
start += fb_fix.line_length;
}
/* draw char */
start = pos + x*bpp + fb_fix.line_length * (f->height-f->eindex[c]->ascent);
fs_render_fb(start,fb_fix.line_length,f->eindex[c],f->gindex[c]);
x += f->eindex[c]->width;
if (x > fb_var.xres - f->width)
return -1;
}
return x;
}
int fs_textwidth(struct fs_font *f, unsigned char *str)
{
int width = 0;
int i,c;
for (i = 0; str[i] != '\0'; i++) {
c = str[i];
if (NULL == f->eindex[c])
continue;
width += f->eindex[c]->width;
}
return width;
}
void fs_render_tty(FSXCharInfo *charInfo, unsigned char *data)
{
int bpr,row,bit,on;
bpr = GLWIDTHBYTESPADDED((charInfo->right - charInfo->left),
SCANLINE_PAD_BYTES);
for (row = 0; row < (charInfo->ascent + charInfo->descent); row++) {
fprintf(stdout,"|");
for (bit = 0; bit < (charInfo->right - charInfo->left); bit++) {
on = data[bit>>3] & fs_masktab[bit&7];
fprintf(stdout,"%s",on ? "##" : " ");
}
fprintf(stdout,"|\n");
data += bpr;
}
fprintf(stdout,"--\n");
}
/* ------------------------------------------------------------------ */
#ifndef X_DISPLAY_MISSING
/* connect to font server */
int fs_connect(char *servername)
{
if (NULL == servername)
servername = getenv("FONTSERVER");
if (NULL == servername)
servername = "unix/:7100";
svr = FSOpenServer(servername);
if (NULL == svr) {
if (NULL == FSServerName(servername)) {
fprintf(stderr, "no font server defined\n");
} else {
fprintf(stderr, "unable to open server \"%s\"\n",
FSServerName(servername));
}
return -1;
}
return 0;
}
/* load font from font server */
struct fs_font* fs_open(char *pattern)
{
int nnames = 1;
int available,high,low,encoding,bpr;
char **fonts;
unsigned char *glyph;
Font dummy;
FSBitmapFormat format;
FSXCharInfo *charInfo;
struct fs_font *f = NULL;
if (NULL == svr) {
fprintf(stderr,"fs: not connected\n");
return NULL;
}
fonts = FSListFonts(svr, pattern, nnames, &available);
if (0 == available) {
fprintf(stderr,"fs: font not available [%s]\n",pattern);
goto out;
}
fprintf(stderr,"using x11 font \"%s\"\n",fonts[0]);
f = malloc(sizeof(*f));
memset(f,0,sizeof(*f));
f->font = FSOpenBitmapFont(svr, 0, 0, fonts[0], &dummy);
FSFreeFontNames(fonts);
if (0 == f->font)
goto out;
FSQueryXInfo(svr,f->font,&f->fontHeader, &f->propInfo,
&f->propOffsets, &f->propData);
format = BYTE_ORDER | BIT_ORDER | SCANLINE_UNIT | SCANLINE_PAD | EXTENTS;
FSQueryXExtents16(svr, f->font, True, (FSChar2b *) 0, 0, &f->extents);
FSQueryXBitmaps16(svr, f->font, format, True, (FSChar2b *) 0, 0,
&f->offsets, &f->glyphs);
f->maxenc = (f->fontHeader.char_range.max_char.high+1) << 8;
f->width = f->fontHeader.max_bounds.right - f->fontHeader.min_bounds.left;
f->height = f->fontHeader.max_bounds.ascent + f->fontHeader.max_bounds.descent;
f->eindex = malloc(f->maxenc * sizeof(FSXCharInfo*));
f->gindex = malloc(f->maxenc * sizeof(unsigned char*));
memset(f->eindex,0,f->maxenc * sizeof(FSXCharInfo*));
memset(f->gindex,0,f->maxenc * sizeof(unsigned char*));
glyph = f->glyphs;
charInfo = f->extents;
for (high = f->fontHeader.char_range.min_char.high;
high <= f->fontHeader.char_range.max_char.high;
high++) {
for (low = f->fontHeader.char_range.min_char.low;
low <= f->fontHeader.char_range.max_char.low;
low++) {
bpr = GLWIDTHBYTESPADDED((charInfo->right - charInfo->left),
SCANLINE_PAD_BYTES);
encoding = (high<<8) + low;
#ifdef TTY
fprintf(stdout,"e=0x%x | w=%d l=%d r=%d | a=%d d=%d\n",
encoding,charInfo->width,charInfo->left,
charInfo->right,charInfo->ascent,charInfo->descent);
#endif
if ((charInfo->width != 0) || (charInfo->right != charInfo->left)) {
f->gindex[encoding] = glyph;
f->eindex[encoding] = charInfo;
#ifdef TTY
fs_render_tty(f->eindex[encoding],
f->gindex[encoding]);
#endif
}
glyph += (charInfo->descent + charInfo->ascent) * bpr;
charInfo++;
}
}
return f;
out:
if (f)
fs_free(f);
return NULL;
}
#endif
void fs_free(struct fs_font *f)
{
if (f->gindex)
free(f->gindex);
#if 0
if (f->extents)
FSFree((char *) f->extents);
if (f->offsets)
FSFree((char *) f->offsets);
if (f->propOffsets)
FSFree((char *) (f->propOffsets));
if (f->propData)
FSFree((char *) (f->propData));
#endif
#if 0 /* FIXME */
if (f->glyphs)
FSFree((char *) f->glyphs);
#endif
free(f);
}
/* ------------------------------------------------------------------ */
/* load console font file */
static char *default_font[] = {
/* why the heck every f*cking distribution picks another
location for these fonts ??? */
"/usr/share/consolefonts/lat1-16.psf",
"/usr/share/consolefonts/lat1-16.psf.gz",
"/usr/share/consolefonts/lat1-16.psfu.gz",
"/usr/share/kbd/consolefonts/lat1-16.psf",
"/usr/share/kbd/consolefonts/lat1-16.psf.gz",
"/usr/share/kbd/consolefonts/lat1-16.psfu.gz",
"/usr/lib/kbd/consolefonts/lat1-16.psf",
"/usr/lib/kbd/consolefonts/lat1-16.psf.gz",
"/usr/lib/kbd/consolefonts/lat1-16.psfu.gz",
"/lib/kbd/consolefonts/lat1-16.psf",
"/lib/kbd/consolefonts/lat1-16.psf.gz",
"/lib/kbd/consolefonts/lat1-16.psfu.gz",
NULL
};
struct fs_font* fs_consolefont(char **filename)
{
int i;
char *h,command[256];
struct fs_font *f = NULL;
FILE *fp;
if (NULL == filename)
filename = default_font;
for(i = 0; filename[i] != NULL; i++) {
if (-1 == access(filename[i],R_OK))
continue;
break;
}
if (NULL == filename[i]) {
fprintf(stderr,"can't find console font file\n");
return NULL;
}
h = filename[i]+strlen(filename[i])-3;
if (0 == strcmp(h,".gz")) {
sprintf(command,"zcat %s",filename[i]);
fp = popen(command,"r");
} else {
fp = fopen(filename[i], "r");
}
if (NULL == fp) {
fprintf(stderr,"can't open %s: %s\n",filename[i],strerror(errno));
return NULL;
}
if (fgetc(fp) != 0x36 ||
fgetc(fp) != 0x04) {
fprintf(stderr,"can't use font %s\n",filename[i]);
return NULL;
}
fprintf(stderr,"using linux console font \"%s\"\n",filename[i]);
f = malloc(sizeof(*f));
memset(f,0,sizeof(*f));
fgetc(fp);
f->maxenc = 256;
f->width = 8;
f->height = fgetc(fp);
f->fontHeader.min_bounds.left = 0;
f->fontHeader.max_bounds.right = f->width;
f->fontHeader.max_bounds.descent = 0;
f->fontHeader.max_bounds.ascent = f->height;
f->glyphs = malloc(f->height * 256);
f->extents = malloc(sizeof(FSXCharInfo)*256);
fread(f->glyphs, 256, f->height, fp);
fclose(fp);
f->eindex = malloc(sizeof(FSXCharInfo*) * 256);
f->gindex = malloc(sizeof(unsigned char*) * 256);
for (i = 0; i < 256; i++) {
f->eindex[i] = f->extents +i;
f->gindex[i] = f->glyphs +i * f->height;
f->eindex[i]->left = 0;
f->eindex[i]->right = 7;
f->eindex[i]->width = 8;
f->eindex[i]->descent = 0;
f->eindex[i]->ascent = f->height;
}
return f;
}
#ifdef TESTING
/* ------------------------------------------------------------------ */
/* for testing */
int debug;
/* list fonts */
int fs_ls(char *pattern)
{
int nnames = 16;
int available,i;
char **fonts;
if (NULL == svr) {
fprintf(stderr,"fs: not connected\n");
return -1;
}
fonts = FSListFonts(svr, pattern, nnames, &available);
while (nnames <= available) {
nnames *= 2;
FSFreeFontNames(fonts);
fonts = FSListFonts(svr, pattern, nnames, &available);
}
for (i = 0; i < available; i++) {
fprintf(stderr,"%s\n",fonts[i]);
}
FSFreeFontNames(fonts);
return 0;
}
void dump_charset(struct fs_font *f)
{
unsigned char *pos;
int c,x,y;
x = 0, y = 0;
for (c = 0; c < f->maxenc; c++) {
if (NULL == f->eindex[c])
continue;
pos = fb_mem+fb_mem_offset;
pos += fb_fix.line_length * (y+f->height-f->eindex[c]->ascent);
pos += x*bpp;
fs_render_fb(pos,fb_fix.line_length,f->eindex[c],f->gindex[c]);
x += f->eindex[c]->right-f->eindex[c]->left+1;
if (x > fb_var.xres - f->width) {
x = 0;
y += f->height+1;
}
if (y > fb_var.yres - f->height)
break;
}
}
int main(int argc, char *argv[])
{
struct fs_font *f = NULL;
unsigned char dummy[42];
int fd;
if (argc < 2) {
fprintf(stderr,"missing arg\n");
exit(1);
}
/* try font server */
if (-1 != fs_connect(NULL)) {
fs_ls(argv[1]);
f = fs_open(argv[1]);
if (NULL == f)
fprintf(stderr,"no such font\n");
}
/* try console font */
if (NULL == f)
f = fs_consolefont(NULL);
if (NULL == f)
exit(1);
#ifdef TTY
exit(1);
#endif
fd = fb_init(NULL, NULL, 0);
fb_cleanup_fork();
fb_switch_init();
fs_init_fb();
if (argc < 3) {
dump_charset(f);
} else {
fs_puts(f,0,0,argv[2]);
}
fgets(dummy,42,stdin);
return 0;
}
#endif
|