]> www.fi.muni.cz Git - evince.git/blobdiff - pdf/xpdf/pdf-document.cc
Beginnings of clipboard support. Incomplete but primary sort of work.
[evince.git] / pdf / xpdf / pdf-document.cc
index e6e72c2774f2afcbb2acd8cf8d16bc93d1fa0b27..2293841fd2e6709433f906311eb27126c5400687 100644 (file)
  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  */
 
+#include <glib/gi18n.h>
+
 #include "gpdf-g-switch.h"
 #include "pdf-document.h"
 #include "ev-ps-exporter.h"
 #include "ev-document-find.h"
 #include "gpdf-g-switch.h"
+#include "ev-document-bookmarks.h"
+#include "ev-document-misc.h"
+#include "ev-document-thumbnails.h"
 
 #include "GlobalParams.h"
 #include "GDKSplashOutputDev.h"
 #include "Outline.h"
 #include "UnicodeMap.h"
 #include "GlobalParams.h"
+#include "GfxState.h"
+#include "Thumb.h"
 #include "goo/GList.h"
 #include "PSOutputDev.h"
 
+enum {
+       PROP_0,
+       PROP_TITLE
+};
+
 typedef struct
 {
        PdfDocument *document;
@@ -78,8 +90,9 @@ struct _PdfDocument
        PdfDocumentSearch *search;
 };
 
-static void pdf_document_document_bookmarks_iface_init (EvDocumentBookmarksIface *iface);
-static void pdf_document_document_iface_init           (EvDocumentIface          *iface);
+static void pdf_document_document_bookmarks_iface_init  (EvDocumentBookmarksIface  *iface);
+static void pdf_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface);
+static void pdf_document_document_iface_init            (EvDocumentIface           *iface);
 static void pdf_document_ps_exporter_iface_init (EvPSExporterIface   *iface);
 static void pdf_document_find_iface_init        (EvDocumentFindIface *iface);
 static void pdf_document_search_free            (PdfDocumentSearch   *search);
@@ -91,6 +104,8 @@ G_DEFINE_TYPE_WITH_CODE (PdfDocument, pdf_document, G_TYPE_OBJECT,
                                                        pdf_document_document_iface_init);
                                 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_BOOKMARKS,
                                                        pdf_document_document_bookmarks_iface_init);
+                                G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_THUMBNAILS,
+                                                       pdf_document_document_thumbnails_iface_init);
                                 G_IMPLEMENT_INTERFACE (EV_TYPE_PS_EXPORTER,
                                                        pdf_document_ps_exporter_iface_init);
                                 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_FIND,
@@ -114,7 +129,7 @@ document_validate_page (PdfDocument *pdf_document)
                 if (pdf_document->search)
                         pdf_document_search_page_changed (pdf_document->search);
        }
-       
+
        return pdf_document->page_valid;
 }
 
