Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/dvi/pixbuf-device.c
blob: fbb059282602e1dc90e2d49d54827132d91eb381 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#include "pixbuf-device.h"
#include <gtk/gtk.h>

typedef struct _DviPixbufDevice
{
    GdkPixbuf *pixbuf;
    
    gboolean valid;
    
    gint xmargin;
    gint ymargin;
    
    Ulong fg;
    Ulong bg;
    
} DviPixbufDevice;

static void dvi_pixbuf_draw_rule(DviContext *dvi, int x, int y, Uint w, Uint h, int fill);

static void dvi_pixbuf_draw_glyph(DviContext *dvi, DviFontChar *ch, int x0, int y0)
{
	DviPixbufDevice *c_device = (DviPixbufDevice *) dvi->device.device_data;
	
	int	x, y, w, h;	
	int	isbox;
	DviGlyph *glyph;
	
	glyph = &ch->grey;

	isbox = (glyph->data == NULL || (dvi->params.flags & MDVI_PARAM_CHARBOXES));
	
	x = - glyph->x + x0 + c_device->xmargin;
	y = - glyph->y + y0 + c_device->ymargin;
	w = glyph->w;
	h = glyph->h;
	
	if (x < 0 || y < 0 
	    || x + w > gdk_pixbuf_get_width (c_device->pixbuf)
	    || y + h > gdk_pixbuf_get_height (c_device->pixbuf))
	    return;
		
	if (isbox) {
		dvi_pixbuf_draw_rule(dvi, x - c_device->xmargin, y - c_device->ymargin, w, h, FALSE);    
	}
	else {
		gdk_pixbuf_copy_area (GDK_PIXBUF (glyph->data),
				      0, 0, 
				      w, h,
				      c_device->pixbuf, x, y);
	}
}

static void dvi_pixbuf_draw_rule(DviContext *dvi, int x, int y, Uint w, Uint h, int fill)
{
	DviPixbufDevice *c_device = (DviPixbufDevice *) dvi->device.device_data;
	gint rowstride;
	gchar *p;
	gint i, j;    
	gint red, green, blue;
	
	red = (c_device->fg >> 16) & 0xff;
	green = (c_device->fg >> 8) & 0xff;
	blue = c_device->fg & 0xff;
	
	x += c_device->xmargin; y += c_device->ymargin;
	
	if (x < 0 || y < 0 
	    || x + w > gdk_pixbuf_get_width (c_device->pixbuf)
	    || y + h > gdk_pixbuf_get_height (c_device->pixbuf))
	    return;
	
	rowstride = gdk_pixbuf_get_rowstride (c_device->pixbuf);
	p = gdk_pixbuf_get_pixels (c_device->pixbuf) + rowstride * y + 4 * x;

	for (i = 0; i < h; i++) {
	    if (i == 0 || i == h - 1 || fill) {
		  for (j = 0; j < w; j++) {
			p[j * 4] = red;
		        p[j * 4 + 1] = green;
			p[j * 4 + 2] = blue;
			p[j * 4 + 3] = 0xff;
		  }
    	    } else {
		p[0] = red;
		p[1] = green;
		p[2] = blue;
		p[3] = 0xff;
		p[(w - 1) * 4] = red;
		p[(w - 1) * 4 + 1] = green;
		p[(w - 1) * 4 + 2] = blue;
		p[(w - 1) * 4 + 3] = 0xff;
    	    }
	    p += rowstride;
      }
}

