Question

I've made a custom font as a bitmap stored in a XPM-image, and want to be able to draw it with a changeable foreground colour to a GdkDrawable-object. Basically, what I want is to use an image as a font and be able to change the colour. Any suggestions how to do this?

Était-ce utile?

La solution

It's not exactly the solution I intended at first, but it's a solution and it will have to do until a better one appears.

/* 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;
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top