Re: render texts to a buffer/drawable



Hi John,

On 25 Jun 2001 12:08:40 +0100, John Cupitt wrote:
> - gdk_font_load() the font you want
> - gdk_text_extents() to work out how big your string will be (in pixels)
> - gdk_pixmap_new() to make a pixmap large enough to hold rendered text
> - make a black/white GC to render with
> - clear the pixmap to the background colour
> - gdk_draw_string() to draw the text into the pixmap
> - gdk_image_get() to read the image from the server as a GdkImage*
> - malloc yor RGB buffer, and unpack the GdkImage into that

Thanks for the tips, I feel like I'm almost there.
Basically, I do see the text, there's just two problem, the color of the
text is changing all the time and I'm not sure how to get the background
transparent.

> About 4 pages of code :-( I believe this is what gimp does (or it did
> last time I looked).

Not that bad, actually....

I'll give the code as I got it now:

GdkColor color;
GdkGC *gc;
GdkPixmap *pixmap;
GdkPixbuf *image;
gdouble colors[3];
int w,h,x,y;
GdkFont *font;
guchar *data;
char *text;
guint32 pixel;

//some initializing
[...]

		color.red = (guint16)(options->colors[0]*65535.0);
		color.green = (guint16)(options->colors[1]*65535.0);
		color.blue = (guint16)(options->colors[2]*65535.0);

		font = gdk_font_load(options->font);

		w = gdk_text_width(font, text, strlen(text));
		h = gdk_text_height(font, text, strlen(text));

		pixmap = gdk_pixmap_new(NULL, w, h, 24);

		gc = gdk_gc_new(pixmap);
		gdk_gc_set_foreground(gc, &color);
		gdk_draw_string(pixmap, font, gc, 0, h, text);

		image = gdk_image_get(pixmap, 0,0,w,h);
		data = malloc(sizeof(guchar)*w*h*4);

		for (y=0;y<h;y++)
		{
			for (x=0;x<w;x++)
			{
				pixel = gdk_image_get_pixel(image,x,y);
				data[(y*w+x)*4] = pixel/(256^3);
				data[(y*w+x)*4+1] = (pixel/(256^2))%256;
				data[(y*w+x)*4+2] = (pixel/256)%(256^2);
				data[(y*w+x)*4+3] = 255;
			}
		}

		options->image = gdk_pixbuf_new_from_data(data, GDK_COLORSPACE_RGB,
			1, 8, w, h, w*4, NULL, NULL);

(and here I unref it all)

The outcome pixbuf has an alpha of 255 everywhere because I'm not sure
how to find out whether it should be transparent at that pixel or not.
Second problem is, as I said, that the text of both the background
(which makes sense because it's just a random data background) and the
text changes everytime I create the pixmap. Is there an error in my
"calculate-color-from-data"?

And do you have an idea on how to see transparency? My idea is to read
the background color (just do a gdk_draw_rectangle() with different
colors) and if the background color matches the color in the GdkPixbuf,
we have transparency - but maybe there's a better way?

Anyway, thanks a lot for all the help for now, I'm almost there :-).

Regards,

Ronald

-- 
- .-.
- /V\ | Ronald Bultje <rbultje ronald bitfreak net>
- // \\ | Running: Linux 2.4.4 and OpenBSD 2.8
- /( )\ | http://ronald.bitfreak.net/
- ^^-^^





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