From 7d44a19e5ca90c63fe183c43a8dd7454c2814fc8 Mon Sep 17 00:00:00 2001
From: =?utf-8?q?I=C3=B1igo=20Mart=C3=ADnez?= <inigomartinez@gmail.com>
Date: Sun, 3 Feb 2008 17:33:53 +0100
Subject: [PATCH] Preliminary PopplerAnnot support.
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit

Signed-off-by: Iñigo Martínez <inigomartinez@gmail.com>
---
 glib/Makefile.am       |    2 +
 glib/poppler-annot.cc  |  405 ++++++++++++++++++++++++++++++++++++++++++++++++
 glib/poppler-annot.h   |   95 +++++++++++
 glib/poppler-private.h |    8 +
 glib/poppler.h         |    2 +
 5 files changed, 512 insertions(+), 0 deletions(-)
 create mode 100644 glib/poppler-annot.cc
 create mode 100644 glib/poppler-annot.h

diff --git a/glib/Makefile.am b/glib/Makefile.am
index d77b3ae..f4df44d 100644
--- a/glib/Makefile.am
+++ b/glib/Makefile.am
@@ -63,6 +63,7 @@ poppler_glib_public_headers =			\
 	poppler-page.h				\
 	poppler-attachment.h			\
 	poppler-form-field.h			\
+	poppler-annot.h				\
 	poppler.h
 
 poppler_glib_includedir = $(includedir)/poppler/glib
@@ -80,6 +81,7 @@ libpoppler_glib_la_SOURCES =			\
 	poppler-page.cc				\
 	poppler-attachment.cc			\
 	poppler-form-field.cc			\
+	poppler-annot.cc			\
 	poppler.cc				\
 	poppler-private.h
 
