[libgxps] Use gxps_value_get_double_positive() to parse page sizes



commit 3eb33debc7272a18d75718b86472c8f4bac79a75
Author: Carlos Garcia Campos <carlosgc gnome org>
Date:   Sun Nov 6 12:58:38 2011 +0100

    Use gxps_value_get_double_positive() to parse page sizes
    
    And report an error if Width or Height are missing or fail to parse
    since they are required attributes of FixedPage. Even though all XPS
    documents we have for testing use integer values for page sizes, the
    spec says with/height are real numbers greater or equal than 1.
    This commit breaks the API because gxps_document_get_page_size() and
    gxps_page_get_size() return gdouble instead of guint values now.

 libgxps/gxps-document.c      |   26 ++++++++++----------------
 libgxps/gxps-document.h      |    4 ++--
 libgxps/gxps-page.c          |   28 ++++++++++++++++++++--------
 libgxps/gxps-page.h          |    4 ++--
 test/test-gxps.c             |    2 +-
 tools/gxps-image-converter.c |    2 +-
 tools/gxps-print-converter.c |   10 +++++-----
 7 files changed, 41 insertions(+), 35 deletions(-)
---
diff --git a/libgxps/gxps-document.c b/libgxps/gxps-document.c
index c37b5d5..6fffb2c 100644
--- a/libgxps/gxps-document.c
+++ b/libgxps/gxps-document.c
@@ -75,13 +75,7 @@ G_DEFINE_TYPE_WITH_CODE (GXPSDocument, gxps_document, G_TYPE_OBJECT,
 static Page *
 page_new (void)
 {
-	Page *page;
-
-	page = g_slice_new0 (Page);
-	page->width = -1;
-	page->height = -1;
-
-	return page;
+	return g_slice_new0 (Page);
 }
 
 static void
@@ -114,19 +108,19 @@ fixed_doc_start_element (GMarkupParseContext  *context,
 	gint                i;
 
 	if (strcmp (element_name, "PageContent") == 0) {
-		gchar *source = NULL;
-		gint   width = -1, height = -1;
+		gchar  *source = NULL;
+		gdouble width = -1, height = -1;
 
 		for (i = 0; names[i]; i++) {
 			if (strcmp (names[i], "Source") == 0) {
 				source = gxps_resolve_relative_path (data->doc->priv->source,
 								     values[i]);
 			} else if (strcmp (names[i], "Width") == 0) {
-				if (!gxps_value_get_int (values[i], &width))
-					width = -1;
+				if (!gxps_value_get_double_positive (values[i], &width))
+					width = 0;
 			} else if (strcmp (names[i], "Height") == 0) {
-				if (!gxps_value_get_int (values[i], &height))
-					height = -1;
+				if (!gxps_value_get_double_positive (values[i], &height))
+					height = 0;
 			}
 		}
 
@@ -517,8 +511,8 @@ gxps_document_get_page (GXPSDocument *doc,
 gboolean
 gxps_document_get_page_size (GXPSDocument *doc,
 			     guint         n_page,
-			     guint        *width,
-			     guint        *height)
+			     gdouble      *width,
+			     gdouble      *height)
 {
 	Page *page;
 
@@ -526,7 +520,7 @@ gxps_document_get_page_size (GXPSDocument *doc,
 	g_return_val_if_fail (n_page < doc->priv->n_pages, FALSE);
 
 	page = doc->priv->pages[n_page];
-	if (page->width == -1 || page->height == -1)
+	if (page->width == 0 || page->height == 0)
 		return FALSE;
 
 	if (width)
diff --git a/libgxps/gxps-document.h b/libgxps/gxps-document.h
index ae2499e..a2aee20 100644
--- a/libgxps/gxps-document.h
+++ b/libgxps/gxps-document.h
@@ -68,8 +68,8 @@ GXPSPage              *gxps_document_get_page            (GXPSDocument *doc,
 							  GError      **error);
 gboolean               gxps_document_get_page_size       (GXPSDocument *doc,
 							  guint         n_page,
-							  guint        *width,
-							  guint        *height);
+							  gdouble      *width,
+							  gdouble      *height);
 gint                   gxps_document_get_page_for_anchor (GXPSDocument *doc,
 							  const gchar  *anchor);
 GXPSDocumentStructure *gxps_document_get_structure       (GXPSDocument *doc);
diff --git a/libgxps/gxps-page.c b/libgxps/gxps-page.c
index a45e783..6f70b6d 100644
--- a/libgxps/gxps-page.c
+++ b/libgxps/gxps-page.c
@@ -56,8 +56,8 @@ struct _GXPSPagePrivate {
 	gboolean     initialized;
 	GError      *init_error;
 
-	gint         width;
-	gint         height;
+	gdouble      width;
+	gdouble      height;
 	gchar       *lang;
 	gchar       *name;
 
@@ -137,11 +137,23 @@ fixed_page_start_element (GMarkupParseContext  *context,
 	if (strcmp (element_name, "FixedPage") == 0) {
 		for (i = 0; names[i] != NULL; i++) {
 			if (strcmp (names[i], "Width") == 0) {
-				if (!gxps_value_get_int (values[i], &page->priv->width))
-					page->priv->width = -1;
+				if (!gxps_value_get_double_positive (values[i], &page->priv->width)) {
+                                        gxps_parse_error (context,
+                                                          page->priv->source,
+                                                          G_MARKUP_ERROR_MISSING_ATTRIBUTE,
+                                                          element_name, "Width",
+                                                          NULL, error);
+                                        return;
+                                }
 			} else if (strcmp (names[i], "Height") == 0) {
-				if (!gxps_value_get_int (values[i], &page->priv->height))
-					page->priv->height = -1;
+				if (!gxps_value_get_double_positive (values[i], &page->priv->height)) {
+                                        gxps_parse_error (context,
+                                                          page->priv->source,
+                                                          G_MARKUP_ERROR_MISSING_ATTRIBUTE,
+                                                          element_name, "Height",
+                                                          NULL, error);
+                                        return;
+                                }
 			} else if (strcmp (names[i], "xml:lang") == 0) {
 				page->priv->lang = g_strdup (values[i]);
 			} else if (strcmp (names[i], "ContentBox") == 0) {
@@ -3975,8 +3987,8 @@ _gxps_page_new (GXPSArchive *zip,
  */
 void
 gxps_page_get_size (GXPSPage *page,
-		    guint    *width,
-		    guint    *height)
+		    gdouble  *width,
+		    gdouble  *height)
 {
 	g_return_if_fail (GXPS_IS_PAGE (page));
 
diff --git a/libgxps/gxps-page.h b/libgxps/gxps-page.h
index efaf7de..5bd6a5a 100644
--- a/libgxps/gxps-page.h
+++ b/libgxps/gxps-page.h
@@ -85,8 +85,8 @@ GType    gxps_page_get_type               (void) G_GNUC_CONST;
 GQuark   gxps_page_error_quark            (void) G_GNUC_CONST;
 
 void     gxps_page_get_size               (GXPSPage          *page,
-					   guint             *width,
-					   guint             *height);
+					   gdouble           *width,
+					   gdouble           *height);
 gboolean gxps_page_render                 (GXPSPage          *page,
 					   cairo_t           *cr,
 					   GError           **error);
diff --git a/test/test-gxps.c b/test/test-gxps.c
index acb9554..451e48c 100644
--- a/test/test-gxps.c
+++ b/test/test-gxps.c
@@ -39,7 +39,7 @@ page_changed_callback (GtkSpinButton *button,
 {
 	GXPSPage *xps_page;
 	gint      page;
-	guint     width, height;
+	gdouble   width, height;
 	cairo_t  *cr;
 	GError   *error = NULL;
 
diff --git a/tools/gxps-image-converter.c b/tools/gxps-image-converter.c
index b7ae18f..9084cbf 100644
--- a/tools/gxps-image-converter.c
+++ b/tools/gxps-image-converter.c
@@ -58,7 +58,7 @@ gxps_converter_image_converter_begin_page (GXPSConverter *converter,
                                            guint          n_page)
 {
         GXPSImageConverter *image_converter = GXPS_IMAGE_CONVERTER (converter);
-        guint               page_width, page_height;
+        gdouble             page_width, page_height;
         gdouble             output_width, output_height;
         cairo_t            *cr;
 
diff --git a/tools/gxps-print-converter.c b/tools/gxps-print-converter.c
index 38b18d7..722f764 100644
--- a/tools/gxps-print-converter.c
+++ b/tools/gxps-print-converter.c
@@ -167,7 +167,7 @@ gxps_converter_print_converter_begin_page (GXPSConverter *converter,
                                            guint          n_page)
 {
         GXPSPrintConverter *print_converter = GXPS_PRINT_CONVERTER (converter);
-        guint               page_width, page_height;
+        gdouble             page_width, page_height;
         gdouble             cropped_width, cropped_height;
         gdouble             output_width, output_height;
         cairo_matrix_t      matrix;
@@ -183,7 +183,7 @@ gxps_converter_print_converter_begin_page (GXPSConverter *converter,
 
         gxps_page_get_size (page, &page_width, &page_height);
         gxps_converter_get_crop_size (converter,
-                                      (gdouble)page_width, (gdouble)page_height,
+                                      page_width, page_height,
                                       &cropped_width, &cropped_height);
         _gxps_converter_print_get_output_size (print_converter, page,
                                                &output_width, &output_height);
@@ -262,17 +262,17 @@ _gxps_converter_print_get_output_size (GXPSPrintConverter *converter,
                                        gdouble            *output_width,
                                        gdouble            *output_height)
 {
-        guint page_width, page_height;
+        gdouble page_width, page_height;
 
         gxps_page_get_size (page, &page_width, &page_height);
 
         if (output_width) {
                 *output_width = converter->paper_width == 0 ?
-                        (gdouble)page_width : (gdouble)converter->paper_width;
+                        page_width : converter->paper_width;
         }
 
         if (output_height) {
                 *output_height = converter->paper_height == 0 ?
-                        (gdouble)page_height : (gdouble)converter->paper_height;
+                        page_height : converter->paper_height;
         }
 }



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]