Skip to content

Instantly share code, notes, and snippets.

@t-8ch
Created June 13, 2012 19:42
Show Gist options
  • Save t-8ch/2926027 to your computer and use it in GitHub Desktop.
Save t-8ch/2926027 to your computer and use it in GitHub Desktop.
poppler and cairo test programm
/*
* Warning:
* This thing is full of memory leaks
*
* filename: benchmark.c
* compile with: LDFLAGS=$(pkg-config --libs --cflags gtk+-3.0 poppler-glib) make benchmark
*/
#include <gtk/gtk.h>
#include <poppler/glib/poppler.h>
GtkWidget *canvas = NULL;
guint max_pages = 0;
guint cur_page = 0;
PopplerDocument *doc;
PopplerPage *page;
void slide_next();
gboolean keypress_callback(GtkWidget *widget, GdkEventKey *ev,
gpointer data) {
if(ev->keyval == GDK_KEY_Escape) gtk_main_quit();
slide_next();
}
gboolean draw_callback(GtkWidget *widget, cairo_t *cr, gpointer data) {
guint width, height;
gdouble popwidth, popheight, scale_factor;
// this should certainly done only on resize
width = gtk_widget_get_allocated_width (canvas);
height = gtk_widget_get_allocated_height (canvas);
poppler_page_get_size(page, &popwidth, &popheight);
scale_factor = height/popheight;
cairo_scale(cr, scale_factor, scale_factor);
poppler_page_render(page, cr);
return FALSE;
}
gboolean quit_callback(GtkWidget *widget, gpointer data) {
gtk_main_quit();
}
void slide_next() {
cur_page++;
cur_page %= max_pages;
page = poppler_document_get_page(doc, cur_page);
// force redraw of whole canvas
gtk_widget_queue_draw_area(canvas, 0, 0,
gtk_widget_get_allocated_width(canvas),
gtk_widget_get_allocated_height(canvas));
printf("Showing page: %2d\n", cur_page);
fflush(stdout);
}
int main(int argc, char **argv) {
GtkWidget *window = NULL;
gchar *uri = NULL;
gdouble elapsed = 0;
guint i;
if(argc < 2) {
fprintf(stderr, "specify a path!\n");
return 42;
}
uri = g_strconcat("file://", argv[1], NULL);
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
canvas = gtk_drawing_area_new();
doc = poppler_document_new_from_file(uri, NULL, NULL);
max_pages = poppler_document_get_n_pages(doc);
gtk_container_add(GTK_CONTAINER(window), canvas);
g_signal_connect(G_OBJECT(window), "key_press_event",
G_CALLBACK(keypress_callback), NULL);
g_signal_connect(G_OBJECT(canvas), "destroy",
G_CALLBACK(quit_callback), NULL);
g_signal_connect(G_OBJECT(canvas), "draw",
G_CALLBACK(draw_callback), NULL);
// get initial page;
page = poppler_document_get_page(doc, cur_page);
gtk_widget_show_all(window);
gtk_main();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment