[GtkGLExt] Re: Pango rendering



>From: Tristan Van Berkom
>
>    I've noticed in the example codes in gtkglext that rendering fonts
>to texture surfaces from pango layouts requires some intimate knowlage
>of the FreeType bitmap, i.e. code that looks like:

Please place the relevant code available when ready.
As a quick font solution I did read each pixel value and made
opengl quad if the pixel was on. Next I should try out outline
polygons, texture fonts, and the stuff mentioned at www.opengl.org.

Here is the code if it has any use. Screenshot is at
"http://www.funet.fi/~kouhia/enved7.png";.

// Generates the opengl quad data for the given text.
void glpangofont_new(char *s, int *quads, float **quaddata)
{
  PangoContext *context;
  PangoLanguage *language;
  PangoFontDescription *font_desc;
  PangoLayout *layout;
  int width,height;
  GdkPixmap *pixmap;
  GdkGC *gc;
  GdkColor colorb,colorw;
  GdkImage *image;
  int k,x,y;
  float *q;
  float scale;

  context = gdk_pango_context_get();
  language = gtk_get_default_language();
  pango_context_set_language (context, language);
  pango_context_set_base_dir (context, PANGO_DIRECTION_LTR);

  font_desc = pango_font_description_from_string ("courier 12");
  pango_context_set_font_description (context, font_desc);

  layout = pango_layout_new(context);
  pango_layout_set_alignment (layout, PANGO_ALIGN_LEFT);
  pango_layout_set_text (layout, s, -1);
  pango_layout_get_pixel_size (layout, &width, &height);

  pixmap = gdk_pixmap_new(NULL,width,height,8);
  gc = gdk_gc_new(pixmap);
  colorw.red = 0xffff; 
  colorw.green = 0xffff; 
  colorw.blue = 0xffff; 
  colorb.red = 0x0000; 
  colorb.green = 0x0000; 
  colorb.blue = 0x0000;
  gdk_gc_set_foreground(gc,&colorw);
  gdk_gc_set_background(gc,&colorw);
  gdk_draw_rectangle (pixmap, gc, TRUE, 0, 0, width, height);
  gdk_gc_set_foreground(gc,&colorb);
  gdk_gc_set_background(gc,&colorw);
  gdk_draw_layout(pixmap, gc, 0, 0, layout);
  image = gdk_drawable_get_image (pixmap, 0, 0, width, height);

  // Count font pixels.
  k = 0;
  for (y = 0; y < height; y++) {
    for (x = 0; x < width; x++) {
      if (gdk_image_get_pixel(image,x,height-1-y) == 0) {
        k++;
      }
    }
  }

  q = array_float(k*4*2); // One "xy xy xy xy" makes a quad.

  // Gen quads. Height will be 1.0.
  scale = 1.0/(float)height;
  k = 0;
  for (y = 0; y < height; y++) {
    for (x = 0; x < width; x++) {
      if (gdk_image_get_pixel(image,x,height-1-y) == 0) {
        q[8*k+0] = (float)x*scale;
        q[8*k+1] = (float)y*scale;
        q[8*k+2] = (float)x*scale;
        q[8*k+3] = (float)(y+1)*scale;
        q[8*k+4] = (float)(x+1)*scale;
        q[8*k+5] = (float)(y+1)*scale;
        q[8*k+6] = (float)(x+1)*scale;
        q[8*k+7] = (float)y*scale;
        k++;
      }
    }
  }

  pango_font_description_free(font_desc);
  // Perhaps something else should be freed; how?

  *quads = k;
  *quaddata = q;
}

// Renders the opengl quad data.
void glpangofont_draw(int quads, float *quaddata)
{
  int i;
  float x1,x2,x3,x4;
  float y1,y2,y3,y4;

  for (i = 0; i < quads; i++) {
    x1 = quaddata[i*8+0];
    y1 = quaddata[i*8+1];
    x2 = quaddata[i*8+2];
    y2 = quaddata[i*8+3];
    x3 = quaddata[i*8+4];
    y3 = quaddata[i*8+5];
    x4 = quaddata[i*8+6];
    y4 = quaddata[i*8+7];
    glVertex3f(x1,y1,0.0);
    glVertex3f(x2,y2,0.0);
    glVertex3f(x3,y3,0.0);
    glVertex3f(x4,y4,0.0);
  }
}

Juhana
-- 
  http://music.columbia.edu/mailman/listinfo/linux-graphics-dev
  for developers of open source graphics software



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]