1 /* -*- Mode: c; tab-width: 8; c-basic-offset: 4; indent-tabs-mode: t; -*- */
2 /* cairo - a vector graphics library with display and print output
4 * Copyright © 2002 University of Southern California
5 * Copyright © 2005 Red Hat, Inc.
7 * This library is free software; you can redistribute it and/or
8 * modify it either under the terms of the GNU Lesser General Public
9 * License version 2.1 as published by the Free Software Foundation
10 * (the "LGPL") or, at your option, under the terms of the Mozilla
11 * Public License Version 1.1 (the "MPL"). If you do not alter this
12 * notice, a recipient may use your version of this file under either
13 * the MPL or the LGPL.
15 * You should have received a copy of the LGPL along with this library
16 * in the file COPYING-LGPL-2.1; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA
18 * You should have received a copy of the MPL along with this library
19 * in the file COPYING-MPL-1.1
21 * The contents of this file are subject to the Mozilla Public License
22 * Version 1.1 (the "License"); you may not use this file except in
23 * compliance with the License. You may obtain a copy of the License at
24 * http://www.mozilla.org/MPL/
26 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
27 * OF ANY KIND, either express or implied. See the LGPL or the MPL for
28 * the specific language governing rights and limitations.
30 * The Original Code is the cairo graphics library.
32 * The Initial Developer of the Original Code is University of Southern
36 * Carl D. Worth <cworth@cworth.org>
41 #include "cairo-array-private.h"
42 #include "cairo-clip-inline.h"
43 #include "cairo-clip-private.h"
44 #include "cairo-damage-private.h"
45 #include "cairo-device-private.h"
46 #include "cairo-error-private.h"
47 #include "cairo-list-inline.h"
48 #include "cairo-image-surface-inline.h"
49 #include "cairo-recording-surface-private.h"
50 #include "cairo-region-private.h"
51 #include "cairo-surface-inline.h"
52 #include "cairo-tee-surface-private.h"
53 #include "cairo-surface-subsurface-private.h"
56 * SECTION:cairo-surface
57 * @Title: cairo_surface_t
58 * @Short_Description: Base class for surfaces
59 * @See_Also: #cairo_t, #cairo_pattern_t
61 * #cairo_surface_t is the abstract type representing all different drawing
62 * targets that cairo can render to. The actual drawings are
63 * performed using a cairo <firstterm>context</firstterm>.
65 * A cairo surface is created by using <firstterm>backend</firstterm>-specific
66 * constructors, typically of the form
67 * <function>cairo_<emphasis>backend</emphasis>_surface_create(<!-- -->)</function>.
69 * Most surface types allow accessing the surface without using Cairo
70 * functions. If you do this, keep in mind that it is mandatory that you call
71 * cairo_surface_flush() before reading from or writing to the surface and that
72 * you must use cairo_surface_mark_dirty() after modifying it.
74 * <title>Directly modifying an image surface</title>
77 * modify_image_surface (cairo_surface_t *surface)
79 * unsigned char *data;
80 * int width, height, stride;
82 * // flush to ensure all writing to the image was done
83 * cairo_surface_flush (surface);
86 * data = cairo_image_surface_get_data (surface);
87 * width = cairo_image_surface_get_width (surface);
88 * height = cairo_image_surface_get_height (surface);
89 * stride = cairo_image_surface_get_stride (surface);
90 * modify_image_data (data, width, height, stride);
92 * // mark the image dirty so Cairo clears its caches.
93 * cairo_surface_mark_dirty (surface);
97 * Note that for other surface types it might be necessary to acquire the
98 * surface's device first. See cairo_device_acquire() for a discussion of
102 #define DEFINE_NIL_SURFACE(status, name) \
103 const cairo_surface_t name = { \
104 NULL, /* backend */ \
106 CAIRO_SURFACE_TYPE_IMAGE, /* type */ \
107 CAIRO_CONTENT_COLOR, /* content */ \
108 CAIRO_REFERENCE_COUNT_INVALID, /* ref_count */ \
109 status, /* status */ \
113 FALSE, /* _finishing */ \
114 FALSE, /* finished */ \
115 TRUE, /* is_clear */ \
116 FALSE, /* has_font_options */ \
117 FALSE, /* owns_device */ \
118 { 0, 0, 0, NULL, }, /* user_data */ \
119 { 0, 0, 0, NULL, }, /* mime_data */ \
120 { 1.0, 0.0, 0.0, 1.0, 0.0, 0.0 }, /* device_transform */ \
121 { 1.0, 0.0, 0.0, 1.0, 0.0, 0.0 }, /* device_transform_inverse */ \
122 { NULL, NULL }, /* device_transform_observers */ \
123 0.0, /* x_resolution */ \
124 0.0, /* y_resolution */ \
125 0.0, /* x_fallback_resolution */ \
126 0.0, /* y_fallback_resolution */ \
127 NULL, /* snapshot_of */ \
128 NULL, /* snapshot_detach */ \
129 NULL, /* snapshot_subsurface */ \
130 { NULL, NULL }, /* snapshots */ \
131 { NULL, NULL }, /* snapshot */ \
132 { CAIRO_ANTIALIAS_DEFAULT, /* antialias */ \
133 CAIRO_SUBPIXEL_ORDER_DEFAULT, /* subpixel_order */ \
134 CAIRO_LCD_FILTER_DEFAULT, /* lcd_filter */ \
135 CAIRO_HINT_STYLE_DEFAULT, /* hint_style */ \
136 CAIRO_HINT_METRICS_DEFAULT, /* hint_metrics */ \
137 CAIRO_ROUND_GLYPH_POS_DEFAULT /* round_glyph_positions */ \
138 } /* font_options */ \
141 /* XXX error object! */
143 static DEFINE_NIL_SURFACE(CAIRO_STATUS_NO_MEMORY, _cairo_surface_nil);
144 static DEFINE_NIL_SURFACE(CAIRO_STATUS_SURFACE_TYPE_MISMATCH, _cairo_surface_nil_surface_type_mismatch);
145 static DEFINE_NIL_SURFACE(CAIRO_STATUS_INVALID_STATUS, _cairo_surface_nil_invalid_status);
146 static DEFINE_NIL_SURFACE(CAIRO_STATUS_INVALID_CONTENT, _cairo_surface_nil_invalid_content);
147 static DEFINE_NIL_SURFACE(CAIRO_STATUS_INVALID_FORMAT, _cairo_surface_nil_invalid_format);
148 static DEFINE_NIL_SURFACE(CAIRO_STATUS_INVALID_VISUAL, _cairo_surface_nil_invalid_visual);
149 static DEFINE_NIL_SURFACE(CAIRO_STATUS_FILE_NOT_FOUND, _cairo_surface_nil_file_not_found);
150 static DEFINE_NIL_SURFACE(CAIRO_STATUS_TEMP_FILE_ERROR, _cairo_surface_nil_temp_file_error);
151 static DEFINE_NIL_SURFACE(CAIRO_STATUS_READ_ERROR, _cairo_surface_nil_read_error);
152 static DEFINE_NIL_SURFACE(CAIRO_STATUS_WRITE_ERROR, _cairo_surface_nil_write_error);
153 static DEFINE_NIL_SURFACE(CAIRO_STATUS_INVALID_STRIDE, _cairo_surface_nil_invalid_stride);
154 static DEFINE_NIL_SURFACE(CAIRO_STATUS_INVALID_SIZE, _cairo_surface_nil_invalid_size);
155 static DEFINE_NIL_SURFACE(CAIRO_STATUS_DEVICE_TYPE_MISMATCH, _cairo_surface_nil_device_type_mismatch);
156 static DEFINE_NIL_SURFACE(CAIRO_STATUS_DEVICE_ERROR, _cairo_surface_nil_device_error);
158 static DEFINE_NIL_SURFACE(CAIRO_INT_STATUS_UNSUPPORTED, _cairo_surface_nil_unsupported);
159 static DEFINE_NIL_SURFACE(CAIRO_INT_STATUS_NOTHING_TO_DO, _cairo_surface_nil_nothing_to_do);
161 static void _cairo_surface_finish_snapshots (cairo_surface_t *surface);
162 static void _cairo_surface_finish (cairo_surface_t *surface);
165 * _cairo_surface_set_error:
166 * @surface: a surface
167 * @status: a status value indicating an error
169 * Atomically sets surface->status to @status and calls _cairo_error;
170 * Does nothing if status is %CAIRO_STATUS_SUCCESS or any of the internal
173 * All assignments of an error status to surface->status should happen
174 * through _cairo_surface_set_error(). Note that due to the nature of
175 * the atomic operation, it is not safe to call this function on the
178 * The purpose of this function is to allow the user to set a
179 * breakpoint in _cairo_error() to generate a stack trace for when the
180 * user causes cairo to detect an error.
182 * Return value: the error status.
185 _cairo_surface_set_error (cairo_surface_t *surface,
186 cairo_int_status_t status)
188 /* NOTHING_TO_DO is magic. We use it to break out of the inner-most
189 * surface function, but anything higher just sees "success".
191 if (status == CAIRO_INT_STATUS_NOTHING_TO_DO)
192 status = CAIRO_INT_STATUS_SUCCESS;
194 if (status == CAIRO_INT_STATUS_SUCCESS ||
195 status >= (int)CAIRO_INT_STATUS_LAST_STATUS)
198 /* Don't overwrite an existing error. This preserves the first
199 * error, which is the most significant. */
200 _cairo_status_set_error (&surface->status, (cairo_status_t)status);
202 return _cairo_error (status);
206 * cairo_surface_get_type:
207 * @surface: a #cairo_surface_t
209 * This function returns the type of the backend used to create
210 * a surface. See #cairo_surface_type_t for available types.
212 * Return value: The type of @surface.
217 cairo_surface_get_type (cairo_surface_t *surface)
219 /* We don't use surface->backend->type here so that some of the
220 * special "wrapper" surfaces such as cairo_paginated_surface_t
221 * can override surface->type with the type of the "child"
223 return surface->type;
227 * cairo_surface_get_content:
228 * @surface: a #cairo_surface_t
230 * This function returns the content type of @surface which indicates
231 * whether the surface contains color and/or alpha information. See
234 * Return value: The content type of @surface.
239 cairo_surface_get_content (cairo_surface_t *surface)
241 return surface->content;
245 * cairo_surface_status:
246 * @surface: a #cairo_surface_t
248 * Checks whether an error has previously occurred for this
251 * Return value: %CAIRO_STATUS_SUCCESS, %CAIRO_STATUS_NULL_POINTER,
252 * %CAIRO_STATUS_NO_MEMORY, %CAIRO_STATUS_READ_ERROR,
253 * %CAIRO_STATUS_INVALID_CONTENT, %CAIRO_STATUS_INVALID_FORMAT, or
254 * %CAIRO_STATUS_INVALID_VISUAL.
259 cairo_surface_status (cairo_surface_t *surface)
261 return surface->status;
263 slim_hidden_def (cairo_surface_status);
266 _cairo_surface_allocate_unique_id (void)
268 static cairo_atomic_int_t unique_id;
271 if (++unique_id == 0)
275 cairo_atomic_int_t old, id;
278 old = _cairo_atomic_uint_get (&unique_id);
282 } while (! _cairo_atomic_uint_cmpxchg (&unique_id, old, id));
289 * cairo_surface_get_device:
290 * @surface: a #cairo_surface_t
292 * This function returns the device for a @surface.
293 * See #cairo_device_t.
295 * Return value: The device for @surface or %NULL if the surface does
296 * not have an associated device.
301 cairo_surface_get_device (cairo_surface_t *surface)
303 if (unlikely (surface->status))
304 return _cairo_device_create_in_error (surface->status);
306 return surface->device;
310 _cairo_surface_has_snapshots (cairo_surface_t *surface)
312 return ! cairo_list_is_empty (&surface->snapshots);
316 _cairo_surface_has_mime_data (cairo_surface_t *surface)
318 return surface->mime_data.num_elements != 0;
322 _cairo_surface_detach_mime_data (cairo_surface_t *surface)
324 if (! _cairo_surface_has_mime_data (surface))
327 _cairo_user_data_array_fini (&surface->mime_data);
328 _cairo_user_data_array_init (&surface->mime_data);
332 _cairo_surface_detach_snapshots (cairo_surface_t *surface)
334 while (_cairo_surface_has_snapshots (surface)) {
335 _cairo_surface_detach_snapshot (cairo_list_first_entry (&surface->snapshots,
342 _cairo_surface_detach_snapshot (cairo_surface_t *snapshot)
344 assert (snapshot->snapshot_of != NULL);
346 snapshot->snapshot_of = NULL;
347 cairo_list_del (&snapshot->snapshot);
349 if (snapshot->snapshot_detach != NULL) {
350 /* If this is a snapshot of a subsurface, call snapshot_detach with the
351 * parent subsurface, so that it can clear its snapshot. */
352 if (snapshot->snapshot_subsurface)
353 snapshot->snapshot_detach (snapshot->snapshot_subsurface);
355 snapshot->snapshot_detach (snapshot);
358 cairo_surface_destroy (snapshot);
362 _cairo_surface_attach_snapshot_internal (cairo_surface_t *surface,
363 cairo_surface_t *snapshot,
364 cairo_surface_func_t detach_func)
366 assert (surface != snapshot);
367 assert (snapshot->snapshot_of != surface);
369 cairo_surface_reference (snapshot);
371 if (snapshot->snapshot_of != NULL)
372 _cairo_surface_detach_snapshot (snapshot);
374 snapshot->snapshot_of = surface;
375 snapshot->snapshot_detach = detach_func;
377 cairo_list_add (&snapshot->snapshot, &surface->snapshots);
381 _cairo_surface_attach_snapshot (cairo_surface_t *surface,
382 cairo_surface_t *snapshot,
383 cairo_surface_func_t detach_func)
385 _cairo_surface_attach_snapshot_internal (surface, snapshot, detach_func);
386 assert (_cairo_surface_has_snapshot (surface, snapshot->backend) == snapshot);
390 _cairo_surface_attach_subsurface_snapshot (cairo_surface_t *surface,
391 cairo_surface_subsurface_t *subsurface,
392 cairo_surface_t *snapshot,
393 cairo_surface_func_t detach_func)
395 _cairo_surface_attach_snapshot_internal (surface, snapshot, detach_func);
396 snapshot->snapshot_subsurface = &subsurface->base;
400 _cairo_surface_has_snapshot (cairo_surface_t *surface,
401 const cairo_surface_backend_t *backend)
403 cairo_surface_t *snapshot;
405 cairo_list_foreach_entry (snapshot, cairo_surface_t,
406 &surface->snapshots, snapshot)
408 if (snapshot->backend == backend &&
409 snapshot->snapshot_subsurface == NULL)
417 _cairo_surface_begin_modification (cairo_surface_t *surface)
419 assert (surface->status == CAIRO_STATUS_SUCCESS);
420 assert (! surface->finished);
422 return _cairo_surface_flush (surface, 1);
426 _cairo_surface_init (cairo_surface_t *surface,
427 const cairo_surface_backend_t *backend,
428 cairo_device_t *device,
429 cairo_content_t content)
431 CAIRO_MUTEX_INITIALIZE ();
433 surface->backend = backend;
434 surface->device = cairo_device_reference (device);
435 surface->content = content;
436 surface->type = backend->type;
438 CAIRO_REFERENCE_COUNT_INIT (&surface->ref_count, 1);
439 surface->status = CAIRO_STATUS_SUCCESS;
440 surface->unique_id = _cairo_surface_allocate_unique_id ();
441 surface->finished = FALSE;
442 surface->_finishing = FALSE;
443 surface->is_clear = FALSE;
445 surface->damage = NULL;
446 surface->owns_device = (device != NULL);
448 _cairo_user_data_array_init (&surface->user_data);
449 _cairo_user_data_array_init (&surface->mime_data);
451 cairo_matrix_init_identity (&surface->device_transform);
452 cairo_matrix_init_identity (&surface->device_transform_inverse);
453 cairo_list_init (&surface->device_transform_observers);
455 surface->x_resolution = CAIRO_SURFACE_RESOLUTION_DEFAULT;
456 surface->y_resolution = CAIRO_SURFACE_RESOLUTION_DEFAULT;
458 surface->x_fallback_resolution = CAIRO_SURFACE_FALLBACK_RESOLUTION_DEFAULT;
459 surface->y_fallback_resolution = CAIRO_SURFACE_FALLBACK_RESOLUTION_DEFAULT;
461 cairo_list_init (&surface->snapshots);
462 surface->snapshot_of = NULL;
463 surface->snapshot_subsurface = NULL;
465 surface->has_font_options = FALSE;
469 _cairo_surface_copy_similar_properties (cairo_surface_t *surface,
470 cairo_surface_t *other)
472 if (other->has_font_options || other->backend != surface->backend) {
473 cairo_font_options_t options;
475 cairo_surface_get_font_options (other, &options);
476 _cairo_surface_set_font_options (surface, &options);
479 cairo_surface_set_fallback_resolution (surface,
480 other->x_fallback_resolution,
481 other->y_fallback_resolution);
485 _cairo_surface_create_similar_scratch (cairo_surface_t *other,
486 cairo_content_t content,
490 cairo_surface_t *surface;
492 if (unlikely (other->status))
493 return _cairo_surface_create_in_error (other->status);
496 if (other->backend->create_similar)
497 surface = other->backend->create_similar (other, content, width, height);
499 surface = cairo_surface_create_similar_image (other,
500 _cairo_format_from_content (content),
503 if (unlikely (surface->status))
506 _cairo_surface_copy_similar_properties (surface, other);
512 * cairo_surface_create_similar:
513 * @other: an existing surface used to select the backend of the new surface
514 * @content: the content for the new surface
515 * @width: width of the new surface, (in device-space units)
516 * @height: height of the new surface (in device-space units)
518 * Create a new surface that is as compatible as possible with an
519 * existing surface. For example the new surface will have the same
520 * fallback resolution and font options as @other. Generally, the new
521 * surface will also use the same backend as @other, unless that is
522 * not possible for some reason. The type of the returned surface may
523 * be examined with cairo_surface_get_type().
525 * Initially the surface contents are all 0 (transparent if contents
526 * have transparency, black otherwise.)
528 * Use cairo_surface_create_similar_image() if you need an image surface
529 * which can be painted quickly to the target surface.
531 * Return value: a pointer to the newly allocated surface. The caller
532 * owns the surface and should call cairo_surface_destroy() when done
535 * This function always returns a valid pointer, but it will return a
536 * pointer to a "nil" surface if @other is already in an error state
537 * or any other error occurs.
542 cairo_surface_create_similar (cairo_surface_t *other,
543 cairo_content_t content,
547 cairo_surface_t *surface;
549 if (unlikely (other->status))
550 return _cairo_surface_create_in_error (other->status);
551 if (unlikely (other->finished))
552 return _cairo_surface_create_in_error (CAIRO_STATUS_SURFACE_FINISHED);
553 if (unlikely (width < 0 || height < 0))
554 return _cairo_surface_create_in_error (CAIRO_STATUS_INVALID_SIZE);
556 if (unlikely (! CAIRO_CONTENT_VALID (content)))
557 return _cairo_surface_create_in_error (CAIRO_STATUS_INVALID_CONTENT);
559 surface = _cairo_surface_create_similar_solid (other,
560 content, width, height,
561 CAIRO_COLOR_TRANSPARENT);
562 assert (surface->is_clear);
568 * cairo_surface_create_similar_image:
569 * @other: an existing surface used to select the preference of the new surface
570 * @format: the format for the new surface
571 * @width: width of the new surface, (in device-space units)
572 * @height: height of the new surface (in device-space units)
574 * Create a new image surface that is as compatible as possible for uploading
575 * to and the use in conjunction with an existing surface. However, this surface
576 * can still be used like any normal image surface.
578 * Initially the surface contents are all 0 (transparent if contents
579 * have transparency, black otherwise.)
581 * Use cairo_surface_create_similar() if you don't need an image surface.
583 * Return value: a pointer to the newly allocated image surface. The caller
584 * owns the surface and should call cairo_surface_destroy() when done
587 * This function always returns a valid pointer, but it will return a
588 * pointer to a "nil" surface if @other is already in an error state
589 * or any other error occurs.
594 cairo_surface_create_similar_image (cairo_surface_t *other,
595 cairo_format_t format,
599 cairo_surface_t *image;
601 if (unlikely (other->status))
602 return _cairo_surface_create_in_error (other->status);
603 if (unlikely (other->finished))
604 return _cairo_surface_create_in_error (CAIRO_STATUS_SURFACE_FINISHED);
606 if (unlikely (width < 0 || height < 0))
607 return _cairo_surface_create_in_error (CAIRO_STATUS_INVALID_SIZE);
608 if (unlikely (! CAIRO_FORMAT_VALID (format)))
609 return _cairo_surface_create_in_error (CAIRO_STATUS_INVALID_FORMAT);
612 if (other->backend->create_similar_image)
613 image = other->backend->create_similar_image (other,
614 format, width, height);
616 image = cairo_image_surface_create (format, width, height);
618 assert (image->is_clear);
622 slim_hidden_def (cairo_surface_create_similar_image);
625 * _cairo_surface_map_to_image:
626 * @surface: an existing surface used to extract the image from
627 * @extents: limit the extraction to an rectangular region
629 * Returns an image surface that is the most efficient mechanism for
630 * modifying the backing store of the target surface. The region
631 * retrieved is limited to @extents.
633 * Note, the use of the original surface as a target or source whilst
634 * it is mapped is undefined. The result of mapping the surface
635 * multiple times is undefined. Calling cairo_surface_destroy() or
636 * cairo_surface_finish() on the resulting image surface results in
637 * undefined behavior. Changing the device transform of the image
638 * surface or of @surface before the image surface is unmapped results
639 * in undefined behavior.
641 * Assumes that @surface is valid (CAIRO_STATUS_SUCCESS,
644 * Return value: a pointer to the newly allocated image surface. The
645 * caller must use _cairo_surface_unmap_image() to destroy this image
648 * This function always returns a valid pointer, but it will return a
649 * pointer to a "nil" surface if @other is already in an error state
650 * or any other error occurs.
652 * The returned image might have a %CAIRO_FORMAT_INVALID format.
654 cairo_image_surface_t *
655 _cairo_surface_map_to_image (cairo_surface_t *surface,
656 const cairo_rectangle_int_t *extents)
658 cairo_image_surface_t *image = NULL;
660 assert (extents != NULL);
662 /* TODO: require map_to_image != NULL */
663 if (surface->backend->map_to_image)
664 image = surface->backend->map_to_image (surface, extents);
667 image = _cairo_image_surface_clone_subimage (surface, extents);
673 * _cairo_surface_unmap_image:
674 * @surface: the surface passed to _cairo_surface_map_to_image().
675 * @image: the currently mapped image
677 * Unmaps the image surface as returned from
678 * _cairo_surface_map_to_image().
680 * The content of the image will be uploaded to the target surface.
681 * Afterwards, the image is destroyed.
683 * Using an image surface which wasn't returned by
684 * _cairo_surface_map_to_image() results in undefined behavior.
686 * An image surface in error status can be passed to
687 * _cairo_surface_unmap_image().
689 * Return value: the unmap status.
691 * Even if the unmap status is not successful, @image is destroyed.
694 _cairo_surface_unmap_image (cairo_surface_t *surface,
695 cairo_image_surface_t *image)
697 cairo_surface_pattern_t pattern;
698 cairo_rectangle_int_t extents;
700 cairo_int_status_t status;
702 /* map_to_image can return error surfaces */
703 if (unlikely (image->base.status)) {
704 status = image->base.status;
708 /* If the image is untouched just skip the update */
709 if (image->base.serial == 0) {
710 status = CAIRO_STATUS_SUCCESS;
714 /* TODO: require unmap_image != NULL */
715 if (surface->backend->unmap_image &&
716 ! _cairo_image_surface_is_clone (image))
718 status = surface->backend->unmap_image (surface, image);
719 if (status != CAIRO_INT_STATUS_UNSUPPORTED)
723 _cairo_pattern_init_for_surface (&pattern, &image->base);
724 pattern.base.filter = CAIRO_FILTER_NEAREST;
726 /* We have to apply the translate from map_to_image's extents.x and .y */
727 cairo_matrix_init_translate (&pattern.base.matrix,
728 image->base.device_transform.x0,
729 image->base.device_transform.y0);
731 /* And we also have to clip the operation to the image's extents */
732 extents.x = image->base.device_transform_inverse.x0;
733 extents.y = image->base.device_transform_inverse.y0;
734 extents.width = image->width;
735 extents.height = image->height;
736 clip = _cairo_clip_intersect_rectangle (NULL, &extents);
738 status = _cairo_surface_paint (surface,
739 CAIRO_OPERATOR_SOURCE,
743 _cairo_pattern_fini (&pattern.base);
744 _cairo_clip_destroy (clip);
747 cairo_surface_finish (&image->base);
748 cairo_surface_destroy (&image->base);
754 * cairo_surface_map_to_image:
755 * @surface: an existing surface used to extract the image from
756 * @extents: limit the extraction to an rectangular region
758 * Returns an image surface that is the most efficient mechanism for
759 * modifying the backing store of the target surface. The region retrieved
760 * may be limited to the @extents or %NULL for the whole surface
762 * Note, the use of the original surface as a target or source whilst
763 * it is mapped is undefined. The result of mapping the surface
764 * multiple times is undefined. Calling cairo_surface_destroy() or
765 * cairo_surface_finish() on the resulting image surface results in
766 * undefined behavior. Changing the device transform of the image
767 * surface or of @surface before the image surface is unmapped results
768 * in undefined behavior.
770 * Return value: a pointer to the newly allocated image surface. The caller
771 * must use cairo_surface_unmap_image() to destroy this image surface.
773 * This function always returns a valid pointer, but it will return a
774 * pointer to a "nil" surface if @other is already in an error state
775 * or any other error occurs. If the returned pointer does not have an
776 * error status, it is guaranteed to be an image surface whose format
777 * is not %CAIRO_FORMAT_INVALID.
782 cairo_surface_map_to_image (cairo_surface_t *surface,
783 const cairo_rectangle_int_t *extents)
785 cairo_rectangle_int_t rect;
786 cairo_image_surface_t *image;
787 cairo_status_t status;
789 if (unlikely (surface->status))
790 return _cairo_surface_create_in_error (surface->status);
791 if (unlikely (surface->finished))
792 return _cairo_surface_create_in_error (CAIRO_STATUS_SURFACE_FINISHED);
794 if (extents == NULL) {
795 if (unlikely (! surface->backend->get_extents (surface, &rect)))
796 return _cairo_surface_create_in_error (CAIRO_STATUS_INVALID_SIZE);
800 cairo_rectangle_int_t surface_extents;
802 /* If this surface is bounded, we can't map parts
803 * that are outside of it. */
804 if (likely (surface->backend->get_extents (surface, &surface_extents))) {
805 if (unlikely (! _cairo_rectangle_contains_rectangle (&surface_extents, extents)))
806 return _cairo_surface_create_in_error (CAIRO_STATUS_INVALID_SIZE);
810 image = _cairo_surface_map_to_image (surface, extents);
812 status = image->base.status;
813 if (unlikely (status)) {
814 cairo_surface_destroy (&image->base);
815 return _cairo_surface_create_in_error (status);
818 if (image->format == CAIRO_FORMAT_INVALID) {
819 cairo_surface_destroy (&image->base);
820 image = _cairo_image_surface_clone_subimage (surface, extents);
827 * cairo_surface_unmap_image:
828 * @surface: the surface passed to cairo_surface_map_to_image().
829 * @image: the currently mapped image
831 * Unmaps the image surface as returned from #cairo_surface_map_to_image().
833 * The content of the image will be uploaded to the target surface.
834 * Afterwards, the image is destroyed.
836 * Using an image surface which wasn't returned by cairo_surface_map_to_image()
837 * results in undefined behavior.
842 cairo_surface_unmap_image (cairo_surface_t *surface,
843 cairo_surface_t *image)
845 cairo_int_status_t status = CAIRO_STATUS_SUCCESS;
847 if (unlikely (surface->status)) {
848 status = surface->status;
851 if (unlikely (surface->finished)) {
852 status = _cairo_error (CAIRO_STATUS_SURFACE_FINISHED);
855 if (unlikely (image->status)) {
856 status = image->status;
859 if (unlikely (image->finished)) {
860 status = _cairo_error (CAIRO_STATUS_SURFACE_FINISHED);
863 if (unlikely (! _cairo_surface_is_image (image))) {
864 status = _cairo_error (CAIRO_STATUS_SURFACE_TYPE_MISMATCH);
868 status = _cairo_surface_unmap_image (surface,
869 (cairo_image_surface_t *) image);
870 if (unlikely (status))
871 _cairo_surface_set_error (surface, status);
876 _cairo_surface_set_error (surface, status);
877 cairo_surface_finish (image);
878 cairo_surface_destroy (image);
882 _cairo_surface_create_similar_solid (cairo_surface_t *other,
883 cairo_content_t content,
886 const cairo_color_t *color)
888 cairo_status_t status;
889 cairo_surface_t *surface;
890 cairo_solid_pattern_t pattern;
892 surface = _cairo_surface_create_similar_scratch (other, content,
894 if (unlikely (surface->status))
897 _cairo_pattern_init_solid (&pattern, color);
898 status = _cairo_surface_paint (surface,
899 color == CAIRO_COLOR_TRANSPARENT ?
900 CAIRO_OPERATOR_CLEAR : CAIRO_OPERATOR_SOURCE,
901 &pattern.base, NULL);
902 if (unlikely (status)) {
903 cairo_surface_destroy (surface);
904 surface = _cairo_surface_create_in_error (status);
911 * cairo_surface_reference:
912 * @surface: a #cairo_surface_t
914 * Increases the reference count on @surface by one. This prevents
915 * @surface from being destroyed until a matching call to
916 * cairo_surface_destroy() is made.
918 * The number of references to a #cairo_surface_t can be get using
919 * cairo_surface_get_reference_count().
921 * Return value: the referenced #cairo_surface_t.
926 cairo_surface_reference (cairo_surface_t *surface)
928 if (surface == NULL ||
929 CAIRO_REFERENCE_COUNT_IS_INVALID (&surface->ref_count))
932 assert (CAIRO_REFERENCE_COUNT_HAS_REFERENCE (&surface->ref_count));
934 _cairo_reference_count_inc (&surface->ref_count);
938 slim_hidden_def (cairo_surface_reference);
941 * cairo_surface_destroy:
942 * @surface: a #cairo_surface_t
944 * Decreases the reference count on @surface by one. If the result is
945 * zero, then @surface and all associated resources are freed. See
946 * cairo_surface_reference().
951 cairo_surface_destroy (cairo_surface_t *surface)
953 if (surface == NULL ||
954 CAIRO_REFERENCE_COUNT_IS_INVALID (&surface->ref_count))
957 assert (CAIRO_REFERENCE_COUNT_HAS_REFERENCE (&surface->ref_count));
959 if (! _cairo_reference_count_dec_and_test (&surface->ref_count))
962 assert (surface->snapshot_of == NULL);
964 if (! surface->finished) {
965 _cairo_surface_finish_snapshots (surface);
966 /* We may have been referenced by a snapshot prior to have
967 * detaching it with the copy-on-write.
969 if (CAIRO_REFERENCE_COUNT_GET_VALUE (&surface->ref_count))
972 _cairo_surface_finish (surface);
976 _cairo_damage_destroy (surface->damage);
978 _cairo_user_data_array_fini (&surface->user_data);
979 _cairo_user_data_array_fini (&surface->mime_data);
981 if (surface->owns_device)
982 cairo_device_destroy (surface->device);
984 assert (surface->snapshot_of == NULL);
985 assert (! _cairo_surface_has_snapshots (surface));
986 /* paranoid check that nobody took a reference whilst finishing */
987 assert (! CAIRO_REFERENCE_COUNT_HAS_REFERENCE (&surface->ref_count));
991 slim_hidden_def(cairo_surface_destroy);
994 * cairo_surface_get_reference_count:
995 * @surface: a #cairo_surface_t
997 * Returns the current reference count of @surface.
999 * Return value: the current reference count of @surface. If the
1000 * object is a nil object, 0 will be returned.
1005 cairo_surface_get_reference_count (cairo_surface_t *surface)
1007 if (surface == NULL ||
1008 CAIRO_REFERENCE_COUNT_IS_INVALID (&surface->ref_count))
1011 return CAIRO_REFERENCE_COUNT_GET_VALUE (&surface->ref_count);
1015 _cairo_surface_finish_snapshots (cairo_surface_t *surface)
1017 cairo_status_t status;
1019 /* update the snapshots *before* we declare the surface as finished */
1020 surface->_finishing = TRUE;
1021 status = _cairo_surface_flush (surface, 0);
1026 _cairo_surface_finish (cairo_surface_t *surface)
1028 cairo_status_t status;
1030 surface->finished = TRUE;
1032 /* call finish even if in error mode */
1033 if (surface->backend->finish) {
1034 status = surface->backend->finish (surface);
1035 if (unlikely (status))
1036 _cairo_surface_set_error (surface, status);
1039 assert (surface->snapshot_of == NULL);
1040 assert (!_cairo_surface_has_snapshots (surface));
1044 * cairo_surface_finish:
1045 * @surface: the #cairo_surface_t to finish
1047 * This function finishes the surface and drops all references to
1048 * external resources. For example, for the Xlib backend it means
1049 * that cairo will no longer access the drawable, which can be freed.
1050 * After calling cairo_surface_finish() the only valid operations on a
1051 * surface are getting and setting user, referencing and
1052 * destroying, and flushing and finishing it.
1053 * Further drawing to the surface will not affect the
1054 * surface but will instead trigger a %CAIRO_STATUS_SURFACE_FINISHED
1057 * When the last call to cairo_surface_destroy() decreases the
1058 * reference count to zero, cairo will call cairo_surface_finish() if
1059 * it hasn't been called already, before freeing the resources
1060 * associated with the surface.
1065 cairo_surface_finish (cairo_surface_t *surface)
1067 if (surface == NULL)
1070 if (CAIRO_REFERENCE_COUNT_IS_INVALID (&surface->ref_count))
1073 if (surface->finished)
1076 /* We have to be careful when decoupling potential reference cycles */
1077 cairo_surface_reference (surface);
1079 _cairo_surface_finish_snapshots (surface);
1080 /* XXX need to block and wait for snapshot references */
1081 _cairo_surface_finish (surface);
1083 cairo_surface_destroy (surface);
1085 slim_hidden_def (cairo_surface_finish);
1088 * _cairo_surface_release_device_reference:
1089 * @surface: a #cairo_surface_t
1091 * This function makes @surface release the reference to its device. The
1092 * function is intended to be used for avoiding cycling references for
1093 * surfaces that are owned by their device, for example cache surfaces.
1094 * Note that the @surface will still assume that the device is available.
1095 * So it is the caller's responsibility to ensure the device stays around
1096 * until the @surface is destroyed. Just calling cairo_surface_finish() is
1100 _cairo_surface_release_device_reference (cairo_surface_t *surface)
1102 assert (surface->owns_device);
1104 cairo_device_destroy (surface->device);
1105 surface->owns_device = FALSE;
1109 * cairo_surface_get_user_data:
1110 * @surface: a #cairo_surface_t
1111 * @key: the address of the #cairo_user_data_key_t the user data was
1114 * Return user data previously attached to @surface using the specified
1115 * key. If no user data has been attached with the given key this
1116 * function returns %NULL.
1118 * Return value: the user data previously attached or %NULL.
1123 cairo_surface_get_user_data (cairo_surface_t *surface,
1124 const cairo_user_data_key_t *key)
1126 return _cairo_user_data_array_get_data (&surface->user_data, key);
1130 * cairo_surface_set_user_data:
1131 * @surface: a #cairo_surface_t
1132 * @key: the address of a #cairo_user_data_key_t to attach the user data to
1133 * @user_data: the user data to attach to the surface
1134 * @destroy: a #cairo_destroy_func_t which will be called when the
1135 * surface is destroyed or when new user data is attached using the
1138 * Attach user data to @surface. To remove user data from a surface,
1139 * call this function with the key that was used to set it and %NULL
1142 * Return value: %CAIRO_STATUS_SUCCESS or %CAIRO_STATUS_NO_MEMORY if a
1143 * slot could not be allocated for the user data.
1148 cairo_surface_set_user_data (cairo_surface_t *surface,
1149 const cairo_user_data_key_t *key,
1151 cairo_destroy_func_t destroy)
1153 if (CAIRO_REFERENCE_COUNT_IS_INVALID (&surface->ref_count))
1154 return surface->status;
1156 return _cairo_user_data_array_set_data (&surface->user_data,
1157 key, user_data, destroy);
1161 * cairo_surface_get_mime_data:
1162 * @surface: a #cairo_surface_t
1163 * @mime_type: the mime type of the image data
1164 * @data: the image data to attached to the surface
1165 * @length: the length of the image data
1167 * Return mime data previously attached to @surface using the
1168 * specified mime type. If no data has been attached with the given
1169 * mime type, @data is set %NULL.
1174 cairo_surface_get_mime_data (cairo_surface_t *surface,
1175 const char *mime_type,
1176 const unsigned char **data,
1177 unsigned long *length)
1179 cairo_user_data_slot_t *slots;
1184 if (unlikely (surface->status))
1187 /* The number of mime-types attached to a surface is usually small,
1188 * typically zero. Therefore it is quicker to do a strcmp() against
1189 * each key than it is to intern the string (i.e. compute a hash,
1190 * search the hash table, and do a final strcmp).
1192 num_slots = surface->mime_data.num_elements;
1193 slots = _cairo_array_index (&surface->mime_data, 0);
1194 for (i = 0; i < num_slots; i++) {
1195 if (slots[i].key != NULL && strcmp ((char *) slots[i].key, mime_type) == 0) {
1196 cairo_mime_data_t *mime_data = slots[i].user_data;
1198 *data = mime_data->data;
1199 *length = mime_data->length;
1204 slim_hidden_def (cairo_surface_get_mime_data);
1207 _cairo_mime_data_destroy (void *ptr)
1209 cairo_mime_data_t *mime_data = ptr;
1211 if (! _cairo_reference_count_dec_and_test (&mime_data->ref_count))
1214 if (mime_data->destroy && mime_data->closure)
1215 mime_data->destroy (mime_data->closure);
1221 * CAIRO_MIME_TYPE_JP2:
1223 * The Joint Photographic Experts Group (JPEG) 2000 image coding standard (ISO/IEC 15444-1).
1229 * CAIRO_MIME_TYPE_JPEG:
1231 * The Joint Photographic Experts Group (JPEG) image coding standard (ISO/IEC 10918-1).
1237 * CAIRO_MIME_TYPE_PNG:
1239 * The Portable Network Graphics image file format (ISO/IEC 15948).
1245 * CAIRO_MIME_TYPE_URI:
1247 * URI for an image file (unofficial MIME type).
1253 * CAIRO_MIME_TYPE_UNIQUE_ID:
1255 * Unique identifier for a surface (cairo specific MIME type).
1261 * cairo_surface_set_mime_data:
1262 * @surface: a #cairo_surface_t
1263 * @mime_type: the MIME type of the image data
1264 * @data: the image data to attach to the surface
1265 * @length: the length of the image data
1266 * @destroy: a #cairo_destroy_func_t which will be called when the
1267 * surface is destroyed or when new image data is attached using the
1269 * @closure: the data to be passed to the @destroy notifier
1271 * Attach an image in the format @mime_type to @surface. To remove
1272 * the data from a surface, call this function with same mime type
1273 * and %NULL for @data.
1275 * The attached image (or filename) data can later be used by backends
1276 * which support it (currently: PDF, PS, SVG and Win32 Printing
1277 * surfaces) to emit this data instead of making a snapshot of the
1278 * @surface. This approach tends to be faster and requires less
1279 * memory and disk space.
1281 * The recognized MIME types are the following: %CAIRO_MIME_TYPE_JPEG,
1282 * %CAIRO_MIME_TYPE_PNG, %CAIRO_MIME_TYPE_JP2, %CAIRO_MIME_TYPE_URI.
1284 * See corresponding backend surface docs for details about which MIME
1285 * types it can handle. Caution: the associated MIME data will be
1286 * discarded if you draw on the surface afterwards. Use this function
1289 * Return value: %CAIRO_STATUS_SUCCESS or %CAIRO_STATUS_NO_MEMORY if a
1290 * slot could not be allocated for the user data.
1295 cairo_surface_set_mime_data (cairo_surface_t *surface,
1296 const char *mime_type,
1297 const unsigned char *data,
1298 unsigned long length,
1299 cairo_destroy_func_t destroy,
1302 cairo_status_t status;
1303 cairo_mime_data_t *mime_data;
1305 if (unlikely (surface->status))
1306 return surface->status;
1307 if (unlikely (surface->finished))
1308 return _cairo_surface_set_error (surface, _cairo_error (CAIRO_STATUS_SURFACE_FINISHED));
1310 status = _cairo_intern_string (&mime_type, -1);
1311 if (unlikely (status))
1312 return _cairo_surface_set_error (surface, status);
1315 mime_data = malloc (sizeof (cairo_mime_data_t));
1316 if (unlikely (mime_data == NULL))
1317 return _cairo_surface_set_error (surface, _cairo_error (CAIRO_STATUS_NO_MEMORY));
1319 CAIRO_REFERENCE_COUNT_INIT (&mime_data->ref_count, 1);
1321 mime_data->data = (unsigned char *) data;
1322 mime_data->length = length;
1323 mime_data->destroy = destroy;
1324 mime_data->closure = closure;
1328 status = _cairo_user_data_array_set_data (&surface->mime_data,
1329 (cairo_user_data_key_t *) mime_type,
1331 _cairo_mime_data_destroy);
1332 if (unlikely (status)) {
1335 return _cairo_surface_set_error (surface, status);
1338 return CAIRO_STATUS_SUCCESS;
1340 slim_hidden_def (cairo_surface_set_mime_data);
1343 * cairo_surface_supports_mime_type:
1344 * @surface: a #cairo_surface_t
1345 * @mime_type: the mime type
1347 * Return whether @surface supports @mime_type.
1349 * Return value: %TRUE if @surface supports
1350 * @mime_type, %FALSE otherwise
1355 cairo_surface_supports_mime_type (cairo_surface_t *surface,
1356 const char *mime_type)
1360 if (surface->backend->get_supported_mime_types) {
1361 types = surface->backend->get_supported_mime_types (surface);
1364 if (strcmp (*types, mime_type) == 0)
1373 slim_hidden_def (cairo_surface_supports_mime_type);
1376 _cairo_mime_data_reference (const void *key, void *elt, void *closure)
1378 cairo_mime_data_t *mime_data = elt;
1380 _cairo_reference_count_inc (&mime_data->ref_count);
1384 _cairo_surface_copy_mime_data (cairo_surface_t *dst,
1385 cairo_surface_t *src)
1387 cairo_status_t status;
1393 return _cairo_surface_set_error (dst, src->status);
1395 /* first copy the mime-data, discarding any already set on dst */
1396 status = _cairo_user_data_array_copy (&dst->mime_data, &src->mime_data);
1397 if (unlikely (status))
1398 return _cairo_surface_set_error (dst, status);
1400 /* now increment the reference counters for the copies */
1401 _cairo_user_data_array_foreach (&dst->mime_data,
1402 _cairo_mime_data_reference,
1405 return CAIRO_STATUS_SUCCESS;
1409 * _cairo_surface_set_font_options:
1410 * @surface: a #cairo_surface_t
1411 * @options: a #cairo_font_options_t object that contains the
1412 * options to use for this surface instead of backend's default
1415 * Sets the default font rendering options for the surface.
1416 * This is useful to correctly propagate default font options when
1417 * falling back to an image surface in a backend implementation.
1418 * This affects the options returned in cairo_surface_get_font_options().
1420 * If @options is %NULL the surface options are reset to those of
1421 * the backend default.
1424 _cairo_surface_set_font_options (cairo_surface_t *surface,
1425 cairo_font_options_t *options)
1427 if (surface->status)
1430 assert (surface->snapshot_of == NULL);
1432 if (surface->finished) {
1433 _cairo_surface_set_error (surface, _cairo_error (CAIRO_STATUS_SURFACE_FINISHED));
1438 surface->has_font_options = TRUE;
1439 _cairo_font_options_init_copy (&surface->font_options, options);
1441 surface->has_font_options = FALSE;
1446 * cairo_surface_get_font_options:
1447 * @surface: a #cairo_surface_t
1448 * @options: a #cairo_font_options_t object into which to store
1449 * the retrieved options. All existing values are overwritten
1451 * Retrieves the default font rendering options for the surface.
1452 * This allows display surfaces to report the correct subpixel order
1453 * for rendering on them, print surfaces to disable hinting of
1454 * metrics and so forth. The result can then be used with
1455 * cairo_scaled_font_create().
1460 cairo_surface_get_font_options (cairo_surface_t *surface,
1461 cairo_font_options_t *options)
1463 if (cairo_font_options_status (options))
1466 if (surface->status) {
1467 _cairo_font_options_init_default (options);
1471 if (! surface->has_font_options) {
1472 surface->has_font_options = TRUE;
1474 _cairo_font_options_init_default (&surface->font_options);
1476 if (!surface->finished && surface->backend->get_font_options) {
1477 surface->backend->get_font_options (surface, &surface->font_options);
1481 _cairo_font_options_init_copy (options, &surface->font_options);
1483 slim_hidden_def (cairo_surface_get_font_options);
1486 _cairo_surface_flush (cairo_surface_t *surface, unsigned flags)
1488 /* update the current snapshots *before* the user updates the surface */
1489 _cairo_surface_detach_snapshots (surface);
1490 if (surface->snapshot_of != NULL)
1491 _cairo_surface_detach_snapshot (surface);
1492 _cairo_surface_detach_mime_data (surface);
1494 return __cairo_surface_flush (surface, flags);
1498 * cairo_surface_flush:
1499 * @surface: a #cairo_surface_t
1501 * Do any pending drawing for the surface and also restore any
1502 * temporary modifications cairo has made to the surface's
1503 * state. This function must be called before switching from
1504 * drawing on the surface with cairo to drawing on it directly
1505 * with native APIs. If the surface doesn't support direct access,
1506 * then this function does nothing.
1511 cairo_surface_flush (cairo_surface_t *surface)
1513 cairo_status_t status;
1515 if (surface->status)
1518 if (surface->finished)
1521 status = _cairo_surface_flush (surface, 0);
1522 if (unlikely (status))
1523 _cairo_surface_set_error (surface, status);
1525 slim_hidden_def (cairo_surface_flush);
1528 * cairo_surface_mark_dirty:
1529 * @surface: a #cairo_surface_t
1531 * Tells cairo that drawing has been done to surface using means other
1532 * than cairo, and that cairo should reread any cached areas. Note
1533 * that you must call cairo_surface_flush() before doing such drawing.
1538 cairo_surface_mark_dirty (cairo_surface_t *surface)
1540 cairo_rectangle_int_t extents;
1542 if (unlikely (surface->status))
1544 if (unlikely (surface->finished)) {
1545 _cairo_surface_set_error (surface, _cairo_error (CAIRO_STATUS_SURFACE_FINISHED));
1549 _cairo_surface_get_extents (surface, &extents);
1550 cairo_surface_mark_dirty_rectangle (surface,
1551 extents.x, extents.y,
1552 extents.width, extents.height);
1554 slim_hidden_def (cairo_surface_mark_dirty);
1557 * cairo_surface_mark_dirty_rectangle:
1558 * @surface: a #cairo_surface_t
1559 * @x: X coordinate of dirty rectangle
1560 * @y: Y coordinate of dirty rectangle
1561 * @width: width of dirty rectangle
1562 * @height: height of dirty rectangle
1564 * Like cairo_surface_mark_dirty(), but drawing has been done only to
1565 * the specified rectangle, so that cairo can retain cached contents
1566 * for other parts of the surface.
1568 * Any cached clip set on the surface will be reset by this function,
1569 * to make sure that future cairo calls have the clip set that they
1575 cairo_surface_mark_dirty_rectangle (cairo_surface_t *surface,
1581 cairo_status_t status;
1583 if (unlikely (surface->status))
1586 assert (surface->snapshot_of == NULL);
1588 if (unlikely (surface->finished)) {
1589 _cairo_surface_set_error (surface, _cairo_error (CAIRO_STATUS_SURFACE_FINISHED));
1593 /* The application *should* have called cairo_surface_flush() before
1594 * modifying the surface independently of cairo (and thus having to
1595 * call mark_dirty()). */
1596 assert (! _cairo_surface_has_snapshots (surface));
1597 assert (! _cairo_surface_has_mime_data (surface));
1599 surface->is_clear = FALSE;
1602 if (surface->damage) {
1607 box.p2.x = x + width;
1608 box.p2.y = y + height;
1610 surface->damage = _cairo_damage_add_box (surface->damage, &box);
1613 if (surface->backend->mark_dirty_rectangle != NULL) {
1614 /* XXX: FRAGILE: We're ignoring the scaling component of
1615 * device_transform here. I don't know what the right thing to
1616 * do would actually be if there were some scaling here, but
1617 * we avoid this since device_transfom scaling is not exported
1618 * publicly and mark_dirty is not used internally. */
1619 status = surface->backend->mark_dirty_rectangle (surface,
1620 x + surface->device_transform.x0,
1621 y + surface->device_transform.y0,
1624 if (unlikely (status))
1625 _cairo_surface_set_error (surface, status);
1628 slim_hidden_def (cairo_surface_mark_dirty_rectangle);
1631 * _cairo_surface_set_device_scale:
1632 * @surface: a #cairo_surface_t
1633 * @sx: a scale factor in the X direction
1634 * @sy: a scale factor in the Y direction
1636 * Private function for setting an extra scale factor to affect all
1637 * drawing to a surface. This is used, for example, when replaying a
1638 * recording surface to an image fallback intended for an eventual
1639 * vector-oriented backend. Since the recording surface will record
1640 * coordinates in one backend space, but the image fallback uses a
1641 * different backend space, (differing by the fallback resolution
1642 * scale factors), we need a scale factor correction.
1644 * Caution: Not all places we use device transform correctly handle
1645 * both a translate and a scale. An audit would be nice.
1648 _cairo_surface_set_device_scale (cairo_surface_t *surface,
1652 cairo_status_t status;
1654 if (unlikely (surface->status))
1657 assert (surface->snapshot_of == NULL);
1659 if (unlikely (surface->finished)) {
1660 _cairo_surface_set_error (surface, _cairo_error (CAIRO_STATUS_SURFACE_FINISHED));
1664 status = _cairo_surface_begin_modification (surface);
1665 if (unlikely (status)) {
1666 _cairo_surface_set_error (surface, status);
1670 surface->device_transform.xx = sx;
1671 surface->device_transform.yy = sy;
1672 surface->device_transform.xy = 0.0;
1673 surface->device_transform.yx = 0.0;
1675 surface->device_transform_inverse = surface->device_transform;
1676 status = cairo_matrix_invert (&surface->device_transform_inverse);
1677 /* should always be invertible unless given pathological input */
1678 assert (status == CAIRO_STATUS_SUCCESS);
1680 _cairo_observers_notify (&surface->device_transform_observers, surface);
1684 * cairo_surface_set_device_offset:
1685 * @surface: a #cairo_surface_t
1686 * @x_offset: the offset in the X direction, in device units
1687 * @y_offset: the offset in the Y direction, in device units
1689 * Sets an offset that is added to the device coordinates determined
1690 * by the CTM when drawing to @surface. One use case for this function
1691 * is when we want to create a #cairo_surface_t that redirects drawing
1692 * for a portion of an onscreen surface to an offscreen surface in a
1693 * way that is completely invisible to the user of the cairo
1694 * API. Setting a transformation via cairo_translate() isn't
1695 * sufficient to do this, since functions like
1696 * cairo_device_to_user() will expose the hidden offset.
1698 * Note that the offset affects drawing to the surface as well as
1699 * using the surface in a source pattern.
1704 cairo_surface_set_device_offset (cairo_surface_t *surface,
1708 cairo_status_t status;
1710 if (unlikely (surface->status))
1713 assert (surface->snapshot_of == NULL);
1715 if (unlikely (surface->finished)) {
1716 _cairo_surface_set_error (surface, _cairo_error (CAIRO_STATUS_SURFACE_FINISHED));
1720 status = _cairo_surface_begin_modification (surface);
1721 if (unlikely (status)) {
1722 _cairo_surface_set_error (surface, status);
1726 surface->device_transform.x0 = x_offset;
1727 surface->device_transform.y0 = y_offset;
1729 surface->device_transform_inverse = surface->device_transform;
1730 status = cairo_matrix_invert (&surface->device_transform_inverse);
1731 /* should always be invertible unless given pathological input */
1732 assert (status == CAIRO_STATUS_SUCCESS);
1734 _cairo_observers_notify (&surface->device_transform_observers, surface);
1736 slim_hidden_def (cairo_surface_set_device_offset);
1739 * cairo_surface_get_device_offset:
1740 * @surface: a #cairo_surface_t
1741 * @x_offset: the offset in the X direction, in device units
1742 * @y_offset: the offset in the Y direction, in device units
1744 * This function returns the previous device offset set by
1745 * cairo_surface_set_device_offset().
1750 cairo_surface_get_device_offset (cairo_surface_t *surface,
1755 *x_offset = surface->device_transform.x0;
1757 *y_offset = surface->device_transform.y0;
1759 slim_hidden_def (cairo_surface_get_device_offset);
1762 * cairo_surface_set_fallback_resolution:
1763 * @surface: a #cairo_surface_t
1764 * @x_pixels_per_inch: horizontal setting for pixels per inch
1765 * @y_pixels_per_inch: vertical setting for pixels per inch
1767 * Set the horizontal and vertical resolution for image fallbacks.
1769 * When certain operations aren't supported natively by a backend,
1770 * cairo will fallback by rendering operations to an image and then
1771 * overlaying that image onto the output. For backends that are
1772 * natively vector-oriented, this function can be used to set the
1773 * resolution used for these image fallbacks, (larger values will
1774 * result in more detailed images, but also larger file sizes).
1776 * Some examples of natively vector-oriented backends are the ps, pdf,
1779 * For backends that are natively raster-oriented, image fallbacks are
1780 * still possible, but they are always performed at the native
1781 * device resolution. So this function has no effect on those
1784 * Note: The fallback resolution only takes effect at the time of
1785 * completing a page (with cairo_show_page() or cairo_copy_page()) so
1786 * there is currently no way to have more than one fallback resolution
1787 * in effect on a single page.
1789 * The default fallback resoultion is 300 pixels per inch in both
1795 cairo_surface_set_fallback_resolution (cairo_surface_t *surface,
1796 double x_pixels_per_inch,
1797 double y_pixels_per_inch)
1799 cairo_status_t status;
1801 if (unlikely (surface->status))
1804 assert (surface->snapshot_of == NULL);
1806 if (unlikely (surface->finished)) {
1807 _cairo_surface_set_error (surface, _cairo_error (CAIRO_STATUS_SURFACE_FINISHED));
1811 if (x_pixels_per_inch <= 0 || y_pixels_per_inch <= 0) {
1812 /* XXX Could delay raising the error until we fallback, but throwing
1813 * the error here means that we can catch the real culprit.
1815 _cairo_surface_set_error (surface, CAIRO_STATUS_INVALID_MATRIX);
1819 status = _cairo_surface_begin_modification (surface);
1820 if (unlikely (status)) {
1821 _cairo_surface_set_error (surface, status);
1825 surface->x_fallback_resolution = x_pixels_per_inch;
1826 surface->y_fallback_resolution = y_pixels_per_inch;
1828 slim_hidden_def (cairo_surface_set_fallback_resolution);
1831 * cairo_surface_get_fallback_resolution:
1832 * @surface: a #cairo_surface_t
1833 * @x_pixels_per_inch: horizontal pixels per inch
1834 * @y_pixels_per_inch: vertical pixels per inch
1836 * This function returns the previous fallback resolution set by
1837 * cairo_surface_set_fallback_resolution(), or default fallback
1838 * resolution if never set.
1843 cairo_surface_get_fallback_resolution (cairo_surface_t *surface,
1844 double *x_pixels_per_inch,
1845 double *y_pixels_per_inch)
1847 if (x_pixels_per_inch)
1848 *x_pixels_per_inch = surface->x_fallback_resolution;
1849 if (y_pixels_per_inch)
1850 *y_pixels_per_inch = surface->y_fallback_resolution;
1854 _cairo_surface_has_device_transform (cairo_surface_t *surface)
1856 return ! _cairo_matrix_is_identity (&surface->device_transform);
1860 * _cairo_surface_acquire_source_image:
1861 * @surface: a #cairo_surface_t
1862 * @image_out: location to store a pointer to an image surface that
1863 * has identical contents to @surface. This surface could be @surface
1864 * itself, a surface held internal to @surface, or it could be a new
1865 * surface with a copy of the relevant portion of @surface.
1866 * @image_extra: location to store image specific backend data
1868 * Gets an image surface to use when drawing as a fallback when drawing with
1869 * @surface as a source. _cairo_surface_release_source_image() must be called
1872 * Return value: %CAIRO_STATUS_SUCCESS if an image was stored in @image_out.
1873 * %CAIRO_INT_STATUS_UNSUPPORTED if an image cannot be retrieved for the specified
1874 * surface. Or %CAIRO_STATUS_NO_MEMORY.
1877 _cairo_surface_acquire_source_image (cairo_surface_t *surface,
1878 cairo_image_surface_t **image_out,
1881 cairo_status_t status;
1883 if (unlikely (surface->status))
1884 return surface->status;
1886 assert (!surface->finished);
1888 if (surface->backend->acquire_source_image == NULL)
1889 return CAIRO_INT_STATUS_UNSUPPORTED;
1891 status = surface->backend->acquire_source_image (surface,
1892 image_out, image_extra);
1893 if (unlikely (status))
1894 return _cairo_surface_set_error (surface, status);
1896 _cairo_debug_check_image_surface_is_defined (&(*image_out)->base);
1898 return CAIRO_STATUS_SUCCESS;
1902 _cairo_surface_default_acquire_source_image (void *_surface,
1903 cairo_image_surface_t **image_out,
1906 cairo_surface_t *surface = _surface;
1907 cairo_rectangle_int_t extents;
1909 if (unlikely (! surface->backend->get_extents (surface, &extents)))
1910 return _cairo_error (CAIRO_STATUS_INVALID_SIZE);
1912 *image_out = _cairo_surface_map_to_image (surface, &extents);
1913 *image_extra = NULL;
1914 return (*image_out)->base.status;
1918 * _cairo_surface_release_source_image:
1919 * @surface: a #cairo_surface_t
1920 * @image_extra: same as return from the matching _cairo_surface_acquire_source_image()
1922 * Releases any resources obtained with _cairo_surface_acquire_source_image()
1925 _cairo_surface_release_source_image (cairo_surface_t *surface,
1926 cairo_image_surface_t *image,
1929 assert (!surface->finished);
1931 if (surface->backend->release_source_image)
1932 surface->backend->release_source_image (surface, image, image_extra);
1936 _cairo_surface_default_release_source_image (void *surface,
1937 cairo_image_surface_t *image,
1940 cairo_status_t ignored;
1942 ignored = _cairo_surface_unmap_image (surface, image);
1948 _cairo_surface_get_source (cairo_surface_t *surface,
1949 cairo_rectangle_int_t *extents)
1951 assert (surface->backend->source);
1952 return surface->backend->source (surface, extents);
1956 _cairo_surface_default_source (void *surface,
1957 cairo_rectangle_int_t *extents)
1960 _cairo_surface_get_extents(surface, extents);
1964 static cairo_status_t
1965 _pattern_has_error (const cairo_pattern_t *pattern)
1967 const cairo_surface_pattern_t *spattern;
1969 if (unlikely (pattern->status))
1970 return pattern->status;
1972 if (pattern->type != CAIRO_PATTERN_TYPE_SURFACE)
1973 return CAIRO_STATUS_SUCCESS;
1975 spattern = (const cairo_surface_pattern_t *) pattern;
1976 if (unlikely (spattern->surface->status))
1977 return spattern->surface->status;
1979 if (unlikely (spattern->surface->finished))
1980 return _cairo_error (CAIRO_STATUS_SURFACE_FINISHED);
1982 return CAIRO_STATUS_SUCCESS;
1986 nothing_to_do (cairo_surface_t *surface,
1987 cairo_operator_t op,
1988 const cairo_pattern_t *source)
1990 if (_cairo_pattern_is_clear (source)) {
1991 if (op == CAIRO_OPERATOR_OVER || op == CAIRO_OPERATOR_ADD)
1994 if (op == CAIRO_OPERATOR_SOURCE)
1995 op = CAIRO_OPERATOR_CLEAR;
1998 if (op == CAIRO_OPERATOR_CLEAR && surface->is_clear)
2001 if (op == CAIRO_OPERATOR_ATOP && (surface->content & CAIRO_CONTENT_COLOR) ==0)
2008 _cairo_surface_paint (cairo_surface_t *surface,
2009 cairo_operator_t op,
2010 const cairo_pattern_t *source,
2011 const cairo_clip_t *clip)
2013 cairo_int_status_t status;
2015 TRACE ((stderr, "%s\n", __FUNCTION__));
2016 if (unlikely (surface->status))
2017 return surface->status;
2019 if (_cairo_clip_is_all_clipped (clip))
2020 return CAIRO_STATUS_SUCCESS;
2022 status = _pattern_has_error (source);
2023 if (unlikely (status))
2026 if (nothing_to_do (surface, op, source))
2027 return CAIRO_STATUS_SUCCESS;
2029 status = _cairo_surface_begin_modification (surface);
2030 if (unlikely (status))
2033 status = surface->backend->paint (surface, op, source, clip);
2034 if (status != CAIRO_INT_STATUS_NOTHING_TO_DO) {
2035 surface->is_clear = op == CAIRO_OPERATOR_CLEAR && clip == NULL;
2039 return _cairo_surface_set_error (surface, status);
2043 _cairo_surface_mask (cairo_surface_t *surface,
2044 cairo_operator_t op,
2045 const cairo_pattern_t *source,
2046 const cairo_pattern_t *mask,
2047 const cairo_clip_t *clip)
2049 cairo_int_status_t status;
2051 TRACE ((stderr, "%s\n", __FUNCTION__));
2052 if (unlikely (surface->status))
2053 return surface->status;
2055 if (_cairo_clip_is_all_clipped (clip))
2056 return CAIRO_STATUS_SUCCESS;
2058 /* If the mask is blank, this is just an expensive no-op */
2059 if (_cairo_pattern_is_clear (mask) &&
2060 _cairo_operator_bounded_by_mask (op))
2062 return CAIRO_STATUS_SUCCESS;
2065 status = _pattern_has_error (source);
2066 if (unlikely (status))
2069 status = _pattern_has_error (mask);
2070 if (unlikely (status))
2073 if (nothing_to_do (surface, op, source))
2074 return CAIRO_STATUS_SUCCESS;
2076 status = _cairo_surface_begin_modification (surface);
2077 if (unlikely (status))
2080 status = surface->backend->mask (surface, op, source, mask, clip);
2081 if (status != CAIRO_INT_STATUS_NOTHING_TO_DO) {
2082 surface->is_clear = FALSE;
2086 return _cairo_surface_set_error (surface, status);
2090 _cairo_surface_fill_stroke (cairo_surface_t *surface,
2091 cairo_operator_t fill_op,
2092 const cairo_pattern_t *fill_source,
2093 cairo_fill_rule_t fill_rule,
2094 double fill_tolerance,
2095 cairo_antialias_t fill_antialias,
2096 cairo_path_fixed_t *path,
2097 cairo_operator_t stroke_op,
2098 const cairo_pattern_t *stroke_source,
2099 const cairo_stroke_style_t *stroke_style,
2100 const cairo_matrix_t *stroke_ctm,
2101 const cairo_matrix_t *stroke_ctm_inverse,
2102 double stroke_tolerance,
2103 cairo_antialias_t stroke_antialias,
2104 const cairo_clip_t *clip)
2106 cairo_int_status_t status;
2108 TRACE ((stderr, "%s\n", __FUNCTION__));
2109 if (unlikely (surface->status))
2110 return surface->status;
2112 if (_cairo_clip_is_all_clipped (clip))
2113 return CAIRO_STATUS_SUCCESS;
2115 if (surface->is_clear &&
2116 fill_op == CAIRO_OPERATOR_CLEAR &&
2117 stroke_op == CAIRO_OPERATOR_CLEAR)
2119 return CAIRO_STATUS_SUCCESS;
2122 status = _pattern_has_error (fill_source);
2123 if (unlikely (status))
2126 status = _pattern_has_error (stroke_source);
2127 if (unlikely (status))
2130 status = _cairo_surface_begin_modification (surface);
2131 if (unlikely (status))
2134 if (surface->backend->fill_stroke) {
2135 cairo_matrix_t dev_ctm = *stroke_ctm;
2136 cairo_matrix_t dev_ctm_inverse = *stroke_ctm_inverse;
2138 status = surface->backend->fill_stroke (surface,
2139 fill_op, fill_source, fill_rule,
2140 fill_tolerance, fill_antialias,
2142 stroke_op, stroke_source,
2144 &dev_ctm, &dev_ctm_inverse,
2145 stroke_tolerance, stroke_antialias,
2148 if (status != CAIRO_INT_STATUS_UNSUPPORTED)
2152 status = _cairo_surface_fill (surface, fill_op, fill_source, path,
2153 fill_rule, fill_tolerance, fill_antialias,
2155 if (unlikely (status))
2158 status = _cairo_surface_stroke (surface, stroke_op, stroke_source, path,
2159 stroke_style, stroke_ctm, stroke_ctm_inverse,
2160 stroke_tolerance, stroke_antialias,
2162 if (unlikely (status))
2166 if (status != CAIRO_INT_STATUS_NOTHING_TO_DO) {
2167 surface->is_clear = FALSE;
2171 return _cairo_surface_set_error (surface, status);
2175 _cairo_surface_stroke (cairo_surface_t *surface,
2176 cairo_operator_t op,
2177 const cairo_pattern_t *source,
2178 const cairo_path_fixed_t *path,
2179 const cairo_stroke_style_t *stroke_style,
2180 const cairo_matrix_t *ctm,
2181 const cairo_matrix_t *ctm_inverse,
2183 cairo_antialias_t antialias,
2184 const cairo_clip_t *clip)
2186 cairo_int_status_t status;
2188 TRACE ((stderr, "%s\n", __FUNCTION__));
2189 if (unlikely (surface->status))
2190 return surface->status;
2192 if (_cairo_clip_is_all_clipped (clip))
2193 return CAIRO_STATUS_SUCCESS;
2195 status = _pattern_has_error (source);
2196 if (unlikely (status))
2199 if (nothing_to_do (surface, op, source))
2200 return CAIRO_STATUS_SUCCESS;
2202 status = _cairo_surface_begin_modification (surface);
2203 if (unlikely (status))
2206 status = surface->backend->stroke (surface, op, source,
2209 tolerance, antialias,
2211 if (status != CAIRO_INT_STATUS_NOTHING_TO_DO) {
2212 surface->is_clear = FALSE;
2216 return _cairo_surface_set_error (surface, status);
2220 _cairo_surface_fill (cairo_surface_t *surface,
2221 cairo_operator_t op,
2222 const cairo_pattern_t *source,
2223 const cairo_path_fixed_t *path,
2224 cairo_fill_rule_t fill_rule,
2226 cairo_antialias_t antialias,
2227 const cairo_clip_t *clip)
2229 cairo_int_status_t status;
2231 TRACE ((stderr, "%s\n", __FUNCTION__));
2232 if (unlikely (surface->status))
2233 return surface->status;
2235 if (_cairo_clip_is_all_clipped (clip))
2236 return CAIRO_STATUS_SUCCESS;
2238 status = _pattern_has_error (source);
2239 if (unlikely (status))
2242 if (nothing_to_do (surface, op, source))
2243 return CAIRO_STATUS_SUCCESS;
2245 status = _cairo_surface_begin_modification (surface);
2246 if (unlikely (status))
2249 status = surface->backend->fill (surface, op, source,
2251 tolerance, antialias,
2253 if (status != CAIRO_INT_STATUS_NOTHING_TO_DO) {
2254 surface->is_clear = FALSE;
2258 return _cairo_surface_set_error (surface, status);
2262 * cairo_surface_copy_page:
2263 * @surface: a #cairo_surface_t
2265 * Emits the current page for backends that support multiple pages,
2266 * but doesn't clear it, so that the contents of the current page will
2267 * be retained for the next page. Use cairo_surface_show_page() if you
2268 * want to get an empty page after the emission.
2270 * There is a convenience function for this that takes a #cairo_t,
2271 * namely cairo_copy_page().
2276 cairo_surface_copy_page (cairo_surface_t *surface)
2278 if (unlikely (surface->status))
2281 assert (surface->snapshot_of == NULL);
2283 if (unlikely (surface->finished)) {
2284 _cairo_surface_set_error (surface, CAIRO_STATUS_SURFACE_FINISHED);
2288 /* It's fine if some backends don't implement copy_page */
2289 if (surface->backend->copy_page == NULL)
2292 _cairo_surface_set_error (surface, surface->backend->copy_page (surface));
2294 slim_hidden_def (cairo_surface_copy_page);
2297 * cairo_surface_show_page:
2298 * @surface: a #cairo_Surface_t
2300 * Emits and clears the current page for backends that support multiple
2301 * pages. Use cairo_surface_copy_page() if you don't want to clear the page.
2303 * There is a convenience function for this that takes a #cairo_t,
2304 * namely cairo_show_page().
2309 cairo_surface_show_page (cairo_surface_t *surface)
2311 cairo_status_t status;
2313 if (unlikely (surface->status))
2316 if (unlikely (surface->finished)) {
2317 _cairo_surface_set_error (surface, CAIRO_STATUS_SURFACE_FINISHED);
2321 status = _cairo_surface_begin_modification (surface);
2322 if (unlikely (status)) {
2323 _cairo_surface_set_error (surface, status);
2327 /* It's fine if some backends don't implement show_page */
2328 if (surface->backend->show_page == NULL)
2331 _cairo_surface_set_error (surface, surface->backend->show_page (surface));
2333 slim_hidden_def (cairo_surface_show_page);
2336 * _cairo_surface_get_extents:
2337 * @surface: the #cairo_surface_t to fetch extents for
2339 * This function returns a bounding box for the surface. The surface
2340 * bounds are defined as a region beyond which no rendering will
2341 * possibly be recorded, in other words, it is the maximum extent of
2342 * potentially usable coordinates.
2344 * For vector surfaces, (PDF, PS, SVG and recording-surfaces), the surface
2345 * might be conceived as unbounded, but we force the user to provide a
2346 * maximum size at the time of surface_create. So get_extents uses
2349 * Note: The coordinates returned are in "backend" space rather than
2350 * "surface" space. That is, they are relative to the true (0,0)
2351 * origin rather than the device_transform origin. This might seem a
2352 * bit inconsistent with other #cairo_surface_t interfaces, but all
2353 * current callers are within the surface layer where backend space is
2356 * This behavior would have to be changed is we ever exported a public
2357 * variant of this function.
2360 _cairo_surface_get_extents (cairo_surface_t *surface,
2361 cairo_rectangle_int_t *extents)
2363 cairo_bool_t bounded;
2366 if (surface->backend->get_extents != NULL)
2367 bounded = surface->backend->get_extents (surface, extents);
2370 _cairo_unbounded_rectangle_init (extents);
2376 * cairo_surface_has_show_text_glyphs:
2377 * @surface: a #cairo_surface_t
2379 * Returns whether the surface supports
2380 * sophisticated cairo_show_text_glyphs() operations. That is,
2381 * whether it actually uses the provided text and cluster data
2382 * to a cairo_show_text_glyphs() call.
2384 * Note: Even if this function returns %FALSE, a
2385 * cairo_show_text_glyphs() operation targeted at @surface will
2386 * still succeed. It just will
2387 * act like a cairo_show_glyphs() operation. Users can use this
2388 * function to avoid computing UTF-8 text and cluster mapping if the
2389 * target surface does not use it.
2391 * Return value: %TRUE if @surface supports
2392 * cairo_show_text_glyphs(), %FALSE otherwise
2397 cairo_surface_has_show_text_glyphs (cairo_surface_t *surface)
2399 if (unlikely (surface->status))
2402 if (unlikely (surface->finished)) {
2403 _cairo_surface_set_error (surface, CAIRO_STATUS_SURFACE_FINISHED);
2407 if (surface->backend->has_show_text_glyphs)
2408 return surface->backend->has_show_text_glyphs (surface);
2410 return surface->backend->show_text_glyphs != NULL;
2412 slim_hidden_def (cairo_surface_has_show_text_glyphs);
2414 /* Note: the backends may modify the contents of the glyph array as long as
2415 * they do not return %CAIRO_INT_STATUS_UNSUPPORTED. This makes it possible to
2416 * avoid copying the array again and again, and edit it in-place.
2417 * Backends are in fact free to use the array as a generic buffer as they
2420 * For show_glyphs backend method, and NOT for show_text_glyphs method,
2421 * when they do return UNSUPPORTED, they may adjust remaining_glyphs to notify
2422 * that they have successfully rendered some of the glyphs (from the beginning
2423 * of the array), but not all. If they don't touch remaining_glyphs, it
2424 * defaults to all glyphs.
2426 * See commits 5a9642c5746fd677aed35ce620ce90b1029b1a0c and
2427 * 1781e6018c17909311295a9cc74b70500c6b4d0a for the rationale.
2430 _cairo_surface_show_text_glyphs (cairo_surface_t *surface,
2431 cairo_operator_t op,
2432 const cairo_pattern_t *source,
2435 cairo_glyph_t *glyphs,
2437 const cairo_text_cluster_t *clusters,
2439 cairo_text_cluster_flags_t cluster_flags,
2440 cairo_scaled_font_t *scaled_font,
2441 const cairo_clip_t *clip)
2443 cairo_int_status_t status;
2444 cairo_scaled_font_t *dev_scaled_font = scaled_font;
2446 TRACE ((stderr, "%s\n", __FUNCTION__));
2447 if (unlikely (surface->status))
2448 return surface->status;
2450 if (num_glyphs == 0 && utf8_len == 0)
2451 return CAIRO_STATUS_SUCCESS;
2453 if (_cairo_clip_is_all_clipped (clip))
2454 return CAIRO_STATUS_SUCCESS;
2456 status = _pattern_has_error (source);
2457 if (unlikely (status))
2460 if (nothing_to_do (surface, op, source))
2461 return CAIRO_STATUS_SUCCESS;
2463 status = _cairo_surface_begin_modification (surface);
2464 if (unlikely (status))
2467 if (_cairo_surface_has_device_transform (surface) &&
2468 ! _cairo_matrix_is_integer_translation (&surface->device_transform, NULL, NULL))
2470 cairo_font_options_t font_options;
2471 cairo_matrix_t dev_ctm, font_matrix;
2473 cairo_scaled_font_get_font_matrix (scaled_font, &font_matrix);
2474 cairo_scaled_font_get_ctm (scaled_font, &dev_ctm);
2475 cairo_matrix_multiply (&dev_ctm, &dev_ctm, &surface->device_transform);
2476 cairo_scaled_font_get_font_options (scaled_font, &font_options);
2477 dev_scaled_font = cairo_scaled_font_create (cairo_scaled_font_get_font_face (scaled_font),
2482 status = cairo_scaled_font_status (dev_scaled_font);
2483 if (unlikely (status))
2484 return _cairo_surface_set_error (surface, status);
2486 status = CAIRO_INT_STATUS_UNSUPPORTED;
2488 /* The logic here is duplicated in _cairo_analysis_surface show_glyphs and
2489 * show_text_glyphs. Keep in synch. */
2491 /* A real show_text_glyphs call. Try show_text_glyphs backend
2493 if (surface->backend->show_text_glyphs != NULL) {
2494 status = surface->backend->show_text_glyphs (surface, op,
2498 clusters, num_clusters, cluster_flags,
2502 if (status == CAIRO_INT_STATUS_UNSUPPORTED &&
2503 surface->backend->show_glyphs)
2505 status = surface->backend->show_glyphs (surface, op,
2512 /* A mere show_glyphs call. Try show_glyphs backend method first */
2513 if (surface->backend->show_glyphs != NULL) {
2514 status = surface->backend->show_glyphs (surface, op,
2519 } else if (surface->backend->show_text_glyphs != NULL) {
2520 /* Intentionally only try show_text_glyphs method for show_glyphs
2521 * calls if backend does not have show_glyphs. If backend has
2522 * both methods implemented, we don't fallback from show_glyphs to
2523 * show_text_glyphs, and hence the backend can assume in its
2524 * show_text_glyphs call that clusters is not NULL (which also
2525 * implies that UTF-8 is not NULL, unless the text is
2528 status = surface->backend->show_text_glyphs (surface, op,
2532 clusters, num_clusters, cluster_flags,
2538 if (dev_scaled_font != scaled_font)
2539 cairo_scaled_font_destroy (dev_scaled_font);
2541 if (status != CAIRO_INT_STATUS_NOTHING_TO_DO) {
2542 surface->is_clear = FALSE;
2546 return _cairo_surface_set_error (surface, status);
2550 * _cairo_surface_set_resolution:
2551 * @surface: the surface
2552 * @x_res: x resolution, in dpi
2553 * @y_res: y resolution, in dpi
2555 * Set the actual surface resolution of @surface to the given x and y DPI.
2556 * Mainly used for correctly computing the scale factor when fallback
2557 * rendering needs to take place in the paginated surface.
2560 _cairo_surface_set_resolution (cairo_surface_t *surface,
2564 if (surface->status)
2567 surface->x_resolution = x_res;
2568 surface->y_resolution = y_res;
2572 _cairo_surface_create_in_error (cairo_status_t status)
2574 assert (status < CAIRO_STATUS_LAST_STATUS);
2576 case CAIRO_STATUS_NO_MEMORY:
2577 return (cairo_surface_t *) &_cairo_surface_nil;
2578 case CAIRO_STATUS_SURFACE_TYPE_MISMATCH:
2579 return (cairo_surface_t *) &_cairo_surface_nil_surface_type_mismatch;
2580 case CAIRO_STATUS_INVALID_STATUS:
2581 return (cairo_surface_t *) &_cairo_surface_nil_invalid_status;
2582 case CAIRO_STATUS_INVALID_CONTENT:
2583 return (cairo_surface_t *) &_cairo_surface_nil_invalid_content;
2584 case CAIRO_STATUS_INVALID_FORMAT:
2585 return (cairo_surface_t *) &_cairo_surface_nil_invalid_format;
2586 case CAIRO_STATUS_INVALID_VISUAL:
2587 return (cairo_surface_t *) &_cairo_surface_nil_invalid_visual;
2588 case CAIRO_STATUS_READ_ERROR:
2589 return (cairo_surface_t *) &_cairo_surface_nil_read_error;
2590 case CAIRO_STATUS_WRITE_ERROR:
2591 return (cairo_surface_t *) &_cairo_surface_nil_write_error;
2592 case CAIRO_STATUS_FILE_NOT_FOUND:
2593 return (cairo_surface_t *) &_cairo_surface_nil_file_not_found;
2594 case CAIRO_STATUS_TEMP_FILE_ERROR:
2595 return (cairo_surface_t *) &_cairo_surface_nil_temp_file_error;
2596 case CAIRO_STATUS_INVALID_STRIDE:
2597 return (cairo_surface_t *) &_cairo_surface_nil_invalid_stride;
2598 case CAIRO_STATUS_INVALID_SIZE:
2599 return (cairo_surface_t *) &_cairo_surface_nil_invalid_size;
2600 case CAIRO_STATUS_DEVICE_TYPE_MISMATCH:
2601 return (cairo_surface_t *) &_cairo_surface_nil_device_type_mismatch;
2602 case CAIRO_STATUS_DEVICE_ERROR:
2603 return (cairo_surface_t *) &_cairo_surface_nil_device_error;
2604 case CAIRO_STATUS_SUCCESS:
2605 case CAIRO_STATUS_LAST_STATUS:
2608 case CAIRO_STATUS_INVALID_RESTORE:
2609 case CAIRO_STATUS_INVALID_POP_GROUP:
2610 case CAIRO_STATUS_NO_CURRENT_POINT:
2611 case CAIRO_STATUS_INVALID_MATRIX:
2612 case CAIRO_STATUS_NULL_POINTER:
2613 case CAIRO_STATUS_INVALID_STRING:
2614 case CAIRO_STATUS_INVALID_PATH_DATA:
2615 case CAIRO_STATUS_SURFACE_FINISHED:
2616 case CAIRO_STATUS_PATTERN_TYPE_MISMATCH:
2617 case CAIRO_STATUS_INVALID_DASH:
2618 case CAIRO_STATUS_INVALID_DSC_COMMENT:
2619 case CAIRO_STATUS_INVALID_INDEX:
2620 case CAIRO_STATUS_CLIP_NOT_REPRESENTABLE:
2621 case CAIRO_STATUS_FONT_TYPE_MISMATCH:
2622 case CAIRO_STATUS_USER_FONT_IMMUTABLE:
2623 case CAIRO_STATUS_USER_FONT_ERROR:
2624 case CAIRO_STATUS_NEGATIVE_COUNT:
2625 case CAIRO_STATUS_INVALID_CLUSTERS:
2626 case CAIRO_STATUS_INVALID_SLANT:
2627 case CAIRO_STATUS_INVALID_WEIGHT:
2628 case CAIRO_STATUS_USER_FONT_NOT_IMPLEMENTED:
2629 case CAIRO_STATUS_INVALID_MESH_CONSTRUCTION:
2630 case CAIRO_STATUS_DEVICE_FINISHED:
2632 _cairo_error_throw (CAIRO_STATUS_NO_MEMORY);
2633 return (cairo_surface_t *) &_cairo_surface_nil;
2638 _cairo_int_surface_create_in_error (cairo_int_status_t status)
2640 if (status < CAIRO_INT_STATUS_LAST_STATUS)
2641 return _cairo_surface_create_in_error (status);
2643 switch ((int)status) {
2644 case CAIRO_INT_STATUS_UNSUPPORTED:
2645 return (cairo_surface_t *) &_cairo_surface_nil_unsupported;
2646 case CAIRO_INT_STATUS_NOTHING_TO_DO:
2647 return (cairo_surface_t *) &_cairo_surface_nil_nothing_to_do;
2649 _cairo_error_throw (CAIRO_STATUS_NO_MEMORY);
2650 return (cairo_surface_t *) &_cairo_surface_nil;
2654 /* LocalWords: rasterized