2012年3月17日 星期六

convert TTF to Bitmap

因研究須要轉TTF到點陣字型所以放了下面的程式

#include 
#include 
struct bitFontMap
{
    char** data;
    int width;
    int height;
};
void bitFont(HDC hdc, int fontSize, char code, bitFontMap* bitfont, char* face);
void print(bitFontMap bitfont);
int main()
{
    SetConsoleTitle("Neo");
    HWND hwnd = FindWindow(0, "Neo");
    PAINTSTRUCT ps;
    HDC hdc = BeginPaint(hwnd, &ps);
    hdc = GetDC(hwnd);
    bitFontMap array;
    bitFont(hdc, 32, 'a', &array, "Arial bold");
    print(array);
    getchar();
    return 0;
}
void bitFont(HDC hdc, int fontSize, char code, bitFontMap* bfm, char* face)
{
    MAT2 mat = {{0, 1}, {0, 0}, {0, 0}, {0, 1}}; //轉置矩陣
    HFONT font = CreateFont(fontSize, 0, 0, 0, 400, 0, 0, 0, 0, 0, 0, 0, 0, face);
    HGDIOBJ oldFont = SelectObject(hdc, font);
    GLYPHMETRICS gm;
    int bytes = GetGlyphOutline(hdc, code, 1, &gm, 0, 0, &mat);
    char* buffer = (char*)malloc(sizeof(char) * bytes);
    GetGlyphOutline(hdc, code, 1, &gm, bytes, buffer, &mat);
    int nx = bytes / gm.gmBlackBoxY;
    bfm->width = nx * 9;
    bfm->height = gm.gmBlackBoxY;
    //-----初始化bfm.data[bfm.height][bfm.width]------
    bfm->data = (char**)malloc(bfm->height * sizeof(char*) + (bfm->height * bfm->width) * sizeof(char));
    char* pData;
    int m;
    for (m = 0, pData = (char*)(bfm->data + bfm->height); m < bfm->height; m++, pData += bfm->width)
    {
        for (int n = 0; n < bfm->width; n++)
        {
            pData[n] = 0;
        }
        bfm->data[m] = pData;
    }
    //------------------------------------------------
    int x, j, i;
    m = 0;
    for (x, j, i = 0; i < bytes;)
    {
        int n = 0;
        for (x = 0; x < nx; x++, i++)
        {
            for (j = 1 << 8; j; j >>= 1)
            {
                if (buffer[i] & (j))
                {
                    bfm->data[m][n] = 1;
                }
                else
                {
                    bfm->data[m][n] = 0;
                }
                n++;
            }
        }
        m++;
    }
}
void print(bitFontMap bitfont)
{
    for (int y = 0; y < bitfont.height; y++)
    {
        printf("\n");
        for (int x = 0; x < bitfont.width; x++)
            printf("%d", bitfont.data[y][x]);
    }
}

沒有留言:

張貼留言