■
VBEのデバイスドライバを書いた後、画面に点や線、四角などは簡単に表示をできるが文字(フォント)を表示することは難しい。しかしビットマップのフォントデータさえあれば簡単に文字を表示することができる。Linux Kernelの/usr/src/linux/drivers/video/のディレクトリにビットマップフォントのファイルがあるのでこれを利用すれば簡単に画面に文字を表示することができる。
フォントファイルはC言語のファイルで保存されている。フォーマットは以下のようになっている。
static const unsigned char fontdata_8x16[FONTDATAMAX] = { ... /* 65 0x41 'A' */ 0x00, /* 00000000 */ 0x00, /* 00000000 */ 0x10, /* 00010000 */ 0x38, /* 00111000 */ 0x6c, /* 01101100 */ 0xc6, /* 11000110 */ 0xc6, /* 11000110 */ 0xfe, /* 11111110 */ 0xc6, /* 11000110 */ 0xc6, /* 11000110 */ 0xc6, /* 11000110 */ 0xc6, /* 11000110 */ 0x00, /* 00000000 */ 0x00, /* 00000000 */ 0x00, /* 00000000 */ 0x00, /* 00000000 */ ... }
フォントの表示はビットをマスクすることによって1ビットずつ表示すれば画面に文字が表示できる。
例えば、8xNビット限定のビットマップフォントを表示するfont.cとfont.hは以下の様になる。
font.h
#define BIT(x) (1 << x) struct font_descr { const char *name; uint32_t width; uint32_t height; uint32_t count; const unsigned char *data; }; extern const struct font_descr font_vga_10x18; extern const struct font_descr font_vga_7x14; extern const struct font_descr font_vga_8x14; extern const struct font_descr font_vga_8x16; extern const struct font_descr font_vga_8x8; extern const struct font_descr font_acorn_8x8; extern const struct font_descr font_mini_4x6; extern const struct font_descr font_pearl_8x8; extern const struct font_descr font_profont_6x11; extern const struct font_descr font_sun_12x22; extern const struct font_descr font_sun_8x16;
font.c
int putfont(int c, uint32_t offset_x, uint32_t offset_y) { uint32_t x, y; struct font_descr *font = &font_vga_8x16; if (c < 0 || 255 < c) return -1; for (y = 0; y < font->height; y++) { for (x = 0; x < font->width; x++) { if (font->data[c * font->height + y] & BIT(font->width - x)) set_color(WHITE); else set_color(BLACK); set_pixel(x + offset_x, y + offset_y); } } return 0; }