@@ -128,7 +143,7 @@ pdf_document_load (EvDocument  *document,
        int err;
        char *filename;
        GString *filename_g;
-       GString *enc; 
+       GString *enc;
 
        if (!globalParams) {
                globalParams = new GlobalParams("/etc/xpdfrc");
@@ -138,7 +153,7 @@ pdf_document_load (EvDocument  *document,
        if (! pdf_document->umap) {
                enc = new GString("UTF-8");
                pdf_document->umap = globalParams->getUnicodeMap(enc);
-               pdf_document->umap->incRefCnt (); 
+               pdf_document->umap->incRefCnt ();
                delete enc;
        }
 
@@ -177,9 +192,30 @@ pdf_document_load (EvDocument  *document,
 
        pdf_document->page_valid = FALSE;
 
+       g_object_notify (G_OBJECT (pdf_document), "title");
+
        return TRUE;
 }
 
+static gboolean
+pdf_document_save (EvDocument  *document,
+                  const char  *uri,
+                  GError     **error)
+{
+       PdfDocument *pdf_document = PDF_DOCUMENT (document);
+       char *filename;
+       gboolean retval = FALSE;
+
+       filename = g_filename_from_uri (uri, NULL, error);
+       if (filename != NULL) {
+               GString *fname = new GString (filename);
+
+               retval = pdf_document->doc->saveAs (fname);
+       }
+
+       return retval;
+}
+
 static int
 pdf_document_get_n_pages (EvDocument  *document)
 {
@@ -204,6 +240,7 @@ pdf_document_set_page (EvDocument  *document,
                pdf_document->page_valid = FALSE;
        }
 
+       ev_document_changed (document);
 }
 
 static int
@@ -358,9 +395,9 @@ pdf_document_search_emit_found (PdfDocumentSearch *search)
                 if (i != pdf_document->page &&
                     search->other_page_flags[i]) {
                         EvFindResult result;
-                        
+
                         result.page_num = i;
-                        
+
                         /* Use bogus coordinates, again we can't get coordinates
                          * until this is the current page because TextOutputDev
                          * isn't good enough
@@ -369,18 +406,18 @@ pdf_document_search_emit_found (PdfDocumentSearch *search)
                         result.highlight_area.y = -1;
                         result.highlight_area.width = 1;
                         result.highlight_area.height = 1;
-                        
+
                         g_array_append_val (tmp_results, result);
                 }
 
                 ++i;
         }
-        
+
         ev_document_find_found (EV_DOCUMENT_FIND (pdf_document),
                                 (EvFindResult*) tmp_results->data,
                                 tmp_results->len,
                                 pages_done / (double) n_pages);
-        
+
         g_array_free (tmp_results, TRUE);
 }
 
@@ -399,13 +436,13 @@ pdf_document_search_page_changed (PdfDocumentSearch   *search)
                 search->current_page = -1;
                 return;
         }
-        
+
         if (search->current_page == current_page)
                 return;
-        
+
         /* We need to create current_page_results for the new current page */
         g_array_set_size (search->current_page_results, 0);
-        
+
         if (pdf_document->out->findText (search->ucs4, search->ucs4_len,
                                          gTrue, gTrue, // startAtTop, stopAtBottom
                                          gFalse, gFalse, // startAtLast, stopAtLast
@@ -431,7 +468,7 @@ pdf_document_search_page_changed (PdfDocumentSearch   *search)
                         result.highlight_area.y = yMin;
                         result.highlight_area.width = xMax - xMin;
                         result.highlight_area.height = yMax - yMin;
-                        
+
                         g_array_append_val (search->current_page_results, result);
                 }
         }
@@ -441,7 +478,7 @@ pdf_document_search_page_changed (PdfDocumentSearch   *search)
          */
         search->other_page_flags[current_page] =
                 search->current_page_results->len > 0;
-        
+
         pdf_document_search_emit_found (search);
 }
 
@@ -473,7 +510,7 @@ pdf_document_search_idle_callback (void *data)
                         goto end_search;
                 }
         }
-                                                  
+
         pdf_document->doc->displayPage (search->output_dev,
                                         search->search_page,
                                         72, 72, 0, gTrue, gFalse);
@@ -486,7 +523,7 @@ pdf_document_search_idle_callback (void *data)
                 /* This page has results */
                 search->other_page_flags[search->search_page] = TRUE;
         }
-        
+
         search->search_page += 1;
         if (search->search_page > n_pages) {
                 /* wrap around */
@@ -495,7 +532,7 @@ pdf_document_search_idle_callback (void *data)
 
         /* We do this even if nothing was found, to update the percent complete */
         pdf_document_search_emit_found (search);
-        
+
         return TRUE;
 
  end_search:
@@ -519,7 +556,7 @@ pdf_document_find_begin (EvDocumentFind   *document,
          * code is always case insensitive for ASCII
          * and case sensitive for all other languaages)
          */
-        
+
         g_assert (sizeof (gunichar) == sizeof (Unicode));
         ucs4 = g_utf8_to_ucs4_fast (search_string, -1,
                                     &ucs4_len);
@@ -538,22 +575,22 @@ pdf_document_find_begin (EvDocumentFind   *document,
                 pdf_document_search_free (pdf_document->search);
                 pdf_document->search = NULL;
         }
-        
+
         search = g_new0 (PdfDocumentSearch, 1);
 
         search->ucs4 = ucs4;
         search->ucs4_len = ucs4_len;
-        
+
         search->current_page_results = g_array_new (FALSE,
                                                     FALSE,
                                                     sizeof (EvFindResult));
-        n_pages = ev_document_get_n_pages (EV_DOCUMENT (document)); 
+        n_pages = ev_document_get_n_pages (EV_DOCUMENT (document));
 
         /* This is an array of bool; with the first value ignored
          * so we can index by the based-at-1 page numbers
          */
         search->other_page_flags = g_new0 (guchar, n_pages + 1);
-        
+
         search->document = pdf_document;
 
         /* We add at low priority so the progress bar repaints */
@@ -572,7 +609,7 @@ pdf_document_find_begin (EvDocumentFind   *document,
         search->current_page = -1;
 
         pdf_document->search = search;
-        
+
         /* Update for the current page right away */
         pdf_document_search_page_changed (search);
 }
@@ -596,10 +633,10 @@ pdf_document_search_free (PdfDocumentSearch   *search)
 
        if (search->output_dev)
                delete search->output_dev;
-       
+
         g_array_free (search->current_page_results, TRUE);
         g_free (search->other_page_flags);
-        
+
         g_free (search->ucs4);
        g_free (search);
 }
@@ -615,7 +652,7 @@ pdf_document_ps_export_begin (EvPSExporter *exporter, const char *filename)
        document->ps_out = new PSOutputDev ((char *)filename, document->doc->getXRef(),
                                            document->doc->getCatalog(), 1,
                                            ev_document_get_n_pages (EV_DOCUMENT (document)),
-                                           psModePS);  
+                                           psModePS);
 }
 
 static void
@@ -647,13 +684,13 @@ typedef struct
 } BookmarksIter;
 
 static gchar *
