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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
|
//========================================================================
//
// GDKSplashOutputDev.cc
//
// Copyright 2003 Glyph & Cog, LLC
// Copyright 2004 Red Hat, Inc. (GDK port)
//
//========================================================================
#include <config.h>
#ifdef USE_GCC_PRAGMAS
#pragma implementation
#endif
#include <goo/gmem.h>
#include <splash/SplashTypes.h>
#include <splash/SplashBitmap.h>
#include "Object.h"
#include "SplashOutputDev.h"
#include "GfxState.h"
#include <gdk/gdk.h>
#include "PDFDoc.h"
#include "GlobalParams.h"
#include "ErrorCodes.h"
#include <gtk/gtk.h>
//------------------------------------------------------------------------
#define xOutMaxRGBCube 6 // max size of RGB color cube
//------------------------------------------------------------------------
// GDKSplashOutputDev
//------------------------------------------------------------------------
class GDKSplashOutputDev: public SplashOutputDev {
public:
GDKSplashOutputDev(GdkScreen *screen,
void (*redrawCbkA)(void *data),
void *redrawCbkDataA, SplashColor sc);
virtual ~GDKSplashOutputDev();
//----- initialization and control
// End a page.
virtual void endPage();
// Dump page contents to display.
virtual void dump();
//----- update text state
virtual void updateFont(GfxState *state);
//----- special access
// Clear out the document (used when displaying an empty window).
void clear();
// Copy the rectangle (srcX, srcY, width, height) to (destX, destY)
// in destDC.
void redraw(int srcX, int srcY,
GdkDrawable *drawable,
int destX, int destY,
int width, int height);
private:
int incrementalUpdate;
void (*redrawCbk)(void *data);
void *redrawCbkData;
};
//------------------------------------------------------------------------
// Constants and macros
//------------------------------------------------------------------------
#define xoutRound(x) ((int)(x + 0.5))
//------------------------------------------------------------------------
// GDKSplashOutputDev
//------------------------------------------------------------------------
GDKSplashOutputDev::GDKSplashOutputDev(GdkScreen *screen,
void (*redrawCbkA)(void *data),
void *redrawCbkDataA, SplashColor sc):
SplashOutputDev(splashModeRGB8, 4, gFalse, sc),
incrementalUpdate (1)
{
redrawCbk = redrawCbkA;
redrawCbkData = redrawCbkDataA;
}
GDKSplashOutputDev::~GDKSplashOutputDev() {
}
void GDKSplashOutputDev::clear() {
startDoc(NULL);
startPage(0, NULL);
}
void GDKSplashOutputDev::endPage() {
SplashOutputDev::endPage();
if (!incrementalUpdate) {
(*redrawCbk)(redrawCbkData);
}
}
void GDKSplashOutputDev::dump() {
if (incrementalUpdate && redrawCbk) {
(*redrawCbk)(redrawCbkData);
}
}
void GDKSplashOutputDev::updateFont(GfxState *state) {
SplashOutputDev::updateFont(state);
}
void GDKSplashOutputDev::redraw(int srcX, int srcY,
GdkDrawable *drawable,
int destX, int destY,
int width, int height) {
GdkGC *gc;
int gdk_rowstride;
gdk_rowstride = getBitmap()->getRowSize();
gc = gdk_gc_new (drawable);
gdk_draw_rgb_image (drawable, gc,
destX, destY,
width, height,
GDK_RGB_DITHER_NORMAL,
getBitmap()->getDataPtr() + srcY * gdk_rowstride + srcX * 3,
gdk_rowstride);
g_object_unref (gc);
}
typedef struct
{
GtkWidget *window;
GtkWidget *sw;
GtkWidget *drawing_area;
GDKSplashOutputDev *out;
PDFDoc *doc;
} View;
static void
drawing_area_expose (GtkWidget *drawing_area,
GdkEventExpose *event,
void *data)
{
View *v = (View*) data;
GdkRectangle document;
GdkRectangle draw;
gdk_window_clear (drawing_area->window);
document.x = 0;
document.y = 0;
document.width = v->out->getBitmapWidth();
document.height = v->out->getBitmapHeight();
if (gdk_rectangle_intersect (&document, &event->area, &draw))
{
v->out->redraw (draw.x, draw.y,
drawing_area->window,
draw.x, draw.y,
draw.width, draw.height);
}
}
static int
view_load (View *v,
const char *filename)
{
PDFDoc *newDoc;
int err;
GooString *filename_g;
int w, h;
filename_g = new GooString (filename);
// open the PDF file
newDoc = new PDFDoc(filename_g, 0, 0);
delete filename_g;
if (!newDoc->isOk())
{
err = newDoc->getErrorCode();
delete newDoc;
return err;
}
if (v->doc)
delete v->doc;
v->doc = newDoc;
v->out->startDoc(v->doc->getXRef());
v->doc->displayPage (v->out, 1, 72, 72, 0, gFalse, gTrue, gTrue);
w = v->out->getBitmapWidth();
h = v->out->getBitmapHeight();
gtk_widget_set_size_request (v->drawing_area, w, h);
return errNone;
}
static void
view_show (View *v)
{
gtk_widget_show (v->window);
}
static void
redraw_callback (void *data)
{
View *v = (View*) data;
gtk_widget_queue_draw (v->drawing_area);
}
static View*
view_new (void)
{
View *v;
GtkWidget *window;
GtkWidget *drawing_area;
GtkWidget *sw;
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
drawing_area = gtk_drawing_area_new ();
sw = gtk_scrolled_window_new (NULL, NULL);
gtk_container_add (GTK_CONTAINER (window), sw);
gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW (sw), drawing_area);
gtk_widget_show_all (sw);
v = g_new0 (View, 1);
v->window = window;
v->drawing_area = drawing_area;
v->sw = sw;
SplashColor sc;
sc[0] = 255;
sc[1] = 255;
sc[2] = 255;
v->out = new GDKSplashOutputDev (gtk_widget_get_screen (window),
redraw_callback, (void*) v, sc);
v->doc = 0;
g_signal_connect (drawing_area,
"expose_event",
G_CALLBACK (drawing_area_expose),
(void*) v);
return v;
}
int
main (int argc, char *argv [])
{
View *v;
int i;
gtk_init (&argc, &argv);
globalParams = new GlobalParams();
if (argc == 1)
{
fprintf (stderr, "usage: %s PDF-FILES...\n", argv[0]);
return -1;
}
i = 1;
while (i < argc)
{
int err;
v = view_new ();
err = view_load (v, argv[i]);
if (err != errNone)
g_printerr ("Error loading document!\n");
view_show (v);
++i;
}
gtk_main ();
delete globalParams;
return 0;
}
|