1 /* cairo - a vector graphics library with display and print output
3 * Copyright © 2005 Red Hat, Inc
4 * Copyright © 2007 Adrian Johnson
6 * This library is free software; you can redistribute it and/or
7 * modify it either under the terms of the GNU Lesser General Public
8 * License version 2.1 as published by the Free Software Foundation
9 * (the "LGPL") or, at your option, under the terms of the Mozilla
10 * Public License Version 1.1 (the "MPL"). If you do not alter this
11 * notice, a recipient may use your version of this file under either
12 * the MPL or the LGPL.
14 * You should have received a copy of the LGPL along with this library
15 * in the file COPYING-LGPL-2.1; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA
17 * You should have received a copy of the MPL along with this library
18 * in the file COPYING-MPL-1.1
20 * The contents of this file are subject to the Mozilla Public License
21 * Version 1.1 (the "License"); you may not use this file except in
22 * compliance with the License. You may obtain a copy of the License at
23 * http://www.mozilla.org/MPL/
25 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
26 * OF ANY KIND, either express or implied. See the LGPL or the MPL for
27 * the specific language governing rights and limitations.
29 * The Original Code is the cairo graphics library.
31 * The Initial Developer of the Original Code is Red Hat, Inc.
34 * Carl Worth <cworth@cworth.org>
35 * Keith Packard <keithp@keithp.com>
36 * Adrian Johnson <ajohnson@redneon.com>
39 /* The paginated surface layer exists to provide as much code sharing
40 * as possible for the various paginated surface backends in cairo
41 * (PostScript, PDF, etc.). See cairo-paginated-private.h for
42 * more details on how it works and how to use it.
47 #include "cairo-paginated-private.h"
48 #include "cairo-paginated-surface-private.h"
49 #include "cairo-recording-surface-private.h"
50 #include "cairo-analysis-surface-private.h"
51 #include "cairo-error-private.h"
52 #include "cairo-image-surface-private.h"
53 #include "cairo-surface-subsurface-inline.h"
55 static const cairo_surface_backend_t cairo_paginated_surface_backend;
57 static cairo_int_status_t
58 _cairo_paginated_surface_show_page (void *abstract_surface);
60 static cairo_surface_t *
61 _cairo_paginated_surface_create_similar (void *abstract_surface,
62 cairo_content_t content,
66 cairo_rectangle_t rect;
70 return cairo_recording_surface_create (content, &rect);
73 static cairo_surface_t *
74 _create_recording_surface_for_target (cairo_surface_t *target,
75 cairo_content_t content)
77 cairo_rectangle_int_t rect;
79 if (_cairo_surface_get_extents (target, &rect)) {
80 cairo_rectangle_t recording_extents;
82 recording_extents.x = rect.x;
83 recording_extents.y = rect.y;
84 recording_extents.width = rect.width;
85 recording_extents.height = rect.height;
87 return cairo_recording_surface_create (content, &recording_extents);
89 return cairo_recording_surface_create (content, NULL);
94 _cairo_paginated_surface_create (cairo_surface_t *target,
95 cairo_content_t content,
96 const cairo_paginated_surface_backend_t *backend)
98 cairo_paginated_surface_t *surface;
99 cairo_status_t status;
101 surface = malloc (sizeof (cairo_paginated_surface_t));
102 if (unlikely (surface == NULL)) {
103 status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
107 _cairo_surface_init (&surface->base,
108 &cairo_paginated_surface_backend,
112 /* Override surface->base.type with target's type so we don't leak
113 * evidence of the paginated wrapper out to the user. */
114 surface->base.type = target->type;
116 surface->target = cairo_surface_reference (target);
118 surface->content = content;
119 surface->backend = backend;
121 surface->recording_surface = _create_recording_surface_for_target (target, content);
122 status = surface->recording_surface->status;
123 if (unlikely (status))
124 goto FAIL_CLEANUP_SURFACE;
126 surface->page_num = 1;
127 surface->base.is_clear = TRUE;
129 return &surface->base;
131 FAIL_CLEANUP_SURFACE:
132 cairo_surface_destroy (target);
133 cairo_surface_destroy (surface->recording_surface);
136 return _cairo_surface_create_in_error (status);
140 _cairo_surface_is_paginated (cairo_surface_t *surface)
142 return surface->backend == &cairo_paginated_surface_backend;
146 _cairo_paginated_surface_get_target (cairo_surface_t *surface)
148 cairo_paginated_surface_t *paginated_surface;
150 assert (_cairo_surface_is_paginated (surface));
152 paginated_surface = (cairo_paginated_surface_t *) surface;
153 return paginated_surface->target;
157 _cairo_paginated_surface_get_recording (cairo_surface_t *surface)
159 cairo_paginated_surface_t *paginated_surface;
161 assert (_cairo_surface_is_paginated (surface));
163 paginated_surface = (cairo_paginated_surface_t *) surface;
164 return paginated_surface->recording_surface;
168 _cairo_paginated_surface_set_size (cairo_surface_t *surface,
172 cairo_paginated_surface_t *paginated_surface;
173 cairo_status_t status;
174 cairo_rectangle_t recording_extents;
176 assert (_cairo_surface_is_paginated (surface));
178 paginated_surface = (cairo_paginated_surface_t *) surface;
180 recording_extents.x = 0;
181 recording_extents.y = 0;
182 recording_extents.width = width;
183 recording_extents.height = height;
185 cairo_surface_destroy (paginated_surface->recording_surface);
186 paginated_surface->recording_surface = cairo_recording_surface_create (paginated_surface->content,
188 status = paginated_surface->recording_surface->status;
189 if (unlikely (status))
190 return _cairo_surface_set_error (surface, status);
192 return CAIRO_STATUS_SUCCESS;
195 static cairo_status_t
196 _cairo_paginated_surface_finish (void *abstract_surface)
198 cairo_paginated_surface_t *surface = abstract_surface;
199 cairo_status_t status = CAIRO_STATUS_SUCCESS;
201 if (! surface->base.is_clear || surface->page_num == 1) {
202 /* Bypass some of the sanity checking in cairo-surface.c, as we
203 * know that the surface is finished...
205 status = _cairo_paginated_surface_show_page (surface);
208 /* XXX We want to propagate any errors from destroy(), but those are not
209 * returned via the api. So we need to explicitly finish the target,
210 * and check the status afterwards. However, we can only call finish()
211 * on the target, if we own it.
213 if (CAIRO_REFERENCE_COUNT_GET_VALUE (&surface->target->ref_count) == 1)
214 cairo_surface_finish (surface->target);
215 if (status == CAIRO_STATUS_SUCCESS)
216 status = cairo_surface_status (surface->target);
217 cairo_surface_destroy (surface->target);
219 cairo_surface_finish (surface->recording_surface);
220 if (status == CAIRO_STATUS_SUCCESS)
221 status = cairo_surface_status (surface->recording_surface);
222 cairo_surface_destroy (surface->recording_surface);
227 static cairo_surface_t *
228 _cairo_paginated_surface_create_image_surface (void *abstract_surface,
232 cairo_paginated_surface_t *surface = abstract_surface;
233 cairo_surface_t *image;
234 cairo_font_options_t options;
236 image = _cairo_image_surface_create_with_content (surface->content,
240 cairo_surface_get_font_options (&surface->base, &options);
241 _cairo_surface_set_font_options (image, &options);
246 static cairo_surface_t *
247 _cairo_paginated_surface_source (void *abstract_surface,
248 cairo_rectangle_int_t *extents)
250 cairo_paginated_surface_t *surface = abstract_surface;
251 return _cairo_surface_get_source (surface->target, extents);
254 static cairo_status_t
255 _cairo_paginated_surface_acquire_source_image (void *abstract_surface,
256 cairo_image_surface_t **image_out,
259 cairo_paginated_surface_t *surface = abstract_surface;
260 cairo_bool_t is_bounded;
261 cairo_surface_t *image;
262 cairo_status_t status;
263 cairo_rectangle_int_t extents;
265 is_bounded = _cairo_surface_get_extents (surface->target, &extents);
267 return CAIRO_INT_STATUS_UNSUPPORTED;
269 image = _cairo_paginated_surface_create_image_surface (surface,
273 status = _cairo_recording_surface_replay (surface->recording_surface, image);
274 if (unlikely (status)) {
275 cairo_surface_destroy (image);
279 *image_out = (cairo_image_surface_t*) image;
282 return CAIRO_STATUS_SUCCESS;
286 _cairo_paginated_surface_release_source_image (void *abstract_surface,
287 cairo_image_surface_t *image,
290 cairo_surface_destroy (&image->base);
293 static cairo_int_status_t
294 _paint_fallback_image (cairo_paginated_surface_t *surface,
295 cairo_rectangle_int_t *rect)
297 double x_scale = surface->base.x_fallback_resolution / surface->target->x_resolution;
298 double y_scale = surface->base.y_fallback_resolution / surface->target->y_resolution;
299 int x, y, width, height;
300 cairo_status_t status;
301 cairo_surface_t *image;
302 cairo_surface_pattern_t pattern;
308 height = rect->height;
309 image = _cairo_paginated_surface_create_image_surface (surface,
310 ceil (width * x_scale),
311 ceil (height * y_scale));
312 _cairo_surface_set_device_scale (image, x_scale, y_scale);
313 /* set_device_offset just sets the x0/y0 components of the matrix;
314 * so we have to do the scaling manually. */
315 cairo_surface_set_device_offset (image, -x*x_scale, -y*y_scale);
317 status = _cairo_recording_surface_replay (surface->recording_surface, image);
318 if (unlikely (status))
321 _cairo_pattern_init_for_surface (&pattern, image);
322 cairo_matrix_init (&pattern.base.matrix,
323 x_scale, 0, 0, y_scale, -x*x_scale, -y*y_scale);
324 /* the fallback should be rendered at native resolution, so disable
325 * filtering (if possible) to avoid introducing potential artifacts. */
326 pattern.base.filter = CAIRO_FILTER_NEAREST;
328 clip = _cairo_clip_intersect_rectangle (NULL, rect);
329 status = _cairo_surface_paint (surface->target,
330 CAIRO_OPERATOR_SOURCE,
331 &pattern.base, clip);
332 _cairo_clip_destroy (clip);
333 _cairo_pattern_fini (&pattern.base);
336 cairo_surface_destroy (image);
341 static cairo_int_status_t
342 _paint_page (cairo_paginated_surface_t *surface)
344 cairo_surface_t *analysis;
345 cairo_int_status_t status;
346 cairo_bool_t has_supported, has_page_fallback, has_finegrained_fallback;
348 if (unlikely (surface->target->status))
349 return surface->target->status;
351 analysis = _cairo_analysis_surface_create (surface->target);
352 if (unlikely (analysis->status)) {
353 status = analysis->status;
357 surface->backend->set_paginated_mode (surface->target,
358 CAIRO_PAGINATED_MODE_ANALYZE);
359 status = _cairo_recording_surface_replay_and_create_regions (surface->recording_surface,
364 assert (analysis->status == CAIRO_STATUS_SUCCESS);
366 if (surface->backend->set_bounding_box) {
369 _cairo_analysis_surface_get_bounding_box (analysis, &bbox);
370 status = surface->backend->set_bounding_box (surface->target, &bbox);
371 if (unlikely (status))
375 if (surface->backend->set_fallback_images_required) {
376 cairo_bool_t has_fallbacks = _cairo_analysis_surface_has_unsupported (analysis);
378 status = surface->backend->set_fallback_images_required (surface->target,
380 if (unlikely (status))
384 /* Finer grained fallbacks are currently only supported for some
386 if (surface->backend->supports_fine_grained_fallbacks != NULL &&
387 surface->backend->supports_fine_grained_fallbacks (surface->target))
389 has_supported = _cairo_analysis_surface_has_supported (analysis);
390 has_page_fallback = FALSE;
391 has_finegrained_fallback = _cairo_analysis_surface_has_unsupported (analysis);
395 if (_cairo_analysis_surface_has_unsupported (analysis)) {
396 has_supported = FALSE;
397 has_page_fallback = TRUE;
399 has_supported = TRUE;
400 has_page_fallback = FALSE;
402 has_finegrained_fallback = FALSE;
406 surface->backend->set_paginated_mode (surface->target,
407 CAIRO_PAGINATED_MODE_RENDER);
409 status = _cairo_recording_surface_replay_region (surface->recording_surface,
412 CAIRO_RECORDING_REGION_NATIVE);
413 assert (status != CAIRO_INT_STATUS_UNSUPPORTED);
414 if (unlikely (status))
418 if (has_page_fallback) {
419 cairo_rectangle_int_t extents;
420 cairo_bool_t is_bounded;
422 surface->backend->set_paginated_mode (surface->target,
423 CAIRO_PAGINATED_MODE_FALLBACK);
425 is_bounded = _cairo_surface_get_extents (surface->target, &extents);
427 status = CAIRO_INT_STATUS_UNSUPPORTED;
431 status = _paint_fallback_image (surface, &extents);
432 if (unlikely (status))
436 if (has_finegrained_fallback) {
437 cairo_region_t *region;
440 surface->backend->set_paginated_mode (surface->target,
441 CAIRO_PAGINATED_MODE_FALLBACK);
443 region = _cairo_analysis_surface_get_unsupported (analysis);
445 num_rects = cairo_region_num_rectangles (region);
446 for (i = 0; i < num_rects; i++) {
447 cairo_rectangle_int_t rect;
449 cairo_region_get_rectangle (region, i, &rect);
450 status = _paint_fallback_image (surface, &rect);
451 if (unlikely (status))
457 cairo_surface_destroy (analysis);
459 return _cairo_surface_set_error (surface->target, status);
462 static cairo_status_t
463 _start_page (cairo_paginated_surface_t *surface)
465 if (surface->target->status)
466 return surface->target->status;
468 if (! surface->backend->start_page)
469 return CAIRO_STATUS_SUCCESS;
471 return _cairo_surface_set_error (surface->target,
472 surface->backend->start_page (surface->target));
475 static cairo_int_status_t
476 _cairo_paginated_surface_copy_page (void *abstract_surface)
478 cairo_status_t status;
479 cairo_paginated_surface_t *surface = abstract_surface;
481 status = _start_page (surface);
482 if (unlikely (status))
485 status = _paint_page (surface);
486 if (unlikely (status))
491 /* XXX: It might make sense to add some support here for calling
492 * cairo_surface_copy_page on the target surface. It would be an
493 * optimization for the output, but the interaction with image
494 * fallbacks gets tricky. For now, we just let the target see a
495 * show_page and we implement the copying by simply not destroying
496 * the recording-surface. */
498 cairo_surface_show_page (surface->target);
499 return cairo_surface_status (surface->target);
502 static cairo_int_status_t
503 _cairo_paginated_surface_show_page (void *abstract_surface)
505 cairo_status_t status;
506 cairo_paginated_surface_t *surface = abstract_surface;
508 status = _start_page (surface);
509 if (unlikely (status))
512 status = _paint_page (surface);
513 if (unlikely (status))
516 cairo_surface_show_page (surface->target);
517 status = surface->target->status;
518 if (unlikely (status))
521 status = surface->recording_surface->status;
522 if (unlikely (status))
525 if (! surface->base.finished) {
526 cairo_surface_destroy (surface->recording_surface);
528 surface->recording_surface = _create_recording_surface_for_target (surface->target,
530 status = surface->recording_surface->status;
531 if (unlikely (status))
535 surface->base.is_clear = TRUE;
538 return CAIRO_STATUS_SUCCESS;
542 _cairo_paginated_surface_get_extents (void *abstract_surface,
543 cairo_rectangle_int_t *rectangle)
545 cairo_paginated_surface_t *surface = abstract_surface;
547 return _cairo_surface_get_extents (surface->target, rectangle);
551 _cairo_paginated_surface_get_font_options (void *abstract_surface,
552 cairo_font_options_t *options)
554 cairo_paginated_surface_t *surface = abstract_surface;
556 cairo_surface_get_font_options (surface->target, options);
559 static cairo_int_status_t
560 _cairo_paginated_surface_paint (void *abstract_surface,
562 const cairo_pattern_t *source,
563 const cairo_clip_t *clip)
565 cairo_paginated_surface_t *surface = abstract_surface;
567 return _cairo_surface_paint (surface->recording_surface, op, source, clip);
570 static cairo_int_status_t
571 _cairo_paginated_surface_mask (void *abstract_surface,
573 const cairo_pattern_t *source,
574 const cairo_pattern_t *mask,
575 const cairo_clip_t *clip)
577 cairo_paginated_surface_t *surface = abstract_surface;
579 return _cairo_surface_mask (surface->recording_surface, op, source, mask, clip);
582 static cairo_int_status_t
583 _cairo_paginated_surface_stroke (void *abstract_surface,
585 const cairo_pattern_t *source,
586 const cairo_path_fixed_t *path,
587 const cairo_stroke_style_t *style,
588 const cairo_matrix_t *ctm,
589 const cairo_matrix_t *ctm_inverse,
591 cairo_antialias_t antialias,
592 const cairo_clip_t *clip)
594 cairo_paginated_surface_t *surface = abstract_surface;
596 return _cairo_surface_stroke (surface->recording_surface, op, source,
599 tolerance, antialias,
603 static cairo_int_status_t
604 _cairo_paginated_surface_fill (void *abstract_surface,
606 const cairo_pattern_t *source,
607 const cairo_path_fixed_t *path,
608 cairo_fill_rule_t fill_rule,
610 cairo_antialias_t antialias,
611 const cairo_clip_t *clip)
613 cairo_paginated_surface_t *surface = abstract_surface;
615 return _cairo_surface_fill (surface->recording_surface, op, source,
617 tolerance, antialias,
622 _cairo_paginated_surface_has_show_text_glyphs (void *abstract_surface)
624 cairo_paginated_surface_t *surface = abstract_surface;
626 return cairo_surface_has_show_text_glyphs (surface->target);
629 static cairo_int_status_t
630 _cairo_paginated_surface_show_text_glyphs (void *abstract_surface,
632 const cairo_pattern_t *source,
635 cairo_glyph_t *glyphs,
637 const cairo_text_cluster_t *clusters,
639 cairo_text_cluster_flags_t cluster_flags,
640 cairo_scaled_font_t *scaled_font,
641 const cairo_clip_t *clip)
643 cairo_paginated_surface_t *surface = abstract_surface;
645 return _cairo_surface_show_text_glyphs (surface->recording_surface, op, source,
648 clusters, num_clusters,
655 _cairo_paginated_surface_get_supported_mime_types (void *abstract_surface)
657 cairo_paginated_surface_t *surface = abstract_surface;
659 if (surface->target->backend->get_supported_mime_types)
660 return surface->target->backend->get_supported_mime_types (surface->target);
665 static cairo_surface_t *
666 _cairo_paginated_surface_snapshot (void *abstract_other)
668 cairo_paginated_surface_t *other = abstract_other;
670 return other->recording_surface->backend->snapshot (other->recording_surface);
674 _cairo_paginated_context_create (void *target)
676 cairo_paginated_surface_t *surface = target;
678 if (_cairo_surface_is_subsurface (&surface->base))
679 surface = (cairo_paginated_surface_t *)
680 _cairo_surface_subsurface_get_target (&surface->base);
682 return surface->recording_surface->backend->create_context (target);
685 static const cairo_surface_backend_t cairo_paginated_surface_backend = {
686 CAIRO_INTERNAL_SURFACE_TYPE_PAGINATED,
687 _cairo_paginated_surface_finish,
689 _cairo_paginated_context_create,
691 _cairo_paginated_surface_create_similar,
692 NULL, /* create simlar image */
693 NULL, /* map to image */
694 NULL, /* unmap image */
696 _cairo_paginated_surface_source,
697 _cairo_paginated_surface_acquire_source_image,
698 _cairo_paginated_surface_release_source_image,
699 _cairo_paginated_surface_snapshot,
701 _cairo_paginated_surface_copy_page,
702 _cairo_paginated_surface_show_page,
704 _cairo_paginated_surface_get_extents,
705 _cairo_paginated_surface_get_font_options,
708 NULL, /* mark_dirty_rectangle */
710 _cairo_paginated_surface_paint,
711 _cairo_paginated_surface_mask,
712 _cairo_paginated_surface_stroke,
713 _cairo_paginated_surface_fill,
714 NULL, /* fill_stroke */
715 NULL, /* show_glyphs */
716 _cairo_paginated_surface_has_show_text_glyphs,
717 _cairo_paginated_surface_show_text_glyphs,
718 _cairo_paginated_surface_get_supported_mime_types,