[cairo-commit] 2 commits - src/cairo-xml-surface.c test/Makefile.am test/ps-eps.c test/ps-eps.ref.eps test/ps-eps.ref.png

Adrian Johnson ajohnson at kemper.freedesktop.org
Tue Nov 23 04:54:26 PST 2010


 src/cairo-xml-surface.c |    2 
 test/Makefile.am        |    1 
 test/ps-eps.c           |  338 ++++++++++++++++++++++++++++++++++++++++++++----
 test/ps-eps.ref.eps     |   84 -----------
 test/ps-eps.ref.png     |binary
 5 files changed, 312 insertions(+), 113 deletions(-)

New commits:
commit 12b1cbb07e5e23dac9f84793924080171deda37f
Author: Adrian Johnson <ajohnson at redneon.com>
Date:   Tue Nov 23 23:20:25 2010 +1030

    Fix xml-surface use of load_truetype_font

diff --git a/src/cairo-xml-surface.c b/src/cairo-xml-surface.c
index b323d03..5f0f13b 100644
--- a/src/cairo-xml-surface.c
+++ b/src/cairo-xml-surface.c
@@ -872,7 +872,7 @@ _cairo_xml_emit_type42_font (cairo_xml_t *xml,
     if (unlikely (buf == NULL))
 	return _cairo_error (CAIRO_STATUS_NO_MEMORY);
 
-    status = backend->load_truetype_table (scaled_font, 0, 0, buf, NULL);
+    status = backend->load_truetype_table (scaled_font, 0, 0, buf, &size);
     if (unlikely (status)) {
 	free (buf);
 	return status;
commit e2dcbfd895463d674d55c826ba72030f5f6daa91
Author: Adrian Johnson <ajohnson at redneon.com>
Date:   Tue Nov 23 00:12:10 2010 +1030

    Automate error checking for ps-eps test

diff --git a/test/Makefile.am b/test/Makefile.am
index 6984ce1..01e5914 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -1667,7 +1667,6 @@ CLEANFILES +=					\
 	ps-surface-source.out.ps		\
 	pdf-features.pdf			\
 	pdf-mime-data.out*			\
-	ps-eps.eps				\
 	ps-features.ps				\
 	svg-clip.svg				\
 	svg-surface.svg				\
diff --git a/test/ps-eps.c b/test/ps-eps.c
index 726cb46..33802f3 100644
--- a/test/ps-eps.c
+++ b/test/ps-eps.c
@@ -1,6 +1,7 @@
 /*
  * Copyright © 2006 Red Hat, Inc.
  * Copyright © 2009 Adrian Johnson
+ * Copyright © 2008 Chris Wilson
  *
  * Permission to use, copy, modify, distribute, and sell this software
  * and its documentation for any purpose is hereby granted without
@@ -22,59 +23,342 @@
  * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  *
  * Author: Carl D. Worth <cworth at cworth.org>
- * Author: Adrian Johnson <ajohnson at redneon.com>
+ *         Adrian Johnson <ajohnson at redneon.com>
+ *         Chris Wilson <chris at chris-wilson.co.uk>
  */
 
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
 #include <stdio.h>
+#include <stdlib.h>
 #include <math.h>
 #include <cairo.h>
 #include <cairo-ps.h>
 
+#ifdef HAVE_UNISTD_H
+#include <unistd.h>
+#include <errno.h>
+#endif
+#if HAVE_SYS_STAT_H
+#include <sys/stat.h>
+#endif
+
 #include "cairo-test.h"
+#include "buffer-diff.h"
 
 /* Test EPS output.
  */
 
-static cairo_test_status_t
-preamble (cairo_test_context_t *ctx)
+#define WIDTH 595
+#define HEIGHT 842
+
+/* Reference Bounding Box */
+#define LLX  95
+#define LLY 687
+#define URX 155
+#define URY 747
+
+static void
+_xunlink (const cairo_test_context_t *ctx, const char *pathname)
 {
-    cairo_surface_t *surface;
-    cairo_t *cr;
+    if (unlink (pathname) < 0 && errno != ENOENT) {
+	cairo_test_log (ctx, "Error: Cannot remove %s: %s\n",
+			pathname, strerror (errno));
+	exit (1);
+    }
+}
+
+static cairo_bool_t
+check_result (cairo_test_context_t *ctx,
+	      const cairo_boilerplate_target_t *target,
+	      const char *test_name,
+	      const char *base_name,
+	      cairo_surface_t *surface)
+{
+    const char *format;
+    char *ref_name;
+    char *png_name;
+    char *diff_name;
+    cairo_surface_t *test_image, *ref_image, *diff_image;
+    buffer_diff_result_t result;
     cairo_status_t status;
-    const char *filename;
+    cairo_bool_t ret;
 
-    if (! (cairo_test_is_target_enabled (ctx, "ps2") ||
-	   cairo_test_is_target_enabled (ctx, "ps3")))
-    {
-	return CAIRO_TEST_UNTESTED;
+    /* XXX log target, OUTPUT, REFERENCE, DIFFERENCE for index.html */
+
+    if (target->finish_surface != NULL) {
+	status = target->finish_surface (surface);
+	if (status) {
+	    cairo_test_log (ctx, "Error: Failed to finish surface: %s\n",
+		    cairo_status_to_string (status));
+	    cairo_surface_destroy (surface);
+	    return FALSE;
+	}
     }
 
-    filename = "ps-eps.out.eps";
+    xasprintf (&png_name,  "%s.out.png", base_name);
+    xasprintf (&diff_name, "%s.diff.png", base_name);
 
-    surface = cairo_ps_surface_create (filename, 595, 842);
-    cairo_ps_surface_set_eps (surface, TRUE);
-    cr = cairo_create (surface);
+    test_image = target->get_image_surface (surface, 0, WIDTH, HEIGHT);
+    if (cairo_surface_status (test_image)) {
+	cairo_test_log (ctx, "Error: Failed to extract page: %s\n",
+		        cairo_status_to_string (cairo_surface_status (test_image)));
+	cairo_surface_destroy (test_image);
+	free (png_name);
+	free (diff_name);
+	return FALSE;
+    }
 
-    cairo_new_sub_path (cr);
-    cairo_arc (cr, 100, 100, 25, 0, 2*M_PI);
-    cairo_set_line_width (cr, 10);
-    cairo_stroke (cr);
+    _xunlink (ctx, png_name);
+    status = cairo_surface_write_to_png (test_image, png_name);
+    if (status) {
+	cairo_test_log (ctx, "Error: Failed to write output image: %s\n",
+		cairo_status_to_string (status));
+	cairo_surface_destroy (test_image);
+	free (png_name);
+	free (diff_name);
+	return FALSE;
+    }
 
-    cairo_show_page (cr);
+    format = cairo_boilerplate_content_name (target->content);
+    ref_name = cairo_test_reference_filename (ctx,
+					      base_name,
+					      test_name,
+					      target->name,
+					      target->basename,
+					      format,
+					      CAIRO_TEST_REF_SUFFIX,
+					      CAIRO_TEST_PNG_EXTENSION);
+    if (ref_name == NULL) {
+	cairo_test_log (ctx, "Error: Cannot find reference image for %s\n",
+		        base_name);
+	cairo_surface_destroy (test_image);
+	free (png_name);
+	free (diff_name);
+	return FALSE;
+    }
 
-    status = cairo_status (cr);
+    ref_image = cairo_test_get_reference_image (ctx, ref_name,
+	    target->content == CAIRO_TEST_CONTENT_COLOR_ALPHA_FLATTENED);
+    if (cairo_surface_status (ref_image)) {
+	cairo_test_log (ctx, "Error: Cannot open reference image for %s: %s\n",
+		        ref_name,
+		cairo_status_to_string (cairo_surface_status (ref_image)));
+	cairo_surface_destroy (ref_image);
+	cairo_surface_destroy (test_image);
+	free (png_name);
+	free (diff_name);
+	free (ref_name);
+	return FALSE;
+    }
 
-    cairo_destroy (cr);
-    cairo_surface_destroy (surface);
+    diff_image = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
+	    WIDTH, HEIGHT);
 
+    ret = TRUE;
+    status = image_diff (ctx,
+	    test_image, ref_image, diff_image,
+	    &result);
+    _xunlink (ctx, diff_name);
     if (status) {
-	cairo_test_log (ctx, "Failed to create ps surface for file %s: %s\n",
-			filename, cairo_status_to_string (status));
-	return CAIRO_TEST_FAILURE;
+	cairo_test_log (ctx, "Error: Failed to compare images: %s\n",
+			cairo_status_to_string (status));
+	ret = FALSE;
+    } else if (image_diff_is_failure (&result, target->error_tolerance))
+    {
+	ret = FALSE;
+
+	status = cairo_surface_write_to_png (diff_image, diff_name);
+	if (status) {
+	    cairo_test_log (ctx, "Error: Failed to write differences image: %s\n",
+		    cairo_status_to_string (status));
+	}
+    }
+
+    cairo_surface_destroy (test_image);
+    cairo_surface_destroy (diff_image);
+    free (png_name);
+    free (diff_name);
+    free (ref_name);
+
+    return ret;
+}
+
+
+#define DOCUMENT_BBOX  "%%BoundingBox:"
+#define PAGE_BBOX      "%%PageBoundingBox:"
+
+static cairo_bool_t
+check_bbox (cairo_test_context_t *ctx,
+	    const char *base_name)
+{
+    char *filename;
+    FILE *f;
+    char buf[256];
+    cairo_bool_t bbox_pass, page_bbox_pass;
+    int llx, lly, urx, ury;
+    int ret;
+
+    xasprintf (&filename,  "%s.out.ps", base_name);
+    f = fopen (filename, "r");
+    if (!f) {
+	cairo_test_log (ctx, "Error: Cannot open EPS output: %s\n",
+		        base_name);
+	free (filename);
+	return FALSE;
+    }
+
+    bbox_pass = FALSE;
+    page_bbox_pass = FALSE;
+    while (!feof(f)) {
+	fgets (buf, sizeof(buf), f);
+
+	if (strncmp (buf, DOCUMENT_BBOX, strlen (DOCUMENT_BBOX)) == 0) {
+	    ret = sscanf (buf+strlen (DOCUMENT_BBOX), "%d %d %d %d", &llx, &lly, &urx, &ury);
+	    if (ret == 4 && llx == LLX && lly == LLY && urx == URX && ury == URY)
+		bbox_pass = TRUE;
+	}
+
+	if (strncmp (buf, PAGE_BBOX, strlen (PAGE_BBOX)) == 0) {
+	    ret = sscanf (buf+strlen (PAGE_BBOX), "%d %d %d %d", &llx, &lly, &urx, &ury);
+	    if (ret == 4 && llx == LLX && lly == LLY && urx == URX && ury == URY)
+		page_bbox_pass = TRUE;
+	}
+    }
+    fclose (f);
+
+    if (!bbox_pass || !page_bbox_pass) {
+	cairo_test_log (ctx, "Error: EPS Bounding Box does not match reference Bounding Box\n");
+	return FALSE;
+    }
+
+    free (filename);
+
+    return TRUE;
+}
+
+static cairo_bool_t
+_cairo_test_mkdir (const char *path)
+{
+#if ! HAVE_MKDIR
+    return FALSE;
+#elif HAVE_MKDIR == 1
+    if (mkdir (path) == 0)
+	return TRUE;
+#elif HAVE_MKDIR == 2
+    if (mkdir (path, 0770) == 0)
+	return TRUE;
+#else
+#error Bad value for HAVE_MKDIR
+#endif
+
+    return errno == EEXIST;
+}
+
+static cairo_test_status_t
+preamble (cairo_test_context_t *ctx)
+{
+    cairo_t *cr;
+    cairo_test_status_t ret = CAIRO_TEST_UNTESTED;
+    const char *path = _cairo_test_mkdir (CAIRO_TEST_OUTPUT_DIR) ? CAIRO_TEST_OUTPUT_DIR : ".";
+    unsigned int i;
+
+    for (i = 0; i < ctx->num_targets; i++) {
+	const cairo_boilerplate_target_t *target = ctx->targets_to_test[i];
+	cairo_surface_t *surface = NULL;
+	char *base_name;
+	void *closure;
+	const char *format;
+	cairo_status_t status;
+	cairo_bool_t pass;
+	char *test_name;
+
+	if (! cairo_test_is_target_enabled (ctx, target->name))
+	    continue;
+
+	format = cairo_boilerplate_content_name (target->content);
+	xasprintf (&test_name, "ps-eps");
+	xasprintf (&base_name, "%s/ps-eps.%s.%s",
+		   path, target->name, format);
+
+	surface = (target->create_surface) (base_name,
+					    target->content,
+					    WIDTH, HEIGHT,
+					    WIDTH, HEIGHT,
+					    CAIRO_BOILERPLATE_MODE_TEST,
+					    0,
+					    &closure);
+
+	if (surface == NULL) {
+	    free (base_name);
+	    free (test_name);
+	    continue;
+	}
+
+	cairo_ps_surface_set_eps (surface, TRUE);
+	if (!cairo_ps_surface_get_eps (surface)) {
+	    cairo_surface_destroy (surface);
+	    if (target->cleanup)
+		target->cleanup (closure);
+
+	    free (base_name);
+	    free (test_name);
+	    continue;
+	}
+	
+	cairo_test_log (ctx,
+			"Testing ps-eps with %s target\n",
+			target->name);
+	printf ("%s:\t", base_name);
+	fflush (stdout);
+
+	cairo_surface_set_device_offset (surface, 25, 25);
+	cr = cairo_create (surface);
+
+	cairo_new_sub_path (cr);
+	cairo_arc (cr, 100, 100, 25, 0, 2*M_PI);
+	cairo_set_line_width (cr, 10);
+	cairo_stroke (cr);
+
+	cairo_show_page (cr);
+
+	status = cairo_status (cr);
+	cairo_destroy (cr);
+
+	if (status) {
+	    cairo_test_log (ctx, "Error: Failed to create target surface: %s\n",
+			    cairo_status_to_string (status));
+	    pass = FALSE;
+	} else {
+	    pass = TRUE;
+	    /* extract the image and compare it to our reference */
+	    if (! check_result (ctx, target, test_name, base_name, surface))
+		pass = FALSE;
+
+	    /* check the bounding box of the EPS file and compare it to our reference */
+	    if (! check_bbox (ctx, base_name))
+		pass = FALSE;
+	}
+	cairo_surface_destroy (surface);
+	if (target->cleanup)
+	    target->cleanup (closure);
+
+	free (base_name);
+	free (test_name);
+
+	if (pass) {
+	    printf ("PASS\n");
+	    ret = CAIRO_TEST_SUCCESS;
+	} else {
+	    printf ("FAIL\n");
+	    ret = CAIRO_TEST_FAILURE;
+	}
+	fflush (stdout);
     }
 
-    printf ("ps-eps: Please check that %s looks/prints the same as ps-eps.ref.eps.\n", filename);
-    return CAIRO_TEST_SUCCESS;
+    return ret;
 }
 
 CAIRO_TEST (ps_eps,
diff --git a/test/ps-eps.ref.eps b/test/ps-eps.ref.eps
deleted file mode 100644
index e4fc1be..0000000
--- a/test/ps-eps.ref.eps
+++ /dev/null
@@ -1,84 +0,0 @@
-%!PS-Adobe-3.0 EPSF-3.0
-%%Creator: cairo 1.8.8 (http://cairographics.org)
-%%CreationDate: Thu Sep 24 23:15:35 2009
-%%Pages: 1
-%%BoundingBox: 70 712 130 772
-%%DocumentData: Clean7Bit
-%%LanguageLevel: 2
-%%EndComments
-%%BeginProlog
-/cairo_eps_state save def
-/dict_count countdictstack def
-/op_count count 1 sub def
-userdict begin
-/q { gsave } bind def
-/Q { grestore } bind def
-/cm { 6 array astore concat } bind def
-/w { setlinewidth } bind def
-/J { setlinecap } bind def
-/j { setlinejoin } bind def
-/M { setmiterlimit } bind def
-/d { setdash } bind def
-/m { moveto } bind def
-/l { lineto } bind def
-/c { curveto } bind def
-/h { closepath } bind def
-/re { exch dup neg 3 1 roll 5 3 roll moveto 0 rlineto
-      0 exch rlineto 0 rlineto closepath } bind def
-/S { stroke } bind def
-/f { fill } bind def
-/f* { eofill } bind def
-/B { fill stroke } bind def
-/B* { eofill stroke } bind def
-/n { newpath } bind def
-/W { clip } bind def
-/W* { eoclip } bind def
-/BT { } bind def
-/ET { } bind def
-/pdfmark where { pop globaldict /?pdfmark /exec load put }
-    { globaldict begin /?pdfmark /pop load def /pdfmark
-    /cleartomark load def end } ifelse
-/BDC { mark 3 1 roll /BDC pdfmark } bind def
-/EMC { mark /EMC pdfmark } bind def
-/cairo_store_point { /cairo_point_y exch def /cairo_point_x exch def } def
-/Tj { show currentpoint cairo_store_point } bind def
-/TJ {
-  {
-    dup
-    type /stringtype eq
-    { show } { -0.001 mul 0 cairo_font_matrix dtransform rmoveto } ifelse
-  } forall
-  currentpoint cairo_store_point
-} bind def
-/cairo_selectfont { cairo_font_matrix aload pop pop pop 0 0 6 array astore
-    cairo_font exch selectfont cairo_point_x cairo_point_y moveto } bind def
-/Tf { pop /cairo_font exch def /cairo_font_matrix where
-      { pop cairo_selectfont } if } bind def
-/Td { matrix translate cairo_font_matrix matrix concatmatrix dup
-      /cairo_font_matrix exch def dup 4 get exch 5 get cairo_store_point
-      /cairo_font where { pop cairo_selectfont } if } bind def
-/Tm { 2 copy 8 2 roll 6 array astore /cairo_font_matrix exch def
-      cairo_store_point /cairo_font where { pop cairo_selectfont } if } bind def
-/g { setgray } bind def
-/rg { setrgbcolor } bind def
-/d1 { setcachedevice } bind def
-%%EndProlog
-%%Page: 1 1
-%%BeginPageSetup
-%%PageBoundingBox: 70 712 130 772
-%%EndPageSetup
-q
-0 g
-10 w
-0 J
-0 j
-[] 0.0 d
-10 M 125 742 m 125 728.191 113.809 717 100 717 c 86.191 717 75 728.191 75 
-742 c 75 755.809 86.191 767 100 767 c 113.809 767 125 755.809 125 742 c S
-Q
-showpage
-%%Trailer
-count op_count sub {pop} repeat
-countdictstack dict_count sub {end} repeat
-cairo_eps_state restore
-%%EOF
diff --git a/test/ps-eps.ref.png b/test/ps-eps.ref.png
new file mode 100644
index 0000000..9aadb08
Binary files /dev/null and b/test/ps-eps.ref.png differ


More information about the cairo-commit mailing list