changeset 2799:ea4163f0c769

Ref #332: include pdf-view Display an image of the first page of a pdf file. GTK3 only.
author Colin Clark <colin.clark@cclark.uk>
date Wed, 18 Jul 2018 11:09:22 +0100
parents d9764817aad8
children 0ecd4885dc09
files README.md configure.in src/Makefile.am src/filefilter.c src/icons/Makefile.am src/icons/icon_pdf.png src/image-load.c src/image.c src/image_load_pdf.c src/image_load_pdf.h src/pixbuf_util.c src/pixbuf_util.h src/preferences.c src/thumb.c src/thumb_standard.c src/typedefs.h
diffstat 16 files changed, 248 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/README.md	Wed Jul 18 10:16:41 2018 +0100
+++ b/README.md	Wed Jul 18 11:09:22 2018 +0100
@@ -228,6 +228,10 @@
         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
--- a/configure.in	Wed Jul 18 10:16:41 2018 +0100
+++ b/configure.in	Wed Jul 18 11:09:22 2018 +0100
@@ -540,6 +540,35 @@
 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 @@
   Libchamplain-gtk:	$HAVE_LIBCHAMPLAIN_GTK
   Lua:	         $HAVE_LUA
   FFmpegthumbnailer:	$HAVE_FFMPEGTHUMBNAILER
+  Pdf:	         $HAVE_PDF
 
 Documentation:
   Doxygen:       $DX_DOXYGEN
--- a/src/Makefile.am	Wed Jul 18 10:16:41 2018 +0100
+++ b/src/Makefile.am	Wed Jul 18 11:09:22 2018 +0100
@@ -11,6 +11,7 @@
 	$(CLUTTER_CFLAGS)		\
 	$(CLUTTER_GTK_CFLAGS)		\
 	$(FFMPEGTHUMBNAILER_CFLAGS)	\
+	$(PDF_CFLAGS)	\
 	-I$(top_srcdir)			\
 	-I$(top_builddir)
 
@@ -25,6 +26,7 @@
 	$(CLUTTER_CFLAGS)		\
 	$(CLUTTER_GTK_CFLAGS)		\
 	$(FFMPEGTHUMBNAILER_CFLAGS)	\
+	$(PDF_CFLAGS)	\
 	-I$(top_srcdir)			\
 	-I$(top_builddir)
 
@@ -183,6 +185,8 @@
 	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 @@
 	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)
--- a/src/filefilter.c	Wed Jul 18 10:16:41 2018 +0100
+++ b/src/filefilter.c	Wed Jul 18 11:09:22 2018 +0100
@@ -291,6 +291,7 @@
 
 	/* 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 @@
 	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;
 }
 
--- a/src/icons/Makefile.am	Wed Jul 18 10:16:41 2018 +0100
+++ b/src/icons/Makefile.am	Wed Jul 18 11:09:22 2018 +0100
@@ -31,7 +31,8 @@
 	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 @@
 	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; \
Binary file src/icons/icon_pdf.png has changed
--- a/src/image-load.c	Wed Jul 18 10:16:41 2018 +0100
+++ b/src/image-load.c	Wed Jul 18 11:09:22 2018 +0100
@@ -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 @@
 		}
 	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)
 		{
--- a/src/image.c	Wed Jul 18 10:16:41 2018 +0100
+++ b/src/image.c	Wed Jul 18 11:09:22 2018 +0100
@@ -700,6 +700,9 @@
 			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);
 			}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/image_load_pdf.c	Wed Jul 18 11:09:22 2018 +0100
@@ -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 <poppler/glib/poppler.h>
+
+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: */
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/image_load_pdf.h	Wed Jul 18 11:09:22 2018 +0100
@@ -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: */
--- a/src/pixbuf_util.c	Wed Jul 18 10:16:41 2018 +0100
+++ b/src/pixbuf_util.c	Wed Jul 18 11:09:22 2018 +0100
@@ -130,6 +130,7 @@
 	{ 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 @@
 		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);
 		}
--- a/src/pixbuf_util.h	Wed Jul 18 10:16:41 2018 +0100
+++ b/src/pixbuf_util.h	Wed Jul 18 11:09:22 2018 +0100
@@ -64,6 +64,7 @@
 #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);
--- a/src/preferences.c	Wed Jul 18 10:16:41 2018 +0100
+++ b/src/preferences.c	Wed Jul 18 11:09:22 2018 +0100
@@ -110,7 +110,8 @@
 	N_("RAW Image"),
 	N_("Metadata"),
 	N_("Video"),
-	N_("Collection")
+	N_("Collection"),
+	N_("Pdf")
 	};
 
 /* config memory values */
--- a/src/thumb.c	Wed Jul 18 10:16:41 2018 +0100
+++ b/src/thumb.c	Wed Jul 18 11:09:22 2018 +0100
@@ -337,7 +337,7 @@
 
 	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;
--- a/src/thumb_standard.c	Wed Jul 18 10:16:41 2018 +0100
+++ b/src/thumb_standard.c	Wed Jul 18 11:09:22 2018 +0100
@@ -667,7 +667,7 @@
 
 
 	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;
--- a/src/typedefs.h	Wed Jul 18 10:16:41 2018 +0100
+++ b/src/typedefs.h	Wed Jul 18 11:09:22 2018 +0100
@@ -144,6 +144,7 @@
 	FORMAT_CLASS_META,
 	FORMAT_CLASS_VIDEO,
 	FORMAT_CLASS_COLLECTION,
+	FORMAT_CLASS_PDF,
 	FILE_FORMAT_CLASSES
 } FileFormatClass;