dotemacs

My Emacs configuration
git clone git://git.entf.net/dotemacs
Log | Files | Refs | LICENSE

poppler-hack.cc (3573B)


      1 // Copyright (C) 2013, 2014  Andreas Politz
      2 
      3 // This program is free software; you can redistribute it and/or modify
      4 // it under the terms of the GNU General Public License as published by
      5 // the Free Software Foundation, either version 3 of the License, or
      6 // (at your option) any later version.
      7 
      8 // This program is distributed in the hope that it will be useful,
      9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
     10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     11 // GNU General Public License for more details.
     12 
     13 // You should have received a copy of the GNU General Public License
     14 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
     15 
     16 #include <config.h>
     17 #include <PDFDocEncoding.h>
     18 #include <Annot.h>
     19 #include <glib.h>
     20 #include <glib-object.h>
     21 #include <poppler-features.h>
     22 
     23 extern "C"
     24 {
     25 
     26 GType poppler_annot_get_type (void) G_GNUC_CONST;
     27 GType poppler_annot_markup_get_type (void) G_GNUC_CONST;
     28 
     29 #define POPPLER_TYPE_ANNOT (poppler_annot_get_type ())
     30 #define POPPLER_ANNOT(obj) \
     31   (G_TYPE_CHECK_INSTANCE_CAST ((obj), POPPLER_TYPE_ANNOT, PopplerAnnot))
     32 #define POPPLER_IS_ANNOT_MARKUP(obj) \
     33   (G_TYPE_CHECK_INSTANCE_TYPE ((obj), POPPLER_TYPE_ANNOT_MARKUP))
     34 #define POPPLER_TYPE_ANNOT_MARKUP (poppler_annot_markup_get_type ())
     35 
     36 #if POPPLER_CHECK_VERSION(0,72,0)
     37 #define GET_CSTR c_str
     38 #else
     39 #define GET_CSTR getCString
     40 #endif
     41 
     42   struct PopplerAnnot
     43   {
     44     GObject  parent_instance;
     45     Annot   *annot;
     46   };
     47 
     48   struct PopplerAnnotMarkup
     49   {
     50     GObject  parent_instance;
     51   };
     52 
     53   struct PopplerRectangle
     54   {
     55     double x1;
     56     double y1;
     57     double x2;
     58     double y2;
     59   };
     60 
     61   // This function does not modify its argument s, but for
     62   // compatibility reasons (e.g. getLength in GooString.h before 2015)
     63   // with older poppler code, it can't be declared as such.
     64   char *_xpoppler_goo_string_to_utf8(/* const */ GooString *s)
     65   {
     66     char *result;
     67 
     68     if (! s)
     69       return NULL;
     70 
     71     if (s->hasUnicodeMarker()) {
     72       result = g_convert (s->GET_CSTR () + 2,
     73                           s->getLength () - 2,
     74                           "UTF-8", "UTF-16BE", NULL, NULL, NULL);
     75     } else {
     76       int len;
     77       gunichar *ucs4_temp;
     78       int i;
     79 
     80       len = s->getLength ();
     81       ucs4_temp = g_new (gunichar, len + 1);
     82       for (i = 0; i < len; ++i) {
     83         ucs4_temp[i] = pdfDocEncoding[(unsigned char)s->getChar(i)];
     84       }
     85       ucs4_temp[i] = 0;
     86 
     87       result = g_ucs4_to_utf8 (ucs4_temp, -1, NULL, NULL, NULL);
     88 
     89       g_free (ucs4_temp);
     90     }
     91 
     92     return result;
     93   }
     94 #ifdef HAVE_POPPLER_ANNOT_WRITE
     95   // Set the rectangle of an annotation.  It was first added in v0.26.
     96   void xpoppler_annot_set_rectangle (PopplerAnnot *a, PopplerRectangle *rectangle)
     97   {
     98     GooString *state = (GooString*) a->annot->getAppearState ();
     99     char *ustate = _xpoppler_goo_string_to_utf8 (state);
    100 
    101     a->annot->setRect (rectangle->x1, rectangle->y1,
    102                        rectangle->x2, rectangle->y2);
    103     a->annot->setAppearanceState (ustate);
    104     g_free (ustate);
    105   }
    106 #endif
    107   // This function is in the library, but the enforced date parsing is
    108   // incomplete (at least in some versions), because it ignores the
    109   // timezone.
    110   gchar *xpoppler_annot_markup_get_created (PopplerAnnotMarkup *poppler_annot)
    111   {
    112     AnnotMarkup *annot;
    113     GooString *text;
    114 
    115     g_return_val_if_fail (POPPLER_IS_ANNOT_MARKUP (poppler_annot), NULL);
    116 
    117     annot = static_cast<AnnotMarkup *>(POPPLER_ANNOT (poppler_annot)->annot);
    118     text = (GooString*) annot->getDate ();
    119 
    120     return text ? _xpoppler_goo_string_to_utf8 (text) : NULL;
    121   }
    122 }