[testing] Use GdkPixbuf instead of cairo

This commit is contained in:
Moritz Lipp 2010-12-27 09:07:17 +01:00
parent a76dd2f4fe
commit 14d744befe
9 changed files with 82 additions and 149 deletions

View file

@ -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)
{

View file

@ -7,7 +7,6 @@
#include <girara.h>
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

View file

@ -1,7 +1,6 @@
/* See LICENSE file for license and copyright information */
#include <stdlib.h>
#include <cairo.h>
#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);
cairo_surface_mark_dirty(surface);
return surface;
guchar* buffer = malloc(sizeof(char) * (page_width * page_height * 3));
if(!buffer) {
goto error_out;
}
ddjvu_page_set_rotation(page->data, Zathura.document->rotate);
/* 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;
}

View file

@ -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

View file

@ -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) {

View file

@ -3,7 +3,7 @@
#ifndef DOCUMENT_H
#define DOCUMENT_H
#include <cairo.h>
#include <gtk/gtk.h>
#include <stdbool.h>
#include <girara-datastructures.h>
@ -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);

View file

@ -1,7 +1,6 @@
/* See LICENSE file for license and copyright information */
#include <stdlib.h>
#include <cairo.h>
#include <poppler/glib/poppler.h>
#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;
}

View file

@ -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

View file

@ -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);