diff --git a/glib/poppler-annot.cc b/glib/poppler-annot.cc
new file mode 100644
index 0000000..198bb71
--- /dev/null
+++ b/glib/poppler-annot.cc
@@ -0,0 +1,405 @@
+/* poppler-annot.cc: glib interface to poppler
+ *
+ * Copyright (C) 2007 Inigo Martinez <inigomartinez@gmail.com>
+ *
+ * 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, 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 "poppler.h"
+#include "poppler-private.h"
+
+typedef struct _PopplerAnnotClass PopplerAnnotClass;
+
+struct _PopplerAnnotClass
+{
+  GObjectClass parent_class;
+};
+
+G_DEFINE_TYPE (PopplerAnnot, poppler_annot, G_TYPE_OBJECT);
+
+static void
+poppler_annot_finalize (GObject *object)
+{
+  PopplerAnnot *poppler_annot = POPPLER_ANNOT (object);
+
+  if (poppler_annot->document)
+  {
+    g_object_unref (poppler_annot->document);
+    poppler_annot->document = NULL;
+  }
+  poppler_annot->annot = NULL;
+
+  G_OBJECT_CLASS (poppler_annot_parent_class)->finalize (object);
+}
+
+static void
+poppler_annot_init (PopplerAnnot *poppler_annot)
+{
+}
+
+static void
+poppler_annot_class_init (PopplerAnnotClass *klass)
+{
+  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
+
+  gobject_class->finalize = poppler_annot_finalize;
+}
+
+PopplerAnnot *
+_poppler_annot_new (PopplerDocument *document,
+                    Annot           *annot)
+{
+  PopplerAnnot *poppler_annot;
+
+  g_return_val_if_fail (POPPLER_IS_DOCUMENT (document), NULL);
+  g_return_val_if_fail (annot != NULL, NULL);
+
+  poppler_annot = POPPLER_ANNOT (g_object_new (POPPLER_TYPE_ANNOT, NULL));
+
+  poppler_annot->document = (PopplerDocument *) g_object_ref (document);
+  poppler_annot->annot = annot;
+  
+  return poppler_annot;
+}
+
+/* Public methods */
+/**
+ * poppler_annot_get_annot_type:
+ * @poppler_annot: a #PopplerAnnot
+ *
+ * Gets the type of @poppler_annot
+ *
+ * Return value: #PopplerAnnotType of @poppler_annot.
+ **/ 
+PopplerAnnotType
+poppler_annot_get_annot_type (PopplerAnnot *poppler_annot)
+{
+  g_return_val_if_fail (POPPLER_IS_ANNOT (poppler_annot), POPPLER_ANNOT_UNKNOWN);
+
+  switch (poppler_annot->annot->getType ())
+  {
+    case Annot::typeText:
+      return POPPLER_ANNOT_TEXT;
+    case Annot::typeLink:
+      return POPPLER_ANNOT_LINK;
+    case Annot::typeFreeText:
+      return POPPLER_ANNOT_FREE_TEXT;
+    case Annot::typeLine:
+      return POPPLER_ANNOT_LINE;
+    case Annot::typeSquare:
+      return POPPLER_ANNOT_SQUARE;
+    case Annot::typeCircle:
+      return POPPLER_ANNOT_CIRCLE;
+    case Annot::typePolygon:
+      return POPPLER_ANNOT_POLYGON;
+    case Annot::typePolyLine:
+      return POPPLER_ANNOT_POLY_LINE;
+    case Annot::typeHighlight:
+      return POPPLER_ANNOT_HIGHLIGHT;
+    case Annot::typeUnderline:
+      return POPPLER_ANNOT_UNDERLINE;
+    case Annot::typeSquiggly:
+      return POPPLER_ANNOT_SQUIGGLY;
+    case Annot::typeStrikeOut:
+      return POPPLER_ANNOT_STRIKE_OUT;
+    case Annot::typeStamp:
+      return POPPLER_ANNOT_STAMP;
+    case Annot::typeCaret:
+      return POPPLER_ANNOT_CARET;
+    case Annot::typeInk:
+      return POPPLER_ANNOT_INK;
+    case Annot::typePopup:
+      return POPPLER_ANNOT_POPUP;
+    case Annot::typeFileAttachment:
+      return POPPLER_ANNOT_FILE_ATTACHMENT;
+    case Annot::typeSound:
+      return POPPLER_ANNOT_SOUND;
+    case Annot::typeMovie:
+      return POPPLER_ANNOT_MOVIE;
+    case Annot::typeWidget:
+      return POPPLER_ANNOT_WIDGET;
+    case Annot::typeScreen:
+      return POPPLER_ANNOT_SCREEN;
+    case Annot::typePrinterMark:
+      return POPPLER_ANNOT_PRINTER_MARK;
+    case Annot::typeTrapNet:
+      return POPPLER_ANNOT_TRAP_NET;
+    case Annot::typeWatermark:
+      return POPPLER_ANNOT_WATERMARK;
+    case Annot::type3D:
+      return POPPLER_ANNOT_3D;
+    default:
+      g_warning ("Unsupported Annot Type");
+  }
+
+  return POPPLER_ANNOT_UNKNOWN;
+}
+
+/**
+ * poppler_annot_get_rect:
+ * @poppler_annot: a #PopplerAnnot
+ *
+ * Gets the rect of @poppler_annot
+ *
+ * Return value: a new allocated #PopplerRectangle of @poppler_annot. It must be
+ *               freed with g_free() when done.
+ **/
+PopplerRectangle *
+poppler_annot_get_rect (PopplerAnnot *poppler_annot)
+{
+  PDFRectangle *rect;
+  PopplerRectangle *poppler_rect;
+
+  g_return_val_if_fail (POPPLER_IS_ANNOT (poppler_annot), NULL);
+
+  rect = poppler_annot->annot->getRect ();
+  poppler_rect = g_new (PopplerRectangle, 1);
+  poppler_rect->x1 = rect->x1;
+  poppler_rect->y1 = rect->y1;
+  poppler_rect->x2 = rect->x2;
+  poppler_rect->y2 = rect->y2;
+
+  return poppler_rect; 
+}
+
+/**
+ * poppler_annot_get_contents:
+ * @poppler_annot: a #PopplerAnnot
+ *
+ * Retrieves the contents of @poppler_annot.
+ *
+ * Return value: a new allocated string with the contents of @poppler_annot. It
+ *               must be freed with g_free() when done.
+ **/
+gchar *
+poppler_annot_get_contents (PopplerAnnot *poppler_annot)
+{
+  GooString *contents;
+
+  g_return_val_if_fail (POPPLER_IS_ANNOT (poppler_annot), NULL);
+
+  contents = poppler_annot->annot->getContents ();
+
+  return contents ? _poppler_goo_string_to_utf8 (contents) : NULL;
+}
+
+/**
+ * poppler_annot_get_name:
+ * @poppler_annot: a #PopplerAnnot
+ *
+ * Retrieves the name of @poppler_annot.
+ *
+ * Return value: a new allocated string with the name of @poppler_annot. It must
+ *               be freed with g_free() when done.
+ **/
+gchar *
+poppler_annot_get_name (PopplerAnnot *poppler_annot)
+{
+  GooString *name;
+
+  g_return_val_if_fail (POPPLER_IS_ANNOT (poppler_annot), NULL);
+
+  name = poppler_annot->annot->getName ();
+
+  return name ? _poppler_goo_string_to_utf8 (name) : NULL;
+}
+
+/**
+ * poppler_annot_get_modified:
+ * @poppler_annot: a #PopplerAnnot
+ *
+ * Retrieves the last modification data of @poppler_annot.
+ *
+ * Return value: a new allocated string with the last modification data of
+ *               @poppler_annot. It must be freed with g_free() when done.
+ **/
+gchar *
+poppler_annot_get_modified (PopplerAnnot *poppler_annot)
+{
+  GooString *text;
+
+  g_return_val_if_fail (POPPLER_IS_ANNOT (poppler_annot), NULL);
+
+  text = poppler_annot->annot->getModified ();
+
+  return text ? _poppler_goo_string_to_utf8 (text) : NULL;
+}
+
+/**
+ * poppler_annot_get_flags
+ * @poppler_annot: a #PopplerAnnot
+ *
+ * Retrieves the flag field specifying various characteristics of the
+ * @poppler_annot.
+ *
+ * Return value: the flag field of @poppler_annot.
+ **/
+guint32
+poppler_annot_get_flags (PopplerAnnot *poppler_annot)
+{
+  g_return_val_if_fail (POPPLER_IS_ANNOT (poppler_annot), 0);
+
+  return poppler_annot->annot->getFlags ();
+}
+
+/**
+ * poppler_annot_get_appear_state:
+ * @poppler_annot: a #PopplerAnnot
+ *
+ * Retrieves the appearance state of @poppler_annot.
+ *
+ * Return value: a new allocated string with the appearance state of
+ *               @poppler_annot. It must be freed with g_free() when done.
+ **/
+gchar *
+poppler_annot_get_appear_state (PopplerAnnot *poppler_annot)
+{
+  GooString *name;
+
+  g_return_val_if_fail (POPPLER_IS_ANNOT (poppler_annot), NULL);
+
+  name = poppler_annot->annot->getAppearState ();
+
+  return name ? _poppler_goo_string_to_utf8 (name) : NULL;
+}
+
+/**
+ * poppler_annot_get_tree_key:
+ * @poppler_annot: a #PopplerAnnot
+ *
+ * Retrieves the integer key of the annotation's entry in the structural parent
+ * tree of @poppler_annot.
+ *
+ * Return value: the integer key of @poppler_annot.
+ **/
+gint
+poppler_annot_get_tree_key (PopplerAnnot *poppler_annot)
+{
+  g_return_val_if_fail (POPPLER_IS_ANNOT (poppler_annot), 0);
+
+  return poppler_annot->annot->getTreeKey ();
+}
+
+/**
+ * poppler_annot_get_border_type:
+ * @poppler_annot: a #PopplerAnnot
+ *
+ * Retrieves the type of the border of @poppler_annot.
+ *
+ * Return value: the #PopplerAnnotBorderType of the border of @poppler_annot.
+ **/
+PopplerAnnotBorderType
+poppler_annot_get_border_type (PopplerAnnot *poppler_annot)
+{
+  AnnotBorder *border;
+
+  g_return_val_if_fail (POPPLER_IS_ANNOT (poppler_annot), POPPLER_ANNOT_BORDER_UNKNOWN);
+
+  if ((border = poppler_annot->annot->getBorder ())) {
+    if (static_cast<AnnotBorderArray *>(border))
+      return POPPLER_ANNOT_BORDER_ARRAY;
+    if (static_cast<AnnotBorderBS *>(border))
+      return POPPLER_ANNOT_BORDER_BS;
+
+    g_warning ("Unsupported Annot Border");
+  }
+
+  return POPPLER_ANNOT_BORDER_UNKNOWN;
+}
+
+/**
+ * poppler_annot_get_border_width:
+ * @poppler_annot: a #PopplerAnnot
+ *
+ * Retrieves the width of the border of @poppler_annot.
+ *
+ * Return value: the width of the border of @poppler_annot.
+ **/
+gdouble
+poppler_annot_get_border_width (PopplerAnnot *poppler_annot)
+{
+  AnnotBorder *border;
+
+  g_return_val_if_fail (POPPLER_IS_ANNOT (poppler_annot), 0);
+
+  if ((border = poppler_annot->annot->getBorder ()))
+    return border->getWidth ();
+
+  return 0;
+}
+
+/**
+ * poppler_annot_get_border_dash:
+ * @poppler_annot: a #PopplerAnnot
+ *
+ * Retrieves the border dash of @poppler_annot.
+ *
+ * Return value: a new allocated #GArray with the dash values of the border of
+ *               @poppler_annot. It must be freed with g_array_free() when done.
+ **/
+GArray *
+poppler_annot_get_border_dash (PopplerAnnot *poppler_annot)
+{
+  AnnotBorder *border;
+
+  g_return_val_if_fail (POPPLER_IS_ANNOT (poppler_annot), NULL);
+
+  if ((border = poppler_annot->annot->getBorder ())) {
+    GArray *garray;
+
+    garray = g_array_new (FALSE, FALSE, sizeof (gdouble));
+    g_array_append_vals (garray, border->getDash (), border->getDashLength ());
+
+    return garray;
+  }
+
+  return NULL;
+}
+
+/**
+ * poppler_annot_get_border_style:
+ * @poppler_annot: a #PopplerAnnot
+ *
+ * Gets the border style of @poppler_annot
+ *
+ * Return value: #PopplerAnnotBorderStyle of the border of @poppler_annot.
+ **/ 
+PopplerAnnotBorderStyle
+poppler_annot_get_border_style (PopplerAnnot *poppler_annot)
+{
+  AnnotBorder *border;
+
+  g_return_val_if_fail (POPPLER_IS_ANNOT (poppler_annot), POPPLER_ANNOT_BORDER_SOLID);
+
+  if ((border = poppler_annot->annot->getBorder ())) {
+    switch (border->getStyle ())
+    {
+      case AnnotBorder::borderSolid:
+        return POPPLER_ANNOT_BORDER_SOLID;
+      case AnnotBorder::borderBeveled:
+        return POPPLER_ANNOT_BORDER_BEVELED;
+      case AnnotBorder::borderDashed:
+        return POPPLER_ANNOT_BORDER_DASHED;
+      case AnnotBorder::borderInset:
+        return POPPLER_ANNOT_BORDER_INSET;
+      case AnnotBorder::borderUnderlined:
+        return POPPLER_ANNOT_BORDER_UNDERLINED;
+      default:
+        g_warning ("Unsupported Annot Border Style");
+    }
+  }
+
+  return POPPLER_ANNOT_BORDER_SOLID;
+}
diff --git a/glib/poppler-annot.h b/glib/poppler-annot.h
new file mode 100644
index 0000000..f90f21b
--- /dev/null
+++ b/glib/poppler-annot.h
@@ -0,0 +1,95 @@
+/* poppler-annot.h: glib interface to poppler
+ *
+ * Copyright (C) 2007 Inigo Martinez <inigomartinez@gmail.com>
+ *
+ * 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, 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 __POPPLER_ANNOT_H__
+#define __POPPLER_ANNOT_H__
+
+#include <glib-object.h>
+#include "poppler.h"
+
+G_BEGIN_DECLS
+
+#define POPPLER_TYPE_ANNOT             (poppler_annot_get_type ())
+#define POPPLER_ANNOT(obj)             (G_TYPE_CHECK_INSTANCE_CAST ((obj), POPPLER_TYPE_ANNOT, PopplerAnnot))
+#define POPPLER_IS_ANNOT(obj)          (G_TYPE_CHECK_INSTANCE_TYPE ((obj), POPPLER_TYPE_ANNOT))
+
+typedef enum
+{
+  POPPLER_ANNOT_UNKNOWN,
+  POPPLER_ANNOT_TEXT,
+  POPPLER_ANNOT_LINK,
+  POPPLER_ANNOT_FREE_TEXT,
+  POPPLER_ANNOT_LINE,
+  POPPLER_ANNOT_SQUARE,
+  POPPLER_ANNOT_CIRCLE,
+  POPPLER_ANNOT_POLYGON,
+  POPPLER_ANNOT_POLY_LINE,
+  POPPLER_ANNOT_HIGHLIGHT,
+  POPPLER_ANNOT_UNDERLINE,
+  POPPLER_ANNOT_SQUIGGLY,
+  POPPLER_ANNOT_STRIKE_OUT,
+  POPPLER_ANNOT_STAMP,
+  POPPLER_ANNOT_CARET,
+  POPPLER_ANNOT_INK,
+  POPPLER_ANNOT_POPUP,
+  POPPLER_ANNOT_FILE_ATTACHMENT,
+  POPPLER_ANNOT_SOUND,
+  POPPLER_ANNOT_MOVIE,
+  POPPLER_ANNOT_WIDGET,
+  POPPLER_ANNOT_SCREEN,
+  POPPLER_ANNOT_PRINTER_MARK,
+  POPPLER_ANNOT_TRAP_NET,
+  POPPLER_ANNOT_WATERMARK,
+  POPPLER_ANNOT_3D
+} PopplerAnnotType;
+
+typedef enum
+{
+  POPPLER_ANNOT_BORDER_UNKNOWN,
+  POPPLER_ANNOT_BORDER_BS,
+  POPPLER_ANNOT_BORDER_ARRAY
+} PopplerAnnotBorderType;
+
+typedef enum
+{
+  POPPLER_ANNOT_BORDER_SOLID,
+  POPPLER_ANNOT_BORDER_DASHED,
+  POPPLER_ANNOT_BORDER_BEVELED,
+  POPPLER_ANNOT_BORDER_INSET,
+  POPPLER_ANNOT_BORDER_UNDERLINED
+} PopplerAnnotBorderStyle;
+
+GType                    poppler_annot_get_type         (void) G_GNUC_CONST;
+
+PopplerAnnotType         poppler_annot_get_annot_type   (PopplerAnnot *poppler_annot);
+PopplerRectangle        *poppler_annot_get_rect         (PopplerAnnot *poppler_annot);
+gchar                   *poppler_annot_get_contents     (PopplerAnnot *poppler_annot);
+gchar                   *poppler_annot_get_name         (PopplerAnnot *poppler_annot);
+gchar                   *poppler_annot_get_modified     (PopplerAnnot *poppler_annot);
+guint32                  poppler_annot_get_flags        (PopplerAnnot *poppler_annot);
+gchar                   *poppler_annot_get_appear_state (PopplerAnnot *poppler_annot);
+gint                     poppler_annot_get_tree_key     (PopplerAnnot *poppler_annot);
+PopplerAnnotBorderType   poppler_annot_get_border_type  (PopplerAnnot *poppler_annot);
+gdouble                  poppler_annot_get_border_width (PopplerAnnot *poppler_annot);
+GArray                  *poppler_annot_get_border_dash  (PopplerAnnot *poppler_annot);
+PopplerAnnotBorderStyle  poppler_annot_get_border_style (PopplerAnnot *poppler_annot);
+
+G_END_DECLS
+
+#endif /* __POPPLER_ANNOT_H__ */
diff --git a/glib/poppler-private.h b/glib/poppler-private.h
index 74601a3..34437a6 100644
--- a/glib/poppler-private.h
+++ b/glib/poppler-private.h
@@ -70,6 +70,12 @@ struct _PopplerFormField
   FormWidget *widget;
 };
 
