xfonts.c

Go to the documentation of this file.
00001 #include <X11/X.h>
00002 #include <X11/Xlib.h>
00003 /* Get the metrics for a character. */
00004 typedef struct gs_point_s {
00005     double x, y;
00006 } gs_point;
00007 typedef struct gs_int_point_s {
00008     int x, y;
00009 } gs_int_point;
00010 typedef struct gs_int_rect_s {
00011     gs_int_point p, q;
00012 } gs_int_rect;
00013 
00014 int x_char_metrics(XFontStruct* x11font, char chr, gs_point* pwidth, gs_int_rect* pbbox)
00015 {
00016   if (x11font->per_char == NULL) {
00017     pwidth->x = x11font->max_bounds.width;
00018     pwidth->y = 0;
00019     pbbox->p.x = x11font->max_bounds.lbearing;
00020     pbbox->q.x = x11font->max_bounds.rbearing;
00021     pbbox->p.y = -x11font->max_bounds.ascent;
00022     pbbox->q.y = x11font->max_bounds.descent;
00023   } else {
00024         int i = (int)chr - x11font->min_char_or_byte2;
00025 
00026         pwidth->x = x11font->per_char[i].width;
00027         pwidth->y = 0;
00028 
00029         pbbox->p.x = x11font->per_char[i].lbearing;
00030         pbbox->q.x = x11font->per_char[i].rbearing;
00031         pbbox->p.y = -x11font->per_char[i].ascent;
00032         pbbox->q.y = x11font->per_char[i].descent;
00033     }
00034     return 0;
00035 }
00036 
00037 /* Render a character. */
00038 int
00039 x_render_char(Display* dpy, XFontStruct* x11font, char chr, int xo, int yo)
00040 {
00041   gs_int_rect bbox;
00042   int x, y, w, h;
00043 
00044 
00045   /* Display on an intermediate bitmap, then copy the bits. */
00046   int i;
00047   XImage *xim;
00048   Pixmap xpm;
00049   GC fgc;
00050   gs_point wxy;
00051   int code;
00052 
00053   code = x_char_metrics(x11font, chr, &wxy, &bbox);
00054 
00055   w = bbox.q.x - bbox.p.x;
00056   h = bbox.q.y - bbox.p.y;
00057 
00058 
00059   xpm = XCreatePixmap(dpy, InputOnly, w, h, 1);
00060   fgc = XCreateGC(dpy, xpm, None, NULL);
00061   XSetForeground(dpy, fgc, 0);
00062   XFillRectangle(dpy, xpm, fgc, 0, 0, w, h);
00063   XSetForeground(dpy, fgc, 1);
00064   XSetFont(dpy, fgc, x11font->fid);
00065   XDrawString(dpy, xpm, fgc, -bbox.p.x, -bbox.p.y, &chr, 1);
00066   xim = XGetImage(dpy, xpm, 0, 0, w, h, 1, ZPixmap);
00067   i = 0;
00068   for (y = 0; y < h; y++) {
00069     char b;
00070 
00071     for (x = 0; x < w; x++) {
00072         b = XGetPixel(xim, x, y);
00073       /*      if ((x & 7) == 7)
00074               bits[i++] = b;*/
00075     }
00076   }
00077   XFreePixmap(dpy, xpm);
00078   XFreeGC(dpy, fgc);
00079   XDestroyImage(xim);
00080   return;
00081     
00082 }

Generated on Sun Mar 4 15:03:54 2007 for Scilab [trunk] by  doxygen 1.5.1