#include #include #include #include void die( char *s) { puts( s); exit(1); } void expose( Display *dpy, Window win, XftDraw *xftdraw, XftColor *xftcolor, XftFont *xftfont) { FT_UInt glyphs[16]; FT_UInt i,j; XClearWindow( dpy, win); for( i=0; i<16; i++) { for( j=0; j<16; j++) { glyphs[j] = 16*i + j; } XftDrawGlyphs( xftdraw, xftcolor, xftfont, 10, 10 + xftfont->ascent + (xftfont->ascent + xftfont->descent)*i, glyphs, 16); } // puts( "drew string"); } int main(int argc, char *argv[]) { Display *dpy; int screen; Window win; Visual *visual; Colormap cmap; XftFont *xftfont = NULL; XftDraw *xftdraw; XRenderColor color; XftColor xftcolor; XftPattern *pattern = XftPatternCreate(), *match; XftResult fr; XEvent ev; char *fontname; if( argc > 1) fontname = argv[1]; else fontname = "Bitstream Vera Sans Mono"; if( !(dpy = XOpenDisplay(0)) ) die("Could not open display"); screen = DefaultScreen( dpy); visual = DefaultVisual( dpy, screen); cmap = DefaultColormap( dpy, screen); XftPatternAddString( pattern, XFT_FAMILY, fontname); XftPatternAddInteger( pattern, XFT_SIZE, 20); XftPatternAddInteger( pattern, XFT_SPACING, XFT_MONO); match = XftFontMatch ( dpy, screen, pattern, &fr); FcPatternPrint( match); xftfont = XftFontOpenPattern( dpy, match); /* * BUG: XftFontOpenPattern() frees the pattern if it returns an already * open font. Thus closing and re-opening the *same* font will cause a * segfault when we attempt to acces the pattern match. Commenting the next * two lines causes perfect execution. */ XftFontClose( dpy, xftfont); xftfont = XftFontOpenPattern( dpy, match); XftPatternDestroy( match); XftPatternDestroy( pattern); win = XCreateSimpleWindow( dpy, DefaultRootWindow(dpy), 0, 0, 20 + xftfont->max_advance_width * 16, 20 + (xftfont->ascent + xftfont->descent) * 16, 0, BlackPixel( dpy, screen), BlackPixel( dpy, screen)); xftdraw = XftDrawCreate(dpy, win, visual, cmap); color.red = 0xb000; color.blue = 0xb000; color.green = 0xb000; color.alpha = 0; XftColorAllocValue(dpy, visual, cmap, &color, &xftcolor); // Wait untill window is mapped XSelectInput( dpy, win, ExposureMask | KeyPressMask ); XMapWindow( dpy, win); while(1) { XNextEvent( dpy, &ev); if( ev.type == KeyPress && ev.xkey.keycode == 24 ) break; else if( ev.type == Expose) { while( XCheckTypedWindowEvent( dpy, win, ExposureMask, &ev) ); expose(dpy, win, xftdraw, &xftcolor, xftfont); } }; XftColorFree( dpy, visual, cmap, &xftcolor); XftDrawDestroy( xftdraw); XftFontClose( dpy, xftfont); XCloseDisplay(dpy); puts( "Closed."); return 0; }