-unicode_to_char (OutlineItem *outline_item, 
+unicode_to_char (OutlineItem *outline_item,
                 UnicodeMap *uMap)
 {
        GString gstr;
        gchar buf[8]; /* 8 is enough for mapping an unicode char to a string */
        int i, n;
-       
+
        for (i = 0; i < outline_item->getTitleLength(); ++i) {
                n = uMap->mapUnicode(outline_item->getTitle()[i], buf, sizeof(buf));
                gstr.append(buf, n);
@@ -716,7 +753,7 @@ pdf_document_bookmarks_get_values (EvDocumentBookmarks      *document_bookmarks,
        LinkDest *link_dest = NULL;
        LinkURI *link_uri = NULL;
        LinkGoTo *link_goto = NULL;
-       GString *named_dest; 
+       GString *named_dest;
        Unicode *link_title;
        Ref page_ref;
        gint page_num = -1;
@@ -729,7 +766,7 @@ pdf_document_bookmarks_get_values (EvDocumentBookmarks      *document_bookmarks,
 
        anItem = (OutlineItem *)iter->items->get(iter->index);
        link_action = anItem->getAction ();
-       link_title = anItem->getTitle (); 
+       link_title = anItem->getTitle ();
 
        if (link_action) {
                switch (link_action->getKind ()) {
@@ -755,15 +792,15 @@ pdf_document_bookmarks_get_values (EvDocumentBookmarks      *document_bookmarks,
                                } else {
                                        page_num = link_dest->getPageNum ();
                                }
-                               
+
                                delete link_dest;
                        }
-                               
+
                        break;
                case actionURI:
                        link_uri = dynamic_cast <LinkURI *> (link_action);
                        break;
-                       
+
                case actionNamed:
                        /*Skip, for now */
                default:
@@ -843,7 +880,7 @@ pdf_document_finalize (GObject *object)
 
        if (pdf_document->search)
                pdf_document_search_free (pdf_document->search);
-       
+
        if (pdf_document->target)
                g_object_unref (pdf_document->target);
 
@@ -856,18 +893,128 @@ pdf_document_finalize (GObject *object)
 
 }
 
+static void
+pdf_document_set_property (GObject *object,
+                          guint prop_id,
+                          const GValue *value,
+                          GParamSpec *pspec)
+{
+       switch (prop_id)
+
+       {
+               case PROP_TITLE:
+                       /* read only */
+                       break;
+       }
+}
+
+static gboolean
+has_unicode_marker (GString *string)
+{
+       return ((string->getChar (0) & 0xff) == 0xfe &&
+               (string->getChar (1) & 0xff) == 0xff);
+}
+
+static gchar *
+pdf_info_dict_get_string (Dict *info_dict, const gchar *key) {
+       Object obj;
+       GString *value;
+       gchar *result;
+
+       g_return_val_if_fail (info_dict != NULL, NULL);
+       g_return_val_if_fail (key != NULL, NULL);
+
+       if (!info_dict->lookup ((gchar *)key, &obj)->isString ()) {
+               obj.free ();
+               return g_strdup (_("Unknown"));
+       }
+
+       value = obj.getString ();
+
+       if (has_unicode_marker (value)) {
+               result = g_convert (value->getCString () + 2,
+                                   value->getLength () - 2,
+                                   "UTF-8", "UTF-16BE", NULL, NULL, NULL);
+       } else {
+               result = g_strndup (value->getCString (), value->getLength ());
+       }
+
+       obj.free ();
+
+       return result;
+}
+
+static char *
+pdf_document_get_title (PdfDocument *pdf_document)
+{
+       char *title = NULL;
+       Object info;
+
+       pdf_document->doc->getDocInfo (&info);
+
+       if (info.isDict ()) {
+               title = pdf_info_dict_get_string (info.getDict(), "Title");
+       }
+
+       return title;
+}
+
+static char *
+pdf_document_get_text (EvDocument *document, GdkRectangle *rect)
+{
+       PdfDocument *pdf_document = PDF_DOCUMENT (document);
+       GString *sel_text = new GString;
+       const char *text;
+       int x1, y1, x2, y2;
+
+       x1 = rect->x;
+       y1 = rect->y;
+       x2 = x1 + rect->width;
+       y2 = y1 + rect->height;
+
+       sel_text = pdf_document->out->getText (x1, y1, x2, y2);
+       text = sel_text->getCString ();
+
+       return text ? g_strdup (text) : NULL;
+}
+
+static void
+pdf_document_get_property (GObject *object,
+                          guint prop_id,
+                          GValue *value,
+                          GParamSpec *pspec)
+{
+       PdfDocument *pdf_document = PDF_DOCUMENT (object);
+       char *title;
+
+       switch (prop_id)
+       {
+               case PROP_TITLE:
+                       title = pdf_document_get_title (pdf_document);
+                       g_value_set_string (value, title);
+                       g_free (title);
+                       break;
+       }
+}
+
 static void
 pdf_document_class_init (PdfDocumentClass *klass)
 {
        GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
 
        gobject_class->finalize = pdf_document_finalize;
+       gobject_class->get_property = pdf_document_get_property;
+       gobject_class->set_property = pdf_document_set_property;
+
+       g_object_class_override_property (gobject_class, PROP_TITLE, "title");
 }
 
 static void
 pdf_document_document_iface_init (EvDocumentIface *iface)
 {
        iface->load = pdf_document_load;
+       iface->save = pdf_document_save;
+       iface->get_text = pdf_document_get_text;
        iface->get_n_pages = pdf_document_get_n_pages;
        iface->set_page = pdf_document_set_page;
        iface->get_page = pdf_document_get_page;
@@ -905,6 +1052,115 @@ pdf_document_document_bookmarks_iface_init (EvDocumentBookmarksIface *iface)
        iface->free_iter = pdf_document_bookmarks_free_iter;
 }
 
+/* Thumbnails */
+static GdkPixbuf *
+pdf_document_thumbnails_get_page_pixbuf (PdfDocument *pdf_document,
+                                        gdouble      scale_factor,
+                                        gint         page_num,
+                                        gint         width,
+                                        gint         height)
+{
+       GdkPixmap *pixmap;
+       GDKSplashOutputDev *output;
+       GdkPixbuf *pixbuf;
+       GdkPixbuf *shadow;
+
+       pixmap = gdk_pixmap_new (pdf_document->target,
+                                width, height, -1);
+
+       output = new GDKSplashOutputDev (gdk_drawable_get_screen (pdf_document->target),
+                                        NULL, NULL);
+       output->startDoc (pdf_document->doc->getXRef());
+       pdf_document->doc->displayPage (output,
+                                       page_num + 1,
+                                       72*scale_factor,
+                                       72*scale_factor,
+                                       0, gTrue, gFalse);
+       output->redraw (0, 0,
+                       pixmap,
+                       0, 0,
+                       width, height);
+       pixbuf = gdk_pixbuf_get_from_drawable (NULL,
+                                              pixmap,
+                                              NULL,
+                                              0, 0,
+                                              0, 0,
+                                              width, height);
+       gdk_drawable_unref (pixmap);
+       delete output;
+
+       shadow = ev_document_misc_get_thumbnail_frame (-1, -1, pixbuf);
+       g_object_unref (pixbuf);
+
+       return shadow;
+}
+
+static GdkPixbuf *
+pdf_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document_thumbnails,
+                                      gint                  page,
+                                      gint                  width)
+{
+       PdfDocument *pdf_document = PDF_DOCUMENT (document_thumbnails);
+       GdkPixbuf *thumbnail;
+       Page *the_page;
+       Object the_thumb;
+       Thumb *thumb = NULL;
+       gboolean have_ethumbs = FALSE;
+       gdouble page_ratio;
+       gint dest_height;
+
+       /* getPage seems to want page + 1 for some reason; */
+       the_page = pdf_document->doc->getCatalog ()->getPage (page + 1);
+       the_page->getThumb(&the_thumb);
+
+       page_ratio = the_page->getHeight () / the_page->getWidth ();
+       dest_height = (gint) (width * page_ratio);
+
+
+       if (!(the_thumb.isNull () || the_thumb.isNone())) {
+               /* Build the thumbnail object */
+               thumb = new Thumb(pdf_document->doc->getXRef (),
+                                 &the_thumb);
+
+               have_ethumbs = thumb->ok();
+       }
+
+       if (have_ethumbs) {
+               guchar *data;
+               GdkPixbuf *tmp_pixbuf;
+
+               data = thumb->getPixbufData();
+               tmp_pixbuf = gdk_pixbuf_new_from_data (data,
+                                                      GDK_COLORSPACE_RGB,
+                                                      FALSE,
+                                                      8,
+                                                      thumb->getWidth (),
+                                                      thumb->getHeight (),
+                                                      thumb->getWidth () * 3,
+                                                      NULL, NULL);
+               /* FIXME: do we want to check that the thumb's size isn't ridiculous?? */
+               thumbnail = ev_document_misc_get_thumbnail_frame (-1, -1, tmp_pixbuf);
+               g_object_unref (tmp_pixbuf);
+       } else {
+               gdouble scale_factor;
+
+               scale_factor = (gdouble)width / the_page->getWidth ();
+
+               thumbnail = pdf_document_thumbnails_get_page_pixbuf (pdf_document,
+                                                                    scale_factor,
+                                                                    page,
+                                                                    width,
+                                                                    dest_height);
+       }
+
+       return thumbnail;
+}
+static void
+pdf_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
+{
+       iface->get_thumbnail = pdf_document_thumbnails_get_thumbnail;
+}
+
 
 static void
 pdf_document_init (PdfDocument *pdf_document)