2008-11-16 20:04:55 +00:00
|
|
|
#include <cairo.h>
|
|
|
|
|
#include <cairo-script-interpreter.h>
|
|
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
|
|
static const cairo_user_data_key_t _key;
|
|
|
|
|
|
|
|
|
|
#if CAIRO_HAS_XLIB_XRENDER_SURFACE
|
|
|
|
|
#include <cairo-xlib.h>
|
|
|
|
|
#include <cairo-xlib-xrender.h>
|
|
|
|
|
|
|
|
|
|
static Display *
|
|
|
|
|
_get_display (void)
|
|
|
|
|
{
|
|
|
|
|
static Display *dpy;
|
|
|
|
|
|
|
|
|
|
if (dpy != NULL)
|
|
|
|
|
return dpy;
|
|
|
|
|
|
|
|
|
|
dpy = XOpenDisplay (NULL);
|
|
|
|
|
if (dpy == NULL) {
|
|
|
|
|
fprintf (stderr, "Failed to open display.\n");
|
|
|
|
|
exit (1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return dpy;
|
|
|
|
|
}
|
|
|
|
|
|
2008-12-18 18:32:44 +00:00
|
|
|
static void
|
|
|
|
|
_destroy_pixmap (void *closure)
|
|
|
|
|
{
|
|
|
|
|
XFreePixmap (_get_display(), (Pixmap) closure);
|
|
|
|
|
}
|
|
|
|
|
|
2008-11-16 20:04:55 +00:00
|
|
|
static void
|
|
|
|
|
_destroy_window (void *closure)
|
|
|
|
|
{
|
|
|
|
|
XFlush (_get_display ());
|
|
|
|
|
XDestroyWindow (_get_display(), (Window) closure);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static cairo_surface_t *
|
2008-12-19 14:11:37 +00:00
|
|
|
_xrender_surface_create (void *closure,
|
|
|
|
|
cairo_content_t content,
|
|
|
|
|
double width, double height)
|
2008-11-16 20:04:55 +00:00
|
|
|
{
|
|
|
|
|
Display *dpy;
|
|
|
|
|
XRenderPictFormat *xrender_format;
|
|
|
|
|
cairo_surface_t *surface;
|
|
|
|
|
|
|
|
|
|
dpy = _get_display ();
|
|
|
|
|
|
2008-12-18 18:32:44 +00:00
|
|
|
content = CAIRO_CONTENT_COLOR_ALPHA;
|
|
|
|
|
if (1) {
|
|
|
|
|
Pixmap pixmap;
|
|
|
|
|
|
|
|
|
|
switch (content) {
|
|
|
|
|
case CAIRO_CONTENT_COLOR_ALPHA:
|
|
|
|
|
xrender_format = XRenderFindStandardFormat (dpy, PictStandardARGB32);
|
|
|
|
|
break;
|
|
|
|
|
case CAIRO_CONTENT_COLOR:
|
|
|
|
|
xrender_format = XRenderFindStandardFormat (dpy, PictStandardRGB24);
|
|
|
|
|
break;
|
|
|
|
|
case CAIRO_CONTENT_ALPHA:
|
|
|
|
|
default:
|
|
|
|
|
xrender_format = XRenderFindStandardFormat (dpy, PictStandardA8);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pixmap = XCreatePixmap (dpy, DefaultRootWindow (dpy),
|
|
|
|
|
width, height, xrender_format->depth);
|
|
|
|
|
|
|
|
|
|
surface = cairo_xlib_surface_create_with_xrender_format (dpy, pixmap,
|
|
|
|
|
DefaultScreenOfDisplay (dpy),
|
|
|
|
|
xrender_format,
|
|
|
|
|
width, height);
|
|
|
|
|
cairo_surface_set_user_data (surface, &_key,
|
|
|
|
|
(void *) pixmap, _destroy_pixmap);
|
|
|
|
|
} else {
|
|
|
|
|
XSetWindowAttributes attr;
|
|
|
|
|
Visual *visual;
|
|
|
|
|
Window w;
|
|
|
|
|
|
|
|
|
|
visual = DefaultVisual (dpy, DefaultScreen (dpy));
|
|
|
|
|
xrender_format = XRenderFindVisualFormat (dpy, visual);
|
|
|
|
|
if (xrender_format == NULL) {
|
|
|
|
|
fprintf (stderr, "X server does not have the Render extension.\n");
|
|
|
|
|
exit (1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
attr.override_redirect = True;
|
|
|
|
|
w = XCreateWindow (dpy, DefaultRootWindow (dpy), 0, 0,
|
|
|
|
|
width <= 0 ? 1 : width,
|
|
|
|
|
height <= 0 ? 1 : height,
|
|
|
|
|
0, xrender_format->depth,
|
|
|
|
|
InputOutput, visual, CWOverrideRedirect, &attr);
|
|
|
|
|
XMapWindow (dpy, w);
|
|
|
|
|
|
|
|
|
|
surface = cairo_xlib_surface_create_with_xrender_format (dpy, w,
|
|
|
|
|
DefaultScreenOfDisplay (dpy),
|
|
|
|
|
xrender_format,
|
|
|
|
|
width, height);
|
|
|
|
|
cairo_surface_set_user_data (surface, &_key, (void *) w, _destroy_window);
|
2008-11-16 20:04:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return surface;
|
|
|
|
|
}
|
2008-12-19 14:11:37 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#if CAIRO_HAS_PDF_SURFACE
|
|
|
|
|
#include <cairo-pdf.h>
|
2008-11-26 17:30:29 +00:00
|
|
|
static cairo_surface_t *
|
2008-12-19 14:11:37 +00:00
|
|
|
_pdf_surface_create (void *closure,
|
|
|
|
|
cairo_content_t content,
|
|
|
|
|
double width, double height)
|
2008-11-26 17:30:29 +00:00
|
|
|
{
|
2008-12-19 14:11:37 +00:00
|
|
|
return cairo_pdf_surface_create_for_stream (NULL, NULL, width, height);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#if CAIRO_HAS_PS_SURFACE
|
|
|
|
|
#include <cairo-ps.h>
|
|
|
|
|
static cairo_surface_t *
|
|
|
|
|
_ps_surface_create (void *closure,
|
|
|
|
|
cairo_content_t content,
|
|
|
|
|
double width, double height)
|
|
|
|
|
{
|
|
|
|
|
return cairo_ps_surface_create_for_stream (NULL, NULL, width, height);
|
2008-11-26 17:30:29 +00:00
|
|
|
}
|
2008-11-16 20:04:55 +00:00
|
|
|
#endif
|
|
|
|
|
|
2008-12-19 14:11:37 +00:00
|
|
|
#if CAIRO_HAS_SVG_SURFACE
|
|
|
|
|
#include <cairo-svg.h>
|
|
|
|
|
static cairo_surface_t *
|
|
|
|
|
_svg_surface_create (void *closure,
|
|
|
|
|
cairo_content_t content,
|
|
|
|
|
double width, double height)
|
|
|
|
|
{
|
|
|
|
|
return cairo_svg_surface_create_for_stream (NULL, NULL, width, height);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
static cairo_surface_t *
|
|
|
|
|
_image_surface_create (void *closure,
|
|
|
|
|
cairo_content_t content,
|
|
|
|
|
double width, double height)
|
|
|
|
|
{
|
|
|
|
|
return cairo_image_surface_create (CAIRO_FORMAT_ARGB32, width, height);
|
|
|
|
|
}
|
|
|
|
|
|
2008-11-16 20:04:55 +00:00
|
|
|
int
|
|
|
|
|
main (int argc, char **argv)
|
|
|
|
|
{
|
|
|
|
|
cairo_script_interpreter_t *csi;
|
2008-12-19 14:11:37 +00:00
|
|
|
cairo_script_interpreter_hooks_t hooks = {
|
|
|
|
|
#if CAIRO_HAS_XLIB_XRENDER_SURFACE
|
|
|
|
|
.surface_create = _xrender_surface_create
|
|
|
|
|
#elif CAIRO_PDF_SURFACE
|
|
|
|
|
.surface_create = _pdf_surface_create
|
|
|
|
|
#elif CAIRO_PS_SURFACE
|
|
|
|
|
.surface_create = _ps_surface_create
|
|
|
|
|
#elif CAIRO_SVG_SURFACE
|
|
|
|
|
.surface_create = _svg_surface_create
|
|
|
|
|
#else
|
|
|
|
|
.surface_create = _image_surface_create
|
|
|
|
|
#endif
|
2008-11-16 20:04:55 +00:00
|
|
|
};
|
2008-12-10 17:56:19 +00:00
|
|
|
int i;
|
2008-12-19 14:11:37 +00:00
|
|
|
const struct backends {
|
|
|
|
|
const char *name;
|
|
|
|
|
csi_surface_create_func_t create;
|
|
|
|
|
} backends[] = {
|
|
|
|
|
{ "--image", _image_surface_create },
|
|
|
|
|
#if CAIRO_HAS_XLIB_XRENDER_SURFACE
|
|
|
|
|
{ "--xrender", _xrender_surface_create },
|
|
|
|
|
#endif
|
|
|
|
|
#if CAIRO_HAS_PDF_SURFACE
|
|
|
|
|
{ "--pdf", _pdf_surface_create },
|
|
|
|
|
#endif
|
|
|
|
|
#if CAIRO_HAS_PS_SURFACE
|
|
|
|
|
{ "--ps", _ps_surface_create },
|
|
|
|
|
#endif
|
|
|
|
|
#if CAIRO_HAS_SVG_SURFACE
|
|
|
|
|
{ "--svg", _svg_surface_create },
|
|
|
|
|
#endif
|
|
|
|
|
{ NULL, NULL }
|
|
|
|
|
};
|
2008-11-16 20:04:55 +00:00
|
|
|
|
|
|
|
|
csi = cairo_script_interpreter_create ();
|
|
|
|
|
cairo_script_interpreter_install_hooks (csi, &hooks);
|
2008-12-19 14:11:37 +00:00
|
|
|
|
|
|
|
|
for (i = 1; i < argc; i++) {
|
|
|
|
|
const struct backends *b;
|
|
|
|
|
|
|
|
|
|
for (b = backends; b->name != NULL; b++) {
|
|
|
|
|
if (strcmp (b->name, argv[i]) == 0) {
|
|
|
|
|
hooks.surface_create = b->create;
|
|
|
|
|
cairo_script_interpreter_install_hooks (csi, &hooks);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (b->name == NULL)
|
|
|
|
|
cairo_script_interpreter_run (csi, argv[i]);
|
|
|
|
|
}
|
|
|
|
|
|
2008-11-16 20:04:55 +00:00
|
|
|
return cairo_script_interpreter_destroy (csi);
|
|
|
|
|
}
|