Coverage Report

Created: 2025-07-23 08:13

/src/cairo/src/cairo-surface.c
Line
Count
Source (jump to first uncovered line)
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
3
 *
4
 * Copyright © 2002 University of Southern California
5
 * Copyright © 2005 Red Hat, Inc.
6
 *
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.
14
 *
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
20
 *
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/
25
 *
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.
29
 *
30
 * The Original Code is the cairo graphics library.
31
 *
32
 * The Initial Developer of the Original Code is University of Southern
33
 * California.
34
 *
35
 * Contributor(s):
36
 *  Carl D. Worth <cworth@cworth.org>
37
 */
38
39
#include "cairoint.h"
40
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
53
/**
54
 * SECTION:cairo-surface
55
 * @Title: cairo_surface_t
56
 * @Short_Description: Base class for surfaces
57
 * @See_Also: #cairo_t, #cairo_pattern_t
58
 *
59
 * #cairo_surface_t is the abstract type representing all different drawing
60
 * targets that cairo can render to.  The actual drawings are
61
 * performed using a cairo <firstterm>context</firstterm>.
62
 *
63
 * A cairo surface is created by using <firstterm>backend</firstterm>-specific
64
 * constructors, typically of the form
65
 * <function>cairo_<emphasis>backend</emphasis>_surface_create(<!-- -->)</function>.
66
 *
67
 * Most surface types allow accessing the surface without using Cairo
68
 * functions. If you do this, keep in mind that it is mandatory that you call
69
 * cairo_surface_flush() before reading from or writing to the surface and that
70
 * you must use cairo_surface_mark_dirty() after modifying it.
71
 * <example>
72
 * <title>Directly modifying an image surface</title>
73
 * <programlisting>
74
 * void
75
 * modify_image_surface (cairo_surface_t *surface)
76
 * {
77
 *   unsigned char *data;
78
 *   int width, height, stride;
79
 *
80
 *   // flush to ensure all writing to the image was done
81
 *   cairo_surface_flush (surface);
82
 *
83
 *   // modify the image
84
 *   data = cairo_image_surface_get_data (surface);
85
 *   width = cairo_image_surface_get_width (surface);
86
 *   height = cairo_image_surface_get_height (surface);
87
 *   stride = cairo_image_surface_get_stride (surface);
88
 *   modify_image_data (data, width, height, stride);
89
 *
90
 *   // mark the image dirty so Cairo clears its caches.
91
 *   cairo_surface_mark_dirty (surface);
92
 * }
93
 * </programlisting>
94
 * </example>
95
 * Note that for other surface types it might be necessary to acquire the
96
 * surface's device first. See cairo_device_acquire() for a discussion of
97
 * devices.
98
 **/
99
100
#define DEFINE_NIL_SURFACE(status, name)      \
101
const cairo_surface_t name = {          \
102
    NULL,       /* backend */   \
103
    NULL,       /* device */    \
104
    CAIRO_SURFACE_TYPE_IMAGE,   /* type */    \
105
    CAIRO_CONTENT_COLOR,    /* content */   \
106
    CAIRO_REFERENCE_COUNT_INVALID,  /* ref_count */   \
107
    status,       /* status */    \
108
    0,          /* unique id */   \
109
    0,          /* serial */    \
110
    NULL,       /* damage */    \
111
    FALSE,        /* _finishing */  \
112
    FALSE,        /* finished */    \
113
    TRUE,       /* is_clear */    \
114
    FALSE,        /* has_font_options */  \
115
    FALSE,        /* owns_device */ \
116
    FALSE,                              /* is_vector */ \
117
    { 0, 0, 0, NULL, },     /* user_data */   \
118
    { 0, 0, 0, NULL, },     /* mime_data */         \
119
    { 1.0, 0.0, 0.0, 1.0, 0.0, 0.0 },   /* device_transform */  \
120
    { 1.0, 0.0, 0.0, 1.0, 0.0, 0.0 }, /* device_transform_inverse */  \
121
    { NULL, NULL },     /* device_transform_observers */ \
122
    0.0,        /* x_resolution */  \
123
    0.0,        /* y_resolution */  \
124
    0.0,        /* x_fallback_resolution */ \
125
    0.0,        /* y_fallback_resolution */ \
126
    NULL,       /* snapshot_of */ \
127
    NULL,       /* snapshot_detach */ \
128
    { NULL, NULL },     /* snapshots */   \
129
    { NULL, NULL },     /* snapshot */    \
130
    {                                   /* font options begin */\
131
      CAIRO_ANTIALIAS_DEFAULT,    /* antialias */   \
132
      CAIRO_SUBPIXEL_ORDER_DEFAULT, /* subpixel_order */  \
133
      CAIRO_LCD_FILTER_DEFAULT,   /* lcd_filter */  \
134
      CAIRO_HINT_STYLE_DEFAULT,   /* hint_style */  \
135
      CAIRO_HINT_METRICS_DEFAULT, /* hint_metrics */  \
136
      CAIRO_ROUND_GLYPH_POS_DEFAULT,  /* round_glyph_positions */ \
137
      NULL,                            /* variations */ \
138
      CAIRO_COLOR_MODE_DEFAULT,                /* color mode */ \
139
      CAIRO_COLOR_PALETTE_DEFAULT,     /* color palette */ \
140
      NULL, 0,                         /* custom palette */ \
141
    },          /* font_options end */    \
142
    NULL,                               /* foreground_source */   \
143
    FALSE,                              /* foreground_used */   \
144
}
145
146
/* XXX error object! */
147
148
static DEFINE_NIL_SURFACE(CAIRO_STATUS_NO_MEMORY, _cairo_surface_nil);
149
static DEFINE_NIL_SURFACE(CAIRO_STATUS_SURFACE_TYPE_MISMATCH, _cairo_surface_nil_surface_type_mismatch);
150
static DEFINE_NIL_SURFACE(CAIRO_STATUS_INVALID_STATUS, _cairo_surface_nil_invalid_status);
151
static DEFINE_NIL_SURFACE(CAIRO_STATUS_INVALID_CONTENT, _cairo_surface_nil_invalid_content);
152
static DEFINE_NIL_SURFACE(CAIRO_STATUS_INVALID_FORMAT, _cairo_surface_nil_invalid_format);
153
static DEFINE_NIL_SURFACE(CAIRO_STATUS_INVALID_VISUAL, _cairo_surface_nil_invalid_visual);
154
static DEFINE_NIL_SURFACE(CAIRO_STATUS_FILE_NOT_FOUND, _cairo_surface_nil_file_not_found);
155
static DEFINE_NIL_SURFACE(CAIRO_STATUS_TEMP_FILE_ERROR, _cairo_surface_nil_temp_file_error);
156
static DEFINE_NIL_SURFACE(CAIRO_STATUS_READ_ERROR, _cairo_surface_nil_read_error);
157
static DEFINE_NIL_SURFACE(CAIRO_STATUS_WRITE_ERROR, _cairo_surface_nil_write_error);
158
static DEFINE_NIL_SURFACE(CAIRO_STATUS_INVALID_STRIDE, _cairo_surface_nil_invalid_stride);
159
static DEFINE_NIL_SURFACE(CAIRO_STATUS_INVALID_SIZE, _cairo_surface_nil_invalid_size);
160
static DEFINE_NIL_SURFACE(CAIRO_STATUS_DEVICE_TYPE_MISMATCH, _cairo_surface_nil_device_type_mismatch);
161
static DEFINE_NIL_SURFACE(CAIRO_STATUS_DEVICE_ERROR, _cairo_surface_nil_device_error);
162
static DEFINE_NIL_SURFACE(CAIRO_STATUS_PNG_ERROR, _cairo_surface_nil_png_error);
163
164
static DEFINE_NIL_SURFACE(CAIRO_INT_STATUS_UNSUPPORTED, _cairo_surface_nil_unsupported);
165
static DEFINE_NIL_SURFACE(CAIRO_INT_STATUS_NOTHING_TO_DO, _cairo_surface_nil_nothing_to_do);
166
167
static void _cairo_surface_finish_snapshots (cairo_surface_t *surface);
168
static void _cairo_surface_finish (cairo_surface_t *surface);
169
170
/**
171
 * _cairo_surface_set_error:
172
 * @surface: a surface
173
 * @status: a status value indicating an error
174
 *
175
 * Atomically sets surface->status to @status and calls _cairo_error;
176
 * Does nothing if status is %CAIRO_STATUS_SUCCESS or any of the internal
177
 * status values.
178
 *
179
 * All assignments of an error status to surface->status should happen
180
 * through _cairo_surface_set_error(). Note that due to the nature of
181
 * the atomic operation, it is not safe to call this function on the
182
 * nil objects.
183
 *
184
 * The purpose of this function is to allow the user to set a
185
 * breakpoint in _cairo_error() to generate a stack trace for when the
186
 * user causes cairo to detect an error.
187
 *
188
 * Return value: the error status.
189
 **/
190
cairo_int_status_t
191
_cairo_surface_set_error (cairo_surface_t *surface,
192
        cairo_int_status_t status)
193
2.00M
{
194
    /* NOTHING_TO_DO is magic. We use it to break out of the inner-most
195
     * surface function, but anything higher just sees "success".
196
     */
197
2.00M
    if (status == CAIRO_INT_STATUS_NOTHING_TO_DO)
198
553
  status = CAIRO_INT_STATUS_SUCCESS;
199
200
2.00M
    if (status == CAIRO_INT_STATUS_SUCCESS ||
201
2.00M
        status >= (int)CAIRO_INT_STATUS_LAST_STATUS)
202
2.00M
        return status;
203
204
    /* Don't overwrite an existing error. This preserves the first
205
     * error, which is the most significant. */
206
8
    _cairo_status_set_error (&surface->status, (cairo_status_t)status);
207
208
8
    return _cairo_error (status);
209
8
}
210
211
/**
212
 * cairo_surface_get_type:
213
 * @surface: a #cairo_surface_t
214
 *
215
 * This function returns the type of the backend used to create
216
 * a surface. See #cairo_surface_type_t for available types.
217
 *
218
 * Return value: The type of @surface.
219
 *
220
 * Since: 1.2
221
 **/
222
cairo_surface_type_t
223
cairo_surface_get_type (cairo_surface_t *surface)
224
1.45k
{
225
    /* We don't use surface->backend->type here so that some of the
226
     * special "wrapper" surfaces such as cairo_paginated_surface_t
227
     * can override surface->type with the type of the "child"
228
     * surface. */
229
1.45k
    return surface->type;
230
1.45k
}
231
232
/**
233
 * cairo_surface_get_content:
234
 * @surface: a #cairo_surface_t
235
 *
236
 * This function returns the content type of @surface which indicates
237
 * whether the surface contains color and/or alpha information. See
238
 * #cairo_content_t.
239
 *
240
 * Return value: The content type of @surface.
241
 *
242
 * Since: 1.2
243
 **/
244
cairo_content_t
245
cairo_surface_get_content (cairo_surface_t *surface)
246
0
{
247
0
    return surface->content;
248
0
}
249
250
/**
251
 * cairo_surface_status:
252
 * @surface: a #cairo_surface_t
253
 *
254
 * Checks whether an error has previously occurred for this
255
 * surface.
256
 *
257
 * Return value: %CAIRO_STATUS_SUCCESS, %CAIRO_STATUS_NULL_POINTER,
258
 * %CAIRO_STATUS_NO_MEMORY, %CAIRO_STATUS_READ_ERROR,
259
 * %CAIRO_STATUS_INVALID_CONTENT, %CAIRO_STATUS_INVALID_FORMAT, or
260
 * %CAIRO_STATUS_INVALID_VISUAL.
261
 *
262
 * Since: 1.0
263
 **/
264
cairo_status_t
265
cairo_surface_status (cairo_surface_t *surface)
266
5.16k
{
267
5.16k
    return surface->status;
268
5.16k
}
269
270
static unsigned int
271
_cairo_surface_allocate_unique_id (void)
272
35.2k
{
273
35.2k
    static cairo_atomic_int_t unique_id;
274
275
#if CAIRO_NO_MUTEX
276
    if (++unique_id == 0)
277
  unique_id = 1;
278
    return unique_id;
279
#else
280
35.2k
    int old, id;
281
282
35.2k
    do {
283
35.2k
  old = _cairo_atomic_uint_get (&unique_id);
284
35.2k
  id = old + 1;
285
35.2k
  if (id == 0)
286
0
      id = 1;
287
35.2k
    } while (! _cairo_atomic_uint_cmpxchg (&unique_id, old, id));
288
289
35.2k
    return id;
290
35.2k
#endif
291
35.2k
}
292
293
/**
294
 * cairo_surface_get_device:
295
 * @surface: a #cairo_surface_t
296
 *
297
 * This function returns the device for a @surface.
298
 * See #cairo_device_t.
299
 *
300
 * Return value: The device for @surface or %NULL if the surface does
301
 *               not have an associated device.
302
 *
303
 * Since: 1.10
304
 **/
305
cairo_device_t *
306
cairo_surface_get_device (cairo_surface_t *surface)
307
0
{
308
0
    if (unlikely (surface->status))
309
0
  return _cairo_device_create_in_error (surface->status);
310
311
0
    return surface->device;
312
0
}
313
314
static cairo_bool_t
315
_cairo_surface_has_snapshots (cairo_surface_t *surface)
316
2.62M
{
317
2.62M
    return ! cairo_list_is_empty (&surface->snapshots);
318
2.62M
}
319
320
static cairo_bool_t
321
_cairo_surface_has_mime_data (cairo_surface_t *surface)
322
2.55M
{
323
2.55M
    return surface->mime_data.num_elements != 0;
324
2.55M
}
325
326
static void
327
_cairo_surface_detach_mime_data (cairo_surface_t *surface)
328
2.55M
{
329
2.55M
    if (! _cairo_surface_has_mime_data (surface))
330
2.54M
  return;
331
332
2.90k
    _cairo_user_data_array_fini (&surface->mime_data);
333
2.90k
    _cairo_user_data_array_init (&surface->mime_data);
334
2.90k
}
335
336
static void
337
_cairo_surface_detach_snapshots (cairo_surface_t *surface)
338
2.55M
{
339
2.55M
    while (_cairo_surface_has_snapshots (surface)) {
340
3.16k
  _cairo_surface_detach_snapshot (cairo_list_first_entry (&surface->snapshots,
341
3.16k
                cairo_surface_t,
342
3.16k
                snapshot));
343
3.16k
    }
344
2.55M
}
345
346
void
347
_cairo_surface_detach_snapshot (cairo_surface_t *snapshot)
348
3.72k
{
349
3.72k
    assert (snapshot->snapshot_of != NULL);
350
351
3.72k
    snapshot->snapshot_of = NULL;
352
3.72k
    cairo_list_del (&snapshot->snapshot);
353
354
3.72k
    if (snapshot->snapshot_detach != NULL)
355
3.16k
  snapshot->snapshot_detach (snapshot);
356
357
3.72k
    cairo_surface_destroy (snapshot);
358
3.72k
}
359
360
void
361
_cairo_surface_attach_snapshot (cairo_surface_t *surface,
362
         cairo_surface_t *snapshot,
363
         cairo_surface_func_t detach_func)
364
3.72k
{
365
3.72k
    assert (surface != snapshot);
366
3.72k
    assert (snapshot->snapshot_of != surface);
367
368
3.72k
    cairo_surface_reference (snapshot);
369
370
3.72k
    if (snapshot->snapshot_of != NULL)
371
0
  _cairo_surface_detach_snapshot (snapshot);
372
373
3.72k
    snapshot->snapshot_of = surface;
374
3.72k
    snapshot->snapshot_detach = detach_func;
375
376
3.72k
    cairo_list_add (&snapshot->snapshot, &surface->snapshots);
377
378
3.72k
    assert (_cairo_surface_has_snapshot (surface, snapshot->backend) == snapshot);
379
3.72k
}
380
381
cairo_surface_t *
382
_cairo_surface_has_snapshot (cairo_surface_t *surface,
383
           const cairo_surface_backend_t *backend)
384
7.45k
{
385
7.45k
    cairo_surface_t *snapshot;
386
387
7.45k
    cairo_list_foreach_entry (snapshot, cairo_surface_t,
388
7.45k
            &surface->snapshots, snapshot)
389
3.72k
    {
390
3.72k
  if (snapshot->backend == backend)
391
3.72k
      return snapshot;
392
3.72k
    }
393
394
3.72k
    return NULL;
395
7.45k
}
396
397
cairo_status_t
398
_cairo_surface_begin_modification (cairo_surface_t *surface)
399
2.01M
{
400
2.01M
    assert (surface->status == CAIRO_STATUS_SUCCESS);
401
2.01M
    assert (! surface->finished);
402
403
2.01M
    return _cairo_surface_flush (surface, 1);
404
2.01M
}
405
406
void
407
_cairo_surface_init (cairo_surface_t      *surface,
408
         const cairo_surface_backend_t  *backend,
409
         cairo_device_t     *device,
410
         cairo_content_t       content,
411
         cairo_bool_t                        is_vector)
