Trying to figure out how to render a single character represented by a FT_Bitmap in X

joe M joe9mail at gmail.com
Fri Feb 15 19:17:55 PST 2013


Hello,

I am trying to figure how to print one character from a FT_Bitmap
(freetype2 bitmap).

But, I am not able to get the character to display properly. I seem to
be missing something simple.

I seem to be missing something simple while creating the X pixmap
based on the FT_Bitmap.buffer.

Any thoughts, please?

Thanks
Joe

#include <X11/Xlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>

#include <ft2build.h>
#include FT_FREETYPE_H

int
main( int     argc,
      char**  argv )
{
  FT_Library    library;
  FT_Face       face;

  FT_GlyphSlot  slot;
  FT_Matrix     matrix;                 /* transformation matrix */
  FT_Vector     pen;                    /* untransformed origin  */
  FT_Error      error;

   Display *d;
   Window w;
   XEvent e;
   int s,i;
   Pixmap p, pixmap;
    unsigned int bwidth, bheight;
    int hotspot_x, hotspot_y;

  error = FT_Init_FreeType( &library );              /* initialize library */
  /* error handling omitted */

  error = FT_New_Face( library,
"/usr/share/fonts/anonymous-pro/Anonymous Pro I.ttf", 0, &face );/*
create face object */
  /* error handling omitted */

 error = FT_Set_Pixel_Sizes( face, 0, 24  );

  slot = face->glyph;

  error = FT_Load_Char( face, 'z', FT_LOAD_RENDER );

   d = XOpenDisplay(NULL);
   if (d == NULL) {
      fprintf(stderr, "Cannot open display\n");
      exit(1);
   }

   s = DefaultScreen(d);
   w = XCreateSimpleWindow(d, RootWindow(d, s), 10, 10, 100, 100, 1,
                           BlackPixel(d, s), WhitePixel(d, s));
   XSelectInput(d, w, ExposureMask | KeyPressMask);
   XMapWindow(d, w);

   while (1) {
      XNextEvent(d, &e); if (e.type == Expose) {
         p = XCreateBitmapFromData(d, w,
               slot->bitmap.buffer, slot->bitmap.width ,
                     slot->bitmap.rows);
         XWriteBitmapFile(d,"test1.bmp",p,slot->bitmap.width,slot->bitmap.rows,0,0);
         XCopyPlane(d, p , w, DefaultGC(d,s),
                    0, 0,
                     slot->bitmap.width, slot->bitmap.rows,
                     40, 40,1);
         XSync(d, False);
         usleep(100000);
      }
      if (e.type == KeyPress)
         break;
   }
   XCloseDisplay(d);

  FT_Done_Face    ( face );
  FT_Done_FreeType( library );

  return 0;
}

/* EOF */


More information about the xorg mailing list