GTK +使用前景色将位图绘制为蒙版

时间:2013-03-15 00:51:03

标签: c gtk gdkpixbuf

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

1 个答案:

答案 0 :(得分: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;
}