Re: Pango offscreen at client side?



Hello. I solved the problem.

I did check pangoft2topgm.c but because installer did not found
the freetype2 in RedHat, I could not use it. If I modify the code to
use X fonts, then there is another problem:
  gdkdisplay = gdk_display_get_default();
  xdisplay = ????????
  context = pango_x_get_context(xdisplay);
How to get Display as returned by XOpenDisplay()?

The working code is included below. I only added code to clean
the pixmap before the text is drawn on to it. The mailing list
archives have mostly GTK related solutions, but here is now GDK
related code. The font is not pretty, though.

I could not figure out how to free some of the objects. Anyone?

There might not be more inefficient way to get fonts to opengl
application.

Juhana

 == cut ==
// 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);

  *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);
  }
}
 == end ==



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