+struct _PopplerAnnot
+{
+  GObject parent_instance;
+  PopplerDocument *document;
+  Annot *annot;
+};
 
 PopplerPage   *_poppler_page_new   (PopplerDocument *document,
 				    Page            *page,
@@ -83,6 +89,8 @@ PopplerFormField *_poppler_form_field_new (PopplerDocument *document,
 					   FormWidget      *field);
 PopplerAttachment *_poppler_attachment_new (PopplerDocument *document,
 					    EmbFile         *file);
+PopplerAnnot  *_poppler_annot_new     (PopplerDocument *document,
+				    Annot           *annot);
 
 char *_poppler_goo_string_to_utf8(GooString *s);
 gboolean _poppler_convert_pdf_date_to_gtime (GooString *date,
diff --git a/glib/poppler.h b/glib/poppler.h
index 8a94198..b04bffa 100644
--- a/glib/poppler.h
+++ b/glib/poppler.h
@@ -92,6 +92,7 @@ typedef union  _PopplerAction           PopplerAction;
 typedef struct _PopplerDest             PopplerDest;
 typedef struct _PopplerFormField        PopplerFormField;
 typedef struct _PopplerAttachment       PopplerAttachment;
+typedef struct _PopplerAnnot            PopplerAnnot;
 
 typedef enum
 {
@@ -112,5 +113,6 @@ G_END_DECLS
 #include "poppler-form-field.h"
 #include "poppler-enums.h"
 #include "poppler-attachment.h"
+#include "poppler-annot.h"
 
 #endif /* __POPPLER_GLIB_H__ */
-- 
1.5.3.7

