[poppler] test/CMakeLists.txt test/gtk-cairo-test.cc test/gtk-splash-test.cc test/gtk-test.cc test/Makefile.am
Carlos Garcia Campos
carlosgc at kemper.freedesktop.org
Sun Feb 13 07:25:03 PST 2011
test/CMakeLists.txt | 20 --
test/Makefile.am | 29 +--
test/gtk-cairo-test.cc | 190 ----------------------
test/gtk-splash-test.cc | 311 -------------------------------------
test/gtk-test.cc | 397 ++++++++++++++++++++++++++++++++++++++++++++++++
5 files changed, 412 insertions(+), 535 deletions(-)
New commits:
commit 675b3505f963a0110dac7f865654232cbb865ef5
Author: Carlos Garcia Campos <carlosgc at gnome.org>
Date: Sun Feb 13 16:21:52 2011 +0100
tests: Merge splash and cairo tests into a single gtk-test tool
With a command line option to use cairo or splash backends.
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
index acb867b..cff715c 100644
--- a/test/CMakeLists.txt
+++ b/test/CMakeLists.txt
@@ -24,24 +24,14 @@ if (GTK_FOUND)
${CMAKE_BINARY_DIR}/glib
)
- if (ENABLE_SPLASH)
-
- set (gtk_splash_test_SRCS
- gtk-splash-test.cc
- )
- poppler_add_test(gtk-splash-test BUILD_GTK_TESTS ${gtk_splash_test_SRCS})
- target_link_libraries(gtk-splash-test poppler ${GTK2_LIBRARIES})
-
- endif (ENABLE_SPLASH)
+ set (gtk_splash_test_SRCS
+ gtk-test.cc
+ )
+ poppler_add_test(gtk-test BUILD_GTK_TESTS ${gtk_splash_test_SRCS})
+ target_link_libraries(gtk-test poppler-glib ${GTK2_LIBRARIES})
if (HAVE_CAIRO)
- set (gtk_cairo_test_SRCS
- gtk-cairo-test.cc
- )
- poppler_add_test(gtk-cairo-test BUILD_GTK_TESTS ${gtk_cairo_test_SRCS})
- target_link_libraries(gtk-cairo-test poppler-glib ${GTK2_LIBRARIES})
-
set (pdf_inspector_SRCS
pdf-inspector.cc
)
diff --git a/test/Makefile.am b/test/Makefile.am
index 38bf72d..a0eb4d0 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -2,9 +2,6 @@ if BUILD_GTK_TEST
if BUILD_SPLASH_OUTPUT
-gtk_splash_test = \
- gtk-splash-test
-
splash_includes = \
-I$(top_srcdir)/splash
@@ -12,9 +9,6 @@ endif
if BUILD_CAIRO_OUTPUT
-gtk_cairo_test = \
- gtk-cairo-test
-
pdf_inspector = \
pdf_inspector
@@ -36,6 +30,9 @@ endif
pdf_fullrewrite = \
pdf-fullrewrite
+gtk_test = \
+ gtk-test
+
INCLUDES = \
-I$(top_srcdir) \
-I$(top_srcdir)/poppler \
@@ -45,27 +42,21 @@ INCLUDES = \
$(GTK_TEST_CFLAGS) \
$(FONTCONFIG_CFLAGS)
-noinst_PROGRAMS = $(gtk_splash_test) $(gtk_cairo_test) $(pdf_inspector) $(perf_test) $(pdf_fullrewrite)
+noinst_PROGRAMS = $(pdf_inspector) $(perf_test) $(pdf_fullrewrite) $(gtk_test)
AM_LDFLAGS = @auto_import_flags@
-gtk_splash_test_SOURCES = \
- gtk-splash-test.cc
-
-gtk_splash_test_LDADD = \
- $(top_builddir)/poppler/libpoppler.la \
- $(GTK_TEST_LIBS)
-
-gtk_cairo_test_SOURCES = \
- gtk-cairo-test.cc
+gtk_test_SOURCES = \
+ gtk-test.cc
-gtk_cairo_test_LDADD = \
- $(top_builddir)/poppler/libpoppler.la \
- $(top_builddir)/glib/libpoppler-glib.la \
+gtk_test_LDADD = \
+ $(top_builddir)/poppler/libpoppler.la \
+ $(top_builddir)/glib/libpoppler-glib.la \
$(CAIRO_LIBS) \
$(GTK_TEST_LIBS) \
$(FONTCONFIG_LIBS)
+
pdf_inspector_SOURCES = \
pdf-inspector.cc
diff --git a/test/gtk-cairo-test.cc b/test/gtk-cairo-test.cc
deleted file mode 100644
index 8c40568..0000000
--- a/test/gtk-cairo-test.cc
+++ /dev/null
@@ -1,190 +0,0 @@
-//========================================================================
-//
-// 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 <math.h>
-#include <gdk/gdk.h>
-#include <gtk/gtk.h>
-#include <poppler.h>
-
-typedef struct
-{
- GtkWidget *window;
- GtkWidget *sw;
- GtkWidget *drawing_area;
- GtkWidget *spin_button;
- PopplerPage *page;
- PopplerDocument *document;
- cairo_surface_t *surface;
- int *window_count;
-} View;
-
-static void
-drawing_area_expose (GtkWidget *drawing_area,
- GdkEventExpose *event,
- void *data)
-{
- View *v = (View*) data;
- cairo_t *cr;
-
- gdk_window_clear (drawing_area->window);
- cr = gdk_cairo_create (drawing_area->window);
-
- cairo_set_source_surface (cr, v->surface, 0, 0);
- cairo_paint (cr);
- cairo_destroy (cr);
-}
-
-static void
-view_set_page (View *v, int page)
-{
- int w, h;
- double width, height;
- cairo_t *cr;
-
- v->page = poppler_document_get_page (v->document, page);
- poppler_page_get_size (v->page, &width, &height);
- w = (int) ceil(width);
- h = (int) ceil(height);
- cairo_surface_destroy (v->surface);
- v->surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, w, h);
- cr = cairo_create (v->surface);
- poppler_page_render (v->page, cr);
- cairo_destroy (cr);
- gtk_widget_set_size_request (v->drawing_area, w, h);
- gtk_widget_queue_draw (v->drawing_area);
- gtk_spin_button_set_value (GTK_SPIN_BUTTON (v->spin_button), page);
-}
-
-static void
-page_changed_callback (GtkSpinButton *button, View *v)
-{
- int page;
-
- page = gtk_spin_button_get_value_as_int (button);
- view_set_page (v, page);
-}
-
-static void
-destroy_window_callback (GtkWindow *window, View *v)
-{
- if (--(*v->window_count) == 0)
- gtk_main_quit();
-}
-
-static View*
-view_new (const char *filename, int *window_count)
-{
- View *v;
- GtkWidget *window;
- GtkWidget *drawing_area;
- GtkWidget *sw;
- GtkWidget *vbox, *hbox;
- GtkWidget *spin_button;
- int n_pages;
-
- v = g_new0 (View, 1);
-
- v->document = poppler_document_new_from_file (filename, NULL, NULL);
- if (v->document == NULL)
- return NULL;
-
- window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
- vbox = gtk_vbox_new(FALSE, 5);
-
- gtk_container_add (GTK_CONTAINER (window), vbox);
- drawing_area = gtk_drawing_area_new ();
-
- sw = gtk_scrolled_window_new (NULL, NULL);
-
- gtk_box_pack_end (GTK_BOX (vbox), sw, TRUE, TRUE, 0);
- gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW (sw),
- drawing_area);
-
- n_pages = poppler_document_get_n_pages (v->document);
- spin_button = gtk_spin_button_new_with_range (0, n_pages - 1, 1);
- g_signal_connect (G_OBJECT (spin_button), "value-changed",
- G_CALLBACK (page_changed_callback), v);
- hbox = gtk_hbox_new (FALSE, 5);
- gtk_box_pack_end (GTK_BOX (hbox), spin_button, FALSE, TRUE, 0);
-
- gtk_box_pack_end (GTK_BOX (vbox), hbox, FALSE, TRUE, 0);
-
- gtk_widget_show_all (window);
- gtk_widget_realize (window);
-
- v->window = window;
- v->drawing_area = drawing_area;
- v->sw = sw;
- v->window_count = window_count;
- v->spin_button = spin_button;
-
- g_signal_connect (drawing_area,
- "expose_event",
- G_CALLBACK (drawing_area_expose),
- (void*) v);
-
- g_signal_connect (window,
- "destroy",
- G_CALLBACK (destroy_window_callback),
- (void*) v);
-
- return v;
-}
-
-static int option_page = 0;
-static GOptionEntry demo_options[] = {
- { "page", 0, 0, G_OPTION_ARG_INT, &option_page, "Page number", "PAGE" },
- { NULL }
-};
-
-int
-main (int argc, char *argv [])
-{
- View *v;
- int i, window_count;
- GOptionContext *ctx;
-
- ctx = g_option_context_new("FILENAME ...");
- g_option_context_add_main_entries(ctx, demo_options, "main");
- g_option_context_parse(ctx, &argc, &argv, NULL);
- g_option_context_free(ctx);
-
- gtk_init (&argc, &argv);
-
- if (argc == 1)
- {
- char *basename = g_path_get_basename (argv[0]);
- fprintf (stderr, "usage: %s PDF-FILES...\n", basename);
- g_free (basename);
- return -1;
- }
-
- window_count = 0;
- for (i = 1; i < argc; i++) {
- v = view_new (argv[i], &window_count);
- if (v == NULL) {
- g_printerr ("Error loading %s\n", argv[i]);
- continue;
- }
-
- view_set_page (v, option_page);
- window_count++;
- }
-
- if (window_count > 0)
- gtk_main ();
-
- return 0;
-}
diff --git a/test/gtk-splash-test.cc b/test/gtk-splash-test.cc
deleted file mode 100644
index 59fba28..0000000
--- a/test/gtk-splash-test.cc
+++ /dev/null
@@ -1,311 +0,0 @@
-//========================================================================
-//
-// 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,
- GdkWindow *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,
- GdkWindow *drawable,
- int destX, int destY,
- int width, int height) {
- cairo_t *cr;
- 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);
-
- cr = gdk_cairo_create (drawable);
- gdk_cairo_set_source_pixbuf (cr, pixbuf, 0, 0);
- cairo_paint (cr);
- cairo_destroy (cr);
-
- g_object_unref (pixbuf);
-}
-
-
-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;
-}
diff --git a/test/gtk-test.cc b/test/gtk-test.cc
new file mode 100644
index 0000000..1e9e1ee
--- /dev/null
+++ b/test/gtk-test.cc
@@ -0,0 +1,397 @@
+#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_expose (GtkWidget *drawing_area,
+ GdkEventExpose *event,
+ View *view)
+{
+ cairo_t *cr;
+ GdkRectangle document;
+ GdkRectangle draw;
+
+ gdk_window_clear (drawing_area->window);
+
+ 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();
+ }
+
+ cr = gdk_cairo_create (drawing_area->window);
+ if (!gdk_rectangle_intersect (&document, &event->area, &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);
+ }
+
+ cairo_destroy (cr);
+
+ 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;
+
+ 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);
+
+ vbox = gtk_vbox_new (FALSE, 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_hbox_new (FALSE, 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->getXRef());
+ }
+
+ g_signal_connect (view->drawing_area,
+ "expose_event",
+ G_CALLBACK (drawing_area_expose),
+ 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;
+ gchar *uri;
+ PopplerDocument *doc;
+ GError *error = NULL;
+
+ file = g_file_new_for_commandline_arg (file_arguments[i]);
+ uri = g_file_get_uri (file);
+ g_object_unref (file);
+
+ doc = poppler_document_new_from_file (uri, NULL, &error);
+ if (!doc) {
+ g_printerr ("Error opening document %s: %s\n", uri, error->message);
+ g_error_free (error);
+ g_free (uri);
+
+ continue;
+ }
+
+ 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));
+ g_free (uri);
+ }
+
+ gtk_main ();
+
+ delete globalParams;
+
+ return 0;
+}
More information about the poppler
mailing list