2010年6月22日 星期二

print bitmap font array

先定義一下bitmap格式方便之後處裡
typedef struct Bitmap_ {
    int             x;
    int             y;
    unsigned int    width;
    unsigned int    height;
    unsigned int    pitch;
    unsigned char*  data;

} Bitmap;
下面是完整的程式,執行後將列印出一個大寫的F

#include <stdlib.h>
#include <stdio.h>
#include <locale.h>

typedef struct Bitmap_ {
    int             x;
    int             y;
    unsigned int    width;
    unsigned int    height;
    unsigned int    pitch;
    unsigned char*  data;

} Bitmap;

void printtext(Bitmap *bitmap)
{
    for(int j=0; j<bitmap->height; j++) {
        for(int i=0; i<bitmap->pitch; i++) {
            for(int k=7; k>=0; k--) {
                if(7-k+i*8 == bitmap->width)
                    break;
                wprintf(L"%c", (bitmap->data[i+j*bitmap->pitch] & 0x1<<k)?L'■':L'□');
            }
        }
        printf("\n");
    }
}

int main(int argc, char *argv[])
{
    if (!setlocale(LC_CTYPE, "")) {
        return EXIT_FAILURE;
    }

    unsigned char rasters[24] = { 0xff,0xc0,
                                  0xff,0xc0,
                                  0xc0,0x00,
                                  0xc0,0x00,
                                  0xc0,0x00,
                                  0xff,0x00,
                                  0xff,0x00,
                                  0xc0,0x00,
                                  0xc0,0x00,
                                  0xc0,0x00,
                                  0xc0,0x00,
                                  0xc0,0x00
                                };

    Bitmap bitmap = {0, 0, 10, 12, 2, rasters};
    printtext(&bitmap);
    return EXIT_SUCCESS;
}