diff options
author | Ramiro Estrugo <ramiro@src.gnome.org> | 2000-07-10 22:35:14 +0000 |
---|---|---|
committer | Ramiro Estrugo <ramiro@src.gnome.org> | 2000-07-10 22:35:14 +0000 |
commit | 4fdb98d8e0df471fb3338a3208e56667ebb6e217 (patch) | |
tree | 7cb160d4d8458c53ec0e60565ce7e3b594f8a899 /libnautilus-private/nautilus-gdk-pixbuf-extensions.c | |
parent | 5730af399523c3ec4873ab0a87b4dc8539c6a7d8 (diff) | |
download | nautilus-4fdb98d8e0df471fb3338a3208e56667ebb6e217.tar.gz |
New function to draw text into a pixbuf given a rect and a string.
* libnautilus-extensions/nautilus-gdk-pixbuf-extensions.c:
(nautilus_gdk_pixbuf_scale_to_fit),
(nautilus_gdk_pixbuf_draw_text):
* libnautilus-extensions/nautilus-gdk-pixbuf-extensions.h:
New function to draw text into a pixbuf given a rect and a
string. Factored from the icon factoyr.
* libnautilus-extensions/nautilus-graphic.c:
(nautilus_graphic_size_allocate):
Add support for drawing text.
* libnautilus-extensions/nautilus-icon-factory.c: (embed_text):
Factored out gdk pixbuf text drawing. Use the new text drawing
function.
* test/test-nautilus-graphic.c: (create_color_scale):
Update for text support.
Diffstat (limited to 'libnautilus-private/nautilus-gdk-pixbuf-extensions.c')
-rw-r--r-- | libnautilus-private/nautilus-gdk-pixbuf-extensions.c | 135 |
1 files changed, 134 insertions, 1 deletions
diff --git a/libnautilus-private/nautilus-gdk-pixbuf-extensions.c b/libnautilus-private/nautilus-gdk-pixbuf-extensions.c index e01e861ea..6195245e8 100644 --- a/libnautilus-private/nautilus-gdk-pixbuf-extensions.c +++ b/libnautilus-private/nautilus-gdk-pixbuf-extensions.c @@ -25,11 +25,13 @@ #include <config.h> #include <math.h> #include "nautilus-gdk-pixbuf-extensions.h" +#include "nautilus-gdk-extensions.h" #include "nautilus-glib-extensions.h" +#include "nautilus-string.h" #include <gdk-pixbuf/gdk-pixbuf-loader.h> -#include <libgnomevfs/gnome-vfs-ops.h> #include <libgnomevfs/gnome-vfs-async-ops.h> +#include <libgnomevfs/gnome-vfs-ops.h> #define LOAD_BUFFER_SIZE 4096 @@ -391,3 +393,134 @@ nautilus_gdk_pixbuf_scale_to_fit (GdkPixbuf *pixbuf, int max_width, int max_heig return pixbuf; } + +/** + * nautilus_gdk_pixbuf_draw_text + * @pixbuf: A GdkPixbuf. + * @font: A GdkFont. + * @destination_rect: An ArtIRect - the destination bounding box for the text. + * @text: A string - the text to draw. + * @overall_alpha: The overall alpha to use when compositing the text on the pixbuf + * + * Draw text onto a GdkPixbuf using the given font and rect + **/ +void +nautilus_gdk_pixbuf_draw_text (GdkPixbuf *pixbuf, + const GdkFont *font, + const ArtIRect *destination_rect, + const char *text, + guint overall_alpha) +{ + ArtIRect pixbuf_rect; + ArtIRect text_rect; + int width, height; + GdkVisual *visual; + GdkPixmap *pixmap; + GdkGC *gc; + GdkColormap *colormap; + int y; + const char *line; + const char *end_of_line; + int line_length; + GdkPixbuf *text_pixbuf; + GdkPixbuf *text_pixbuf_with_alpha; + guchar *pixels; + + g_return_if_fail (pixbuf != NULL); + g_return_if_fail (font != NULL); + g_return_if_fail (destination_rect != NULL); + g_return_if_fail (text != NULL); + g_return_if_fail (nautilus_strlen (text) > 0); + g_return_if_fail (overall_alpha <= 255); + + /* Compute the intersection of the text rectangle with the pixbuf. + * This is only to prevent pathological cases from upsetting the + * GdkPixbuf routines. It should not happen in any normal circumstance. + */ + pixbuf_rect.x0 = 0; + pixbuf_rect.y0 = 0; + pixbuf_rect.x1 = gdk_pixbuf_get_width (pixbuf); + pixbuf_rect.y1 = gdk_pixbuf_get_height (pixbuf); + art_irect_intersect (&text_rect, destination_rect, &pixbuf_rect); + + /* Get the system visual. I wish I could do this all in 1-bit mode, + * but I can't. + */ + visual = gdk_visual_get_system (); + + /* Allocate a GdkPixmap of the appropriate size. */ + width = text_rect.x1 - text_rect.x0; + height = text_rect.y1 - text_rect.y0; + pixmap = gdk_pixmap_new (NULL, width, height, visual->depth); + gc = gdk_gc_new (pixmap); + + /* Set up a white background. */ + gdk_rgb_gc_set_foreground (gc, NAUTILUS_RGB_COLOR_WHITE); + gdk_draw_rectangle (pixmap, gc, TRUE, 0, 0, width, height); + + /* Draw black text. */ + gdk_rgb_gc_set_foreground (gc, NAUTILUS_RGB_COLOR_BLACK); + gdk_gc_set_font (gc, (GdkFont *) font); + line = text; + + /* FIXME bugzilla.eazel.com xxxx: + * The iteration code should work with strings that dont have + * new lines. Its broken right now for single line strings. The + * (y + font->ascent) <= height test always fails and no text is drawn. + */ + if (strchr (line, '\n')) { + for (y = font->ascent; + y + font->descent <= height; + y += font->ascent + font->descent) { + + /* Extract the next line of text. */ + end_of_line = strchr (line, '\n'); + line_length = end_of_line == NULL + ? strlen (line) + : end_of_line - line; + + /* Draw the next line of text. */ + gdk_draw_text (pixmap, (GdkFont *) font, gc, 0, y, + line, line_length); + + /* Move on to the subsequent line. */ + line = end_of_line == NULL + ? "" + : end_of_line + 1; + } + } + else { + /* Draw the next line of text. */ + gdk_draw_text (pixmap, (GdkFont *) font, gc, 0, font->ascent, + line, strlen (line)); + } + gdk_gc_unref (gc); + + /* Convert into a GdkPixbuf with gdk_pixbuf_get_from_drawable. */ + colormap = gdk_colormap_new (visual, FALSE); + text_pixbuf = gdk_pixbuf_get_from_drawable (NULL, pixmap, colormap, + 0, 0, + 0, 0, width, height); + gdk_colormap_unref (colormap); + gdk_pixmap_unref (pixmap); + + /* White is not always FF FF FF. So we get the top left corner pixel. */ + pixels = gdk_pixbuf_get_pixels (text_pixbuf); + text_pixbuf_with_alpha = gdk_pixbuf_add_alpha + (text_pixbuf, + TRUE, pixels[0], pixels[1], pixels[2]); + gdk_pixbuf_unref (text_pixbuf); + + gdk_pixbuf_composite (text_pixbuf_with_alpha, + pixbuf, + text_rect.x0, + text_rect.y0, + width, height, + text_rect.x0, + text_rect.y0, + 1, 1, + GDK_INTERP_BILINEAR, + overall_alpha); + + gdk_pixbuf_unref (text_pixbuf_with_alpha); +} |