[cairo-commit] 3 commits - src/cairo-pdf.h src/cairo-pdf-surface.c test/.gitignore test/Makefile.am test/ps-features.c

Carl Worth cworth at kemper.freedesktop.org
Wed May 3 12:43:24 PDT 2006


 src/cairo-pdf-surface.c |   90 ++++++++++++++++++++++++++++++++++++++++--------
 src/cairo-pdf.h         |    5 ++
 test/.gitignore         |    2 +
 test/Makefile.am        |    5 ++
 test/ps-features.c      |   11 +++++
 5 files changed, 97 insertions(+), 16 deletions(-)

New commits:
diff-tree c35bfffa1057cfe2aeca6fe681ea59aa628f5315 (from eb1b102e9a9e16cfc38948f8d37e08f263d10f7c)
Author: Carl Worth <cworth at cworth.org>
Date:   Wed May 3 12:39:45 2006 -0700

    Add new pdf-features test to exercise cairo_pdf_surface_set_size

diff --git a/test/.gitignore b/test/.gitignore
index bc4855c..eb16e4f 100644
--- a/test/.gitignore
+++ b/test/.gitignore
@@ -52,6 +52,8 @@ paint-with-alpha
 path-data
 pattern-get-type
 pdf2png
+pdf-features
+pdf-features.pdf
 png-flatten
 ps-features
 ps-features.ps
diff --git a/test/Makefile.am b/test/Makefile.am
index 5f1e7f0..4c17064 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -83,6 +83,10 @@ if CAIRO_HAS_SVG_SURFACE
 TESTS += svg-surface svg-clip
 endif
 
+if CAIRO_HAS_PDF_SURFACE
+TESTS += pdf-features
+endif
+
 if CAIRO_HAS_PS_SURFACE
 TESTS += ps-features
 endif
@@ -331,6 +335,7 @@ CLEANFILES =					\
 	*.log					\
 	multi-page.ps				\
 	multi-page.pdf				\
+	pdf-features.pdf			\
 	ps-features.ps				\
 	svg-surface.svg				\
 	svg-clip.svg
diff-tree eb1b102e9a9e16cfc38948f8d37e08f263d10f7c (from 5448278d6e9c7452f855f8b262295818b27c5a11)
Author: Carl Worth <cworth at cworth.org>
Date:   Wed May 3 12:39:23 2006 -0700

    PDF: Add new cairo_pdf_surface_set_size for doing per-page size changes

diff --git a/src/cairo-pdf-surface.c b/src/cairo-pdf-surface.c
index 4d557aa..d06e91a 100644
--- a/src/cairo-pdf-surface.c
+++ b/src/cairo-pdf-surface.c
@@ -362,9 +362,32 @@ _cairo_surface_is_pdf (cairo_surface_t *
     return surface->backend == &cairo_pdf_surface_backend;
 }
 
+/* If the abstract_surface is a paginated surface, and that paginated
+ * surface's target is a pdf_surface, then set pdf_surface to that
+ * target. Otherwise return CAIRO_STATUS_SURFACE_TYPE_MISMATCH.
+ */
+static cairo_status_t
+_extract_pdf_surface (cairo_surface_t		 *surface,
+		      cairo_pdf_surface_t	**pdf_surface)
+{
+    cairo_surface_t *target;
+
+    if (! _cairo_surface_is_paginated (surface))
+	return CAIRO_STATUS_SURFACE_TYPE_MISMATCH;
+
+    target = _cairo_paginated_surface_get_target (surface);
+
+    if (! _cairo_surface_is_pdf (target))
+	return CAIRO_STATUS_SURFACE_TYPE_MISMATCH;
+
+    *pdf_surface = (cairo_pdf_surface_t *) target;
+
+    return CAIRO_STATUS_SUCCESS;
+}
+
 /**
  * cairo_pdf_surface_set_dpi:
- * @surface: a postscript cairo_surface_t
+ * @surface: a PDF cairo_surface_t
  * @x_dpi: horizontal dpi
  * @y_dpi: vertical dpi
  * 
@@ -379,25 +402,50 @@ cairo_pdf_surface_set_dpi (cairo_surface
 			   double		x_dpi,
 			   double		y_dpi)
 {
-    cairo_surface_t *target;
     cairo_pdf_surface_t *pdf_surface;
+    cairo_status_t status;
 
-    if (! _cairo_surface_is_paginated (surface)) {
-	_cairo_error (CAIRO_STATUS_SURFACE_TYPE_MISMATCH);
+    status = _extract_pdf_surface (surface, &pdf_surface);
+    if (status) {
+	_cairo_surface_set_error (surface, CAIRO_STATUS_SURFACE_TYPE_MISMATCH);
 	return;
     }
 
-    target = _cairo_paginated_surface_get_target (surface);
+    pdf_surface->document->x_dpi = x_dpi;    
+    pdf_surface->document->y_dpi = y_dpi;    
+}
 
-    if (! _cairo_surface_is_pdf (target)) {
-	_cairo_error (CAIRO_STATUS_SURFACE_TYPE_MISMATCH);
+/**
+ * cairo_pdf_surface_set_size:
+ * @surface: a PDF cairo_surface_t
+ * @width_in_points: new surface width, in points (1 point == 1/72.0 inch)
+ * @height_in_points: new surface height, in points (1 point == 1/72.0 inch)
+ * 
+ * Changes the size of a PDF surface for the current (and
+ * subsequent) pages.
+ *
+ * This function should only be called before any drawing operations
+ * have been performed on the current page. The simplest way to do
+ * this is to call this function immediately after creating the
+ * surface or immediately after completing a page with either
+ * cairo_show_page() or cairo_copy_page().
+ **/
+void
+cairo_pdf_surface_set_size (cairo_surface_t	*surface,
+			    double		 width_in_points,
+			    double		 height_in_points)
+{
+    cairo_pdf_surface_t *pdf_surface;
+    cairo_status_t status;
+
+    status = _extract_pdf_surface (surface, &pdf_surface);
+    if (status) {
+	_cairo_surface_set_error (surface, CAIRO_STATUS_SURFACE_TYPE_MISMATCH);
 	return;
     }
 
-    pdf_surface = (cairo_pdf_surface_t *) target;
-
-    pdf_surface->document->x_dpi = x_dpi;    
-    pdf_surface->document->y_dpi = y_dpi;    
+    pdf_surface->width = width_in_points;
+    pdf_surface->height = height_in_points;
 }
 
 static cairo_surface_t *
