Ref #332: include pdf-view
authorColin Clark <colin.clark@cclark.uk>
Wed, 18 Jul 2018 10:09:22 +0000 (11:09 +0100)
committerColin Clark <colin.clark@cclark.uk>
Wed, 18 Jul 2018 10:09:22 +0000 (11:09 +0100)
Display an image of the first page of a pdf file.
GTK3 only.

16 files changed:
README.md
configure.in
src/Makefile.am
src/filefilter.c
src/icons/Makefile.am
src/icons/icon_pdf.png [new file with mode: 0644]
src/image-load.c
src/image.c
src/image_load_pdf.c [new file with mode: 0644]
src/image_load_pdf.h [new file with mode: 0644]
src/pixbuf_util.c
src/pixbuf_util.h
src/preferences.c
src/thumb.c
src/thumb_standard.c
src/typedefs.h

index a8303c7..b8a433f 100644 (file)
--- 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
index ed62c1c..da4e7e0 100644 (file)
@@ -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
index c6951b1..8354c30 100644 (file)
@@ -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)
index 0a3df19..5410f77 100644 (file)
@@ -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;
 }
 
index 31d01ff..bd71013 100644 (file)
@@ -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 (file)
index 0000000..31fc959
Binary files /dev/null and b/src/icons/icon_pdf.png differ
index 4e7d63f..9fc4bdc 100644 (file)
@@ -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)
                {
index e644a6d..c84e818 100644 (file)
@@ -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 (file)
index 0000000..5bc4571
--- /dev/null
@@ -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: */
diff --git a/src/image_load_pdf.h b/src/image_load_pdf.h
new file mode 100644 (file)
index 0000000..c969774
--- /dev/null
@@ -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: */
index 714ef6d..9b10376 100644 (file)
@@ -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);
                }
index fd68d39..d033464 100644 (file)
@@ -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);
index 3eb493f..febe1d7 100644 (file)
@@ -110,7 +110,8 @@ gchar *format_class_list[] = {
        N_("RAW Image"),
        N_("Metadata"),
        N_("Video"),
-       N_("Collection")
+       N_("Collection"),
+       N_("Pdf")
        };
 
 /* config memory values */
index b960d9a..bf18c60 100644 (file)
@@ -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;
index b156f70..f21906b 100644 (file)
@@ -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;
index 66e828c..844a53d 100644 (file)
@@ -144,6 +144,7 @@ typedef enum {
        FORMAT_CLASS_META,
        FORMAT_CLASS_VIDEO,
        FORMAT_CLASS_COLLECTION,
+       FORMAT_CLASS_PDF,
        FILE_FORMAT_CLASSES
 } FileFormatClass;