所以我利用bdf2c產生字型的font.h和font.c檔案
之後只要
#include "font.h"
extern struct bitmap_font font;
就可以使用
struct bitmap_font {
unsigned char Width; //字元最大寬度
unsigned char Height; //字元高度
unsigned short Chars; //總字元數
const unsigned char *Widths; //每個字元的寬度
const unsigned short *Index; //每個字元的索引
const unsigned char *Bitmap; //每個字元的資料
};
舉例如果我要印出A字元(65)
就要找出65在font.Index的位置(pos)
然後資料就是在font.Bitmap的font.Width*font.Height/8*pos的位置
長度則是font.Width*font.Height/8
範例
bool rasters(const wchar_t texts[], Bitmap *bitmap)
{
if(texts[0] == '\0') {
return true;
}
extern struct bitmap_font font;
int maxsize = font.Width * font.Height / 8;
for(int i=0; i<wcslen(texts); i++) {
unsigned int ucode = texts[i];
if(ucode >= font.Chars) {
continue;
}
unsigned short index;
for(index = 0; index<= ucode; index++) {
if(font.Index[index] == ucode) {
break;
}
}
if(index > ucode) {
continue;
}
unsigned int w = font.Widths[index];
unsigned int pitch = font.Width/8;
if(w % 8 != 0) {
pitch += 1;
}
unsigned char *array = calloc(maxsize, sizeof(unsigned char));
memcpy(array, font.Bitmap+index*maxsize, maxsize);
Bitmap chars = {0, 0, w, font.Height, pitch, array};
combinetext(bitmap, &chars);
}
return true;
}
沒有留言:
張貼留言