[poppler] 7 commits - glib/demo glib/Makefile.am glib/poppler-annot.cc glib/poppler-attachment.cc glib/poppler-date.cc glib/poppler-date.h glib/poppler-document.cc glib/poppler.h glib/poppler-private.h

Carlos Garcia Campos carlosgc at kemper.freedesktop.org
Tue Apr 21 11:06:01 PDT 2009


 glib/Makefile.am           |    2 +
 glib/demo/annots.c         |   69 +++++++++------------------------------------
 glib/demo/info.cc          |   27 +----------------
 glib/demo/utils.c          |    2 -
 glib/demo/utils.h          |    2 -
 glib/poppler-annot.cc      |   26 +++++++++-------
 glib/poppler-attachment.cc |    4 +-
 glib/poppler-date.cc       |   66 +++++++++++++++++++++++++++++++++++++++++++
 glib/poppler-date.h        |   30 +++++++++++++++++++
 glib/poppler-document.cc   |   45 +++++------------------------
 glib/poppler-private.h     |    2 -
 glib/poppler.h             |    1 
 12 files changed, 142 insertions(+), 134 deletions(-)

New commits:
commit 0750c6502faeabff571f5730b567097e793dca64
Author: Carlos Garcia Campos <carlosgc at gnome.org>
Date:   Tue Apr 21 20:04:27 2009 +0200

    [glib-demo] Fix dates handling in annots demo

diff --git a/glib/demo/annots.c b/glib/demo/annots.c
index fb3ffcf..338f5dd 100644
--- a/glib/demo/annots.c
+++ b/glib/demo/annots.c
@@ -187,64 +187,18 @@ gchar *
 get_markup_date (PopplerAnnotMarkup *poppler_annot)
 {
     GDate *date;
+    struct tm t;
+    time_t timet;
 
-    if ((date = poppler_annot_markup_get_date (poppler_annot))) {
-        gchar *text;
-        
-        text = g_strdup_printf ("D: %d:", g_date_get_day (date));
-        
-        switch (g_date_get_month (date))
-        {
-          case G_DATE_BAD_MONTH:
-            text = g_strdup_printf ("%s,M: Bad", text);
-            break;
-          case G_DATE_JANUARY:
-            text = g_strdup_printf ("%s,M: January", text);
-            break;
-          case G_DATE_FEBRUARY:
-            text = g_strdup_printf ("%s,M: February", text);
-            break;
-          case G_DATE_MARCH:
-            text = g_strdup_printf ("%s,M: March", text);
-            break;
-          case G_DATE_APRIL:
-            text = g_strdup_printf ("%s,M: April", text);
-            break;
-          case G_DATE_MAY:
-            text = g_strdup_printf ("%s,M: May", text);
-            break;
-          case G_DATE_JUNE:
-            text = g_strdup_printf ("%s,M: June", text);
-            break;
-          case G_DATE_JULY:
-            text = g_strdup_printf ("%s,M: July", text);
-            break;
-          case G_DATE_AUGUST:
-            text = g_strdup_printf ("%s,M: August", text);
-            break;
-          case G_DATE_SEPTEMBER:
-            text = g_strdup_printf ("%s,M: September", text);
-            break;
-          case G_DATE_OCTOBER:
-            text = g_strdup_printf ("%s,M: October", text);
-            break;
-          case G_DATE_NOVEMBER:
-            text = g_strdup_printf ("%s,M: November", text);
-            break;
-          case G_DATE_DECEMBER:
-            text = g_strdup_printf ("%s,M: December", text);
-            break;
-          default:
-            text = g_strdup_printf ("%s,M: Unknown", text);
-            break;
-        }
-        text = g_strdup_printf ("%s,Y: %d", text, g_date_get_year (date));
+    date = poppler_annot_markup_get_date (poppler_annot);
+    if (!date)
+	    return NULL;
 
-        g_free (date);
-        return text;
-    }
+    g_date_to_struct_tm (date, &t);
+    g_date_free (date);
 
-    return NULL;
+    timet = mktime (&t);
+    return timet == (time_t) - 1 ? NULL : pgd_format_date (timet);
 }
 
 const gchar *
@@ -417,6 +371,7 @@ pgd_annot_view_set_annot (GtkWidget    *annot_view,
     GEnumValue *enum_value;
     gint        row = 0;
     gchar      *text, *warning;
+    time_t      timet;
 
     alignment = gtk_bin_get_child (GTK_BIN (annot_view));
     if (alignment) {
@@ -444,6 +399,10 @@ pgd_annot_view_set_annot (GtkWidget    *annot_view,
     g_free (text);
 
     text = poppler_annot_get_modified (annot);
+    if (poppler_date_parse (text, &timet)) {
+	    g_free (text);
+	    text = pgd_format_date (timet);
+    }
     pgd_table_add_property (GTK_TABLE (table), "<b>Modified:</b>", text, &row);
     g_free (text);
 
commit fe73bf9771e8294af4c8a11ec4c2891ff8f8d859
Author: Carlos Garcia Campos <carlosgc at gnome.org>
Date:   Tue Apr 21 20:02:25 2009 +0200

    [glib-demo] GTime is deprecated, use time_t instead

diff --git a/glib/demo/utils.c b/glib/demo/utils.c
index 5fd0a10..d1fa8ff 100644
--- a/glib/demo/utils.c
+++ b/glib/demo/utils.c
@@ -238,7 +238,7 @@ pgd_action_view_set_action (GtkWidget     *action_view,
 }
 
 gchar *
-pgd_format_date (GTime utime)
+pgd_format_date (time_t utime)
 {
 	time_t time = (time_t) utime;
 	char s[256];
diff --git a/glib/demo/utils.h b/glib/demo/utils.h
index a757482..72c3ef3 100644
--- a/glib/demo/utils.h
+++ b/glib/demo/utils.h
@@ -36,7 +36,7 @@ void       pgd_table_add_property_with_value_widget (GtkTable        *table,
 GtkWidget *pgd_action_view_new                      (PopplerDocument *document);
 void       pgd_action_view_set_action               (GtkWidget       *action_view,
 						     PopplerAction   *action);
-gchar     *pgd_format_date                          (GTime            utime);
+gchar     *pgd_format_date                          (time_t           utime);
 
 G_END_DECLS
 
commit 6d468cfa0cb89760e5d6cca43521cde6d99aa52e
Author: Carlos Garcia Campos <carlosgc at gnome.org>
Date:   Tue Apr 21 20:00:20 2009 +0200

    [glib-demo] Use format_date from utils and remove duplicated code

diff --git a/glib/demo/info.cc b/glib/demo/info.cc
index 574b0f5..87be8ea 100644
--- a/glib/demo/info.cc
+++ b/glib/demo/info.cc
@@ -22,29 +22,6 @@
 #include "info.h"
 #include "utils.h"
 
-static gchar *
-poppler_format_date (GTime utime)
-{
-	time_t time = (time_t) utime;
-	char s[256];
-	const char *fmt_hack = "%c";
-	size_t len;
-
-#ifdef HAVE_LOCALTIME_R
-	struct tm t;
-	if (time == 0 || !localtime_r (&time, &t)) return NULL;
-	len = strftime (s, sizeof (s), fmt_hack, &t);
-#else
-	struct tm *t;
-	if (time == 0 || !(t = localtime (&time)) ) return NULL;
-	len = strftime (s, sizeof (s), fmt_hack, t);
-#endif
-
-	if (len == 0 || s[0] == '\0') return NULL;
-
-	return g_locale_to_utf8 (s, -1, NULL, NULL, NULL);
-}
-
 static void
 pgd_info_add_permissions (GtkTable           *table,
 			  PopplerPermissions  permissions,
@@ -220,11 +197,11 @@ pgd_info_create_widget (PopplerDocument *document)
 	pgd_table_add_property (GTK_TABLE (table), "<b>Linearized:</b>", linearized, &row);
 	g_free (linearized);
 	
-	str = poppler_format_date (creation_date);
+	str = pgd_format_date (creation_date);
 	pgd_table_add_property (GTK_TABLE (table), "<b>Creation Date:</b>", str, &row);
 	g_free (str);
 
-	str = poppler_format_date (mod_date);
+	str = pgd_format_date (mod_date);
 	pgd_table_add_property (GTK_TABLE (table), "<b>Modification Date:</b>", str, &row);
 	g_free (str);
 
commit e5f5ea01d0da4c4d877b93755523dc2a7f2ec049
Author: Carlos Garcia Campos <carlosgc at gnome.org>
Date:   Tue Apr 21 19:57:11 2009 +0200

    [glib] Fix poppler_annot_markup_get_date()
    
    Date field in Markup annots is also a PDF format date string, so it
    should be parsed and the resulting time_t used to create a correct
    GDate.

diff --git a/glib/poppler-annot.cc b/glib/poppler-annot.cc
index 2cb00cd..58a2478 100644
--- a/glib/poppler-annot.cc
+++ b/glib/poppler-annot.cc
@@ -455,22 +455,22 @@ poppler_annot_markup_get_date (PopplerAnnotMarkup *poppler_annot)
 {
   AnnotMarkup *annot;
   GooString *annot_date;
+  time_t timet;
 
   g_return_val_if_fail (POPPLER_IS_ANNOT_MARKUP (poppler_annot), NULL);
 
   annot = static_cast<AnnotMarkup *>(POPPLER_ANNOT (poppler_annot)->annot);
+  annot_date = annot->getDate ();
+  if (!annot_date)
+    return NULL;
 
-  if ((annot_date = annot->getDate ())) {
-    GDateYear year = g_ascii_digit_value (annot_date->getChar(2)) * 1000 +
-                     g_ascii_digit_value (annot_date->getChar(3)) * 100 +
-                     g_ascii_digit_value (annot_date->getChar(4)) * 10 +
-                     g_ascii_digit_value (annot_date->getChar(5));
-    GDateMonth month = (GDateMonth) (g_ascii_digit_value (annot_date->getChar(6)) * 10 +
-                                     g_ascii_digit_value (annot_date->getChar(7)));
-    GDateDay day = g_ascii_digit_value (annot_date->getChar(8)) * 10 +
-                   g_ascii_digit_value (annot_date->getChar(9));
-
-    return g_date_new_dmy (day, month, year);
+  if (_poppler_convert_pdf_date_to_gtime (annot_date, &timet)) {
+    GDate *date;
+
+    date = g_date_new ();
+    g_date_set_time_t (date, timet);
+
+    return date;
   }
 
   return NULL;
commit 876ad1bcc8a7b2f37656bf15cee7eb888fd46ed4
Author: Carlos Garcia Campos <carlosgc at gnome.org>
Date:   Tue Apr 21 19:36:33 2009 +0200

    [glib] Add info about the return value in poppler_annot_get_modified() doc

diff --git a/glib/poppler-annot.cc b/glib/poppler-annot.cc
index 5e5a4aa..2cb00cd 100644
--- a/glib/poppler-annot.cc
+++ b/glib/poppler-annot.cc
@@ -301,7 +301,9 @@ poppler_annot_get_name (PopplerAnnot *poppler_annot)
  * poppler_annot_get_modified:
  * @poppler_annot: a #PopplerAnnot
  *
- * Retrieves the last modification data of @poppler_annot.
+ * Retrieves the last modification data of @poppler_annot. The returned
+ * string will be either a PDF format date or a text string.
+ * See also #poppler_date_parse()
  *
  * Return value: a new allocated string with the last modification data of
  *               @poppler_annot. It must be freed with g_free() when done.
commit 8c54a15e1715721ee7af7e82c90b8dda3689c65f
Author: Carlos Garcia Campos <carlosgc at gnome.org>
Date:   Tue Apr 21 19:35:14 2009 +0200

    [glib] Fix a typo in poppler_date_parse documentation

diff --git a/glib/poppler-date.cc b/glib/poppler-date.cc
index f29a819..2444f93 100644
--- a/glib/poppler-date.cc
+++ b/glib/poppler-date.cc
@@ -27,7 +27,7 @@
  * @timet: an uninitialized #time_t
  *
  * Parses a PDF format date string and converts it to a #time_t. Returns #FALSE
- * if the parsing file or the input string is not a valid PDF format date string
+ * if the parsing fails or the input string is not a valid PDF format date string
  *
  * Return value: #TRUE, if @timet was set
  **/
commit 39d09fa237d06fa93b02eb916d2c0242c4e8fe85
Author: Carlos Garcia Campos <carlosgc at gnome.org>
Date:   Tue Apr 21 19:29:32 2009 +0200

    [glib] Add poppler_date_parse to parse PDF format date strings
    
    We need to make this public because the field M in the Annot dictionary
    might be a Date string (in PDF date format) or a text string. According
    to the PDF spec: "The preferred format is a date string as described in
    Section 3.8.3, “Dates,” but viewer applications should be prepared to
    accept and display a string in any format". The only way to know whether
    it's a PDF date string or not, is by trying to parse it. For this reason
    poppler_annot_get_modified() returns a gchar * instead of a time_t. So,
    viewers should try to parse the string in order to convert it to a
    time_t, and if it fails to parse, use the date string as provided by the
    document.

diff --git a/glib/Makefile.am b/glib/Makefile.am
index fe75c67..1548ce1 100644
--- a/glib/Makefile.am
+++ b/glib/Makefile.am
@@ -45,6 +45,7 @@ EXTRA_DIST = \
 
 poppler_glib_public_headers =			\
 	poppler-action.h			\
+	poppler-date.h				\
 	poppler-document.h			\
 	poppler-page.h				\
 	poppler-attachment.h			\
@@ -64,6 +65,7 @@ lib_LTLIBRARIES = libpoppler-glib.la
 libpoppler_glib_la_SOURCES =			\
 	$(BUILT_SOURCES)			\
 	poppler-action.cc			\
+	poppler-date.cc				\
 	poppler-document.cc			\
 	poppler-page.cc				\
 	poppler-attachment.cc			\
diff --git a/glib/poppler-attachment.cc b/glib/poppler-attachment.cc
index 1c3eef0..2e2d12d 100644
--- a/glib/poppler-attachment.cc
+++ b/glib/poppler-attachment.cc
@@ -94,8 +94,8 @@ _poppler_attachment_new (PopplerDocument *document,
 
   attachment->size = emb_file->size ();
   
-  _poppler_convert_pdf_date_to_gtime (emb_file->createDate (), &attachment->ctime);
-  _poppler_convert_pdf_date_to_gtime (emb_file->modDate (), &attachment->mtime);
+  _poppler_convert_pdf_date_to_gtime (emb_file->createDate (), (time_t *)&attachment->ctime);
+  _poppler_convert_pdf_date_to_gtime (emb_file->modDate (), (time_t *)&attachment->mtime);
 
   attachment->checksum = g_string_new_len (emb_file->checksum ()->getCString (),
 					   emb_file->checksum ()->getLength ());
diff --git a/glib/poppler-date.cc b/glib/poppler-date.cc
new file mode 100644
index 0000000..f29a819
--- /dev/null
+++ b/glib/poppler-date.cc
@@ -0,0 +1,66 @@
+/* poppler-date.cc: glib interface to poppler
+ *
+ * Copyright (C) 2009 Carlos Garcia Campos <carlosgc at gnome.org>
+ *
+ * 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 <DateInfo.h>
+
+#include "poppler-date.h"
+
+/**
+ * poppler_date_parse:
+ * @date: string to parse
+ * @timet: an uninitialized #time_t
+ *
+ * Parses a PDF format date string and converts it to a #time_t. Returns #FALSE
+ * if the parsing file or the input string is not a valid PDF format date string
+ *
+ * Return value: #TRUE, if @timet was set
+ **/
+gboolean
+poppler_date_parse (const gchar *date,
+		    time_t      *timet)
+{
+  gint year, mon, day, hour, min, sec, tz_hour, tz_minute;
+  gchar tz;
+  struct tm time;
+  time_t retval;
+  
+  /* See PDF Reference 1.3, Section 3.8.2 for PDF Date representation */
+  // TODO do something with the timezone information
+  if (!parseDateString (date, &year, &mon, &day, &hour, &min, &sec, &tz, &tz_hour, &tz_minute))
+    return FALSE;
+	
+  time.tm_year = year - 1900;
+  time.tm_mon = mon - 1;
+  time.tm_mday = day;
+  time.tm_hour = hour;
+  time.tm_min = min;
+  time.tm_sec = sec;
+  time.tm_wday = -1;
+  time.tm_yday = -1;
+  time.tm_isdst = -1; /* 0 = DST off, 1 = DST on, -1 = don't know */
+ 
+  /* compute tm_wday and tm_yday and check date */
+  retval = mktime (&time);
+  if (retval == (time_t) - 1)
+    return FALSE;
+    
+  *timet = retval;
+
+  return TRUE;	
+}
diff --git a/glib/poppler-date.h b/glib/poppler-date.h
new file mode 100644
index 0000000..2bb52b2
--- /dev/null
+++ b/glib/poppler-date.h
@@ -0,0 +1,30 @@
+/* poppler-date.h: glib interface to poppler
+ *
+ * Copyright (C) 2009 Carlos Garcia Campos <carlosgc at gnome.org>
+ *
+ * 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_DATE_H__
+#define __POPPLER_DATE_H__
+
+#include "poppler.h"
+
+G_BEGIN_DECLS
+gboolean poppler_date_parse (const gchar *date,
+			     time_t      *timet);
+G_END_DECLS
+
+#endif /* __POPPLER_DATE_H__ */
diff --git a/glib/poppler-document.cc b/glib/poppler-document.cc
index 3387bec..17eec51 100644
--- a/glib/poppler-document.cc
+++ b/glib/poppler-document.cc
@@ -30,7 +30,6 @@
 #include <Stream.h>
 #include <FontInfo.h>
 #include <PDFDocEncoding.h>
-#include <DateInfo.h>
 #include <OptionalContent.h>
 
 #include "poppler.h"
@@ -567,7 +566,7 @@ static void
 info_dict_get_date (Dict *info_dict, const gchar *key, GValue *value) 
 {
   Object obj;
-  GTime result;
+  time_t result;
 
   if (!info_dict->lookup ((gchar *)key, &obj)->isString ()) {
     obj.free ();
@@ -1895,13 +1894,10 @@ poppler_document_get_form_field (PopplerDocument *document,
 
 gboolean
 _poppler_convert_pdf_date_to_gtime (GooString *date,
-				    GTime     *gdate) 
+				    time_t    *gdate) 
 {
-  int year, mon, day, hour, min, sec, tz_hour, tz_minute;
-  char tz;
-  struct tm time;
-  gchar *date_string, *ds;
-  GTime result;
+  gchar *date_string;
+  gboolean retval;
 
   if (date->hasUnicodeMarker()) {
     date_string = g_convert (date->getCString () + 2,
@@ -1910,34 +1906,9 @@ _poppler_convert_pdf_date_to_gtime (GooString *date,
   } else {
     date_string = g_strndup (date->getCString (), date->getLength ());
   }
-  ds = date_string;
-  
-  /* See PDF Reference 1.3, Section 3.8.2 for PDF Date representation */
-  // TODO do something with the timezone information
-  if (!parseDateString(ds, &year, &mon, &day, &hour, &min, &sec, &tz, &tz_hour, &tz_minute)) {
-    g_free (ds);
-    return FALSE;
-  }
-	
-  time.tm_year = year - 1900;
-  time.tm_mon = mon - 1;
-  time.tm_mday = day;
-  time.tm_hour = hour;
-  time.tm_min = min;
-  time.tm_sec = sec;
-  time.tm_wday = -1;
-  time.tm_yday = -1;
-  time.tm_isdst = -1; /* 0 = DST off, 1 = DST on, -1 = don't know */
- 
-  /* compute tm_wday and tm_yday and check date */
-  result = mktime (&time);
-  if (result == (time_t) - 1) {
-    g_free (ds);
-    return FALSE;
-  }
-    
-  g_free (ds);
-  *gdate = result;
 
-  return TRUE;
+  retval = poppler_date_parse (date_string, gdate);
+  g_free (date_string);
+
+  return retval;
 }
diff --git a/glib/poppler-private.h b/glib/poppler-private.h
index 1cb1eac..e8ace14 100644
--- a/glib/poppler-private.h
+++ b/glib/poppler-private.h
@@ -111,7 +111,7 @@ PopplerAnnot      *_poppler_annot_free_text_new (Annot *annot);
 
 char *_poppler_goo_string_to_utf8(GooString *s);
 gboolean _poppler_convert_pdf_date_to_gtime (GooString *date,
-					     GTime     *gdate);
+					     time_t    *gdate);
 
 /*
  * A convenience macro for boxed type implementations, which defines a
diff --git a/glib/poppler.h b/glib/poppler.h
index d554093..b1a7730 100644
--- a/glib/poppler.h
+++ b/glib/poppler.h
@@ -125,5 +125,6 @@ G_END_DECLS
 #include "poppler-enums.h"
 #include "poppler-attachment.h"
 #include "poppler-annot.h"
+#include "poppler-date.h"
 
 #endif /* __POPPLER_GLIB_H__ */


More information about the poppler mailing list