From 43f7481548feb8b6f3a82af9efe9f522011678d7 Mon Sep 17 00:00:00 2001 From: Colin Clark Date: Wed, 18 Jul 2018 11:09:22 +0100 Subject: [PATCH] Ref #332: include pdf-view Display an image of the first page of a pdf file. GTK3 only. --- README.md | 4 ++ configure.in | 30 ++++++++ src/Makefile.am | 6 +- src/filefilter.c | 2 + src/icons/Makefile.am | 6 +- src/icons/icon_pdf.png | Bin 0 -> 1137 bytes src/image-load.c | 9 +++ src/image.c | 3 + src/image_load_pdf.c | 152 +++++++++++++++++++++++++++++++++++++++++ src/image_load_pdf.h | 29 ++++++++ src/pixbuf_util.c | 4 ++ src/pixbuf_util.h | 1 + src/preferences.c | 3 +- src/thumb.c | 2 +- src/thumb_standard.c | 2 +- src/typedefs.h | 1 + 16 files changed, 248 insertions(+), 6 deletions(-) create mode 100644 src/icons/icon_pdf.png create mode 100644 src/image_load_pdf.c create mode 100644 src/image_load_pdf.h diff --git a/README.md b/README.md index a8303c73..b8a433f4 100644 --- a/README.md +++ b/README.md @@ -228,6 +228,10 @@ And either the ChangeLog file or [Geeqie ChangeLog](http://geeqie.org/cgi-bin/gi for thumbnailing camera video clips disable with configure option: --disable-ffmpegthumbnailer + libpoppler-glib-dev 0.62 + for displaying pdf files + disable with configure option: --disable-pdf + ### Code hackers: If you plan on making any major changes to the code that will be offered for diff --git a/configure.in b/configure.in index ed62c1cc..da4e7e05 100644 --- a/configure.in +++ b/configure.in @@ -540,6 +540,35 @@ AM_CONDITIONAL(HAVE_LUA, [test "x$HAVE_LUA" = xyes]) AC_SUBST(LUA_CFLAGS) AC_SUBST(LUA_LIBS) +# Pdf support +# ---------------------------------------------------------------------- + +if test "x${gtk3}" != "xno"; then + AC_ARG_ENABLE([pdf], + AC_HELP_STRING([--disable-pdf], [disable pdf support]), + [libpdf=$enableval], [libpdf=auto]) + + if test "x${libpdf}" != "xno"; then + PKG_CHECK_MODULES(PDF, poppler-glib >= 0.62, + [ + HAVE_PDF=yes + AC_DEFINE(HAVE_PDF, 1, [define to enable pdf support]) + ], + [ + HAVE_PDF=no + AC_MSG_WARN([$PDF_PKG_ERRORS]) + ]) + else + HAVE_PDF=disabled + fi +else + HAVE_PDF=disabled +fi + +AM_CONDITIONAL(HAVE_PDF, [test "x$HAVE_PDF" = xyes]) +AC_SUBST(PDF_CFLAGS) +AC_SUBST(PDF_LIBS) + # Markdown support # ---------------------------------------------------------------------- @@ -641,6 +670,7 @@ Support: Libchamplain-gtk: $HAVE_LIBCHAMPLAIN_GTK Lua: $HAVE_LUA FFmpegthumbnailer: $HAVE_FFMPEGTHUMBNAILER + Pdf: $HAVE_PDF Documentation: Doxygen: $DX_DOXYGEN diff --git a/src/Makefile.am b/src/Makefile.am index c6951b19..8354c30e 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -11,6 +11,7 @@ AM_CFLAGS = \ $(CLUTTER_CFLAGS) \ $(CLUTTER_GTK_CFLAGS) \ $(FFMPEGTHUMBNAILER_CFLAGS) \ + $(PDF_CFLAGS) \ -I$(top_srcdir) \ -I$(top_builddir) @@ -25,6 +26,7 @@ AM_CXXFLAGS = \ $(CLUTTER_CFLAGS) \ $(CLUTTER_GTK_CFLAGS) \ $(FFMPEGTHUMBNAILER_CFLAGS) \ + $(PDF_CFLAGS) \ -I$(top_srcdir) \ -I$(top_builddir) @@ -183,6 +185,8 @@ geeqie_SOURCES = \ image_load_dds.h\ image_load_collection.c\ image_load_collection.h\ + image_load_pdf.c\ + image_load_pdf.h\ image_load_ffmpegthumbnailer.c\ image_load_ffmpegthumbnailer.h\ image-overlay.c \ @@ -270,7 +274,7 @@ geeqie_SOURCES = \ zonedetect.c \ zonedetect.h -geeqie_LDADD = $(GTK_LIBS) $(GLIB_LIBS) $(INTLLIBS) $(JPEG_LIBS) $(TIFF_LIBS) $(LCMS_LIBS) $(EXIV2_LIBS) $(LIBCHAMPLAIN_LIBS) $(LIBCHAMPLAIN_GTK_LIBS) $(LUA_LIBS) $(CLUTTER_LIBS) $(CLUTTER_GTK_LIBS) $(FFMPEGTHUMBNAILER_LIBS) +geeqie_LDADD = $(GTK_LIBS) $(GLIB_LIBS) $(INTLLIBS) $(JPEG_LIBS) $(TIFF_LIBS) $(LCMS_LIBS) $(EXIV2_LIBS) $(LIBCHAMPLAIN_LIBS) $(LIBCHAMPLAIN_GTK_LIBS) $(LUA_LIBS) $(CLUTTER_LIBS) $(CLUTTER_GTK_LIBS) $(FFMPEGTHUMBNAILER_LIBS) $(PDF_LIBS) EXTRA_DIST = \ $(extra_SLIK) diff --git a/src/filefilter.c b/src/filefilter.c index 0a3df19e..5410f778 100644 --- a/src/filefilter.c +++ b/src/filefilter.c @@ -291,6 +291,7 @@ void filter_add_defaults(void) /* other supported formats */ filter_add_if_missing("dds", "DirectDraw Surface", ".dds", FORMAT_CLASS_IMAGE, FALSE, FALSE, TRUE); + filter_add_if_missing("pdf", "Portable Document Format", ".pdf", FORMAT_CLASS_PDF, FALSE, FALSE, TRUE); } GList *filter_to_list(const gchar *extensions) @@ -470,6 +471,7 @@ FileFormatClass filter_file_get_class(const gchar *name) if (filter_file_class(name, FORMAT_CLASS_META)) return FORMAT_CLASS_META; if (filter_file_class(name, FORMAT_CLASS_VIDEO)) return FORMAT_CLASS_VIDEO; if (filter_file_class(name, FORMAT_CLASS_COLLECTION)) return FORMAT_CLASS_COLLECTION; + if (filter_file_class(name, FORMAT_CLASS_PDF)) return FORMAT_CLASS_PDF; return FORMAT_CLASS_UNKNOWN; } diff --git a/src/icons/Makefile.am b/src/icons/Makefile.am index 31d01ff8..bd71013d 100644 --- a/src/icons/Makefile.am +++ b/src/icons/Makefile.am @@ -31,7 +31,8 @@ ICONS_INLINE = \ icon_exif.png \ icon_marks.png \ icon_info.png \ - icon_sort.png + icon_sort.png \ + icon_pdf.png ICONS_INLINE_PAIRS = \ folder_closed $(srcdir)/folder_closed.png \ @@ -61,7 +62,8 @@ ICONS_INLINE_PAIRS = \ icon_exif $(srcdir)/icon_exif.png \ icon_marks $(srcdir)/icon_marks.png \ icon_info $(srcdir)/icon_info.png \ - icon_sort $(srcdir)/icon_sort.png + icon_sort $(srcdir)/icon_sort.png \ + icon_pdf $(srcdir)/icon_pdf.png icons_inline.h: $(ICONS_INLINE) Makefile.in @sh -ec "echo '/* Auto generated file, do not edit */'; echo; \ diff --git a/src/icons/icon_pdf.png b/src/icons/icon_pdf.png new file mode 100644 index 0000000000000000000000000000000000000000..31fc959e097774095e2ec7ced2129be98f99db6e GIT binary patch literal 1137 zcmeAS@N?(olHy`uVBq!ia0vp^1|ZDA0wn)(8}a}tmUKs7M+SzC{oH>NS%G|oWRD45dJguM!v-tY$DUh!@P+6=(yLU`q0KcVYP7-hXC4kjGiz z5n0T@z_%HM86Owj2?h$Xmw5WRvOi)M;1N}2I2ACFfq~iH)5S3);_%xW{uwc$B5WV- z&pzp+mMe8PLP$tA*COJ3J?@kS#i`ibj6O99SNS>SjF{p z&N{g|-s%up7&1$&nz@O~;?eubH5)vaooi_L3T6I<$2A9j&CU-)ttZM_ku5gySSs&Gk^3+U;$>~~f=bgFY z-5C$6P91Wq5H!oM=~^^vf$WQ2A{)ylybAESv1uBM_SbhNah@BcnzHNqU);?+bZx7m zhDDm)qI&nAF>CZ(-Sk!=mcH>=U~x8CBxz=;YO~XU!Z3 zuSG8=PSw9Av~-`suV%%rgkv)zBW>kOR^3;ef085TOT+Setrz&ns8~M$gRJ3~j+wV(?nJzCob2Z|TuLg(6lHS>0JJ!bv9bVlQ^Qp_f_JQ+} zJ3%*Lw+e#=kJkxvb<8$8X0%rloV1d+iQv=$9?NRJpsy%pu3gar@g#sh9Uh-eTQsCcQO${hr%*9=Cnt z%rnm9nt!e0>C!{?jMrRW&B&d89GJUQOI#yLQW8s2t&)pUffR$0 zfuV)2fw``sS%{&Lm64&9vAMQ^p_PHblcnC}Q!>*kach{$ZV(F8APKS|I6tkV oJh3R1p}f3YFEcN@I61K(RWH9NefB#WDWD<-Pgg&ebxsLQ00=|g{Qv*} literal 0 HcmV?d00001 diff --git a/src/image-load.c b/src/image-load.c index 4e7d63f2..9fc4bdc5 100644 --- a/src/image-load.c +++ b/src/image-load.c @@ -25,6 +25,7 @@ #include "image_load_jpeg.h" #include "image_load_tiff.h" #include "image_load_dds.h" +#include "image_load_pdf.h" #include "image_load_ffmpegthumbnailer.h" #include "image_load_collection.h" @@ -618,6 +619,14 @@ static void image_loader_setup_loader(ImageLoader *il) } else #endif +#ifdef HAVE_PDF + if (il->fd->format_class == FORMAT_CLASS_PDF) + { + DEBUG_1("Using custom pdf loader"); + image_loader_backend_set_pdf(&il->backend); + } + else +#endif #ifdef HAVE_JPEG if (il->bytes_total >= 2 && il->mapped_file[0] == 0xff && il->mapped_file[1] == 0xd8) { diff --git a/src/image.c b/src/image.c index e644a6df..c84e8181 100644 --- a/src/image.c +++ b/src/image.c @@ -700,6 +700,9 @@ static void image_load_done_cb(ImageLoader *il, gpointer data) case FORMAT_CLASS_COLLECTION: pixbuf = pixbuf_inline(PIXBUF_INLINE_COLLECTION); break; + case FORMAT_CLASS_PDF: + pixbuf = pixbuf_inline(PIXBUF_INLINE_ICON_PDF); + break; default: pixbuf = pixbuf_inline(PIXBUF_INLINE_BROKEN); } diff --git a/src/image_load_pdf.c b/src/image_load_pdf.c new file mode 100644 index 00000000..5bc4571a --- /dev/null +++ b/src/image_load_pdf.c @@ -0,0 +1,152 @@ +/* + * Copyright (C) 20018 - The Geeqie Team + * + * Author: Colin Clark + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "main.h" + +#include "image-load.h" +#include "image_load_pdf.h" + +#ifdef HAVE_PDF +#include + +typedef struct _ImageLoaderPDF ImageLoaderPDF; +struct _ImageLoaderPDF { + ImageLoaderBackendCbAreaUpdated area_updated_cb; + ImageLoaderBackendCbSize size_cb; + ImageLoaderBackendCbAreaPrepared area_prepared_cb; + gpointer data; + GdkPixbuf *pixbuf; + guint requested_width; + guint requested_height; + gboolean abort; +}; + +static gboolean image_loader_pdf_load(gpointer loader, const guchar *buf, gsize count, GError **error) +{ + ImageLoaderPDF *ld = (ImageLoaderPDF *) loader; + ImageLoader *il = ld->data; + GError *poppler_error; + gchar *uri; + PopplerPage *page; + PopplerDocument *document; + gint page_num; + gdouble width, height; + cairo_surface_t *surface; + cairo_t *cr; + gboolean ret = FALSE; + + uri = g_filename_to_uri(il->fd->path, NULL, &poppler_error); + if (uri == NULL) + { + log_printf("warning: pdf reader error: %s\n", poppler_error->message); + } + else + { + page_num = 0; + document = poppler_document_new_from_file(uri, NULL, &poppler_error); + page = poppler_document_get_page(document, page_num); + poppler_page_get_size(page, &width, &height); + + surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height); + cr = cairo_create(surface); + poppler_page_render(page, cr); + + ld->pixbuf = gdk_pixbuf_get_from_surface(surface, 0, 0, width, height); + ld->area_updated_cb(loader, 0, 0, width, height, ld->data); + + cairo_destroy (cr); + cairo_surface_destroy(surface); + g_free(uri); + g_object_unref(page); + g_object_unref(document); + ret = TRUE; + } + + return ret; +} + +static gpointer image_loader_pdf_new(ImageLoaderBackendCbAreaUpdated area_updated_cb, ImageLoaderBackendCbSize size_cb, ImageLoaderBackendCbAreaPrepared area_prepared_cb, gpointer data) +{ + ImageLoaderPDF *loader = g_new0(ImageLoaderPDF, 1); + loader->area_updated_cb = area_updated_cb; + loader->size_cb = size_cb; + loader->area_prepared_cb = area_prepared_cb; + loader->data = data; + return (gpointer) loader; +} + +static void image_loader_pdf_set_size(gpointer loader, int width, int height) +{ + ImageLoaderPDF *ld = (ImageLoaderPDF *) loader; + ld->requested_width = width; + ld->requested_height = height; +} + +static GdkPixbuf* image_loader_pdf_get_pixbuf(gpointer loader) +{ + ImageLoaderPDF *ld = (ImageLoaderPDF *) loader; + return ld->pixbuf; +} + +static gchar* image_loader_pdf_get_format_name(gpointer loader) +{ + return g_strdup("pdf"); +} + +static gchar** image_loader_pdf_get_format_mime_types(gpointer loader) +{ + static gchar *mime[] = {"application/pdf", NULL}; + return g_strdupv(mime); +} + +static gboolean image_loader_pdf_close(gpointer loader, GError **error) +{ + return TRUE; +} + +static void image_loader_pdf_abort(gpointer loader) +{ + ImageLoaderPDF *ld = (ImageLoaderPDF *) loader; + ld->abort = TRUE; +} + +static void image_loader_pdf_free(gpointer loader) +{ + ImageLoaderPDF *ld = (ImageLoaderPDF *) loader; + if (ld->pixbuf) g_object_unref(ld->pixbuf); + g_free(ld); +} + +void image_loader_backend_set_pdf(ImageLoaderBackend *funcs) +{ + funcs->loader_new = image_loader_pdf_new; + funcs->set_size = image_loader_pdf_set_size; + funcs->load = image_loader_pdf_load; + funcs->write = NULL; + funcs->get_pixbuf = image_loader_pdf_get_pixbuf; + funcs->close = image_loader_pdf_close; + funcs->abort = image_loader_pdf_abort; + funcs->free = image_loader_pdf_free; + funcs->get_format_name = image_loader_pdf_get_format_name; + funcs->get_format_mime_types = image_loader_pdf_get_format_mime_types; +} + +#endif +/* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */ diff --git a/src/image_load_pdf.h b/src/image_load_pdf.h new file mode 100644 index 00000000..c969774b --- /dev/null +++ b/src/image_load_pdf.h @@ -0,0 +1,29 @@ +/* + * Copyright (C) 20018 - The Geeqie Team + * + * Author: Colin Clark + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef IMAGE_LOAD_PDF_H +#define IMAGE_LOAD_PDF_H + +#ifdef HAVE_PDF +void image_loader_backend_set_pdf(ImageLoaderBackend *funcs); +#endif + +#endif +/* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */ diff --git a/src/pixbuf_util.c b/src/pixbuf_util.c index 714ef6d4..9b103768 100644 --- a/src/pixbuf_util.c +++ b/src/pixbuf_util.c @@ -130,6 +130,7 @@ static PixbufInline inline_pixbuf_data[] = { { PIXBUF_INLINE_ICON_MARKS, icon_marks }, { PIXBUF_INLINE_ICON_INFO, icon_info }, { PIXBUF_INLINE_ICON_SORT, icon_sort }, + { PIXBUF_INLINE_ICON_PDF, icon_pdf }, { NULL, NULL } }; @@ -268,6 +269,9 @@ GdkPixbuf *pixbuf_fallback(FileData *fd, gint requested_width, gint requested_he case FORMAT_CLASS_COLLECTION: pixbuf = pixbuf_inline(PIXBUF_INLINE_COLLECTION); break; + case FORMAT_CLASS_PDF: + pixbuf = pixbuf_inline(PIXBUF_INLINE_ICON_PDF); + break; default: pixbuf = pixbuf_inline(PIXBUF_INLINE_BROKEN); } diff --git a/src/pixbuf_util.h b/src/pixbuf_util.h index fd68d393..d0334647 100644 --- a/src/pixbuf_util.h +++ b/src/pixbuf_util.h @@ -64,6 +64,7 @@ gboolean pixbuf_scale_aspect(gint req_w, gint req_h, gint old_w, gint old_h, gin #define PIXBUF_INLINE_ICON_MARKS "icon_marks" #define PIXBUF_INLINE_ICON_INFO "icon_info" #define PIXBUF_INLINE_ICON_SORT "icon_sort" +#define PIXBUF_INLINE_ICON_PDF "icon_pdf" GdkPixbuf *pixbuf_copy_rotate_90(GdkPixbuf *src, gboolean counter_clockwise); GdkPixbuf *pixbuf_copy_mirror(GdkPixbuf *src, gboolean mirror, gboolean flip); diff --git a/src/preferences.c b/src/preferences.c index 3eb493f9..febe1d72 100644 --- a/src/preferences.c +++ b/src/preferences.c @@ -110,7 +110,8 @@ gchar *format_class_list[] = { N_("RAW Image"), N_("Metadata"), N_("Video"), - N_("Collection") + N_("Collection"), + N_("Pdf") }; /* config memory values */ diff --git a/src/thumb.c b/src/thumb.c index b960d9a5..bf18c60b 100644 --- a/src/thumb.c +++ b/src/thumb.c @@ -337,7 +337,7 @@ gboolean thumb_loader_start(ThumbLoader *tl, FileData *fd) if (!tl->fd) tl->fd = file_data_ref(fd); - if (tl->fd->format_class != FORMAT_CLASS_IMAGE && tl->fd->format_class != FORMAT_CLASS_RAWIMAGE && tl->fd->format_class != FORMAT_CLASS_COLLECTION && tl->fd->format_class != FORMAT_CLASS_VIDEO && !options->file_filter.disable) + if (tl->fd->format_class != FORMAT_CLASS_IMAGE && tl->fd->format_class != FORMAT_CLASS_RAWIMAGE && tl->fd->format_class != FORMAT_CLASS_COLLECTION && tl->fd->format_class != FORMAT_CLASS_VIDEO && tl->fd->format_class != FORMAT_CLASS_PDF && !options->file_filter.disable) { thumb_loader_set_fallback(tl); return FALSE; diff --git a/src/thumb_standard.c b/src/thumb_standard.c index b156f702..f21906b6 100644 --- a/src/thumb_standard.c +++ b/src/thumb_standard.c @@ -667,7 +667,7 @@ gboolean thumb_loader_std_start(ThumbLoaderStd *tl, FileData *fd) tl->fd = file_data_ref(fd); - if (!stat_utf8(fd->path, &st) || (tl->fd->format_class != FORMAT_CLASS_IMAGE && tl->fd->format_class != FORMAT_CLASS_RAWIMAGE && tl->fd->format_class != FORMAT_CLASS_VIDEO && tl->fd->format_class != FORMAT_CLASS_COLLECTION && !options->file_filter.disable)) + if (!stat_utf8(fd->path, &st) || (tl->fd->format_class != FORMAT_CLASS_IMAGE && tl->fd->format_class != FORMAT_CLASS_RAWIMAGE && tl->fd->format_class != FORMAT_CLASS_VIDEO && tl->fd->format_class != FORMAT_CLASS_COLLECTION && tl->fd->format_class != FORMAT_CLASS_PDF && !options->file_filter.disable)) { thumb_loader_std_set_fallback(tl); return FALSE; diff --git a/src/typedefs.h b/src/typedefs.h index 66e828c8..844a53db 100644 --- a/src/typedefs.h +++ b/src/typedefs.h @@ -144,6 +144,7 @@ typedef enum { FORMAT_CLASS_META, FORMAT_CLASS_VIDEO, FORMAT_CLASS_COLLECTION, + FORMAT_CLASS_PDF, FILE_FORMAT_CLASSES } FileFormatClass; -- 2.20.1