0

我已经制作了一个自定义字体作为存储在 XPM 图像中的位图,并且希望能够使用可更改的前景色将其绘制到 GdkDrawable 对象。基本上,我想要的是使用图像作为字体并能够更改颜色。任何建议如何做到这一点?

4

1 回答 1

0

这并不完全是我最初想要的解决方案,但它是一个解决方案,它必须这样做,直到出现更好的解决方案。

/* XPM-image containing font consist of 96 (16x6) ASCII-characters starting with space. */
#include "font.xpm"

typedef struct Font {
    GdkPixbuf *image[16]; /* Image of font for each colour. */
    const int width; /* Grid-width. */
    const int height; /* Grid-height */
    const int ascent; /* Font ascent from baseline. */
    const int char_width[96]; /* Width of each character. */
} Font;

typedef enum TextColor { FONT_BLACK,FONT_BROWN,FONT_YELLOW,FONT_CYAN,FONT_RED,FONT_WHITE } TextColor;
typedef enum TextAlign { ALIGN_LEFT,ALIGN_RIGHT,ALIGN_CENTER } TextAlign;

Font font = {
    {0},
    7,9,9,
    {
        5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
        5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
        5,5,5,5,5,5,5,5,5,5,5,5,5,6,5,5,
        5,5,5,5,5,5,5,6,5,5,5,5,5,5,5,5,
        5,5,5,5,5,5,5,5,5,5,5,5,5,6,5,5,
        5,5,5,5,5,5,5,6,5,5,5,5,5,5,5,5,
    }
};

void load_font(Font *font,const char **font_xpm) {
    const char *colors[] = { /* It's not complicated to adjust for more elaborate colour schemes. */
        ".  c #000000", /* Black */
        ".  c #3A2613", /* Brown */
        ".  c #FFFF00", /* Yellow */
        ".  c #00FFFF", /* Cyan */
        ".  c #FF0000", /* Red */
        ".  c #FFFFFF", /* White */
    NULL};
    int i;
    memset(font->image,0,sizeof(GdkPixbuf *)*16);
    for(i=0; colors[i]!=NULL; ++i) {
        font_xpm[2] = colors[i]; /* Second colour is assumed to be font colour. */
        font->image[i] = gdk_pixbuf_new_from_xpm_data(font_xpm);
    }
}

int draw_string(Font *font,int x,int y,TextAlign align,TextColor color,const char *str) {
    int i,w = 0;
    const char *p;
    for(p=str; *p; ++p) i = *p-' ',w += i>=0 && i<96? font->char_width[i] : 0;
    if(align==ALIGN_RIGHT) x -= w;
    else if(align==ALIGN_CENTER) x -= w/2;
    for(p=str; *p; ++p) {
        i = *p-' ';
        if(i>=0 && i<96) {
            gdk_draw_pixbuf(pixmap,gc,font->image[(int)color],(i%16)*font->width,(i/16)*font->height,x,y,font->char_width[i],font->height,GDK_RGB_DITHER_NONE,0,0);
            x += font->metrics[i];
        }
    }
    return x;
}
于 2013-03-16T04:02:05.177 回答