1 /* -*- Mode: c; c-basic-offset: 4; indent-tabs-mode: t; tab-width: 8; -*- */
2 /*
3  * Copyright © Chris Wilson
4  *
5  * Permission to use, copy, modify, distribute, and sell this software
6  * and its documentation for any purpose is hereby granted without
7  * fee, provided that the above copyright notice appear in all copies
8  * and that both that copyright notice and this permission notice
9  * appear in supporting documentation, and that the name of
10  * Chris Wilson not be used in advertising or publicity pertaining to
11  * distribution of the software without specific, written prior
12  * permission. Chris Wilson makes no representations about the
13  * suitability of this software for any purpose.  It is provided "as
14  * is" without express or implied warranty.
15  *
16  * CHRIS WILSON DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
17  * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
18  * FITNESS, IN NO EVENT SHALL CHRIS WILSON BE LIABLE FOR ANY SPECIAL,
19  * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
20  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
21  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
22  * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23  *
24  * Author: Chris Wilson <chris@chris-wilson.co.uk>
25  */
26 
27 #include "cairo-boilerplate-private.h"
28 
29 #include "cairo-script.h"
30 
31 static cairo_user_data_key_t script_closure_key;
32 
33 typedef struct _script_target_closure {
34     char		*filename;
35     double		 width;
36     double		 height;
37 } script_target_closure_t;
38 
39 static cairo_surface_t *
_cairo_boilerplate_script_create_surface(const char * name,cairo_content_t content,double width,double height,double max_width,double max_height,cairo_boilerplate_mode_t mode,void ** closure)40 _cairo_boilerplate_script_create_surface (const char		    *name,
41 					  cairo_content_t	     content,
42 					  double		     width,
43 					  double		     height,
44 					  double		     max_width,
45 					  double		     max_height,
46 					  cairo_boilerplate_mode_t   mode,
47 					  void			   **closure)
48 {
49     script_target_closure_t *ptc;
50     cairo_device_t *ctx;
51     cairo_surface_t *surface;
52     cairo_status_t status;
53 
54     *closure = ptc = xmalloc (sizeof (script_target_closure_t));
55 
56     ptc->width = width;
57     ptc->height = height;
58 
59     xasprintf (&ptc->filename, "%s.out.cs", name);
60     xunlink (ptc->filename);
61 
62     ctx = cairo_script_create (ptc->filename);
63     surface = cairo_script_surface_create (ctx, content, width, height);
64     cairo_device_destroy (ctx);
65 
66     status = cairo_surface_set_user_data (surface,
67 					  &script_closure_key, ptc, NULL);
68     if (status == CAIRO_STATUS_SUCCESS)
69 	return surface;
70 
71     cairo_surface_destroy (surface);
72     surface = cairo_boilerplate_surface_create_in_error (status);
73 
74     free (ptc->filename);
75     free (ptc);
76     return surface;
77 }
78 
79 static cairo_status_t
_cairo_boilerplate_script_finish_surface(cairo_surface_t * surface)80 _cairo_boilerplate_script_finish_surface (cairo_surface_t *surface)
81 {
82     cairo_surface_finish (surface);
83     return cairo_surface_status (surface);
84 }
85 
86 static cairo_status_t
_cairo_boilerplate_script_surface_write_to_png(cairo_surface_t * surface,const char * filename)87 _cairo_boilerplate_script_surface_write_to_png (cairo_surface_t *surface,
88 						const char	*filename)
89 {
90     return CAIRO_STATUS_WRITE_ERROR;
91 }
92 
93 static cairo_surface_t *
_cairo_boilerplate_script_convert_to_image(cairo_surface_t * surface,int page)94 _cairo_boilerplate_script_convert_to_image (cairo_surface_t *surface,
95 					    int 	     page)
96 {
97     script_target_closure_t *ptc = cairo_surface_get_user_data (surface,
98 								&script_closure_key);
99     return cairo_boilerplate_convert_to_image (ptc->filename, page);
100 }
101 
102 static cairo_surface_t *
_cairo_boilerplate_script_get_image_surface(cairo_surface_t * surface,int page,int width,int height)103 _cairo_boilerplate_script_get_image_surface (cairo_surface_t *surface,
104 					     int	      page,
105 					     int	      width,
106 					     int	      height)
107 {
108     cairo_surface_t *image;
109 
110     image = _cairo_boilerplate_script_convert_to_image (surface, page);
111     cairo_surface_set_device_offset (image,
112 				     cairo_image_surface_get_width (image) - width,
113 				     cairo_image_surface_get_height (image) - height);
114     surface = _cairo_boilerplate_get_image_surface (image, 0, width, height);
115     cairo_surface_destroy (image);
116 
117     return surface;
118 }
119 
120 static void
_cairo_boilerplate_script_cleanup(void * closure)121 _cairo_boilerplate_script_cleanup (void *closure)
122 {
123     script_target_closure_t *ptc = closure;
124     free (ptc->filename);
125     free (ptc);
126 }
127 
128 static const cairo_boilerplate_target_t target[] = {{
129     "script", "script", ".cs", NULL,
130     CAIRO_SURFACE_TYPE_SCRIPT, CAIRO_CONTENT_COLOR_ALPHA, 0,
131     "cairo_script_surface_create",
132     _cairo_boilerplate_script_create_surface,
133     cairo_surface_create_similar,
134     NULL,
135     _cairo_boilerplate_script_finish_surface,
136     _cairo_boilerplate_script_get_image_surface,
137     _cairo_boilerplate_script_surface_write_to_png,
138     _cairo_boilerplate_script_cleanup,
139     NULL, NULL, FALSE, FALSE, FALSE
140 }};
141 CAIRO_BOILERPLATE (script, target)
142