cbc3974592f67e5e14f48a0e80e2e22b15dcdb12
[framework/graphics/cairo.git] / src / cairo.c
1 /* -*- Mode: c; c-basic-offset: 4; indent-tabs-mode: t; tab-width: 8; -*- */
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  * Copyright © 2011 Intel Corporation
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it either under the terms of the GNU Lesser General Public
10  * License version 2.1 as published by the Free Software Foundation
11  * (the "LGPL") or, at your option, under the terms of the Mozilla
12  * Public License Version 1.1 (the "MPL"). If you do not alter this
13  * notice, a recipient may use your version of this file under either
14  * the MPL or the LGPL.
15  *
16  * You should have received a copy of the LGPL along with this library
17  * in the file COPYING-LGPL-2.1; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA
19  * You should have received a copy of the MPL along with this library
20  * in the file COPYING-MPL-1.1
21  *
22  * The contents of this file are subject to the Mozilla Public License
23  * Version 1.1 (the "License"); you may not use this file except in
24  * compliance with the License. You may obtain a copy of the License at
25  * http://www.mozilla.org/MPL/
26  *
27  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
28  * OF ANY KIND, either express or implied. See the LGPL or the MPL for
29  * the specific language governing rights and limitations.
30  *
31  * The Original Code is the cairo graphics library.
32  *
33  * The Initial Developer of the Original Code is University of Southern
34  * California.
35  *
36  * Contributor(s):
37  *      Carl D. Worth <cworth@cworth.org>
38  *      Chris Wilson <chris@chris-wilson.co.uk>
39  */
40
41 #include "cairoint.h"
42 #include "cairo-private.h"
43
44 #include "cairo-backend-private.h"
45 #include "cairo-error-private.h"
46 #include "cairo-path-private.h"
47 #include "cairo-pattern-private.h"
48 #include "cairo-surface-private.h"
49 #include "cairo-surface-backend-private.h"
50
51 #include <assert.h>
52
53 /**
54  * SECTION:cairo
55  * @Title: cairo_t
56  * @Short_Description: The cairo drawing context
57  * @See_Also: #cairo_surface_t
58  *
59  * #cairo_t is the main object used when drawing with cairo. To
60  * draw with cairo, you create a #cairo_t, set the target surface,
61  * and drawing options for the #cairo_t, create shapes with
62  * functions like cairo_move_to() and cairo_line_to(), and then
63  * draw shapes with cairo_stroke() or cairo_fill().
64  *
65  * #cairo_t<!-- -->'s can be pushed to a stack via cairo_save().
66  * They may then safely be changed, without loosing the current state.
67  * Use cairo_restore() to restore to the saved state.
68  */
69
70 /**
71  * SECTION:cairo-text
72  * @Title: text
73  * @Short_Description: Rendering text and glyphs
74  * @See_Also: #cairo_font_face_t, #cairo_scaled_font_t, cairo_text_path(),
75  *            cairo_glyph_path()
76  *
77  * The functions with <emphasis>text</emphasis> in their name form cairo's
78  * <firstterm>toy</firstterm> text API.  The toy API takes UTF-8 encoded
79  * text and is limited in its functionality to rendering simple
80  * left-to-right text with no advanced features.  That means for example
81  * that most complex scripts like Hebrew, Arabic, and Indic scripts are
82  * out of question.  No kerning or correct positioning of diacritical marks
83  * either.  The font selection is pretty limited too and doesn't handle the
84  * case that the selected font does not cover the characters in the text.
85  * This set of functions are really that, a toy text API, for testing and
86  * demonstration purposes.  Any serious application should avoid them.
87  *
88  * The functions with <emphasis>glyphs</emphasis> in their name form cairo's
89  * <firstterm>low-level</firstterm> text API.  The low-level API relies on
90  * the user to convert text to a set of glyph indexes and positions.  This
91  * is a very hard problem and is best handled by external libraries, like
92  * the pangocairo that is part of the Pango text layout and rendering library.
93  * Pango is available from <ulink
94  * url="http://www.pango.org/">http://www.pango.org/</ulink>.
95  */
96
97 /**
98  * SECTION:cairo-transforms
99  * @Title: Transformations
100  * @Short_Description: Manipulating the current transformation matrix
101  * @See_Also: #cairo_matrix_t
102  *
103  * The current transformation matrix, <firstterm>ctm</firstterm>, is a
104  * two-dimensional affine transformation that maps all coordinates and other
105  * drawing instruments from the <firstterm>user space</firstterm> into the
106  * surface's canonical coordinate system, also known as the <firstterm>device
107  * space</firstterm>.
108  */
109
110 #define DEFINE_NIL_CONTEXT(status)                                      \
111     {                                                                   \
112         CAIRO_REFERENCE_COUNT_INVALID,  /* ref_count */                 \
113         status,                         /* status */                    \
114         { 0, 0, 0, NULL },              /* user_data */                 \
115         NULL                                                            \
116     }
117
118 static const cairo_t _cairo_nil[] = {
119     DEFINE_NIL_CONTEXT (CAIRO_STATUS_NO_MEMORY),
120     DEFINE_NIL_CONTEXT (CAIRO_STATUS_INVALID_RESTORE),
121     DEFINE_NIL_CONTEXT (CAIRO_STATUS_INVALID_POP_GROUP),
122     DEFINE_NIL_CONTEXT (CAIRO_STATUS_NO_CURRENT_POINT),
123     DEFINE_NIL_CONTEXT (CAIRO_STATUS_INVALID_MATRIX),
124     DEFINE_NIL_CONTEXT (CAIRO_STATUS_INVALID_STATUS),
125     DEFINE_NIL_CONTEXT (CAIRO_STATUS_NULL_POINTER),
126     DEFINE_NIL_CONTEXT (CAIRO_STATUS_INVALID_STRING),
127     DEFINE_NIL_CONTEXT (CAIRO_STATUS_INVALID_PATH_DATA),
128     DEFINE_NIL_CONTEXT (CAIRO_STATUS_READ_ERROR),
129     DEFINE_NIL_CONTEXT (CAIRO_STATUS_WRITE_ERROR),
130     DEFINE_NIL_CONTEXT (CAIRO_STATUS_SURFACE_FINISHED),
131     DEFINE_NIL_CONTEXT (CAIRO_STATUS_SURFACE_TYPE_MISMATCH),
132     DEFINE_NIL_CONTEXT (CAIRO_STATUS_PATTERN_TYPE_MISMATCH),
133     DEFINE_NIL_CONTEXT (CAIRO_STATUS_INVALID_CONTENT),
134     DEFINE_NIL_CONTEXT (CAIRO_STATUS_INVALID_FORMAT),
135     DEFINE_NIL_CONTEXT (CAIRO_STATUS_INVALID_VISUAL),
136     DEFINE_NIL_CONTEXT (CAIRO_STATUS_FILE_NOT_FOUND),
137     DEFINE_NIL_CONTEXT (CAIRO_STATUS_INVALID_DASH),
138     DEFINE_NIL_CONTEXT (CAIRO_STATUS_INVALID_DSC_COMMENT),
139     DEFINE_NIL_CONTEXT (CAIRO_STATUS_INVALID_INDEX),
140     DEFINE_NIL_CONTEXT (CAIRO_STATUS_CLIP_NOT_REPRESENTABLE),
141     DEFINE_NIL_CONTEXT (CAIRO_STATUS_TEMP_FILE_ERROR),
142     DEFINE_NIL_CONTEXT (CAIRO_STATUS_INVALID_STRIDE),
143     DEFINE_NIL_CONTEXT (CAIRO_STATUS_FONT_TYPE_MISMATCH),
144     DEFINE_NIL_CONTEXT (CAIRO_STATUS_USER_FONT_IMMUTABLE),
145     DEFINE_NIL_CONTEXT (CAIRO_STATUS_USER_FONT_ERROR),
146     DEFINE_NIL_CONTEXT (CAIRO_STATUS_NEGATIVE_COUNT),
147     DEFINE_NIL_CONTEXT (CAIRO_STATUS_INVALID_CLUSTERS),
148     DEFINE_NIL_CONTEXT (CAIRO_STATUS_INVALID_SLANT),
149     DEFINE_NIL_CONTEXT (CAIRO_STATUS_INVALID_WEIGHT),
150     DEFINE_NIL_CONTEXT (CAIRO_STATUS_INVALID_SIZE),
151     DEFINE_NIL_CONTEXT (CAIRO_STATUS_USER_FONT_NOT_IMPLEMENTED),
152     DEFINE_NIL_CONTEXT (CAIRO_STATUS_DEVICE_TYPE_MISMATCH),
153     DEFINE_NIL_CONTEXT (CAIRO_STATUS_DEVICE_ERROR),
154     DEFINE_NIL_CONTEXT (CAIRO_STATUS_INVALID_MESH_CONSTRUCTION),
155     DEFINE_NIL_CONTEXT (CAIRO_STATUS_DEVICE_FINISHED)
156 };
157 COMPILE_TIME_ASSERT (ARRAY_LENGTH (_cairo_nil) == CAIRO_STATUS_LAST_STATUS - 1);
158
159 /**
160  * _cairo_set_error:
161  * @cr: a cairo context
162  * @status: a status value indicating an error
163  *
164  * Atomically sets cr->status to @status and calls _cairo_error;
165  * Does nothing if status is %CAIRO_STATUS_SUCCESS.
166  *
167  * All assignments of an error status to cr->status should happen
168  * through _cairo_set_error(). Note that due to the nature of the atomic
169  * operation, it is not safe to call this function on the nil objects.
170  *
171  * The purpose of this function is to allow the user to set a
172  * breakpoint in _cairo_error() to generate a stack trace for when the
173  * user causes cairo to detect an error.
174  **/
175 static void
176 _cairo_set_error (cairo_t *cr, cairo_status_t status)
177 {
178     /* Don't overwrite an existing error. This preserves the first
179      * error, which is the most significant. */
180     _cairo_status_set_error (&cr->status, _cairo_error (status));
181 }
182
183 cairo_t *
184 _cairo_create_in_error (cairo_status_t status)
185 {
186     cairo_t *cr;
187
188     assert (status != CAIRO_STATUS_SUCCESS);
189
190     cr = (cairo_t *) &_cairo_nil[status - CAIRO_STATUS_NO_MEMORY];
191     assert (status == cr->status);
192
193     return cr;
194 }
195
196 /**
197  * cairo_create:
198  * @target: target surface for the context
199  *
200  * Creates a new #cairo_t with all graphics state parameters set to
201  * default values and with @target as a target surface. The target
202  * surface should be constructed with a backend-specific function such
203  * as cairo_image_surface_create() (or any other
204  * <function>cairo_<emphasis>backend</emphasis>_surface_create(<!-- -->)</function>
205  * variant).
206  *
207  * This function references @target, so you can immediately
208  * call cairo_surface_destroy() on it if you don't need to
209  * maintain a separate reference to it.
210  *
211  * Return value: a newly allocated #cairo_t with a reference
212  *  count of 1. The initial reference count should be released
213  *  with cairo_destroy() when you are done using the #cairo_t.
214  *  This function never returns %NULL. If memory cannot be
215  *  allocated, a special #cairo_t object will be returned on
216  *  which cairo_status() returns %CAIRO_STATUS_NO_MEMORY. If
217  *  you attempt to target a surface which does not support
218  *  writing (such as #cairo_mime_surface_t) then a
219  *  %CAIRO_STATUS_WRITE_ERROR will be raised.  You can use this
220  *  object normally, but no drawing will be done.
221  **/
222 cairo_t *
223 cairo_create (cairo_surface_t *target)
224 {
225     if (unlikely (target == NULL))
226         return _cairo_create_in_error (_cairo_error (CAIRO_STATUS_NULL_POINTER));
227     if (unlikely (target->status))
228         return _cairo_create_in_error (target->status);
229
230     if (target->backend->create_context == NULL)
231         return _cairo_create_in_error (_cairo_error (CAIRO_STATUS_WRITE_ERROR));
232
233     return target->backend->create_context (target);
234
235 }
236 slim_hidden_def (cairo_create);
237
238 void
239 _cairo_init (cairo_t *cr,
240              const cairo_backend_t *backend)
241 {
242     CAIRO_REFERENCE_COUNT_INIT (&cr->ref_count, 1);
243     cr->status = CAIRO_STATUS_SUCCESS;
244     _cairo_user_data_array_init (&cr->user_data);
245
246     cr->backend = backend;
247 }
248
249 /**
250  * cairo_reference:
251  * @cr: a #cairo_t
252  *
253  * Increases the reference count on @cr by one. This prevents
254  * @cr from being destroyed until a matching call to cairo_destroy()
255  * is made.
256  *
257  * The number of references to a #cairo_t can be get using
258  * cairo_get_reference_count().
259  *
260  * Return value: the referenced #cairo_t.
261  **/
262 cairo_t *
263 cairo_reference (cairo_t *cr)
264 {
265     if (cr == NULL || CAIRO_REFERENCE_COUNT_IS_INVALID (&cr->ref_count))
266         return cr;
267
268     assert (CAIRO_REFERENCE_COUNT_HAS_REFERENCE (&cr->ref_count));
269
270     _cairo_reference_count_inc (&cr->ref_count);
271
272     return cr;
273 }
274
275 void
276 _cairo_fini (cairo_t *cr)
277 {
278     _cairo_user_data_array_fini (&cr->user_data);
279 }
280
281 /**
282  * cairo_destroy:
283  * @cr: a #cairo_t
284  *
285  * Decreases the reference count on @cr by one. If the result
286  * is zero, then @cr and all associated resources are freed.
287  * See cairo_reference().
288  **/
289 void
290 cairo_destroy (cairo_t *cr)
291 {
292     if (cr == NULL || CAIRO_REFERENCE_COUNT_IS_INVALID (&cr->ref_count))
293         return;
294
295     assert (CAIRO_REFERENCE_COUNT_HAS_REFERENCE (&cr->ref_count));
296
297     if (! _cairo_reference_count_dec_and_test (&cr->ref_count))
298         return;
299
300     cr->backend->destroy (cr);
301 }
302 slim_hidden_def (cairo_destroy);
303
304 /**
305  * cairo_get_user_data:
306  * @cr: a #cairo_t
307  * @key: the address of the #cairo_user_data_key_t the user data was
308  * attached to
309  *
310  * Return user data previously attached to @cr using the specified
311  * key.  If no user data has been attached with the given key this
312  * function returns %NULL.
313  *
314  * Return value: the user data previously attached or %NULL.
315  *
316  * Since: 1.4
317  **/
318 void *
319 cairo_get_user_data (cairo_t                     *cr,
320                      const cairo_user_data_key_t *key)
321 {
322     return _cairo_user_data_array_get_data (&cr->user_data, key);
323 }
324
325 /**
326  * cairo_set_user_data:
327  * @cr: a #cairo_t
328  * @key: the address of a #cairo_user_data_key_t to attach the user data to
329  * @user_data: the user data to attach to the #cairo_t
330  * @destroy: a #cairo_destroy_func_t which will be called when the
331  * #cairo_t is destroyed or when new user data is attached using the
332  * same key.
333  *
334  * Attach user data to @cr.  To remove user data from a surface,
335  * call this function with the key that was used to set it and %NULL
336  * for @data.
337  *
338  * Return value: %CAIRO_STATUS_SUCCESS or %CAIRO_STATUS_NO_MEMORY if a
339  * slot could not be allocated for the user data.
340  *
341  * Since: 1.4
342  **/
343 cairo_status_t
344 cairo_set_user_data (cairo_t                     *cr,
345                      const cairo_user_data_key_t *key,
346                      void                        *user_data,
347                      cairo_destroy_func_t        destroy)
348 {
349     if (CAIRO_REFERENCE_COUNT_IS_INVALID (&cr->ref_count))
350         return cr->status;
351
352     return _cairo_user_data_array_set_data (&cr->user_data,
353                                             key, user_data, destroy);
354 }
355
356 /**
357  * cairo_get_reference_count:
358  * @cr: a #cairo_t
359  *
360  * Returns the current reference count of @cr.
361  *
362  * Return value: the current reference count of @cr.  If the
363  * object is a nil object, 0 will be returned.
364  *
365  * Since: 1.4
366  **/
367 unsigned int
368 cairo_get_reference_count (cairo_t *cr)
369 {
370     if (cr == NULL || CAIRO_REFERENCE_COUNT_IS_INVALID (&cr->ref_count))
371         return 0;
372
373     return CAIRO_REFERENCE_COUNT_GET_VALUE (&cr->ref_count);
374 }
375
376 /**
377  * cairo_save:
378  * @cr: a #cairo_t
379  *
380  * Makes a copy of the current state of @cr and saves it
381  * on an internal stack of saved states for @cr. When
382  * cairo_restore() is called, @cr will be restored to
383  * the saved state. Multiple calls to cairo_save() and
384  * cairo_restore() can be nested; each call to cairo_restore()
385  * restores the state from the matching paired cairo_save().
386  *
387  * It isn't necessary to clear all saved states before
388  * a #cairo_t is freed. If the reference count of a #cairo_t
389  * drops to zero in response to a call to cairo_destroy(),
390  * any saved states will be freed along with the #cairo_t.
391  **/
392 void
393 cairo_save (cairo_t *cr)
394 {
395     cairo_status_t status;
396
397     if (unlikely (cr->status))
398         return;
399
400     status = cr->backend->save (cr);
401     if (unlikely (status))
402         _cairo_set_error (cr, status);
403 }
404 slim_hidden_def(cairo_save);
405
406 /**
407  * cairo_restore:
408  * @cr: a #cairo_t
409  *
410  * Restores @cr to the state saved by a preceding call to
411  * cairo_save() and removes that state from the stack of
412  * saved states.
413  **/
414 void
415 cairo_restore (cairo_t *cr)
416 {
417     cairo_status_t status;
418
419     if (unlikely (cr->status))
420         return;
421
422     status = cr->backend->restore (cr);
423     if (unlikely (status))
424         _cairo_set_error (cr, status);
425 }
426 slim_hidden_def(cairo_restore);
427
428 /**
429  * cairo_push_group:
430  * @cr: a cairo context
431  *
432  * Temporarily redirects drawing to an intermediate surface known as a
433  * group. The redirection lasts until the group is completed by a call
434  * to cairo_pop_group() or cairo_pop_group_to_source(). These calls
435  * provide the result of any drawing to the group as a pattern,
436  * (either as an explicit object, or set as the source pattern).
437  *
438  * This group functionality can be convenient for performing
439  * intermediate compositing. One common use of a group is to render
440  * objects as opaque within the group, (so that they occlude each
441  * other), and then blend the result with translucence onto the
442  * destination.
443  *
444  * Groups can be nested arbitrarily deep by making balanced calls to
445  * cairo_push_group()/cairo_pop_group(). Each call pushes/pops the new
446  * target group onto/from a stack.
447  *
448  * The cairo_push_group() function calls cairo_save() so that any
449  * changes to the graphics state will not be visible outside the
450  * group, (the pop_group functions call cairo_restore()).
451  *
452  * By default the intermediate group will have a content type of
453  * %CAIRO_CONTENT_COLOR_ALPHA. Other content types can be chosen for
454  * the group by using cairo_push_group_with_content() instead.
455  *
456  * As an example, here is how one might fill and stroke a path with
457  * translucence, but without any portion of the fill being visible
458  * under the stroke:
459  *
460  * <informalexample><programlisting>
461  * cairo_push_group (cr);
462  * cairo_set_source (cr, fill_pattern);
463  * cairo_fill_preserve (cr);
464  * cairo_set_source (cr, stroke_pattern);
465  * cairo_stroke (cr);
466  * cairo_pop_group_to_source (cr);
467  * cairo_paint_with_alpha (cr, alpha);
468  * </programlisting></informalexample>
469  *
470  * Since: 1.2
471  */
472 void
473 cairo_push_group (cairo_t *cr)
474 {
475     cairo_push_group_with_content (cr, CAIRO_CONTENT_COLOR_ALPHA);
476 }
477
478 /**
479  * cairo_push_group_with_content:
480  * @cr: a cairo context
481  * @content: a #cairo_content_t indicating the type of group that
482  *           will be created
483  *
484  * Temporarily redirects drawing to an intermediate surface known as a
485  * group. The redirection lasts until the group is completed by a call
486  * to cairo_pop_group() or cairo_pop_group_to_source(). These calls
487  * provide the result of any drawing to the group as a pattern,
488  * (either as an explicit object, or set as the source pattern).
489  *
490  * The group will have a content type of @content. The ability to
491  * control this content type is the only distinction between this
492  * function and cairo_push_group() which you should see for a more
493  * detailed description of group rendering.
494  *
495  * Since: 1.2
496  */
497 void
498 cairo_push_group_with_content (cairo_t *cr, cairo_content_t content)
499 {
500     cairo_status_t status;
501
502     if (unlikely (cr->status))
503         return;
504
505     status = cr->backend->push_group (cr, content);
506     if (unlikely (status))
507         _cairo_set_error (cr, status);
508 }
509 slim_hidden_def(cairo_push_group_with_content);
510
511 /**
512  * cairo_pop_group:
513  * @cr: a cairo context
514  *
515  * Terminates the redirection begun by a call to cairo_push_group() or
516  * cairo_push_group_with_content() and returns a new pattern
517  * containing the results of all drawing operations performed to the
518  * group.
519  *
520  * The cairo_pop_group() function calls cairo_restore(), (balancing a
521  * call to cairo_save() by the push_group function), so that any
522  * changes to the graphics state will not be visible outside the
523  * group.
524  *
525  * Return value: a newly created (surface) pattern containing the
526  * results of all drawing operations performed to the group. The
527  * caller owns the returned object and should call
528  * cairo_pattern_destroy() when finished with it.
529  *
530  * Since: 1.2
531  **/
532 cairo_pattern_t *
533 cairo_pop_group (cairo_t *cr)
534 {
535     cairo_pattern_t *group_pattern;
536
537     if (unlikely (cr->status))
538         return _cairo_pattern_create_in_error (cr->status);
539
540     group_pattern = cr->backend->pop_group (cr);
541     if (unlikely (group_pattern->status))
542         _cairo_set_error (cr, group_pattern->status);
543
544     return group_pattern;
545 }
546 slim_hidden_def(cairo_pop_group);
547
548 /**
549  * cairo_pop_group_to_source:
550  * @cr: a cairo context
551  *
552  * Terminates the redirection begun by a call to cairo_push_group() or
553  * cairo_push_group_with_content() and installs the resulting pattern
554  * as the source pattern in the given cairo context.
555  *
556  * The behavior of this function is equivalent to the sequence of
557  * operations:
558  *
559  * <informalexample><programlisting>
560  * cairo_pattern_t *group = cairo_pop_group (cr);
561  * cairo_set_source (cr, group);
562  * cairo_pattern_destroy (group);
563  * </programlisting></informalexample>
564  *
565  * but is more convenient as their is no need for a variable to store
566  * the short-lived pointer to the pattern.
567  *
568  * The cairo_pop_group() function calls cairo_restore(), (balancing a
569  * call to cairo_save() by the push_group function), so that any
570  * changes to the graphics state will not be visible outside the
571  * group.
572  *
573  * Since: 1.2
574  **/
575 void
576 cairo_pop_group_to_source (cairo_t *cr)
577 {
578     cairo_pattern_t *group_pattern;
579
580     group_pattern = cairo_pop_group (cr);
581     cairo_set_source (cr, group_pattern);
582     cairo_pattern_destroy (group_pattern);
583 }
584
585 /**
586  * cairo_set_operator:
587  * @cr: a #cairo_t
588  * @op: a compositing operator, specified as a #cairo_operator_t
589  *
590  * Sets the compositing operator to be used for all drawing
591  * operations. See #cairo_operator_t for details on the semantics of
592  * each available compositing operator.
593  *
594  * The default operator is %CAIRO_OPERATOR_OVER.
595  **/
596 void
597 cairo_set_operator (cairo_t *cr, cairo_operator_t op)
598 {
599     cairo_status_t status;
600
601     if (unlikely (cr->status))
602         return;
603
604     status = cr->backend->set_operator (cr, op);
605     if (unlikely (status))
606         _cairo_set_error (cr, status);
607 }
608 slim_hidden_def (cairo_set_operator);
609
610
611 #if 0
612 /**
613  * cairo_set_opacity:
614  * @cr: a #cairo_t
615  * @opacity: the level of opacity to use when compositing
616  *
617  * Sets the compositing opacity to be used for all drawing
618  * operations. The effect is to fade out the operations
619  * using the alpha value.
620  *
621  * The default opacity is 1.
622  **/
623 void
624 cairo_set_opacity (cairo_t *cr, double opacity)
625 {
626     cairo_status_t status;
627
628     if (unlikely (cr->status))
629         return;
630
631     status = cr->backend->set_opacity (cr, opacity);
632     if (unlikely (status))
633         _cairo_set_error (cr, status);
634 }
635 #endif
636
637 /**
638  * cairo_set_source_rgb
639  * @cr: a cairo context
640  * @red: red component of color
641  * @green: green component of color
642  * @blue: blue component of color
643  *
644  * Sets the source pattern within @cr to an opaque color. This opaque
645  * color will then be used for any subsequent drawing operation until
646  * a new source pattern is set.
647  *
648  * The color components are floating point numbers in the range 0 to
649  * 1. If the values passed in are outside that range, they will be
650  * clamped.
651  *
652  * The default source pattern is opaque black, (that is, it is
653  * equivalent to cairo_set_source_rgb(cr, 0.0, 0.0, 0.0)).
654  **/
655 void
656 cairo_set_source_rgb (cairo_t *cr, double red, double green, double blue)
657 {
658     cairo_status_t status;
659
660     if (unlikely (cr->status))
661         return;
662
663     status = cr->backend->set_source_rgba (cr, red, green, blue, 1.);
664     if (unlikely (status))
665         _cairo_set_error (cr, status);
666 }
667 slim_hidden_def (cairo_set_source_rgb);
668
669 /**
670  * cairo_set_source_rgba:
671  * @cr: a cairo context
672  * @red: red component of color
673  * @green: green component of color
674  * @blue: blue component of color
675  * @alpha: alpha component of color
676  *
677  * Sets the source pattern within @cr to a translucent color. This
678  * color will then be used for any subsequent drawing operation until
679  * a new source pattern is set.
680  *
681  * The color and alpha components are floating point numbers in the
682  * range 0 to 1. If the values passed in are outside that range, they
683  * will be clamped.
684  *
685  * The default source pattern is opaque black, (that is, it is
686  * equivalent to cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 1.0)).
687  **/
688 void
689 cairo_set_source_rgba (cairo_t *cr,
690                        double red, double green, double blue,
691                        double alpha)
692 {
693     cairo_status_t status;
694
695     if (unlikely (cr->status))
696         return;
697
698     status = cr->backend->set_source_rgba (cr, red, green, blue, alpha);
699     if (unlikely (status))
700         _cairo_set_error (cr, status);
701 }
702
703 /**
704  * cairo_set_source_surface:
705  * @cr: a cairo context
706  * @surface: a surface to be used to set the source pattern
707  * @x: User-space X coordinate for surface origin
708  * @y: User-space Y coordinate for surface origin
709  *
710  * This is a convenience function for creating a pattern from @surface
711  * and setting it as the source in @cr with cairo_set_source().
712  *
713  * The @x and @y parameters give the user-space coordinate at which
714  * the surface origin should appear. (The surface origin is its
715  * upper-left corner before any transformation has been applied.) The
716  * @x and @y parameters are negated and then set as translation values
717  * in the pattern matrix.
718  *
719  * Other than the initial translation pattern matrix, as described
720  * above, all other pattern attributes, (such as its extend mode), are
721  * set to the default values as in cairo_pattern_create_for_surface().
722  * The resulting pattern can be queried with cairo_get_source() so
723  * that these attributes can be modified if desired, (eg. to create a
724  * repeating pattern with cairo_pattern_set_extend()).
725  **/
726 void
727 cairo_set_source_surface (cairo_t         *cr,
728                           cairo_surface_t *surface,
729                           double           x,
730                           double           y)
731 {
732     cairo_status_t status;
733
734     if (unlikely (cr->status))
735         return;
736
737     if (unlikely (surface == NULL)) {
738         _cairo_set_error (cr, CAIRO_STATUS_NULL_POINTER);
739         return;
740     }
741
742     status = cr->backend->set_source_surface (cr, surface, x, y);
743     if (unlikely (status))
744         _cairo_set_error (cr, status);
745 }
746 slim_hidden_def (cairo_set_source_surface);
747
748 /**
749  * cairo_set_source
750  * @cr: a cairo context
751  * @source: a #cairo_pattern_t to be used as the source for
752  * subsequent drawing operations.
753  *
754  * Sets the source pattern within @cr to @source. This pattern
755  * will then be used for any subsequent drawing operation until a new
756  * source pattern is set.
757  *
758  * Note: The pattern's transformation matrix will be locked to the
759  * user space in effect at the time of cairo_set_source(). This means
760  * that further modifications of the current transformation matrix
761  * will not affect the source pattern. See cairo_pattern_set_matrix().
762  *
763  * The default source pattern is a solid pattern that is opaque black,
764  * (that is, it is equivalent to cairo_set_source_rgb(cr, 0.0, 0.0,
765  * 0.0)).
766  **/
767 void
768 cairo_set_source (cairo_t *cr, cairo_pattern_t *source)
769 {
770     cairo_status_t status;
771
772     if (unlikely (cr->status))
773         return;
774
775     if (unlikely (source == NULL)) {
776         _cairo_set_error (cr, CAIRO_STATUS_NULL_POINTER);
777         return;
778     }
779
780     if (unlikely (source->status)) {
781         _cairo_set_error (cr, source->status);
782         return;
783     }
784
785     status = cr->backend->set_source (cr, source);
786     if (unlikely (status))
787         _cairo_set_error (cr, status);
788 }
789 slim_hidden_def (cairo_set_source);
790
791 /**
792  * cairo_get_source:
793  * @cr: a cairo context
794  *
795  * Gets the current source pattern for @cr.
796  *
797  * Return value: the current source pattern. This object is owned by
798  * cairo. To keep a reference to it, you must call
799  * cairo_pattern_reference().
800  **/
801 cairo_pattern_t *
802 cairo_get_source (cairo_t *cr)
803 {
804     if (unlikely (cr->status))
805         return _cairo_pattern_create_in_error (cr->status);
806
807     return cr->backend->get_source (cr);
808 }
809
810 /**
811  * cairo_set_tolerance:
812  * @cr: a #cairo_t
813  * @tolerance: the tolerance, in device units (typically pixels)
814  *
815  * Sets the tolerance used when converting paths into trapezoids.
816  * Curved segments of the path will be subdivided until the maximum
817  * deviation between the original path and the polygonal approximation
818  * is less than @tolerance. The default value is 0.1. A larger
819  * value will give better performance, a smaller value, better
820  * appearance. (Reducing the value from the default value of 0.1
821  * is unlikely to improve appearance significantly.)  The accuracy of paths
822  * within Cairo is limited by the precision of its internal arithmetic, and
823  * the prescribed @tolerance is restricted to the smallest
824  * representable internal value.
825  **/
826 void
827 cairo_set_tolerance (cairo_t *cr, double tolerance)
828 {
829     cairo_status_t status;
830
831     if (unlikely (cr->status))
832         return;
833
834     status = cr->backend->set_tolerance (cr, tolerance);
835     if (unlikely (status))
836         _cairo_set_error (cr, status);
837 }
838 slim_hidden_def (cairo_set_tolerance);
839
840 /**
841  * cairo_set_antialias:
842  * @cr: a #cairo_t
843  * @antialias: the new antialiasing mode
844  *
845  * Set the antialiasing mode of the rasterizer used for drawing shapes.
846  * This value is a hint, and a particular backend may or may not support
847  * a particular value.  At the current time, no backend supports
848  * %CAIRO_ANTIALIAS_SUBPIXEL when drawing shapes.
849  *
850  * Note that this option does not affect text rendering, instead see
851  * cairo_font_options_set_antialias().
852  **/
853 void
854 cairo_set_antialias (cairo_t *cr, cairo_antialias_t antialias)
855 {
856     cairo_status_t status;
857
858     if (unlikely (cr->status))
859         return;
860
861     status = cr->backend->set_antialias (cr, antialias);
862     if (unlikely (status))
863         _cairo_set_error (cr, status);
864 }
865
866 /**
867  * cairo_set_fill_rule:
868  * @cr: a #cairo_t
869  * @fill_rule: a fill rule, specified as a #cairo_fill_rule_t
870  *
871  * Set the current fill rule within the cairo context. The fill rule
872  * is used to determine which regions are inside or outside a complex
873  * (potentially self-intersecting) path. The current fill rule affects
874  * both cairo_fill() and cairo_clip(). See #cairo_fill_rule_t for details
875  * on the semantics of each available fill rule.
876  *
877  * The default fill rule is %CAIRO_FILL_RULE_WINDING.
878  **/
879 void
880 cairo_set_fill_rule (cairo_t *cr, cairo_fill_rule_t fill_rule)
881 {
882     cairo_status_t status;
883
884     if (unlikely (cr->status))
885         return;
886
887     status = cr->backend->set_fill_rule (cr, fill_rule);
888     if (unlikely (status))
889         _cairo_set_error (cr, status);
890 }
891
892 /**
893  * cairo_set_line_width:
894  * @cr: a #cairo_t
895  * @width: a line width
896  *
897  * Sets the current line width within the cairo context. The line
898  * width value specifies the diameter of a pen that is circular in
899  * user space, (though device-space pen may be an ellipse in general
900  * due to scaling/shear/rotation of the CTM).
901  *
902  * Note: When the description above refers to user space and CTM it
903  * refers to the user space and CTM in effect at the time of the
904  * stroking operation, not the user space and CTM in effect at the
905  * time of the call to cairo_set_line_width(). The simplest usage
906  * makes both of these spaces identical. That is, if there is no
907  * change to the CTM between a call to cairo_set_line_width() and the
908  * stroking operation, then one can just pass user-space values to
909  * cairo_set_line_width() and ignore this note.
910  *
911  * As with the other stroke parameters, the current line width is
912  * examined by cairo_stroke(), cairo_stroke_extents(), and
913  * cairo_stroke_to_path(), but does not have any effect during path
914  * construction.
915  *
916  * The default line width value is 2.0.
917  **/
918 void
919 cairo_set_line_width (cairo_t *cr, double width)
920 {
921     cairo_status_t status;
922
923     if (unlikely (cr->status))
924         return;
925
926     if (width < 0.)
927         width = 0.;
928
929     status = cr->backend->set_line_width (cr, width);
930     if (unlikely (status))
931         _cairo_set_error (cr, status);
932 }
933 slim_hidden_def (cairo_set_line_width);
934
935 /**
936  * cairo_set_line_cap:
937  * @cr: a cairo context
938  * @line_cap: a line cap style
939  *
940  * Sets the current line cap style within the cairo context. See
941  * #cairo_line_cap_t for details about how the available line cap
942  * styles are drawn.
943  *
944  * As with the other stroke parameters, the current line cap style is
945  * examined by cairo_stroke(), cairo_stroke_extents(), and
946  * cairo_stroke_to_path(), but does not have any effect during path
947  * construction.
948  *
949  * The default line cap style is %CAIRO_LINE_CAP_BUTT.
950  **/
951 void
952 cairo_set_line_cap (cairo_t *cr, cairo_line_cap_t line_cap)
953 {
954     cairo_status_t status;
955
956     if (unlikely (cr->status))
957         return;
958
959     status = cr->backend->set_line_cap (cr, line_cap);
960     if (unlikely (status))
961         _cairo_set_error (cr, status);
962 }
963 slim_hidden_def (cairo_set_line_cap);
964
965 /**
966  * cairo_set_line_join:
967  * @cr: a cairo context
968  * @line_join: a line join style
969  *
970  * Sets the current line join style within the cairo context. See
971  * #cairo_line_join_t for details about how the available line join
972  * styles are drawn.
973  *
974  * As with the other stroke parameters, the current line join style is
975  * examined by cairo_stroke(), cairo_stroke_extents(), and
976  * cairo_stroke_to_path(), but does not have any effect during path
977  * construction.
978  *
979  * The default line join style is %CAIRO_LINE_JOIN_MITER.
980  **/
981 void
982 cairo_set_line_join (cairo_t *cr, cairo_line_join_t line_join)
983 {
984     cairo_status_t status;
985
986     if (unlikely (cr->status))
987         return;
988
989     status = cr->backend->set_line_join (cr, line_join);
990     if (unlikely (status))
991         _cairo_set_error (cr, status);
992 }
993 slim_hidden_def (cairo_set_line_join);
994
995 /**
996  * cairo_set_dash:
997  * @cr: a cairo context
998  * @dashes: an array specifying alternate lengths of on and off stroke portions
999  * @num_dashes: the length of the dashes array
1000  * @offset: an offset into the dash pattern at which the stroke should start
1001  *
1002  * Sets the dash pattern to be used by cairo_stroke(). A dash pattern
1003  * is specified by @dashes, an array of positive values. Each value
1004  * provides the length of alternate "on" and "off" portions of the
1005  * stroke. The @offset specifies an offset into the pattern at which
1006  * the stroke begins.
1007  *
1008  * Each "on" segment will have caps applied as if the segment were a
1009  * separate sub-path. In particular, it is valid to use an "on" length
1010  * of 0.0 with %CAIRO_LINE_CAP_ROUND or %CAIRO_LINE_CAP_SQUARE in order
1011  * to distributed dots or squares along a path.
1012  *
1013  * Note: The length values are in user-space units as evaluated at the
1014  * time of stroking. This is not necessarily the same as the user
1015  * space at the time of cairo_set_dash().
1016  *
1017  * If @num_dashes is 0 dashing is disabled.
1018  *
1019  * If @num_dashes is 1 a symmetric pattern is assumed with alternating
1020  * on and off portions of the size specified by the single value in
1021  * @dashes.
1022  *
1023  * If any value in @dashes is negative, or if all values are 0, then
1024  * @cr will be put into an error state with a status of
1025  * %CAIRO_STATUS_INVALID_DASH.
1026  **/
1027 void
1028 cairo_set_dash (cairo_t      *cr,
1029                 const double *dashes,
1030                 int           num_dashes,
1031                 double        offset)
1032 {
1033     cairo_status_t status;
1034
1035     if (unlikely (cr->status))
1036         return;
1037
1038     status = cr->backend->set_dash (cr, dashes, num_dashes, offset);
1039     if (unlikely (status))
1040         _cairo_set_error (cr, status);
1041 }
1042
1043 /**
1044  * cairo_get_dash_count:
1045  * @cr: a #cairo_t
1046  *
1047  * This function returns the length of the dash array in @cr (0 if dashing
1048  * is not currently in effect).
1049  *
1050  * See also cairo_set_dash() and cairo_get_dash().
1051  *
1052  * Return value: the length of the dash array, or 0 if no dash array set.
1053  *
1054  * Since: 1.4
1055  */
1056 int
1057 cairo_get_dash_count (cairo_t *cr)
1058 {
1059     int num_dashes;
1060
1061     if (unlikely (cr->status))
1062         return 0;
1063
1064     cr->backend->get_dash (cr, NULL, &num_dashes, NULL);
1065
1066     return num_dashes;
1067 }
1068
1069 /**
1070  * cairo_get_dash:
1071  * @cr: a #cairo_t
1072  * @dashes: return value for the dash array, or %NULL
1073  * @offset: return value for the current dash offset, or %NULL
1074  *
1075  * Gets the current dash array.  If not %NULL, @dashes should be big
1076  * enough to hold at least the number of values returned by
1077  * cairo_get_dash_count().
1078  *
1079  * Since: 1.4
1080  **/
1081 void
1082 cairo_get_dash (cairo_t *cr,
1083                 double  *dashes,
1084                 double  *offset)
1085 {
1086     if (unlikely (cr->status))
1087         return;
1088
1089     cr->backend->get_dash (cr, dashes, NULL, offset);
1090 }
1091
1092 /**
1093  * cairo_set_miter_limit:
1094  * @cr: a cairo context
1095  * @limit: miter limit to set
1096  *
1097  * Sets the current miter limit within the cairo context.
1098  *
1099  * If the current line join style is set to %CAIRO_LINE_JOIN_MITER
1100  * (see cairo_set_line_join()), the miter limit is used to determine
1101  * whether the lines should be joined with a bevel instead of a miter.
1102  * Cairo divides the length of the miter by the line width.
1103  * If the result is greater than the miter limit, the style is
1104  * converted to a bevel.
1105  *
1106  * As with the other stroke parameters, the current line miter limit is
1107  * examined by cairo_stroke(), cairo_stroke_extents(), and
1108  * cairo_stroke_to_path(), but does not have any effect during path
1109  * construction.
1110  *
1111  * The default miter limit value is 10.0, which will convert joins
1112  * with interior angles less than 11 degrees to bevels instead of
1113  * miters. For reference, a miter limit of 2.0 makes the miter cutoff
1114  * at 60 degrees, and a miter limit of 1.414 makes the cutoff at 90
1115  * degrees.
1116  *
1117  * A miter limit for a desired angle can be computed as: miter limit =
1118  * 1/sin(angle/2)
1119  **/
1120 void
1121 cairo_set_miter_limit (cairo_t *cr, double limit)
1122 {
1123     cairo_status_t status;
1124
1125     if (unlikely (cr->status))
1126         return;
1127
1128     status = cr->backend->set_miter_limit (cr, limit);
1129     if (unlikely (status))
1130         _cairo_set_error (cr, status);
1131 }
1132
1133 /**
1134  * cairo_translate:
1135  * @cr: a cairo context
1136  * @tx: amount to translate in the X direction
1137  * @ty: amount to translate in the Y direction
1138  *
1139  * Modifies the current transformation matrix (CTM) by translating the
1140  * user-space origin by (@tx, @ty). This offset is interpreted as a
1141  * user-space coordinate according to the CTM in place before the new
1142  * call to cairo_translate(). In other words, the translation of the
1143  * user-space origin takes place after any existing transformation.
1144  **/
1145 void
1146 cairo_translate (cairo_t *cr, double tx, double ty)
1147 {
1148     cairo_status_t status;
1149
1150     if (unlikely (cr->status))
1151         return;
1152
1153     status = cr->backend->translate (cr, tx, ty);
1154     if (unlikely (status))
1155         _cairo_set_error (cr, status);
1156 }
1157 slim_hidden_def (cairo_translate);
1158
1159 /**
1160  * cairo_scale:
1161  * @cr: a cairo context
1162  * @sx: scale factor for the X dimension
1163  * @sy: scale factor for the Y dimension
1164  *
1165  * Modifies the current transformation matrix (CTM) by scaling the X
1166  * and Y user-space axes by @sx and @sy respectively. The scaling of
1167  * the axes takes place after any existing transformation of user
1168  * space.
1169  **/
1170 void
1171 cairo_scale (cairo_t *cr, double sx, double sy)
1172 {
1173     cairo_status_t status;
1174
1175     if (unlikely (cr->status))
1176         return;
1177
1178     status = cr->backend->scale (cr, sx, sy);
1179     if (unlikely (status))
1180         _cairo_set_error (cr, status);
1181 }
1182 slim_hidden_def (cairo_scale);
1183
1184 /**
1185  * cairo_rotate:
1186  * @cr: a cairo context
1187  * @angle: angle (in radians) by which the user-space axes will be
1188  * rotated
1189  *
1190  * Modifies the current transformation matrix (CTM) by rotating the
1191  * user-space axes by @angle radians. The rotation of the axes takes
1192  * places after any existing transformation of user space. The
1193  * rotation direction for positive angles is from the positive X axis
1194  * toward the positive Y axis.
1195  **/
1196 void
1197 cairo_rotate (cairo_t *cr, double angle)
1198 {
1199     cairo_status_t status;
1200
1201     if (unlikely (cr->status))
1202         return;
1203
1204     status = cr->backend->rotate (cr, angle);
1205     if (unlikely (status))
1206         _cairo_set_error (cr, status);
1207 }
1208
1209 /**
1210  * cairo_transform:
1211  * @cr: a cairo context
1212  * @matrix: a transformation to be applied to the user-space axes
1213  *
1214  * Modifies the current transformation matrix (CTM) by applying
1215  * @matrix as an additional transformation. The new transformation of
1216  * user space takes place after any existing transformation.
1217  **/
1218 void
1219 cairo_transform (cairo_t              *cr,
1220                  const cairo_matrix_t *matrix)
1221 {
1222     cairo_status_t status;
1223
1224     if (unlikely (cr->status))
1225         return;
1226
1227     status = cr->backend->transform (cr, matrix);
1228     if (unlikely (status))
1229         _cairo_set_error (cr, status);
1230 }
1231 slim_hidden_def (cairo_transform);
1232
1233 /**
1234  * cairo_set_matrix:
1235  * @cr: a cairo context
1236  * @matrix: a transformation matrix from user space to device space
1237  *
1238  * Modifies the current transformation matrix (CTM) by setting it
1239  * equal to @matrix.
1240  **/
1241 void
1242 cairo_set_matrix (cairo_t              *cr,
1243                   const cairo_matrix_t *matrix)
1244 {
1245     cairo_status_t status;
1246
1247     if (unlikely (cr->status))
1248         return;
1249
1250     status = cr->backend->set_matrix (cr, matrix);
1251     if (unlikely (status))
1252         _cairo_set_error (cr, status);
1253 }
1254 slim_hidden_def (cairo_set_matrix);
1255
1256 /**
1257  * cairo_identity_matrix:
1258  * @cr: a cairo context
1259  *
1260  * Resets the current transformation matrix (CTM) by setting it equal
1261  * to the identity matrix. That is, the user-space and device-space
1262  * axes will be aligned and one user-space unit will transform to one
1263  * device-space unit.
1264  **/
1265 void
1266 cairo_identity_matrix (cairo_t *cr)
1267 {
1268     cairo_status_t status;
1269
1270     if (unlikely (cr->status))
1271         return;
1272
1273     status = cr->backend->set_identity_matrix (cr);
1274     if (unlikely (status))
1275         _cairo_set_error (cr, status);
1276 }
1277
1278 /**
1279  * cairo_user_to_device:
1280  * @cr: a cairo context
1281  * @x: X value of coordinate (in/out parameter)
1282  * @y: Y value of coordinate (in/out parameter)
1283  *
1284  * Transform a coordinate from user space to device space by
1285  * multiplying the given point by the current transformation matrix
1286  * (CTM).
1287  **/
1288 void
1289 cairo_user_to_device (cairo_t *cr, double *x, double *y)
1290 {
1291     if (unlikely (cr->status))
1292         return;
1293
1294     cr->backend->user_to_device (cr, x, y);
1295 }
1296 slim_hidden_def (cairo_user_to_device);
1297
1298 /**
1299  * cairo_user_to_device_distance:
1300  * @cr: a cairo context
1301  * @dx: X component of a distance vector (in/out parameter)
1302  * @dy: Y component of a distance vector (in/out parameter)
1303  *
1304  * Transform a distance vector from user space to device space. This
1305  * function is similar to cairo_user_to_device() except that the
1306  * translation components of the CTM will be ignored when transforming
1307  * (@dx,@dy).
1308  **/
1309 void
1310 cairo_user_to_device_distance (cairo_t *cr, double *dx, double *dy)
1311 {
1312     if (unlikely (cr->status))
1313         return;
1314
1315     cr->backend->user_to_device_distance (cr, dx, dy);
1316 }
1317 slim_hidden_def (cairo_user_to_device_distance);
1318
1319 /**
1320  * cairo_device_to_user:
1321  * @cr: a cairo
1322  * @x: X value of coordinate (in/out parameter)
1323  * @y: Y value of coordinate (in/out parameter)
1324  *
1325  * Transform a coordinate from device space to user space by
1326  * multiplying the given point by the inverse of the current
1327  * transformation matrix (CTM).
1328  **/
1329 void
1330 cairo_device_to_user (cairo_t *cr, double *x, double *y)
1331 {
1332     if (unlikely (cr->status))
1333         return;
1334
1335     cr->backend->device_to_user (cr, x, y);
1336 }
1337 slim_hidden_def (cairo_device_to_user);
1338
1339 /**
1340  * cairo_device_to_user_distance:
1341  * @cr: a cairo context
1342  * @dx: X component of a distance vector (in/out parameter)
1343  * @dy: Y component of a distance vector (in/out parameter)
1344  *
1345  * Transform a distance vector from device space to user space. This
1346  * function is similar to cairo_device_to_user() except that the
1347  * translation components of the inverse CTM will be ignored when
1348  * transforming (@dx,@dy).
1349  **/
1350 void
1351 cairo_device_to_user_distance (cairo_t *cr, double *dx, double *dy)
1352 {
1353     if (unlikely (cr->status))
1354         return;
1355
1356     cr->backend->device_to_user_distance (cr, dx, dy);
1357 }
1358
1359 /**
1360  * cairo_new_path:
1361  * @cr: a cairo context
1362  *
1363  * Clears the current path. After this call there will be no path and
1364  * no current point.
1365  **/
1366 void
1367 cairo_new_path (cairo_t *cr)
1368 {
1369     cairo_status_t status;
1370
1371     if (unlikely (cr->status))
1372         return;
1373
1374     status = cr->backend->new_path (cr);
1375     if (unlikely (status))
1376         _cairo_set_error (cr, status);
1377 }
1378 slim_hidden_def(cairo_new_path);
1379
1380 /**
1381  * cairo_new_sub_path:
1382  * @cr: a cairo context
1383  *
1384  * Begin a new sub-path. Note that the existing path is not
1385  * affected. After this call there will be no current point.
1386  *
1387  * In many cases, this call is not needed since new sub-paths are
1388  * frequently started with cairo_move_to().
1389  *
1390  * A call to cairo_new_sub_path() is particularly useful when
1391  * beginning a new sub-path with one of the cairo_arc() calls. This
1392  * makes things easier as it is no longer necessary to manually
1393  * compute the arc's initial coordinates for a call to
1394  * cairo_move_to().
1395  *
1396  * Since: 1.2
1397  **/
1398 void
1399 cairo_new_sub_path (cairo_t *cr)
1400 {
1401     cairo_status_t status;
1402
1403     if (unlikely (cr->status))
1404         return;
1405
1406     status = cr->backend->new_sub_path (cr);
1407     if (unlikely (status))
1408         _cairo_set_error (cr, status);
1409 }
1410
1411 /**
1412  * cairo_move_to:
1413  * @cr: a cairo context
1414  * @x: the X coordinate of the new position
1415  * @y: the Y coordinate of the new position
1416  *
1417  * Begin a new sub-path. After this call the current point will be (@x,
1418  * @y).
1419  **/
1420 void
1421 cairo_move_to (cairo_t *cr, double x, double y)
1422 {
1423     cairo_status_t status;
1424
1425     if (unlikely (cr->status))
1426         return;
1427
1428     status = cr->backend->move_to (cr, x, y);
1429     if (unlikely (status))
1430         _cairo_set_error (cr, status);
1431 }
1432 slim_hidden_def(cairo_move_to);
1433
1434
1435 /**
1436  * cairo_line_to:
1437  * @cr: a cairo context
1438  * @x: the X coordinate of the end of the new line
1439  * @y: the Y coordinate of the end of the new line
1440  *
1441  * Adds a line to the path from the current point to position (@x, @y)
1442  * in user-space coordinates. After this call the current point
1443  * will be (@x, @y).
1444  *
1445  * If there is no current point before the call to cairo_line_to()
1446  * this function will behave as cairo_move_to(@cr, @x, @y).
1447  **/
1448 void
1449 cairo_line_to (cairo_t *cr, double x, double y)
1450 {
1451     cairo_status_t status;
1452
1453     if (unlikely (cr->status))
1454         return;
1455
1456     status = cr->backend->line_to (cr, x, y);
1457     if (unlikely (status))
1458         _cairo_set_error (cr, status);
1459 }
1460 slim_hidden_def (cairo_line_to);
1461
1462 /**
1463  * cairo_curve_to:
1464  * @cr: a cairo context
1465  * @x1: the X coordinate of the first control point
1466  * @y1: the Y coordinate of the first control point
1467  * @x2: the X coordinate of the second control point
1468  * @y2: the Y coordinate of the second control point
1469  * @x3: the X coordinate of the end of the curve
1470  * @y3: the Y coordinate of the end of the curve
1471  *
1472  * Adds a cubic Bézier spline to the path from the current point to
1473  * position (@x3, @y3) in user-space coordinates, using (@x1, @y1) and
1474  * (@x2, @y2) as the control points. After this call the current point
1475  * will be (@x3, @y3).
1476  *
1477  * If there is no current point before the call to cairo_curve_to()
1478  * this function will behave as if preceded by a call to
1479  * cairo_move_to(@cr, @x1, @y1).
1480  **/
1481 void
1482 cairo_curve_to (cairo_t *cr,
1483                 double x1, double y1,
1484                 double x2, double y2,
1485                 double x3, double y3)
1486 {
1487     cairo_status_t status;
1488
1489     if (unlikely (cr->status))
1490         return;
1491
1492     status = cr->backend->curve_to (cr,
1493                                     x1, y1,
1494                                     x2, y2,
1495                                     x3, y3);
1496     if (unlikely (status))
1497         _cairo_set_error (cr, status);
1498 }
1499 slim_hidden_def (cairo_curve_to);
1500
1501 /**
1502  * cairo_arc:
1503  * @cr: a cairo context
1504  * @xc: X position of the center of the arc
1505  * @yc: Y position of the center of the arc
1506  * @radius: the radius of the arc
1507  * @angle1: the start angle, in radians
1508  * @angle2: the end angle, in radians
1509  *
1510  * Adds a circular arc of the given @radius to the current path.  The
1511  * arc is centered at (@xc, @yc), begins at @angle1 and proceeds in
1512  * the direction of increasing angles to end at @angle2. If @angle2 is
1513  * less than @angle1 it will be progressively increased by
1514  * <literal>2*M_PI</literal> until it is greater than @angle1.
1515  *
1516  * If there is a current point, an initial line segment will be added
1517  * to the path to connect the current point to the beginning of the
1518  * arc. If this initial line is undesired, it can be avoided by
1519  * calling cairo_new_sub_path() before calling cairo_arc().
1520  *
1521  * Angles are measured in radians. An angle of 0.0 is in the direction
1522  * of the positive X axis (in user space). An angle of
1523  * <literal>M_PI/2.0</literal> radians (90 degrees) is in the
1524  * direction of the positive Y axis (in user space). Angles increase
1525  * in the direction from the positive X axis toward the positive Y
1526  * axis. So with the default transformation matrix, angles increase in
1527  * a clockwise direction.
1528  *
1529  * (To convert from degrees to radians, use <literal>degrees * (M_PI /
1530  * 180.)</literal>.)
1531  *
1532  * This function gives the arc in the direction of increasing angles;
1533  * see cairo_arc_negative() to get the arc in the direction of
1534  * decreasing angles.
1535  *
1536  * The arc is circular in user space. To achieve an elliptical arc,
1537  * you can scale the current transformation matrix by different
1538  * amounts in the X and Y directions. For example, to draw an ellipse
1539  * in the box given by @x, @y, @width, @height:
1540  *
1541  * <informalexample><programlisting>
1542  * cairo_save (cr);
1543  * cairo_translate (cr, x + width / 2., y + height / 2.);
1544  * cairo_scale (cr, width / 2., height / 2.);
1545  * cairo_arc (cr, 0., 0., 1., 0., 2 * M_PI);
1546  * cairo_restore (cr);
1547  * </programlisting></informalexample>
1548  **/
1549 void
1550 cairo_arc (cairo_t *cr,
1551            double xc, double yc,
1552            double radius,
1553            double angle1, double angle2)
1554 {
1555     cairo_status_t status;
1556
1557     if (unlikely (cr->status))
1558         return;
1559
1560     if (angle2 < angle1) {
1561         /* increase angle2 by multiples of full circle until it
1562          * satisfies angle2 >= angle1 */
1563         angle2 = fmod (angle2 - angle1, 2 * M_PI);
1564         if (angle2 < 0)
1565             angle2 += 2 * M_PI;
1566         angle2 += angle1;
1567     }
1568
1569     status = cr->backend->arc (cr, xc, yc, radius, angle1, angle2, TRUE);
1570     if (unlikely (status))
1571         _cairo_set_error (cr, status);
1572 }
1573
1574 /**
1575  * cairo_arc_negative:
1576  * @cr: a cairo context
1577  * @xc: X position of the center of the arc
1578  * @yc: Y position of the center of the arc
1579  * @radius: the radius of the arc
1580  * @angle1: the start angle, in radians
1581  * @angle2: the end angle, in radians
1582  *
1583  * Adds a circular arc of the given @radius to the current path.  The
1584  * arc is centered at (@xc, @yc), begins at @angle1 and proceeds in
1585  * the direction of decreasing angles to end at @angle2. If @angle2 is
1586  * greater than @angle1 it will be progressively decreased by
1587  * <literal>2*M_PI</literal> until it is less than @angle1.
1588  *
1589  * See cairo_arc() for more details. This function differs only in the
1590  * direction of the arc between the two angles.
1591  **/
1592 void
1593 cairo_arc_negative (cairo_t *cr,
1594                     double xc, double yc,
1595                     double radius,
1596                     double angle1, double angle2)
1597 {
1598     cairo_status_t status;
1599
1600     if (unlikely (cr->status))
1601         return;
1602
1603     if (angle2 > angle1) {
1604         /* decrease angle2 by multiples of full circle until it
1605          * satisfies angle2 <= angle1 */
1606         angle2 = fmod (angle2 - angle1, 2 * M_PI);
1607         if (angle2 > 0)
1608             angle2 -= 2 * M_PI;
1609         angle2 += angle1;
1610     }
1611
1612     status = cr->backend->arc (cr, xc, yc, radius, angle1, angle2, FALSE);
1613     if (unlikely (status))
1614         _cairo_set_error (cr, status);
1615 }
1616
1617 /* XXX: NYI
1618 void
1619 cairo_arc_to (cairo_t *cr,
1620               double x1, double y1,
1621               double x2, double y2,
1622               double radius)
1623 {
1624     cairo_status_t status;
1625
1626     if (unlikely (cr->status))
1627         return;
1628
1629     status = cr->backend->arc_to (cr, x1, y1, x2, y2, radius);
1630     if (unlikely (status))
1631         _cairo_set_error (cr, status);
1632 }
1633
1634 void
1635 cairo_rel_arc_to (cairo_t *cr,
1636               double dx1, double dy1,
1637               double dx2, double dy2,
1638               double radius)
1639 {
1640     cairo_status_t status;
1641
1642     if (unlikely (cr->status))
1643         return;
1644
1645     status = cr->backend->rel_arc_to (cr, dx1, dy1, dx2, dy2, radius);
1646     if (unlikely (status))
1647         _cairo_set_error (cr, status);
1648 }
1649 */
1650
1651 /**
1652  * cairo_rel_move_to:
1653  * @cr: a cairo context
1654  * @dx: the X offset
1655  * @dy: the Y offset
1656  *
1657  * Begin a new sub-path. After this call the current point will offset
1658  * by (@x, @y).
1659  *
1660  * Given a current point of (x, y), cairo_rel_move_to(@cr, @dx, @dy)
1661  * is logically equivalent to cairo_move_to(@cr, x + @dx, y + @dy).
1662  *
1663  * It is an error to call this function with no current point. Doing
1664  * so will cause @cr to shutdown with a status of
1665  * %CAIRO_STATUS_NO_CURRENT_POINT.
1666  **/
1667 void
1668 cairo_rel_move_to (cairo_t *cr, double dx, double dy)
1669 {
1670     cairo_status_t status;
1671
1672     if (unlikely (cr->status))
1673         return;
1674
1675     status = cr->backend->rel_move_to (cr, dx, dy);
1676     if (unlikely (status))
1677         _cairo_set_error (cr, status);
1678 }
1679
1680 /**
1681  * cairo_rel_line_to:
1682  * @cr: a cairo context
1683  * @dx: the X offset to the end of the new line
1684  * @dy: the Y offset to the end of the new line
1685  *
1686  * Relative-coordinate version of cairo_line_to(). Adds a line to the
1687  * path from the current point to a point that is offset from the
1688  * current point by (@dx, @dy) in user space. After this call the
1689  * current point will be offset by (@dx, @dy).
1690  *
1691  * Given a current point of (x, y), cairo_rel_line_to(@cr, @dx, @dy)
1692  * is logically equivalent to cairo_line_to(@cr, x + @dx, y + @dy).
1693  *
1694  * It is an error to call this function with no current point. Doing
1695  * so will cause @cr to shutdown with a status of
1696  * %CAIRO_STATUS_NO_CURRENT_POINT.
1697  **/
1698 void
1699 cairo_rel_line_to (cairo_t *cr, double dx, double dy)
1700 {
1701     cairo_status_t status;
1702
1703     if (unlikely (cr->status))
1704         return;
1705
1706     status = cr->backend->rel_line_to (cr, dx, dy);
1707     if (unlikely (status))
1708         _cairo_set_error (cr, status);
1709 }
1710 slim_hidden_def(cairo_rel_line_to);
1711
1712 /**
1713  * cairo_rel_curve_to:
1714  * @cr: a cairo context
1715  * @dx1: the X offset to the first control point
1716  * @dy1: the Y offset to the first control point
1717  * @dx2: the X offset to the second control point
1718  * @dy2: the Y offset to the second control point
1719  * @dx3: the X offset to the end of the curve
1720  * @dy3: the Y offset to the end of the curve
1721  *
1722  * Relative-coordinate version of cairo_curve_to(). All offsets are
1723  * relative to the current point. Adds a cubic Bézier spline to the
1724  * path from the current point to a point offset from the current
1725  * point by (@dx3, @dy3), using points offset by (@dx1, @dy1) and
1726  * (@dx2, @dy2) as the control points. After this call the current
1727  * point will be offset by (@dx3, @dy3).
1728  *
1729  * Given a current point of (x, y), cairo_rel_curve_to(@cr, @dx1,
1730  * @dy1, @dx2, @dy2, @dx3, @dy3) is logically equivalent to
1731  * cairo_curve_to(@cr, x+@dx1, y+@dy1, x+@dx2, y+@dy2, x+@dx3, y+@dy3).
1732  *
1733  * It is an error to call this function with no current point. Doing
1734  * so will cause @cr to shutdown with a status of
1735  * %CAIRO_STATUS_NO_CURRENT_POINT.
1736  **/
1737 void
1738 cairo_rel_curve_to (cairo_t *cr,
1739                     double dx1, double dy1,
1740                     double dx2, double dy2,
1741                     double dx3, double dy3)
1742 {
1743     cairo_status_t status;
1744
1745     if (unlikely (cr->status))
1746         return;
1747
1748     status = cr->backend->rel_curve_to (cr,
1749                                         dx1, dy1,
1750                                         dx2, dy2,
1751                                         dx3, dy3);
1752     if (unlikely (status))
1753         _cairo_set_error (cr, status);
1754 }
1755
1756 /**
1757  * cairo_rectangle:
1758  * @cr: a cairo context
1759  * @x: the X coordinate of the top left corner of the rectangle
1760  * @y: the Y coordinate to the top left corner of the rectangle
1761  * @width: the width of the rectangle
1762  * @height: the height of the rectangle
1763  *
1764  * Adds a closed sub-path rectangle of the given size to the current
1765  * path at position (@x, @y) in user-space coordinates.
1766  *
1767  * This function is logically equivalent to:
1768  * <informalexample><programlisting>
1769  * cairo_move_to (cr, x, y);
1770  * cairo_rel_line_to (cr, width, 0);
1771  * cairo_rel_line_to (cr, 0, height);
1772  * cairo_rel_line_to (cr, -width, 0);
1773  * cairo_close_path (cr);
1774  * </programlisting></informalexample>
1775  **/
1776 void
1777 cairo_rectangle (cairo_t *cr,
1778                  double x, double y,
1779                  double width, double height)
1780 {
1781     cairo_status_t status;
1782
1783     if (unlikely (cr->status))
1784         return;
1785
1786     status = cr->backend->rectangle (cr, x, y, width, height);
1787     if (unlikely (status))
1788         _cairo_set_error (cr, status);
1789 }
1790
1791 #if 0
1792 /* XXX: NYI */
1793 void
1794 cairo_stroke_to_path (cairo_t *cr)
1795 {
1796     cairo_status_t status;
1797
1798     if (unlikely (cr->status))
1799         return;
1800
1801     /* The code in _cairo_recording_surface_get_path has a poorman's stroke_to_path */
1802
1803     status = _cairo_gstate_stroke_path (cr->gstate);
1804     if (unlikely (status))
1805         _cairo_set_error (cr, status);
1806 }
1807 #endif
1808
1809 /**
1810  * cairo_close_path:
1811  * @cr: a cairo context
1812  *
1813  * Adds a line segment to the path from the current point to the
1814  * beginning of the current sub-path, (the most recent point passed to
1815  * cairo_move_to()), and closes this sub-path. After this call the
1816  * current point will be at the joined endpoint of the sub-path.
1817  *
1818  * The behavior of cairo_close_path() is distinct from simply calling
1819  * cairo_line_to() with the equivalent coordinate in the case of
1820  * stroking. When a closed sub-path is stroked, there are no caps on
1821  * the ends of the sub-path. Instead, there is a line join connecting
1822  * the final and initial segments of the sub-path.
1823  *
1824  * If there is no current point before the call to cairo_close_path(),
1825  * this function will have no effect.
1826  *
1827  * Note: As of cairo version 1.2.4 any call to cairo_close_path() will
1828  * place an explicit MOVE_TO element into the path immediately after
1829  * the CLOSE_PATH element, (which can be seen in cairo_copy_path() for
1830  * example). This can simplify path processing in some cases as it may
1831  * not be necessary to save the "last move_to point" during processing
1832  * as the MOVE_TO immediately after the CLOSE_PATH will provide that
1833  * point.
1834  **/
1835 void
1836 cairo_close_path (cairo_t *cr)
1837 {
1838     cairo_status_t status;
1839
1840     if (unlikely (cr->status))
1841         return;
1842
1843     status = cr->backend->close_path (cr);
1844     if (unlikely (status))
1845         _cairo_set_error (cr, status);
1846 }
1847 slim_hidden_def(cairo_close_path);
1848
1849 /**
1850  * cairo_path_extents:
1851  * @cr: a cairo context
1852  * @x1: left of the resulting extents
1853  * @y1: top of the resulting extents
1854  * @x2: right of the resulting extents
1855  * @y2: bottom of the resulting extents
1856  *
1857  * Computes a bounding box in user-space coordinates covering the
1858  * points on the current path. If the current path is empty, returns
1859  * an empty rectangle ((0,0), (0,0)). Stroke parameters, fill rule,
1860  * surface dimensions and clipping are not taken into account.
1861  *
1862  * Contrast with cairo_fill_extents() and cairo_stroke_extents() which
1863  * return the extents of only the area that would be "inked" by
1864  * the corresponding drawing operations.
1865  *
1866  * The result of cairo_path_extents() is defined as equivalent to the
1867  * limit of cairo_stroke_extents() with %CAIRO_LINE_CAP_ROUND as the
1868  * line width approaches 0.0, (but never reaching the empty-rectangle
1869  * returned by cairo_stroke_extents() for a line width of 0.0).
1870  *
1871  * Specifically, this means that zero-area sub-paths such as
1872  * cairo_move_to();cairo_line_to() segments, (even degenerate cases
1873  * where the coordinates to both calls are identical), will be
1874  * considered as contributing to the extents. However, a lone
1875  * cairo_move_to() will not contribute to the results of
1876  * cairo_path_extents().
1877  *
1878  * Since: 1.6
1879  **/
1880 void
1881 cairo_path_extents (cairo_t *cr,
1882                     double *x1, double *y1, double *x2, double *y2)
1883 {
1884     if (unlikely (cr->status)) {
1885         if (x1)
1886             *x1 = 0.0;
1887         if (y1)
1888             *y1 = 0.0;
1889         if (x2)
1890             *x2 = 0.0;
1891         if (y2)
1892             *y2 = 0.0;
1893
1894         return;
1895     }
1896
1897     cr->backend->path_extents (cr, x1, y1, x2, y2);
1898 }
1899
1900 /**
1901  * cairo_paint:
1902  * @cr: a cairo context
1903  *
1904  * A drawing operator that paints the current source everywhere within
1905  * the current clip region.
1906  **/
1907 void
1908 cairo_paint (cairo_t *cr)
1909 {
1910     cairo_status_t status;
1911
1912     if (unlikely (cr->status))
1913         return;
1914
1915     status = cr->backend->paint (cr);
1916     if (unlikely (status))
1917         _cairo_set_error (cr, status);
1918 }
1919 slim_hidden_def (cairo_paint);
1920
1921 /**
1922  * cairo_paint_with_alpha:
1923  * @cr: a cairo context
1924  * @alpha: alpha value, between 0 (transparent) and 1 (opaque)
1925  *
1926  * A drawing operator that paints the current source everywhere within
1927  * the current clip region using a mask of constant alpha value
1928  * @alpha. The effect is similar to cairo_paint(), but the drawing
1929  * is faded out using the alpha value.
1930  **/
1931 void
1932 cairo_paint_with_alpha (cairo_t *cr,
1933                         double   alpha)
1934 {
1935     cairo_status_t status;
1936
1937     if (unlikely (cr->status))
1938         return;
1939
1940     status = cr->backend->paint_with_alpha (cr, alpha);
1941     if (unlikely (status))
1942         _cairo_set_error (cr, status);
1943 }
1944
1945 /**
1946  * cairo_mask:
1947  * @cr: a cairo context
1948  * @pattern: a #cairo_pattern_t
1949  *
1950  * A drawing operator that paints the current source
1951  * using the alpha channel of @pattern as a mask. (Opaque
1952  * areas of @pattern are painted with the source, transparent
1953  * areas are not painted.)
1954  */
1955 void
1956 cairo_mask (cairo_t         *cr,
1957             cairo_pattern_t *pattern)
1958 {
1959     cairo_status_t status;
1960
1961     if (unlikely (cr->status))
1962         return;
1963
1964     if (unlikely (pattern == NULL)) {
1965         _cairo_set_error (cr, CAIRO_STATUS_NULL_POINTER);
1966         return;
1967     }
1968
1969     if (unlikely (pattern->status)) {
1970         _cairo_set_error (cr, pattern->status);
1971         return;
1972     }
1973
1974     status = cr->backend->mask (cr, pattern);
1975     if (unlikely (status))
1976         _cairo_set_error (cr, status);
1977 }
1978 slim_hidden_def (cairo_mask);
1979
1980 /**
1981  * cairo_mask_surface:
1982  * @cr: a cairo context
1983  * @surface: a #cairo_surface_t
1984  * @surface_x: X coordinate at which to place the origin of @surface
1985  * @surface_y: Y coordinate at which to place the origin of @surface
1986  *
1987  * A drawing operator that paints the current source
1988  * using the alpha channel of @surface as a mask. (Opaque
1989  * areas of @surface are painted with the source, transparent
1990  * areas are not painted.)
1991  */
1992 void
1993 cairo_mask_surface (cairo_t         *cr,
1994                     cairo_surface_t *surface,
1995                     double           surface_x,
1996                     double           surface_y)
1997 {
1998     cairo_pattern_t *pattern;
1999     cairo_matrix_t matrix;
2000
2001     if (unlikely (cr->status))
2002         return;
2003
2004     pattern = cairo_pattern_create_for_surface (surface);
2005
2006     cairo_matrix_init_translate (&matrix, - surface_x, - surface_y);
2007     cairo_pattern_set_matrix (pattern, &matrix);
2008
2009     cairo_mask (cr, pattern);
2010
2011     cairo_pattern_destroy (pattern);
2012 }
2013
2014 /**
2015  * cairo_stroke:
2016  * @cr: a cairo context
2017  *
2018  * A drawing operator that strokes the current path according to the
2019  * current line width, line join, line cap, and dash settings. After
2020  * cairo_stroke(), the current path will be cleared from the cairo
2021  * context. See cairo_set_line_width(), cairo_set_line_join(),
2022  * cairo_set_line_cap(), cairo_set_dash(), and
2023  * cairo_stroke_preserve().
2024  *
2025  * Note: Degenerate segments and sub-paths are treated specially and
2026  * provide a useful result. These can result in two different
2027  * situations:
2028  *
2029  * 1. Zero-length "on" segments set in cairo_set_dash(). If the cap
2030  * style is %CAIRO_LINE_CAP_ROUND or %CAIRO_LINE_CAP_SQUARE then these
2031  * segments will be drawn as circular dots or squares respectively. In
2032  * the case of %CAIRO_LINE_CAP_SQUARE, the orientation of the squares
2033  * is determined by the direction of the underlying path.
2034  *
2035  * 2. A sub-path created by cairo_move_to() followed by either a
2036  * cairo_close_path() or one or more calls to cairo_line_to() to the
2037  * same coordinate as the cairo_move_to(). If the cap style is
2038  * %CAIRO_LINE_CAP_ROUND then these sub-paths will be drawn as circular
2039  * dots. Note that in the case of %CAIRO_LINE_CAP_SQUARE a degenerate
2040  * sub-path will not be drawn at all, (since the correct orientation
2041  * is indeterminate).
2042  *
2043  * In no case will a cap style of %CAIRO_LINE_CAP_BUTT cause anything
2044  * to be drawn in the case of either degenerate segments or sub-paths.
2045  **/
2046 void
2047 cairo_stroke (cairo_t *cr)
2048 {
2049     cairo_status_t status;
2050
2051     if (unlikely (cr->status))
2052         return;
2053
2054     status = cr->backend->stroke (cr);
2055     if (unlikely (status))
2056         _cairo_set_error (cr, status);
2057 }
2058 slim_hidden_def(cairo_stroke);
2059
2060 /**
2061  * cairo_stroke_preserve:
2062  * @cr: a cairo context
2063  *
2064  * A drawing operator that strokes the current path according to the
2065  * current line width, line join, line cap, and dash settings. Unlike
2066  * cairo_stroke(), cairo_stroke_preserve() preserves the path within the
2067  * cairo context.
2068  *
2069  * See cairo_set_line_width(), cairo_set_line_join(),
2070  * cairo_set_line_cap(), cairo_set_dash(), and
2071  * cairo_stroke_preserve().
2072  **/
2073 void
2074 cairo_stroke_preserve (cairo_t *cr)
2075 {
2076     cairo_status_t status;
2077
2078     if (unlikely (cr->status))
2079         return;
2080
2081     status = cr->backend->stroke_preserve (cr);
2082     if (unlikely (status))
2083         _cairo_set_error (cr, status);
2084 }
2085 slim_hidden_def(cairo_stroke_preserve);
2086
2087 /**
2088  * cairo_fill:
2089  * @cr: a cairo context
2090  *
2091  * A drawing operator that fills the current path according to the
2092  * current fill rule, (each sub-path is implicitly closed before being
2093  * filled). After cairo_fill(), the current path will be cleared from
2094  * the cairo context. See cairo_set_fill_rule() and
2095  * cairo_fill_preserve().
2096  **/
2097 void
2098 cairo_fill (cairo_t *cr)
2099 {
2100     cairo_status_t status;
2101
2102     if (unlikely (cr->status))
2103         return;
2104
2105     status = cr->backend->fill (cr);
2106     if (unlikely (status))
2107         _cairo_set_error (cr, status);
2108 }
2109
2110 /**
2111  * cairo_fill_preserve:
2112  * @cr: a cairo context
2113  *
2114  * A drawing operator that fills the current path according to the
2115  * current fill rule, (each sub-path is implicitly closed before being
2116  * filled). Unlike cairo_fill(), cairo_fill_preserve() preserves the
2117  * path within the cairo context.
2118  *
2119  * See cairo_set_fill_rule() and cairo_fill().
2120  **/
2121 void
2122 cairo_fill_preserve (cairo_t *cr)
2123 {
2124     cairo_status_t status;
2125
2126     if (unlikely (cr->status))
2127         return;
2128
2129     status = cr->backend->fill_preserve (cr);
2130     if (unlikely (status))
2131         _cairo_set_error (cr, status);
2132 }
2133 slim_hidden_def(cairo_fill_preserve);
2134
2135 /**
2136  * cairo_copy_page:
2137  * @cr: a cairo context
2138  *
2139  * Emits the current page for backends that support multiple pages, but
2140  * doesn't clear it, so, the contents of the current page will be retained
2141  * for the next page too.  Use cairo_show_page() if you want to get an
2142  * empty page after the emission.
2143  *
2144  * This is a convenience function that simply calls
2145  * cairo_surface_copy_page() on @cr's target.
2146  **/
2147 void
2148 cairo_copy_page (cairo_t *cr)
2149 {
2150     cairo_status_t status;
2151
2152     if (unlikely (cr->status))
2153         return;
2154
2155     status = cr->backend->copy_page (cr);
2156     if (unlikely (status))
2157         _cairo_set_error (cr, status);
2158 }
2159
2160 /**
2161  * cairo_show_page:
2162  * @cr: a cairo context
2163  *
2164  * Emits and clears the current page for backends that support multiple
2165  * pages.  Use cairo_copy_page() if you don't want to clear the page.
2166  *
2167  * This is a convenience function that simply calls
2168  * cairo_surface_show_page() on @cr's target.
2169  **/
2170 void
2171 cairo_show_page (cairo_t *cr)
2172 {
2173     cairo_status_t status;
2174
2175     if (unlikely (cr->status))
2176         return;
2177
2178     status = cr->backend->show_page (cr);
2179     if (unlikely (status))
2180         _cairo_set_error (cr, status);
2181 }
2182
2183 /**
2184  * cairo_in_stroke:
2185  * @cr: a cairo context
2186  * @x: X coordinate of the point to test
2187  * @y: Y coordinate of the point to test
2188  *
2189  * Tests whether the given point is inside the area that would be
2190  * affected by a cairo_stroke() operation given the current path and
2191  * stroking parameters. Surface dimensions and clipping are not taken
2192  * into account.
2193  *
2194  * See cairo_stroke(), cairo_set_line_width(), cairo_set_line_join(),
2195  * cairo_set_line_cap(), cairo_set_dash(), and
2196  * cairo_stroke_preserve().
2197  *
2198  * Return value: A non-zero value if the point is inside, or zero if
2199  * outside.
2200  **/
2201 cairo_bool_t
2202 cairo_in_stroke (cairo_t *cr, double x, double y)
2203 {
2204     cairo_status_t status;
2205     cairo_bool_t inside = FALSE;
2206
2207     if (unlikely (cr->status))
2208         return FALSE;
2209
2210     status = cr->backend->in_stroke (cr, x, y, &inside);
2211     if (unlikely (status))
2212         _cairo_set_error (cr, status);
2213
2214     return inside;
2215 }
2216
2217 /**
2218  * cairo_in_fill:
2219  * @cr: a cairo context
2220  * @x: X coordinate of the point to test
2221  * @y: Y coordinate of the point to test
2222  *
2223  * Tests whether the given point is inside the area that would be
2224  * affected by a cairo_fill() operation given the current path and
2225  * filling parameters. Surface dimensions and clipping are not taken
2226  * into account.
2227  *
2228  * See cairo_fill(), cairo_set_fill_rule() and cairo_fill_preserve().
2229  *
2230  * Return value: A non-zero value if the point is inside, or zero if
2231  * outside.
2232  **/
2233 cairo_bool_t
2234 cairo_in_fill (cairo_t *cr, double x, double y)
2235 {
2236     cairo_status_t status;
2237     cairo_bool_t inside = FALSE;
2238
2239     if (unlikely (cr->status))
2240         return FALSE;
2241
2242     status = cr->backend->in_fill (cr, x, y, &inside);
2243     if (unlikely (status))
2244         _cairo_set_error (cr, status);
2245
2246     return inside;
2247 }
2248
2249 /**
2250  * cairo_stroke_extents:
2251  * @cr: a cairo context
2252  * @x1: left of the resulting extents
2253  * @y1: top of the resulting extents
2254  * @x2: right of the resulting extents
2255  * @y2: bottom of the resulting extents
2256  *
2257  * Computes a bounding box in user coordinates covering the area that
2258  * would be affected, (the "inked" area), by a cairo_stroke()
2259  * operation given the current path and stroke parameters.
2260  * If the current path is empty, returns an empty rectangle ((0,0), (0,0)).
2261  * Surface dimensions and clipping are not taken into account.
2262  *
2263  * Note that if the line width is set to exactly zero, then
2264  * cairo_stroke_extents() will return an empty rectangle. Contrast with
2265  * cairo_path_extents() which can be used to compute the non-empty
2266  * bounds as the line width approaches zero.
2267  *
2268  * Note that cairo_stroke_extents() must necessarily do more work to
2269  * compute the precise inked areas in light of the stroke parameters,
2270  * so cairo_path_extents() may be more desirable for sake of
2271  * performance if non-inked path extents are desired.
2272  *
2273  * See cairo_stroke(), cairo_set_line_width(), cairo_set_line_join(),
2274  * cairo_set_line_cap(), cairo_set_dash(), and
2275  * cairo_stroke_preserve().
2276  **/
2277 void
2278 cairo_stroke_extents (cairo_t *cr,
2279                       double *x1, double *y1, double *x2, double *y2)
2280 {
2281     cairo_status_t status;
2282
2283     if (unlikely (cr->status)) {
2284         if (x1)
2285             *x1 = 0.0;
2286         if (y1)
2287             *y1 = 0.0;
2288         if (x2)
2289             *x2 = 0.0;
2290         if (y2)
2291             *y2 = 0.0;
2292
2293         return;
2294     }
2295
2296     status = cr->backend->stroke_extents (cr, x1, y1, x2, y2);
2297     if (unlikely (status))
2298         _cairo_set_error (cr, status);
2299 }
2300
2301 /**
2302  * cairo_fill_extents:
2303  * @cr: a cairo context
2304  * @x1: left of the resulting extents
2305  * @y1: top of the resulting extents
2306  * @x2: right of the resulting extents
2307  * @y2: bottom of the resulting extents
2308  *
2309  * Computes a bounding box in user coordinates covering the area that
2310  * would be affected, (the "inked" area), by a cairo_fill() operation
2311  * given the current path and fill parameters. If the current path is
2312  * empty, returns an empty rectangle ((0,0), (0,0)). Surface
2313  * dimensions and clipping are not taken into account.
2314  *
2315  * Contrast with cairo_path_extents(), which is similar, but returns
2316  * non-zero extents for some paths with no inked area, (such as a
2317  * simple line segment).
2318  *
2319  * Note that cairo_fill_extents() must necessarily do more work to
2320  * compute the precise inked areas in light of the fill rule, so
2321  * cairo_path_extents() may be more desirable for sake of performance
2322  * if the non-inked path extents are desired.
2323  *
2324  * See cairo_fill(), cairo_set_fill_rule() and cairo_fill_preserve().
2325  **/
2326 void
2327 cairo_fill_extents (cairo_t *cr,
2328                     double *x1, double *y1, double *x2, double *y2)
2329 {
2330     cairo_status_t status;
2331
2332     if (unlikely (cr->status)) {
2333         if (x1)
2334             *x1 = 0.0;
2335         if (y1)
2336             *y1 = 0.0;
2337         if (x2)
2338             *x2 = 0.0;
2339         if (y2)
2340             *y2 = 0.0;
2341
2342         return;
2343     }
2344
2345     status = cr->backend->fill_extents (cr, x1, y1, x2, y2);
2346     if (unlikely (status))
2347         _cairo_set_error (cr, status);
2348 }
2349
2350 /**
2351  * cairo_clip:
2352  * @cr: a cairo context
2353  *
2354  * Establishes a new clip region by intersecting the current clip
2355  * region with the current path as it would be filled by cairo_fill()
2356  * and according to the current fill rule (see cairo_set_fill_rule()).
2357  *
2358  * After cairo_clip(), the current path will be cleared from the cairo
2359  * context.
2360  *
2361  * The current clip region affects all drawing operations by
2362  * effectively masking out any changes to the surface that are outside
2363  * the current clip region.
2364  *
2365  * Calling cairo_clip() can only make the clip region smaller, never
2366  * larger. But the current clip is part of the graphics state, so a
2367  * temporary restriction of the clip region can be achieved by
2368  * calling cairo_clip() within a cairo_save()/cairo_restore()
2369  * pair. The only other means of increasing the size of the clip
2370  * region is cairo_reset_clip().
2371  **/
2372 void
2373 cairo_clip (cairo_t *cr)
2374 {
2375     cairo_status_t status;
2376
2377     if (unlikely (cr->status))
2378         return;
2379
2380     status = cr->backend->clip (cr);
2381     if (unlikely (status))
2382         _cairo_set_error (cr, status);
2383 }
2384
2385 /**
2386  * cairo_clip_preserve:
2387  * @cr: a cairo context
2388  *
2389  * Establishes a new clip region by intersecting the current clip
2390  * region with the current path as it would be filled by cairo_fill()
2391  * and according to the current fill rule (see cairo_set_fill_rule()).
2392  *
2393  * Unlike cairo_clip(), cairo_clip_preserve() preserves the path within
2394  * the cairo context.
2395  *
2396  * The current clip region affects all drawing operations by
2397  * effectively masking out any changes to the surface that are outside
2398  * the current clip region.
2399  *
2400  * Calling cairo_clip_preserve() can only make the clip region smaller, never
2401  * larger. But the current clip is part of the graphics state, so a
2402  * temporary restriction of the clip region can be achieved by
2403  * calling cairo_clip_preserve() within a cairo_save()/cairo_restore()
2404  * pair. The only other means of increasing the size of the clip
2405  * region is cairo_reset_clip().
2406  **/
2407 void
2408 cairo_clip_preserve (cairo_t *cr)
2409 {
2410     cairo_status_t status;
2411
2412     if (unlikely (cr->status))
2413         return;
2414
2415     status = cr->backend->clip_preserve (cr);
2416     if (unlikely (status))
2417         _cairo_set_error (cr, status);
2418 }
2419 slim_hidden_def(cairo_clip_preserve);
2420
2421 /**
2422  * cairo_reset_clip:
2423  * @cr: a cairo context
2424  *
2425  * Reset the current clip region to its original, unrestricted
2426  * state. That is, set the clip region to an infinitely large shape
2427  * containing the target surface. Equivalently, if infinity is too
2428  * hard to grasp, one can imagine the clip region being reset to the
2429  * exact bounds of the target surface.
2430  *
2431  * Note that code meant to be reusable should not call
2432  * cairo_reset_clip() as it will cause results unexpected by
2433  * higher-level code which calls cairo_clip(). Consider using
2434  * cairo_save() and cairo_restore() around cairo_clip() as a more
2435  * robust means of temporarily restricting the clip region.
2436  **/
2437 void
2438 cairo_reset_clip (cairo_t *cr)
2439 {
2440     cairo_status_t status;
2441
2442     if (unlikely (cr->status))
2443         return;
2444
2445     status = cr->backend->reset_clip (cr);
2446     if (unlikely (status))
2447         _cairo_set_error (cr, status);
2448 }
2449
2450 /**
2451  * cairo_clip_extents:
2452  * @cr: a cairo context
2453  * @x1: left of the resulting extents
2454  * @y1: top of the resulting extents
2455  * @x2: right of the resulting extents
2456  * @y2: bottom of the resulting extents
2457  *
2458  * Computes a bounding box in user coordinates covering the area inside the
2459  * current clip.
2460  *
2461  * Since: 1.4
2462  **/
2463 void
2464 cairo_clip_extents (cairo_t *cr,
2465                     double *x1, double *y1,
2466                     double *x2, double *y2)
2467 {
2468     cairo_status_t status;
2469
2470     if (x1)
2471         *x1 = 0.0;
2472     if (y1)
2473         *y1 = 0.0;
2474     if (x2)
2475         *x2 = 0.0;
2476     if (y2)
2477         *y2 = 0.0;
2478
2479     if (unlikely (cr->status))
2480         return;
2481
2482     status = cr->backend->clip_extents (cr, x1, y1, x2, y2);
2483     if (unlikely (status))
2484         _cairo_set_error (cr, status);
2485 }
2486
2487 /**
2488  * cairo_in_clip:
2489  * @cr: a cairo context
2490  * @x: X coordinate of the point to test
2491  * @y: Y coordinate of the point to test
2492  *
2493  * Tests whether the given point is inside the area that would be
2494  * visible through the current clip, i.e. the area that would be filled by
2495  * a cairo_paint() operation.
2496  *
2497  * See cairo_clip(), and cairo_clip_preserve().
2498  *
2499  * Return value: A non-zero value if the point is inside, or zero if
2500  * outside.
2501  *
2502  * Since: 1.10
2503  **/
2504 cairo_bool_t
2505 cairo_in_clip (cairo_t *cr, double x, double y)
2506 {
2507     cairo_status_t status;
2508     cairo_bool_t inside = FALSE;
2509
2510     if (unlikely (cr->status))
2511         return FALSE;
2512
2513     status = cr->backend->in_clip (cr, x, y, &inside);
2514     if (unlikely (status))
2515         _cairo_set_error (cr, status);
2516
2517     return inside;
2518 }
2519
2520 /**
2521  * cairo_copy_clip_rectangle_list:
2522  * @cr: a cairo context
2523  *
2524  * Gets the current clip region as a list of rectangles in user coordinates.
2525  * Never returns %NULL.
2526  *
2527  * The status in the list may be %CAIRO_STATUS_CLIP_NOT_REPRESENTABLE to
2528  * indicate that the clip region cannot be represented as a list of
2529  * user-space rectangles. The status may have other values to indicate
2530  * other errors.
2531  *
2532  * Returns: the current clip region as a list of rectangles in user coordinates,
2533  * which should be destroyed using cairo_rectangle_list_destroy().
2534  *
2535  * Since: 1.4
2536  **/
2537 cairo_rectangle_list_t *
2538 cairo_copy_clip_rectangle_list (cairo_t *cr)
2539 {
2540     if (unlikely (cr->status))
2541         return _cairo_rectangle_list_create_in_error (cr->status);
2542
2543     return cr->backend->clip_copy_rectangle_list (cr);
2544 }
2545
2546 /**
2547  * cairo_select_font_face:
2548  * @cr: a #cairo_t
2549  * @family: a font family name, encoded in UTF-8
2550  * @slant: the slant for the font
2551  * @weight: the weight for the font
2552  *
2553  * Note: The cairo_select_font_face() function call is part of what
2554  * the cairo designers call the "toy" text API. It is convenient for
2555  * short demos and simple programs, but it is not expected to be
2556  * adequate for serious text-using applications.
2557  *
2558  * Selects a family and style of font from a simplified description as
2559  * a family name, slant and weight. Cairo provides no operation to
2560  * list available family names on the system (this is a "toy",
2561  * remember), but the standard CSS2 generic family names, ("serif",
2562  * "sans-serif", "cursive", "fantasy", "monospace"), are likely to
2563  * work as expected.
2564  *
2565  * If @family starts with the string "@cairo:", or if no native font
2566  * backends are compiled in, cairo will use an internal font family.
2567  * The internal font family recognizes many modifiers in the @family
2568  * string, most notably, it recognizes the string "monospace".  That is,
2569  * the family name "@cairo:monospace" will use the monospace version of
2570  * the internal font family.
2571  *
2572  * For "real" font selection, see the font-backend-specific
2573  * font_face_create functions for the font backend you are using. (For
2574  * example, if you are using the freetype-based cairo-ft font backend,
2575  * see cairo_ft_font_face_create_for_ft_face() or
2576  * cairo_ft_font_face_create_for_pattern().) The resulting font face
2577  * could then be used with cairo_scaled_font_create() and
2578  * cairo_set_scaled_font().
2579  *
2580  * Similarly, when using the "real" font support, you can call
2581  * directly into the underlying font system, (such as fontconfig or
2582  * freetype), for operations such as listing available fonts, etc.
2583  *
2584  * It is expected that most applications will need to use a more
2585  * comprehensive font handling and text layout library, (for example,
2586  * pango), in conjunction with cairo.
2587  *
2588  * If text is drawn without a call to cairo_select_font_face(), (nor
2589  * cairo_set_font_face() nor cairo_set_scaled_font()), the default
2590  * family is platform-specific, but is essentially "sans-serif".
2591  * Default slant is %CAIRO_FONT_SLANT_NORMAL, and default weight is
2592  * %CAIRO_FONT_WEIGHT_NORMAL.
2593  *
2594  * This function is equivalent to a call to cairo_toy_font_face_create()
2595  * followed by cairo_set_font_face().
2596  **/
2597 void
2598 cairo_select_font_face (cairo_t              *cr,
2599                         const char           *family,
2600                         cairo_font_slant_t    slant,
2601                         cairo_font_weight_t   weight)
2602 {
2603     cairo_font_face_t *font_face;
2604     cairo_status_t status;
2605
2606     if (unlikely (cr->status))
2607         return;
2608
2609     font_face = cairo_toy_font_face_create (family, slant, weight);
2610     if (unlikely (font_face->status)) {
2611         _cairo_set_error (cr, font_face->status);
2612         return;
2613     }
2614
2615     status = cr->backend->set_font_face (cr, font_face);
2616     cairo_font_face_destroy (font_face);
2617
2618     if (unlikely (status))
2619         _cairo_set_error (cr, status);
2620 }
2621
2622 /**
2623  * cairo_font_extents:
2624  * @cr: a #cairo_t
2625  * @extents: a #cairo_font_extents_t object into which the results
2626  * will be stored.
2627  *
2628  * Gets the font extents for the currently selected font.
2629  **/
2630 void
2631 cairo_font_extents (cairo_t              *cr,
2632                     cairo_font_extents_t *extents)
2633 {
2634     cairo_status_t status;
2635
2636     extents->ascent = 0.0;
2637     extents->descent = 0.0;
2638     extents->height = 0.0;
2639     extents->max_x_advance = 0.0;
2640     extents->max_y_advance = 0.0;
2641
2642     if (unlikely (cr->status))
2643         return;
2644
2645     status = cr->backend->font_extents (cr, extents);
2646     if (unlikely (status))
2647         _cairo_set_error (cr, status);
2648 }
2649
2650 /**
2651  * cairo_set_font_face:
2652  * @cr: a #cairo_t
2653  * @font_face: a #cairo_font_face_t, or %NULL to restore to the default font
2654  *
2655  * Replaces the current #cairo_font_face_t object in the #cairo_t with
2656  * @font_face. The replaced font face in the #cairo_t will be
2657  * destroyed if there are no other references to it.
2658  **/
2659 void
2660 cairo_set_font_face (cairo_t           *cr,
2661                      cairo_font_face_t *font_face)
2662 {
2663     cairo_status_t status;
2664
2665     if (unlikely (cr->status))
2666         return;
2667
2668     status = cr->backend->set_font_face (cr, font_face);
2669     if (unlikely (status))
2670         _cairo_set_error (cr, status);
2671 }
2672
2673 /**
2674  * cairo_get_font_face:
2675  * @cr: a #cairo_t
2676  *
2677  * Gets the current font face for a #cairo_t.
2678  *
2679  * Return value: the current font face.  This object is owned by
2680  * cairo. To keep a reference to it, you must call
2681  * cairo_font_face_reference().
2682  *
2683  * This function never returns %NULL. If memory cannot be allocated, a
2684  * special "nil" #cairo_font_face_t object will be returned on which
2685  * cairo_font_face_status() returns %CAIRO_STATUS_NO_MEMORY. Using
2686  * this nil object will cause its error state to propagate to other
2687  * objects it is passed to, (for example, calling
2688  * cairo_set_font_face() with a nil font will trigger an error that
2689  * will shutdown the #cairo_t object).
2690  **/
2691 cairo_font_face_t *
2692 cairo_get_font_face (cairo_t *cr)
2693 {
2694     if (unlikely (cr->status))
2695         return (cairo_font_face_t*) &_cairo_font_face_nil;
2696
2697     return cr->backend->get_font_face (cr);
2698 }
2699
2700 /**
2701  * cairo_set_font_size:
2702  * @cr: a #cairo_t
2703  * @size: the new font size, in user space units
2704  *
2705  * Sets the current font matrix to a scale by a factor of @size, replacing
2706  * any font matrix previously set with cairo_set_font_size() or
2707  * cairo_set_font_matrix(). This results in a font size of @size user space
2708  * units. (More precisely, this matrix will result in the font's
2709  * em-square being a @size by @size square in user space.)
2710  *
2711  * If text is drawn without a call to cairo_set_font_size(), (nor
2712  * cairo_set_font_matrix() nor cairo_set_scaled_font()), the default
2713  * font size is 10.0.
2714  **/
2715 void
2716 cairo_set_font_size (cairo_t *cr, double size)
2717 {
2718     cairo_status_t status;
2719
2720     if (unlikely (cr->status))
2721         return;
2722
2723     status = cr->backend->set_font_size (cr, size);
2724     if (unlikely (status))
2725         _cairo_set_error (cr, status);
2726 }
2727 slim_hidden_def (cairo_set_font_size);
2728
2729 /**
2730  * cairo_set_font_matrix
2731  * @cr: a #cairo_t
2732  * @matrix: a #cairo_matrix_t describing a transform to be applied to
2733  * the current font.
2734  *
2735  * Sets the current font matrix to @matrix. The font matrix gives a
2736  * transformation from the design space of the font (in this space,
2737  * the em-square is 1 unit by 1 unit) to user space. Normally, a
2738  * simple scale is used (see cairo_set_font_size()), but a more
2739  * complex font matrix can be used to shear the font
2740  * or stretch it unequally along the two axes
2741  **/
2742 void
2743 cairo_set_font_matrix (cairo_t              *cr,
2744                        const cairo_matrix_t *matrix)
2745 {
2746     cairo_status_t status;
2747
2748     if (unlikely (cr->status))
2749         return;
2750
2751     status = cr->backend->set_font_matrix (cr, matrix);
2752     if (unlikely (status))
2753         _cairo_set_error (cr, status);
2754 }
2755 slim_hidden_def (cairo_set_font_matrix);
2756
2757 /**
2758  * cairo_get_font_matrix
2759  * @cr: a #cairo_t
2760  * @matrix: return value for the matrix
2761  *
2762  * Stores the current font matrix into @matrix. See
2763  * cairo_set_font_matrix().
2764  **/
2765 void
2766 cairo_get_font_matrix (cairo_t *cr, cairo_matrix_t *matrix)
2767 {
2768     if (unlikely (cr->status)) {
2769         cairo_matrix_init_identity (matrix);
2770         return;
2771     }
2772
2773     cr->backend->get_font_matrix (cr, matrix);
2774 }
2775
2776 /**
2777  * cairo_set_font_options:
2778  * @cr: a #cairo_t
2779  * @options: font options to use
2780  *
2781  * Sets a set of custom font rendering options for the #cairo_t.
2782  * Rendering options are derived by merging these options with the
2783  * options derived from underlying surface; if the value in @options
2784  * has a default value (like %CAIRO_ANTIALIAS_DEFAULT), then the value
2785  * from the surface is used.
2786  **/
2787 void
2788 cairo_set_font_options (cairo_t                    *cr,
2789                         const cairo_font_options_t *options)
2790 {
2791     cairo_status_t status;
2792
2793     if (unlikely (cr->status))
2794         return;
2795
2796     status = cairo_font_options_status ((cairo_font_options_t *) options);
2797     if (unlikely (status)) {
2798         _cairo_set_error (cr, status);
2799         return;
2800     }
2801
2802     status = cr->backend->set_font_options (cr, options);
2803     if (unlikely (status))
2804         _cairo_set_error (cr, status);
2805 }
2806 slim_hidden_def (cairo_set_font_options);
2807
2808 /**
2809  * cairo_get_font_options:
2810  * @cr: a #cairo_t
2811  * @options: a #cairo_font_options_t object into which to store
2812  *   the retrieved options. All existing values are overwritten
2813  *
2814  * Retrieves font rendering options set via #cairo_set_font_options.
2815  * Note that the returned options do not include any options derived
2816  * from the underlying surface; they are literally the options
2817  * passed to cairo_set_font_options().
2818  **/
2819 void
2820 cairo_get_font_options (cairo_t              *cr,
2821                         cairo_font_options_t *options)
2822 {
2823     /* check that we aren't trying to overwrite the nil object */
2824     if (cairo_font_options_status (options))
2825         return;
2826
2827     if (unlikely (cr->status)) {
2828         _cairo_font_options_init_default (options);
2829         return;
2830     }
2831
2832     cr->backend->get_font_options (cr, options);
2833 }
2834
2835 /**
2836  * cairo_set_scaled_font:
2837  * @cr: a #cairo_t
2838  * @scaled_font: a #cairo_scaled_font_t
2839  *
2840  * Replaces the current font face, font matrix, and font options in
2841  * the #cairo_t with those of the #cairo_scaled_font_t.  Except for
2842  * some translation, the current CTM of the #cairo_t should be the
2843  * same as that of the #cairo_scaled_font_t, which can be accessed
2844  * using cairo_scaled_font_get_ctm().
2845  *
2846  * Since: 1.2
2847  **/
2848 void
2849 cairo_set_scaled_font (cairo_t                   *cr,
2850                        const cairo_scaled_font_t *scaled_font)
2851 {
2852     cairo_status_t status;
2853
2854     if (unlikely (cr->status))
2855         return;
2856
2857     if ((scaled_font == NULL)) {
2858         _cairo_set_error (cr, _cairo_error (CAIRO_STATUS_NULL_POINTER));
2859         return;
2860     }
2861
2862     status = scaled_font->status;
2863     if (unlikely (status)) {
2864         _cairo_set_error (cr, status);
2865         return;
2866     }
2867
2868     status = cr->backend->set_scaled_font (cr, (cairo_scaled_font_t *) scaled_font);
2869     if (unlikely (status))
2870         _cairo_set_error (cr, status);
2871 }
2872
2873 /**
2874  * cairo_get_scaled_font:
2875  * @cr: a #cairo_t
2876  *
2877  * Gets the current scaled font for a #cairo_t.
2878  *
2879  * Return value: the current scaled font. This object is owned by
2880  * cairo. To keep a reference to it, you must call
2881  * cairo_scaled_font_reference().
2882  *
2883  * This function never returns %NULL. If memory cannot be allocated, a
2884  * special "nil" #cairo_scaled_font_t object will be returned on which
2885  * cairo_scaled_font_status() returns %CAIRO_STATUS_NO_MEMORY. Using
2886  * this nil object will cause its error state to propagate to other
2887  * objects it is passed to, (for example, calling
2888  * cairo_set_scaled_font() with a nil font will trigger an error that
2889  * will shutdown the #cairo_t object).
2890  *
2891  * Since: 1.4
2892  **/
2893 cairo_scaled_font_t *
2894 cairo_get_scaled_font (cairo_t *cr)
2895 {
2896     if (unlikely (cr->status))
2897         return _cairo_scaled_font_create_in_error (cr->status);
2898
2899     return cr->backend->get_scaled_font (cr);
2900 }
2901 slim_hidden_def (cairo_get_scaled_font);
2902
2903 /**
2904  * cairo_text_extents:
2905  * @cr: a #cairo_t
2906  * @utf8: a NUL-terminated string of text encoded in UTF-8, or %NULL
2907  * @extents: a #cairo_text_extents_t object into which the results
2908  * will be stored
2909  *
2910  * Gets the extents for a string of text. The extents describe a
2911  * user-space rectangle that encloses the "inked" portion of the text,
2912  * (as it would be drawn by cairo_show_text()). Additionally, the
2913  * x_advance and y_advance values indicate the amount by which the
2914  * current point would be advanced by cairo_show_text().
2915  *
2916  * Note that whitespace characters do not directly contribute to the
2917  * size of the rectangle (extents.width and extents.height). They do
2918  * contribute indirectly by changing the position of non-whitespace
2919  * characters. In particular, trailing whitespace characters are
2920  * likely to not affect the size of the rectangle, though they will
2921  * affect the x_advance and y_advance values.
2922  **/
2923 void
2924 cairo_text_extents (cairo_t              *cr,
2925                     const char           *utf8,
2926                     cairo_text_extents_t *extents)
2927 {
2928     cairo_status_t status;
2929     cairo_scaled_font_t *scaled_font;
2930     cairo_glyph_t *glyphs = NULL;
2931     int num_glyphs = 0;
2932     double x, y;
2933
2934     extents->x_bearing = 0.0;
2935     extents->y_bearing = 0.0;
2936     extents->width  = 0.0;
2937     extents->height = 0.0;
2938     extents->x_advance = 0.0;
2939     extents->y_advance = 0.0;
2940
2941     if (unlikely (cr->status))
2942         return;
2943
2944     if (utf8 == NULL)
2945         return;
2946
2947     scaled_font = cairo_get_scaled_font (cr);
2948     if (unlikely (scaled_font->status)) {
2949         _cairo_set_error (cr, scaled_font->status);
2950         return;
2951     }
2952
2953     cairo_get_current_point (cr, &x, &y);
2954     status = cairo_scaled_font_text_to_glyphs (scaled_font,
2955                                                x, y,
2956                                                utf8, -1,
2957                                                &glyphs, &num_glyphs,
2958                                                NULL, NULL, NULL);
2959
2960     if (likely (status == CAIRO_STATUS_SUCCESS)) {
2961         status = cr->backend->glyph_extents (cr,
2962                                              glyphs, num_glyphs,
2963                                              extents);
2964     }
2965     cairo_glyph_free (glyphs);
2966
2967     if (unlikely (status))
2968         _cairo_set_error (cr, status);
2969 }
2970
2971 /**
2972  * cairo_glyph_extents:
2973  * @cr: a #cairo_t
2974  * @glyphs: an array of #cairo_glyph_t objects
2975  * @num_glyphs: the number of elements in @glyphs
2976  * @extents: a #cairo_text_extents_t object into which the results
2977  * will be stored
2978  *
2979  * Gets the extents for an array of glyphs. The extents describe a
2980  * user-space rectangle that encloses the "inked" portion of the
2981  * glyphs, (as they would be drawn by cairo_show_glyphs()).
2982  * Additionally, the x_advance and y_advance values indicate the
2983  * amount by which the current point would be advanced by
2984  * cairo_show_glyphs().
2985  *
2986  * Note that whitespace glyphs do not contribute to the size of the
2987  * rectangle (extents.width and extents.height).
2988  **/
2989 void
2990 cairo_glyph_extents (cairo_t                *cr,
2991                      const cairo_glyph_t    *glyphs,
2992                      int                    num_glyphs,
2993                      cairo_text_extents_t   *extents)
2994 {
2995     cairo_status_t status;
2996
2997     extents->x_bearing = 0.0;
2998     extents->y_bearing = 0.0;
2999     extents->width  = 0.0;
3000     extents->height = 0.0;
3001     extents->x_advance = 0.0;
3002     extents->y_advance = 0.0;
3003
3004     if (unlikely (cr->status))
3005         return;
3006
3007     if (num_glyphs == 0)
3008         return;
3009
3010     if (unlikely (num_glyphs < 0)) {
3011         _cairo_set_error (cr, CAIRO_STATUS_NEGATIVE_COUNT);
3012         return;
3013     }
3014
3015     if (unlikely (glyphs == NULL)) {
3016         _cairo_set_error (cr, CAIRO_STATUS_NULL_POINTER);
3017         return;
3018     }
3019
3020     status = cr->backend->glyph_extents (cr, glyphs, num_glyphs, extents);
3021     if (unlikely (status))
3022         _cairo_set_error (cr, status);
3023 }
3024
3025 /**
3026  * cairo_show_text:
3027  * @cr: a cairo context
3028  * @utf8: a NUL-terminated string of text encoded in UTF-8, or %NULL
3029  *
3030  * A drawing operator that generates the shape from a string of UTF-8
3031  * characters, rendered according to the current font_face, font_size
3032  * (font_matrix), and font_options.
3033  *
3034  * This function first computes a set of glyphs for the string of
3035  * text. The first glyph is placed so that its origin is at the
3036  * current point. The origin of each subsequent glyph is offset from
3037  * that of the previous glyph by the advance values of the previous
3038  * glyph.
3039  *
3040  * After this call the current point is moved to the origin of where
3041  * the next glyph would be placed in this same progression. That is,
3042  * the current point will be at the origin of the final glyph offset
3043  * by its advance values. This allows for easy display of a single
3044  * logical string with multiple calls to cairo_show_text().
3045  *
3046  * Note: The cairo_show_text() function call is part of what the cairo
3047  * designers call the "toy" text API. It is convenient for short demos
3048  * and simple programs, but it is not expected to be adequate for
3049  * serious text-using applications. See cairo_show_glyphs() for the
3050  * "real" text display API in cairo.
3051  **/
3052 void
3053 cairo_show_text (cairo_t *cr, const char *utf8)
3054 {
3055     cairo_text_extents_t extents;
3056     cairo_status_t status;
3057     cairo_glyph_t *glyphs, *last_glyph;
3058     cairo_text_cluster_t *clusters;
3059     int utf8_len, num_glyphs, num_clusters;
3060     cairo_text_cluster_flags_t cluster_flags;
3061     double x, y;
3062     cairo_bool_t has_show_text_glyphs;
3063     cairo_glyph_t stack_glyphs[CAIRO_STACK_ARRAY_LENGTH (cairo_glyph_t)];
3064     cairo_text_cluster_t stack_clusters[CAIRO_STACK_ARRAY_LENGTH (cairo_text_cluster_t)];
3065     cairo_scaled_font_t *scaled_font;
3066     cairo_glyph_text_info_t info, *i;
3067
3068     if (unlikely (cr->status))
3069         return;
3070
3071     if (utf8 == NULL)
3072         return;
3073
3074     scaled_font = cairo_get_scaled_font (cr);
3075     if (unlikely (scaled_font->status)) {
3076         _cairo_set_error (cr, scaled_font->status);
3077         return;
3078     }
3079
3080     utf8_len = strlen (utf8);
3081
3082     has_show_text_glyphs =
3083         cairo_surface_has_show_text_glyphs (cairo_get_target (cr));
3084
3085     glyphs = stack_glyphs;
3086     num_glyphs = ARRAY_LENGTH (stack_glyphs);
3087
3088     if (has_show_text_glyphs) {
3089         clusters = stack_clusters;
3090         num_clusters = ARRAY_LENGTH (stack_clusters);
3091     } else {
3092         clusters = NULL;
3093         num_clusters = 0;
3094     }
3095
3096     cairo_get_current_point (cr, &x, &y);
3097     status = cairo_scaled_font_text_to_glyphs (scaled_font,
3098                                                x, y,
3099                                                utf8, utf8_len,
3100                                                &glyphs, &num_glyphs,
3101                                                has_show_text_glyphs ? &clusters : NULL, &num_clusters,
3102                                                &cluster_flags);
3103     if (unlikely (status))
3104         goto BAIL;
3105
3106     if (num_glyphs == 0)
3107         return;
3108
3109     i = NULL;
3110     if (has_show_text_glyphs) {
3111         info.utf8 = utf8;
3112         info.utf8_len = utf8_len;
3113         info.clusters = clusters;
3114         info.num_clusters = num_clusters;
3115         info.cluster_flags = cluster_flags;
3116         i = &info;
3117     }
3118
3119     status = cr->backend->glyphs (cr, glyphs, num_glyphs, i);
3120     if (unlikely (status))
3121         goto BAIL;
3122
3123     last_glyph = &glyphs[num_glyphs - 1];
3124     status = cr->backend->glyph_extents (cr, last_glyph, 1, &extents);
3125     if (unlikely (status))
3126         goto BAIL;
3127
3128     x = last_glyph->x + extents.x_advance;
3129     y = last_glyph->y + extents.y_advance;
3130     cr->backend->move_to (cr, x, y);
3131
3132  BAIL:
3133     if (glyphs != stack_glyphs)
3134         cairo_glyph_free (glyphs);
3135     if (clusters != stack_clusters)
3136         cairo_text_cluster_free (clusters);
3137
3138     if (unlikely (status))
3139         _cairo_set_error (cr, status);
3140 }
3141
3142 /**
3143  * cairo_show_glyphs:
3144  * @cr: a cairo context
3145  * @glyphs: array of glyphs to show
3146  * @num_glyphs: number of glyphs to show
3147  *
3148  * A drawing operator that generates the shape from an array of glyphs,
3149  * rendered according to the current font face, font size
3150  * (font matrix), and font options.
3151  **/
3152 void
3153 cairo_show_glyphs (cairo_t *cr, const cairo_glyph_t *glyphs, int num_glyphs)
3154 {
3155     cairo_status_t status;
3156
3157     if (unlikely (cr->status))
3158         return;
3159
3160     if (num_glyphs == 0)
3161         return;
3162
3163     if (num_glyphs < 0) {
3164         _cairo_set_error (cr, CAIRO_STATUS_NEGATIVE_COUNT);
3165         return;
3166     }
3167
3168     if (glyphs == NULL) {
3169         _cairo_set_error (cr, CAIRO_STATUS_NULL_POINTER);
3170         return;
3171     }
3172
3173     status = cr->backend->glyphs (cr, glyphs, num_glyphs, NULL);
3174     if (unlikely (status))
3175         _cairo_set_error (cr, status);
3176 }
3177
3178 /**
3179  * cairo_show_text_glyphs:
3180  * @cr: a cairo context
3181  * @utf8: a string of text encoded in UTF-8
3182  * @utf8_len: length of @utf8 in bytes, or -1 if it is NUL-terminated
3183  * @glyphs: array of glyphs to show
3184  * @num_glyphs: number of glyphs to show
3185  * @clusters: array of cluster mapping information
3186  * @num_clusters: number of clusters in the mapping
3187  * @cluster_flags: cluster mapping flags
3188  *
3189  * This operation has rendering effects similar to cairo_show_glyphs()
3190  * but, if the target surface supports it, uses the provided text and
3191  * cluster mapping to embed the text for the glyphs shown in the output.
3192  * If the target does not support the extended attributes, this function
3193  * acts like the basic cairo_show_glyphs() as if it had been passed
3194  * @glyphs and @num_glyphs.
3195  *
3196  * The mapping between @utf8 and @glyphs is provided by an array of
3197  * <firstterm>clusters</firstterm>.  Each cluster covers a number of
3198  * text bytes and glyphs, and neighboring clusters cover neighboring
3199  * areas of @utf8 and @glyphs.  The clusters should collectively cover @utf8
3200  * and @glyphs in entirety.
3201  *
3202  * The first cluster always covers bytes from the beginning of @utf8.
3203  * If @cluster_flags do not have the %CAIRO_TEXT_CLUSTER_FLAG_BACKWARD
3204  * set, the first cluster also covers the beginning
3205  * of @glyphs, otherwise it covers the end of the @glyphs array and
3206  * following clusters move backward.
3207  *
3208  * See #cairo_text_cluster_t for constraints on valid clusters.
3209  *
3210  * Since: 1.8
3211  **/
3212 void
3213 cairo_show_text_glyphs (cairo_t                    *cr,
3214                         const char                 *utf8,
3215                         int                         utf8_len,
3216                         const cairo_glyph_t        *glyphs,
3217                         int                         num_glyphs,
3218                         const cairo_text_cluster_t *clusters,
3219                         int                         num_clusters,
3220                         cairo_text_cluster_flags_t  cluster_flags)
3221 {
3222     cairo_status_t status;
3223
3224     if (unlikely (cr->status))
3225         return;
3226
3227     /* A slew of sanity checks */
3228
3229     /* Special case for NULL and -1 */
3230     if (utf8 == NULL && utf8_len == -1)
3231         utf8_len = 0;
3232
3233     /* No NULLs for non-zeros */
3234     if ((num_glyphs   && glyphs   == NULL) ||
3235         (utf8_len     && utf8     == NULL) ||
3236         (num_clusters && clusters == NULL)) {
3237         _cairo_set_error (cr, CAIRO_STATUS_NULL_POINTER);
3238         return;
3239     }
3240
3241     /* A -1 for utf8_len means NUL-terminated */
3242     if (utf8_len == -1)
3243         utf8_len = strlen (utf8);
3244
3245     /* Apart from that, no negatives */
3246     if (num_glyphs < 0 || utf8_len < 0 || num_clusters < 0) {
3247         _cairo_set_error (cr, CAIRO_STATUS_NEGATIVE_COUNT);
3248         return;
3249     }
3250
3251     if (num_glyphs == 0 && utf8_len == 0)
3252         return;
3253
3254     if (utf8) {
3255         /* Make sure clusters cover the entire glyphs and utf8 arrays,
3256          * and that cluster boundaries are UTF-8 boundaries. */
3257         status = _cairo_validate_text_clusters (utf8, utf8_len,
3258                                                 glyphs, num_glyphs,
3259                                                 clusters, num_clusters, cluster_flags);
3260         if (status == CAIRO_STATUS_INVALID_CLUSTERS) {
3261             /* Either got invalid UTF-8 text, or cluster mapping is bad.
3262              * Differentiate those. */
3263
3264             cairo_status_t status2;
3265
3266             status2 = _cairo_utf8_to_ucs4 (utf8, utf8_len, NULL, NULL);
3267             if (status2)
3268                 status = status2;
3269         } else {
3270             cairo_glyph_text_info_t info;
3271
3272             info.utf8 = utf8;
3273             info.utf8_len = utf8_len;
3274             info.clusters = clusters;
3275             info.num_clusters = num_clusters;
3276             info.cluster_flags = cluster_flags;
3277
3278             status = cr->backend->glyphs (cr, glyphs, num_glyphs, &info);
3279         }
3280     } else {
3281         status = cr->backend->glyphs (cr, glyphs, num_glyphs, NULL);
3282     }
3283     if (unlikely (status))
3284         _cairo_set_error (cr, status);
3285 }
3286
3287 /**
3288  * cairo_text_path:
3289  * @cr: a cairo context
3290  * @utf8: a NUL-terminated string of text encoded in UTF-8, or %NULL
3291  *
3292  * Adds closed paths for text to the current path.  The generated
3293  * path if filled, achieves an effect similar to that of
3294  * cairo_show_text().
3295  *
3296  * Text conversion and positioning is done similar to cairo_show_text().
3297  *
3298  * Like cairo_show_text(), After this call the current point is
3299  * moved to the origin of where the next glyph would be placed in
3300  * this same progression.  That is, the current point will be at
3301  * the origin of the final glyph offset by its advance values.
3302  * This allows for chaining multiple calls to to cairo_text_path()
3303  * without having to set current point in between.
3304  *
3305  * Note: The cairo_text_path() function call is part of what the cairo
3306  * designers call the "toy" text API. It is convenient for short demos
3307  * and simple programs, but it is not expected to be adequate for
3308  * serious text-using applications. See cairo_glyph_path() for the
3309  * "real" text path API in cairo.
3310  **/
3311 void
3312 cairo_text_path (cairo_t *cr, const char *utf8)
3313 {
3314     cairo_status_t status;
3315     cairo_text_extents_t extents;
3316     cairo_glyph_t stack_glyphs[CAIRO_STACK_ARRAY_LENGTH (cairo_glyph_t)];
3317     cairo_glyph_t *glyphs, *last_glyph;
3318     cairo_scaled_font_t *scaled_font;
3319     int num_glyphs;
3320     double x, y;
3321
3322     if (unlikely (cr->status))
3323         return;
3324
3325     if (utf8 == NULL)
3326         return;
3327
3328
3329     glyphs = stack_glyphs;
3330     num_glyphs = ARRAY_LENGTH (stack_glyphs);
3331
3332     scaled_font = cairo_get_scaled_font (cr);
3333     if (unlikely (scaled_font->status)) {
3334         _cairo_set_error (cr, scaled_font->status);
3335         return;
3336     }
3337
3338     cairo_get_current_point (cr, &x, &y);
3339     status = cairo_scaled_font_text_to_glyphs (scaled_font,
3340                                                x, y,
3341                                                utf8, -1,
3342                                                &glyphs, &num_glyphs,
3343                                                NULL, NULL, NULL);
3344
3345     if (num_glyphs == 0)
3346         return;
3347
3348     status = cr->backend->glyph_path (cr, glyphs, num_glyphs);
3349
3350     if (unlikely (status))
3351         goto BAIL;
3352
3353     last_glyph = &glyphs[num_glyphs - 1];
3354     status = cr->backend->glyph_extents (cr, last_glyph, 1, &extents);
3355
3356     if (unlikely (status))
3357         goto BAIL;
3358
3359     x = last_glyph->x + extents.x_advance;
3360     y = last_glyph->y + extents.y_advance;
3361     cr->backend->move_to (cr, x, y);
3362
3363  BAIL:
3364     if (glyphs != stack_glyphs)
3365         cairo_glyph_free (glyphs);
3366
3367     if (unlikely (status))
3368         _cairo_set_error (cr, status);
3369 }
3370
3371 /**
3372  * cairo_glyph_path:
3373  * @cr: a cairo context
3374  * @glyphs: array of glyphs to show
3375  * @num_glyphs: number of glyphs to show
3376  *
3377  * Adds closed paths for the glyphs to the current path.  The generated
3378  * path if filled, achieves an effect similar to that of
3379  * cairo_show_glyphs().
3380  **/
3381 void
3382 cairo_glyph_path (cairo_t *cr, const cairo_glyph_t *glyphs, int num_glyphs)
3383 {
3384     cairo_status_t status;
3385
3386     if (unlikely (cr->status))
3387         return;
3388
3389     if (num_glyphs == 0)
3390         return;
3391
3392     if (unlikely (num_glyphs < 0)) {
3393         _cairo_set_error (cr, CAIRO_STATUS_NEGATIVE_COUNT);
3394         return;
3395     }
3396
3397     if (unlikely (glyphs == NULL)) {
3398         _cairo_set_error (cr, CAIRO_STATUS_NULL_POINTER);
3399         return;
3400     }
3401
3402     status = cr->backend->glyph_path (cr, glyphs, num_glyphs);
3403     if (unlikely (status))
3404         _cairo_set_error (cr, status);
3405 }
3406
3407 /**
3408  * cairo_get_operator:
3409  * @cr: a cairo context
3410  *
3411  * Gets the current compositing operator for a cairo context.
3412  *
3413  * Return value: the current compositing operator.
3414  **/
3415 cairo_operator_t
3416 cairo_get_operator (cairo_t *cr)
3417 {
3418     if (unlikely (cr->status))
3419         return CAIRO_GSTATE_OPERATOR_DEFAULT;
3420
3421     return cr->backend->get_operator (cr);
3422 }
3423
3424 #if 0
3425 /**
3426  * cairo_get_opacity:
3427  * @cr: a cairo context
3428  *
3429  * Gets the current compositing opacity for a cairo context.
3430  *
3431  * Return value: the current compositing opacity.
3432  **/
3433 double
3434 cairo_get_opacity (cairo_t *cr)
3435 {
3436     if (unlikely (cr->status))
3437         return 1.;
3438
3439     return cr->backend->get_opacity (cr);
3440 }
3441 #endif
3442
3443 /**
3444  * cairo_get_tolerance:
3445  * @cr: a cairo context
3446  *
3447  * Gets the current tolerance value, as set by cairo_set_tolerance().
3448  *
3449  * Return value: the current tolerance value.
3450  **/
3451 double
3452 cairo_get_tolerance (cairo_t *cr)
3453 {
3454     if (unlikely (cr->status))
3455         return CAIRO_GSTATE_TOLERANCE_DEFAULT;
3456
3457     return cr->backend->get_tolerance (cr);
3458 }
3459 slim_hidden_def (cairo_get_tolerance);
3460
3461 /**
3462  * cairo_get_antialias:
3463  * @cr: a cairo context
3464  *
3465  * Gets the current shape antialiasing mode, as set by
3466  * cairo_set_antialias().
3467  *
3468  * Return value: the current shape antialiasing mode.
3469  **/
3470 cairo_antialias_t
3471 cairo_get_antialias (cairo_t *cr)
3472 {
3473     if (unlikely (cr->status))
3474         return CAIRO_ANTIALIAS_DEFAULT;
3475
3476     return cr->backend->get_antialias (cr);
3477 }
3478
3479 /**
3480  * cairo_has_current_point:
3481  * @cr: a cairo context
3482  *
3483  * Returns whether a current point is defined on the current path.
3484  * See cairo_get_current_point() for details on the current point.
3485  *
3486  * Return value: whether a current point is defined.
3487  *
3488  * Since: 1.6
3489  **/
3490 cairo_bool_t
3491 cairo_has_current_point (cairo_t *cr)
3492 {
3493     if (unlikely (cr->status))
3494         return FALSE;
3495
3496     return cr->backend->has_current_point (cr);
3497 }
3498
3499 /**
3500  * cairo_get_current_point:
3501  * @cr: a cairo context
3502  * @x: return value for X coordinate of the current point
3503  * @y: return value for Y coordinate of the current point
3504  *
3505  * Gets the current point of the current path, which is
3506  * conceptually the final point reached by the path so far.
3507  *
3508  * The current point is returned in the user-space coordinate
3509  * system. If there is no defined current point or if @cr is in an
3510  * error status, @x and @y will both be set to 0.0. It is possible to
3511  * check this in advance with cairo_has_current_point().
3512  *
3513  * Most path construction functions alter the current point. See the
3514  * following for details on how they affect the current point:
3515  * cairo_new_path(), cairo_new_sub_path(),
3516  * cairo_append_path(), cairo_close_path(),
3517  * cairo_move_to(), cairo_line_to(), cairo_curve_to(),
3518  * cairo_rel_move_to(), cairo_rel_line_to(), cairo_rel_curve_to(),
3519  * cairo_arc(), cairo_arc_negative(), cairo_rectangle(),
3520  * cairo_text_path(), cairo_glyph_path(), cairo_stroke_to_path().
3521  *
3522  * Some functions use and alter the current point but do not
3523  * otherwise change current path:
3524  * cairo_show_text().
3525  *
3526  * Some functions unset the current path and as a result, current point:
3527  * cairo_fill(), cairo_stroke().
3528  **/
3529 void
3530 cairo_get_current_point (cairo_t *cr, double *x_ret, double *y_ret)
3531 {
3532     double x, y;
3533
3534     x = y = 0;
3535     if (cr->status == CAIRO_STATUS_SUCCESS &&
3536         cr->backend->has_current_point (cr))
3537     {
3538         cr->backend->get_current_point (cr, &x, &y);
3539     }
3540
3541     if (x_ret)
3542         *x_ret = x;
3543     if (y_ret)
3544         *y_ret = y;
3545 }
3546 slim_hidden_def(cairo_get_current_point);
3547
3548 /**
3549  * cairo_get_fill_rule:
3550  * @cr: a cairo context
3551  *
3552  * Gets the current fill rule, as set by cairo_set_fill_rule().
3553  *
3554  * Return value: the current fill rule.
3555  **/
3556 cairo_fill_rule_t
3557 cairo_get_fill_rule (cairo_t *cr)
3558 {
3559     if (unlikely (cr->status))
3560         return CAIRO_GSTATE_FILL_RULE_DEFAULT;
3561
3562     return cr->backend->get_fill_rule (cr);
3563 }
3564
3565 /**
3566  * cairo_get_line_width:
3567  * @cr: a cairo context
3568  *
3569  * This function returns the current line width value exactly as set by
3570  * cairo_set_line_width(). Note that the value is unchanged even if
3571  * the CTM has changed between the calls to cairo_set_line_width() and
3572  * cairo_get_line_width().
3573  *
3574  * Return value: the current line width.
3575  **/
3576 double
3577 cairo_get_line_width (cairo_t *cr)
3578 {
3579     if (unlikely (cr->status))
3580         return CAIRO_GSTATE_LINE_WIDTH_DEFAULT;
3581
3582     return cr->backend->get_line_width (cr);
3583 }
3584 slim_hidden_def (cairo_get_line_width);
3585
3586 /**
3587  * cairo_get_line_cap:
3588  * @cr: a cairo context
3589  *
3590  * Gets the current line cap style, as set by cairo_set_line_cap().
3591  *
3592  * Return value: the current line cap style.
3593  **/
3594 cairo_line_cap_t
3595 cairo_get_line_cap (cairo_t *cr)
3596 {
3597     if (unlikely (cr->status))
3598         return CAIRO_GSTATE_LINE_CAP_DEFAULT;
3599
3600     return cr->backend->get_line_cap (cr);
3601 }
3602
3603 /**
3604  * cairo_get_line_join:
3605  * @cr: a cairo context
3606  *
3607  * Gets the current line join style, as set by cairo_set_line_join().
3608  *
3609  * Return value: the current line join style.
3610  **/
3611 cairo_line_join_t
3612 cairo_get_line_join (cairo_t *cr)
3613 {
3614     if (unlikely (cr->status))
3615         return CAIRO_GSTATE_LINE_JOIN_DEFAULT;
3616
3617     return cr->backend->get_line_join (cr);
3618 }
3619
3620 /**
3621  * cairo_get_miter_limit:
3622  * @cr: a cairo context
3623  *
3624  * Gets the current miter limit, as set by cairo_set_miter_limit().
3625  *
3626  * Return value: the current miter limit.
3627  **/
3628 double
3629 cairo_get_miter_limit (cairo_t *cr)
3630 {
3631     if (unlikely (cr->status))
3632         return CAIRO_GSTATE_MITER_LIMIT_DEFAULT;
3633
3634     return cr->backend->get_miter_limit (cr);
3635 }
3636
3637 /**
3638  * cairo_get_matrix:
3639  * @cr: a cairo context
3640  * @matrix: return value for the matrix
3641  *
3642  * Stores the current transformation matrix (CTM) into @matrix.
3643  **/
3644 void
3645 cairo_get_matrix (cairo_t *cr, cairo_matrix_t *matrix)
3646 {
3647     if (unlikely (cr->status)) {
3648         cairo_matrix_init_identity (matrix);
3649         return;
3650     }
3651
3652     cr->backend->get_matrix (cr, matrix);
3653 }
3654 slim_hidden_def (cairo_get_matrix);
3655
3656 /**
3657  * cairo_get_target:
3658  * @cr: a cairo context
3659  *
3660  * Gets the target surface for the cairo context as passed to
3661  * cairo_create().
3662  *
3663  * This function will always return a valid pointer, but the result
3664  * can be a "nil" surface if @cr is already in an error state,
3665  * (ie. cairo_status() <literal>!=</literal> %CAIRO_STATUS_SUCCESS).
3666  * A nil surface is indicated by cairo_surface_status()
3667  * <literal>!=</literal> %CAIRO_STATUS_SUCCESS.
3668  *
3669  * Return value: the target surface. This object is owned by cairo. To
3670  * keep a reference to it, you must call cairo_surface_reference().
3671  **/
3672 cairo_surface_t *
3673 cairo_get_target (cairo_t *cr)
3674 {
3675     if (unlikely (cr->status))
3676         return _cairo_surface_create_in_error (cr->status);
3677
3678     return cr->backend->get_original_target (cr);
3679 }
3680 slim_hidden_def (cairo_get_target);
3681
3682 /**
3683  * cairo_get_group_target:
3684  * @cr: a cairo context
3685  *
3686  * Gets the current destination surface for the context. This is either
3687  * the original target surface as passed to cairo_create() or the target
3688  * surface for the current group as started by the most recent call to
3689  * cairo_push_group() or cairo_push_group_with_content().
3690  *
3691  * This function will always return a valid pointer, but the result
3692  * can be a "nil" surface if @cr is already in an error state,
3693  * (ie. cairo_status() <literal>!=</literal> %CAIRO_STATUS_SUCCESS).
3694  * A nil surface is indicated by cairo_surface_status()
3695  * <literal>!=</literal> %CAIRO_STATUS_SUCCESS.
3696  *
3697  * Return value: the target surface. This object is owned by cairo. To
3698  * keep a reference to it, you must call cairo_surface_reference().
3699  *
3700  * Since: 1.2
3701  **/
3702 cairo_surface_t *
3703 cairo_get_group_target (cairo_t *cr)
3704 {
3705     if (unlikely (cr->status))
3706         return _cairo_surface_create_in_error (cr->status);
3707
3708     return cr->backend->get_current_target (cr);
3709 }
3710
3711 /**
3712  * cairo_copy_path:
3713  * @cr: a cairo context
3714  *
3715  * Creates a copy of the current path and returns it to the user as a
3716  * #cairo_path_t. See #cairo_path_data_t for hints on how to iterate
3717  * over the returned data structure.
3718  *
3719  * This function will always return a valid pointer, but the result
3720  * will have no data (<literal>data==%NULL</literal> and
3721  * <literal>num_data==0</literal>), if either of the following
3722  * conditions hold:
3723  *
3724  * <orderedlist>
3725  * <listitem>If there is insufficient memory to copy the path. In this
3726  *     case <literal>path->status</literal> will be set to
3727  *     %CAIRO_STATUS_NO_MEMORY.</listitem>
3728  * <listitem>If @cr is already in an error state. In this case
3729  *    <literal>path->status</literal> will contain the same status that
3730  *    would be returned by cairo_status().</listitem>
3731  * </orderedlist>
3732  *
3733  * Return value: the copy of the current path. The caller owns the
3734  * returned object and should call cairo_path_destroy() when finished
3735  * with it.
3736  **/
3737 cairo_path_t *
3738 cairo_copy_path (cairo_t *cr)
3739 {
3740     if (unlikely (cr->status))
3741         return _cairo_path_create_in_error (cr->status);
3742
3743     return cr->backend->copy_path (cr);
3744 }
3745
3746 /**
3747  * cairo_copy_path_flat:
3748  * @cr: a cairo context
3749  *
3750  * Gets a flattened copy of the current path and returns it to the
3751  * user as a #cairo_path_t. See #cairo_path_data_t for hints on
3752  * how to iterate over the returned data structure.
3753  *
3754  * This function is like cairo_copy_path() except that any curves
3755  * in the path will be approximated with piecewise-linear
3756  * approximations, (accurate to within the current tolerance
3757  * value). That is, the result is guaranteed to not have any elements
3758  * of type %CAIRO_PATH_CURVE_TO which will instead be replaced by a
3759  * series of %CAIRO_PATH_LINE_TO elements.
3760  *
3761  * This function will always return a valid pointer, but the result
3762  * will have no data (<literal>data==%NULL</literal> and
3763  * <literal>num_data==0</literal>), if either of the following
3764  * conditions hold:
3765  *
3766  * <orderedlist>
3767  * <listitem>If there is insufficient memory to copy the path. In this
3768  *     case <literal>path->status</literal> will be set to
3769  *     %CAIRO_STATUS_NO_MEMORY.</listitem>
3770  * <listitem>If @cr is already in an error state. In this case
3771  *    <literal>path->status</literal> will contain the same status that
3772  *    would be returned by cairo_status().</listitem>
3773  * </orderedlist>
3774  *
3775  * Return value: the copy of the current path. The caller owns the
3776  * returned object and should call cairo_path_destroy() when finished
3777  * with it.
3778  **/
3779 cairo_path_t *
3780 cairo_copy_path_flat (cairo_t *cr)
3781 {
3782     if (unlikely (cr->status))
3783         return _cairo_path_create_in_error (cr->status);
3784
3785     return cr->backend->copy_path_flat (cr);
3786 }
3787
3788 /**
3789  * cairo_append_path:
3790  * @cr: a cairo context
3791  * @path: path to be appended
3792  *
3793  * Append the @path onto the current path. The @path may be either the
3794  * return value from one of cairo_copy_path() or
3795  * cairo_copy_path_flat() or it may be constructed manually.  See
3796  * #cairo_path_t for details on how the path data structure should be
3797  * initialized, and note that <literal>path->status</literal> must be
3798  * initialized to %CAIRO_STATUS_SUCCESS.
3799  **/
3800 void
3801 cairo_append_path (cairo_t              *cr,
3802                    const cairo_path_t   *path)
3803 {
3804     cairo_status_t status;
3805
3806     if (unlikely (cr->status))
3807         return;
3808
3809     if (unlikely (path == NULL)) {
3810         _cairo_set_error (cr, CAIRO_STATUS_NULL_POINTER);
3811         return;
3812     }
3813
3814     if (unlikely (path->status)) {
3815         if (path->status > CAIRO_STATUS_SUCCESS &&
3816             path->status <= CAIRO_STATUS_LAST_STATUS)
3817             _cairo_set_error (cr, path->status);
3818         else
3819             _cairo_set_error (cr, CAIRO_STATUS_INVALID_STATUS);
3820         return;
3821     }
3822
3823     if (path->num_data == 0)
3824         return;
3825
3826     if (unlikely (path->data == NULL)) {
3827         _cairo_set_error (cr, CAIRO_STATUS_NULL_POINTER);
3828         return;
3829     }
3830
3831     status = cr->backend->append_path (cr, path);
3832     if (unlikely (status))
3833         _cairo_set_error (cr, status);
3834 }
3835
3836 /**
3837  * cairo_status:
3838  * @cr: a cairo context
3839  *
3840  * Checks whether an error has previously occurred for this context.
3841  *
3842  * Returns: the current status of this context, see #cairo_status_t
3843  **/
3844 cairo_status_t
3845 cairo_status (cairo_t *cr)
3846 {
3847     return cr->status;
3848 }
3849 slim_hidden_def (cairo_status);