suckfont.c from gzz at Krugle
Show suckfont.c syntax highlighted
/*
* Ripped for ZZXBL by Tjl from:
*
* Text item type for GnomeCanvas widget
*
* GnomeCanvas is basically a port of the Tk toolkit's most excellent canvas
* widget. Tk is copyrighted by the Regents of the University of California,
* Sun Microsystems, and other parties.
*
* Copyright (C) 1998 The Free Software Foundation
*
* Author: Federico Mena <federico@nuclecu.unam.mx>
*/
#include <gdk/gdk.h>
#include <gdk/gdkx.h>
#include <gtk/gtk.h>
#include "suckfont.h"
/* Render handler for the text item */
#if 0
static void
gnome_canvas_text_render (GnomeCanvasItem *item, GnomeCanvasBuf *buf)
{
GnomeCanvasText *text;
guint32 fg_color;
double x_start, y_start;
double xpos, ypos;
struct line *lines;
int i, j;
double affine[6];
GnomeCanvasTextSuckFont *suckfont;
int dx, dy;
ArtPoint start_i, start_c;
text = GNOME_CANVAS_TEXT (item);
if (!text->text || !text->font || !text->suckfont)
return;
suckfont = text->suckfont;
fg_color = text->rgba;
gnome_canvas_buf_ensure_buf (buf);
lines = text->lines;
start_i.y = get_line_ypos_item_relative (text);
art_affine_scale (affine, item->canvas->pixels_per_unit, item->canvas->pixels_per_unit);
for (i = 0; i < 6; i++)
affine[i] = text->affine[i];
for (i = 0; i < text->num_lines; i++) {
if (lines->length != 0) {
start_i.x = get_line_xpos_item_relative (text, lines);
art_affine_point (&start_c, &start_i, text->affine);
xpos = start_c.x;
ypos = start_c.y;
for (j = 0; j < lines->length; j++) {
GnomeCanvasTextSuckChar *ch;
ch = &suckfont->chars[(unsigned char)((lines->text)[j])];
affine[4] = xpos;
affine[5] = ypos;
art_rgb_bitmap_affine (
buf->buf,
buf->rect.x0, buf->rect.y0, buf->rect.x1, buf->rect.y1,
buf->buf_rowstride,
suckfont->bitmap + (ch->bitmap_offset >> 3),
ch->width,
suckfont->bitmap_height,
suckfont->bitmap_width >> 3,
fg_color,
affine,
ART_FILTER_NEAREST, NULL);
dx = ch->left_sb + ch->width + ch->right_sb;
xpos += dx * affine[0];
ypos += dx * affine[1];
}
}
dy = text->font->ascent + text->font->descent;
start_i.y += dy;
lines++;
}
buf->is_bg = 0;
}
#endif
/* Routines for sucking fonts from the X server */
GnomeCanvasTextSuckFont *
gnome_canvas_suck_font (GdkFont *font)
{
GnomeCanvasTextSuckFont *suckfont;
int i;
int x, y;
char text[1];
int lbearing, rbearing, ch_width, ascent, descent;
GdkPixmap *pixmap;
GdkColor black, white;
GdkImage *image;
GdkGC *gc;
guchar *bitmap, *line;
int width, height;
int black_pixel, pixel;
if (!font)
return NULL;
suckfont = g_new (GnomeCanvasTextSuckFont, 1);
height = font->ascent + font->descent;
x = 0;
for (i = 0; i < 256; i++) {
text[0] = i;
gdk_text_extents (font, text, 1,
&lbearing, &rbearing, &ch_width, &ascent, &descent);
suckfont->chars[i].left_sb = lbearing;
suckfont->chars[i].right_sb = ch_width - rbearing;
suckfont->chars[i].width = rbearing - lbearing;
suckfont->chars[i].ascent = ascent;
suckfont->chars[i].descent = descent;
suckfont->chars[i].bitmap_offset = x;
x += (ch_width + 31) & -32;
}
width = x;
suckfont->bitmap_width = width;
suckfont->bitmap_height = height;
suckfont->ascent = font->ascent;
pixmap = gdk_pixmap_new (NULL, suckfont->bitmap_width,
suckfont->bitmap_height, 1);
gc = gdk_gc_new (pixmap);
gdk_gc_set_font (gc, font);
black_pixel = BlackPixel (gdk_display, DefaultScreen (gdk_display));
black.pixel = black_pixel;
white.pixel = WhitePixel (gdk_display, DefaultScreen (gdk_display));
gdk_gc_set_foreground (gc, &white);
gdk_draw_rectangle (pixmap, gc, 1, 0, 0, width, height);
gdk_gc_set_foreground (gc, &black);
for (i = 0; i < 256; i++) {
text[0] = i;
gdk_draw_text (pixmap, font, gc,
suckfont->chars[i].bitmap_offset - suckfont->chars[i].left_sb,
font->ascent,
text, 1);
}
/* The handling of the image leaves me with distinct unease. But this
* is more or less copied out of gimp/app/text_tool.c, so it _ought_ to
* work. -RLL
*/
image = gdk_image_get (pixmap, 0, 0, width, height);
suckfont->bitmap = (guchar *)g_malloc0 ((width >> 3) * height);
// printf("Wid: %d hei: %d\n", width, height);
line = suckfont->bitmap;
for (y = 0; y < height; y++) {
for (x = 0; x < width; x++) {
pixel = gdk_image_get_pixel (image, x, y);
if (pixel == black_pixel)
line[x >> 3] |= 128 >> (x & 7);
}
line += width >> 3;
}
gdk_image_destroy (image);
/* free the pixmap */
gdk_pixmap_unref (pixmap);
/* free the gc */
gdk_gc_destroy (gc);
return suckfont;
}
void
gnome_canvas_suck_font_free (GnomeCanvasTextSuckFont *suckfont)
{
g_free (suckfont->bitmap);
g_free (suckfont);
}
See more files for this project here