#include #include #include #include #include #include #include #include #include #include /*! Simple Cairo/Xlib example. * @author Bernhard R. Fischer, 2048R/5C5FFD47 . * @version 2014110801 * Compile with * gcc -Wall $(pkg-config --libs --cflags cairo x11) -o cairo_xlib_simple cairo_xlib_simple.c */ /*! Check for Xlib Mouse/Keypress events. All other events are discarded. * @param sfc Pointer to Xlib surface. * @param block If block is set to 0, this function always returns immediately * and does not block. if set to a non-zero value, the function will block * until the next event is received. * @return The function returns 0 if no event occured (and block is set). A * positive value indicates that a key was pressed and the X11 key symbol as * defined in is returned. A negative value indicates a mouse * button event. -1 is button 1 (left button), -2 is the middle button, and -3 * the right button. */ int cairo_check_event(cairo_surface_t *sfc, int block) { char keybuf[8]; KeySym key; XEvent e; for (;;) { cairo_surface_flush (sfc); if (block || XPending(cairo_xlib_surface_get_display(sfc))) XNextEvent(cairo_xlib_surface_get_display(sfc), &e); else return 0; switch (e.type) { case ButtonPress: return -e.xbutton.button; case KeyPress: XLookupString(&e.xkey, keybuf, sizeof(keybuf), &key, NULL); return key; default: fprintf(stderr, "Dropping unhandled XEevent.type = %d.\n", e.type); } } } /*! Open an X11 window and create a cairo surface base on that window. * @param x Width of window. * @param y Height of window. * @return Returns a pointer to a valid Xlib cairo surface. The function does * not return on error (exit(3)). */ cairo_surface_t *cairo_create_x11_surface0(int x, int y) { Display *dsp; Drawable da; int screen; cairo_surface_t *sfc; if ((dsp = XOpenDisplay(NULL)) == NULL) exit(1); screen = DefaultScreen(dsp); da = XCreateSimpleWindow(dsp, DefaultRootWindow(dsp), 0, 0, x, y, 0, 0, 0); XSelectInput(dsp, da, ButtonPressMask | KeyPressMask); XMapWindow(dsp, da); sfc = cairo_xlib_surface_create(dsp, da, DefaultVisual(dsp, screen), x, y); cairo_xlib_surface_set_size(sfc, x, y); return sfc; } /*! Destroy cairo Xlib surface and close X connection. */ void cairo_close_x11_surface(cairo_surface_t *sfc) { Display *dsp = cairo_xlib_surface_get_display(sfc); cairo_surface_destroy(sfc); XCloseDisplay(dsp); } static int sizes[]= {8,10,12,16,18,24,36,48,60,72,84,96}; int main(int argc, char **argv) { cairo_surface_t *sfc; cairo_t *ctx; sfc = cairo_create_x11_surface0(700, 400); ctx = cairo_create(sfc); cairo_set_source_rgb(ctx, 1, 1, 1); cairo_paint(ctx); FT_Library lib; FT_Init_FreeType(&lib); FT_Face ft_face; FT_New_Face(lib,"cmex10.otf",0,&ft_face); cairo_font_options_t *opts = cairo_font_options_create (); cairo_font_options_set_hint_style (opts, CAIRO_HINT_STYLE_SLIGHT); cairo_set_font_options (ctx, opts); cairo_font_options_destroy (opts); cairo_font_face_t *font_face = cairo_ft_font_face_create_for_ft_face (ft_face, 0); cairo_set_source_rgb(ctx, 0, 0, 0); int i; for (i=0; i<12; i++) { cairo_move_to (ctx, 5+100*(i%6), 20+80*(i/6)); #if 1 cairo_select_font_face(ctx,"sans-serif",CAIRO_FONT_SLANT_NORMAL,CAIRO_FONT_WEIGHT_NORMAL); cairo_set_font_size (ctx, 8); char size[3]; sprintf(size,"%d",sizes[i]); cairo_show_text(ctx,size); #endif #if 1 cairo_set_font_face (ctx, font_face); cairo_set_font_size (ctx, sizes[i]); cairo_show_text(ctx,"r"); #endif } cairo_destroy(ctx); cairo_check_event(sfc, 1); cairo_close_x11_surface(sfc); return 0; }