Debian Codegraph @

utils/viewer-cairo.c

raw
1 /* viewer-cairo.c: Common code for Cairo-based viewers
2 *
3 * Copyright (C) 1999,2004,2005 Red Hat, Inc.
4 * Copyright (C) 2001 Sun Microsystems
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
15 *
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
20 */
21 #include "config.h"
22
23 #include "viewer-cairo.h"
24 #include "viewer-render.h"
25
26 #include <cairo.h>
27
28 #include <string.h>
29
30
31
32 #ifdef HAVE_CAIRO_XLIB
33 #ifdef HAVE_XFT
34 #include "viewer-x.h"
35 #include <cairo-xlib.h>
36
37 static cairo_surface_t *
38 cairo_x_view_iface_create_surface (gpointer instance,
39 gpointer surface,
40 int width,
41 int height)
42 {
43 XViewer *x = (XViewer *)instance;
44 Drawable drawable = (Drawable) surface;
45
46 return cairo_xlib_surface_create (x->display, drawable,
47 DefaultVisual (x->display, x->screen),
48 width, height);
49 }
50
51 static void
52 cairo_x_view_iface_paint_background (gpointer instance G_GNUC_UNUSED,
53 cairo_t *cr)
54 {
55 cairo_set_source_rgb (cr, 1, 1, 1);
56 cairo_paint (cr);
57
58 if (opt_bg_set)
59 {
60 cairo_set_source_rgba (cr,
61 opt_bg_color.red / 65535.,
62 opt_bg_color.green / 65535.,
63 opt_bg_color.blue / 65535.,
64 opt_bg_alpha / 65535.);
65 cairo_paint (cr);
66 }
67 }
68
69 static CairoViewerIface cairo_x_viewer_iface = {
70 &x_viewer,
71 cairo_x_view_iface_create_surface,
72 cairo_x_view_iface_paint_background
73 };
74 #endif /* HAVE_XFT */
75 #endif /* HAVE_CAIRO_XLIB */
76
77
78
79
80 #ifdef HAVE_CAIRO_QUARTZ
81 #include "viewer-cocoa.h"
82
83 static cairo_surface_t *
84 cairo_cocoa_view_iface_create_surface (gpointer instance,
85 gpointer surface,
86 int width,
87 int height)
88 {
89 return cairo_surface_reference (surface);
90 }
91
92 static void
93 cairo_cocoa_view_iface_paint_background (gpointer instance G_GNUC_UNUSED,
94 cairo_t *cr)
95 {
96 cairo_set_source_rgb (cr, 1, 1, 1);
97 cairo_paint (cr);
98
99 if (opt_bg_set)
100 {
101 cairo_set_source_rgba (cr,
102 opt_bg_color.red / 65535.,
103 opt_bg_color.green / 65535.,
104 opt_bg_color.blue / 65535.,
105 opt_bg_alpha / 65535.);
106 cairo_paint (cr);
107 }
108 }
109
110 static CairoViewerIface cairo_cocoa_viewer_iface = {
111 &cocoa_viewer,
112 cairo_cocoa_view_iface_create_surface,
113 cairo_cocoa_view_iface_paint_background
114 };
115 #endif /* HAVE_CAIRO_QUARTZ */
116
117
118
119 static cairo_surface_t *
120 cairo_view_iface_create_surface (gpointer instance,
121 gpointer surface,
122 int width,
123 int height)
124 {
125 return cairo_surface_reference (surface);
126 }
127
128
129
130 static gpointer
131 cairo_image_view_create (const PangoViewer *klass G_GNUC_UNUSED)
132 {
133 return NULL;
134 }
135
136 static void
137 cairo_image_view_destroy (gpointer instance G_GNUC_UNUSED)
138 {
139 }
140
141 static gpointer
142 cairo_image_view_create_surface (gpointer instance,
143 int width,
144 int height)
145 {
146 cairo_t *cr;
147 cairo_surface_t *surface;
148
149 /* TODO: Be smarter about format? */
150 surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, width, height);
151
152 cr = cairo_create (surface);
153 cairo_set_source_rgb (cr, 1.0, 1.0, 1.0);
154 cairo_paint (cr);
155 cairo_destroy (cr);
156
157 return surface;
158 }
159
160 static void
161 cairo_image_view_destroy_surface (gpointer instance,
162 gpointer surface)
163 {
164 cairo_surface_destroy (surface);
165 }
166
167 const PangoViewer cairo_image_viewer = {
168 "CairoImage",
169 NULL,
170 NULL,
171 cairo_image_view_create,
172 cairo_image_view_destroy,
173 NULL,
174 cairo_image_view_create_surface,
175 cairo_image_view_destroy_surface,
176 NULL,
177 NULL,
178 NULL,
179 NULL,
180 NULL
181 };
182
183 static void
184 cairo_image_view_iface_paint_background (gpointer instance G_GNUC_UNUSED,
185 cairo_t *cr)
186 {
187 cairo_set_source_rgb (cr, 1, 1, 1);
188 cairo_paint (cr);
189
190 if (opt_bg_set)
191 {
192 cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
193 cairo_set_source_rgba (cr,
194 opt_bg_color.red / 65535.,
195 opt_bg_color.green / 65535.,
196 opt_bg_color.blue / 65535.,
197 opt_bg_alpha / 65535.);
198 cairo_paint (cr);
199 }
200 }
201
202 static CairoViewerIface cairo_image_viewer_iface = {
203 &cairo_image_viewer,
204 cairo_view_iface_create_surface,
205 cairo_image_view_iface_paint_background
206 };
207
208
209
210
211 #ifdef CAIRO_HAS_SVG_SURFACE
212 # include <cairo-svg.h>
213 #endif
214 #ifdef CAIRO_HAS_PDF_SURFACE
215 # include <cairo-pdf.h>
216 #endif
217 #ifdef CAIRO_HAS_PS_SURFACE
218 # include <cairo-ps.h>
219 # if CAIRO_VERSION >= CAIRO_VERSION_ENCODE(1,6,0)
220 # define HAS_EPS 1
221
222 static cairo_surface_t *
223 _cairo_eps_surface_create (const char *filename,
224 double width,
225 double height)
226 {
227 cairo_surface_t *surface;
228
229 surface = cairo_ps_surface_create (filename, width, height);
230 cairo_ps_surface_set_eps (surface, TRUE);
231
232 return surface;
233 }
234
235 # else
236 # undef HAS_EPS
237 # endif
238 #endif
239
240 typedef cairo_surface_t *(*CairoVectorFileCreateFunc) (const char *filename,
241 double width,
242 double height);
243
244 typedef struct
245 {
246 const char *filename;
247 CairoVectorFileCreateFunc constructor;
248 } CairoVectorViewer;
249
250 static gpointer
251 cairo_vector_view_create (const PangoViewer *klass G_GNUC_UNUSED)
252 {
253 const char *extension = NULL;
254 CairoVectorFileCreateFunc constructor = NULL;
255
256 if (opt_output)
257 {
258 extension = strrchr (opt_output, '.');
259 if (extension)
260 extension++; /* skip the dot */
261 }
262
263 if (!extension)
264 return NULL;
265
266 if (0)
267 {}
268 #ifdef CAIRO_HAS_SVG_SURFACE
269 else if (0 == g_ascii_strcasecmp (extension, "svg"))
270 constructor = cairo_svg_surface_create;
271 #endif
272 #ifdef CAIRO_HAS_PDF_SURFACE
273 else if (0 == g_ascii_strcasecmp (extension, "pdf"))
274 constructor = cairo_pdf_surface_create;
275 #endif
276 #ifdef CAIRO_HAS_PS_SURFACE
277 else if (0 == g_ascii_strcasecmp (extension, "ps"))
278 constructor = cairo_ps_surface_create;
279 #ifdef HAS_EPS
280 else if (0 == g_ascii_strcasecmp (extension, "eps"))
281 constructor = _cairo_eps_surface_create;
282 #endif
283 #endif
284
285 if (constructor)
286 {
287 CairoVectorViewer *instance;
288
289 instance = g_slice_new (CairoVectorViewer);
290
291 /* save output filename and unset it such that the viewer layer
292 * doesn't try to save to file.
293 */
294 instance->filename = opt_output;
295 opt_output = NULL;
296
297 instance->constructor = constructor;
298
299 /* Fix dpi on 72. That's what cairo vector surfaces are. */
300 opt_dpi = 72;
301
302 return instance;
303 }
304
305 return NULL;
306 }
307
308 static void
309 cairo_vector_view_destroy (gpointer instance G_GNUC_UNUSED)
310 {
311 CairoVectorViewer *c = (CairoVectorViewer *) instance;
312
313 g_slice_free (CairoVectorViewer, c);
314 }
315
316 static gpointer
317 cairo_vector_view_create_surface (gpointer instance,
318 int width,
319 int height)
320 {
321 CairoVectorViewer *c = (CairoVectorViewer *) instance;
322 cairo_surface_t *surface;
323
324 surface = c->constructor (c->filename, width, height);
325
326 /*cairo_surface_set_fallback_resolution (surface, fallback_resolution_x, fallback_resolution_y);*/
327
328 return surface;
329 }
330
331 static void
332 cairo_vector_view_destroy_surface (gpointer instance,
333 gpointer surface)
334 {
335 /* TODO: check for errors */
336 cairo_surface_destroy (surface);
337 }
338
339 const PangoViewer cairo_vector_viewer = {
340 "CairoFile",
341 NULL,
342 NULL,
343 cairo_vector_view_create,
344 cairo_vector_view_destroy,
345 NULL,
346 cairo_vector_view_create_surface,
347 cairo_vector_view_destroy_surface,
348 NULL,
349 NULL,
350 NULL,
351 NULL,
352 NULL
353 };
354
355 static void
356 cairo_vector_view_iface_paint_background (gpointer instance G_GNUC_UNUSED,
357 cairo_t *cr)
358 {
359 if (opt_bg_set)
360 {
361 cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
362 cairo_set_source_rgba (cr,
363 opt_bg_color.red / 65535.,
364 opt_bg_color.green / 65535.,
365 opt_bg_color.blue / 65535.,
366 opt_bg_alpha / 65535.);
367 cairo_paint (cr);
368 }
369 }
370
371 static CairoViewerIface cairo_vector_viewer_iface = {
372 &cairo_vector_viewer,
373 cairo_view_iface_create_surface,
374 cairo_vector_view_iface_paint_background
375 };
376
377
378
379 gpointer
380 cairo_viewer_iface_create (const CairoViewerIface **iface)
381 {
382 gpointer ret;
383
384 *iface = &cairo_vector_viewer_iface;
385 ret = (*iface)->backend_class->create ((*iface)->backend_class);
386 if (ret)
387 return ret;
388
389 if (opt_display)
390 {
391 #ifdef HAVE_CAIRO_QUARTZ
392 *iface = &cairo_cocoa_viewer_iface;
393 return (*iface)->backend_class->create ((*iface)->backend_class);
394 #endif /* HAVE_CAIRO_QUARTZ */
395
396 #ifdef HAVE_CAIRO_XLIB
397 #ifdef HAVE_XFT
398 *iface = &cairo_x_viewer_iface;
399 return (*iface)->backend_class->create ((*iface)->backend_class);
400 #endif /* HAVE_XFT */
401 #endif /* HAVE_CAIRO_XLIB */
402 }
403
404 *iface = &cairo_image_viewer_iface;
405 return (*iface)->backend_class->create ((*iface)->backend_class);
406 }
407
408 void
409 cairo_viewer_add_options (GOptionGroup *group G_GNUC_UNUSED)
410 {
411 }