summaryrefslogtreecommitdiff
path: root/test/gtk-test.cc
blob: a5759d7fa316b9be6701ed7256ddcd18f5c29c07 (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
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
#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 <poppler.h>
#include <poppler-private.h>
#include <gtk/gtk.h>
#include <math.h>

static int page = 0;
static gboolean cairo_output = FALSE;
static gboolean splash_output = FALSE;
static const char **file_arguments = NULL;
static const GOptionEntry options[] = {
  { "cairo", 'c', 0, G_OPTION_ARG_NONE, &cairo_output, "Cairo Output Device", NULL},
  { "splash", 's', 0, G_OPTION_ARG_NONE, &splash_output, "Splash Output Device", NULL},
  { "page", 'p', 0, G_OPTION_ARG_INT, &page, "Page number", "PAGE" },
  { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &file_arguments, NULL, "PDF-FILES…" },
  { NULL }
};

static GList *view_list = NULL;

//------------------------------------------------------------------------

#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,
              cairo_t *cr,
	      int destX, int destY,
	      int width, int height);

private:

  int incrementalUpdate;
  void (*redrawCbk)(void *data);
  void *redrawCbkData;
};

typedef struct
{
  PopplerDocument    *doc;
  GtkWidget          *drawing_area;
  GtkWidget          *spin_button;
  cairo_surface_t    *surface;
  GDKSplashOutputDev *out;
} View;

//------------------------------------------------------------------------
// 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,
                                cairo_t *cr,
                                int destX, int destY,
                                int width, int height) {
  GdkPixbuf *pixbuf;
  int gdk_rowstride;

  gdk_rowstride = getBitmap()->getRowSize();
  pixbuf = gdk_pixbuf_new_from_data (getBitmap()->getDataPtr() + srcY * gdk_rowstride + srcX * 3,
                                     GDK_COLORSPACE_RGB, FALSE, 8,
                                     width, height, gdk_rowstride,
                                     NULL, NULL);

  gdk_cairo_set_source_pixbuf (cr, pixbuf, 0, 0);
  cairo_paint (cr);

  g_object_unref (pixbuf);
}

static gboolean
drawing_area_draw (GtkWidget *drawing_area,
                   cairo_t   *cr,
                   View      *view)
{
  GdkRectangle document;
  GdkRectangle clip;
  GdkRectangle draw;

  document.x = 0;
  document.y = 0;
  if (cairo_output) {
    document.width = cairo_image_surface_get_width (view->surface);
    document.height = cairo_image_surface_get_height (view->surface);
  } else {
    document.width = view->out->getBitmapWidth();
    document.height = view->out->getBitmapHeight();
  }

  if (!gdk_cairo_get_clip_rectangle (cr, &clip))
    return FALSE;

  if (!gdk_rectangle_intersect (&document, &clip, &draw))
    return FALSE;

  if (cairo_output) {
    cairo_set_source_surface (cr, view->surface, 0, 0);
    cairo_paint (cr);
  } else {
    view->out->redraw (draw.x, draw.y,
                       cr,
                       draw.x, draw.y,
                       draw.width, draw.height);
  }

  return TRUE;
}

static void
view_set_page (View *view, int page)
{
  int w, h;

  if (cairo_output) {
    cairo_t     *cr;
    double       width, height;
    PopplerPage *poppler_page;

    poppler_page = poppler_document_get_page (view->doc, page);
    poppler_page_get_size (poppler_page, &width, &height);
    w = (int) ceil(width);
    h = (int) ceil(height);

    if (view->surface)
      cairo_surface_destroy (view->surface);
    view->surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, w, h);

    cr = cairo_create (view->surface);
    poppler_page_render (poppler_page, cr);

    cairo_set_operator (cr, CAIRO_OPERATOR_DEST_OVER);
    cairo_set_source_rgb (cr, 1., 1., 1.);
    cairo_paint (cr);

    cairo_destroy (cr);
    g_object_unref (poppler_page);
  } else {
    view->doc->doc->displayPage (view->out, page + 1, 72, 72, 0, gFalse, gTrue, gTrue);
    w = view->out->getBitmapWidth();
    h = view->out->getBitmapHeight();
  }

  gtk_widget_set_size_request (view->drawing_area, w, h);
  gtk_widget_queue_draw (view->drawing_area);
  gtk_spin_button_set_value (GTK_SPIN_BUTTON (view->spin_button), page);
}

