]> www.fi.muni.cz Git - evince.git/blob - dvi/pixbuf-device.c
Hungarian translation updated.
[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 + 4 * 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 * 4] = red;
79                         p[j * 4 + 1] = green;
80                         p[j * 4 + 2] = blue;
81                         p[j * 4 + 3] = 0xff;
82                   }
83             } else {
84                 p[0] = red;
85                 p[1] = green;
86                 p[2] = blue;
87                 p[3] = 0xff;
88                 p[(w - 1) * 4] = red;
89                 p[(w - 1) * 4 + 1] = green;
90                 p[(w - 1) * 4 + 2] = blue;
91                 p[(w - 1) * 4 + 3] = 0xff;
92             }
93             p += rowstride;
94       }
95 }
96
97 static int dvi_pixbuf_interpolate_colors(void *device_data,
98         Ulong *pixels, int nlevels, Ulong fg, Ulong bg, double g, int density)
99 {
100         double  frac;
101         GdkColor color, color_fg, color_bg;
102         int     i, n;
103         
104         color_bg.red = (bg >> 16) & 0xff;
105         color_bg.green = (bg >> 8) & 0xff;
106         color_bg.blue = bg & 0xff;
107
108         color_fg.red = fg >> 16 & 0xff;
109         color_fg.green = fg >> 8 & 0xff;
110         color_fg.blue = fg & 0xff;
111
112         n = nlevels - 1;
113         for(i = 0; i < nlevels; i++) {
114                 if(g > 0)
115                         frac = pow((double)i / n, 1 / g);
116                 else
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;
121                 
122                 pixels[i] = (color.red << 16) + (color.green << 8) + color.blue + 0xff000000;
123         }
124
125         return nlevels;
126 }
127
128 static void *dvi_pixbuf_create_image(void *device_data, Uint w, Uint h, Uint bpp)
129 {
130
131     return gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8, w, h);
132     
133     return NULL;
134 }
135
136 static void dvi_pixbuf_free_image(void *ptr)
137 {
138     g_object_unref (GDK_PIXBUF(ptr)); 
139 }
140
141 static void dvi_pixbuf_put_pixel(void *image, int x, int y, Ulong color)
142 {
143     guchar *p;
144     
145     p = gdk_pixbuf_get_pixels (GDK_PIXBUF(image)) + y * gdk_pixbuf_get_rowstride(GDK_PIXBUF(image)) + x * 4;
146
147     p[0] = (color >> 16) & 0xff;
148     p[1] = (color >> 8) & 0xff;
149     p[2] = color & 0xff;
150     p[3] = (color >> 24) & 0xff;
151 }
152
153 static void dvi_pixbuf_set_color(void *device_data, Ulong fg, Ulong bg)
154 {
155     DviPixbufDevice *c_device = (DviPixbufDevice *) device_data;
156     
157     c_device->fg = fg;
158         
159     return; 
160 }
161
162 void mdvi_pixbuf_device_init (DviDevice *device)
163 {
164     device->device_data = 
165          g_new0 (DviPixbufDevice, 1);
166          
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; 
175     
176     return;
177 }
178
179 void mdvi_pixbuf_device_free (DviDevice *device)
180 {
181     DviPixbufDevice *c_device = (DviPixbufDevice *) device->device_data;
182     
183     if (c_device->pixbuf)
184         g_object_unref (c_device->pixbuf);
185     
186     g_free (c_device);
187 }
188
189 GdkPixbuf * 
190 mdvi_pixbuf_device_get_pixbuf (DviDevice *device)
191 {
192     DviPixbufDevice *c_device = (DviPixbufDevice *) device->device_data;
193     
194     return g_object_ref (c_device->pixbuf);
195 }
196
197 void
198 mdvi_pixbuf_device_render (DviContext * dvi)
199 {
200   DviPixbufDevice *c_device = (DviPixbufDevice *) dvi->device.device_data;
201   gint page_width;
202   gint page_height;
203
204   if (c_device->pixbuf)
205     g_object_unref (c_device->pixbuf);
206     
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;
209     
210   c_device->pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8, page_width, page_height);
211   gdk_pixbuf_fill (c_device->pixbuf, 0xffffffff);
212
213   mdvi_dopage (dvi, dvi->currpage);
214 }
215
216
217 void 
218 mdvi_pixbuf_device_set_margins (DviDevice *device, gint xmargin, gint ymargin)
219 {
220   DviPixbufDevice *c_device = (DviPixbufDevice *) device->device_data;
221     
222   c_device->xmargin = xmargin;
223   c_device->ymargin = ymargin;
224 }