Hi, I try to use freetype with xlib, I , I need to pass a char type array 
(RGBA/pixel) to a xlib function.

from freetype I got :

  face->glyph->bitmap.pixel_mode = 2
so if I understood corectly in face->glyph->bitmap.buffer datas are stored in 
gray level.
  face->glyph->bitmap.rows=16
  face->glyph->bitmap.width=8
  face->glyph->bitmap.pitch=8

so I guess,  length of face->glyph->bitmap.buffer = 1(gray 
lvl)*16(width)*8(rows)=128 unsigned chars, right ?

then I create an array of char for xlib from face->glyph->bitmap.buffer

  for(i=0;i<128;i++){                                            
    Xdata[4*i]     = face->glyph->bitmap.buffer[i];
    Xdata[4*i+1] = face->glyph->bitmap.buffer[i];
    Xdata[4*i+2] = face->glyph->bitmap.buffer[i];
    Xdata[4*i+3] = 255;                                        
   }                                                                       
and I try to create a XImage :
  ximage = XCreateImage(display, DefaultVisual(display,0), 24, ZPixmap, 0,      
                     

                                        &Xdata[0], 8/*rows*/, 16/*width*/, 
32/*RGBA*/, 8/*pitch??*/);
  XPutImage(display,XP,gc,ximage,0,0,0,0,8/*rows*/,16/*width*/);                
                       

unfornatly I got a segmentation fault at XPutImage, I'm stuck with this for two 
days now... I join the full code, to compile :
g++ testFT.cpp -lX11 -I/usr/include/freetype2 -lfreetype


-Nicoo
#include<iostream>
#include<unistd.h>
#include<X11/Xlib.h>
#include<X11/Xutil.h>
#include<ft2build.h>
#include FT_FREETYPE_H

using namespace std;

int main()
{
  FT_Library library;
  FT_Face    face;
  Display *display;
  int error;
  int glyph_index;
  GC gc;
  XImage *ximage;

  error = FT_Init_FreeType(&library);
  if(error){
    cout << "error 1\n";
    exit(1);
  }

  error = FT_New_Face(library, "/usr/share/fonts/TTF/comic.ttf",0,&face);

  if(error==FT_Err_Unknown_File_Format){
    cout << "error 2\n";
    exit(1);
  }else if (error){
    cout << "error 3\n";
    exit(1);
  }

  error = FT_Set_Pixel_Sizes(face,0,16);
  if(error){
    cout << "error 4\n";
    exit(1);
  }

  glyph_index = FT_Get_Char_Index(face,0x00028);
  if(glyph_index==0){
    cout << "error 5\n";
    exit(1);
  }

  error = FT_Load_Glyph(face, glyph_index, FT_LOAD_DEFAULT);
  if(error){
    cout << "error 6\n";
    exit(1);
  }

  error= FT_Render_Glyph(face->glyph,FT_RENDER_MODE_NORMAL);
  if(error){
    cout << "error 7\n";
    exit(1);
  }

  cout << "rows=" << face->glyph->bitmap.rows << endl;
  cout << "width=" << face->glyph->bitmap.width << endl;
  cout << "pitch=" << face->glyph->bitmap.pitch << endl;
  cout << "pixel_mode=" << size_t(face->glyph->bitmap.pixel_mode) << endl;

  char Xdata[4*128];
  int i;
  for(i=0;i<128;i++){
    Xdata[4*i]   = face->glyph->bitmap.buffer[i];
    Xdata[4*i+1] = face->glyph->bitmap.buffer[i];
    Xdata[4*i+2] = face->glyph->bitmap.buffer[i];
    Xdata[4*i+3] = 0;
  }



  display = XOpenDisplay(0);
  Window XP = XCreateSimpleWindow(display,DefaultRootWindow(display),0,0,360,90,0,0,0);
  gc = XCreateGC(display,XP,0,0);
  ximage = XCreateImage(display,DefaultVisual(display,0),24,ZPixmap,0,&Xdata[0],8,16,32,8);
  XPutImage(display,XP,gc,ximage,0,0,0,0,8,16);


  XMapWindow(display,XP);

  XFlush(display);
  XFreeGC(display,gc);

  sleep(2);

  return 0;
}
_______________________________________________
xorg@lists.x.org: X.Org support
Archives: http://lists.freedesktop.org/archives/xorg
Info: http://lists.x.org/mailman/listinfo/xorg
Your subscription address: arch...@mail-archive.com

Reply via email to