]> www.fi.muni.cz Git - evince.git/blob - dvi/pixbuf-device.c
1ef436587812c3728d005dff8445f98235d78642
[evince.git] / dvi / pixbuf-device.c
1 #include "pixbuf-device.h"
2 #include <gtk/gtk.h>
3
4 typedef struct _DviPixbufDevice
5 {
6     GdkPixbuf *pixbuf;
7     
8     gboolean valid;
9     
10     gint xmargin;
11     gint ymargin;
12     
13     Ulong fg;
14     Ulong bg;
15     
16 } DviPixbufDevice;
17
18 static void dvi_pixbuf_draw_rule(DviContext *dvi, int x, int y, Uint w, Uint h, int fill);
19
20 static void dvi_pixbuf_draw_glyph(DviContext *dvi, DviFontChar *ch, int x0, int y0)
21 {
22         DviPixbufDevice *c_device = (DviPixbufDevice *) dvi->device.device_data;
23         
24         int     x, y, w, h;     
25         int     isbox;
26         DviGlyph *glyph;
27         
28         glyph = &ch->grey;
29
30         isbox = (glyph->data == NULL || (dvi->params.flags & MDVI_PARAM_CHARBOXES));
31         
32         x = - glyph->x + x0 + c_device->xmargin;
33         y = - glyph->y + y0 + c_device->ymargin;
34         w = glyph->w;
35         h = glyph->h;
36         
37         if (x < 0 || y < 0 
38             || x + w > gdk_pixbuf_get_width (c_device->pixbuf)
39             || y + h > gdk_pixbuf_get_height (c_device->pixbuf))
40             return;
41                 
42         if (isbox) {
43                 dvi_pixbuf_draw_rule(dvi, x - c_device->xmargin, y - c_device->ymargin, w, h, FALSE);    
44         }
45         else {
46                 gdk_pixbuf_copy_area (GDK_PIXBUF (glyph->data),
47                                       0, 0, 
48                                       w, h,
49                                       c_device->pixbuf, x, y);
50         }
51 }
52
53 static void dvi_pixbuf_draw_rule(DviContext *dvi, int x, int y, Uint w, Uint h, int fill)
54 {
55         DviPixbufDevice *c_device = (DviPixbufDevice *) dvi->device.device_data;
56         gint rowstride;
57         guchar *p;
58         gint i, j;    
59         gint red, green, blue;
60         
61         red = (c_device->fg >> 16) & 0xff;
62         green = (c_device->fg >> 8) & 0xff;
63         blue = c_device->fg & 0xff;
64         
65         x += c_device->xmargin; y += c_device->ymargin;
66         
67         if (x < 0 || y < 0 
68             || x + w > gdk_pixbuf_get_width (c_device->pixbuf)
69             || y + h > gdk_pixbuf_get_height (c_device->pixbuf))
70             return;
71         
72         rowstride = gdk_pixbuf_get_rowstride (c_device->pixbuf);
73         p = gdk_pixbuf_get_pixels (c_device->pixbuf) + rowstride * y + 3 * x;
74
75         for (i = 0; i < h; i++) {
76             if (i == 0 || i == h - 1 || fill) {
77                   for (j = 0; j < w; j++) {
78                         p[j * 3] = red;
79                         p[j * 3 + 1] = green;
80                         p[j * 3 + 2] = blue;
81                   }
82             } else {
83                 p[0] = red;
84                 p[1] = green;
85                 p[2] = blue;
86                 p[(w - 1) * 3] = red;
87                 p[(w - 1) * 3 + 1] = green;
88                 p[(w - 1) * 3 + 2] = blue;
89             }
90             p += rowstride;
91       }
92 }
93
94 static int dvi_pixbuf_interpolate_colors(void *device_data,
95         Ulong *pixels, int nlevels, Ulong fg, Ulong bg, double g, int density)
96 {
97         double  frac;
98         GdkColor color, color_fg, color_bg;
99         int     i, n;
100         
101         color_bg.red = (bg >> 16) & 0xff;
102         color_bg.green = (bg >> 8) & 0xff;
103         color_bg.blue = bg & 0xff;
104
105         color_fg.red = fg >> 16 & 0xff;
106         color_fg.green = fg >> 8 & 0xff;
107         color_fg.blue = fg & 0xff;
108
109         n = nlevels - 1;
110         for(i = 0; i < nlevels; i++) {
111                 if(g > 0)
112                         frac = pow((double)i / n, 1 / g);
113                 else
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;
118                 
119                 pixels[i] = (color.red << 16) + (color.green << 8) + color.blue + 0xff000000;
120         }
121
122         return nlevels;
123 }
124
125 static void *dvi_pixbuf_create_image(void *device_data, Uint w, Uint h, Uint bpp)
126 {
127
128     return gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8, w, h);
129     
130     return NULL;
131 }
132
133 static void dvi_pixbuf_free_image(void *ptr)
134 {
135     g_object_unref (GDK_PIXBUF(ptr)); 
136 }
137
138 static void dvi_pixbuf_put_pixel(void *image, int x, int y, Ulong color)
139 {
140     guchar *p;
141     
142     p = gdk_pixbuf_get_pixels (GDK_PIXBUF(image)) + y * gdk_pixbuf_get_rowstride(GDK_PIXBUF(image)) + x * 3;
143
144     p[0] = (color >> 16) & 0xff;
145     p[1] = (color >> 8) & 0xff;
146     p[2] = color & 0xff;
147 }
148
149 static void dvi_pixbuf_set_color(void *device_data, Ulong fg, Ulong bg)
150 {
151     DviPixbufDevice *c_device = (DviPixbufDevice *) device_data;
152     
153     c_device->fg = fg;
154         
155     return; 
156 }
157
158 void mdvi_pixbuf_device_init (DviDevice *device)
159 {
160     device->device_data = 
161          g_new0 (DviPixbufDevice, 1);
162          
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; 
171     
172     return;
173 }
174
175 void mdvi_pixbuf_device_free (DviDevice *device)
176 {
177     DviPixbufDevice *c_device = (DviPixbufDevice *) device->device_data;
178     
179     if (c_device->pixbuf)
180         g_object_unref (c_device->pixbuf);
181     
182     g_free (c_device);
183 }
184
185 GdkPixbuf * 
186 mdvi_pixbuf_device_get_pixbuf (DviDevice *device)
187 {
188     DviPixbufDevice *c_device = (DviPixbufDevice *) device->device_data;
189     
190     return g_object_ref (c_device->pixbuf);
191 }
192
193 void
194 mdvi_pixbuf_device_render (DviContext * dvi)
195 {
196   DviPixbufDevice *c_device = (DviPixbufDevice *) dvi->device.device_data;
197   gint page_width;
198   gint page_height;
199
200   if (c_device->pixbuf)
201     g_object_unref (c_device->pixbuf);
202     
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;
205     
206   c_device->pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8, page_width, page_height);
207   gdk_pixbuf_fill (c_device->pixbuf, 0xffffffff);
208
209   mdvi_dopage (dvi, dvi->currpage);
210 }
211
212
213 void 
214 mdvi_pixbuf_device_set_margins (DviDevice *device, gint xmargin, gint ymargin)
215 {
216   DviPixbufDevice *c_device = (DviPixbufDevice *) device->device_data;
217     
218   c_device->xmargin = xmargin;
219   c_device->ymargin = ymargin;
220 }