static void
redraw_callback (void *data)
{
  View *view = (View*) data;

  gtk_widget_queue_draw (view->drawing_area);
}

static void
view_free (View *view)
{
  if (G_UNLIKELY (!view))
    return;

  g_object_unref (view->doc);
  delete view->out;
  cairo_surface_destroy (view->surface);
  g_slice_free (View, view);
}

static void
destroy_window_callback (GtkWindow *window, View *view)
{
  view_list = g_list_remove (view_list, view);
  view_free (view);

  if (!view_list)
    gtk_main_quit ();
}

static void
page_changed_callback (GtkSpinButton *button, View *view)
{
  int page;

  page = gtk_spin_button_get_value_as_int (button);
  view_set_page (view, page);
}

static View *
view_new (PopplerDocument *doc)
{
  View *view;
  GtkWidget *window;
  GtkWidget *sw;
  GtkWidget *vbox, *hbox;
  guint n_pages;
  PopplerPage *page;

  view = g_slice_new0 (View);

  view->doc = doc;

  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  g_signal_connect (window, "destroy",
                    G_CALLBACK (destroy_window_callback),
                    view);

  page = poppler_document_get_page (doc, 0);
  if (page) {
    double width, height;

    poppler_page_get_size (page, &width, &height);
    gtk_window_set_default_size (GTK_WINDOW (window), (gint)width, (gint)height);
    g_object_unref (page);
  }

  vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 5);

  view->drawing_area = gtk_drawing_area_new ();
  sw = gtk_scrolled_window_new (NULL, NULL);
  gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
                                  GTK_POLICY_AUTOMATIC,
                                  GTK_POLICY_AUTOMATIC);
  gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW (sw),
                                         view->drawing_area);
  gtk_widget_show (view->drawing_area);

  gtk_box_pack_end (GTK_BOX (vbox), sw, TRUE, TRUE, 0);
  gtk_widget_show (sw);

  hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 5);

  n_pages = poppler_document_get_n_pages (doc);
  view->spin_button = gtk_spin_button_new_with_range  (0, n_pages - 1, 1);
  g_signal_connect (view->spin_button, "value-changed",
                    G_CALLBACK (page_changed_callback), view);

  gtk_box_pack_end (GTK_BOX (hbox), view->spin_button, FALSE, TRUE, 0);
  gtk_widget_show (view->spin_button);

  gtk_box_pack_end (GTK_BOX (vbox), hbox, FALSE, TRUE, 0);
  gtk_widget_show (hbox);

  gtk_container_add (GTK_CONTAINER (window), vbox);
  gtk_widget_show (vbox);

  gtk_widget_show (window);


  if (!cairo_output) {
    SplashColor sc = { 255, 255, 255};

    view->out = new GDKSplashOutputDev (gtk_widget_get_screen (window),
                                        redraw_callback, (void*) view, sc);
    view->out->startDoc(view->doc->doc);
  }

  g_signal_connect (view->drawing_area,
                    "draw",
                    G_CALLBACK (drawing_area_draw),
                    view);

  return view;
}

int
main (int argc, char *argv [])
{
  GOptionContext *ctx;

  if (argc == 1) {
    char *basename = g_path_get_basename (argv[0]);

    g_printerr ("usage: %s PDF-FILES…\n", basename);
    g_free (basename);

    return -1;
  }

  ctx = g_option_context_new (NULL);
  g_option_context_add_main_entries (ctx, options, "main");
  g_option_context_parse (ctx, &argc, &argv, NULL);
  g_option_context_free (ctx);

  gtk_init (&argc, &argv);

  globalParams = new GlobalParams();

  for (int i = 0; file_arguments[i]; i++) {
    View            *view;
    GFile           *file;
    PopplerDocument *doc;
    GError          *error = NULL;

    file = g_file_new_for_commandline_arg (file_arguments[i]);
    doc = poppler_document_new_from_gfile (file, NULL, NULL, &error);
    if (!doc) {
      gchar *uri;

      uri = g_file_get_uri (file);
      g_printerr ("Error opening document %s: %s\n", uri, error->message);
      g_error_free (error);
      g_free (uri);
      g_object_unref (file);

      continue;
    }
    g_object_unref (file);

    view = view_new (doc);
    view_list = g_list_prepend (view_list, view);
    view_set_page (view, CLAMP (page, 0, poppler_document_get_n_pages (doc) - 1));
  }

  gtk_main ();

  delete globalParams;

  return 0;
}