From 1bfdf06e6717994963aedf97bbf7efbdd6f44a7e Mon Sep 17 00:00:00 2001 From: Carlos Garcia Campos Date: Sun, 21 Sep 2008 14:01:22 +0200 Subject: [PATCH] [test/any2ppm] Implement ps converter using libspectre --- configure.ac | 17 ++++++++---- test/Makefile.am | 4 +- test/any2ppm.c | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 87 insertions(+), 10 deletions(-) diff --git a/configure.ac b/configure.ac index 02e8c8e..ddeac47 100644 --- a/configure.ac +++ b/configure.ac @@ -337,19 +337,23 @@ CAIRO_ENABLE_SURFACE_BACKEND(ps, PostScript, yes, [ dnl =========================================================================== +SPECTRE_VERSION_REQUIRED=0.2.0 test_ps=no if test "x$use_ps" = "xyes"; then - AC_CHECK_PROG(GS, gs, gs) - if test "$GS"; then + libspectre_DEPENDENCY="libspectre >= $SPECTRE_VERSION_REQUIRED" + PKG_CHECK_MODULES(LIBSPECTRE, $libspectre_DEPENDENCY, + [test_ps=yes], + [AC_MSG_RESULT(no); test_ps="no (requires $poppler_DEPENDENCY)"]) + if test "x$test_ps" = "xyes"; then AC_DEFINE([CAIRO_CAN_TEST_PS_SURFACE], 1, [Define to 1 if the PS backend can be tested (needs ghostscript)]) - test_ps="yes" else - AC_MSG_WARN([PS backend will not be tested since ghostscript is not available]) - test_ps="no (requires ghostscript)" + AC_MSG_WARN([PS backend will not be tested since libspectre >= $SPECTRE_VERSION_REQUIRED is not available]) fi fi AM_CONDITIONAL(CAIRO_CAN_TEST_PS_SURFACE, test "x$test_ps" = "xyes") +AC_SUBST(LIBSPECTRE_CFLAGS) +AC_SUBST(LIBSPECTRE_LIBS) dnl =========================================================================== @@ -435,7 +439,8 @@ dnl =========================================================================== dnl Build the external converter if we have any of the test backends AM_CONDITIONAL(BUILD_ANY2PPM, test "x$test_svg" = "xyes" \ - -o "x$test_pdf" = "xyes" ) # -o "x$test_ps" = "xyes") + -o "x$test_pdf" = "xyes" \ + -o "x$test_ps" = "xyes") dnl =========================================================================== diff --git a/test/Makefile.am b/test/Makefile.am index 1dcde7d..8faf1fd 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -876,10 +876,10 @@ check_PROGRAMS += imagediff png-flatten if BUILD_ANY2PPM check_PROGRAMS += any2ppm -any2ppm_CFLAGS = $(POPPLER_CFLAGS) $(LIBRSVG_CFLAGS) +any2ppm_CFLAGS = $(POPPLER_CFLAGS) $(LIBRSVG_CFLAGS) $(LIBSPECTRE_CFLAGS) # add LDADD, so poppler/librsvg uses "our" cairo any2ppm_LDFLAGS = $(CAIRO_TEST_UNDEFINED_LDFLAGS) -any2ppm_LDADD = $(LDADD) $(POPPLER_LIBS) $(LIBRSVG_LIBS) +any2ppm_LDADD = $(LDADD) $(POPPLER_LIBS) $(LIBRSVG_LIBS) $(LIBSPECTRE_LIBS) endif if CAIRO_CAN_TEST_PDF_SURFACE diff --git a/test/any2ppm.c b/test/any2ppm.c index bca35f3..8315dda 100644 --- a/test/any2ppm.c +++ b/test/any2ppm.c @@ -70,6 +70,7 @@ #endif #if CAIRO_CAN_TEST_PS_SURFACE +#include #endif #if HAVE_FCNTL_H && HAVE_SIGNAL_H && HAVE_SYS_STAT_H && HAVE_SYS_SOCKET_H && HAVE_SYS_POLL_H && HAVE_SYS_UN_H @@ -323,11 +324,82 @@ svg_convert (char **argv, int fd) #if CAIRO_CAN_TEST_PS_SURFACE static const char * +_spectre_render_page (const char *filename, + const char *page_label, + cairo_surface_t **surface_out) +{ + SpectreDocument *document; + int width, height, stride; + unsigned char *pixels; + cairo_surface_t *surface; + static const cairo_user_data_key_t key; + + document = spectre_document_new (); + spectre_document_load (document, filename); + if (spectre_document_status (document)) { + const char *err; + + err = spectre_status_to_string (spectre_document_status (document)); + spectre_document_free (document); + + return err; + } + + if (page_label) { + SpectrePage *page; + SpectreRenderContext *rc; + + page = spectre_document_get_page_by_label (document, page_label); + spectre_document_free (document); + if (page == NULL) + return "page not found"; + + spectre_page_get_size (page, &width, &height); + rc = spectre_render_context_new (); + spectre_render_context_set_page_size (rc, width, height); + spectre_page_render (page, rc, &pixels, &stride); + spectre_render_context_free (rc); + if (spectre_page_status (page)) { + const char *err; + + err = spectre_status_to_string (spectre_page_status (page)); + free (pixels); + spectre_page_free (page); + + return err; + } + + spectre_page_free (page); + } else { + spectre_document_get_page_size (document, &width, &height); + spectre_document_render (document, &pixels, &stride); + spectre_document_free (document); + } + + surface = cairo_image_surface_create_for_data (pixels, + CAIRO_FORMAT_RGB24, + width, height, + stride); + cairo_surface_set_user_data (surface, &key, + pixels, (cairo_destroy_func_t)free); + *surface_out = surface; + return NULL; +} + +static const char * ps_convert (char **argv, int fd) { - /* XXX libspectre */ + const char *err; + cairo_surface_t *surface = NULL; /* silence compiler warning */ - return "no method to convert PS"; + err = _spectre_render_page (argv[0], argv[1], &surface); + if (err != NULL) + return err; + + err = write_ppm (surface, fd); + cairo_surface_destroy (surface); + + return err; } #endif -- 1.5.6.3