diff --git a/callbacks.c b/callbacks.c index a76e5ca..07c0f5e 100644 --- a/callbacks.c +++ b/callbacks.c @@ -16,51 +16,6 @@ cb_destroy(GtkWidget* widget, gpointer data) return TRUE; } -gboolean -cb_draw(GtkWidget* widget, GdkEventExpose* expose, gpointer data) -{ - if(!widget || !Zathura.document) { - return false; - } - - gdk_window_clear(widget->window); - cairo_t *cairo = gdk_cairo_create(widget->window); - - if(!cairo) { - return false; - } - - // FIXME: Split up - zathura_page_t* page = zathura_page_get(Zathura.document, Zathura.document->current_page_number); - if(!page) { - cairo_destroy(cairo); - goto error_out; - } - - cairo_surface_t* surface = zathura_page_render(page); - if(!surface) { - zathura_page_free(page); - fprintf(stderr, "error: rendering failed\n"); - goto error_out; - } - - cairo_set_source_surface(cairo, surface, 0, 0); - cairo_paint(cairo); - cairo_destroy(cairo); - - cairo_surface_destroy(surface); - zathura_page_free(page); - - gtk_widget_set_size_request(Zathura.UI.drawing_area, page->width, page->height); - gtk_widget_queue_draw(Zathura.UI.drawing_area); - - return true; - -error_out: - - return false; -} - void buffer_changed(girara_session_t* session) { diff --git a/callbacks.h b/callbacks.h index 45aa179..254d7e8 100644 --- a/callbacks.h +++ b/callbacks.h @@ -7,7 +7,6 @@ #include gboolean cb_destroy(GtkWidget* widget, gpointer data); -gboolean cb_draw(GtkWidget* widget, GdkEventExpose* expose, gpointer data); void buffer_changed(girara_session_t* session); #endif // CALLBACKS_H diff --git a/ft/djvu/djvu.c b/ft/djvu/djvu.c index 3100ad3..da8ba9c 100644 --- a/ft/djvu/djvu.c +++ b/ft/djvu/djvu.c @@ -1,7 +1,6 @@ /* See LICENSE file for license and copyright information */ #include -#include #include "djvu.h" #include "../../zathura.h" @@ -188,7 +187,7 @@ djvu_page_form_fields_get(zathura_page_t* page) return NULL; } -cairo_surface_t* +GtkWidget* djvu_page_render(zathura_page_t* page) { if(!Zathura.document || !page || !page->data || !page->document) { @@ -199,54 +198,51 @@ djvu_page_render(zathura_page_t* page) unsigned int page_width = Zathura.document->scale * page->width; unsigned int page_height = Zathura.document->scale * page->height; - /* init ddjvu render data */ - ddjvu_rect_t rrect = { 0, 0, page_width, page_height }; - ddjvu_rect_t prect = { 0, 0, page_width, page_height }; - - ddjvu_page_rotation_t rotation = DDJVU_ROTATE_0; - - unsigned int dim_temp = 0; - - switch(Zathura.document->rotate) { - case 90: - dim_temp = page_width; - page_width = page_height; - page_height = dim_temp; - - rotation = DDJVU_ROTATE_90; - break; - case 180: - rotation = DDJVU_ROTATE_180; - break; - case 270: - dim_temp = page_width; - page_width = page_height; - page_height = dim_temp; - - rotation = DDJVU_ROTATE_270; - break; - } - - djvu_document_t* djvu_document = (djvu_document_t*) page->document->data; - - /* create cairo data */ - cairo_surface_t* surface = cairo_image_surface_create(CAIRO_FORMAT_RGB24, - page->width, page->height); - - if(!surface) { + if(!page_width || !page_height) { return NULL; } - int rowsize = cairo_image_surface_get_stride(surface); - char* data = (char*) cairo_image_surface_get_data(surface); + /* init ddjvu render data */ + djvu_document_t* djvu_document = (djvu_document_t*) page->document->data; - /* render */ - ddjvu_page_set_rotation(page->data, rotation); + ddjvu_rect_t rrect = { 0, 0, page_width, page_height }; + ddjvu_rect_t prect = { 0, 0, page_width, page_height }; - ddjvu_page_render(page->data, DDJVU_RENDER_COLOR, &prect, &rrect, - djvu_document->format, rowsize, data); + guchar* buffer = malloc(sizeof(char) * (page_width * page_height * 3)); + if(!buffer) { + goto error_out; + } - cairo_surface_mark_dirty(surface); + ddjvu_page_set_rotation(page->data, Zathura.document->rotate); - return surface; + /* render page */ + ddjvu_page_render(page->data, DDJVU_RENDER_COLOR, &prect, &rrect, djvu_document->format, + 3 * page_width, (char*) buffer); + + /* create pixbuf */ + GdkPixbuf* pixbuf = gdk_pixbuf_new_from_data(buffer, GDK_COLORSPACE_RGB, FALSE, 8, + page_width, page_height, 3 * page_width, NULL, NULL); + + if(!pixbuf) { + free(buffer); + g_object_unref(pixbuf); + goto error_out; + } + + GtkWidget* image = gtk_image_new(); + + if(!image) { + free(buffer); + g_object_unref(pixbuf); + goto error_out; + } + + gtk_image_set_from_pixbuf(GTK_IMAGE(image), pixbuf); + gtk_widget_show(image); + + return image; + +error_out: + + return NULL; } diff --git a/ft/djvu/djvu.h b/ft/djvu/djvu.h index bc3b4a9..5f6d894 100644 --- a/ft/djvu/djvu.h +++ b/ft/djvu/djvu.h @@ -24,7 +24,7 @@ zathura_page_t* djvu_page_get(zathura_document_t* document, unsigned int page); zathura_list_t* djvu_page_search_text(zathura_page_t* page, const char* text); zathura_list_t* djvu_page_links_get(zathura_page_t* page); zathura_list_t* djvu_page_form_fields_get(zathura_page_t* page); -cairo_surface_t* djvu_page_render(zathura_page_t* page); +GtkWidget* djvu_page_render(zathura_page_t* page); bool djvu_page_free(zathura_page_t* page); #endif // DJVU_H diff --git a/ft/document.c b/ft/document.c index d07e42c..d202893 100644 --- a/ft/document.c +++ b/ft/document.c @@ -284,7 +284,7 @@ zathura_page_form_fields_free(zathura_list_t* list) return false; } -cairo_surface_t* +GtkWidget* zathura_page_render(zathura_page_t* page) { if(!page || !page->document) { diff --git a/ft/document.h b/ft/document.h index a374374..1442382 100644 --- a/ft/document.h +++ b/ft/document.h @@ -3,7 +3,7 @@ #ifndef DOCUMENT_H #define DOCUMENT_H -#include +#include #include #include @@ -102,7 +102,7 @@ struct zathura_document_s zathura_list_t* (*page_search_text)(zathura_page_t* page, const char* text); zathura_list_t* (*page_links_get)(zathura_page_t* page); zathura_list_t* (*page_form_fields_get)(zathura_page_t* page); - cairo_surface_t* (*page_render)(zathura_page_t* page); + GtkWidget* (*page_render)(zathura_page_t* page); bool (*page_free)(zathura_page_t* page); } functions; }; @@ -122,7 +122,7 @@ zathura_list_t* zathura_page_links_get(zathura_page_t* page); bool zathura_page_links_free(zathura_list_t* list); zathura_list_t* zathura_page_form_fields_get(zathura_page_t* page); bool zathura_page_form_fields_free(zathura_list_t* list); -cairo_surface_t* zathura_page_render(zathura_page_t* page); +GtkWidget* zathura_page_render(zathura_page_t* page); zathura_index_element_t* zathura_index_element_new(const char* title); void zathura_index_element_free(zathura_index_element_t* index); diff --git a/ft/pdf/pdf.c b/ft/pdf/pdf.c index 63abe0c..de003f3 100644 --- a/ft/pdf/pdf.c +++ b/ft/pdf/pdf.c @@ -1,7 +1,6 @@ /* See LICENSE file for license and copyright information */ #include -#include #include #include "pdf.h" @@ -242,7 +241,7 @@ pdf_page_form_fields_get(zathura_page_t* page) return NULL; } -cairo_surface_t* +GtkWidget* pdf_page_render(zathura_page_t* page) { if(!Zathura.document || !page || !page->data || !page->document) { @@ -261,47 +260,27 @@ pdf_page_render(zathura_page_t* page) page_height = dim_temp; } - /* create cairo data */ - cairo_surface_t* surface = cairo_image_surface_create(CAIRO_FORMAT_RGB24, + /* create pixbuf */ + GdkPixbuf* pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, page_width, page_height); - if(!surface) { + if(!pixbuf) { return NULL; } - cairo_t* cairo = cairo_create(surface); + poppler_page_render_to_pixbuf(page->data, 0, 0, page_width, page_height, Zathura.document->scale, + Zathura.document->rotate, pixbuf); - if(!cairo) { - cairo_surface_destroy(surface); + /* write pixbuf */ + GtkWidget* image = gtk_image_new(); + + if(!image) { + g_object_unref(pixbuf); return NULL; } - cairo_set_source_rgb(cairo, 1, 1, 1); - cairo_rectangle(cairo, 0, 0, page_width, page_height); - cairo_fill(cairo); + gtk_image_set_from_pixbuf(GTK_IMAGE(image), pixbuf); + gtk_widget_show(image); - switch(Zathura.document->rotate) { - case 90: - cairo_translate(cairo, page->width, 0); - break; - case 180: - cairo_translate(cairo, page->width, page->height); - break; - case 270: - cairo_translate(cairo, 0, page->height); - break; - default: - cairo_translate(cairo, 0, 0); - break; - } - - cairo_scale(cairo, Zathura.document->scale, Zathura.document->scale); - cairo_rotate(cairo, Zathura.document->rotate * G_PI / 180.0); - - /* render */ - poppler_page_render(page->data, cairo); - - cairo_destroy(cairo); - - return surface; + return image; } diff --git a/ft/pdf/pdf.h b/ft/pdf/pdf.h index 1506880..39afbe0 100644 --- a/ft/pdf/pdf.h +++ b/ft/pdf/pdf.h @@ -22,7 +22,7 @@ zathura_page_t* pdf_page_get(zathura_document_t* document, unsigned int page); zathura_list_t* pdf_page_search_text(zathura_page_t* page, const char* text); zathura_list_t* pdf_page_links_get(zathura_page_t* page); zathura_list_t* pdf_page_form_fields_get(zathura_page_t* page); -cairo_surface_t* pdf_page_render(zathura_page_t* page); +GtkWidget* pdf_page_render(zathura_page_t* page); bool pdf_page_free(zathura_page_t* page); #endif // PDF_H diff --git a/zathura.c b/zathura.c index 7bb4dc1..f0a41a9 100644 --- a/zathura.c +++ b/zathura.c @@ -42,23 +42,6 @@ init_zathura() girara_statusbar_item_set_text(Zathura.UI.session, Zathura.UI.statusbar.file, "[No Name]"); - /* drawing area */ - Zathura.UI.drawing_area = gtk_drawing_area_new(); - if(!Zathura.UI.drawing_area) { - goto error_free; - } - - gtk_widget_modify_bg(Zathura.UI.drawing_area, GTK_STATE_NORMAL, - &(Zathura.UI.session->style.default_background)); - - g_signal_connect(G_OBJECT(Zathura.UI.drawing_area), "expose-event", G_CALLBACK(cb_draw), NULL); - gtk_widget_show(Zathura.UI.drawing_area); - - /* set view */ - if(!girara_set_view(Zathura.UI.session, Zathura.UI.drawing_area)) { - goto error_free; - } - /* signals */ g_signal_connect(G_OBJECT(Zathura.UI.session->gtk.window), "destroy", G_CALLBACK(cb_destroy), NULL); @@ -129,6 +112,27 @@ page_set(unsigned int page_id) goto error_out; } + /* render page */ + zathura_page_t* page = zathura_page_get(Zathura.document, page_id); + if(!page) { + goto error_out; + } + + GtkWidget* image = zathura_page_render(page); + if(!image) { + zathura_page_free(page); + fprintf(stderr, "error: rendering failed\n"); + goto error_out; + } + + zathura_page_free(page); + + /* draw new rendered page */ + if(!girara_set_view(Zathura.UI.session, image)) { + goto error_out; + } + + /* update page number */ Zathura.document->current_page_number = page_id; char* page_number = g_strdup_printf("[%d/%d]", page_id + 1, Zathura.document->number_of_pages);