我已经制作了一个自定义字体作为存储在xpm图像中的位图,并希望能够将其绘制为具有可变前景颜色的GdkDrawable-object。基本上,我想要的是使用图像作为字体,并能够改变颜色。有什么建议吗?
这不是我最初想要的解决方案,但这是一个解决方案,在更好的解决方案出现之前,它将不得不这样做。
/* 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;
}