@@ -581,7 +629,7 @@ _cairo_pdf_surface_ensure_stream (cairo_
 	if (_cairo_array_num_elements (&surface->streams) == 1)
 	    _cairo_output_stream_printf (output,
 					 "1 0 0 -1 0 %f cm\r\n",
-					 document->height);
+					 surface->height);
     }
 }
 
@@ -1819,11 +1867,21 @@ _cairo_pdf_document_add_page (cairo_pdf_
     _cairo_output_stream_printf (output,
 				 "%d 0 obj\r\n"
 				 "<< /Type /Page\r\n"
-				 "   /Parent %d 0 R\r\n"
-				 "   /Contents [",
+				 "   /Parent %d 0 R\r\n",
 				 page_id,
 				 document->pages_id);
 
+    if (surface->width != document->width ||
+	surface->height != document->height)
+    {
+	_cairo_output_stream_printf (output,
+				     "   /MediaBox [ 0 0 %f %f ]\r\n",
+				     surface->width,
+				     surface->height);
+    }
+
+    _cairo_output_stream_printf (output,
+				 "   /Contents [");
     num_streams = _cairo_array_num_elements (&surface->streams);
     for (i = 0; i < num_streams; i++) {
 	_cairo_array_copy_element (&surface->streams, i, &stream);	
@@ -1831,9 +1889,11 @@ _cairo_pdf_document_add_page (cairo_pdf_
 				     " %d 0 R",
 				     stream->id);
     }
+    _cairo_output_stream_printf (output,
+				 " ]\r\n");
+
 
     _cairo_output_stream_printf (output,
-				 " ]\r\n"
 				 "   /Resources <<\r\n");
 
     num_resources =  _cairo_array_num_elements (&surface->fonts);
diff --git a/src/cairo-pdf.h b/src/cairo-pdf.h
index abcab0f..bbbe9bd 100644
--- a/src/cairo-pdf.h
+++ b/src/cairo-pdf.h
@@ -59,6 +59,11 @@ cairo_pdf_surface_set_dpi (cairo_surface
 			   double		x_dpi,
 			   double		y_dpi);
 
+void
+cairo_pdf_surface_set_size (cairo_surface_t	*surface,
+			    double		 width_in_points,
+			    double		 height_in_points);
+
 CAIRO_END_DECLS
 
 #else  /* CAIRO_HAS_PDF_SURFACE */
diff-tree 5448278d6e9c7452f855f8b262295818b27c5a11 (from a4fc0c2e2c9ce52750f43dabc2be5daeb8aee9c4)
Author: Carl Worth <cworth at cworth.org>
Date:   Wed May 3 12:38:49 2006 -0700

    ps-features: Minor touchups (missing include, typo in error message)

diff --git a/test/ps-features.c b/test/ps-features.c
index 4f5affc..577d706 100644
--- a/test/ps-features.c
+++ b/test/ps-features.c
@@ -26,6 +26,9 @@
 #include <stdio.h>
 #include <cairo.h>
 #include <cairo-ps.h>
+#if HAVE_FCFINI
+#include <fontconfig/fontconfig.h>
+#endif
 
 #include "cairo-test.h"
 
@@ -34,6 +37,9 @@
  * Currently, this test exercises the following function calls:
  *
  *	cairo_ps_surface_set_size
+ *	cairo_ps_surface_dsc_comment
+ *	cairo_ps_surface_dsc_begin_setup
+ *	cairo_ps_surface_dsc_begin_page_setup
  */
 
 #define INCHES_TO_POINTS(in) ((in) * 72.0)
@@ -141,7 +147,7 @@ main (void)
     cairo_surface_destroy (surface);
 
     if (status) {
-	cairo_test_log ("Failed to create pdf surface for file %s: %s\n",
+	cairo_test_log ("Failed to create ps surface for file %s: %s\n",
 			filename, cairo_status_to_string (status));
 	return CAIRO_TEST_FAILURE;
     }
@@ -149,7 +155,10 @@ main (void)
     printf ("ps-features: Please check %s to ensure it looks/prints correctly.\n", filename);
 
     cairo_debug_reset_static_data ();
+
+#if HAVE_FCFINI
     FcFini ();
+#endif
 
     return CAIRO_TEST_SUCCESS;
 }


More information about the cairo-commit mailing list