⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cairo-test.c

📁 按照官方的说法:Cairo is a vector graphics library with cross-device output support. 翻译过来
💻 C
📖 第 1 页 / 共 4 页
字号:
    root = XCBSetupRootsIter(XCBGetSetup(c)).data;    xtc->drawable.pixmap = XCBPIXMAPNew (c);    {	XCBDRAWABLE root_drawable;	root_drawable.window = root->root;	XCBCreatePixmap (c, 32, xtc->drawable.pixmap, root_drawable,			 width, height);    }    switch (content) {    case CAIRO_CONTENT_COLOR:	format = CAIRO_FORMAT_RGB24;	break;    case CAIRO_CONTENT_COLOR_ALPHA:	format = CAIRO_FORMAT_ARGB32;	break;    default:	cairo_test_log ("Invalid content for XCB test: %d\n", content);	return NULL;    }    render_format = _format_from_cairo (c, format);    if (render_format.id.xid == 0)	return NULL;    surface = cairo_xcb_surface_create_with_xrender_format (c, xtc->drawable, root,							    &render_format,							    width, height);    return surface;}static voidcleanup_xcb (void *closure){    xcb_target_closure_t *xtc = closure;    XCBFreePixmap (xtc->c, xtc->drawable.pixmap);    XCBDisconnect (xtc->c);    free (xtc);}#endif#if CAIRO_HAS_XLIB_SURFACE#include "cairo-xlib-xrender.h"typedef struct _xlib_target_closure{    Display *dpy;    Pixmap pixmap;} xlib_target_closure_t;static cairo_surface_t *create_xlib_surface (cairo_test_t	 *test,		     cairo_content_t	  content,		     void		**closure){    int width = test->width;    int height = test->height;    xlib_target_closure_t *xtc;    cairo_surface_t *surface;    Display *dpy;    XRenderPictFormat *xrender_format;    *closure = xtc = xmalloc (sizeof (xlib_target_closure_t));    if (width == 0)	width = 1;    if (height == 0)	height = 1;    xtc->dpy = dpy = XOpenDisplay (NULL);    if (xtc->dpy == NULL) {	cairo_test_log ("Failed to open display: %s\n", XDisplayName(0));	return NULL;    }    XSynchronize (xtc->dpy, 1);    /* XXX: Currently we don't do any xlib testing when the X server     * doesn't have the Render extension. We could do better here,     * (perhaps by converting the tests from ARGB32 to RGB24). One     * step better would be to always test the non-Render fallbacks     * for each test even if the server does have the Render     * extension. That would probably be through another     * cairo_test_target which would use an extended version of     * cairo_test_xlib_disable_render.  */    switch (content) {    case CAIRO_CONTENT_COLOR_ALPHA:	xrender_format = XRenderFindStandardFormat (dpy, PictStandardARGB32);	break;    case CAIRO_CONTENT_COLOR:	xrender_format = XRenderFindStandardFormat (dpy, PictStandardRGB24);	break;    default:	cairo_test_log ("Invalid content for xlib test: %d\n", content);	return NULL;    }    if (xrender_format == NULL) {	cairo_test_log ("X server does not have the Render extension.\n");	return NULL;    }    xtc->pixmap = XCreatePixmap (dpy, DefaultRootWindow (dpy),				 width, height, xrender_format->depth);    surface = cairo_xlib_surface_create_with_xrender_format (dpy, xtc->pixmap,							     DefaultScreenOfDisplay (dpy),							     xrender_format,							     width, height);    return surface;}static voidcleanup_xlib (void *closure){    xlib_target_closure_t *xtc = closure;    XFreePixmap (xtc->dpy, xtc->pixmap);    XCloseDisplay (xtc->dpy);    free (xtc);}#endif#if CAIRO_HAS_BEOS_SURFACE/* BeOS test functions are external as they need to be C++ */#include "cairo-test-beos.h"#endif#if CAIRO_HAS_DIRECTFB_SURFACE#include "cairo-test-directfb.h"#endif#if CAIRO_HAS_PS_SURFACE#include "cairo-ps.h"cairo_user_data_key_t	ps_closure_key;typedef struct _ps_target_closure{    char		*filename;    int			 width;    int			 height;    cairo_surface_t	*target;} ps_target_closure_t;static cairo_surface_t *create_ps_surface (cairo_test_t		 *test,		   cairo_content_t	  content,		   void			**closure){    int width = test->width;    int height = test->height;    ps_target_closure_t	*ptc;    cairo_surface_t *surface;    /* Sanitize back to a real cairo_content_t value. */    if (content == CAIRO_TEST_CONTENT_COLOR_ALPHA_FLATTENED)	content = CAIRO_CONTENT_COLOR_ALPHA;    *closure = ptc = xmalloc (sizeof (ps_target_closure_t));    xasprintf (&ptc->filename, "%s-ps-%s-out.ps",	       test->name, _cairo_test_content_name (content));    ptc->width = width;    ptc->height = height;    surface = cairo_ps_surface_create (ptc->filename, width, height);    if (cairo_surface_status (surface)) {	free (ptc->filename);	free (ptc);	return NULL;    }    cairo_surface_set_fallback_resolution (surface, 72., 72.);    if (content == CAIRO_CONTENT_COLOR) {	ptc->target = surface;	surface = cairo_surface_create_similar (ptc->target,						CAIRO_CONTENT_COLOR,						width, height);    } else {	ptc->target = NULL;    }    cairo_surface_set_user_data (surface, &ps_closure_key, ptc, NULL);    return surface;}static cairo_status_tps_surface_write_to_png (cairo_surface_t *surface, const char *filename){    ps_target_closure_t *ptc = cairo_surface_get_user_data (surface, &ps_closure_key);    char    command[4096];    /* Both surface and ptc->target were originally created at the     * same dimensions. We want a 1:1 copy here, so we first clear any     * device offset on surface.     *     * In a more realistic use case of device offsets, the target of     * this copying would be of a different size than the source, and     * the offset would be desirable during the copy operation. */    cairo_surface_set_device_offset (surface, 0, 0);    if (ptc->target) {	cairo_t *cr;	cr = cairo_create (ptc->target);	cairo_set_source_surface (cr, surface, 0, 0);	cairo_paint (cr);	cairo_show_page (cr);	cairo_destroy (cr);	cairo_surface_finish (surface);	surface = ptc->target;    }    cairo_surface_finish (surface);    sprintf (command, "gs -q -r72 -g%dx%d -dSAFER -dBATCH -dNOPAUSE -sDEVICE=png16m -sOutputFile=%s %s",	     ptc->width, ptc->height, filename, ptc->filename);    if (system (command) == 0)	return CAIRO_STATUS_SUCCESS;    return CAIRO_STATUS_WRITE_ERROR;}static voidcleanup_ps (void *closure){    ps_target_closure_t *ptc = closure;    if (ptc->target)	cairo_surface_destroy (ptc->target);    free (ptc->filename);    free (ptc);}#endif /* CAIRO_HAS_PS_SURFACE */#if CAIRO_HAS_PDF_SURFACE && CAIRO_CAN_TEST_PDF_SURFACE#include "cairo-pdf.h"static const char *pdf_ignored_tests[] = {    /* We can't match the results of tests that depend on     * CAIRO_ANTIALIAS_NONE, (nor do we care). */    "ft-text-antialias-none",    "rectangle-rounding-error",    "unantialiased-shapes",    NULL};cairo_user_data_key_t pdf_closure_key;typedef struct _pdf_target_closure{    char		*filename;    int			 width;    int			 height;    cairo_surface_t	*target;} pdf_target_closure_t;static cairo_surface_t *create_pdf_surface (cairo_test_t	 *test,		    cairo_content_t	  content,		    void		**closure){    int width = test->width;    int height = test->height;    pdf_target_closure_t *ptc;    cairo_surface_t *surface;    int i;    for (i = 0; pdf_ignored_tests[i] != NULL; i++)	if (strcmp (test->name, pdf_ignored_tests[i]) == 0)	    return NULL;    /* Sanitize back to a real cairo_content_t value. */    if (content == CAIRO_TEST_CONTENT_COLOR_ALPHA_FLATTENED)	content = CAIRO_CONTENT_COLOR_ALPHA;    *closure = ptc = xmalloc (sizeof (pdf_target_closure_t));    ptc->width = width;    ptc->height = height;    xasprintf (&ptc->filename, "%s-pdf-%s-out.pdf",	       test->name, _cairo_test_content_name (content));    surface = cairo_pdf_surface_create (ptc->filename, width, height);    if (cairo_surface_status (surface)) {	free (ptc->filename);	free (ptc);	return NULL;    }    cairo_surface_set_fallback_resolution (surface, 72., 72.);    if (content == CAIRO_CONTENT_COLOR) {	ptc->target = surface;	surface = cairo_surface_create_similar (ptc->target,						CAIRO_CONTENT_COLOR,						width, height);    } else {	ptc->target = NULL;    }    cairo_surface_set_user_data (surface, &pdf_closure_key, ptc, NULL);    return surface;}static cairo_status_tpdf_surface_write_to_png (cairo_surface_t *surface, const char *filename){    pdf_target_closure_t *ptc = cairo_surface_get_user_data (surface, &pdf_closure_key);    char    command[4096];    /* Both surface and ptc->target were originally created at the     * same dimensions. We want a 1:1 copy here, so we first clear any     * device offset on surface.     *     * In a more realistic use case of device offsets, the target of     * this copying would be of a different size than the source, and     * the offset would be desirable during the copy operation. */    cairo_surface_set_device_offset (surface, 0, 0);    if (ptc->target) {	cairo_t *cr;	cr = cairo_create (ptc->target);	cairo_set_source_surface (cr, surface, 0, 0);	cairo_paint (cr);	cairo_show_page (cr);	cairo_destroy (cr);	cairo_surface_finish (surface);	surface = ptc->target;    }    cairo_surface_finish (surface);    sprintf (command, "./pdf2png %s %s 1",	     ptc->filename, filename);    if (system (command) != 0)	return CAIRO_STATUS_WRITE_ERROR;    return CAIRO_STATUS_SUCCESS;}static voidcleanup_pdf (void *closure){    pdf_target_closure_t *ptc = closure;    if (ptc->target)	cairo_surface_destroy (ptc->target);    free (ptc->filename);    free (ptc);}#endif /* CAIRO_HAS_PDF_SURFACE && CAIRO_CAN_TEST_PDF_SURFACE */#if CAIRO_HAS_SVG_SURFACE && CAIRO_CAN_TEST_SVG_SURFACE#include "cairo-svg.h"static const char *svg_ignored_tests[] = {    /* rectangle-rounding-error uses CAIRO_ANTIALIAS_NONE,     * which is not supported */    "ft-text-antialias-none",    "rectangle-rounding-error",    NULL};cairo_user_data_key_t	svg_closure_key;typedef struct _svg_target_closure{    char    *filename;    int	    width, height;    cairo_surface_t	*target;} svg_target_closure_t;static cairo_surface_t *create_svg_surface (cairo_test_t	 *test,		    cairo_content_t	  content,		    void		**closure){    int width = test->width;    int height = test->height;    int i;    svg_target_closure_t *ptc;    cairo_surface_t *surface;    for (i = 0; svg_ignored_tests[i] != NULL; i++)	if (strcmp (test->name, svg_ignored_tests[i]) == 0)	    return NULL;    *closure = ptc = xmalloc (sizeof (svg_target_closure_t));    ptc->width = width;    ptc->height = height;    xasprintf (&ptc->filename, "%s-svg-%s-out.svg",	       test->name, _cairo_test_content_name (content));    surface = cairo_svg_surface_create (ptc->filename, width, height);    if (cairo_surface_status (surface)) {	free (ptc->filename);	free (ptc);	return NULL;    }    cairo_surface_set_fallback_resolution (surface, 72., 72.);    if (content == CAIRO_CONTENT_COLOR) {	ptc->target = surface;	surface = cairo_surface_create_similar (ptc->target,						CAIRO_CONTENT_COLOR,						width, height);    } else {	ptc->target = NULL;    }    cairo_surface_set_user_data (surface, &svg_closure_key, ptc, NULL);    return surface;}static cairo_status_tsvg_surface_write_to_png (cairo_surface_t *surface, const char *filename){    svg_target_closure_t *ptc = cairo_surface_get_user_data (surface, &svg_closure_key);    char    command[4096];    /* Both surface and ptc->target were originally created at the     * same dimensions. We want a 1:1 copy here, so we first clear any     * device offset on surface.     *     * In a more realistic use case of device offsets, the target of     * this copying would be of a different size than the source, and     * the offset would be desirable during the copy operation. */    cairo_surface_set_device_offset (surface, 0, 0);    if (ptc->target) {	cairo_t *cr;	cr = cairo_create (ptc->target);	cairo_set_source_surface (cr, surface, 0, 0);	cairo_paint (cr);	cairo_show_page (cr);	cairo_destroy (cr);	cairo_surface_finish (surface);	surface = ptc->target;    }    cairo_surface_finish (surface);    sprintf (command, "./svg2png %s %s",	     ptc->filename, filename);    if (system (command) != 0)	return CAIRO_STATUS_WRITE_ERROR;    return CAIRO_STATUS_SUCCESS;}static voidcleanup_svg (void *closure){    svg_target_closure_t *ptc = closure;    if (ptc->target)	cairo_surface_destroy (ptc->target);    free (ptc->filename);    free (ptc);}#endif /* CAIRO_HAS_SVG_SURFACE && CAIRO_CAN_TEST_SVG_SURFACE */static cairo_test_status_tcairo_test_for_target (cairo_test_t *test,		       cairo_test_draw_function_t draw,		       cairo_test_target_t	 *target,		       int			  dev_offset){    cairo_test_status_t status;    cairo_surface_t *surface;    cairo_t *cr;    char *png_name, *ref_name, *diff_name, *offset_str;    char *format;    cairo_test_status_t ret;    cairo_content_t expected_content;    /* Get the strings ready that we'll need. */    format = _cairo_test_content_name (target->content);    if (dev_offset)	xasprintf (&offset_str, "-%d", dev_offset);    else	offset_str = strdup("");    xasprintf (&png_name, "%s-%s-%s%s%s", test->name,	       target->name, format, offset_str, CAIRO_TEST_PNG_SUFFIX);    /* First look for a target/format-specific reference image. */    xasprintf (&ref_name, "%s/%s-%s-%s%s", srcdir, test->name,	       target->name, format, CAIRO_TEST_REF_SUFFIX);    if (access (ref_name, F_OK) != 0) {	free (ref_name);	/* Next, look for format-specifc reference image. */	xasprintf (&ref_name, "%s/%s-%s%s", srcdir, test->name,		   format,CAIRO_TEST_REF_SUFFIX);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -