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 + 4 * x;
75 for (i = 0; i < h; i++) {
76 if (i == 0 || i == h - 1 || fill) {
77 for (j = 0; j < w; j++) {
89 p[(w - 1) * 4 + 1] = green;
90 p[(w - 1) * 4 + 2] = blue;
91 p[(w - 1) * 4 + 3] = 0xff;
97 static int dvi_pixbuf_interpolate_colors(void *device_data,
98 Ulong *pixels, int nlevels, Ulong fg, Ulong bg, double g, int density)
101 GdkColor color, color_fg, color_bg;
104 color_bg.red = (bg >> 16) & 0xff;
105 color_bg.green = (bg >> 8) & 0xff;
106 color_bg.blue = bg & 0xff;
108 color_fg.red = fg >> 16 & 0xff;
109 color_fg.green = fg >> 8 & 0xff;
110 color_fg.blue = fg & 0xff;
113 for(i = 0; i < nlevels; i++) {
115 frac = pow((double)i / n, 1 / g);
117 frac = 1 - pow((double)(n - i) / n, -g);
118 color.red = frac * ((double)color_fg.red - color_bg.red) + color_bg.red;
119 color.green = frac * ((double)color_fg.green - color_bg.green) + color_bg.green;
120 color.blue = frac * ((double)color_fg.blue - color_bg.blue) + color_bg.blue;
122 pixels[i] = (color.red << 16) + (color.green << 8) + color.blue + 0xff000000;
128 static void *dvi_pixbuf_create_image(void *device_data, Uint w, Uint h, Uint bpp)
131 return gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8, w, h);
136 static void dvi_pixbuf_free_image(void *ptr)
138 g_object_unref (GDK_PIXBUF(ptr));
141 static void dvi_pixbuf_put_pixel(void *image, int x, int y, Ulong color)
145 p = gdk_pixbuf_get_pixels (GDK_PIXBUF(image)) + y * gdk_pixbuf_get_rowstride(GDK_PIXBUF(image)) + x * 4;
147 p[0] = (color >> 16) & 0xff;
148 p[1] = (color >> 8) & 0xff;
150 p[3] = (color >> 24) & 0xff;
153 static void dvi_pixbuf_set_color(void *device_data, Ulong fg, Ulong bg)
155 DviPixbufDevice *c_device = (DviPixbufDevice *) device_data;
162 void mdvi_pixbuf_device_init (DviDevice *device)
164 device->device_data =
165 g_new0 (DviPixbufDevice, 1);
167 device->draw_glyph = dvi_pixbuf_draw_glyph;
168 device->draw_rule = dvi_pixbuf_draw_rule;
169 device->alloc_colors = dvi_pixbuf_interpolate_colors;
170 device->create_image = dvi_pixbuf_create_image;
171 device->free_image = dvi_pixbuf_free_image;
172 device->put_pixel = dvi_pixbuf_put_pixel;
173 device->set_color = dvi_pixbuf_set_color;
174 device->refresh = NULL;
179 void mdvi_pixbuf_device_free (DviDevice *device)
181 DviPixbufDevice *c_device = (DviPixbufDevice *) device->device_data;
183 if (c_device->pixbuf)
184 g_object_unref (c_device->pixbuf);
190 mdvi_pixbuf_device_get_pixbuf (DviDevice *device)
192 DviPixbufDevice *c_device = (DviPixbufDevice *) device->device_data;
194 return g_object_ref (c_device->pixbuf);
198 mdvi_pixbuf_device_render (DviContext * dvi)
200 DviPixbufDevice *c_device = (DviPixbufDevice *) dvi->device.device_data;
204 if (c_device->pixbuf)
205 g_object_unref (c_device->pixbuf);
207 page_width = dvi->dvi_page_w * dvi->params.conv + 2 * c_device->xmargin;
208 page_height = dvi->dvi_page_h * dvi->params.vconv + 2 * c_device->ymargin;
210 c_device->pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8, page_width, page_height);
211 gdk_pixbuf_fill (c_device->pixbuf, 0xffffffff);
213 mdvi_dopage (dvi, dvi->currpage);
218 mdvi_pixbuf_device_set_margins (DviDevice *device, gint xmargin, gint ymargin)
220 DviPixbufDevice *c_device = (DviPixbufDevice *) device->device_data;
222 c_device->xmargin = xmargin;
223 c_device->ymargin = ymargin;