]> www.fi.muni.cz Git - evince.git/blobdiff - backend/pdf/ev-poppler.cc
Updated Brazilian Portuguese translation.
[evince.git] / backend / pdf / ev-poppler.cc
index a9a0a8555e1763311d4300c8a6be6b86dc386862..fb2a5ba5d8399697f08fa231e3692b39477894bc 100644 (file)
 #include "ev-document-transition.h"
 #include "ev-document-forms.h"
 #include "ev-document-layers.h"
+#include "ev-document-print.h"
 #include "ev-document-annotations.h"
 #include "ev-selection.h"
 #include "ev-transition-effect.h"
 #include "ev-attachment.h"
 #include "ev-image.h"
 
+#include <libxml/tree.h>
+#include <libxml/parser.h>
+#include <libxml/xpath.h>
+#include <libxml/xpathInternals.h>
+
 #if (defined (HAVE_POPPLER_PAGE_RENDER)) && (defined (HAVE_CAIRO_PDF) || defined (HAVE_CAIRO_PS))
 #define HAVE_CAIRO_PRINT
 #endif
@@ -113,6 +119,9 @@ static void pdf_document_document_images_iface_init      (EvDocumentImagesIface
 static void pdf_document_document_forms_iface_init       (EvDocumentFormsIface       *iface);
 static void pdf_document_document_fonts_iface_init       (EvDocumentFontsIface       *iface);
 static void pdf_document_document_layers_iface_init      (EvDocumentLayersIface      *iface);
+#ifdef HAVE_POPPLER_PAGE_RENDER
+static void pdf_document_document_print_iface_init       (EvDocumentPrintIface       *iface);
+#endif
 static void pdf_document_document_annotations_iface_init (EvDocumentAnnotationsIface *iface);
 static void pdf_document_find_iface_init                 (EvDocumentFindIface        *iface);
 static void pdf_document_file_exporter_iface_init        (EvFileExporterIface        *iface);
@@ -147,6 +156,10 @@ EV_BACKEND_REGISTER_WITH_CODE (PdfDocument, pdf_document,
                                                                 pdf_document_document_fonts_iface_init);
                                 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_LAYERS,
                                                                 pdf_document_document_layers_iface_init);
+#ifdef HAVE_POPPLER_PAGE_RENDER
+                                EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_PRINT,
+                                                                pdf_document_document_print_iface_init);
+#endif
                                 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_ANNOTATIONS,
                                                                 pdf_document_document_annotations_iface_init);
                                 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_FIND,
@@ -565,6 +578,94 @@ pdf_document_set_password (EvDocumentSecurity *document_security,
        document->password = g_strdup (password);
 }
 