static int dvi_pixbuf_interpolate_colors(void *device_data,
	Ulong *pixels, int nlevels, Ulong fg, Ulong bg, double g, int density)
{
	double	frac;
	GdkColor color, color_fg, color_bg;
	int	i, n;
	
	color_bg.red = (bg >> 16) & 0xff;
	color_bg.green = (bg >> 8) & 0xff;
	color_bg.blue = bg & 0xff;

	color_fg.red = fg >> 16 & 0xff;
	color_fg.green = fg >> 8 & 0xff;
	color_fg.blue = fg & 0xff;

	n = nlevels - 1;
	for(i = 0; i < nlevels; i++) {
		if(g > 0)
			frac = pow((double)i / n, 1 / g);
		else
			frac = 1 - pow((double)(n - i) / n, -g);
		color.red = frac * ((double)color_fg.red - color_bg.red) + color_bg.red;
		color.green = frac * ((double)color_fg.green - color_bg.green) + color_bg.green;
		color.blue = frac * ((double)color_fg.blue - color_bg.blue) + color_bg.blue;
		
		pixels[i] = (color.red << 16) + (color.green << 8) + color.blue + 0xff000000;
	}

	return nlevels;
}

static void *dvi_pixbuf_create_image(void *device_data, Uint w, Uint h, Uint bpp)
{

    return gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8, w, h);
    
    return NULL;
}

static void dvi_pixbuf_free_image(void *ptr)
{
    g_object_unref (GDK_PIXBUF(ptr)); 
}

static void dvi_pixbuf_put_pixel(void *image, int x, int y, Ulong color)
{
    gchar *p;
    
    p = gdk_pixbuf_get_pixels (GDK_PIXBUF(image)) + y * gdk_pixbuf_get_rowstride(GDK_PIXBUF(image)) + x * 4;

    p[0] = (color >> 16) & 0xff;
    p[1] = (color >> 8) & 0xff;
    p[2] = color & 0xff;
    p[3] = (color >> 24) & 0xff;
}

static void dvi_pixbuf_set_color(void *device_data, Ulong fg, Ulong bg)
{
    DviPixbufDevice *c_device = (DviPixbufDevice *) device_data;
    
    c_device->fg = fg;
        
    return; 
}

void mdvi_pixbuf_device_init (DviDevice *device)
{
    device->device_data = 
	 g_new0 (DviPixbufDevice, 1);
	 
    device->draw_glyph   = dvi_pixbuf_draw_glyph;
    device->draw_rule    = dvi_pixbuf_draw_rule;
    device->alloc_colors = dvi_pixbuf_interpolate_colors;
    device->create_image = dvi_pixbuf_create_image;
    device->free_image   = dvi_pixbuf_free_image;
    device->put_pixel    = dvi_pixbuf_put_pixel;
    device->set_color    = dvi_pixbuf_set_color;
    device->refresh      = NULL; 
    
    return;
}

void mdvi_pixbuf_device_free (DviDevice *device)
{
    DviPixbufDevice *c_device = (DviPixbufDevice *) device->device_data;
    
    if (c_device->pixbuf)
	g_object_unref (c_device->pixbuf);
    
    g_free (c_device);
}

GdkPixbuf * 
mdvi_pixbuf_device_get_pixbuf (DviDevice *device)
{
    DviPixbufDevice *c_device = (DviPixbufDevice *) device->device_data;
    
    return g_object_ref (c_device->pixbuf);
}

void
mdvi_pixbuf_device_render (DviContext * dvi)
{
  DviPixbufDevice *c_device = (DviPixbufDevice *) dvi->device.device_data;
  gint page_width;
  gint page_height;

  if (c_device->pixbuf)
    g_object_unref (c_device->pixbuf);
    
  page_width = dvi->dvi_page_w * dvi->params.conv + 2 * c_device->xmargin;
  page_height = dvi->dvi_page_h * dvi->params.vconv + 2 * c_device->ymargin;
    
  c_device->pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8, page_width, page_height);
  gdk_pixbuf_fill (c_device->pixbuf, 0xffffffff);

  mdvi_dopage (dvi, dvi->currpage);
}


void 
mdvi_pixbuf_device_set_margins (DviDevice *device, gint xmargin, gint ymargin)
{
  DviPixbufDevice *c_device = (DviPixbufDevice *) device->device_data;
    
  c_device->xmargin = xmargin;
  c_device->ymargin = ymargin;
}