412
35.2k
{
413
35.2k
    CAIRO_MUTEX_INITIALIZE ();
414
415
35.2k
    surface->backend = backend;
416
35.2k
    surface->device = cairo_device_reference (device);
417
35.2k
    surface->content = content;
418
35.2k
    surface->type = backend->type;
419
35.2k
    surface->is_vector = is_vector;
420
421
35.2k
    CAIRO_REFERENCE_COUNT_INIT (&surface->ref_count, 1);
422
35.2k
    surface->status = CAIRO_STATUS_SUCCESS;
423
35.2k
    surface->unique_id = _cairo_surface_allocate_unique_id ();
424
35.2k
    surface->finished = FALSE;
425
35.2k
    surface->_finishing = FALSE;
426
35.2k
    surface->is_clear = FALSE;
427
35.2k
    surface->serial = 0;
428
35.2k
    surface->damage = NULL;
429
35.2k
    surface->owns_device = (device != NULL);
430
431
35.2k
    _cairo_user_data_array_init (&surface->user_data);
432
35.2k
    _cairo_user_data_array_init (&surface->mime_data);
433
434
35.2k
    cairo_matrix_init_identity (&surface->device_transform);
435
35.2k
    cairo_matrix_init_identity (&surface->device_transform_inverse);
436
35.2k
    cairo_list_init (&surface->device_transform_observers);
437
438
35.2k
    surface->x_resolution = CAIRO_SURFACE_RESOLUTION_DEFAULT;
439
35.2k
    surface->y_resolution = CAIRO_SURFACE_RESOLUTION_DEFAULT;
440
441
35.2k
    surface->x_fallback_resolution = CAIRO_SURFACE_FALLBACK_RESOLUTION_DEFAULT;
442
35.2k
    surface->y_fallback_resolution = CAIRO_SURFACE_FALLBACK_RESOLUTION_DEFAULT;
443
444
35.2k
    cairo_list_init (&surface->snapshots);
445
35.2k
    surface->snapshot_of = NULL;
446
447
35.2k
    surface->has_font_options = FALSE;
448
449
35.2k
    surface->foreground_source = NULL;
450
35.2k
    surface->foreground_used = FALSE;
451
35.2k
}
452
453
static void
454
_cairo_surface_copy_similar_properties (cairo_surface_t *surface,
455
          cairo_surface_t *other)
456
608
{
457
608
    if (other->has_font_options || other->backend != surface->backend) {
458
606
  cairo_font_options_t options;
459
460
606
  cairo_surface_get_font_options (other, &options);
461
606
  _cairo_surface_set_font_options (surface, &options);
462
606
  _cairo_font_options_fini (&options);
463
606
    }
464
465
608
    cairo_surface_set_fallback_resolution (surface,
466
608
             other->x_fallback_resolution,
467
608
             other->y_fallback_resolution);
468
608
}
469
470
/**
471
 * cairo_surface_create_similar:
472
 * @other: an existing surface used to select the backend of the new surface
473
 * @content: the content for the new surface
474
 * @width: width of the new surface, (in device-space units)
475
 * @height: height of the new surface (in device-space units)
476
 *
477
 * Create a new surface that is as compatible as possible with an
478
 * existing surface. For example the new surface will have the same
479
 * device scale, fallback resolution and font options as
480
 * @other. Generally, the new surface will also use the same backend
481
 * as @other, unless that is not possible for some reason. The type of
482
 * the returned surface may be examined with
483
 * cairo_surface_get_type().
484
 *
485
 * Initially the surface contents are all 0 (transparent if contents
486
 * have transparency, black otherwise.)
487
 *
488
 * Use cairo_surface_create_similar_image() if you need an image surface
489
 * which can be painted quickly to the target surface.
490
 *
491
 * Return value: a pointer to the newly allocated surface. The caller
492
 * owns the surface and should call cairo_surface_destroy() when done
493
 * with it.
494
 *
495
 * This function always returns a valid pointer, but it will return a
496
 * pointer to a "nil" surface if @other is already in an error state
497
 * or any other error occurs.
498
 *
499
 * Since: 1.0
500
 **/
501
cairo_surface_t *
502
cairo_surface_create_similar (cairo_surface_t  *other,
503
            cairo_content_t content,
504
            int   width,
505
            int   height)
506
550
{
507
550
    cairo_surface_t *surface;
508
550
    cairo_status_t status;
509
550
    cairo_solid_pattern_t pattern;
510
511
550
    if (unlikely (other->status))
512
0
  return _cairo_surface_create_in_error (other->status);
513
550
    if (unlikely (other->finished))
514
0
  return _cairo_surface_create_in_error (CAIRO_STATUS_SURFACE_FINISHED);
515
550
    if (unlikely (width < 0 || height < 0))
516
0
  return _cairo_surface_create_in_error (CAIRO_STATUS_INVALID_SIZE);
517
550
    if (unlikely (! CAIRO_CONTENT_VALID (content)))
518
0
  return _cairo_surface_create_in_error (CAIRO_STATUS_INVALID_CONTENT);
519
520
    /* We inherit the device scale, so create a larger surface */
521
550
    width = width * other->device_transform.xx;
522
550
    height = height * other->device_transform.yy;
523
524
550
    surface = NULL;
525
550
    if (other->backend->create_similar)
526
550
  surface = other->backend->create_similar (other, content, width, height);
527
550
    if (surface == NULL)
528
0
  surface = cairo_surface_create_similar_image (other,
529
0
                  _cairo_format_from_content (content),
530
0
                  width, height);
531
532
550
    if (unlikely (surface->status))
533
0
  return surface;
534
535
550
    _cairo_surface_copy_similar_properties (surface, other);
536
550
    cairo_surface_set_device_scale (surface,
537
550
            other->device_transform.xx,
538
550
            other->device_transform.yy);
539
540
550
    if (unlikely (surface->status))
541
0
  return surface;
542
543
550
    _cairo_pattern_init_solid (&pattern, CAIRO_COLOR_TRANSPARENT);
544
550
    status = _cairo_surface_paint (surface,
545
550
           CAIRO_OPERATOR_CLEAR,
546
550
           &pattern.base, NULL);
547
550
    if (unlikely (status)) {
548
0
  cairo_surface_destroy (surface);
549
0
  surface = _cairo_surface_create_in_error (status);
550
0
    }
551
552
550
    assert (surface->is_clear);
553
554
550
    return surface;
555
550
}
556
557
/**
558
 * cairo_surface_create_similar_image:
559
 * @other: an existing surface used to select the preference of the new surface
560
 * @format: the format for the new surface
561
 * @width: width of the new surface, (in pixels)
562
 * @height: height of the new surface (in pixels)
563
 *
564
 * Create a new image surface that is as compatible as possible for uploading
565
 * to and the use in conjunction with an existing surface. However, this surface
566
 * can still be used like any normal image surface. Unlike
567
 * cairo_surface_create_similar() the new image surface won't inherit
568
 * the device scale from @other.
569
 *
570
 * Initially the surface contents are all 0 (transparent if contents
571
 * have transparency, black otherwise.)
572
 *
573
 * Use cairo_surface_create_similar() if you don't need an image surface.
574
 *
575
 * Return value: a pointer to the newly allocated image surface. The caller
576
 * owns the surface and should call cairo_surface_destroy() when done
577
 * with it.
578
 *
579
 * This function always returns a valid pointer, but it will return a
580
 * pointer to a "nil" surface if @other is already in an error state
581
 * or any other error occurs.
582
 *
583
 * Since: 1.12
584
 **/
585
cairo_surface_t *
586
cairo_surface_create_similar_image (cairo_surface_t  *other,
587
            cairo_format_t    format,
588
            int   width,
589
            int   height)
590
0
{
591
0
    cairo_surface_t *image;
592
593
0
    if (unlikely (other->status))
594
0
  return _cairo_surface_create_in_error (other->status);
595
0
    if (unlikely (other->finished))
596
0
  return _cairo_surface_create_in_error (CAIRO_STATUS_SURFACE_FINISHED);
597
598
0
    if (unlikely (width < 0 || height < 0))
599
0
  return _cairo_surface_create_in_error (CAIRO_STATUS_INVALID_SIZE);
600
0
    if (unlikely (! CAIRO_FORMAT_VALID (format)))
601
0
  return _cairo_surface_create_in_error (CAIRO_STATUS_INVALID_FORMAT);
602
603
0
    image = NULL;
604
0
    if (other->backend->create_similar_image)
605
0
  image = other->backend->create_similar_image (other,
606
0
                  format, width, height);
607
0
    if (image == NULL)
608
0
  image = cairo_image_surface_create (format, width, height);
609
610
0
    assert (image->is_clear);
611
612
0
    return image;
613
0
}
614
615
/**
616
 * _cairo_surface_map_to_image:
617
 * @surface: an existing surface used to extract the image from
618
 * @extents: limit the extraction to an rectangular region
619
 *
620
 * Returns an image surface that is the most efficient mechanism for
621
 * modifying the backing store of the target surface. The region
622
 * retrieved is limited to @extents.
623
 *
624
 * Note, the use of the original surface as a target or source whilst
625
 * it is mapped is undefined. The result of mapping the surface
626
 * multiple times is undefined. Calling cairo_surface_destroy() or
627
 * cairo_surface_finish() on the resulting image surface results in
628
 * undefined behavior. Changing the device transform of the image
629
 * surface or of @surface before the image surface is unmapped results
630
 * in undefined behavior.
631
 *
632
 * Assumes that @surface is valid (CAIRO_STATUS_SUCCESS,
633
 * non-finished).
634
 *
635
 * Return value: a pointer to the newly allocated image surface. The
636
 * caller must use _cairo_surface_unmap_image() to destroy this image
637
 * surface.
638
 *
639
 * This function always returns a valid pointer, but it will return a
640
 * pointer to a "nil" surface if @other is already in an error state
641
 * or any other error occurs.
642
 *
643
 * The returned image might have a %CAIRO_FORMAT_INVALID format.
644
 **/
645
cairo_image_surface_t *
646
_cairo_surface_map_to_image (cairo_surface_t  *surface,
647
           const cairo_rectangle_int_t *extents)
648
0
{
649
0
    cairo_image_surface_t *image = NULL;
650
651
0
    assert (extents != NULL);
652
653
    /* TODO: require map_to_image != NULL */
654
0
    if (surface->backend->map_to_image)
655
0
  image = surface->backend->map_to_image (surface, extents);
656
657
0
    if (image == NULL)
658
0
  image = _cairo_image_surface_clone_subimage (surface, extents);
659
660
0
    return image;
661
0
}
662
663
/**
664
 * _cairo_surface_unmap_image:
665
 * @surface: the surface passed to _cairo_surface_map_to_image().
666
 * @image: the currently mapped image
667
 *
668
 * Unmaps the image surface as returned from
669
 * _cairo_surface_map_to_image().
670
 *
671
 * The content of the image will be uploaded to the target surface.
672
 * Afterwards, the image is destroyed.
673
 *
674
 * Using an image surface which wasn't returned by
675
 * _cairo_surface_map_to_image() results in undefined behavior.
676
 *
677
 * An image surface in error status can be passed to
678
 * _cairo_surface_unmap_image().
679
 *
680
 * Return value: the unmap status.
681
 *
682
 * Even if the unmap status is not successful, @image is destroyed.
683
 **/
684
cairo_int_status_t
685
_cairo_surface_unmap_image (cairo_surface_t       *surface,
686
          cairo_image_surface_t *image)
687
0
{
688
0
    cairo_surface_pattern_t pattern;
689
0
    cairo_rectangle_int_t extents;
690
0
    cairo_clip_t *clip;
691
0
    cairo_int_status_t status;
692
693
    /* map_to_image can return error surfaces */
694
0
    if (unlikely (image->base.status)) {
695
0
  status = image->base.status;
696
0
  goto destroy;
697
0
    }
698
699
    /* If the image is untouched just skip the update */
700
0
    if (image->base.serial == 0) {
701
0
  status = CAIRO_STATUS_SUCCESS;
702
0
  goto destroy;
703
0
    }
704
705
    /* TODO: require unmap_image != NULL */
706
0
    if (surface->backend->unmap_image &&
707
0
  ! _cairo_image_surface_is_clone (image))
708
0
    {
709
0
  status = surface->backend->unmap_image (surface, image);
710
0
  if (status != CAIRO_INT_STATUS_UNSUPPORTED)
711
0
      return status;
712
0
    }
713
714
0
    _cairo_pattern_init_for_surface (&pattern, &image->base);
715
0
    pattern.base.filter = CAIRO_FILTER_NEAREST;
716
717
    /* We have to apply the translate from map_to_image's extents.x and .y */
718
0
    cairo_matrix_init_translate (&pattern.base.matrix,
719
0
         image->base.device_transform.x0,
720
0
         image->base.device_transform.y0);
721
722
    /* And we also have to clip the operation to the image's extents */
723
0
    extents.x = image->base.device_transform_inverse.x0;
724
0
    extents.y = image->base.device_transform_inverse.y0;
725
0
    extents.width  = image->width;
726
0
    extents.height = image->height;
727
0
    clip = _cairo_clip_intersect_rectangle (NULL, &extents);
728
729
0
    status = _cairo_surface_paint (surface,
730
0
           CAIRO_OPERATOR_SOURCE,
731
0
           &pattern.base,
732
0
           clip);
733
734
0
    _cairo_pattern_fini (&pattern.base);
735
0
    _cairo_clip_destroy (clip);
736
737
0
destroy:
738
0
    cairo_surface_finish (&image->base);
739
0
    cairo_surface_destroy (&image->base);
740
741
0
    return status;
742
0
}
743
744
/**
745
 * cairo_surface_map_to_image:
746
 * @surface: an existing surface used to extract the image from
747
 * @extents: limit the extraction to an rectangular region
748
 *
749
 * Returns an image surface that is the most efficient mechanism for
750
 * modifying the backing store of the target surface. The region retrieved
751
 * may be limited to the @extents or %NULL for the whole surface
752
 *
753
 * Note, the use of the original surface as a target or source whilst
754
 * it is mapped is undefined. The result of mapping the surface
755
 * multiple times is undefined. Calling cairo_surface_destroy() or
756
 * cairo_surface_finish() on the resulting image surface results in
757
 * undefined behavior. Changing the device transform of the image
758
 * surface or of @surface before the image surface is unmapped results
759
 * in undefined behavior.
760
 *
761
 * Return value: a pointer to the newly allocated image surface. The caller
762
 * must use cairo_surface_unmap_image() to destroy this image surface.
763
 *
764
 * This function always returns a valid pointer, but it will return a
765
 * pointer to a "nil" surface if @other is already in an error state
766
 * or any other error occurs. If the returned pointer does not have an
767
 * error status, it is guaranteed to be an image surface whose format
768
 * is not %CAIRO_FORMAT_INVALID.
769
 *
770
 * Since: 1.12
771
 **/
772
cairo_surface_t *
773
cairo_surface_map_to_image (cairo_surface_t  *surface,
774
          const cairo_rectangle_int_t *extents)
775
0
{
776
0
    cairo_rectangle_int_t rect;
777
0
    cairo_image_surface_t *image;
778
0
    cairo_status_t status;
779
780
0
    if (unlikely (surface->status))
781
0
  return _cairo_surface_create_in_error (surface->status);
782
0
    if (unlikely (surface->finished))
783
0
  return _cairo_surface_create_in_error (CAIRO_STATUS_SURFACE_FINISHED);
784
785
0
    if (extents == NULL) {
786
0
  if (unlikely (! surface->backend->get_extents (surface, &rect)))
787
0
      return _cairo_surface_create_in_error (CAIRO_STATUS_INVALID_SIZE);
788
789
0
  extents = &rect;
790
0
    } else {
791
0
  cairo_rectangle_int_t surface_extents;
792
793
  /* If this surface is bounded, we can't map parts
794
   * that are outside of it. */
795
0
  if (likely (surface->backend->get_extents (surface, &surface_extents))) {
796
0
      if (unlikely (! _cairo_rectangle_contains_rectangle (&surface_extents, extents)))
797
0
    return _cairo_surface_create_in_error (CAIRO_STATUS_INVALID_SIZE);
798
0
  }
799
0
    }
800
801
0
    image = _cairo_surface_map_to_image (surface, extents);
802
803
0
    status = image->base.status;
804
0
    if (unlikely (status)) {
805
0
  cairo_surface_destroy (&image->base);
806
0
  return _cairo_surface_create_in_error (status);
807
0
    }
808
809
0
    if (image->format == CAIRO_FORMAT_INVALID) {
810
0
  cairo_surface_destroy (&image->base);
811
0
  image = _cairo_image_surface_clone_subimage (surface, extents);
812
0
    }
813
814
0
    return &image->base;
815
0
}
816
817
/**
818
 * cairo_surface_unmap_image:
819
 * @surface: the surface passed to cairo_surface_map_to_image().
820
 * @image: the currently mapped image
821
 *
822
 * Unmaps the image surface as returned from #cairo_surface_map_to_image().
823
 *
824
 * The content of the image will be uploaded to the target surface.
825
 * Afterwards, the image is destroyed.
826
 *
827
 * Using an image surface which wasn't returned by cairo_surface_map_to_image()
828
 * results in undefined behavior.
829
 *
830
 * Since: 1.12
831
 **/
832
void
833
cairo_surface_unmap_image (cairo_surface_t *surface,
834
         cairo_surface_t *image)
