forked from vain/pdfPres
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopplergdk.c
More file actions
153 lines (128 loc) · 4.17 KB
/
popplergdk.c
File metadata and controls
153 lines (128 loc) · 4.17 KB
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
/*
Copyright 2011 Peter Hofmann
Copyright (C) 2005, Red Hat, Inc.
This file is part of pdfpres.
pdfpres is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
pdfpres is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with pdfpres. If not, see <http://www.gnu.org/licenses/>.
*/
/* This is a temporary workaround. GDK-API has been removed from poppler
* in poppler 0.17 (commit 149b7fe) but pdfpres relies on that API. So
* for now, I just copied the old poppler code. Of course, this
* workaround is likely to break.
*
* Unfortunately, I'm running out of free time. If you'd like to help me
* improve pdfpres, please send me patches and/or fork the project on
* GitHub. I'd very much appreciate it! :-)
*/
#include <glib/poppler.h>
#if POPPLER_MINOR_VERSION > 16
#include <gdk/gdk.h>
#include <glib.h>
#include <stdbool.h>
#include "popplergdk.h"
static void
copy_cairo_surface_to_pixbuf (cairo_surface_t *surface,
GdkPixbuf *pixbuf)
{
int cairo_width, cairo_height, cairo_rowstride;
unsigned char *pixbuf_data, *dst, *cairo_data;
int pixbuf_rowstride, pixbuf_n_channels;
unsigned int *src;
int x, y;
cairo_width = cairo_image_surface_get_width (surface);
cairo_height = cairo_image_surface_get_height (surface);
cairo_rowstride = cairo_image_surface_get_stride (surface);
cairo_data = cairo_image_surface_get_data (surface);
pixbuf_data = gdk_pixbuf_get_pixels (pixbuf);
pixbuf_rowstride = gdk_pixbuf_get_rowstride (pixbuf);
pixbuf_n_channels = gdk_pixbuf_get_n_channels (pixbuf);
if (cairo_width > gdk_pixbuf_get_width (pixbuf))
cairo_width = gdk_pixbuf_get_width (pixbuf);
if (cairo_height > gdk_pixbuf_get_height (pixbuf))
cairo_height = gdk_pixbuf_get_height (pixbuf);
for (y = 0; y < cairo_height; y++)
{
src = (unsigned int *) (cairo_data + y * cairo_rowstride);
dst = pixbuf_data + y * pixbuf_rowstride;
for (x = 0; x < cairo_width; x++)
{
dst[0] = (*src >> 16) & 0xff;
dst[1] = (*src >> 8) & 0xff;
dst[2] = (*src >> 0) & 0xff;
if (pixbuf_n_channels == 4)
dst[3] = (*src >> 24) & 0xff;
dst += pixbuf_n_channels;
src++;
}
}
}
static void
_poppler_page_render_to_pixbuf (PopplerPage *page,
int src_x, int src_y,
int src_width, int src_height,
double scale,
int rotation,
bool printing,
GdkPixbuf *pixbuf)
{
cairo_t *cr;
cairo_surface_t *surface;
surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
src_width, src_height);
cr = cairo_create (surface);
cairo_save (cr);
switch (rotation) {
case 90:
cairo_translate (cr, src_x + src_width, -src_y);
break;
case 180:
cairo_translate (cr, src_x + src_width, src_y + src_height);
break;
case 270:
cairo_translate (cr, -src_x, src_y + src_height);
break;
default:
cairo_translate (cr, -src_x, -src_y);
}
if (scale != 1.0)
cairo_scale (cr, scale, scale);
if (rotation != 0)
cairo_rotate (cr, rotation * G_PI / 180.0);
if (printing)
poppler_page_render_for_printing (page, cr);
else
poppler_page_render (page, cr);
cairo_restore (cr);
cairo_set_operator (cr, CAIRO_OPERATOR_DEST_OVER);
cairo_set_source_rgb (cr, 1., 1., 1.);
cairo_paint (cr);
cairo_destroy (cr);
copy_cairo_surface_to_pixbuf (surface, pixbuf);
cairo_surface_destroy (surface);
}
void
poppler_page_render_to_pixbuf (PopplerPage *page,
int src_x, int src_y,
int src_width, int src_height,
double scale,
int rotation,
GdkPixbuf *pixbuf)
{
g_return_if_fail (POPPLER_IS_PAGE (page));
g_return_if_fail (scale > 0.0);
g_return_if_fail (pixbuf != NULL);
_poppler_page_render_to_pixbuf (page, src_x, src_y,
src_width, src_height,
scale, rotation,
false,
pixbuf);
}
#endif /* POPPLER_MINOR_VERSION */