1 #include "pixbuf-device.h"
4 typedef struct _DviPixbufDevice
14 static void dvi_pixbuf_draw_rule(DviContext *dvi, int x, int y, Uint w, Uint h, int fill);
16 static void dvi_pixbuf_draw_glyph(DviContext *dvi, DviFontChar *ch, int x0, int y0)
18 DviPixbufDevice *c_device = (DviPixbufDevice *) dvi->device.device_data;
26 isbox = (glyph->data == NULL || (dvi->params.flags & MDVI_PARAM_CHARBOXES));
28 x = - glyph->x + x0 + c_device->xmargin;
29 y = - glyph->y + y0 + c_device->ymargin;
34 || x + w > gdk_pixbuf_get_width (c_device->pixbuf)
35 || y + h > gdk_pixbuf_get_height (c_device->pixbuf))
39 dvi_pixbuf_draw_rule(dvi, x - c_device->xmargin, y - c_device->ymargin, w, h, FALSE);
42 gdk_pixbuf_copy_area (GDK_PIXBUF (glyph->data),
45 c_device->pixbuf, x, y);
49 static void dvi_pixbuf_draw_rule(DviContext *dvi, int x, int y, Uint w, Uint h, int fill)
51 DviPixbufDevice *c_device = (DviPixbufDevice *) dvi->device.device_data;
56 x += c_device->xmargin; y += c_device->ymargin;
59 || x + w > gdk_pixbuf_get_width (c_device->pixbuf)
60 || y + h > gdk_pixbuf_get_height (c_device->pixbuf))
63 rowstride = gdk_pixbuf_get_rowstride (c_device->pixbuf);
64 p = gdk_pixbuf_get_pixels (c_device->pixbuf) + rowstride * y + 4 * x;
66 for (i = 0; i < h; i++) {
67 if (i == 0 || i == h - 1 || fill) {
68 for (j = 0; j < w; j++) {
79 p[(w - 1) * 4] = 0x00;
80 p[(w - 1) * 4 + 1] = 0x00;
81 p[(w - 1) * 4 + 2] = 0x00;
82 p[(w - 1) * 4 + 3] = 0xff;
88 static int dvi_pixbuf_interpolate_colors(void *device_data,
89 Ulong *pixels, int nlevels, Ulong fg, Ulong bg, double g, int density)
92 GdkColor color, color_fg, color_bg;
95 color_bg.red = (bg >> 16) & 0xff;
96 color_bg.green = (bg >> 8) & 0xff;
97 color_bg.blue = bg & 0xff;
99 color_fg.red = fg >> 16 & 0xff;
100 color_fg.green = fg >> 8 & 0xff;
101 color_fg.blue = fg & 0xff;
104 for(i = 0; i < nlevels; i++) {
106 frac = pow((double)i / n, 1 / g);
108 frac = 1 - pow((double)(n - i) / n, -g);
109 color.red = frac * ((double)color_fg.red - color_bg.red) + color_bg.red;
110 color.green = frac * ((double)color_fg.green - color_bg.green) + color_bg.green;
111 color.blue = frac * ((double)color_fg.blue - color_bg.blue) + color_bg.blue;
113 pixels[i] = (color.red << 16) + (color.green << 8) + color.blue + 0xff000000;
119 static void *dvi_pixbuf_create_image(void *device_data, Uint w, Uint h, Uint bpp)
122 return gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8, w, h);
127 static void dvi_pixbuf_free_image(void *ptr)
129 g_object_unref (GDK_PIXBUF(ptr));
132 static void dvi_pixbuf_put_pixel(void *image, int x, int y, Ulong color)
136 p = gdk_pixbuf_get_pixels (GDK_PIXBUF(image)) + y * gdk_pixbuf_get_rowstride(GDK_PIXBUF(image)) + x * 4;
138 p[0] = (color >> 16) & 0xff;
139 p[1] = (color >> 8) & 0xff;
141 p[3] = (color >> 24) & 0xff;
144 static void dvi_pixbuf_set_color(void *device_data, Ulong fg, Ulong bg)
150 void mdvi_pixbuf_device_init (DviDevice *device)
152 device->device_data =
153 g_new0 (DviPixbufDevice, 1);
155 device->draw_glyph = dvi_pixbuf_draw_glyph;
156 device->draw_rule = dvi_pixbuf_draw_rule;
157 device->alloc_colors = dvi_pixbuf_interpolate_colors;
158 device->create_image = dvi_pixbuf_create_image;
159 device->free_image = dvi_pixbuf_free_image;
160 device->put_pixel = dvi_pixbuf_put_pixel;
161 device->set_color = dvi_pixbuf_set_color;
162 device->refresh = NULL;
167 void mdvi_pixbuf_device_free (DviDevice *device)
169 DviPixbufDevice *c_device = (DviPixbufDevice *) device->device_data;
171 if (c_device->pixbuf)
172 g_object_unref (c_device->pixbuf);
178 mdvi_pixbuf_device_get_pixbuf (DviDevice *device)
180 DviPixbufDevice *c_device = (DviPixbufDevice *) device->device_data;
182 return g_object_ref (c_device->pixbuf);
186 mdvi_pixbuf_device_render (DviContext * dvi)
188 DviPixbufDevice *c_device = (DviPixbufDevice *) dvi->device.device_data;
192 if (c_device->pixbuf)
193 g_object_unref (c_device->pixbuf);
195 page_width = dvi->dvi_page_w * dvi->params.conv + 2 * c_device->xmargin;
196 page_height = dvi->dvi_page_h * dvi->params.vconv + 2 * c_device->ymargin;
198 c_device->pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8, page_width, page_height);
199 gdk_pixbuf_fill (c_device->pixbuf, 0xffffffff);
201 mdvi_dopage (dvi, dvi->currpage);
206 mdvi_pixbuf_device_set_margins (DviDevice *device, gint xmargin, gint ymargin)
208 DviPixbufDevice *c_device = (DviPixbufDevice *) device->device_data;
210 c_device->xmargin = xmargin;
211 c_device->ymargin = ymargin;