1 #include "pixbuf-device.h"
4 typedef struct _DviPixbufDevice
18 static void dvi_pixbuf_draw_rule(DviContext *dvi, int x, int y, Uint w, Uint h, int fill);
20 static void dvi_pixbuf_draw_glyph(DviContext *dvi, DviFontChar *ch, int x0, int y0)
22 DviPixbufDevice *c_device = (DviPixbufDevice *) dvi->device.device_data;
30 isbox = (glyph->data == NULL || (dvi->params.flags & MDVI_PARAM_CHARBOXES));
32 x = - glyph->x + x0 + c_device->xmargin;
33 y = - glyph->y + y0 + c_device->ymargin;
38 || x + w > gdk_pixbuf_get_width (c_device->pixbuf)
39 || y + h > gdk_pixbuf_get_height (c_device->pixbuf))
43 dvi_pixbuf_draw_rule(dvi, x - c_device->xmargin, y - c_device->ymargin, w, h, FALSE);
46 gdk_pixbuf_copy_area (GDK_PIXBUF (glyph->data),
49 c_device->pixbuf, x, y);
53 static void dvi_pixbuf_draw_rule(DviContext *dvi, int x, int y, Uint w, Uint h, int fill)
55 DviPixbufDevice *c_device = (DviPixbufDevice *) dvi->device.device_data;
59 gint red, green, blue;
61 red = (c_device->fg >> 16) & 0xff;
62 green = (c_device->fg >> 8) & 0xff;
63 blue = c_device->fg & 0xff;
65 x += c_device->xmargin; y += c_device->ymargin;
68 || x + w > gdk_pixbuf_get_width (c_device->pixbuf)
69 || y + h > gdk_pixbuf_get_height (c_device->pixbuf))
72 rowstride = gdk_pixbuf_get_rowstride (c_device->pixbuf);
73 p = gdk_pixbuf_get_pixels (c_device->pixbuf) + rowstride * y + 3 * x;
75 for (i = 0; i < h; i++) {
76 if (i == 0 || i == h - 1 || fill) {
77 for (j = 0; j < w; j++) {
87 p[(w - 1) * 3 + 1] = green;
88 p[(w - 1) * 3 + 2] = blue;
94 static int dvi_pixbuf_interpolate_colors(void *device_data,
95 Ulong *pixels, int nlevels, Ulong fg, Ulong bg, double g, int density)
98 GdkColor color, color_fg, color_bg;
101 color_bg.red = (bg >> 16) & 0xff;
102 color_bg.green = (bg >> 8) & 0xff;
103 color_bg.blue = bg & 0xff;
105 color_fg.red = fg >> 16 & 0xff;
106 color_fg.green = fg >> 8 & 0xff;
107 color_fg.blue = fg & 0xff;
110 for(i = 0; i < nlevels; i++) {
112 frac = pow((double)i / n, 1 / g);
114 frac = 1 - pow((double)(n - i) / n, -g);
115 color.red = frac * ((double)color_fg.red - color_bg.red) + color_bg.red;
116 color.green = frac * ((double)color_fg.green - color_bg.green) + color_bg.green;
117 color.blue = frac * ((double)color_fg.blue - color_bg.blue) + color_bg.blue;
119 pixels[i] = (color.red << 16) + (color.green << 8) + color.blue + 0xff000000;
125 static void *dvi_pixbuf_create_image(void *device_data, Uint w, Uint h, Uint bpp)
128 return gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8, w, h);
133 static void dvi_pixbuf_free_image(void *ptr)
135 g_object_unref (GDK_PIXBUF(ptr));
138 static void dvi_pixbuf_put_pixel(void *image, int x, int y, Ulong color)
142 p = gdk_pixbuf_get_pixels (GDK_PIXBUF(image)) + y * gdk_pixbuf_get_rowstride(GDK_PIXBUF(image)) + x * 3;
144 p[0] = (color >> 16) & 0xff;
145 p[1] = (color >> 8) & 0xff;
149 static void dvi_pixbuf_set_color(void *device_data, Ulong fg, Ulong bg)
151 DviPixbufDevice *c_device = (DviPixbufDevice *) device_data;
158 void mdvi_pixbuf_device_init (DviDevice *device)
160 device->device_data =
161 g_new0 (DviPixbufDevice, 1);
163 device->draw_glyph = dvi_pixbuf_draw_glyph;
164 device->draw_rule = dvi_pixbuf_draw_rule;
165 device->alloc_colors = dvi_pixbuf_interpolate_colors;
166 device->create_image = dvi_pixbuf_create_image;
167 device->free_image = dvi_pixbuf_free_image;
168 device->put_pixel = dvi_pixbuf_put_pixel;
169 device->set_color = dvi_pixbuf_set_color;
170 device->refresh = NULL;
175 void mdvi_pixbuf_device_free (DviDevice *device)
177 DviPixbufDevice *c_device = (DviPixbufDevice *) device->device_data;
179 if (c_device->pixbuf)
180 g_object_unref (c_device->pixbuf);
186 mdvi_pixbuf_device_get_pixbuf (DviDevice *device)
188 DviPixbufDevice *c_device = (DviPixbufDevice *) device->device_data;
190 return g_object_ref (c_device->pixbuf);
194 mdvi_pixbuf_device_render (DviContext * dvi)
196 DviPixbufDevice *c_device = (DviPixbufDevice *) dvi->device.device_data;
200 if (c_device->pixbuf)
201 g_object_unref (c_device->pixbuf);
203 page_width = dvi->dvi_page_w * dvi->params.conv + 2 * c_device->xmargin;
204 page_height = dvi->dvi_page_h * dvi->params.vconv + 2 * c_device->ymargin;
206 c_device->pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8, page_width, page_height);
207 gdk_pixbuf_fill (c_device->pixbuf, 0xffffffff);
209 mdvi_dopage (dvi, dvi->currpage);
214 mdvi_pixbuf_device_set_margins (DviDevice *device, gint xmargin, gint ymargin)
216 DviPixbufDevice *c_device = (DviPixbufDevice *) device->device_data;
218 c_device->xmargin = xmargin;
219 c_device->ymargin = ymargin;