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