+
+/* reference:
+http://www.pdfa.org/lib/exe/fetch.php?id=pdfa%3Aen%3Atechdoc&cache=cache&media=pdfa:techdoc:tn0001_pdfa-1_and_namespaces_2008-03-18.pdf */
+static char *
+pdf_document_get_format_from_metadata (const char *metadata)
+{
+       xmlDocPtr doc;
+       xmlXPathContextPtr xpathCtx;
+       xmlXPathObjectPtr xpathObj;
+       xmlChar *part = NULL;
+       xmlChar *conf = NULL;
+       char *result = NULL;
+       int i;
+
+       doc = xmlParseMemory (metadata, strlen (metadata));
+       if (doc == NULL)
+               return NULL;      /* invalid xml metadata */
+
+       xpathCtx = xmlXPathNewContext (doc);
+       if (xpathCtx == NULL) {
+               xmlFreeDoc (doc);
+               return NULL;      /* invalid xpath context */
+       }
+
+       /* add pdf/a namespaces */
+       xmlXPathRegisterNs (xpathCtx, BAD_CAST "x", BAD_CAST "adobe:ns:meta/");
+       xmlXPathRegisterNs (xpathCtx, BAD_CAST "rdf", BAD_CAST "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
+       xmlXPathRegisterNs (xpathCtx, BAD_CAST "pdfaid", BAD_CAST "http://www.aiim.org/pdfa/ns/id/");
+
+       /* reads pdf/a part */
+       /* first syntax: child node */
+       xpathObj = xmlXPathEvalExpression (BAD_CAST "/x:xmpmeta/rdf:RDF/rdf:Description/pdfaid:part", xpathCtx);
+       if (xpathObj != NULL) {
+               if (xpathObj->nodesetval != NULL && xpathObj->nodesetval->nodeNr != 0)
+                       part = xmlNodeGetContent (xpathObj->nodesetval->nodeTab[0]);
+
+               xmlXPathFreeObject (xpathObj);
+       }
+       if (part == NULL) {
+               /* second syntax: attribute */
+               xpathObj = xmlXPathEvalExpression (BAD_CAST "/x:xmpmeta/rdf:RDF/rdf:Description/@pdfaid:part", xpathCtx);
+               if (xpathObj != NULL) {
+                       if (xpathObj->nodesetval != NULL && xpathObj->nodesetval->nodeNr != 0)
+                               part = xmlNodeGetContent (xpathObj->nodesetval->nodeTab[0]);
+
+                       xmlXPathFreeObject (xpathObj);
+               }
+       }
+
+       /* reads pdf/a conformance */
+       /* first syntax: child node */
+       xpathObj = xmlXPathEvalExpression (BAD_CAST "/x:xmpmeta/rdf:RDF/rdf:Description/pdfaid:conformance", xpathCtx);
+       if (xpathObj != NULL) {
+               if (xpathObj->nodesetval != NULL && xpathObj->nodesetval->nodeNr != 0)
+                       conf = xmlNodeGetContent (xpathObj->nodesetval->nodeTab[0]);
+
+               xmlXPathFreeObject (xpathObj);
+       }
+       if (conf == NULL) {
+               /* second syntax: attribute */
+               xpathObj = xmlXPathEvalExpression (BAD_CAST "/x:xmpmeta/rdf:RDF/rdf:Description/@pdfaid:conformance", xpathCtx);
+               if (xpathObj != NULL) {
+                       if (xpathObj->nodesetval != NULL && xpathObj->nodesetval->nodeNr != 0)
+                               conf = xmlNodeGetContent (xpathObj->nodesetval->nodeTab[0]);
+
+                       xmlXPathFreeObject (xpathObj);
+               }
+       }
+
+       if (part != NULL && conf != NULL) {
+               /* makes conf lowercase */
+               for (i = 0; conf[i]; i++)
+                       conf[i] = g_ascii_tolower (conf[i]);
+
+               /* return buffer */
+               result = g_strdup_printf ("PDF/A - %s%s", part, conf);
+       }
+
+       /* Cleanup */
+       xmlFree (part);
+       xmlFree (conf);
+       xmlXPathFreeContext (xpathCtx);
+       xmlFreeDoc (doc);
+
+       return result;
+}
+
+
 static EvDocumentInfo *
 pdf_document_get_info (EvDocument *document)
 {
@@ -574,6 +675,8 @@ pdf_document_get_info (EvDocument *document)
        PopplerViewerPreferences view_prefs;
        PopplerPermissions permissions;
        EvPage *page;
+       char *metadata;
+       char *fmt;
 
        info = g_new0 (EvDocumentInfo, 1);
 
@@ -610,8 +713,18 @@ pdf_document_get_info (EvDocument *document)
                      "creation-date", &(info->creation_date),
                      "mod-date", &(info->modified_date),
                      "linearized", &(info->linearized),
+                     "metadata", &metadata,
                      NULL);
 
+       if (metadata != NULL) {
+               fmt = pdf_document_get_format_from_metadata(metadata);
+               if (fmt != NULL) {
+                       g_free (info->format);
+                       info->format = fmt;
+               }
+               g_free (metadata);
+       }
+
        info->n_pages = ev_document_get_n_pages (document);
 
        if (info->n_pages > 0) {
@@ -902,9 +1015,11 @@ ev_link_dest_from_dest (PdfDocument *pdf_document,
                        g_object_unref (poppler_page);
                }
                        break;
-               case POPPLER_DEST_FIT:
+               case POPPLER_DEST_FITB:
+               case POPPLER_DEST_FIT:
                        ev_dest = ev_link_dest_new_fit (dest->page_num - 1);
                        break;
+               case POPPLER_DEST_FITBH:
                case POPPLER_DEST_FITH: {
                        PopplerPage *poppler_page;
                        double height;
@@ -918,6 +1033,7 @@ ev_link_dest_from_dest (PdfDocument *pdf_document,
                        g_object_unref (poppler_page);
                }
                        break;
+               case POPPLER_DEST_FITBV:
                case POPPLER_DEST_FITV:
                        ev_dest = ev_link_dest_new_fitv (dest->page_num - 1,
                                                         dest->left,
@@ -938,15 +1054,6 @@ ev_link_dest_from_dest (PdfDocument *pdf_document,
                        g_object_unref (poppler_page);
                }
                        break;
-               case POPPLER_DEST_FITB:
-                       unimplemented_dest = "POPPLER_DEST_FITB";
-                       break;
-               case POPPLER_DEST_FITBH:
-                       unimplemented_dest = "POPPLER_DEST_FITBH";
-                       break;
-               case POPPLER_DEST_FITBV:
-                       unimplemented_dest = "POPPLER_DEST_FITBV";
-                       break;
                case POPPLER_DEST_NAMED:
                        ev_dest = ev_link_dest_new_named (dest->named_dest);
                        break;
@@ -1366,9 +1473,9 @@ pdf_document_thumbnails_get_dimensions (EvDocumentThumbnails *document_thumbnail
        poppler_page_get_size (POPPLER_PAGE (rc->page->backend_page),
                               &page_width, &page_height);
        
-       *width = (gint) MAX (page_width * rc->scale, 1);
-       *height = (gint) MAX (page_height * rc->scale, 1);
-       
+       *width = MAX ((gint)(page_width * rc->scale + 0.5), 1);
+       *height = MAX ((gint)(page_height * rc->scale + 0.5), 1);
+
        if (rc->rotation == 90 || rc->rotation == 270) {
                gint  temp;
 
@@ -1713,6 +1820,25 @@ pdf_document_file_exporter_iface_init (EvFileExporterIface *iface)
        iface->get_capabilities = pdf_document_file_exporter_get_capabilities;
 }
 
+#ifdef HAVE_POPPLER_PAGE_RENDER
+/* EvDocumentPrint */
+static void
+pdf_document_print_print_page (EvDocumentPrint *document,
+                              EvPage          *page,
+                              cairo_t         *cr)
+{
+       PdfDocument *pdf_document = PDF_DOCUMENT (document);
+
+       poppler_page_render_for_printing (POPPLER_PAGE (page->backend_page), cr);
+}
+
+static void
+pdf_document_document_print_iface_init (EvDocumentPrintIface *iface)
+{
+       iface->print_page = pdf_document_print_print_page;
+}
+#endif /* HAVE_POPPLER_PAGE_RENDER */
+
 static void
 pdf_selection_render_selection (EvSelection      *selection,
                                EvRenderContext  *rc,