2013-03-15 71 views
0

我已經將自定義字體制作爲存儲在XPM圖像中的位圖,並希望能夠使用可變前景色將其繪製到GdkDrawable對象。基本上,我想要的是使用圖像作爲字體,並能夠改變顏色。任何建議如何做到這一點?GTK +使用前景色作爲蒙版繪製位圖

回答

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; 
}