Re: Render Text to Pixbuf



John,

Thanks for the code.  I managed to get a C++ version working.

Although, as I was worried, its slower than molasses.  The basic flow
is more or less like this:

Copy pixbuf to pixmap
render text on top of pixmap
Create temporary pixbuf from pixmap
copy from pixbuf back to original pixbuf.

If anyone has any ideas on how to make this faster, I'd much appreciate it.

Thanks,
Paul Davis

On 4/30/07, John Spray <jcspray icculus org> wrote:
On Mon, 2007-04-30 at 14:32 -0500, Paul Davis wrote:
> Hey,
>
> I'm working on trying to render text to a image that eventually gets
> painted on screen. Basically all I have access to is a Gdk::Pixbuf
> which I edit directly when I'm trying to do the text rendering.

Here's some code that I wrote quickly a while ago, no guarantees as to
how good/right it is.  GTK+ I'm afraid, but you should have no trouble
translating to gtkmm.

John

GdkPixbuf *DrawOverlay (GdkPixbuf *pb, int w, int h, gchar *text)
{
  GdkPixmap *pm = gdk_pixmap_new (NULL, w, h, 24);


  GdkGC *gc = gdk_gc_new (pm);
  gdk_draw_pixbuf (pm, gc, pb,
    0, 0, 0, 0,
    w, h,
    GDK_RGB_DITHER_NONE,
    0,
    0);

  GtkWidget *scratch = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_widget_realize (scratch);
  PangoLayout *layout = gtk_widget_create_pango_layout (scratch, NULL);
  gtk_widget_destroy (scratch);

  gchar *markup = g_strdup_printf (
    "<big><b><span foreground='#FFFF00'>%s</span></b></big>", text);
  pango_layout_set_markup (layout, markup, -1);
  g_free (markup);

  gdk_draw_layout (pm, gc, 10, 10, layout);
  g_object_unref (layout);

  GdkPixbuf *ret = gdk_pixbuf_get_from_drawable
    (NULL,
    pm,
    NULL,
    0,
    0,
    0,
    0,
    w,
    h);

  return ret;
}






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