835
0
{
836
0
    cairo_int_status_t status = CAIRO_STATUS_SUCCESS;
837
838
0
    if (unlikely (surface->status)) {
839
0
  status = surface->status;
840
0
  goto error;
841
0
    }
842
0
    if (unlikely (surface->finished)) {
843
0
  status = _cairo_error (CAIRO_STATUS_SURFACE_FINISHED);
844
0
  goto error;
845
0
    }
846
0
    if (unlikely (image->status)) {
847
0
  status = image->status;
848
0
  goto error;
849
0
    }
850
0
    if (unlikely (image->finished)) {
851
0
  status = _cairo_error (CAIRO_STATUS_SURFACE_FINISHED);
852
0
  goto error;
853
0
    }
854
0
    if (unlikely (! _cairo_surface_is_image (image))) {
855
0
  status = _cairo_error (CAIRO_STATUS_SURFACE_TYPE_MISMATCH);
856
0
  goto error;
857
0
    }
858
859
0
    status = _cairo_surface_unmap_image (surface,
860
0
           (cairo_image_surface_t *) image);
861
0
    if (unlikely (status))
862
0
  _cairo_surface_set_error (surface, status);
863
864
0
    return;
865
866
0
error:
867
0
    _cairo_surface_set_error (surface, status);
868
0
    cairo_surface_finish (image);
869
0
    cairo_surface_destroy (image);
870
0
}
871
872
cairo_surface_t *
873
_cairo_surface_create_scratch (cairo_surface_t   *other,
874
             cairo_content_t    content,
875
             int      width,
876
             int      height,
877
             const cairo_color_t *color)
878
58
{
879
58
    cairo_surface_t *surface;
880
58
    cairo_status_t status;
881
58
    cairo_solid_pattern_t pattern;
882
883
58
    if (unlikely (other->status))
884
0
  return _cairo_surface_create_in_error (other->status);
885
886
58
    surface = NULL;
887
58
    if (other->backend->create_similar)
888
58
  surface = other->backend->create_similar (other, content, width, height);
889
58
    if (surface == NULL)
890
0
  surface = cairo_surface_create_similar_image (other,
891
0
                  _cairo_format_from_content (content),
892
0
                  width, height);
893
894
58
    if (unlikely (surface->status))
895
0
  return surface;
896
897
58
    _cairo_surface_copy_similar_properties (surface, other);
898
899
58
    if (unlikely (surface->status))
900
0
  return surface;
901
902
58
    if (color) {
903
56
  _cairo_pattern_init_solid (&pattern, color);
904
56
  status = _cairo_surface_paint (surface,
905
56
               color == CAIRO_COLOR_TRANSPARENT ?
906
56
               CAIRO_OPERATOR_CLEAR : CAIRO_OPERATOR_SOURCE,
907
56
               &pattern.base, NULL);
908
56
  if (unlikely (status)) {
909
0
      cairo_surface_destroy (surface);
910
0
      surface = _cairo_surface_create_in_error (status);
911
0
  }
912
56
    }
913
914
58
    return surface;
915
58
}
916
917
/**
918
 * cairo_surface_reference:
919
 * @surface: a #cairo_surface_t
920
 *
921
 * Increases the reference count on @surface by one. This prevents
922
 * @surface from being destroyed until a matching call to
923
 * cairo_surface_destroy() is made.
924
 *
925
 * Use cairo_surface_get_reference_count() to get the number of
926
 * references to a #cairo_surface_t.
927
 *
928
 * Return value: the referenced #cairo_surface_t.
929
 *
930
 * Since: 1.0
931
 **/
932
cairo_surface_t *
933
cairo_surface_reference (cairo_surface_t *surface)
934
105k
{
935
105k
    if (surface == NULL ||
936
105k
      CAIRO_REFERENCE_COUNT_IS_INVALID (&surface->ref_count))
937
0
  return surface;
938
939
105k
    assert (CAIRO_REFERENCE_COUNT_HAS_REFERENCE (&surface->ref_count));
940
941
105k
    _cairo_reference_count_inc (&surface->ref_count);
942
943
105k
    return surface;
944
105k
}
945
946
/**
947
 * cairo_surface_destroy:
948
 * @surface: a #cairo_surface_t
949
 *
950
 * Decreases the reference count on @surface by one. If the result is
951
 * zero, then @surface and all associated resources are freed.  See
952
 * cairo_surface_reference().
953
 *
954
 * Since: 1.0
955
 **/
956
void
957
cairo_surface_destroy (cairo_surface_t *surface)
958
211k
{
959
211k
    if (surface == NULL ||
960
211k
      CAIRO_REFERENCE_COUNT_IS_INVALID (&surface->ref_count))
961
40.2k
  return;
962
963
171k
    assert (CAIRO_REFERENCE_COUNT_HAS_REFERENCE (&surface->ref_count));
964
965
171k
    if (! _cairo_reference_count_dec_and_test (&surface->ref_count))
966
139k
  return;
967
968
32.4k
    assert (surface->snapshot_of == NULL);
969
970
32.4k
    if (! surface->finished) {
971
27.3k
  _cairo_surface_finish_snapshots (surface);
972
  /* We may have been referenced by a snapshot prior to have
973
   * detaching it with the copy-on-write.
974
   */
975
27.3k
  if (CAIRO_REFERENCE_COUNT_GET_VALUE (&surface->ref_count))
976
0
      return;
977
978
27.3k
  _cairo_surface_finish (surface);
979
27.3k
    }
980
981
32.4k
    if (surface->damage)
982
0
  _cairo_damage_destroy (surface->damage);
983
984
32.4k
    _cairo_user_data_array_fini (&surface->user_data);
985
32.4k
    _cairo_user_data_array_fini (&surface->mime_data);
986
987
32.4k
    if (surface->foreground_source)
988
0
  cairo_pattern_destroy (surface->foreground_source);
989
990
32.4k
    if (surface->owns_device)
991
0
        cairo_device_destroy (surface->device);
992
993
32.4k
    if (surface->has_font_options)
994
1.23k
  _cairo_font_options_fini (&surface->font_options);
995
996
32.4k
    assert (surface->snapshot_of == NULL);
997
32.4k
    assert (! _cairo_surface_has_snapshots (surface));
998
    /* paranoid check that nobody took a reference whilst finishing */
999
32.4k
    assert (! CAIRO_REFERENCE_COUNT_HAS_REFERENCE (&surface->ref_count));
1000
1001
32.4k
    free (surface);
1002
32.4k
}
1003
1004
/**
1005
 * cairo_surface_get_reference_count:
1006
 * @surface: a #cairo_surface_t
1007
 *
1008
 * Returns the current reference count of @surface.
1009
 *
1010
 * Return value: the current reference count of @surface.  If the
1011
 * object is a nil object, 0 will be returned.
1012
 *
1013
 * Since: 1.4
1014
 **/
1015
unsigned int
1016
cairo_surface_get_reference_count (cairo_surface_t *surface)
1017
0
{
1018
0
    if (surface == NULL ||
1019
0
      CAIRO_REFERENCE_COUNT_IS_INVALID (&surface->ref_count))
1020
0
  return 0;
1021
1022
0
    return CAIRO_REFERENCE_COUNT_GET_VALUE (&surface->ref_count);
1023
0
}
1024
1025
static void
1026
_cairo_surface_finish_snapshots (cairo_surface_t *surface)
1027
32.4k
{
1028
32.4k
    cairo_status_t status;
1029
1030
    /* update the snapshots *before* we declare the surface as finished */
1031
32.4k
    surface->_finishing = TRUE;
1032
32.4k
    status = _cairo_surface_flush (surface, 0);
1033
32.4k
    (void) status;
1034
32.4k
}
1035
1036
static void
1037
_cairo_surface_finish (cairo_surface_t *surface)
1038
32.4k
{
1039
32.4k
    cairo_status_t status;
1040
1041
    /* call finish even if in error mode */
1042
32.4k
    if (surface->backend->finish) {
1043
32.3k
  status = surface->backend->finish (surface);
1044
32.3k
  if (unlikely (status))
1045
8
      _cairo_surface_set_error (surface, status);
1046
32.3k
    }
1047
1048
32.4k
    surface->finished = TRUE;
1049
1050
32.4k
    assert (surface->snapshot_of == NULL);
1051
32.4k
    assert (!_cairo_surface_has_snapshots (surface));
1052
32.4k
}
1053
1054
/**
1055
 * cairo_surface_finish:
1056
 * @surface: the #cairo_surface_t to finish
1057
 *
1058
 * This function finishes the surface and drops all references to
1059
 * external resources.  For example, for the Xlib backend it means
1060
 * that cairo will no longer access the drawable, which can be freed.
1061
 * After calling cairo_surface_finish() the only valid operations on a
1062
 * surface are checking status, getting and setting user, referencing
1063
 * and destroying, and flushing and finishing it.
1064
 * Further drawing to the surface will not affect the
1065
 * surface but will instead trigger a %CAIRO_STATUS_SURFACE_FINISHED
1066
 * error.
1067
 *
1068
 * When the last call to cairo_surface_destroy() decreases the
1069
 * reference count to zero, cairo will call cairo_surface_finish() if
1070
 * it hasn't been called already, before freeing the resources
1071
 * associated with the surface.
1072
 *
1073
 * Since: 1.0
1074
 **/
1075
void
1076
cairo_surface_finish (cairo_surface_t *surface)
1077
5.06k
{
1078
5.06k
    if (surface == NULL)
1079
0
  return;
1080
1081
5.06k
    if (CAIRO_REFERENCE_COUNT_IS_INVALID (&surface->ref_count))
1082
0
  return;
1083
1084
5.06k
    if (surface->finished)
1085
0
  return;
1086
1087
    /* We have to be careful when decoupling potential reference cycles */
1088
5.06k
    cairo_surface_reference (surface);
1089
1090
5.06k
    _cairo_surface_finish_snapshots (surface);
1091
    /* XXX need to block and wait for snapshot references */
1092
5.06k
    _cairo_surface_finish (surface);
1093
1094
5.06k
    cairo_surface_destroy (surface);
1095
5.06k
}
1096
1097
/**
1098
 * _cairo_surface_release_device_reference:
1099
 * @surface: a #cairo_surface_t
1100
 *
1101
 * This function makes @surface release the reference to its device. The
1102
 * function is intended to be used for avoiding cycling references for
1103
 * surfaces that are owned by their device, for example cache surfaces.
1104
 * Note that the @surface will still assume that the device is available.
1105
 * So it is the caller's responsibility to ensure the device stays around
1106
 * until the @surface is destroyed. Just calling cairo_surface_finish() is
1107
 * not enough.
1108
 **/
1109
void
1110
_cairo_surface_release_device_reference (cairo_surface_t *surface)
1111
0
{
1112
0
    assert (surface->owns_device);
1113
1114
0
    cairo_device_destroy (surface->device);
1115
0
    surface->owns_device = FALSE;
1116
0
}
1117
1118
/**
1119
 * cairo_surface_get_user_data:
1120
 * @surface: a #cairo_surface_t
1121
 * @key: the address of the #cairo_user_data_key_t the user data was
1122
 * attached to
1123
 *
1124
 * Return user data previously attached to @surface using the specified
1125
 * key.  If no user data has been attached with the given key this
1126
 * function returns %NULL.
1127
 *
1128
 * Return value: the user data previously attached or %NULL.
1129
 *
1130
 * Since: 1.0
1131
 **/
1132
void *
1133
cairo_surface_get_user_data (cairo_surface_t     *surface,
1134
           const cairo_user_data_key_t *key)
1135
0
{
1136
    /* Prevent reads of the array during teardown */
1137
0
    if (! CAIRO_REFERENCE_COUNT_HAS_REFERENCE (&surface->ref_count))
1138
0
  return NULL;
1139
1140
0
    return _cairo_user_data_array_get_data (&surface->user_data, key);
1141
0
}
1142
1143
/**
1144
 * cairo_surface_set_user_data:
1145
 * @surface: a #cairo_surface_t
1146
 * @key: the address of a #cairo_user_data_key_t to attach the user data to
1147
 * @user_data: the user data to attach to the surface
1148
 * @destroy: a #cairo_destroy_func_t which will be called when the
1149
 * surface is destroyed or when new user data is attached using the
1150
 * same key.
1151
 *
1152
 * Attach user data to @surface.  To remove user data from a surface,
1153
 * call this function with the key that was used to set it and %NULL
1154
 * for @data.
1155
 *
1156
 * Return value: %CAIRO_STATUS_SUCCESS or %CAIRO_STATUS_NO_MEMORY if a
1157
 * slot could not be allocated for the user data.
1158
 *
1159
 * Since: 1.0
1160
 **/
1161
cairo_status_t
1162
cairo_surface_set_user_data (cairo_surface_t     *surface,
1163
           const cairo_user_data_key_t *key,
1164
           void      *user_data,
1165
           cairo_destroy_func_t  destroy)
1166
0
{
1167
0
    if (CAIRO_REFERENCE_COUNT_IS_INVALID (&surface->ref_count))
1168
0
  return surface->status;
1169
1170
0
    if (! CAIRO_REFERENCE_COUNT_HAS_REFERENCE (&surface->ref_count))
1171
0
  return _cairo_error (CAIRO_STATUS_SURFACE_FINISHED);
1172
1173
0
    return _cairo_user_data_array_set_data (&surface->user_data,
1174
0
              key, user_data, destroy);
1175
0
}
1176
1177
/**
1178
 * cairo_surface_get_mime_data:
1179
 * @surface: a #cairo_surface_t
1180
 * @mime_type: the mime type of the image data
1181
 * @data: the image data to attached to the surface
1182
 * @length: the length of the image data
1183
 *
1184
 * Return mime data previously attached to @surface using the
1185
 * specified mime type.  If no data has been attached with the given
1186
 * mime type, @data is set %NULL.
1187
 *
1188
 * Since: 1.10
1189
 **/
1190
void
1191
cairo_surface_get_mime_data (cairo_surface_t    *surface,
1192
                             const char     *mime_type,
1193
                             const unsigned char       **data,
1194
                             unsigned long    *length)
1195
13.7k
{
1196
13.7k
    cairo_user_data_slot_t *slots;
1197
13.7k
    int i, num_slots;
1198
1199
13.7k
    *data = NULL;
1200
13.7k
    *length = 0;
1201
1202
    /* Prevent reads of the array during teardown */
1203
13.7k
    if (! CAIRO_REFERENCE_COUNT_HAS_REFERENCE (&surface->ref_count))
1204
0
  return;
1205
1206
    /* The number of mime-types attached to a surface is usually small,
1207
     * typically zero. Therefore it is quicker to do a strcmp() against
1208
     * each key than it is to intern the string (i.e. compute a hash,
1209
     * search the hash table, and do a final strcmp).
1210
     */
1211
13.7k
    num_slots = surface->mime_data.num_elements;
1212
13.7k
    slots = _cairo_array_index (&surface->mime_data, 0);
1213
17.4k
    for (i = 0; i < num_slots; i++) {
1214
5.18k
  if (slots[i].key != NULL && strcmp ((char *) slots[i].key, mime_type) == 0) {
1215
1.46k
      cairo_mime_data_t *mime_data = slots[i].user_data;
1216
1217
1.46k
      *data = mime_data->data;
1218
1.46k
      *length = mime_data->length;
1219
1.46k
      return;
1220
1.46k
  }
1221
5.18k
    }
1222
13.7k
}
1223
1224
static void
1225
_cairo_mime_data_destroy (void *ptr)
1226
2.92k
{
1227
2.92k
    cairo_mime_data_t *mime_data = ptr;
1228
1229
2.92k
    if (! _cairo_reference_count_dec_and_test (&mime_data->ref_count))
1230
1.46k
  return;
1231
1232
1.46k
    if (mime_data->destroy && mime_data->closure)
1233
1.46k
  mime_data->destroy (mime_data->closure);
1234
1235
1.46k
    free (mime_data);
1236
1.46k
}
1237
1238
1239
static const char *_cairo_surface_image_mime_types[] = {
1240
    CAIRO_MIME_TYPE_JPEG,
1241
    CAIRO_MIME_TYPE_PNG,
1242
    CAIRO_MIME_TYPE_JP2,
1243
    CAIRO_MIME_TYPE_JBIG2,
1244
    CAIRO_MIME_TYPE_CCITT_FAX,
1245
};
1246
1247
cairo_bool_t
1248
_cairo_surface_has_mime_image (cairo_surface_t *surface)
1249
548
{
1250
548
    cairo_user_data_slot_t *slots;
1251
548
    int i, j, num_slots;
1252
1253
    /* Prevent reads of the array during teardown */
1254
548
    if (! CAIRO_REFERENCE_COUNT_HAS_REFERENCE (&surface->ref_count))
1255
0
  return FALSE;
1256
1257
    /* The number of mime-types attached to a surface is usually small,
1258
     * typically zero. Therefore it is quicker to do a strcmp() against
1259
     * each key than it is to intern the string (i.e. compute a hash,
1260
     * search the hash table, and do a final strcmp).
1261
     */
1262
548
    num_slots = surface->mime_data.num_elements;
1263
548
    slots = _cairo_array_index (&surface->mime_data, 0);
1264
548
    for (i = 0; i < num_slots; i++) {
1265
0
  if (slots[i].key != NULL) {
1266
0
      for (j = 0; j < ARRAY_LENGTH (_cairo_surface_image_mime_types); j++) {
1267
0
    if (strcmp ((char *) slots[i].key, _cairo_surface_image_mime_types[j]) == 0)
1268
0
        return TRUE;
1269
0
      }
1270
0
  }
1271
0
    }
1272
1273
548
    return FALSE;
1274
548
}
1275
1276
/**
1277
 * CAIRO_MIME_TYPE_CCITT_FAX:
1278
 *
1279
 * Group 3 or Group 4 CCITT facsimile encoding (International
1280
 * Telecommunication Union, Recommendations T.4 and T.6.)
1281
 *
1282
 * Since: 1.16
1283
 **/
1284
1285
/**
1286
 * CAIRO_MIME_TYPE_CCITT_FAX_PARAMS:
1287
 *
1288
 * Decode parameters for Group 3 or Group 4 CCITT facsimile encoding.
1289
 * See [CCITT Fax Images][ccitt].
1290
 *
1291
 * Since: 1.16
1292
 **/
1293
1294
/**
1295
 * CAIRO_MIME_TYPE_EPS:
1296
 *
1297
 * Encapsulated PostScript file.
1298
 * [Encapsulated PostScript File Format Specification](http://wwwimages.adobe.com/content/dam/Adobe/endevnet/postscript/pdfs/5002.EPSF_Spec.pdf)
1299
 *
1300
 * Since: 1.16
1301
 **/
1302
1303
/**
1304
 * CAIRO_MIME_TYPE_EPS_PARAMS:
1305
 *
1306
 * Embedding parameters Encapsulated PostScript data.
1307
 * See [Embedding EPS files][eps].
1308
 *
1309
 * Since: 1.16
1310
 **/
1311
1312
/**
1313
 * CAIRO_MIME_TYPE_JBIG2:
1314
 *
1315
 * Joint Bi-level Image Experts Group image coding standard (ISO/IEC 11544).
1316
 *
1317
 * Since: 1.14
1318
 **/
1319
1320
/**
1321
 * CAIRO_MIME_TYPE_JBIG2_GLOBAL:
1322
 *
1323
 * Joint Bi-level Image Experts Group image coding standard (ISO/IEC 11544) global segment.
1324
 *
1325
 * Since: 1.14
1326
 **/
1327
1328
/**
1329
 * CAIRO_MIME_TYPE_JBIG2_GLOBAL_ID:
1330
 *
1331
 * An unique identifier shared by a JBIG2 global segment and all JBIG2 images
1332
 * that depend on the global segment.
1333
 *
1334
 * Since: 1.14
1335
 **/
1336
1337
/**
1338
 * CAIRO_MIME_TYPE_JP2:
1339
 *
1340
 * The Joint Photographic Experts Group (JPEG) 2000 image coding standard (ISO/IEC 15444-1).
1341
 *
1342
 * Since: 1.10
1343
 **/
1344
1345
/**
1346
 * CAIRO_MIME_TYPE_JPEG:
1347
 *
1348
 * The Joint Photographic Experts Group (JPEG) image coding standard (ISO/IEC 10918-1).
1349
 *
1350
 * Since: 1.10
1351
 **/
1352
1353
/**
1354
 * CAIRO_MIME_TYPE_PNG:
1355
 *
1356
 * The Portable Network Graphics image file format (ISO/IEC 15948).
1357
 *
1358
 * Since: 1.10
1359
 **/
1360
1361
/**
1362
 * CAIRO_MIME_TYPE_URI:
1363
 *
1364
 * URI for an image file (unofficial MIME type).
1365
 *
1366
 * Since: 1.10
1367
 **/
1368
1369
/**
1370
 * CAIRO_MIME_TYPE_UNIQUE_ID:
1371
 *
1372
 * Unique identifier for a surface (cairo specific MIME type). All surfaces with
1373
 * the same unique identifier will only be embedded once.
1374
 *
1375
 * Since: 1.12
1376
 **/
1377
1378
/**
1379
 * cairo_surface_set_mime_data:
1380
 * @surface: a #cairo_surface_t
1381
 * @mime_type: the MIME type of the image data
1382
 * @data: the image data to attach to the surface
1383
 * @length: the length of the image data
1384
 * @destroy: a #cairo_destroy_func_t which will be called when the
1385
 * surface is destroyed or when new image data is attached using the
1386
 * same mime type.
1387
 * @closure: the data to be passed to the @destroy notifier
1388
 *
1389
 * Attach an image in the format @mime_type to @surface. To remove
1390
 * the data from a surface, call this function with same mime type
1391
 * and %NULL for @data.
1392
 *
1393
 * The attached image (or filename) data can later be used by backends
1394
 * which support it (currently: PDF, PS, SVG and Win32 Printing
1395
 * surfaces) to emit this data instead of making a snapshot of the
1396
 * @surface.  This approach tends to be faster and requires less
1397
 * memory and disk space.
1398
 *
1399
 * The recognized MIME types are the following: %CAIRO_MIME_TYPE_JPEG,
1400
 * %CAIRO_MIME_TYPE_PNG, %CAIRO_MIME_TYPE_JP2, %CAIRO_MIME_TYPE_URI,
1401
 * %CAIRO_MIME_TYPE_UNIQUE_ID, %CAIRO_MIME_TYPE_JBIG2,
1402
 * %CAIRO_MIME_TYPE_JBIG2_GLOBAL, %CAIRO_MIME_TYPE_JBIG2_GLOBAL_ID,
1403
 * %CAIRO_MIME_TYPE_CCITT_FAX, %CAIRO_MIME_TYPE_CCITT_FAX_PARAMS.
1404
 *
1405
 * See corresponding backend surface docs for details about which MIME
1406
 * types it can handle. Caution: the associated MIME data will be
1407
 * discarded if you draw on the surface afterwards. Use this function
1408
 * with care.
1409
 *
1410
 * Even if a backend supports a MIME type, that does not mean cairo
1411
 * will always be able to use the attached MIME data. For example, if
1412
 * the backend does not natively support the compositing operation used
1413
 * to apply the MIME data to the backend. In that case, the MIME data
1414
 * will be ignored. Therefore, to apply an image in all cases, it is best
1415
 * to create an image surface which contains the decoded image data and
1416
 * then attach the MIME data to that. This ensures the image will always
1417
 * be used while still allowing the MIME data to be used whenever
1418
 * possible.
1419
 *
1420
 * Return value: %CAIRO_STATUS_SUCCESS or %CAIRO_STATUS_NO_MEMORY if a
1421
 * slot could not be allocated for the user data.
1422
 *
1423
 * Since: 1.10
1424
 **/
1425
cairo_status_t
1426
cairo_surface_set_mime_data (cairo_surface_t    *surface,
1427
                             const char     *mime_type,
1428
                             const unsigned char  *data,
1429
                             unsigned long     length,
1430
           cairo_destroy_func_t  destroy,
1431
           void     *closure)
1432
1.46k
{
1433
1.46k
    cairo_status_t status;
1434
1.46k
    cairo_mime_data_t *mime_data;
1435
1436
1.46k
    if (CAIRO_REFERENCE_COUNT_IS_INVALID (&surface->ref_count))
1437
0
  return surface->status;
1438
1439
1.46k
    if (! CAIRO_REFERENCE_COUNT_HAS_REFERENCE (&surface->ref_count))
1440
0
  return _cairo_error (CAIRO_STATUS_SURFACE_FINISHED);
1441
1442
1.46k
    if (unlikely (surface->status))
1443
0
  return surface->status;
1444
1.46k
    if (unlikely (surface->finished))
1445
0
  return _cairo_surface_set_error (surface, _cairo_error (CAIRO_STATUS_SURFACE_FINISHED));
1446
1447
1.46k
    status = _cairo_intern_string (&mime_type, -1);
1448
1.46k
    if (unlikely (status))
1449
0
  return _cairo_surface_set_error (surface, status);
1450
1451
1.46k
    if (data != NULL) {
1452
1.46k
  mime_data = _cairo_calloc (sizeof (cairo_mime_data_t));
1453
1.46k
  if (unlikely (mime_data == NULL))
1454
0
      return _cairo_surface_set_error (surface, _cairo_error (CAIRO_STATUS_NO_MEMORY));
1455
1456
1.46k
  CAIRO_REFERENCE_COUNT_INIT (&mime_data->ref_count, 1);
1457
1458
1.46k
  mime_data->data = (unsigned char *) data;
1459
1.46k
  mime_data->length = length;
1460
1.46k
  mime_data->destroy = destroy;
1461
1.46k
  mime_data->closure = closure;
1462
1.46k
    } else
1463
0
  mime_data = NULL;
1464
1465
1.46k
    status = _cairo_user_data_array_set_data (&surface->mime_data,
1466
1.46k
                (cairo_user_data_key_t *) mime_type,
1467
1.46k
                mime_data,
1468
1.46k
                _cairo_mime_data_destroy);
1469
1.46k
    if (unlikely (status)) {
1470
0
  free (mime_data);
1471
1472
0
  return _cairo_surface_set_error (surface, status);
1473
0
    }
1474
1475
1.46k
    surface->is_clear = FALSE;
1476
1477
1.46k
    return CAIRO_STATUS_SUCCESS;
1478
1.46k
}
1479
1480
/**
1481
 * cairo_surface_supports_mime_type:
1482
 * @surface: a #cairo_surface_t
1483
 * @mime_type: the mime type
1484
 *
1485
 * Return whether @surface supports @mime_type.
1486
 *
1487
 * Return value: %TRUE if @surface supports
1488
 *               @mime_type, %FALSE otherwise
1489
 *
1490
 * Since: 1.12
1491
 **/
1492
cairo_bool_t
1493
cairo_surface_supports_mime_type (cairo_surface_t   *surface,
1494
          const char      *mime_type)
1495
0
{
1496
0
    const char **types;
1497
1498
0
    if (unlikely (surface->status))
1499
0
  return FALSE;
1500
0
    if (unlikely (surface->finished)) {
1501
0
  _cairo_surface_set_error (surface, _cairo_error (CAIRO_STATUS_SURFACE_FINISHED));
1502
0
  return FALSE;
1503
0
    }
1504
1505
0
    if (surface->backend->get_supported_mime_types) {
1506
0
  types = surface->backend->get_supported_mime_types (surface);
1507
0
  if (types) {
1508
0
      while (*types) {
1509
0
    if (strcmp (*types, mime_type) == 0)
1510
0
        return TRUE;
1511
0
    types++;
1512
0
      }
1513
0
  }
1514
0
    }
1515
1516
0
    return FALSE;
1517
0
}
1518
1519
static void
1520
_cairo_mime_data_reference (const void *key, void *elt, void *closure)
1521
1.46k
{
1522
1.46k
    cairo_mime_data_t *mime_data = elt;
1523
1524
1.46k
    _cairo_reference_count_inc (&mime_data->ref_count);
1525
1.46k
}
1526
1527
cairo_status_t
1528
_cairo_surface_copy_mime_data (cairo_surface_t *dst,
1529
             cairo_surface_t *src)
1530
3.16k
{
1531
3.16k
    cairo_status_t status;
1532
1533
3.16k
    if (dst->status)
1534
0
  return dst->status;
1535
1536
3.16k
    if (src->status)
1537
0
  return _cairo_surface_set_error (dst, src->status);
1538
1539
    /* first copy the mime-data, discarding any already set on dst */
1540
3.16k
    status = _cairo_user_data_array_copy (&dst->mime_data, &src->mime_data);
1541
3.16k
    if (unlikely (status))
1542
0
  return _cairo_surface_set_error (dst, status);
1543
1544
    /* now increment the reference counters for the copies */
1545
3.16k
    _cairo_user_data_array_foreach (&dst->mime_data,
1546
3.16k
            _cairo_mime_data_reference,
1547
3.16k
            NULL);
1548
1549
3.16k
    dst->is_clear = FALSE;
1550
1551
3.16k
    return CAIRO_STATUS_SUCCESS;
1552
3.16k
}
1553
1554
/**
1555
 * _cairo_surface_set_font_options:
1556
 * @surface: a #cairo_surface_t
1557
 * @options: a #cairo_font_options_t object that contains the
1558
 *   options to use for this surface instead of backend's default
1559
 *   font options.
1560
 *
1561
 * Sets the default font rendering options for the surface.
1562
 * This is useful to correctly propagate default font options when
1563
 * falling back to an image surface in a backend implementation.
1564
 * This affects the options returned in cairo_surface_get_font_options().
1565
 *
1566
 * If @options is %NULL the surface options are reset to those of
1567
 * the backend default.
1568
 **/
1569
void
1570
_cairo_surface_set_font_options (cairo_surface_t       *surface,
1571
         cairo_font_options_t  *options)
1572
606
{
1573
606
    if (surface->status)
1574
0
  return;
1575
1576
606
    assert (surface->snapshot_of == NULL);
1577
1578
606
    if (surface->finished) {
1579
0
  _cairo_surface_set_error (surface, _cairo_error (CAIRO_STATUS_SURFACE_FINISHED));
1580
0
  return;
1581
0
    }
1582
1583
606
    if (options) {
1584
606
  surface->has_font_options = TRUE;
1585
606
  _cairo_font_options_init_copy (&surface->font_options, options);
1586
606
    } else {
1587
0
  surface->has_font_options = FALSE;
1588
0
    }
1589
606
}
1590
1591
/**
1592
 * cairo_surface_get_font_options:
1593
 * @surface: a #cairo_surface_t
1594
 * @options: a #cairo_font_options_t object into which to store
1595
 *   the retrieved options. All existing values are overwritten
1596
 *
1597
 * Retrieves the default font rendering options for the surface.
1598
 * This allows display surfaces to report the correct subpixel order
1599
 * for rendering on them, print surfaces to disable hinting of
1600
 * metrics and so forth. The result can then be used with
1601
 * cairo_scaled_font_create().
1602
 *
1603
 * Since: 1.0
1604
 **/
1605
void
1606
cairo_surface_get_font_options (cairo_surface_t       *surface,
1607
        cairo_font_options_t  *options)
1608
989k
{
1609
989k
    if (cairo_font_options_status (options))
1610
0
  return;
1611
1612
989k
    if (surface->status) {
1613
0
  _cairo_font_options_init_default (options);
1614
0
  return;
1615
0
    }
1616
1617
989k
    if (! surface->has_font_options) {
1618
633
  surface->has_font_options = TRUE;
1619
1620
633
  _cairo_font_options_init_default (&surface->font_options);
1621
1622
633
  if (!surface->finished && surface->backend->get_font_options) {
1623
146
      surface->backend->get_font_options (surface, &surface->font_options);
1624
146
  }
1625
633
    }
1626
1627
989k
    _cairo_font_options_init_copy (options, &surface->font_options);
1628
989k
}
1629
1630
cairo_status_t
1631
_cairo_surface_flush (cairo_surface_t *surface, unsigned flags)
1632
2.55M
{
1633
    /* update the current snapshots *before* the user updates the surface */
1634
2.55M
    _cairo_surface_detach_snapshots (surface);
1635
2.55M
    if (surface->snapshot_of != NULL)
1636
556
  _cairo_surface_detach_snapshot (surface);
1637
2.55M
    _cairo_surface_detach_mime_data (surface);
1638
1639
2.55M
    return __cairo_surface_flush (surface, flags);
1640
2.55M
}
1641
1642
/**
1643
 * cairo_surface_flush:
1644
 * @surface: a #cairo_surface_t
1645
 *
1646
 * Do any pending drawing for the surface and also restore any temporary
1647
 * modifications cairo has made to the surface's state. This function
1648
 * must be called before switching from drawing on the surface with
1649
 * cairo to drawing on it directly with native APIs, or accessing its
1650
 * memory outside of Cairo. If the surface doesn't support direct
1651
 * access, then this function does nothing.
1652
 *
1653
 * Since: 1.0
1654
 **/
1655
void
1656
cairo_surface_flush (cairo_surface_t *surface)
1657
500k
{
1658
500k
    cairo_status_t status;
1659
1660
500k
    if (surface->status)
1661
0
  return;
1662
1663
500k
    if (surface->finished)
1664
0
  return;
1665
1666
500k
    status = _cairo_surface_flush (surface, 0);
1667
500k
    if (unlikely (status))
1668
0
  _cairo_surface_set_error (surface, status);
1669
500k
}
1670
1671
/**
1672
 * cairo_surface_mark_dirty:
1673
 * @surface: a #cairo_surface_t
1674
 *
1675
 * Tells cairo that drawing has been done to surface using means other
1676
 * than cairo, and that cairo should reread any cached areas. Note
1677
 * that you must call cairo_surface_flush() before doing such drawing.
1678
 *
1679
 * Since: 1.0
1680
 **/
1681
void
1682
cairo_surface_mark_dirty (cairo_surface_t *surface)
1683
2.62k
{
1684
2.62k
    cairo_rectangle_int_t extents;
1685
1686
2.62k
    if (unlikely (surface->status))
1687
0
  return;
1688
2.62k
    if (unlikely (surface->finished)) {
1689
0
  _cairo_surface_set_error (surface, _cairo_error (CAIRO_STATUS_SURFACE_FINISHED));
1690
0
  return;
1691
0
    }
1692
1693
2.62k
    _cairo_surface_get_extents (surface, &extents);
1694
2.62k
    cairo_surface_mark_dirty_rectangle (surface,
1695
2.62k
          extents.x, extents.y,
1696
2.62k
          extents.width, extents.height);
1697
2.62k
}
1698
1699
/**
1700
 * cairo_surface_mark_dirty_rectangle:
1701
 * @surface: a #cairo_surface_t
1702
 * @x: X coordinate of dirty rectangle
1703
 * @y: Y coordinate of dirty rectangle
1704
 * @width: width of dirty rectangle
1705
 * @height: height of dirty rectangle
1706
 *
1707
 * Like cairo_surface_mark_dirty(), but drawing has been done only to
1708
 * the specified rectangle, so that cairo can retain cached contents
1709
 * for other parts of the surface.
1710
 *
1711
 * Any cached clip set on the surface will be reset by this function,
1712
 * to make sure that future cairo calls have the clip set that they
1713
 * expect.
1714
 *
1715
 * Since: 1.0
1716
 **/
1717
void
1718
cairo_surface_mark_dirty_rectangle (cairo_surface_t *surface,
1719
            int              x,
1720
            int              y,
1721
            int              width,
1722
            int              height)
1723
2.62k
{
1724
2.62k
    cairo_status_t status;
1725
1726
2.62k
    if (unlikely (surface->status))
1727
0
  return;
1728
1729
2.62k
    assert (surface->snapshot_of == NULL);
1730
1731
2.62k
    if (unlikely (surface->finished)) {
1732
0
  _cairo_surface_set_error (surface, _cairo_error (CAIRO_STATUS_SURFACE_FINISHED));
1733
0
  return;
1734
0
    }
1735
1736
    /* The application *should* have called cairo_surface_flush() before
1737
     * modifying the surface independently of cairo (and thus having to
1738
     * call mark_dirty()). */
1739
2.62k
    assert (! _cairo_surface_has_snapshots (surface));
1740
2.62k
    assert (! _cairo_surface_has_mime_data (surface));
1741
1742
2.62k
    surface->is_clear = FALSE;
1743
2.62k
    surface->serial++;
1744
1745
2.62k
    if (surface->damage) {
1746
0
  cairo_box_t box;
1747
1748
0
  box.p1.x = x;
1749
0
  box.p1.y = y;
1750
0
  box.p2.x = x + width;
1751
0
  box.p2.y = y + height;
1752
1753
0
  surface->damage = _cairo_damage_add_box (surface->damage, &box);
1754
0
    }
1755
1756
2.62k
    if (surface->backend->mark_dirty_rectangle != NULL) {
1757
  /* XXX: FRAGILE: We're ignoring the scaling component of
1758
   * device_transform here. I don't know what the right thing to
1759
   * do would actually be if there were some scaling here, but
1760
   * we avoid this since device_transfom scaling is not exported
1761
   * publicly and mark_dirty is not used internally. */
1762
0
  status = surface->backend->mark_dirty_rectangle (surface,
1763
0
                                                         x + surface->device_transform.x0,
1764
0
                                                         y + surface->device_transform.y0,
1765
0
               width, height);
1766
1767
0
  if (unlikely (status))
1768
0
      _cairo_surface_set_error (surface, status);
1769
0
    }
1770
2.62k
}
1771
1772
/**
1773
 * cairo_surface_set_device_scale:
1774
 * @surface: a #cairo_surface_t
1775
 * @x_scale: a scale factor in the X direction
1776
 * @y_scale: a scale factor in the Y direction
1777
 *
1778
 * Sets a scale that is multiplied to the device coordinates determined
1779
 * by the CTM when drawing to @surface. One common use for this is to
1780
 * render to very high resolution display devices at a scale factor, so
1781
 * that code that assumes 1 pixel will be a certain size will still work.
1782
 * Setting a transformation via cairo_scale() isn't
1783
 * sufficient to do this, since functions like
1784
 * cairo_device_to_user() will expose the hidden scale.
1785
 *
1786
 * Note that the scale affects drawing to the surface as well as
1787
 * using the surface in a source pattern.
1788
 *
1789
 * Since: 1.14
1790
 **/
1791
void
1792
cairo_surface_set_device_scale (cairo_surface_t *surface,
1793
        double     x_scale,
1794
        double     y_scale)
1795
606
{
1796
606
    cairo_status_t status;
1797
1798
606
    if (unlikely (surface->status))
1799
0
  return;
1800
1801
606
    assert (surface->snapshot_of == NULL);
1802
1803
606
    if (unlikely (surface->finished)) {
1804
0
  _cairo_surface_set_error (surface, _cairo_error (CAIRO_STATUS_SURFACE_FINISHED));
1805
0
  return;
1806
0
    }
1807
1808
606
    status = _cairo_surface_begin_modification (surface);
1809
606
    if (unlikely (status)) {
1810
0
  _cairo_surface_set_error (surface, status);
1811
0
  return;
1812
0
    }
1813
1814
606
    surface->device_transform.xx = x_scale;
1815
606
    surface->device_transform.yy = y_scale;
1816
606
    surface->device_transform.xy = 0.0;
1817
606
    surface->device_transform.yx = 0.0;
1818
1819
606
    surface->device_transform_inverse = surface->device_transform;
1820
606
    status = cairo_matrix_invert (&surface->device_transform_inverse);
1821
    /* should always be invertible unless given pathological input */
1822
606
    assert (status == CAIRO_STATUS_SUCCESS);
1823
1824
606
    _cairo_observers_notify (&surface->device_transform_observers, surface);
1825
606
}
1826
1827
/**
1828
 * cairo_surface_get_device_scale:
1829
 * @surface: a #cairo_surface_t
1830
 * @x_scale: the scale in the X direction, in device units
1831
 * @y_scale: the scale in the Y direction, in device units
1832
 *
1833
 * This function returns the previous device scale set by
1834
 * cairo_surface_set_device_scale().
1835
 *
1836
 * Since: 1.14
1837
 **/
1838
void
1839
cairo_surface_get_device_scale (cairo_surface_t *surface,
1840
        double          *x_scale,
1841
        double          *y_scale)
1842
0
{
1843
0
    if (x_scale)
1844
0
  *x_scale = surface->device_transform.xx;
1845
0
    if (y_scale)
1846
0
  *y_scale = surface->device_transform.yy;
1847
0
}
1848
1849
/**
1850
 * cairo_surface_set_device_offset:
1851
 * @surface: a #cairo_surface_t
1852
 * @x_offset: the offset in the X direction, in device units
1853
 * @y_offset: the offset in the Y direction, in device units
1854
 *
1855
 * Sets an offset that is added to the device coordinates determined
1856
 * by the CTM when drawing to @surface. One use case for this function
1857
 * is when we want to create a #cairo_surface_t that redirects drawing
1858
 * for a portion of an onscreen surface to an offscreen surface in a
1859
 * way that is completely invisible to the user of the cairo
1860
 * API. Setting a transformation via cairo_translate() isn't
1861
 * sufficient to do this, since functions like
1862
 * cairo_device_to_user() will expose the hidden offset.
1863
 *
1864
 * Note that the offset affects drawing to the surface as well as
1865
 * using the surface in a source pattern.
1866
 *
1867
 * Since: 1.0
1868
 **/
1869
void
1870
cairo_surface_set_device_offset (cairo_surface_t *surface,
1871
         double           x_offset,
1872
         double           y_offset)
1873
14.7k
{
1874
14.7k
    cairo_status_t status;
1875
1876
14.7k
    if (unlikely (surface->status))
1877
0
  return;
1878
1879
14.7k
    assert (surface->snapshot_of == NULL);
1880
1881
14.7k
    if (unlikely (surface->finished)) {
1882
0
  _cairo_surface_set_error (surface, _cairo_error (CAIRO_STATUS_SURFACE_FINISHED));
1883
0
  return;
1884
0
    }
1885
1886
14.7k
    status = _cairo_surface_begin_modification (surface);
1887
14.7k
    if (unlikely (status)) {
1888
0
  _cairo_surface_set_error (surface, status);
1889
0
  return;
1890
0
    }
1891
1892
14.7k
    surface->device_transform.x0 = x_offset;
1893
14.7k
    surface->device_transform.y0 = y_offset;
1894
1895
14.7k
    surface->device_transform_inverse = surface->device_transform;
1896
14.7k
    status = cairo_matrix_invert (&surface->device_transform_inverse);
1897
    /* should always be invertible unless given pathological input */
1898
14.7k
    assert (status == CAIRO_STATUS_SUCCESS);
1899
1900
14.7k
    _cairo_observers_notify (&surface->device_transform_observers, surface);
1901
14.7k
}
1902
1903
/**
1904
 * cairo_surface_get_device_offset:
1905
 * @surface: a #cairo_surface_t
1906
 * @x_offset: the offset in the X direction, in device units
1907
 * @y_offset: the offset in the Y direction, in device units
1908
 *
1909
 * This function returns the previous device offset set by
1910
 * cairo_surface_set_device_offset().
1911
 *
1912
 * Since: 1.2
1913
 **/
1914
void
1915
cairo_surface_get_device_offset (cairo_surface_t *surface,
1916
         double          *x_offset,
1917
         double          *y_offset)
1918
67
{
1919
67
    if (x_offset)
1920
67
  *x_offset = surface->device_transform.x0;
1921
67
    if (y_offset)
1922
67
  *y_offset = surface->device_transform.y0;
1923
67
}
1924
1925
/**
1926
 * cairo_surface_set_fallback_resolution:
1927
 * @surface: a #cairo_surface_t
1928
 * @x_pixels_per_inch: horizontal setting for pixels per inch
1929
 * @y_pixels_per_inch: vertical setting for pixels per inch
1930
 *
1931
 * Set the horizontal and vertical resolution for image fallbacks.
1932
 *
1933
 * When certain operations aren't supported natively by a backend,
1934
 * cairo will fallback by rendering operations to an image and then
1935
 * overlaying that image onto the output. For backends that are
1936
 * natively vector-oriented, this function can be used to set the
1937
 * resolution used for these image fallbacks, (larger values will
1938
 * result in more detailed images, but also larger file sizes).
1939
 *
1940
 * Some examples of natively vector-oriented backends are the ps, pdf,
1941
 * and svg backends.
1942
 *
1943
 * For backends that are natively raster-oriented, image fallbacks are
1944
 * still possible, but they are always performed at the native
1945
 * device resolution. So this function has no effect on those
1946
 * backends.
1947
 *
1948
 * Note: The fallback resolution only takes effect at the time of
1949
 * completing a page (with cairo_show_page() or cairo_copy_page()) so
1950
 * there is currently no way to have more than one fallback resolution
1951
 * in effect on a single page.
1952
 *
1953
 * The default fallback resolution is 300 pixels per inch in both
1954
 * dimensions.
1955
 *
1956
 * Since: 1.2
1957
 **/
1958
void
1959
cairo_surface_set_fallback_resolution (cairo_surface_t  *surface,
1960
               double    x_pixels_per_inch,
1961
               double    y_pixels_per_inch)
1962
608
{
1963
608
    cairo_status_t status;
1964
1965
608
    if (unlikely (surface->status))
1966
0
  return;
1967
1968
608
    assert (surface->snapshot_of == NULL);
1969
1970
608
    if (unlikely (surface->finished)) {
1971
0
  _cairo_surface_set_error (surface, _cairo_error (CAIRO_STATUS_SURFACE_FINISHED));
1972
0
  return;
1973
0
    }
1974
1975
608
    if (x_pixels_per_inch <= 0 || y_pixels_per_inch <= 0) {
1976
  /* XXX Could delay raising the error until we fallback, but throwing
1977
   * the error here means that we can catch the real culprit.
1978
   */
1979
0
  _cairo_surface_set_error (surface, CAIRO_STATUS_INVALID_MATRIX);
1980
0
  return;
1981
0
    }
1982
1983
608
    status = _cairo_surface_begin_modification (surface);
1984
608
    if (unlikely (status)) {
1985
0
  _cairo_surface_set_error (surface, status);
1986
0
  return;
1987
0
    }
1988
1989
608
    surface->x_fallback_resolution = x_pixels_per_inch;
1990
608
    surface->y_fallback_resolution = y_pixels_per_inch;
1991
608
}
1992
1993
/**
1994
 * cairo_surface_get_fallback_resolution:
1995
 * @surface: a #cairo_surface_t
1996
 * @x_pixels_per_inch: horizontal pixels per inch
1997
 * @y_pixels_per_inch: vertical pixels per inch
1998
 *
1999
 * This function returns the previous fallback resolution set by
2000
 * cairo_surface_set_fallback_resolution(), or default fallback
2001
 * resolution if never set.
2002
 *
2003
 * Since: 1.8
2004
 **/
2005
void
2006
cairo_surface_get_fallback_resolution (cairo_surface_t  *surface,
2007
               double   *x_pixels_per_inch,
2008
               double   *y_pixels_per_inch)
2009
0
{
2010
0
    if (x_pixels_per_inch)
2011
0
  *x_pixels_per_inch = surface->x_fallback_resolution;
2012
0
    if (y_pixels_per_inch)
2013
0
  *y_pixels_per_inch = surface->y_fallback_resolution;
2014
0
}
2015
2016
cairo_bool_t
2017
_cairo_surface_has_device_transform (cairo_surface_t *surface)
2018
504k
{
2019
504k
    return ! _cairo_matrix_is_identity (&surface->device_transform);
2020
504k
}
2021
2022
/**
2023
 * _cairo_surface_acquire_source_image:
2024
 * @surface: a #cairo_surface_t
2025
 * @image_out: location to store a pointer to an image surface that
2026
 *    has identical contents to @surface. This surface could be @surface
2027
 *    itself, a surface held internal to @surface, or it could be a new
2028
 *    surface with a copy of the relevant portion of @surface.
2029
 * @image_extra: location to store image specific backend data
2030
 *
2031
 * Gets an image surface to use when drawing as a fallback when drawing with
2032
 * @surface as a source. _cairo_surface_release_source_image() must be called
2033
 * when finished.
2034
 *
2035
 * Return value: %CAIRO_STATUS_SUCCESS if an image was stored in @image_out.
2036
 * %CAIRO_INT_STATUS_UNSUPPORTED if an image cannot be retrieved for the specified
2037
 * surface. Or %CAIRO_STATUS_NO_MEMORY.
2038
 **/
2039
cairo_status_t
2040
_cairo_surface_acquire_source_image (cairo_surface_t         *surface,
2041
             cairo_image_surface_t  **image_out,
2042
             void                   **image_extra)
2043
8.76k
{
2044
8.76k
    cairo_status_t status;
2045
2046
8.76k
    if (unlikely (surface->status))
2047
0
  return surface->status;
2048
2049
8.76k
    assert (!surface->finished);
2050
2051
8.76k
    if (surface->backend->acquire_source_image == NULL)
2052
0
  return CAIRO_INT_STATUS_UNSUPPORTED;
2053
2054
8.76k
    status = surface->backend->acquire_source_image (surface,
2055
8.76k
                 image_out, image_extra);
2056
8.76k
    if (unlikely (status))
2057
0
  return _cairo_surface_set_error (surface, status);
2058
2059
8.76k
    _cairo_debug_check_image_surface_is_defined (&(*image_out)->base);
2060
2061
8.76k
    return CAIRO_STATUS_SUCCESS;
2062
8.76k
}
2063
2064
cairo_status_t
2065
_cairo_surface_default_acquire_source_image (void                    *_surface,
2066
               cairo_image_surface_t  **image_out,
2067
               void                   **image_extra)
2068
0
{
2069
0
    cairo_surface_t *surface = _surface;
2070
0
    cairo_rectangle_int_t extents;
2071
2072
0
    if (unlikely (! surface->backend->get_extents (surface, &extents)))
2073
0
  return _cairo_error (CAIRO_STATUS_INVALID_SIZE);
2074
2075
0
    *image_out = _cairo_surface_map_to_image (surface, &extents);
2076
0
    *image_extra = NULL;
2077
0
    return (*image_out)->base.status;
2078
0
}
2079
2080
/**
2081
 * _cairo_surface_release_source_image:
2082
 * @surface: a #cairo_surface_t
2083
 * @image_extra: same as return from the matching _cairo_surface_acquire_source_image()
2084
 *
2085
 * Releases any resources obtained with _cairo_surface_acquire_source_image()
2086
 **/
2087
void
2088
_cairo_surface_release_source_image (cairo_surface_t        *surface,
2089
             cairo_image_surface_t  *image,
2090
             void                   *image_extra)
2091
8.76k
{
2092
8.76k
    assert (!surface->finished);
2093
2094
8.76k
    if (surface->backend->release_source_image)
2095
8.76k
  surface->backend->release_source_image (surface, image, image_extra);
2096
8.76k
}
2097
2098
void
2099
_cairo_surface_default_release_source_image (void                   *surface,
2100
               cairo_image_surface_t  *image,
2101
               void                   *image_extra)
2102
0
{
2103
0
    cairo_status_t ignored;
2104
2105
0
    ignored = _cairo_surface_unmap_image (surface, image);
2106
0
    (void)ignored;
2107
0
}
2108
2109
2110
cairo_surface_t *
2111
_cairo_surface_get_source (cairo_surface_t *surface,
2112
         cairo_rectangle_int_t *extents)
2113
5.79k
{
2114
5.79k
    assert (surface->backend->source);
2115
5.79k
    return surface->backend->source (surface, extents);
2116
5.79k
}
2117
2118
cairo_surface_t *
2119
_cairo_surface_default_source (void *surface,
2120
             cairo_rectangle_int_t *extents)
2121
602
{
2122
602
    if (extents)
2123
8
  _cairo_surface_get_extents(surface, extents);
2124
602
    return surface;
2125
602
}
2126
2127
static cairo_status_t
2128
_pattern_has_error (const cairo_pattern_t *pattern)
2129
2.00M
{
2130
2.00M
    const cairo_surface_pattern_t *spattern;
2131
2132
2.00M
    if (unlikely (pattern->status))
2133
0
  return pattern->status;
2134
2135
2.00M
    if (pattern->type != CAIRO_PATTERN_TYPE_SURFACE)
2136
1.99M
  return CAIRO_STATUS_SUCCESS;
2137
2138
11.7k
    spattern = (const cairo_surface_pattern_t *) pattern;
2139
11.7k
    if (unlikely (spattern->surface->status))
2140
0
  return spattern->surface->status;
2141
2142
11.7k
    if (unlikely (spattern->surface->finished))
2143
0
  return _cairo_error (CAIRO_STATUS_SURFACE_FINISHED);
2144
2145
11.7k
    return CAIRO_STATUS_SUCCESS;
2146
11.7k
}
2147
2148
static cairo_bool_t
2149
nothing_to_do (cairo_surface_t *surface,
2150
         cairo_operator_t op,
2151
         const cairo_pattern_t *source)
2152
1.99M
{
2153
1.99M
    if (_cairo_pattern_is_clear (source)) {
2154
696
  if (op == CAIRO_OPERATOR_OVER || op == CAIRO_OPERATOR_ADD)
2155
38
      return TRUE;
2156
2157
658
  if (op == CAIRO_OPERATOR_SOURCE)
2158
40
      op = CAIRO_OPERATOR_CLEAR;
2159
658
    }
2160
2161
1.99M
    if (op == CAIRO_OPERATOR_CLEAR && surface->is_clear)
2162
646
  return TRUE;
2163
2164
1.99M
    if (op == CAIRO_OPERATOR_ATOP && (surface->content & CAIRO_CONTENT_COLOR) ==0)
2165
0
  return TRUE;
2166
2167
1.99M
    return FALSE;
2168
1.99M
}
2169
2170
cairo_status_t
2171
_cairo_surface_paint (cairo_surface_t   *surface,
2172
          cairo_operator_t     op,
2173
          const cairo_pattern_t *source,
2174
          const cairo_clip_t  *clip)
2175
1.97k
{
2176
1.97k
    cairo_int_status_t status;
2177
1.97k
    cairo_bool_t is_clear;
2178
2179
1.97k
    TRACE ((stderr, "%s\n", __FUNCTION__));
2180
1.97k
    if (unlikely (surface->status))
2181
0
  return surface->status;
2182
1.97k
    if (unlikely (surface->finished))
2183
0
  return _cairo_surface_set_error (surface, _cairo_error (CAIRO_STATUS_SURFACE_FINISHED));
2184
2185
1.97k
    if (_cairo_clip_is_all_clipped (clip))
2186
0
  return CAIRO_STATUS_SUCCESS;
2187
2188
1.97k
    status = _pattern_has_error (source);
2189
1.97k
    if (unlikely (status))
2190
0
  return status;
2191
2192
1.97k
    if (nothing_to_do (surface, op, source))
2193
650
  return CAIRO_STATUS_SUCCESS;
2194
2195
1.32k
    status = _cairo_surface_begin_modification (surface);
2196
1.32k
    if (unlikely (status))
2197
0
  return status;
2198
2199
1.32k
    if (source->is_foreground_marker && surface->foreground_source) {
2200
0
  source = surface->foreground_source;
2201
0
  surface->foreground_used = TRUE;
2202
0
    }
2203
2204
1.32k
    status = surface->backend->paint (surface, op, source, clip);
2205
1.32k
    is_clear = op == CAIRO_OPERATOR_CLEAR && clip == NULL;
2206
1.32k
    if (status != CAIRO_INT_STATUS_NOTHING_TO_DO || is_clear) {
2207
1.32k
  surface->is_clear = is_clear;
2208
1.32k
  surface->serial++;
2209
1.32k
    }
2210
2211
1.32k
    return _cairo_surface_set_error (surface, status);
2212
1.32k
}
2213
2214
cairo_status_t
2215
_cairo_surface_mask (cairo_surface_t    *surface,
2216
         cairo_operator_t    op,
2217
         const cairo_pattern_t  *source,
2218
         const cairo_pattern_t  *mask,
2219
         const cairo_clip_t   *clip)
2220
4.20k
{
2221
4.20k
    cairo_int_status_t status;
2222
2223
4.20k
    TRACE ((stderr, "%s\n", __FUNCTION__));
2224
4.20k
    if (unlikely (surface->status))
2225
0
  return surface->status;
2226
4.20k
    if (unlikely (surface->finished))
2227
0
  return _cairo_surface_set_error (surface, _cairo_error (CAIRO_STATUS_SURFACE_FINISHED));
2228
2229
4.20k
    if (_cairo_clip_is_all_clipped (clip))
2230
0
  return CAIRO_STATUS_SUCCESS;
2231
2232
    /* If the mask is blank, this is just an expensive no-op */
2233
4.20k
    if (_cairo_pattern_is_clear (mask) &&
2234
4.20k
  _cairo_operator_bounded_by_mask (op))
2235
0
    {
2236
0
  return CAIRO_STATUS_SUCCESS;
2237
0
    }
2238
2239
4.20k
    status = _pattern_has_error (source);
2240
4.20k
    if (unlikely (status))
2241
0
  return status;
2242
2243
4.20k
    status = _pattern_has_error (mask);
2244
4.20k
    if (unlikely (status))
2245
0
  return status;
2246
2247
4.20k
    if (nothing_to_do (surface, op, source))
2248
2
  return CAIRO_STATUS_SUCCESS;
2249
2250
4.20k
    status = _cairo_surface_begin_modification (surface);
2251
4.20k
    if (unlikely (status))
2252
0
  return status;
2253
2254
4.20k
    if (source->is_foreground_marker && surface->foreground_source) {
2255
0
  source = surface->foreground_source;
2256
0
  surface->foreground_used = TRUE;
2257
0
    }
2258
2259
4.20k
    status = surface->backend->mask (surface, op, source, mask, clip);
2260
4.20k
    if (status != CAIRO_INT_STATUS_NOTHING_TO_DO) {
2261
4.20k
  surface->is_clear = FALSE;
2262
4.20k
  surface->serial++;
2263
4.20k
    }
2264
2265
4.20k
    return _cairo_surface_set_error (surface, status);
2266
4.20k
}
2267
2268
cairo_status_t
2269
_cairo_surface_fill_stroke (cairo_surface_t     *surface,
2270
          cairo_operator_t       fill_op,
2271
          const cairo_pattern_t   *fill_source,
2272
          cairo_fill_rule_t      fill_rule,
2273
          double         fill_tolerance,
2274
          cairo_antialias_t      fill_antialias,
2275
          cairo_path_fixed_t      *path,
2276
          cairo_operator_t       stroke_op,
2277
          const cairo_pattern_t   *stroke_source,
2278
          const cairo_stroke_style_t    *stroke_style,
2279
          const cairo_matrix_t      *stroke_ctm,
2280
          const cairo_matrix_t      *stroke_ctm_inverse,
2281
          double         stroke_tolerance,
2282
          cairo_antialias_t      stroke_antialias,
2283
          const cairo_clip_t      *clip)
2284
1.79k
{
2285
1.79k
    cairo_int_status_t status;
2286
2287
1.79k
    TRACE ((stderr, "%s\n", __FUNCTION__));
2288
1.79k
    if (unlikely (surface->status))
2289
0
  return surface->status;
2290
1.79k
    if (unlikely (surface->finished))
2291
0
  return _cairo_surface_set_error (surface, _cairo_error (CAIRO_STATUS_SURFACE_FINISHED));
2292
2293
1.79k
    if (_cairo_clip_is_all_clipped (clip))
2294
0
  return CAIRO_STATUS_SUCCESS;
2295
2296
1.79k
    if (surface->is_clear &&
2297
1.79k
  fill_op == CAIRO_OPERATOR_CLEAR &&
2298
1.79k
  stroke_op == CAIRO_OPERATOR_CLEAR)
2299
0
    {
2300
0
  return CAIRO_STATUS_SUCCESS;
2301
0
    }
2302
2303
1.79k
    status = _pattern_has_error (fill_source);
2304
1.79k
    if (unlikely (status))
2305
0
  return status;
2306
2307
1.79k
    status = _pattern_has_error (stroke_source);
2308
1.79k
    if (unlikely (status))
2309
0
  return status;
2310
2311
1.79k
    status = _cairo_surface_begin_modification (surface);
2312
1.79k
    if (unlikely (status))
2313
0
  return status;
2314
2315
1.79k
    if (fill_source->is_foreground_marker && surface->foreground_source) {
2316
0
  fill_source = surface->foreground_source;
2317
0
  surface->foreground_used = TRUE;
2318
0
    }
2319
2320
1.79k
    if (stroke_source->is_foreground_marker && surface->foreground_source) {
2321
0
  stroke_source = surface->foreground_source;
2322
0
  surface->foreground_used = TRUE;
2323
0
    }
2324
2325
1.79k
    if (surface->backend->fill_stroke) {
2326
1.79k
  cairo_matrix_t dev_ctm = *stroke_ctm;
2327
1.79k
  cairo_matrix_t dev_ctm_inverse = *stroke_ctm_inverse;
2328
2329
1.79k
  status = surface->backend->fill_stroke (surface,
2330
1.79k
            fill_op, fill_source, fill_rule,
2331
1.79k
            fill_tolerance, fill_antialias,
2332
1.79k
            path,
2333
1.79k
            stroke_op, stroke_source,
2334
1.79k
            stroke_style,
2335
1.79k
            &dev_ctm, &dev_ctm_inverse,
2336
1.79k
            stroke_tolerance, stroke_antialias,
2337
1.79k
            clip);
2338
2339
1.79k
  if (status != CAIRO_INT_STATUS_UNSUPPORTED)
2340
1.79k
      goto FINISH;
2341
1.79k
    }
2342
2343
0
    status = _cairo_surface_fill (surface, fill_op, fill_source, path,
2344
0
          fill_rule, fill_tolerance, fill_antialias,
2345
0
          clip);
2346
0
    if (unlikely (status))
2347
0
  goto FINISH;
2348
2349
0
    status = _cairo_surface_stroke (surface, stroke_op, stroke_source, path,
2350
0
            stroke_style, stroke_ctm, stroke_ctm_inverse,
2351
0
            stroke_tolerance, stroke_antialias,
2352
0
            clip);
2353
0
    if (unlikely (status))
2354
0
  goto FINISH;
2355
2356
1.79k
  FINISH:
2357
1.79k
    if (status != CAIRO_INT_STATUS_NOTHING_TO_DO) {
2358
1.79k
  surface->is_clear = FALSE;
2359
1.79k
  surface->serial++;
2360
1.79k
    }
2361
2362
1.79k
    return _cairo_surface_set_error (surface, status);
2363
0
}
2364
2365
cairo_status_t
2366
_cairo_surface_stroke (cairo_surface_t      *surface,
2367
           cairo_operator_t      op,
2368
           const cairo_pattern_t    *source,
2369
           const cairo_path_fixed_t   *path,
2370
           const cairo_stroke_style_t *stroke_style,
2371
           const cairo_matrix_t   *ctm,
2372
           const cairo_matrix_t   *ctm_inverse,
2373
           double        tolerance,
2374
           cairo_antialias_t     antialias,
2375
           const cairo_clip_t   *clip)
2376
27.1k
{
2377
27.1k
    cairo_int_status_t status;
2378
2379
27.1k
    TRACE ((stderr, "%s\n", __FUNCTION__));
2380
27.1k
    if (unlikely (surface->status))
2381
0
  return surface->status;
2382
27.1k
    if (unlikely (surface->finished))
2383
0
  return _cairo_surface_set_error (surface, _cairo_error (CAIRO_STATUS_SURFACE_FINISHED));
2384
2385
27.1k
    if (_cairo_clip_is_all_clipped (clip))
2386
0
  return CAIRO_STATUS_SUCCESS;
2387
2388
27.1k
    status = _pattern_has_error (source);
2389
27.1k
    if (unlikely (status))
2390
0
  return status;
2391
2392
27.1k
    if (nothing_to_do (surface, op, source))
2393
0
  return CAIRO_STATUS_SUCCESS;
2394
2395
27.1k
    status = _cairo_surface_begin_modification (surface);
2396
27.1k
    if (unlikely (status))
2397
0
  return status;
2398
2399
27.1k
    if (source->is_foreground_marker && surface->foreground_source) {
2400
0
  source = surface->foreground_source;
2401
0
  surface->foreground_used = TRUE;
2402
0
    }
2403
2404
27.1k
    status = surface->backend->stroke (surface, op, source,
2405
27.1k
               path, stroke_style,
2406
27.1k
               ctm, ctm_inverse,
2407
27.1k
               tolerance, antialias,
2408
27.1k
               clip);
2409
27.1k
    if (status != CAIRO_INT_STATUS_NOTHING_TO_DO) {
2410
27.1k
  surface->is_clear = FALSE;
2411
27.1k
  surface->serial++;
2412
27.1k
    }
2413
2414
27.1k
    return _cairo_surface_set_error (surface, status);
2415
27.1k
}
2416
2417
cairo_status_t
2418
_cairo_surface_fill (cairo_surface_t    *surface,
2419
         cairo_operator_t    op,
2420
         const cairo_pattern_t   *source,
2421
         const cairo_path_fixed_t *path,
2422
         cairo_fill_rule_t     fill_rule,
2423
         double      tolerance,
2424
         cairo_antialias_t     antialias,
2425
         const cairo_clip_t   *clip)
2426
20.3k
{
2427
20.3k
    cairo_int_status_t status;
2428
2429
20.3k
    TRACE ((stderr, "%s\n", __FUNCTION__));
2430
20.3k
    if (unlikely (surface->status))
2431
0
  return surface->status;
2432
20.3k
    if (unlikely (surface->finished))
2433
0
  return _cairo_surface_set_error (surface, _cairo_error (CAIRO_STATUS_SURFACE_FINISHED));
2434
2435
20.3k
    if (_cairo_clip_is_all_clipped (clip))
2436
0
  return CAIRO_STATUS_SUCCESS;
2437
2438
20.3k
    status = _pattern_has_error (source);
2439
20.3k
    if (unlikely (status))
2440
0
  return status;
2441
2442
20.3k
    if (nothing_to_do (surface, op, source))
2443
32
  return CAIRO_STATUS_SUCCESS;
2444
2445
20.3k
    status = _cairo_surface_begin_modification (surface);
2446
20.3k
    if (unlikely (status))
2447
0
  return status;
2448
2449
20.3k
    if (source->is_foreground_marker && surface->foreground_source) {
2450
0
  source = surface->foreground_source;
2451
0
  surface->foreground_used = TRUE;
2452
0
    }
2453
2454
20.3k
    status = surface->backend->fill (surface, op, source,
2455
20.3k
             path, fill_rule,
2456
20.3k
             tolerance, antialias,
2457
20.3k
             clip);
2458
20.3k
    if (status != CAIRO_INT_STATUS_NOTHING_TO_DO) {
2459
20.3k
  surface->is_clear = FALSE;
2460
20.3k
  surface->serial++;
2461
20.3k
    }
2462
2463
20.3k
    return _cairo_surface_set_error (surface, status);
2464
20.3k
}
2465
2466
/**
2467
 * cairo_surface_copy_page:
2468
 * @surface: a #cairo_surface_t
2469
 *
2470
 * Emits the current page for backends that support multiple pages,
2471
 * but doesn't clear it, so that the contents of the current page will
2472
 * be retained for the next page.  Use cairo_surface_show_page() if you
2473
 * want to get an empty page after the emission.
2474
 *
2475
 * There is a convenience function for this that takes a #cairo_t,
2476
 * namely cairo_copy_page().
2477
 *
2478
 * Since: 1.6
2479
 **/
2480
void
2481
cairo_surface_copy_page (cairo_surface_t *surface)
2482
0
{
2483
0
    if (unlikely (surface->status))
2484
0
  return;
2485
2486
0
    assert (surface->snapshot_of == NULL);
2487
2488
0
    if (unlikely (surface->finished)) {
2489
0
  _cairo_surface_set_error (surface, CAIRO_STATUS_SURFACE_FINISHED);
2490
0
  return;
2491
0
    }
2492
2493
    /* It's fine if some backends don't implement copy_page */
2494
0
    if (surface->backend->copy_page == NULL)
2495
0
  return;
2496
2497
0
    _cairo_surface_set_error (surface, surface->backend->copy_page (surface));
2498
0
}
2499
2500
/**
2501
 * cairo_surface_show_page:
2502
 * @surface: a #cairo_Surface_t
2503
 *
2504
 * Emits and clears the current page for backends that support multiple
2505
 * pages.  Use cairo_surface_copy_page() if you don't want to clear the page.
2506
 *
2507
 * There is a convenience function for this that takes a #cairo_t,
2508
 * namely cairo_show_page().
2509
 *
2510
 * Since: 1.6
2511
 **/
2512
void
2513
cairo_surface_show_page (cairo_surface_t *surface)
2514
3.64k
{
2515
3.64k
    cairo_status_t status;
2516
2517
3.64k
    if (unlikely (surface->status))
2518
0
  return;
2519
2520
3.64k
    if (unlikely (surface->finished)) {
2521
0
  _cairo_surface_set_error (surface, CAIRO_STATUS_SURFACE_FINISHED);
2522
0
  return;
2523
0
    }
2524
2525
3.64k
    status = _cairo_surface_begin_modification (surface);
2526
3.64k
    if (unlikely (status)) {
2527
0
  _cairo_surface_set_error (surface, status);
2528
0
  return;
2529
0
    }
2530
2531
    /* It's fine if some backends don't implement show_page */
2532
3.64k
    if (surface->backend->show_page == NULL)
2533
0
  return;
2534
2535
3.64k
    _cairo_surface_set_error (surface, surface->backend->show_page (surface));
2536
3.64k
}
2537
2538
/**
2539
 * _cairo_surface_get_extents:
2540
 * @surface: the #cairo_surface_t to fetch extents for
2541
 *
2542
 * This function returns a bounding box for the surface.  The surface
2543
 * bounds are defined as a region beyond which no rendering will
2544
 * possibly be recorded, in other words, it is the maximum extent of
2545
 * potentially usable coordinates.
2546
 *
2547
 * For vector surfaces, (PDF, PS, SVG and recording-surfaces), the surface
2548
 * might be conceived as unbounded, but we force the user to provide a
2549
 * maximum size at the time of surface_create. So get_extents uses
2550
 * that size.
2551
 *
2552
 * Note: The coordinates returned are in "backend" space rather than
2553
 * "surface" space. That is, they are relative to the true (0,0)
2554
 * origin rather than the device_transform origin. This might seem a
2555
 * bit inconsistent with other #cairo_surface_t interfaces, but all
2556
 * current callers are within the surface layer where backend space is
2557
 * desired.
2558
 *
2559
 * This behavior would have to be changed is we ever exported a public
2560
 * variant of this function.
2561
 **/
2562
cairo_bool_t
2563
_cairo_surface_get_extents (cairo_surface_t         *surface,
2564
          cairo_rectangle_int_t   *extents)
2565
3.53M
{
2566
3.53M
    cairo_bool_t bounded;
2567
2568
3.53M
    if (unlikely (surface->status))
2569
0
  goto zero_extents;
2570
3.53M
    if (unlikely (surface->finished)) {
2571
0
  _cairo_surface_set_error(surface, CAIRO_STATUS_SURFACE_FINISHED);
2572
0
  goto zero_extents;
2573
0
    }
2574
2575
3.53M
    bounded = FALSE;
2576
3.53M
    if (surface->backend->get_extents != NULL)
2577
3.53M
  bounded = surface->backend->get_extents (surface, extents);
2578
2579
3.53M
    if (! bounded)
2580
734
  _cairo_unbounded_rectangle_init (extents);
2581
2582
3.53M
    return bounded;
2583
2584
0
zero_extents:
2585
0
    extents->x = extents->y = 0;
2586
0
    extents->width = extents->height = 0;
2587
0
    return TRUE;
2588
3.53M
}
2589
2590
/**
2591
 * cairo_surface_has_show_text_glyphs:
2592
 * @surface: a #cairo_surface_t
2593
 *
2594
 * Returns whether the surface supports
2595
 * sophisticated cairo_show_text_glyphs() operations.  That is,
2596
 * whether it actually uses the provided text and cluster data
2597
 * to a cairo_show_text_glyphs() call.
2598
 *
2599
 * Note: Even if this function returns %FALSE, a
2600
 * cairo_show_text_glyphs() operation targeted at @surface will
2601
 * still succeed.  It just will
2602
 * act like a cairo_show_glyphs() operation.  Users can use this
2603
 * function to avoid computing UTF-8 text and cluster mapping if the
2604
 * target surface does not use it.
2605
 *
2606
 * Return value: %TRUE if @surface supports
2607
 *               cairo_show_text_glyphs(), %FALSE otherwise
2608
 *
2609
 * Since: 1.8
2610
 **/
2611
cairo_bool_t
2612
cairo_surface_has_show_text_glyphs (cairo_surface_t     *surface)
2613
985k
{
2614
985k
    if (unlikely (surface->status))
2615
0
  return FALSE;
2616
2617
985k
    if (unlikely (surface->finished)) {
2618
0
  _cairo_surface_set_error (surface, CAIRO_STATUS_SURFACE_FINISHED);
2619
0
  return FALSE;
2620
0
    }
2621
2622
985k
    if (surface->backend->has_show_text_glyphs)
2623
985k
  return surface->backend->has_show_text_glyphs (surface);
2624
0
    else
2625
0
  return surface->backend->show_text_glyphs != NULL;
2626
985k
}
2627
2628
0
#define GLYPH_CACHE_SIZE 64
2629
2630
static inline cairo_int_status_t
2631
ensure_scaled_glyph (cairo_scaled_font_t   *scaled_font,
2632
         cairo_color_t         *foreground_color,
2633
                     cairo_scaled_glyph_t **glyph_cache,
2634
                     cairo_glyph_t         *glyph,
2635
                     cairo_scaled_glyph_t **scaled_glyph)
2636
0
{
2637
0
    int cache_index;
2638
0
    cairo_int_status_t status = CAIRO_INT_STATUS_SUCCESS;
2639
2640
0
    cache_index = glyph->index % GLYPH_CACHE_SIZE;
2641
0
    *scaled_glyph = glyph_cache[cache_index];
2642
0
    if (*scaled_glyph == NULL || _cairo_scaled_glyph_index (*scaled_glyph) != glyph->index) {
2643
0
        status = _cairo_scaled_glyph_lookup (scaled_font,
2644
0
                                             glyph->index,
2645
0
                                             CAIRO_SCALED_GLYPH_INFO_COLOR_SURFACE,
2646
0
                                             foreground_color,
2647
0
                                             scaled_glyph);
2648
0
        if (status == CAIRO_INT_STATUS_UNSUPPORTED) {
2649
            /* If the color surface not available, ensure scaled_glyph is not NULL. */
2650
0
            status = _cairo_scaled_glyph_lookup (scaled_font,
2651
0
                                                 glyph->index,
2652
0
                                                 CAIRO_SCALED_GLYPH_INFO_SURFACE,
2653
0
                                                 NULL, /* foreground color */
2654
0
                                                 scaled_glyph);
2655
0
        }
2656
0
        if (unlikely (status))
2657
0
            status = _cairo_scaled_font_set_error (scaled_font, status);
2658
2659
0
        glyph_cache[cache_index] = *scaled_glyph;
2660
0
    }
2661
2662
0
    return status;
2663
0
}
2664
2665
static inline cairo_int_status_t
2666
composite_one_color_glyph (cairo_surface_t       *surface,
2667
                           cairo_operator_t       op,
2668
                           const cairo_pattern_t *source,
2669
                           const cairo_clip_t    *clip,
2670
                           cairo_glyph_t         *glyph,
2671
                           cairo_scaled_glyph_t  *scaled_glyph,
2672
         double                 x_scale,
2673
         double                 y_scale)
2674
0
{
2675
0
    cairo_int_status_t status;
2676
0
    cairo_image_surface_t *glyph_surface;
2677
0
    cairo_pattern_t *pattern;
2678
0
    cairo_matrix_t matrix;
2679
0
    int has_color;
2680
2681
0
    status = CAIRO_INT_STATUS_SUCCESS;
2682
2683
0
    has_color = scaled_glyph->has_info & CAIRO_SCALED_GLYPH_INFO_COLOR_SURFACE;
2684
0
    if (has_color)
2685
0
        glyph_surface = scaled_glyph->color_surface;
2686
0
    else
2687
0
        glyph_surface = scaled_glyph->surface;
2688
2689
0
    if (glyph_surface->width && glyph_surface->height) {
2690
0
        int x, y;
2691
        /* round glyph locations to the nearest pixels */
2692
        /* XXX: FRAGILE: We're ignoring device_transform scaling here. A bug? */
2693
0
  x = _cairo_lround (glyph->x * x_scale - glyph_surface->base.device_transform.x0);
2694
0
  y = _cairo_lround (glyph->y * y_scale - glyph_surface->base.device_transform.y0);
2695
2696
0
        pattern = cairo_pattern_create_for_surface ((cairo_surface_t *)glyph_surface);
2697
0
        cairo_matrix_init_translate (&matrix, - x, - y);
2698
0
  cairo_matrix_scale (&matrix, x_scale, y_scale);
2699
0
        cairo_pattern_set_matrix (pattern, &matrix);
2700
0
        if (op == CAIRO_OPERATOR_SOURCE || op == CAIRO_OPERATOR_CLEAR || !has_color)
2701
0
      status = _cairo_surface_mask (surface, op, pattern, pattern, clip);
2702
0
        else
2703
0
      status = _cairo_surface_paint (surface, op, pattern, clip);
2704
0
        cairo_pattern_destroy (pattern);
2705
0
    }
2706
2707
0
    return status;
2708
0
}
2709
2710
static cairo_int_status_t
2711
composite_color_glyphs (cairo_surface_t             *surface,
2712
                        cairo_operator_t             op,
2713
                        const cairo_pattern_t       *source,
2714
                        char                        *utf8,
2715
                        int                         *utf8_len,
2716
                        cairo_glyph_t               *glyphs,
2717
                        int                         *num_glyphs,
2718
                        cairo_text_cluster_t        *clusters,
2719
                  int         *num_clusters,
2720
            cairo_text_cluster_flags_t   cluster_flags,
2721
                        cairo_scaled_font_t         *scaled_font,
2722
                        const cairo_clip_t          *clip)
2723
0
{
2724
0
    cairo_int_status_t status;
2725
0
    int i, j;
2726
0
    cairo_scaled_glyph_t *scaled_glyph;
2727
0
    int remaining_clusters = 0;
2728
0
    int remaining_glyphs = 0;
2729
0
    int remaining_bytes = 0;
2730
0
    int glyph_pos = 0;
2731
0
    int byte_pos = 0;
2732
0
    int gp;
2733
0
    cairo_scaled_glyph_t *glyph_cache[GLYPH_CACHE_SIZE];
2734
0
    cairo_color_t *foreground_color = NULL;
2735
0
    double x_scale = 1.0;
2736
0
    double y_scale = 1.0;
2737
2738
0
    if (surface->is_vector) {
2739
0
  cairo_font_face_t *font_face;
2740
0
  cairo_matrix_t font_matrix;
2741
0
  cairo_matrix_t ctm;
2742
0
  cairo_font_options_t font_options;
2743
2744
0
  x_scale = surface->x_fallback_resolution / surface->x_resolution;
2745
0
  y_scale = surface->y_fallback_resolution / surface->y_resolution;
2746
0
  font_face = cairo_scaled_font_get_font_face (scaled_font);
2747
0
  cairo_scaled_font_get_font_matrix (scaled_font, &font_matrix);
2748
0
  cairo_scaled_font_get_ctm (scaled_font, &ctm);
2749
0
  _cairo_font_options_init_default (&font_options);
2750
0
  cairo_scaled_font_get_font_options (scaled_font, &font_options);
2751
0
  cairo_matrix_scale (&ctm, x_scale, y_scale);
2752
0
  scaled_font = cairo_scaled_font_create (font_face,
2753
0
            &font_matrix,
2754
0
            &ctm,
2755
0
            &font_options);
2756
0
    }
2757
2758
0
    if (source->type == CAIRO_PATTERN_TYPE_SOLID)
2759
0
  foreground_color = &((cairo_solid_pattern_t *) source)->color;
2760
2761
0
    memset (glyph_cache, 0, sizeof (glyph_cache));
2762
2763
0
    status = CAIRO_INT_STATUS_SUCCESS;
2764
2765
0
    _cairo_scaled_font_freeze_cache (scaled_font);
2766
2767
0
    if (clusters) {
2768
2769
0
        if (cluster_flags & CAIRO_TEXT_CLUSTER_FLAG_BACKWARD)
2770
0
            glyph_pos = *num_glyphs - 1;
2771
2772
0
        for (i = 0; i < *num_clusters; i++) {
2773
0
            cairo_bool_t skip_cluster = TRUE;
2774
2775
0
            for (j = 0; j < clusters[i].num_glyphs; j++) {
2776
0
                if (cluster_flags & CAIRO_TEXT_CLUSTER_FLAG_BACKWARD)
2777
0
                    gp = glyph_pos - j;
2778
0
                else
2779
0
                    gp = glyph_pos + j;
2780
2781
0
                status = ensure_scaled_glyph (scaled_font, foreground_color, glyph_cache,
2782
0
                                              &glyphs[gp], &scaled_glyph);
2783
0
                if (unlikely (status))
2784
0
                    goto UNLOCK;
2785
2786
0
                if ((scaled_glyph->has_info & CAIRO_SCALED_GLYPH_INFO_COLOR_SURFACE) != 0) {
2787
0
        cairo_bool_t supports_color_glyph = FALSE;
2788
2789
0
        if (surface->backend->supports_color_glyph) {
2790
0
      _cairo_scaled_font_thaw_cache (scaled_font);
2791
0
      supports_color_glyph = _cairo_surface_supports_color_glyph (surface, scaled_font, glyphs[gp].index);
2792
2793
0
      memset (glyph_cache, 0, sizeof (glyph_cache));
2794
0
      _cairo_scaled_font_freeze_cache (scaled_font);
2795
0
        }
2796
2797
0
        if (!supports_color_glyph) {
2798
0
      skip_cluster = FALSE;
2799
0
      break;
2800
0
        }
2801
0
    }
2802
0
            }
2803
2804
0
            if (skip_cluster) {
2805
0
                memmove (utf8 + remaining_bytes, utf8 + byte_pos, clusters[i].num_bytes);
2806
0
                remaining_bytes += clusters[i].num_bytes;
2807
0
                byte_pos += clusters[i].num_bytes;
2808
0
                for (j = 0; j < clusters[i].num_glyphs; j++, remaining_glyphs++) {
2809
0
                    if (cluster_flags & CAIRO_TEXT_CLUSTER_FLAG_BACKWARD)
2810
0
                        glyphs[*num_glyphs - 1 - remaining_glyphs] = glyphs[glyph_pos--];
2811
0
                    else
2812
0
                        glyphs[remaining_glyphs] = glyphs[glyph_pos++];
2813
0
                }
2814
0
                clusters[remaining_clusters++] = clusters[i];
2815
0
                continue;
2816
0
            }
2817
2818
0
            for (j = 0; j < clusters[i].num_glyphs; j++) {
2819
0
                if (cluster_flags & CAIRO_TEXT_CLUSTER_FLAG_BACKWARD)
2820
0
                    gp = glyph_pos - j;
2821
0
                else
2822
0
                    gp = glyph_pos + j;
2823
2824
0
                status = ensure_scaled_glyph (scaled_font, foreground_color, glyph_cache,
2825
0
                                              &glyphs[gp], &scaled_glyph);
2826
0
                if (unlikely (status))
2827
0
                    goto UNLOCK;
2828
2829
0
                status = composite_one_color_glyph (surface, op, source, clip,
2830
0
                &glyphs[gp], scaled_glyph,
2831
0
                x_scale, y_scale);
2832
0
                if (unlikely (status && status != CAIRO_INT_STATUS_NOTHING_TO_DO))
2833
0
                    goto UNLOCK;
2834
0
            }
2835
2836
0
            if (cluster_flags & CAIRO_TEXT_CLUSTER_FLAG_BACKWARD)
2837
0
                glyph_pos -= clusters[i].num_glyphs;
2838
0
            else
2839
0
                glyph_pos += clusters[i].num_glyphs;
2840
2841
0
            byte_pos += clusters[i].num_bytes;
2842
0
        }
2843
2844
0
        if (cluster_flags & CAIRO_TEXT_CLUSTER_FLAG_BACKWARD) {
2845
0
            memmove (utf8, utf8 + *utf8_len - remaining_bytes, remaining_bytes);
2846
0
            memmove (glyphs, glyphs + (*num_glyphs - remaining_glyphs), sizeof (cairo_glyph_t) * remaining_glyphs);
2847
0
        }
2848
2849
0
        *utf8_len = remaining_bytes;
2850
0
        *num_glyphs = remaining_glyphs;
2851
0
        *num_clusters = remaining_clusters;
2852
2853
0
    } else {
2854
2855
0
       for (glyph_pos = 0; glyph_pos < *num_glyphs; glyph_pos++) {
2856
0
           status = ensure_scaled_glyph (scaled_font, foreground_color, glyph_cache,
2857
0
                                         &glyphs[glyph_pos], &scaled_glyph);
2858
0
           if (unlikely (status))
2859
0
               goto UNLOCK;
2860
2861
0
           if ((scaled_glyph->has_info & CAIRO_SCALED_GLYPH_INFO_COLOR_SURFACE) == 0) {
2862
0
               glyphs[remaining_glyphs++] = glyphs[glyph_pos];
2863
0
               continue;
2864
0
           }
2865
2866
0
           status = composite_one_color_glyph (surface, op, source, clip,
2867
0
                 &glyphs[glyph_pos], scaled_glyph,
2868
0
                 x_scale, y_scale);
2869
0
           if (unlikely (status && status != CAIRO_INT_STATUS_NOTHING_TO_DO))
2870
0
               goto UNLOCK;
2871
0
        }
2872
2873
0
        *num_glyphs = remaining_glyphs;
2874
0
    }
2875
2876
0
UNLOCK:
2877
0
    _cairo_scaled_font_thaw_cache (scaled_font);
2878
2879
0
    if (surface->is_vector)
2880
0
  cairo_scaled_font_destroy (scaled_font);
2881
2882
0
    return status;
2883
0
}
2884
2885
/* Note: the backends may modify the contents of the glyph array as long as
2886
 * they do not return %CAIRO_INT_STATUS_UNSUPPORTED. This makes it possible to
2887
 * avoid copying the array again and again, and edit it in-place.
2888
 * Backends are in fact free to use the array as a generic buffer as they
2889
 * see fit.
2890
 *
2891
 * For show_glyphs backend method, and NOT for show_text_glyphs method,
2892
 * when they do return UNSUPPORTED, they may adjust remaining_glyphs to notify
2893
 * that they have successfully rendered some of the glyphs (from the beginning
2894
 * of the array), but not all.  If they don't touch remaining_glyphs, it
2895
 * defaults to all glyphs.
2896
 *
2897
 * See commits 5a9642c5746fd677aed35ce620ce90b1029b1a0c and
2898
 * 1781e6018c17909311295a9cc74b70500c6b4d0a for the rationale.
2899
 */
2900
cairo_status_t
2901
_cairo_surface_show_text_glyphs (cairo_surface_t      *surface,
2902
         cairo_operator_t      op,
2903
         const cairo_pattern_t      *source,
2904
         const char       *utf8,
2905
         int           utf8_len,
2906
         cairo_glyph_t        *glyphs,
2907
         int           num_glyphs,
2908
         const cairo_text_cluster_t *clusters,
2909
         int           num_clusters,
2910
         cairo_text_cluster_flags_t  cluster_flags,
2911
         cairo_scaled_font_t      *scaled_font,
2912
         const cairo_clip_t     *clip)
2913
1.94M
{
2914
1.94M
    cairo_int_status_t status;
2915
1.94M
    char *utf8_copy = NULL;
2916
2917
1.94M
    TRACE ((stderr, "%s\n", __FUNCTION__));
2918
1.94M
    if (unlikely (surface->status))
2919
0
  return surface->status;
2920
1.94M
    if (unlikely (surface->finished))
2921
0
  return _cairo_surface_set_error (surface, _cairo_error (CAIRO_STATUS_SURFACE_FINISHED));
2922
2923
1.94M
    if (num_glyphs == 0 && utf8_len == 0)
2924
0
  return CAIRO_STATUS_SUCCESS;
2925
2926
1.94M
    if (_cairo_clip_is_all_clipped (clip))
2927
0
  return CAIRO_STATUS_SUCCESS;
2928
2929
1.94M
    status = _pattern_has_error (source);
2930
1.94M
    if (unlikely (status))
2931
0
  return status;
2932
2933
1.94M
    status = cairo_scaled_font_status (scaled_font);
2934
1.94M
    if (unlikely (status))
2935
0
  return status;
2936
2937
1.94M
    if (!(_cairo_scaled_font_has_color_glyphs (scaled_font) &&
2938
1.94M
    scaled_font->options.color_mode != CAIRO_COLOR_MODE_NO_COLOR))
2939
1.94M
    {
2940
1.94M
        if (nothing_to_do (surface, op, source))
2941
0
      return CAIRO_STATUS_SUCCESS;
2942
1.94M
    }
2943
2944
1.94M
    status = _cairo_surface_begin_modification (surface);
2945
1.94M
    if (unlikely (status))
2946
0
  return status;
2947
2948
1.94M
    if (source->is_foreground_marker && surface->foreground_source)
2949
0
  source = surface->foreground_source;
2950
2951
1.94M
    if (_cairo_scaled_font_has_color_glyphs (scaled_font) &&
2952
1.94M
  scaled_font->options.color_mode != CAIRO_COLOR_MODE_NO_COLOR)
2953
0
    {
2954
0
        utf8_copy = malloc (sizeof (char) * utf8_len);
2955
0
        memcpy (utf8_copy, utf8, sizeof (char) * utf8_len);
2956
0
        utf8 = utf8_copy;
2957
2958
0
        status = composite_color_glyphs (surface, op,
2959
0
                                         source,
2960
0
                                         (char *)utf8, &utf8_len,
2961
0
                                         glyphs, &num_glyphs,
2962
0
                                         (cairo_text_cluster_t *)clusters, &num_clusters, cluster_flags,
2963
0
                                         scaled_font,
2964
0
                                         clip);
2965
2966
0
        if (unlikely (status && status != CAIRO_INT_STATUS_NOTHING_TO_DO))
2967
0
            goto DONE;
2968
2969
0
        if (num_glyphs == 0)
2970
0
            goto DONE;
2971
1.94M
    } else {
2972
1.94M
      utf8_copy = NULL;
2973
1.94M
    }
2974
2975
    /* The logic here is duplicated in _cairo_analysis_surface show_glyphs and
2976
     * show_text_glyphs.  Keep in synch. */
2977
1.94M
    if (clusters) {
2978
522k
        status = CAIRO_INT_STATUS_UNSUPPORTED;
2979
  /* A real show_text_glyphs call.  Try show_text_glyphs backend
2980
   * method first */
2981
522k
  if (surface->backend->show_text_glyphs != NULL) {
2982
522k
      status = surface->backend->show_text_glyphs (surface, op,
2983
522k
               source,
2984
522k
               utf8, utf8_len,
2985
522k
               glyphs, num_glyphs,
2986
522k
               clusters, num_clusters, cluster_flags,
2987
522k
               scaled_font,
2988
522k
               clip);
2989
522k
  }
2990
522k
  if (status == CAIRO_INT_STATUS_UNSUPPORTED &&
2991
522k
      surface->backend->show_glyphs)
2992
0
  {
2993
0
      status = surface->backend->show_glyphs (surface, op,
2994
0
                source,
2995
0
                glyphs, num_glyphs,
2996
0
                scaled_font,
2997
0
                clip);
2998
0
  }
2999
1.41M
    } else {
3000
  /* A mere show_glyphs call.  Try show_glyphs backend method first */
3001
1.41M
  if (surface->backend->show_glyphs != NULL) {
3002
354k
      status = surface->backend->show_glyphs (surface, op,
3003
354k
                source,
3004
354k
                glyphs, num_glyphs,
3005
354k
                scaled_font,
3006
354k
                clip);
3007
1.06M
  } else if (surface->backend->show_text_glyphs != NULL) {
3008
      /* Intentionally only try show_text_glyphs method for show_glyphs
3009
       * calls if backend does not have show_glyphs.  If backend has
3010
       * both methods implemented, we don't fallback from show_glyphs to
3011
       * show_text_glyphs, and hence the backend can assume in its
3012
       * show_text_glyphs call that clusters is not NULL (which also
3013
       * implies that UTF-8 is not NULL, unless the text is
3014
       * zero-length).
3015
       */
3016
1.06M
      status = surface->backend->show_text_glyphs (surface, op,
3017
1.06M
               source,
3018
1.06M
               utf8, utf8_len,
3019
1.06M
               glyphs, num_glyphs,
3020
1.06M
               clusters, num_clusters, cluster_flags,
3021
1.06M
               scaled_font,
3022
1.06M
               clip);
3023
1.06M
  }
3024
1.41M
    }
3025
3026
1.94M
DONE:
3027
1.94M
    if (status != CAIRO_INT_STATUS_NOTHING_TO_DO) {
3028
1.93M
  surface->is_clear = FALSE;
3029
1.93M
  surface->serial++;
3030
1.93M
    }
3031
3032
1.94M
    if (utf8_copy)
3033
0
        free (utf8_copy);
3034
3035
1.94M
    return _cairo_surface_set_error (surface, status);
3036
1.94M
}
3037
3038
cairo_status_t
3039
_cairo_surface_tag (cairo_surface_t         *surface,
3040
        cairo_bool_t                 begin,
3041
        const char                  *tag_name,
3042
        const char                  *attributes)
3043
0
{
3044
0
    cairo_int_status_t status;
3045
3046
0
    TRACE ((stderr, "%s\n", __FUNCTION__));
3047
0
    if (unlikely (surface->status))
3048
0
  return surface->status;
3049
0
    if (unlikely (surface->finished))
3050
0
  return _cairo_surface_set_error (surface, _cairo_error (CAIRO_STATUS_SURFACE_FINISHED));
3051
3052
0
    if (surface->backend->tag == NULL)
3053
0
  return CAIRO_STATUS_SUCCESS;
3054
3055
0
    status = surface->backend->tag (surface, begin, tag_name, attributes);
3056
0
    surface->is_clear = FALSE;
3057
3058
0
    return _cairo_surface_set_error (surface, status);
3059
0
}
3060
3061
cairo_bool_t
3062
_cairo_surface_supports_color_glyph (cairo_surface_t       *surface,
3063
             cairo_scaled_font_t   *scaled_font,
3064
             unsigned long          glyph_index)
3065
0
{
3066
0
    if (surface->backend->supports_color_glyph != NULL)
3067
0
  return surface->backend->supports_color_glyph (surface, scaled_font, glyph_index);
3068
3069
0
    return FALSE;
3070
0
}
3071
3072
/**
3073
 * _cairo_surface_set_resolution:
3074
 * @surface: the surface
3075
 * @x_res: x resolution, in dpi
3076
 * @y_res: y resolution, in dpi
3077
 *
3078
 * Set the actual surface resolution of @surface to the given x and y DPI.
3079
 * Mainly used for correctly computing the scale factor when fallback
3080
 * rendering needs to take place in the paginated surface.
3081
 **/
3082
void
3083
_cairo_surface_set_resolution (cairo_surface_t *surface,
3084
             double x_res,
3085
             double y_res)
3086
0
{
3087
0
    if (surface->status)
3088
0
  return;
3089
3090
0
    surface->x_resolution = x_res;
3091
0
    surface->y_resolution = y_res;
3092
0
}
3093
3094
/**
3095
 * _cairo_surface_create_in_error:
3096
 * @status: the error status
3097
 *
3098
 * Return an appropriate static error surface for the error status.
3099
 * On error, surface creation functions should always return a surface
3100
 * created with _cairo_surface_create_in_error() instead of a new surface
3101
 * in an error state. This simplifies internal code as no refcounting has
3102
 * to be done.
3103
 **/
3104
cairo_surface_t *
3105
_cairo_surface_create_in_error (cairo_status_t status)
3106
0
{
3107
0
    assert (status < CAIRO_STATUS_LAST_STATUS);
3108
0
    switch (status) {
3109
0
    case CAIRO_STATUS_NO_MEMORY:
3110
0
  return (cairo_surface_t *) &_cairo_surface_nil;
3111
0
    case CAIRO_STATUS_SURFACE_TYPE_MISMATCH:
3112
0
  return (cairo_surface_t *) &_cairo_surface_nil_surface_type_mismatch;
3113
0
    case CAIRO_STATUS_INVALID_STATUS:
3114
0
  return (cairo_surface_t *) &_cairo_surface_nil_invalid_status;
3115
0
    case CAIRO_STATUS_INVALID_CONTENT:
3116
0
  return (cairo_surface_t *) &_cairo_surface_nil_invalid_content;
3117
0
    case CAIRO_STATUS_INVALID_FORMAT:
3118
0
  return (cairo_surface_t *) &_cairo_surface_nil_invalid_format;
3119
0
    case CAIRO_STATUS_INVALID_VISUAL:
3120
0
  return (cairo_surface_t *) &_cairo_surface_nil_invalid_visual;
3121
0
    case CAIRO_STATUS_READ_ERROR:
3122
0
  return (cairo_surface_t *) &_cairo_surface_nil_read_error;
3123
0
    case CAIRO_STATUS_WRITE_ERROR:
3124
0
  return (cairo_surface_t *) &_cairo_surface_nil_write_error;
3125
0
    case CAIRO_STATUS_FILE_NOT_FOUND:
3126
0
  return (cairo_surface_t *) &_cairo_surface_nil_file_not_found;
3127
0
    case CAIRO_STATUS_TEMP_FILE_ERROR:
3128
0
  return (cairo_surface_t *) &_cairo_surface_nil_temp_file_error;
3129
0
    case CAIRO_STATUS_INVALID_STRIDE:
3130
0
  return (cairo_surface_t *) &_cairo_surface_nil_invalid_stride;
3131
0
    case CAIRO_STATUS_INVALID_SIZE:
3132
0
  return (cairo_surface_t *) &_cairo_surface_nil_invalid_size;
3133
0
    case CAIRO_STATUS_DEVICE_TYPE_MISMATCH:
3134
0
  return (cairo_surface_t *) &_cairo_surface_nil_device_type_mismatch;
3135
0
    case CAIRO_STATUS_DEVICE_ERROR:
3136
0
  return (cairo_surface_t *) &_cairo_surface_nil_device_error;
3137
0
    case CAIRO_STATUS_PNG_ERROR:
3138
0
  return (cairo_surface_t *) &_cairo_surface_nil_png_error;
3139
0
    case CAIRO_STATUS_SUCCESS:
3140
0
    case CAIRO_STATUS_LAST_STATUS:
3141
0
  ASSERT_NOT_REACHED;
3142
  /* fall-through */
3143
0
    case CAIRO_STATUS_INVALID_RESTORE:
3144
0
    case CAIRO_STATUS_INVALID_POP_GROUP:
3145
0
    case CAIRO_STATUS_NO_CURRENT_POINT:
3146
0
    case CAIRO_STATUS_INVALID_MATRIX:
3147
0
    case CAIRO_STATUS_NULL_POINTER:
3148
0
    case CAIRO_STATUS_INVALID_STRING:
3149
0
    case CAIRO_STATUS_INVALID_PATH_DATA:
3150
0
    case CAIRO_STATUS_SURFACE_FINISHED:
3151
0
    case CAIRO_STATUS_PATTERN_TYPE_MISMATCH:
3152
0
    case CAIRO_STATUS_INVALID_DASH:
3153
0
    case CAIRO_STATUS_INVALID_DSC_COMMENT:
3154
0
    case CAIRO_STATUS_INVALID_INDEX:
3155
0
    case CAIRO_STATUS_CLIP_NOT_REPRESENTABLE:
3156
0
    case CAIRO_STATUS_FONT_TYPE_MISMATCH:
3157
0
    case CAIRO_STATUS_USER_FONT_IMMUTABLE:
3158
0
    case CAIRO_STATUS_USER_FONT_ERROR:
3159
0
    case CAIRO_STATUS_NEGATIVE_COUNT:
3160
0
    case CAIRO_STATUS_INVALID_CLUSTERS:
3161
0
    case CAIRO_STATUS_INVALID_SLANT:
3162
0
    case CAIRO_STATUS_INVALID_WEIGHT:
3163
0
    case CAIRO_STATUS_USER_FONT_NOT_IMPLEMENTED:
3164
0
    case CAIRO_STATUS_INVALID_MESH_CONSTRUCTION:
3165
0
    case CAIRO_STATUS_DEVICE_FINISHED:
3166
0
    case CAIRO_STATUS_JBIG2_GLOBAL_MISSING:
3167
0
    case CAIRO_STATUS_FREETYPE_ERROR:
3168
0
    case CAIRO_STATUS_WIN32_GDI_ERROR:
3169
0
    case CAIRO_INT_STATUS_DWRITE_ERROR:
3170
0
    case CAIRO_STATUS_TAG_ERROR:
3171
0
    case CAIRO_STATUS_SVG_FONT_ERROR:
3172
0
    default:
3173
0
  _cairo_error_throw (CAIRO_STATUS_NO_MEMORY);
3174
0
  return (cairo_surface_t *) &_cairo_surface_nil;
3175
0
    }
3176
0
}
3177
3178
cairo_surface_t *
3179
_cairo_int_surface_create_in_error (cairo_int_status_t status)
3180
0
{
3181
0
    if (status < CAIRO_INT_STATUS_LAST_STATUS)
3182
0
  return _cairo_surface_create_in_error (status);
3183
3184
0
    switch ((int)status) {
3185
0
    case CAIRO_INT_STATUS_UNSUPPORTED:
3186
0
  return (cairo_surface_t *) &_cairo_surface_nil_unsupported;
3187
0
    case CAIRO_INT_STATUS_NOTHING_TO_DO:
3188
0
  return (cairo_surface_t *) &_cairo_surface_nil_nothing_to_do;
3189
0
    default:
3190
0
  _cairo_error_throw (CAIRO_STATUS_NO_MEMORY);
3191
0
  return (cairo_surface_t *) &_cairo_surface_nil;
3192
0
    }
3193
0
}
3194
3195
/*  LocalWords:  rasterized
3196
 */