2006-06-14 19:15:50 -04:00
|
|
|
/*
|
|
|
|
|
* Copyright © 2006 Red Hat, Inc.
|
|
|
|
|
*
|
|
|
|
|
* Permission to use, copy, modify, distribute, and sell this software
|
|
|
|
|
* and its documentation for any purpose is hereby granted without
|
|
|
|
|
* fee, provided that the above copyright notice appear in all copies
|
|
|
|
|
* and that both that copyright notice and this permission notice
|
|
|
|
|
* appear in supporting documentation, and that the name of
|
|
|
|
|
* Red Hat, Inc. not be used in advertising or publicity pertaining to
|
|
|
|
|
* distribution of the software without specific, written prior
|
|
|
|
|
* permission. Red Hat, Inc. makes no representations about the
|
|
|
|
|
* suitability of this software for any purpose. It is provided "as
|
|
|
|
|
* is" without express or implied warranty.
|
|
|
|
|
*
|
|
|
|
|
* RED HAT, INC. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
|
|
|
|
|
* SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
|
|
|
|
* FITNESS, IN NO EVENT SHALL RED HAT, INC. BE LIABLE FOR ANY SPECIAL,
|
|
|
|
|
* INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
|
|
|
|
|
* RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
|
|
|
|
|
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
|
|
|
|
|
* IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
|
*
|
|
|
|
|
* Author: Kristian Høgsberg <krh@redhat.com>
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
|
|
#include <cairo.h>
|
|
|
|
|
|
|
|
|
|
#if CAIRO_HAS_PS_SURFACE
|
|
|
|
|
#include <cairo-ps.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#if CAIRO_HAS_PDF_SURFACE
|
|
|
|
|
#include <cairo-pdf.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#if CAIRO_HAS_SVG_SURFACE
|
|
|
|
|
#include <cairo-svg.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#include "cairo-test.h"
|
|
|
|
|
|
|
|
|
|
/* The main test suite doesn't test the *_create_for_stream
|
|
|
|
|
* constructors for the PDF, PS and SVG surface, so we do that here.
|
|
|
|
|
* We draw to an in-memory buffer using the stream constructor and
|
|
|
|
|
* compare the output to the contents of a file writting using the
|
|
|
|
|
* file constructor.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#define MAX_OUTPUT_SIZE 4096
|
|
|
|
|
|
|
|
|
|
#define WIDTH_IN_INCHES 3
|
|
|
|
|
#define HEIGHT_IN_INCHES 3
|
|
|
|
|
#define WIDTH_IN_POINTS (WIDTH_IN_INCHES * 72.0)
|
|
|
|
|
#define HEIGHT_IN_POINTS (HEIGHT_IN_INCHES * 72.0)
|
|
|
|
|
|
2006-07-11 22:19:39 -04:00
|
|
|
static cairo_test_status_t
|
|
|
|
|
draw (cairo_t *cr, int width, int height)
|
2006-06-14 19:15:50 -04:00
|
|
|
{
|
|
|
|
|
/* Just draw a rectangle. */
|
|
|
|
|
|
2006-07-11 22:19:39 -04:00
|
|
|
cairo_rectangle (cr, width / 10., height /10.,
|
|
|
|
|
width - 2 * width / 10.,
|
|
|
|
|
height - 2 * height /10.);
|
2006-06-14 19:15:50 -04:00
|
|
|
cairo_fill (cr);
|
|
|
|
|
|
|
|
|
|
cairo_show_page (cr);
|
2006-08-08 01:16:49 -07:00
|
|
|
|
|
|
|
|
return CAIRO_TEST_SUCCESS;
|
2006-07-11 22:19:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
draw_to (cairo_surface_t *surface)
|
|
|
|
|
{
|
|
|
|
|
cairo_t *cr;
|
|
|
|
|
|
|
|
|
|
cr = cairo_create (surface);
|
|
|
|
|
|
|
|
|
|
draw (cr, WIDTH_IN_POINTS, HEIGHT_IN_POINTS);
|
2006-06-14 19:15:50 -04:00
|
|
|
|
|
|
|
|
cairo_destroy (cr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
typedef struct _write_closure {
|
2008-08-11 21:12:45 +01:00
|
|
|
const cairo_test_context_t *ctx;
|
2006-06-14 19:15:50 -04:00
|
|
|
char buffer[MAX_OUTPUT_SIZE];
|
2006-08-08 01:16:49 -07:00
|
|
|
size_t index;
|
2006-06-14 19:15:50 -04:00
|
|
|
cairo_test_status_t status;
|
|
|
|
|
} write_closure_t;
|
|
|
|
|
|
2007-09-26 00:28:47 +01:00
|
|
|
static cairo_status_t
|
|
|
|
|
bad_write (void *closure,
|
|
|
|
|
const unsigned char *data,
|
|
|
|
|
unsigned int length)
|
|
|
|
|
{
|
|
|
|
|
return CAIRO_STATUS_WRITE_ERROR;
|
|
|
|
|
}
|
|
|
|
|
|
2006-06-14 19:15:50 -04:00
|
|
|
static cairo_status_t
|
|
|
|
|
test_write (void *closure,
|
|
|
|
|
const unsigned char *data,
|
|
|
|
|
unsigned int length)
|
|
|
|
|
{
|
|
|
|
|
write_closure_t *wc = closure;
|
|
|
|
|
|
|
|
|
|
if (wc->index + length >= sizeof wc->buffer) {
|
2008-08-11 21:12:45 +01:00
|
|
|
cairo_test_log (wc->ctx, "Error: out of bounds in write callback\n");
|
2006-06-14 19:15:50 -04:00
|
|
|
wc->status = CAIRO_TEST_FAILURE;
|
|
|
|
|
return CAIRO_STATUS_SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
memcpy (&wc->buffer[wc->index], data, length);
|
|
|
|
|
wc->index += length;
|
|
|
|
|
|
|
|
|
|
return CAIRO_STATUS_SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
typedef cairo_surface_t *
|
|
|
|
|
(*file_constructor_t) (const char *filename,
|
|
|
|
|
double width_in_points,
|
|
|
|
|
double height_in_points);
|
|
|
|
|
|
|
|
|
|
typedef cairo_surface_t *
|
|
|
|
|
(*stream_constructor_t) (cairo_write_func_t write_func,
|
|
|
|
|
void *closure,
|
|
|
|
|
double width_in_points,
|
|
|
|
|
double height_in_points);
|
|
|
|
|
|
|
|
|
|
static cairo_test_status_t
|
2008-08-11 21:12:45 +01:00
|
|
|
test_surface (const cairo_test_context_t *ctx,
|
|
|
|
|
const char *backend,
|
2007-09-26 00:28:47 +01:00
|
|
|
const char *filename,
|
2006-06-14 19:15:50 -04:00
|
|
|
file_constructor_t file_constructor,
|
|
|
|
|
stream_constructor_t stream_constructor)
|
|
|
|
|
{
|
|
|
|
|
cairo_surface_t *surface;
|
|
|
|
|
write_closure_t wc;
|
|
|
|
|
char file_contents[MAX_OUTPUT_SIZE];
|
|
|
|
|
cairo_status_t status;
|
|
|
|
|
FILE *fp;
|
|
|
|
|
|
2007-09-26 00:28:47 +01:00
|
|
|
/* test propagation of user errors */
|
|
|
|
|
surface = stream_constructor (bad_write, &wc,
|
|
|
|
|
WIDTH_IN_POINTS, HEIGHT_IN_POINTS);
|
|
|
|
|
|
|
|
|
|
status = cairo_surface_status (surface);
|
|
|
|
|
if (status) {
|
2008-08-11 21:12:45 +01:00
|
|
|
cairo_test_log (ctx,
|
|
|
|
|
"%s: Failed to create surface for stream.\n",
|
|
|
|
|
backend);
|
2007-09-26 00:28:47 +01:00
|
|
|
return CAIRO_TEST_FAILURE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
draw_to (surface);
|
|
|
|
|
|
2007-09-26 00:41:31 +01:00
|
|
|
cairo_surface_finish (surface);
|
2007-09-26 00:28:47 +01:00
|
|
|
status = cairo_surface_status (surface);
|
|
|
|
|
cairo_surface_destroy (surface);
|
|
|
|
|
|
|
|
|
|
if (status != CAIRO_STATUS_WRITE_ERROR) {
|
2008-08-11 21:12:45 +01:00
|
|
|
cairo_test_log (ctx,
|
|
|
|
|
"%s: Error: expected \"write error\", but received \"%s\".\n",
|
2007-09-26 00:28:47 +01:00
|
|
|
backend, cairo_status_to_string (status));
|
|
|
|
|
return CAIRO_TEST_FAILURE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* construct the real surface */
|
2008-08-11 21:12:45 +01:00
|
|
|
wc.ctx = ctx;
|
2006-06-14 19:15:50 -04:00
|
|
|
wc.status = CAIRO_TEST_SUCCESS;
|
|
|
|
|
wc.index = 0;
|
|
|
|
|
|
|
|
|
|
surface = stream_constructor (test_write, &wc,
|
|
|
|
|
WIDTH_IN_POINTS, HEIGHT_IN_POINTS);
|
|
|
|
|
|
|
|
|
|
status = cairo_surface_status (surface);
|
|
|
|
|
if (status) {
|
2008-08-11 21:12:45 +01:00
|
|
|
cairo_test_log (ctx,
|
|
|
|
|
"%s: Failed to create surface for stream.\n", backend);
|
2006-06-14 19:15:50 -04:00
|
|
|
return CAIRO_TEST_FAILURE;
|
|
|
|
|
}
|
|
|
|
|
|
2006-07-11 22:19:39 -04:00
|
|
|
draw_to (surface);
|
2006-06-14 19:15:50 -04:00
|
|
|
|
|
|
|
|
cairo_surface_destroy (surface);
|
|
|
|
|
|
|
|
|
|
if (wc.status != CAIRO_TEST_SUCCESS) {
|
|
|
|
|
/* Error already reported. */
|
|
|
|
|
return wc.status;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
surface = file_constructor (filename,
|
|
|
|
|
WIDTH_IN_POINTS, HEIGHT_IN_POINTS);
|
|
|
|
|
|
|
|
|
|
status = cairo_surface_status (surface);
|
|
|
|
|
if (status) {
|
2008-08-11 21:12:45 +01:00
|
|
|
cairo_test_log (ctx, "%s: Failed to create surface for file %s: %s.\n",
|
2007-09-26 00:28:47 +01:00
|
|
|
backend, filename, cairo_status_to_string (status));
|
2006-06-14 19:15:50 -04:00
|
|
|
return CAIRO_TEST_FAILURE;
|
|
|
|
|
}
|
|
|
|
|
|
2006-07-11 22:19:39 -04:00
|
|
|
draw_to (surface);
|
2006-06-14 19:15:50 -04:00
|
|
|
|
|
|
|
|
cairo_surface_destroy (surface);
|
|
|
|
|
|
|
|
|
|
fp = fopen (filename, "r");
|
|
|
|
|
if (fp == NULL) {
|
2008-08-11 21:12:45 +01:00
|
|
|
cairo_test_log (ctx, "%s: Failed to open %s for reading: %s.\n",
|
2007-09-26 00:28:47 +01:00
|
|
|
backend, filename, strerror (errno));
|
2006-06-14 19:15:50 -04:00
|
|
|
return CAIRO_TEST_FAILURE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (fread (file_contents, 1, wc.index, fp) != wc.index) {
|
2008-08-11 21:12:45 +01:00
|
|
|
cairo_test_log (ctx, "%s: Failed to read %s: %s.\n",
|
2007-09-26 00:28:47 +01:00
|
|
|
backend, filename, strerror (errno));
|
2007-04-16 16:57:24 +01:00
|
|
|
fclose (fp);
|
2006-06-14 19:15:50 -04:00
|
|
|
return CAIRO_TEST_FAILURE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (memcmp (file_contents, wc.buffer, wc.index) != 0) {
|
2008-08-11 21:12:45 +01:00
|
|
|
cairo_test_log (ctx, "%s: Stream based output differ from file output for %s.\n",
|
2007-09-26 00:28:47 +01:00
|
|
|
backend, filename);
|
2007-04-16 16:57:24 +01:00
|
|
|
fclose (fp);
|
2006-06-14 19:15:50 -04:00
|
|
|
return CAIRO_TEST_FAILURE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fclose (fp);
|
|
|
|
|
|
|
|
|
|
return CAIRO_TEST_SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
|
main (void)
|
|
|
|
|
{
|
2008-08-11 21:12:45 +01:00
|
|
|
cairo_test_context_t ctx;
|
2008-08-12 13:33:04 +01:00
|
|
|
cairo_test_status_t status = CAIRO_TEST_UNTESTED;
|
2007-04-16 16:57:24 +01:00
|
|
|
cairo_test_status_t test_status;
|
2008-01-16 08:11:51 -08:00
|
|
|
const char test_name[] = "create-for-stream";
|
2006-06-14 19:15:50 -04:00
|
|
|
|
2008-08-11 21:12:45 +01:00
|
|
|
cairo_test_init (&ctx, test_name);
|
2006-06-14 19:15:50 -04:00
|
|
|
|
|
|
|
|
#if CAIRO_HAS_PS_SURFACE
|
2008-09-27 21:53:21 +01:00
|
|
|
if (cairo_test_is_target_enabled (&ctx, "ps2") ||
|
|
|
|
|
cairo_test_is_target_enabled (&ctx, "ps3"))
|
|
|
|
|
{
|
2008-08-12 13:33:04 +01:00
|
|
|
if (status == CAIRO_TEST_UNTESTED)
|
|
|
|
|
status = CAIRO_TEST_SUCCESS;
|
|
|
|
|
|
2008-01-10 13:04:52 +00:00
|
|
|
test_status = test_surface (&ctx, "ps", "create-for-stream.ps",
|
|
|
|
|
cairo_ps_surface_create,
|
|
|
|
|
cairo_ps_surface_create_for_stream);
|
|
|
|
|
cairo_test_log (&ctx, "TEST: %s TARGET: %s RESULT: %s\n",
|
|
|
|
|
test_name, "ps",
|
|
|
|
|
test_status ? "FAIL" : "PASS");
|
|
|
|
|
if (status == CAIRO_TEST_SUCCESS)
|
|
|
|
|
status = test_status;
|
|
|
|
|
}
|
2006-06-14 19:15:50 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#if CAIRO_HAS_PDF_SURFACE
|
2008-01-10 13:04:52 +00:00
|
|
|
if (cairo_test_is_target_enabled (&ctx, "pdf")) {
|
2008-08-12 13:33:04 +01:00
|
|
|
if (status == CAIRO_TEST_UNTESTED)
|
|
|
|
|
status = CAIRO_TEST_SUCCESS;
|
|
|
|
|
|
2008-01-10 13:04:52 +00:00
|
|
|
test_status = test_surface (&ctx, "pdf", "create-for-stream.pdf",
|
|
|
|
|
cairo_pdf_surface_create,
|
|
|
|
|
cairo_pdf_surface_create_for_stream);
|
|
|
|
|
cairo_test_log (&ctx, "TEST: %s TARGET: %s RESULT: %s\n",
|
|
|
|
|
test_name, "pdf",
|
|
|
|
|
test_status ? "FAIL" : "PASS");
|
|
|
|
|
if (status == CAIRO_TEST_SUCCESS)
|
|
|
|
|
status = test_status;
|
|
|
|
|
}
|
2006-06-14 19:15:50 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#if CAIRO_HAS_SVG_SURFACE
|
2008-09-27 21:53:21 +01:00
|
|
|
if (cairo_test_is_target_enabled (&ctx, "svg11") ||
|
|
|
|
|
cairo_test_is_target_enabled (&ctx, "svg12"))
|
|
|
|
|
{
|
2008-08-12 13:33:04 +01:00
|
|
|
if (status == CAIRO_TEST_UNTESTED)
|
|
|
|
|
status = CAIRO_TEST_SUCCESS;
|
|
|
|
|
|
2008-01-10 13:04:52 +00:00
|
|
|
test_status = test_surface (&ctx, "svg", "create-for-stream.svg",
|
|
|
|
|
cairo_svg_surface_create,
|
|
|
|
|
cairo_svg_surface_create_for_stream);
|
|
|
|
|
cairo_test_log (&ctx, "TEST: %s TARGET: %s RESULT: %s\n",
|
|
|
|
|
test_name, "svg",
|
|
|
|
|
test_status ? "FAIL" : "PASS");
|
|
|
|
|
if (status == CAIRO_TEST_SUCCESS)
|
|
|
|
|
status = test_status;
|
|
|
|
|
}
|
2006-06-14 19:15:50 -04:00
|
|
|
#endif
|
|
|
|
|
|
2008-08-11 21:12:45 +01:00
|
|
|
cairo_test_fini (&ctx);
|
2007-03-02 11:31:13 -08:00
|
|
|
|
2007-04-16 16:57:24 +01:00
|
|
|
return status;
|
2006-06-14 19:15:50 -04:00
|
|
|
}
|