Fixed texturing in Gallium.
[cairo/gpu.git] / src / cairo.c
blobb3185f409810d2f0df2ffce1492a4bc7b15a49e9
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
4 * Copyright © 2002 University of Southern California
5 * Copyright © 2005 Red Hat, Inc.
7 * This library is free software; you can redistribute it and/or
8 * modify it either under the terms of the GNU Lesser General Public
9 * License version 2.1 as published by the Free Software Foundation
10 * (the "LGPL") or, at your option, under the terms of the Mozilla
11 * Public License Version 1.1 (the "MPL"). If you do not alter this
12 * notice, a recipient may use your version of this file under either
13 * the MPL or the LGPL.
15 * You should have received a copy of the LGPL along with this library
16 * in the file COPYING-LGPL-2.1; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 * You should have received a copy of the MPL along with this library
19 * in the file COPYING-MPL-1.1
21 * The contents of this file are subject to the Mozilla Public License
22 * Version 1.1 (the "License"); you may not use this file except in
23 * compliance with the License. You may obtain a copy of the License at
24 * http://www.mozilla.org/MPL/
26 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
27 * OF ANY KIND, either express or implied. See the LGPL or the MPL for
28 * the specific language governing rights and limitations.
30 * The Original Code is the cairo graphics library.
32 * The Initial Developer of the Original Code is University of Southern
33 * California.
35 * Contributor(s):
36 * Carl D. Worth <cworth@cworth.org>
39 #include "cairoint.h"
40 #include "cairo-private.h"
42 #include "cairo-arc-private.h"
43 #include "cairo-path-private.h"
45 #define CAIRO_TOLERANCE_MINIMUM _cairo_fixed_to_double(1)
47 static const cairo_t _cairo_nil = {
48 CAIRO_REFERENCE_COUNT_INVALID, /* ref_count */
49 CAIRO_STATUS_NO_MEMORY, /* status */
50 { 0, 0, 0, NULL }, /* user_data */
51 NULL, /* gstate */
52 {{ 0 }, { 0 }}, /* gstate_tail */
53 NULL, /* gstate_freelist */
54 {{ /* path */
55 { 0, 0 }, /* last_move_point */
56 { 0, 0 }, /* current point */
57 FALSE, /* has_current_point */
58 FALSE, /* has_curve_to */
59 NULL, {{NULL}} /* buf_tail, buf_head */
63 #include <assert.h>
65 /**
66 * _cairo_error:
67 * @status: a status value indicating an error, (eg. not
68 * %CAIRO_STATUS_SUCCESS)
70 * Checks that status is an error status, but does nothing else.
72 * All assignments of an error status to any user-visible object
73 * within the cairo application should result in a call to
74 * _cairo_error().
76 * The purpose of this function is to allow the user to set a
77 * breakpoint in _cairo_error() to generate a stack trace for when the
78 * user causes cairo to detect an error.
80 * Return value: the error status.
81 **/
82 cairo_status_t
83 _cairo_error (cairo_status_t status)
85 CAIRO_ENSURE_UNIQUE;
86 assert (_cairo_status_is_error (status));
88 return status;
91 /**
92 * _cairo_set_error:
93 * @cr: a cairo context
94 * @status: a status value indicating an error
96 * Atomically sets cr->status to @status and calls _cairo_error;
97 * Does nothing if status is %CAIRO_STATUS_SUCCESS.
99 * All assignments of an error status to cr->status should happen
100 * through _cairo_set_error(). Note that due to the nature of the atomic
101 * operation, it is not safe to call this function on the nil objects.
103 * The purpose of this function is to allow the user to set a
104 * breakpoint in _cairo_error() to generate a stack trace for when the
105 * user causes cairo to detect an error.
107 static void
108 _cairo_set_error (cairo_t *cr, cairo_status_t status)
110 /* Don't overwrite an existing error. This preserves the first
111 * error, which is the most significant. */
112 _cairo_status_set_error (&cr->status, _cairo_error (status));
115 #if HAS_ATOMIC_OPS
116 /* We keep a small stash of contexts to reduce malloc pressure */
117 #define CAIRO_STASH_SIZE 4
118 static struct {
119 cairo_t pool[CAIRO_STASH_SIZE];
120 int occupied;
121 } _context_stash;
123 static cairo_t *
124 _context_get (void)
126 int avail, old, new;
128 do {
129 old = _context_stash.occupied;
130 avail = ffs (~old) - 1;
131 if (avail >= CAIRO_STASH_SIZE)
132 return malloc (sizeof (cairo_t));
134 new = old | (1 << avail);
135 } while (_cairo_atomic_int_cmpxchg (&_context_stash.occupied, old, new) != old);
137 return &_context_stash.pool[avail];
140 static void
141 _context_put (cairo_t *cr)
143 int old, new, avail;
145 if (cr < &_context_stash.pool[0] ||
146 cr >= &_context_stash.pool[CAIRO_STASH_SIZE])
148 free (cr);
149 return;
152 avail = ~(1 << (cr - &_context_stash.pool[0]));
153 do {
154 old = _context_stash.occupied;
155 new = old & avail;
156 } while (_cairo_atomic_int_cmpxchg (&_context_stash.occupied, old, new) != old);
158 #else
159 #define _context_get() malloc (sizeof (cairo_t))
160 #define _context_put(cr) free (cr)
161 #endif
164 * cairo_create:
165 * @target: target surface for the context
167 * Creates a new #cairo_t with all graphics state parameters set to
168 * default values and with @target as a target surface. The target
169 * surface should be constructed with a backend-specific function such
170 * as cairo_image_surface_create() (or any other
171 * cairo_<emphasis>backend</emphasis>_surface_create() variant).
173 * This function references @target, so you can immediately
174 * call cairo_surface_destroy() on it if you don't need to
175 * maintain a separate reference to it.
177 * Return value: a newly allocated #cairo_t with a reference
178 * count of 1. The initial reference count should be released
179 * with cairo_destroy() when you are done using the #cairo_t.
180 * This function never returns %NULL. If memory cannot be
181 * allocated, a special #cairo_t object will be returned on
182 * which cairo_status() returns %CAIRO_STATUS_NO_MEMORY.
183 * You can use this object normally, but no drawing will
184 * be done.
186 cairo_t *
187 cairo_create (cairo_surface_t *target)
189 cairo_t *cr;
190 cairo_status_t status;
192 /* special case OOM in order to avoid another allocation */
193 if (target && target->status == CAIRO_STATUS_NO_MEMORY)
194 return (cairo_t *) &_cairo_nil;
196 cr = _context_get ();
197 if (unlikely (cr == NULL)) {
198 status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
199 return (cairo_t *) &_cairo_nil;
202 CAIRO_REFERENCE_COUNT_INIT (&cr->ref_count, 1);
204 cr->status = CAIRO_STATUS_SUCCESS;
206 _cairo_user_data_array_init (&cr->user_data);
207 _cairo_path_fixed_init (cr->path);
209 cr->gstate = &cr->gstate_tail[0];
210 cr->gstate_freelist = &cr->gstate_tail[1];
211 cr->gstate_tail[1].next = NULL;
213 status = _cairo_gstate_init (cr->gstate, target);
214 if (unlikely (status))
215 _cairo_set_error (cr, status);
217 return cr;
219 slim_hidden_def (cairo_create);
222 * cairo_reference:
223 * @cr: a #cairo_t
225 * Increases the reference count on @cr by one. This prevents
226 * @cr from being destroyed until a matching call to cairo_destroy()
227 * is made.
229 * The number of references to a #cairo_t can be get using
230 * cairo_get_reference_count().
232 * Return value: the referenced #cairo_t.
234 cairo_t *
235 cairo_reference (cairo_t *cr)
237 if (cr == NULL || CAIRO_REFERENCE_COUNT_IS_INVALID (&cr->ref_count))
238 return cr;
240 assert (CAIRO_REFERENCE_COUNT_HAS_REFERENCE (&cr->ref_count));
242 _cairo_reference_count_inc (&cr->ref_count);
244 return cr;
248 * cairo_destroy:
249 * @cr: a #cairo_t
251 * Decreases the reference count on @cr by one. If the result
252 * is zero, then @cr and all associated resources are freed.
253 * See cairo_reference().
255 void
256 cairo_destroy (cairo_t *cr)
258 cairo_surface_t *surface;
260 if (cr == NULL || CAIRO_REFERENCE_COUNT_IS_INVALID (&cr->ref_count))
261 return;
263 assert (CAIRO_REFERENCE_COUNT_HAS_REFERENCE (&cr->ref_count));
265 if (! _cairo_reference_count_dec_and_test (&cr->ref_count))
266 return;
268 while (cr->gstate != &cr->gstate_tail[0]) {
269 if (_cairo_gstate_restore (&cr->gstate, &cr->gstate_freelist))
270 break;
273 /* The context is expected (>99% of all use cases) to be held for the
274 * duration of a single expose event/sequence of graphic operations.
275 * Therefore, on destroy we explicitly flush the Cairo pipeline of any
276 * pending operations.
278 surface = _cairo_gstate_get_original_target (cr->gstate);
279 if (surface != NULL)
280 cairo_surface_flush (surface);
282 _cairo_gstate_fini (cr->gstate);
283 cr->gstate_freelist = cr->gstate_freelist->next; /* skip over tail[1] */
284 while (cr->gstate_freelist != NULL) {
285 cairo_gstate_t *gstate = cr->gstate_freelist;
286 cr->gstate_freelist = gstate->next;
287 free (gstate);
290 _cairo_path_fixed_fini (cr->path);
292 _cairo_user_data_array_fini (&cr->user_data);
294 _context_put (cr);
296 slim_hidden_def (cairo_destroy);
299 * cairo_get_user_data:
300 * @cr: a #cairo_t
301 * @key: the address of the #cairo_user_data_key_t the user data was
302 * attached to
304 * Return user data previously attached to @cr using the specified
305 * key. If no user data has been attached with the given key this
306 * function returns %NULL.
308 * Return value: the user data previously attached or %NULL.
310 * Since: 1.4
312 void *
313 cairo_get_user_data (cairo_t *cr,
314 const cairo_user_data_key_t *key)
316 return _cairo_user_data_array_get_data (&cr->user_data,
317 key);
321 * cairo_set_user_data:
322 * @cr: a #cairo_t
323 * @key: the address of a #cairo_user_data_key_t to attach the user data to
324 * @user_data: the user data to attach to the #cairo_t
325 * @destroy: a #cairo_destroy_func_t which will be called when the
326 * #cairo_t is destroyed or when new user data is attached using the
327 * same key.
329 * Attach user data to @cr. To remove user data from a surface,
330 * call this function with the key that was used to set it and %NULL
331 * for @data.
333 * Return value: %CAIRO_STATUS_SUCCESS or %CAIRO_STATUS_NO_MEMORY if a
334 * slot could not be allocated for the user data.
336 * Since: 1.4
338 cairo_status_t
339 cairo_set_user_data (cairo_t *cr,
340 const cairo_user_data_key_t *key,
341 void *user_data,
342 cairo_destroy_func_t destroy)
344 if (CAIRO_REFERENCE_COUNT_IS_INVALID (&cr->ref_count))
345 return cr->status;
347 return _cairo_user_data_array_set_data (&cr->user_data,
348 key, user_data, destroy);
352 * cairo_get_reference_count:
353 * @cr: a #cairo_t
355 * Returns the current reference count of @cr.
357 * Return value: the current reference count of @cr. If the
358 * object is a nil object, 0 will be returned.
360 * Since: 1.4
362 unsigned int
363 cairo_get_reference_count (cairo_t *cr)
365 if (cr == NULL || CAIRO_REFERENCE_COUNT_IS_INVALID (&cr->ref_count))
366 return 0;
368 return CAIRO_REFERENCE_COUNT_GET_VALUE (&cr->ref_count);
372 * cairo_save:
373 * @cr: a #cairo_t
375 * Makes a copy of the current state of @cr and saves it
376 * on an internal stack of saved states for @cr. When
377 * cairo_restore() is called, @cr will be restored to
378 * the saved state. Multiple calls to cairo_save() and
379 * cairo_restore() can be nested; each call to cairo_restore()
380 * restores the state from the matching paired cairo_save().
382 * It isn't necessary to clear all saved states before
383 * a #cairo_t is freed. If the reference count of a #cairo_t
384 * drops to zero in response to a call to cairo_destroy(),
385 * any saved states will be freed along with the #cairo_t.
387 void
388 cairo_save (cairo_t *cr)
390 cairo_status_t status;
392 if (unlikely (cr->status))
393 return;
395 status = _cairo_gstate_save (&cr->gstate, &cr->gstate_freelist);
396 if (unlikely (status))
397 _cairo_set_error (cr, status);
399 slim_hidden_def(cairo_save);
402 * cairo_restore:
403 * @cr: a #cairo_t
405 * Restores @cr to the state saved by a preceding call to
406 * cairo_save() and removes that state from the stack of
407 * saved states.
409 void
410 cairo_restore (cairo_t *cr)
412 cairo_status_t status;
414 if (unlikely (cr->status))
415 return;
417 status = _cairo_gstate_restore (&cr->gstate, &cr->gstate_freelist);
418 if (unlikely (status))
419 _cairo_set_error (cr, status);
421 slim_hidden_def(cairo_restore);
424 * cairo_push_group:
425 * @cr: a cairo context
427 * Temporarily redirects drawing to an intermediate surface known as a
428 * group. The redirection lasts until the group is completed by a call
429 * to cairo_pop_group() or cairo_pop_group_to_source(). These calls
430 * provide the result of any drawing to the group as a pattern,
431 * (either as an explicit object, or set as the source pattern).
433 * This group functionality can be convenient for performing
434 * intermediate compositing. One common use of a group is to render
435 * objects as opaque within the group, (so that they occlude each
436 * other), and then blend the result with translucence onto the
437 * destination.
439 * Groups can be nested arbitrarily deep by making balanced calls to
440 * cairo_push_group()/cairo_pop_group(). Each call pushes/pops the new
441 * target group onto/from a stack.
443 * The cairo_push_group() function calls cairo_save() so that any
444 * changes to the graphics state will not be visible outside the
445 * group, (the pop_group functions call cairo_restore()).
447 * By default the intermediate group will have a content type of
448 * %CAIRO_CONTENT_COLOR_ALPHA. Other content types can be chosen for
449 * the group by using cairo_push_group_with_content() instead.
451 * As an example, here is how one might fill and stroke a path with
452 * translucence, but without any portion of the fill being visible
453 * under the stroke:
455 * <informalexample><programlisting>
456 * cairo_push_group (cr);
457 * cairo_set_source (cr, fill_pattern);
458 * cairo_fill_preserve (cr);
459 * cairo_set_source (cr, stroke_pattern);
460 * cairo_stroke (cr);
461 * cairo_pop_group_to_source (cr);
462 * cairo_paint_with_alpha (cr, alpha);
463 * </programlisting></informalexample>
465 * Since: 1.2
467 void
468 cairo_push_group (cairo_t *cr)
470 cairo_push_group_with_content (cr, CAIRO_CONTENT_COLOR_ALPHA);
474 * cairo_push_group_with_content:
475 * @cr: a cairo context
476 * @content: a #cairo_content_t indicating the type of group that
477 * will be created
479 * Temporarily redirects drawing to an intermediate surface known as a
480 * group. The redirection lasts until the group is completed by a call
481 * to cairo_pop_group() or cairo_pop_group_to_source(). These calls
482 * provide the result of any drawing to the group as a pattern,
483 * (either as an explicit object, or set as the source pattern).
485 * The group will have a content type of @content. The ability to
486 * control this content type is the only distinction between this
487 * function and cairo_push_group() which you should see for a more
488 * detailed description of group rendering.
490 * Since: 1.2
492 void
493 cairo_push_group_with_content (cairo_t *cr, cairo_content_t content)
495 cairo_status_t status;
496 cairo_rectangle_int_t extents;
497 cairo_surface_t *parent_surface, *group_surface = NULL;
499 if (unlikely (cr->status))
500 return;
502 parent_surface = _cairo_gstate_get_target (cr->gstate);
503 /* Get the extents that we'll use in creating our new group surface */
504 status = _cairo_surface_get_extents (parent_surface, &extents);
505 if (unlikely (status))
506 goto bail;
507 status = _cairo_clip_intersect_to_rectangle (_cairo_gstate_get_clip (cr->gstate), &extents);
508 if (unlikely (status))
509 goto bail;
511 group_surface = cairo_surface_create_similar (parent_surface,
512 content,
513 extents.width,
514 extents.height);
515 status = cairo_surface_status (group_surface);
516 if (unlikely (status))
517 goto bail;
519 /* Set device offsets on the new surface so that logically it appears at
520 * the same location on the parent surface -- when we pop_group this,
521 * the source pattern will get fixed up for the appropriate target surface
522 * device offsets, so we want to set our own surface offsets from /that/,
523 * and not from the device origin. */
524 cairo_surface_set_device_offset (group_surface,
525 parent_surface->device_transform.x0 - extents.x,
526 parent_surface->device_transform.y0 - extents.y);
528 /* If we have a current path, we need to adjust it to compensate for
529 * the device offset just applied. */
530 _cairo_path_fixed_transform (cr->path,
531 &group_surface->device_transform);
533 /* create a new gstate for the redirect */
534 cairo_save (cr);
535 if (unlikely (cr->status))
536 goto bail;
538 status = _cairo_gstate_redirect_target (cr->gstate, group_surface);
540 bail:
541 cairo_surface_destroy (group_surface);
542 if (unlikely (status))
543 _cairo_set_error (cr, status);
545 slim_hidden_def(cairo_push_group_with_content);
548 * cairo_pop_group:
549 * @cr: a cairo context
551 * Terminates the redirection begun by a call to cairo_push_group() or
552 * cairo_push_group_with_content() and returns a new pattern
553 * containing the results of all drawing operations performed to the
554 * group.
556 * The cairo_pop_group() function calls cairo_restore(), (balancing a
557 * call to cairo_save() by the push_group function), so that any
558 * changes to the graphics state will not be visible outside the
559 * group.
561 * Return value: a newly created (surface) pattern containing the
562 * results of all drawing operations performed to the group. The
563 * caller owns the returned object and should call
564 * cairo_pattern_destroy() when finished with it.
566 * Since: 1.2
568 cairo_pattern_t *
569 cairo_pop_group (cairo_t *cr)
571 cairo_surface_t *group_surface, *parent_target;
572 cairo_pattern_t *group_pattern;
573 cairo_matrix_t group_matrix;
574 cairo_status_t status;
576 if (unlikely (cr->status))
577 return _cairo_pattern_create_in_error (cr->status);
579 /* Grab the active surfaces */
580 group_surface = _cairo_gstate_get_target (cr->gstate);
581 parent_target = _cairo_gstate_get_parent_target (cr->gstate);
583 /* Verify that we are at the right nesting level */
584 if (parent_target == NULL) {
585 _cairo_set_error (cr, CAIRO_STATUS_INVALID_POP_GROUP);
586 return _cairo_pattern_create_in_error (CAIRO_STATUS_INVALID_POP_GROUP);
589 /* We need to save group_surface before we restore; we don't need
590 * to reference parent_target and original_target, since the
591 * gstate will still hold refs to them once we restore. */
592 group_surface = cairo_surface_reference (group_surface);
594 cairo_restore (cr);
596 if (unlikely (cr->status)) {
597 group_pattern = _cairo_pattern_create_in_error (cr->status);
598 goto done;
601 group_pattern = cairo_pattern_create_for_surface (group_surface);
602 status = group_pattern->status;
603 if (unlikely (status)) {
604 _cairo_set_error (cr, status);
605 goto done;
608 _cairo_gstate_get_matrix (cr->gstate, &group_matrix);
609 /* Transform by group_matrix centered around device_transform so that when
610 * we call _cairo_gstate_copy_transformed_pattern the result is a pattern
611 * with a matrix equivalent to the device_transform of group_surface. */
612 if (_cairo_surface_has_device_transform (group_surface)) {
613 cairo_pattern_set_matrix (group_pattern, &group_surface->device_transform);
614 _cairo_pattern_transform (group_pattern, &group_matrix);
615 _cairo_pattern_transform (group_pattern, &group_surface->device_transform_inverse);
616 } else {
617 cairo_pattern_set_matrix (group_pattern, &group_matrix);
620 /* If we have a current path, we need to adjust it to compensate for
621 * the device offset just removed. */
622 _cairo_path_fixed_transform (cr->path,
623 &group_surface->device_transform_inverse);
625 done:
626 cairo_surface_destroy (group_surface);
628 return group_pattern;
630 slim_hidden_def(cairo_pop_group);
633 * cairo_pop_group_to_source:
634 * @cr: a cairo context
636 * Terminates the redirection begun by a call to cairo_push_group() or
637 * cairo_push_group_with_content() and installs the resulting pattern
638 * as the source pattern in the given cairo context.
640 * The behavior of this function is equivalent to the sequence of
641 * operations:
643 * <informalexample><programlisting>
644 * #cairo_pattern_t *group = cairo_pop_group (cr);
645 * cairo_set_source (cr, group);
646 * cairo_pattern_destroy (group);
647 * </programlisting></informalexample>
649 * but is more convenient as their is no need for a variable to store
650 * the short-lived pointer to the pattern.
652 * The cairo_pop_group() function calls cairo_restore(), (balancing a
653 * call to cairo_save() by the push_group function), so that any
654 * changes to the graphics state will not be visible outside the
655 * group.
657 * Since: 1.2
659 void
660 cairo_pop_group_to_source (cairo_t *cr)
662 cairo_pattern_t *group_pattern;
664 group_pattern = cairo_pop_group (cr);
665 cairo_set_source (cr, group_pattern);
666 cairo_pattern_destroy (group_pattern);
670 * cairo_set_operator:
671 * @cr: a #cairo_t
672 * @op: a compositing operator, specified as a #cairo_operator_t
674 * Sets the compositing operator to be used for all drawing
675 * operations. See #cairo_operator_t for details on the semantics of
676 * each available compositing operator.
678 * The default operator is %CAIRO_OPERATOR_OVER.
680 void
681 cairo_set_operator (cairo_t *cr, cairo_operator_t op)
683 cairo_status_t status;
685 if (unlikely (cr->status))
686 return;
688 status = _cairo_gstate_set_operator (cr->gstate, op);
689 if (unlikely (status))
690 _cairo_set_error (cr, status);
692 slim_hidden_def (cairo_set_operator);
695 static cairo_bool_t
696 _current_source_matches_solid (cairo_t *cr,
697 double red,
698 double green,
699 double blue,
700 double alpha)
702 const cairo_pattern_t *current;
703 cairo_color_t color;
705 current = cr->gstate->source;
706 if (current->type != CAIRO_PATTERN_TYPE_SOLID)
707 return FALSE;
709 red = _cairo_restrict_value (red, 0.0, 1.0);
710 green = _cairo_restrict_value (green, 0.0, 1.0);
711 blue = _cairo_restrict_value (blue, 0.0, 1.0);
712 alpha = _cairo_restrict_value (alpha, 0.0, 1.0);
714 _cairo_color_init_rgba (&color, red, green, blue, alpha);
715 return _cairo_color_equal (&color,
716 &((cairo_solid_pattern_t *) current)->color);
719 * cairo_set_source_rgb
720 * @cr: a cairo context
721 * @red: red component of color
722 * @green: green component of color
723 * @blue: blue component of color
725 * Sets the source pattern within @cr to an opaque color. This opaque
726 * color will then be used for any subsequent drawing operation until
727 * a new source pattern is set.
729 * The color components are floating point numbers in the range 0 to
730 * 1. If the values passed in are outside that range, they will be
731 * clamped.
733 * The default source pattern is opaque black, (that is, it is
734 * equivalent to cairo_set_source_rgb(cr, 0.0, 0.0, 0.0)).
736 void
737 cairo_set_source_rgb (cairo_t *cr, double red, double green, double blue)
739 cairo_pattern_t *pattern;
741 if (unlikely (cr->status))
742 return;
744 if (_current_source_matches_solid (cr, red, green, blue, 1.))
745 return;
747 /* push the current pattern to the freed lists */
748 cairo_set_source (cr, (cairo_pattern_t *) &_cairo_pattern_black);
750 pattern = cairo_pattern_create_rgb (red, green, blue);
751 cairo_set_source (cr, pattern);
752 cairo_pattern_destroy (pattern);
754 slim_hidden_def (cairo_set_source_rgb);
757 * cairo_set_source_rgba:
758 * @cr: a cairo context
759 * @red: red component of color
760 * @green: green component of color
761 * @blue: blue component of color
762 * @alpha: alpha component of color
764 * Sets the source pattern within @cr to a translucent color. This
765 * color will then be used for any subsequent drawing operation until
766 * a new source pattern is set.
768 * The color and alpha components are floating point numbers in the
769 * range 0 to 1. If the values passed in are outside that range, they
770 * will be clamped.
772 * The default source pattern is opaque black, (that is, it is
773 * equivalent to cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 1.0)).
775 void
776 cairo_set_source_rgba (cairo_t *cr,
777 double red, double green, double blue,
778 double alpha)
780 cairo_pattern_t *pattern;
782 if (unlikely (cr->status))
783 return;
785 if (_current_source_matches_solid (cr, red, green, blue, alpha))
786 return;
788 /* push the current pattern to the freed lists */
789 cairo_set_source (cr, (cairo_pattern_t *) &_cairo_pattern_black);
791 pattern = cairo_pattern_create_rgba (red, green, blue, alpha);
792 cairo_set_source (cr, pattern);
793 cairo_pattern_destroy (pattern);
797 * cairo_set_source_surface:
798 * @cr: a cairo context
799 * @surface: a surface to be used to set the source pattern
800 * @x: User-space X coordinate for surface origin
801 * @y: User-space Y coordinate for surface origin
803 * This is a convenience function for creating a pattern from @surface
804 * and setting it as the source in @cr with cairo_set_source().
806 * The @x and @y parameters give the user-space coordinate at which
807 * the surface origin should appear. (The surface origin is its
808 * upper-left corner before any transformation has been applied.) The
809 * @x and @y patterns are negated and then set as translation values
810 * in the pattern matrix.
812 * Other than the initial translation pattern matrix, as described
813 * above, all other pattern attributes, (such as its extend mode), are
814 * set to the default values as in cairo_pattern_create_for_surface().
815 * The resulting pattern can be queried with cairo_get_source() so
816 * that these attributes can be modified if desired, (eg. to create a
817 * repeating pattern with cairo_pattern_set_extend()).
819 void
820 cairo_set_source_surface (cairo_t *cr,
821 cairo_surface_t *surface,
822 double x,
823 double y)
825 cairo_pattern_t *pattern;
826 cairo_matrix_t matrix;
828 if (unlikely (cr->status))
829 return;
831 /* push the current pattern to the freed lists */
832 cairo_set_source (cr, (cairo_pattern_t *) &_cairo_pattern_black);
834 pattern = cairo_pattern_create_for_surface (surface);
836 cairo_matrix_init_translate (&matrix, -x, -y);
837 cairo_pattern_set_matrix (pattern, &matrix);
839 cairo_set_source (cr, pattern);
840 cairo_pattern_destroy (pattern);
842 slim_hidden_def (cairo_set_source_surface);
845 * cairo_set_source
846 * @cr: a cairo context
847 * @source: a #cairo_pattern_t to be used as the source for
848 * subsequent drawing operations.
850 * Sets the source pattern within @cr to @source. This pattern
851 * will then be used for any subsequent drawing operation until a new
852 * source pattern is set.
854 * Note: The pattern's transformation matrix will be locked to the
855 * user space in effect at the time of cairo_set_source(). This means
856 * that further modifications of the current transformation matrix
857 * will not affect the source pattern. See cairo_pattern_set_matrix().
859 * The default source pattern is a solid pattern that is opaque black,
860 * (that is, it is equivalent to cairo_set_source_rgb(cr, 0.0, 0.0,
861 * 0.0)).
863 void
864 cairo_set_source (cairo_t *cr, cairo_pattern_t *source)
866 cairo_status_t status;
868 if (unlikely (cr->status))
869 return;
871 if (source == NULL) {
872 _cairo_set_error (cr, CAIRO_STATUS_NULL_POINTER);
873 return;
876 if (source->status) {
877 _cairo_set_error (cr, source->status);
878 return;
881 status = _cairo_gstate_set_source (cr->gstate, source);
882 if (unlikely (status))
883 _cairo_set_error (cr, status);
885 slim_hidden_def (cairo_set_source);
888 * cairo_get_source:
889 * @cr: a cairo context
891 * Gets the current source pattern for @cr.
893 * Return value: the current source pattern. This object is owned by
894 * cairo. To keep a reference to it, you must call
895 * cairo_pattern_reference().
897 cairo_pattern_t *
898 cairo_get_source (cairo_t *cr)
900 if (unlikely (cr->status))
901 return _cairo_pattern_create_in_error (cr->status);
903 return _cairo_gstate_get_source (cr->gstate);
907 * cairo_set_tolerance:
908 * @cr: a #cairo_t
909 * @tolerance: the tolerance, in device units (typically pixels)
911 * Sets the tolerance used when converting paths into trapezoids.
912 * Curved segments of the path will be subdivided until the maximum
913 * deviation between the original path and the polygonal approximation
914 * is less than @tolerance. The default value is 0.1. A larger
915 * value will give better performance, a smaller value, better
916 * appearance. (Reducing the value from the default value of 0.1
917 * is unlikely to improve appearance significantly.) The accuracy of paths
918 * within Cairo is limited by the precision of its internal arithmetic, and
919 * the prescribed @tolerance is restricted to the smallest
920 * representable internal value.
922 void
923 cairo_set_tolerance (cairo_t *cr, double tolerance)
925 cairo_status_t status;
927 if (unlikely (cr->status))
928 return;
930 if (tolerance < CAIRO_TOLERANCE_MINIMUM)
931 tolerance = CAIRO_TOLERANCE_MINIMUM;
933 status = _cairo_gstate_set_tolerance (cr->gstate, tolerance);
934 if (unlikely (status))
935 _cairo_set_error (cr, status);
937 slim_hidden_def (cairo_set_tolerance);
940 * cairo_set_antialias:
941 * @cr: a #cairo_t
942 * @antialias: the new antialiasing mode
944 * Set the antialiasing mode of the rasterizer used for drawing shapes.
945 * This value is a hint, and a particular backend may or may not support
946 * a particular value. At the current time, no backend supports
947 * %CAIRO_ANTIALIAS_SUBPIXEL when drawing shapes.
949 * Note that this option does not affect text rendering, instead see
950 * cairo_font_options_set_antialias().
952 void
953 cairo_set_antialias (cairo_t *cr, cairo_antialias_t antialias)
955 cairo_status_t status;
957 if (unlikely (cr->status))
958 return;
960 status = _cairo_gstate_set_antialias (cr->gstate, antialias);
961 if (unlikely (status))
962 _cairo_set_error (cr, status);
966 * cairo_set_fill_rule:
967 * @cr: a #cairo_t
968 * @fill_rule: a fill rule, specified as a #cairo_fill_rule_t
970 * Set the current fill rule within the cairo context. The fill rule
971 * is used to determine which regions are inside or outside a complex
972 * (potentially self-intersecting) path. The current fill rule affects
973 * both cairo_fill() and cairo_clip(). See #cairo_fill_rule_t for details
974 * on the semantics of each available fill rule.
976 * The default fill rule is %CAIRO_FILL_RULE_WINDING.
978 void
979 cairo_set_fill_rule (cairo_t *cr, cairo_fill_rule_t fill_rule)
981 cairo_status_t status;
983 if (unlikely (cr->status))
984 return;
986 status = _cairo_gstate_set_fill_rule (cr->gstate, fill_rule);
987 if (unlikely (status))
988 _cairo_set_error (cr, status);
992 * cairo_set_line_width:
993 * @cr: a #cairo_t
994 * @width: a line width
996 * Sets the current line width within the cairo context. The line
997 * width value specifies the diameter of a pen that is circular in
998 * user space, (though device-space pen may be an ellipse in general
999 * due to scaling/shear/rotation of the CTM).
1001 * Note: When the description above refers to user space and CTM it
1002 * refers to the user space and CTM in effect at the time of the
1003 * stroking operation, not the user space and CTM in effect at the
1004 * time of the call to cairo_set_line_width(). The simplest usage
1005 * makes both of these spaces identical. That is, if there is no
1006 * change to the CTM between a call to cairo_set_line_width() and the
1007 * stroking operation, then one can just pass user-space values to
1008 * cairo_set_line_width() and ignore this note.
1010 * As with the other stroke parameters, the current line width is
1011 * examined by cairo_stroke(), cairo_stroke_extents(), and
1012 * cairo_stroke_to_path(), but does not have any effect during path
1013 * construction.
1015 * The default line width value is 2.0.
1017 void
1018 cairo_set_line_width (cairo_t *cr, double width)
1020 cairo_status_t status;
1022 if (unlikely (cr->status))
1023 return;
1025 if (width < 0.)
1026 width = 0.;
1028 status = _cairo_gstate_set_line_width (cr->gstate, width);
1029 if (unlikely (status))
1030 _cairo_set_error (cr, status);
1032 slim_hidden_def (cairo_set_line_width);
1035 * cairo_set_line_cap:
1036 * @cr: a cairo context
1037 * @line_cap: a line cap style
1039 * Sets the current line cap style within the cairo context. See
1040 * #cairo_line_cap_t for details about how the available line cap
1041 * styles are drawn.
1043 * As with the other stroke parameters, the current line cap style is
1044 * examined by cairo_stroke(), cairo_stroke_extents(), and
1045 * cairo_stroke_to_path(), but does not have any effect during path
1046 * construction.
1048 * The default line cap style is %CAIRO_LINE_CAP_BUTT.
1050 void
1051 cairo_set_line_cap (cairo_t *cr, cairo_line_cap_t line_cap)
1053 cairo_status_t status;
1055 if (unlikely (cr->status))
1056 return;
1058 status = _cairo_gstate_set_line_cap (cr->gstate, line_cap);
1059 if (unlikely (status))
1060 _cairo_set_error (cr, status);
1062 slim_hidden_def (cairo_set_line_cap);
1065 * cairo_set_line_join:
1066 * @cr: a cairo context
1067 * @line_join: a line join style
1069 * Sets the current line join style within the cairo context. See
1070 * #cairo_line_join_t for details about how the available line join
1071 * styles are drawn.
1073 * As with the other stroke parameters, the current line join style is
1074 * examined by cairo_stroke(), cairo_stroke_extents(), and
1075 * cairo_stroke_to_path(), but does not have any effect during path
1076 * construction.
1078 * The default line join style is %CAIRO_LINE_JOIN_MITER.
1080 void
1081 cairo_set_line_join (cairo_t *cr, cairo_line_join_t line_join)
1083 cairo_status_t status;
1085 if (unlikely (cr->status))
1086 return;
1088 status = _cairo_gstate_set_line_join (cr->gstate, line_join);
1089 if (unlikely (status))
1090 _cairo_set_error (cr, status);
1092 slim_hidden_def (cairo_set_line_join);
1095 * cairo_set_dash:
1096 * @cr: a cairo context
1097 * @dashes: an array specifying alternate lengths of on and off stroke portions
1098 * @num_dashes: the length of the dashes array
1099 * @offset: an offset into the dash pattern at which the stroke should start
1101 * Sets the dash pattern to be used by cairo_stroke(). A dash pattern
1102 * is specified by @dashes, an array of positive values. Each value
1103 * provides the length of alternate "on" and "off" portions of the
1104 * stroke. The @offset specifies an offset into the pattern at which
1105 * the stroke begins.
1107 * Each "on" segment will have caps applied as if the segment were a
1108 * separate sub-path. In particular, it is valid to use an "on" length
1109 * of 0.0 with %CAIRO_LINE_CAP_ROUND or %CAIRO_LINE_CAP_SQUARE in order
1110 * to distributed dots or squares along a path.
1112 * Note: The length values are in user-space units as evaluated at the
1113 * time of stroking. This is not necessarily the same as the user
1114 * space at the time of cairo_set_dash().
1116 * If @num_dashes is 0 dashing is disabled.
1118 * If @num_dashes is 1 a symmetric pattern is assumed with alternating
1119 * on and off portions of the size specified by the single value in
1120 * @dashes.
1122 * If any value in @dashes is negative, or if all values are 0, then
1123 * @cr will be put into an error state with a status of
1124 * %CAIRO_STATUS_INVALID_DASH.
1126 void
1127 cairo_set_dash (cairo_t *cr,
1128 const double *dashes,
1129 int num_dashes,
1130 double offset)
1132 cairo_status_t status;
1134 if (unlikely (cr->status))
1135 return;
1137 status = _cairo_gstate_set_dash (cr->gstate,
1138 dashes, num_dashes, offset);
1139 if (unlikely (status))
1140 _cairo_set_error (cr, status);
1144 * cairo_get_dash_count:
1145 * @cr: a #cairo_t
1147 * This function returns the length of the dash array in @cr (0 if dashing
1148 * is not currently in effect).
1150 * See also cairo_set_dash() and cairo_get_dash().
1152 * Return value: the length of the dash array, or 0 if no dash array set.
1154 * Since: 1.4
1157 cairo_get_dash_count (cairo_t *cr)
1159 int num_dashes;
1161 if (unlikely (cr->status))
1162 return 0;
1164 _cairo_gstate_get_dash (cr->gstate, NULL, &num_dashes, NULL);
1166 return num_dashes;
1170 * cairo_get_dash:
1171 * @cr: a #cairo_t
1172 * @dashes: return value for the dash array, or %NULL
1173 * @offset: return value for the current dash offset, or %NULL
1175 * Gets the current dash array. If not %NULL, @dashes should be big
1176 * enough to hold at least the number of values returned by
1177 * cairo_get_dash_count().
1179 * Since: 1.4
1181 void
1182 cairo_get_dash (cairo_t *cr,
1183 double *dashes,
1184 double *offset)
1186 if (unlikely (cr->status))
1187 return;
1189 _cairo_gstate_get_dash (cr->gstate, dashes, NULL, offset);
1193 * cairo_set_miter_limit:
1194 * @cr: a cairo context
1195 * @limit: miter limit to set
1197 * Sets the current miter limit within the cairo context.
1199 * If the current line join style is set to %CAIRO_LINE_JOIN_MITER
1200 * (see cairo_set_line_join()), the miter limit is used to determine
1201 * whether the lines should be joined with a bevel instead of a miter.
1202 * Cairo divides the length of the miter by the line width.
1203 * If the result is greater than the miter limit, the style is
1204 * converted to a bevel.
1206 * As with the other stroke parameters, the current line miter limit is
1207 * examined by cairo_stroke(), cairo_stroke_extents(), and
1208 * cairo_stroke_to_path(), but does not have any effect during path
1209 * construction.
1211 * The default miter limit value is 10.0, which will convert joins
1212 * with interior angles less than 11 degrees to bevels instead of
1213 * miters. For reference, a miter limit of 2.0 makes the miter cutoff
1214 * at 60 degrees, and a miter limit of 1.414 makes the cutoff at 90
1215 * degrees.
1217 * A miter limit for a desired angle can be computed as: miter limit =
1218 * 1/sin(angle/2)
1220 void
1221 cairo_set_miter_limit (cairo_t *cr, double limit)
1223 cairo_status_t status;
1225 if (unlikely (cr->status))
1226 return;
1228 status = _cairo_gstate_set_miter_limit (cr->gstate, limit);
1229 if (unlikely (status))
1230 _cairo_set_error (cr, status);
1234 * cairo_translate:
1235 * @cr: a cairo context
1236 * @tx: amount to translate in the X direction
1237 * @ty: amount to translate in the Y direction
1239 * Modifies the current transformation matrix (CTM) by translating the
1240 * user-space origin by (@tx, @ty). This offset is interpreted as a
1241 * user-space coordinate according to the CTM in place before the new
1242 * call to cairo_translate(). In other words, the translation of the
1243 * user-space origin takes place after any existing transformation.
1245 void
1246 cairo_translate (cairo_t *cr, double tx, double ty)
1248 cairo_status_t status;
1250 if (unlikely (cr->status))
1251 return;
1253 status = _cairo_gstate_translate (cr->gstate, tx, ty);
1254 if (unlikely (status))
1255 _cairo_set_error (cr, status);
1257 slim_hidden_def (cairo_translate);
1260 * cairo_scale:
1261 * @cr: a cairo context
1262 * @sx: scale factor for the X dimension
1263 * @sy: scale factor for the Y dimension
1265 * Modifies the current transformation matrix (CTM) by scaling the X
1266 * and Y user-space axes by @sx and @sy respectively. The scaling of
1267 * the axes takes place after any existing transformation of user
1268 * space.
1270 void
1271 cairo_scale (cairo_t *cr, double sx, double sy)
1273 cairo_status_t status;
1275 if (unlikely (cr->status))
1276 return;
1278 status = _cairo_gstate_scale (cr->gstate, sx, sy);
1279 if (unlikely (status))
1280 _cairo_set_error (cr, status);
1282 slim_hidden_def (cairo_scale);
1285 * cairo_rotate:
1286 * @cr: a cairo context
1287 * @angle: angle (in radians) by which the user-space axes will be
1288 * rotated
1290 * Modifies the current transformation matrix (CTM) by rotating the
1291 * user-space axes by @angle radians. The rotation of the axes takes
1292 * places after any existing transformation of user space. The
1293 * rotation direction for positive angles is from the positive X axis
1294 * toward the positive Y axis.
1296 void
1297 cairo_rotate (cairo_t *cr, double angle)
1299 cairo_status_t status;
1301 if (unlikely (cr->status))
1302 return;
1304 status = _cairo_gstate_rotate (cr->gstate, angle);
1305 if (unlikely (status))
1306 _cairo_set_error (cr, status);
1310 * cairo_transform:
1311 * @cr: a cairo context
1312 * @matrix: a transformation to be applied to the user-space axes
1314 * Modifies the current transformation matrix (CTM) by applying
1315 * @matrix as an additional transformation. The new transformation of
1316 * user space takes place after any existing transformation.
1318 void
1319 cairo_transform (cairo_t *cr,
1320 const cairo_matrix_t *matrix)
1322 cairo_status_t status;
1324 if (unlikely (cr->status))
1325 return;
1327 status = _cairo_gstate_transform (cr->gstate, matrix);
1328 if (unlikely (status))
1329 _cairo_set_error (cr, status);
1331 slim_hidden_def (cairo_transform);
1334 * cairo_set_matrix:
1335 * @cr: a cairo context
1336 * @matrix: a transformation matrix from user space to device space
1338 * Modifies the current transformation matrix (CTM) by setting it
1339 * equal to @matrix.
1341 void
1342 cairo_set_matrix (cairo_t *cr,
1343 const cairo_matrix_t *matrix)
1345 cairo_status_t status;
1347 if (unlikely (cr->status))
1348 return;
1350 status = _cairo_gstate_set_matrix (cr->gstate, matrix);
1351 if (unlikely (status))
1352 _cairo_set_error (cr, status);
1354 slim_hidden_def (cairo_set_matrix);
1357 * cairo_identity_matrix:
1358 * @cr: a cairo context
1360 * Resets the current transformation matrix (CTM) by setting it equal
1361 * to the identity matrix. That is, the user-space and device-space
1362 * axes will be aligned and one user-space unit will transform to one
1363 * device-space unit.
1365 void
1366 cairo_identity_matrix (cairo_t *cr)
1368 if (unlikely (cr->status))
1369 return;
1371 _cairo_gstate_identity_matrix (cr->gstate);
1375 * cairo_user_to_device:
1376 * @cr: a cairo context
1377 * @x: X value of coordinate (in/out parameter)
1378 * @y: Y value of coordinate (in/out parameter)
1380 * Transform a coordinate from user space to device space by
1381 * multiplying the given point by the current transformation matrix
1382 * (CTM).
1384 void
1385 cairo_user_to_device (cairo_t *cr, double *x, double *y)
1387 if (unlikely (cr->status))
1388 return;
1390 _cairo_gstate_user_to_device (cr->gstate, x, y);
1392 slim_hidden_def (cairo_user_to_device);
1395 * cairo_user_to_device_distance:
1396 * @cr: a cairo context
1397 * @dx: X component of a distance vector (in/out parameter)
1398 * @dy: Y component of a distance vector (in/out parameter)
1400 * Transform a distance vector from user space to device space. This
1401 * function is similar to cairo_user_to_device() except that the
1402 * translation components of the CTM will be ignored when transforming
1403 * (@dx,@dy).
1405 void
1406 cairo_user_to_device_distance (cairo_t *cr, double *dx, double *dy)
1408 if (unlikely (cr->status))
1409 return;
1411 _cairo_gstate_user_to_device_distance (cr->gstate, dx, dy);
1413 slim_hidden_def (cairo_user_to_device_distance);
1416 * cairo_device_to_user:
1417 * @cr: a cairo
1418 * @x: X value of coordinate (in/out parameter)
1419 * @y: Y value of coordinate (in/out parameter)
1421 * Transform a coordinate from device space to user space by
1422 * multiplying the given point by the inverse of the current
1423 * transformation matrix (CTM).
1425 void
1426 cairo_device_to_user (cairo_t *cr, double *x, double *y)
1428 if (unlikely (cr->status))
1429 return;
1431 _cairo_gstate_device_to_user (cr->gstate, x, y);
1435 * cairo_device_to_user_distance:
1436 * @cr: a cairo context
1437 * @dx: X component of a distance vector (in/out parameter)
1438 * @dy: Y component of a distance vector (in/out parameter)
1440 * Transform a distance vector from device space to user space. This
1441 * function is similar to cairo_device_to_user() except that the
1442 * translation components of the inverse CTM will be ignored when
1443 * transforming (@dx,@dy).
1445 void
1446 cairo_device_to_user_distance (cairo_t *cr, double *dx, double *dy)
1448 if (unlikely (cr->status))
1449 return;
1451 _cairo_gstate_device_to_user_distance (cr->gstate, dx, dy);
1455 * cairo_new_path:
1456 * @cr: a cairo context
1458 * Clears the current path. After this call there will be no path and
1459 * no current point.
1461 void
1462 cairo_new_path (cairo_t *cr)
1464 if (unlikely (cr->status))
1465 return;
1467 _cairo_path_fixed_fini (cr->path);
1468 _cairo_path_fixed_init (cr->path);
1470 slim_hidden_def(cairo_new_path);
1473 * cairo_move_to:
1474 * @cr: a cairo context
1475 * @x: the X coordinate of the new position
1476 * @y: the Y coordinate of the new position
1478 * Begin a new sub-path. After this call the current point will be (@x,
1479 * @y).
1481 void
1482 cairo_move_to (cairo_t *cr, double x, double y)
1484 cairo_status_t status;
1485 cairo_fixed_t x_fixed, y_fixed;
1487 if (unlikely (cr->status))
1488 return;
1490 _cairo_gstate_user_to_backend (cr->gstate, &x, &y);
1491 x_fixed = _cairo_fixed_from_double (x);
1492 y_fixed = _cairo_fixed_from_double (y);
1494 status = _cairo_path_fixed_move_to (cr->path, x_fixed, y_fixed);
1495 if (unlikely (status))
1496 _cairo_set_error (cr, status);
1498 slim_hidden_def(cairo_move_to);
1501 * cairo_new_sub_path:
1502 * @cr: a cairo context
1504 * Begin a new sub-path. Note that the existing path is not
1505 * affected. After this call there will be no current point.
1507 * In many cases, this call is not needed since new sub-paths are
1508 * frequently started with cairo_move_to().
1510 * A call to cairo_new_sub_path() is particularly useful when
1511 * beginning a new sub-path with one of the cairo_arc() calls. This
1512 * makes things easier as it is no longer necessary to manually
1513 * compute the arc's initial coordinates for a call to
1514 * cairo_move_to().
1516 * Since: 1.2
1518 void
1519 cairo_new_sub_path (cairo_t *cr)
1521 if (unlikely (cr->status))
1522 return;
1524 _cairo_path_fixed_new_sub_path (cr->path);
1528 * cairo_line_to:
1529 * @cr: a cairo context
1530 * @x: the X coordinate of the end of the new line
1531 * @y: the Y coordinate of the end of the new line
1533 * Adds a line to the path from the current point to position (@x, @y)
1534 * in user-space coordinates. After this call the current point
1535 * will be (@x, @y).
1537 * If there is no current point before the call to cairo_line_to()
1538 * this function will behave as cairo_move_to(@cr, @x, @y).
1540 void
1541 cairo_line_to (cairo_t *cr, double x, double y)
1543 cairo_status_t status;
1544 cairo_fixed_t x_fixed, y_fixed;
1546 if (unlikely (cr->status))
1547 return;
1549 _cairo_gstate_user_to_backend (cr->gstate, &x, &y);
1550 x_fixed = _cairo_fixed_from_double (x);
1551 y_fixed = _cairo_fixed_from_double (y);
1553 status = _cairo_path_fixed_line_to (cr->path, x_fixed, y_fixed);
1554 if (unlikely (status))
1555 _cairo_set_error (cr, status);
1557 slim_hidden_def (cairo_line_to);
1560 * cairo_curve_to:
1561 * @cr: a cairo context
1562 * @x1: the X coordinate of the first control point
1563 * @y1: the Y coordinate of the first control point
1564 * @x2: the X coordinate of the second control point
1565 * @y2: the Y coordinate of the second control point
1566 * @x3: the X coordinate of the end of the curve
1567 * @y3: the Y coordinate of the end of the curve
1569 * Adds a cubic Bézier spline to the path from the current point to
1570 * position (@x3, @y3) in user-space coordinates, using (@x1, @y1) and
1571 * (@x2, @y2) as the control points. After this call the current point
1572 * will be (@x3, @y3).
1574 * If there is no current point before the call to cairo_curve_to()
1575 * this function will behave as if preceded by a call to
1576 * cairo_move_to(@cr, @x1, @y1).
1578 void
1579 cairo_curve_to (cairo_t *cr,
1580 double x1, double y1,
1581 double x2, double y2,
1582 double x3, double y3)
1584 cairo_status_t status;
1585 cairo_fixed_t x1_fixed, y1_fixed;
1586 cairo_fixed_t x2_fixed, y2_fixed;
1587 cairo_fixed_t x3_fixed, y3_fixed;
1589 if (unlikely (cr->status))
1590 return;
1592 _cairo_gstate_user_to_backend (cr->gstate, &x1, &y1);
1593 _cairo_gstate_user_to_backend (cr->gstate, &x2, &y2);
1594 _cairo_gstate_user_to_backend (cr->gstate, &x3, &y3);
1596 x1_fixed = _cairo_fixed_from_double (x1);
1597 y1_fixed = _cairo_fixed_from_double (y1);
1599 x2_fixed = _cairo_fixed_from_double (x2);
1600 y2_fixed = _cairo_fixed_from_double (y2);
1602 x3_fixed = _cairo_fixed_from_double (x3);
1603 y3_fixed = _cairo_fixed_from_double (y3);
1605 status = _cairo_path_fixed_curve_to (cr->path,
1606 x1_fixed, y1_fixed,
1607 x2_fixed, y2_fixed,
1608 x3_fixed, y3_fixed);
1609 if (unlikely (status))
1610 _cairo_set_error (cr, status);
1612 slim_hidden_def (cairo_curve_to);
1615 * cairo_arc:
1616 * @cr: a cairo context
1617 * @xc: X position of the center of the arc
1618 * @yc: Y position of the center of the arc
1619 * @radius: the radius of the arc
1620 * @angle1: the start angle, in radians
1621 * @angle2: the end angle, in radians
1623 * Adds a circular arc of the given @radius to the current path. The
1624 * arc is centered at (@xc, @yc), begins at @angle1 and proceeds in
1625 * the direction of increasing angles to end at @angle2. If @angle2 is
1626 * less than @angle1 it will be progressively increased by 2*M_PI
1627 * until it is greater than @angle1.
1629 * If there is a current point, an initial line segment will be added
1630 * to the path to connect the current point to the beginning of the
1631 * arc. If this initial line is undesired, it can be avoided by
1632 * calling cairo_new_sub_path() before calling cairo_arc().
1634 * Angles are measured in radians. An angle of 0.0 is in the direction
1635 * of the positive X axis (in user space). An angle of %M_PI/2.0 radians
1636 * (90 degrees) is in the direction of the positive Y axis (in
1637 * user space). Angles increase in the direction from the positive X
1638 * axis toward the positive Y axis. So with the default transformation
1639 * matrix, angles increase in a clockwise direction.
1641 * (To convert from degrees to radians, use <literal>degrees * (M_PI /
1642 * 180.)</literal>.)
1644 * This function gives the arc in the direction of increasing angles;
1645 * see cairo_arc_negative() to get the arc in the direction of
1646 * decreasing angles.
1648 * The arc is circular in user space. To achieve an elliptical arc,
1649 * you can scale the current transformation matrix by different
1650 * amounts in the X and Y directions. For example, to draw an ellipse
1651 * in the box given by @x, @y, @width, @height:
1653 * <informalexample><programlisting>
1654 * cairo_save (cr);
1655 * cairo_translate (cr, x + width / 2., y + height / 2.);
1656 * cairo_scale (cr, width / 2., height / 2.);
1657 * cairo_arc (cr, 0., 0., 1., 0., 2 * M_PI);
1658 * cairo_restore (cr);
1659 * </programlisting></informalexample>
1661 void
1662 cairo_arc (cairo_t *cr,
1663 double xc, double yc,
1664 double radius,
1665 double angle1, double angle2)
1667 if (unlikely (cr->status))
1668 return;
1670 /* Do nothing, successfully, if radius is <= 0 */
1671 if (radius <= 0.0)
1672 return;
1674 while (angle2 < angle1)
1675 angle2 += 2 * M_PI;
1677 cairo_line_to (cr,
1678 xc + radius * cos (angle1),
1679 yc + radius * sin (angle1));
1681 _cairo_arc_path (cr, xc, yc, radius,
1682 angle1, angle2);
1686 * cairo_arc_negative:
1687 * @cr: a cairo context
1688 * @xc: X position of the center of the arc
1689 * @yc: Y position of the center of the arc
1690 * @radius: the radius of the arc
1691 * @angle1: the start angle, in radians
1692 * @angle2: the end angle, in radians
1694 * Adds a circular arc of the given @radius to the current path. The
1695 * arc is centered at (@xc, @yc), begins at @angle1 and proceeds in
1696 * the direction of decreasing angles to end at @angle2. If @angle2 is
1697 * greater than @angle1 it will be progressively decreased by 2*M_PI
1698 * until it is less than @angle1.
1700 * See cairo_arc() for more details. This function differs only in the
1701 * direction of the arc between the two angles.
1703 void
1704 cairo_arc_negative (cairo_t *cr,
1705 double xc, double yc,
1706 double radius,
1707 double angle1, double angle2)
1709 if (unlikely (cr->status))
1710 return;
1712 /* Do nothing, successfully, if radius is <= 0 */
1713 if (radius <= 0.0)
1714 return;
1716 while (angle2 > angle1)
1717 angle2 -= 2 * M_PI;
1719 cairo_line_to (cr,
1720 xc + radius * cos (angle1),
1721 yc + radius * sin (angle1));
1723 _cairo_arc_path_negative (cr, xc, yc, radius,
1724 angle1, angle2);
1727 /* XXX: NYI
1728 void
1729 cairo_arc_to (cairo_t *cr,
1730 double x1, double y1,
1731 double x2, double y2,
1732 double radius)
1734 cairo_status_t status;
1736 if (unlikely (cr->status))
1737 return;
1739 status = _cairo_gstate_arc_to (cr->gstate,
1740 x1, y1,
1741 x2, y2,
1742 radius);
1743 if (unlikely (status))
1744 _cairo_set_error (cr, status);
1749 * cairo_rel_move_to:
1750 * @cr: a cairo context
1751 * @dx: the X offset
1752 * @dy: the Y offset
1754 * Begin a new sub-path. After this call the current point will offset
1755 * by (@x, @y).
1757 * Given a current point of (x, y), cairo_rel_move_to(@cr, @dx, @dy)
1758 * is logically equivalent to cairo_move_to(@cr, x + @dx, y + @dy).
1760 * It is an error to call this function with no current point. Doing
1761 * so will cause @cr to shutdown with a status of
1762 * %CAIRO_STATUS_NO_CURRENT_POINT.
1764 void
1765 cairo_rel_move_to (cairo_t *cr, double dx, double dy)
1767 cairo_fixed_t dx_fixed, dy_fixed;
1768 cairo_status_t status;
1770 if (unlikely (cr->status))
1771 return;
1773 _cairo_gstate_user_to_device_distance (cr->gstate, &dx, &dy);
1775 dx_fixed = _cairo_fixed_from_double (dx);
1776 dy_fixed = _cairo_fixed_from_double (dy);
1778 status = _cairo_path_fixed_rel_move_to (cr->path, dx_fixed, dy_fixed);
1779 if (unlikely (status))
1780 _cairo_set_error (cr, status);
1784 * cairo_rel_line_to:
1785 * @cr: a cairo context
1786 * @dx: the X offset to the end of the new line
1787 * @dy: the Y offset to the end of the new line
1789 * Relative-coordinate version of cairo_line_to(). Adds a line to the
1790 * path from the current point to a point that is offset from the
1791 * current point by (@dx, @dy) in user space. After this call the
1792 * current point will be offset by (@dx, @dy).
1794 * Given a current point of (x, y), cairo_rel_line_to(@cr, @dx, @dy)
1795 * is logically equivalent to cairo_line_to(@cr, x + @dx, y + @dy).
1797 * It is an error to call this function with no current point. Doing
1798 * so will cause @cr to shutdown with a status of
1799 * %CAIRO_STATUS_NO_CURRENT_POINT.
1801 void
1802 cairo_rel_line_to (cairo_t *cr, double dx, double dy)
1804 cairo_fixed_t dx_fixed, dy_fixed;
1805 cairo_status_t status;
1807 if (unlikely (cr->status))
1808 return;
1810 _cairo_gstate_user_to_device_distance (cr->gstate, &dx, &dy);
1812 dx_fixed = _cairo_fixed_from_double (dx);
1813 dy_fixed = _cairo_fixed_from_double (dy);
1815 status = _cairo_path_fixed_rel_line_to (cr->path, dx_fixed, dy_fixed);
1816 if (unlikely (status))
1817 _cairo_set_error (cr, status);
1819 slim_hidden_def(cairo_rel_line_to);
1822 * cairo_rel_curve_to:
1823 * @cr: a cairo context
1824 * @dx1: the X offset to the first control point
1825 * @dy1: the Y offset to the first control point
1826 * @dx2: the X offset to the second control point
1827 * @dy2: the Y offset to the second control point
1828 * @dx3: the X offset to the end of the curve
1829 * @dy3: the Y offset to the end of the curve
1831 * Relative-coordinate version of cairo_curve_to(). All offsets are
1832 * relative to the current point. Adds a cubic Bézier spline to the
1833 * path from the current point to a point offset from the current
1834 * point by (@dx3, @dy3), using points offset by (@dx1, @dy1) and
1835 * (@dx2, @dy2) as the control points. After this call the current
1836 * point will be offset by (@dx3, @dy3).
1838 * Given a current point of (x, y), cairo_rel_curve_to(@cr, @dx1,
1839 * @dy1, @dx2, @dy2, @dx3, @dy3) is logically equivalent to
1840 * cairo_curve_to(@cr, x+@dx1, y+@dy1, x+@dx2, y+@dy2, x+@dx3, y+@dy3).
1842 * It is an error to call this function with no current point. Doing
1843 * so will cause @cr to shutdown with a status of
1844 * %CAIRO_STATUS_NO_CURRENT_POINT.
1846 void
1847 cairo_rel_curve_to (cairo_t *cr,
1848 double dx1, double dy1,
1849 double dx2, double dy2,
1850 double dx3, double dy3)
1852 cairo_fixed_t dx1_fixed, dy1_fixed;
1853 cairo_fixed_t dx2_fixed, dy2_fixed;
1854 cairo_fixed_t dx3_fixed, dy3_fixed;
1855 cairo_status_t status;
1857 if (unlikely (cr->status))
1858 return;
1860 _cairo_gstate_user_to_device_distance (cr->gstate, &dx1, &dy1);
1861 _cairo_gstate_user_to_device_distance (cr->gstate, &dx2, &dy2);
1862 _cairo_gstate_user_to_device_distance (cr->gstate, &dx3, &dy3);
1864 dx1_fixed = _cairo_fixed_from_double (dx1);
1865 dy1_fixed = _cairo_fixed_from_double (dy1);
1867 dx2_fixed = _cairo_fixed_from_double (dx2);
1868 dy2_fixed = _cairo_fixed_from_double (dy2);
1870 dx3_fixed = _cairo_fixed_from_double (dx3);
1871 dy3_fixed = _cairo_fixed_from_double (dy3);
1873 status = _cairo_path_fixed_rel_curve_to (cr->path,
1874 dx1_fixed, dy1_fixed,
1875 dx2_fixed, dy2_fixed,
1876 dx3_fixed, dy3_fixed);
1877 if (unlikely (status))
1878 _cairo_set_error (cr, status);
1882 * cairo_rectangle:
1883 * @cr: a cairo context
1884 * @x: the X coordinate of the top left corner of the rectangle
1885 * @y: the Y coordinate to the top left corner of the rectangle
1886 * @width: the width of the rectangle
1887 * @height: the height of the rectangle
1889 * Adds a closed sub-path rectangle of the given size to the current
1890 * path at position (@x, @y) in user-space coordinates.
1892 * This function is logically equivalent to:
1893 * <informalexample><programlisting>
1894 * cairo_move_to (cr, x, y);
1895 * cairo_rel_line_to (cr, width, 0);
1896 * cairo_rel_line_to (cr, 0, height);
1897 * cairo_rel_line_to (cr, -width, 0);
1898 * cairo_close_path (cr);
1899 * </programlisting></informalexample>
1901 void
1902 cairo_rectangle (cairo_t *cr,
1903 double x, double y,
1904 double width, double height)
1906 if (unlikely (cr->status))
1907 return;
1909 cairo_move_to (cr, x, y);
1910 cairo_rel_line_to (cr, width, 0);
1911 cairo_rel_line_to (cr, 0, height);
1912 cairo_rel_line_to (cr, -width, 0);
1913 cairo_close_path (cr);
1916 #if 0
1917 /* XXX: NYI */
1918 void
1919 cairo_stroke_to_path (cairo_t *cr)
1921 cairo_status_t status;
1923 if (unlikely (cr->status))
1924 return;
1926 /* The code in _cairo_meta_surface_get_path has a poorman's stroke_to_path */
1928 status = _cairo_gstate_stroke_path (cr->gstate);
1929 if (unlikely (status))
1930 _cairo_set_error (cr, status);
1932 #endif
1935 * cairo_close_path:
1936 * @cr: a cairo context
1938 * Adds a line segment to the path from the current point to the
1939 * beginning of the current sub-path, (the most recent point passed to
1940 * cairo_move_to()), and closes this sub-path. After this call the
1941 * current point will be at the joined endpoint of the sub-path.
1943 * The behavior of cairo_close_path() is distinct from simply calling
1944 * cairo_line_to() with the equivalent coordinate in the case of
1945 * stroking. When a closed sub-path is stroked, there are no caps on
1946 * the ends of the sub-path. Instead, there is a line join connecting
1947 * the final and initial segments of the sub-path.
1949 * If there is no current point before the call to cairo_close_path(),
1950 * this function will have no effect.
1952 * Note: As of cairo version 1.2.4 any call to cairo_close_path() will
1953 * place an explicit MOVE_TO element into the path immediately after
1954 * the CLOSE_PATH element, (which can be seen in cairo_copy_path() for
1955 * example). This can simplify path processing in some cases as it may
1956 * not be necessary to save the "last move_to point" during processing
1957 * as the MOVE_TO immediately after the CLOSE_PATH will provide that
1958 * point.
1960 void
1961 cairo_close_path (cairo_t *cr)
1963 cairo_status_t status;
1965 if (unlikely (cr->status))
1966 return;
1968 status = _cairo_path_fixed_close_path (cr->path);
1969 if (unlikely (status))
1970 _cairo_set_error (cr, status);
1972 slim_hidden_def(cairo_close_path);
1975 * cairo_path_extents:
1976 * @cr: a cairo context
1977 * @x1: left of the resulting extents
1978 * @y1: top of the resulting extents
1979 * @x2: right of the resulting extents
1980 * @y2: bottom of the resulting extents
1982 * Computes a bounding box in user-space coordinates covering the
1983 * points on the current path. If the current path is empty, returns
1984 * an empty rectangle ((0,0), (0,0)). Stroke parameters, fill rule,
1985 * surface dimensions and clipping are not taken into account.
1987 * Contrast with cairo_fill_extents() and cairo_stroke_extents() which
1988 * return the extents of only the area that would be "inked" by
1989 * the corresponding drawing operations.
1991 * The result of cairo_path_extents() is defined as equivalent to the
1992 * limit of cairo_stroke_extents() with %CAIRO_LINE_CAP_ROUND as the
1993 * line width approaches 0.0, (but never reaching the empty-rectangle
1994 * returned by cairo_stroke_extents() for a line width of 0.0).
1996 * Specifically, this means that zero-area sub-paths such as
1997 * cairo_move_to();cairo_line_to() segments, (even degenerate cases
1998 * where the coordinates to both calls are identical), will be
1999 * considered as contributing to the extents. However, a lone
2000 * cairo_move_to() will not contribute to the results of
2001 * cairo_path_extents().
2003 * Since: 1.6
2005 void
2006 cairo_path_extents (cairo_t *cr,
2007 double *x1, double *y1, double *x2, double *y2)
2009 if (unlikely (cr->status)) {
2010 if (x1)
2011 *x1 = 0.0;
2012 if (y1)
2013 *y1 = 0.0;
2014 if (x2)
2015 *x2 = 0.0;
2016 if (y2)
2017 *y2 = 0.0;
2019 return;
2022 _cairo_gstate_path_extents (cr->gstate,
2023 cr->path,
2024 x1, y1, x2, y2);
2028 * cairo_paint:
2029 * @cr: a cairo context
2031 * A drawing operator that paints the current source everywhere within
2032 * the current clip region.
2034 void
2035 cairo_paint (cairo_t *cr)
2037 cairo_status_t status;
2039 if (unlikely (cr->status))
2040 return;
2042 status = _cairo_gstate_paint (cr->gstate);
2043 if (unlikely (status))
2044 _cairo_set_error (cr, status);
2046 slim_hidden_def (cairo_paint);
2049 * cairo_paint_with_alpha:
2050 * @cr: a cairo context
2051 * @alpha: alpha value, between 0 (transparent) and 1 (opaque)
2053 * A drawing operator that paints the current source everywhere within
2054 * the current clip region using a mask of constant alpha value
2055 * @alpha. The effect is similar to cairo_paint(), but the drawing
2056 * is faded out using the alpha value.
2058 void
2059 cairo_paint_with_alpha (cairo_t *cr,
2060 double alpha)
2062 cairo_status_t status;
2063 cairo_color_t color;
2064 cairo_solid_pattern_t pattern;
2066 if (unlikely (cr->status))
2067 return;
2069 if (CAIRO_ALPHA_IS_OPAQUE (alpha)) {
2070 cairo_paint (cr);
2071 return;
2074 if (CAIRO_ALPHA_IS_ZERO (alpha)) {
2075 return;
2078 _cairo_color_init_rgba (&color, 1., 1., 1., alpha);
2079 _cairo_pattern_init_solid (&pattern, &color, CAIRO_CONTENT_ALPHA);
2081 status = _cairo_gstate_mask (cr->gstate, &pattern.base);
2082 if (unlikely (status))
2083 _cairo_set_error (cr, status);
2085 _cairo_pattern_fini (&pattern.base);
2089 * cairo_mask:
2090 * @cr: a cairo context
2091 * @pattern: a #cairo_pattern_t
2093 * A drawing operator that paints the current source
2094 * using the alpha channel of @pattern as a mask. (Opaque
2095 * areas of @pattern are painted with the source, transparent
2096 * areas are not painted.)
2098 void
2099 cairo_mask (cairo_t *cr,
2100 cairo_pattern_t *pattern)
2102 cairo_status_t status;
2104 if (unlikely (cr->status))
2105 return;
2107 if (pattern == NULL) {
2108 _cairo_set_error (cr, CAIRO_STATUS_NULL_POINTER);
2109 return;
2112 if (pattern->status) {
2113 _cairo_set_error (cr, pattern->status);
2114 return;
2117 status = _cairo_gstate_mask (cr->gstate, pattern);
2118 if (unlikely (status))
2119 _cairo_set_error (cr, status);
2121 slim_hidden_def (cairo_mask);
2124 * cairo_mask_surface:
2125 * @cr: a cairo context
2126 * @surface: a #cairo_surface_t
2127 * @surface_x: X coordinate at which to place the origin of @surface
2128 * @surface_y: Y coordinate at which to place the origin of @surface
2130 * A drawing operator that paints the current source
2131 * using the alpha channel of @surface as a mask. (Opaque
2132 * areas of @surface are painted with the source, transparent
2133 * areas are not painted.)
2135 void
2136 cairo_mask_surface (cairo_t *cr,
2137 cairo_surface_t *surface,
2138 double surface_x,
2139 double surface_y)
2141 cairo_pattern_t *pattern;
2142 cairo_matrix_t matrix;
2144 if (unlikely (cr->status))
2145 return;
2147 pattern = cairo_pattern_create_for_surface (surface);
2149 cairo_matrix_init_translate (&matrix, - surface_x, - surface_y);
2150 cairo_pattern_set_matrix (pattern, &matrix);
2152 cairo_mask (cr, pattern);
2154 cairo_pattern_destroy (pattern);
2158 * cairo_stroke:
2159 * @cr: a cairo context
2161 * A drawing operator that strokes the current path according to the
2162 * current line width, line join, line cap, and dash settings. After
2163 * cairo_stroke(), the current path will be cleared from the cairo
2164 * context. See cairo_set_line_width(), cairo_set_line_join(),
2165 * cairo_set_line_cap(), cairo_set_dash(), and
2166 * cairo_stroke_preserve().
2168 * Note: Degenerate segments and sub-paths are treated specially and
2169 * provide a useful result. These can result in two different
2170 * situations:
2172 * 1. Zero-length "on" segments set in cairo_set_dash(). If the cap
2173 * style is %CAIRO_LINE_CAP_ROUND or %CAIRO_LINE_CAP_SQUARE then these
2174 * segments will be drawn as circular dots or squares respectively. In
2175 * the case of %CAIRO_LINE_CAP_SQUARE, the orientation of the squares
2176 * is determined by the direction of the underlying path.
2178 * 2. A sub-path created by cairo_move_to() followed by either a
2179 * cairo_close_path() or one or more calls to cairo_line_to() to the
2180 * same coordinate as the cairo_move_to(). If the cap style is
2181 * %CAIRO_LINE_CAP_ROUND then these sub-paths will be drawn as circular
2182 * dots. Note that in the case of %CAIRO_LINE_CAP_SQUARE a degenerate
2183 * sub-path will not be drawn at all, (since the correct orientation
2184 * is indeterminate).
2186 * In no case will a cap style of %CAIRO_LINE_CAP_BUTT cause anything
2187 * to be drawn in the case of either degenerate segments or sub-paths.
2189 void
2190 cairo_stroke (cairo_t *cr)
2192 cairo_stroke_preserve (cr);
2194 cairo_new_path (cr);
2196 slim_hidden_def(cairo_stroke);
2199 * cairo_stroke_preserve:
2200 * @cr: a cairo context
2202 * A drawing operator that strokes the current path according to the
2203 * current line width, line join, line cap, and dash settings. Unlike
2204 * cairo_stroke(), cairo_stroke_preserve() preserves the path within the
2205 * cairo context.
2207 * See cairo_set_line_width(), cairo_set_line_join(),
2208 * cairo_set_line_cap(), cairo_set_dash(), and
2209 * cairo_stroke_preserve().
2211 void
2212 cairo_stroke_preserve (cairo_t *cr)
2214 cairo_status_t status;
2216 if (unlikely (cr->status))
2217 return;
2219 status = _cairo_gstate_stroke (cr->gstate, cr->path);
2220 if (unlikely (status))
2221 _cairo_set_error (cr, status);
2223 slim_hidden_def(cairo_stroke_preserve);
2226 * cairo_fill:
2227 * @cr: a cairo context
2229 * A drawing operator that fills the current path according to the
2230 * current fill rule, (each sub-path is implicitly closed before being
2231 * filled). After cairo_fill(), the current path will be cleared from
2232 * the cairo context. See cairo_set_fill_rule() and
2233 * cairo_fill_preserve().
2235 void
2236 cairo_fill (cairo_t *cr)
2238 cairo_fill_preserve (cr);
2240 cairo_new_path (cr);
2244 * cairo_fill_preserve:
2245 * @cr: a cairo context
2247 * A drawing operator that fills the current path according to the
2248 * current fill rule, (each sub-path is implicitly closed before being
2249 * filled). Unlike cairo_fill(), cairo_fill_preserve() preserves the
2250 * path within the cairo context.
2252 * See cairo_set_fill_rule() and cairo_fill().
2254 void
2255 cairo_fill_preserve (cairo_t *cr)
2257 cairo_status_t status;
2259 if (unlikely (cr->status))
2260 return;
2262 status = _cairo_gstate_fill (cr->gstate, cr->path);
2263 if (unlikely (status))
2264 _cairo_set_error (cr, status);
2266 slim_hidden_def(cairo_fill_preserve);
2269 * cairo_copy_page:
2270 * @cr: a cairo context
2272 * Emits the current page for backends that support multiple pages, but
2273 * doesn't clear it, so, the contents of the current page will be retained
2274 * for the next page too. Use cairo_show_page() if you want to get an
2275 * empty page after the emission.
2277 * This is a convenience function that simply calls
2278 * cairo_surface_copy_page() on @cr's target.
2280 void
2281 cairo_copy_page (cairo_t *cr)
2283 cairo_status_t status;
2285 if (unlikely (cr->status))
2286 return;
2288 status = _cairo_gstate_copy_page (cr->gstate);
2289 if (unlikely (status))
2290 _cairo_set_error (cr, status);
2294 * cairo_show_page:
2295 * @cr: a cairo context
2297 * Emits and clears the current page for backends that support multiple
2298 * pages. Use cairo_copy_page() if you don't want to clear the page.
2300 * This is a convenience function that simply calls
2301 * cairo_surface_show_page() on @cr's target.
2303 void
2304 cairo_show_page (cairo_t *cr)
2306 cairo_status_t status;
2308 if (unlikely (cr->status))
2309 return;
2311 status = _cairo_gstate_show_page (cr->gstate);
2312 if (unlikely (status))
2313 _cairo_set_error (cr, status);
2317 * cairo_swap_page:
2318 * @cr: a cairo context
2320 * Emits and makes undefined the current page for backends that support multiple
2321 * pages. Use cairo_show_page() if you want to clear the page.
2323 * This is a convenience function that simply calls
2324 * cairo_surface_swap_page() on @cr's target.
2326 void
2327 cairo_swap_page (cairo_t *cr)
2329 cairo_status_t status;
2331 if (cr->status)
2332 return;
2334 status = _cairo_gstate_swap_page (cr->gstate);
2335 if (unlikely (status))
2336 _cairo_set_error (cr, status);
2340 * cairo_in_stroke:
2341 * @cr: a cairo context
2342 * @x: X coordinate of the point to test
2343 * @y: Y coordinate of the point to test
2345 * Tests whether the given point is inside the area that would be
2346 * affected by a cairo_stroke() operation given the current path and
2347 * stroking parameters. Surface dimensions and clipping are not taken
2348 * into account.
2350 * See cairo_stroke(), cairo_set_line_width(), cairo_set_line_join(),
2351 * cairo_set_line_cap(), cairo_set_dash(), and
2352 * cairo_stroke_preserve().
2354 * Return value: A non-zero value if the point is inside, or zero if
2355 * outside.
2357 cairo_bool_t
2358 cairo_in_stroke (cairo_t *cr, double x, double y)
2360 cairo_status_t status;
2361 cairo_bool_t inside = FALSE;
2363 if (unlikely (cr->status))
2364 return 0;
2366 status = _cairo_gstate_in_stroke (cr->gstate,
2367 cr->path,
2368 x, y, &inside);
2369 if (unlikely (status))
2370 _cairo_set_error (cr, status);
2372 return inside;
2376 * cairo_in_fill:
2377 * @cr: a cairo context
2378 * @x: X coordinate of the point to test
2379 * @y: Y coordinate of the point to test
2381 * Tests whether the given point is inside the area that would be
2382 * affected by a cairo_fill() operation given the current path and
2383 * filling parameters. Surface dimensions and clipping are not taken
2384 * into account.
2386 * See cairo_fill(), cairo_set_fill_rule() and cairo_fill_preserve().
2388 * Return value: A non-zero value if the point is inside, or zero if
2389 * outside.
2391 cairo_bool_t
2392 cairo_in_fill (cairo_t *cr, double x, double y)
2394 cairo_bool_t inside;
2396 if (unlikely (cr->status))
2397 return 0;
2399 _cairo_gstate_in_fill (cr->gstate,
2400 cr->path,
2401 x, y, &inside);
2403 return inside;
2407 * cairo_stroke_extents:
2408 * @cr: a cairo context
2409 * @x1: left of the resulting extents
2410 * @y1: top of the resulting extents
2411 * @x2: right of the resulting extents
2412 * @y2: bottom of the resulting extents
2414 * Computes a bounding box in user coordinates covering the area that
2415 * would be affected, (the "inked" area), by a cairo_stroke()
2416 * operation given the current path and stroke parameters.
2417 * If the current path is empty, returns an empty rectangle ((0,0), (0,0)).
2418 * Surface dimensions and clipping are not taken into account.
2420 * Note that if the line width is set to exactly zero, then
2421 * cairo_stroke_extents() will return an empty rectangle. Contrast with
2422 * cairo_path_extents() which can be used to compute the non-empty
2423 * bounds as the line width approaches zero.
2425 * Note that cairo_stroke_extents() must necessarily do more work to
2426 * compute the precise inked areas in light of the stroke parameters,
2427 * so cairo_path_extents() may be more desirable for sake of
2428 * performance if non-inked path extents are desired.
2430 * See cairo_stroke(), cairo_set_line_width(), cairo_set_line_join(),
2431 * cairo_set_line_cap(), cairo_set_dash(), and
2432 * cairo_stroke_preserve().
2434 void
2435 cairo_stroke_extents (cairo_t *cr,
2436 double *x1, double *y1, double *x2, double *y2)
2438 cairo_status_t status;
2440 if (unlikely (cr->status)) {
2441 if (x1)
2442 *x1 = 0.0;
2443 if (y1)
2444 *y1 = 0.0;
2445 if (x2)
2446 *x2 = 0.0;
2447 if (y2)
2448 *y2 = 0.0;
2450 return;
2453 status = _cairo_gstate_stroke_extents (cr->gstate,
2454 cr->path,
2455 x1, y1, x2, y2);
2456 if (unlikely (status))
2457 _cairo_set_error (cr, status);
2461 * cairo_fill_extents:
2462 * @cr: a cairo context
2463 * @x1: left of the resulting extents
2464 * @y1: top of the resulting extents
2465 * @x2: right of the resulting extents
2466 * @y2: bottom of the resulting extents
2468 * Computes a bounding box in user coordinates covering the area that
2469 * would be affected, (the "inked" area), by a cairo_fill() operation
2470 * given the current path and fill parameters. If the current path is
2471 * empty, returns an empty rectangle ((0,0), (0,0)). Surface
2472 * dimensions and clipping are not taken into account.
2474 * Contrast with cairo_path_extents(), which is similar, but returns
2475 * non-zero extents for some paths with no inked area, (such as a
2476 * simple line segment).
2478 * Note that cairo_fill_extents() must necessarily do more work to
2479 * compute the precise inked areas in light of the fill rule, so
2480 * cairo_path_extents() may be more desirable for sake of performance
2481 * if the non-inked path extents are desired.
2483 * See cairo_fill(), cairo_set_fill_rule() and cairo_fill_preserve().
2485 void
2486 cairo_fill_extents (cairo_t *cr,
2487 double *x1, double *y1, double *x2, double *y2)
2489 cairo_status_t status;
2491 if (unlikely (cr->status)) {
2492 if (x1)
2493 *x1 = 0.0;
2494 if (y1)
2495 *y1 = 0.0;
2496 if (x2)
2497 *x2 = 0.0;
2498 if (y2)
2499 *y2 = 0.0;
2501 return;
2504 status = _cairo_gstate_fill_extents (cr->gstate,
2505 cr->path,
2506 x1, y1, x2, y2);
2507 if (unlikely (status))
2508 _cairo_set_error (cr, status);
2512 * cairo_clip:
2513 * @cr: a cairo context
2515 * Establishes a new clip region by intersecting the current clip
2516 * region with the current path as it would be filled by cairo_fill()
2517 * and according to the current fill rule (see cairo_set_fill_rule()).
2519 * After cairo_clip(), the current path will be cleared from the cairo
2520 * context.
2522 * The current clip region affects all drawing operations by
2523 * effectively masking out any changes to the surface that are outside
2524 * the current clip region.
2526 * Calling cairo_clip() can only make the clip region smaller, never
2527 * larger. But the current clip is part of the graphics state, so a
2528 * temporary restriction of the clip region can be achieved by
2529 * calling cairo_clip() within a cairo_save()/cairo_restore()
2530 * pair. The only other means of increasing the size of the clip
2531 * region is cairo_reset_clip().
2533 void
2534 cairo_clip (cairo_t *cr)
2536 cairo_clip_preserve (cr);
2538 cairo_new_path (cr);
2542 * cairo_clip_preserve:
2543 * @cr: a cairo context
2545 * Establishes a new clip region by intersecting the current clip
2546 * region with the current path as it would be filled by cairo_fill()
2547 * and according to the current fill rule (see cairo_set_fill_rule()).
2549 * Unlike cairo_clip(), cairo_clip_preserve() preserves the path within
2550 * the cairo context.
2552 * The current clip region affects all drawing operations by
2553 * effectively masking out any changes to the surface that are outside
2554 * the current clip region.
2556 * Calling cairo_clip_preserve() can only make the clip region smaller, never
2557 * larger. But the current clip is part of the graphics state, so a
2558 * temporary restriction of the clip region can be achieved by
2559 * calling cairo_clip_preserve() within a cairo_save()/cairo_restore()
2560 * pair. The only other means of increasing the size of the clip
2561 * region is cairo_reset_clip().
2563 void
2564 cairo_clip_preserve (cairo_t *cr)
2566 cairo_status_t status;
2568 if (unlikely (cr->status))
2569 return;
2571 status = _cairo_gstate_clip (cr->gstate, cr->path);
2572 if (unlikely (status))
2573 _cairo_set_error (cr, status);
2575 slim_hidden_def(cairo_clip_preserve);
2578 * cairo_reset_clip:
2579 * @cr: a cairo context
2581 * Reset the current clip region to its original, unrestricted
2582 * state. That is, set the clip region to an infinitely large shape
2583 * containing the target surface. Equivalently, if infinity is too
2584 * hard to grasp, one can imagine the clip region being reset to the
2585 * exact bounds of the target surface.
2587 * Note that code meant to be reusable should not call
2588 * cairo_reset_clip() as it will cause results unexpected by
2589 * higher-level code which calls cairo_clip(). Consider using
2590 * cairo_save() and cairo_restore() around cairo_clip() as a more
2591 * robust means of temporarily restricting the clip region.
2593 void
2594 cairo_reset_clip (cairo_t *cr)
2596 cairo_status_t status;
2598 if (unlikely (cr->status))
2599 return;
2601 status = _cairo_gstate_reset_clip (cr->gstate);
2602 if (unlikely (status))
2603 _cairo_set_error (cr, status);
2607 * cairo_clip_extents:
2608 * @cr: a cairo context
2609 * @x1: left of the resulting extents
2610 * @y1: top of the resulting extents
2611 * @x2: right of the resulting extents
2612 * @y2: bottom of the resulting extents
2614 * Computes a bounding box in user coordinates covering the area inside the
2615 * current clip.
2617 * Since: 1.4
2619 void
2620 cairo_clip_extents (cairo_t *cr,
2621 double *x1, double *y1,
2622 double *x2, double *y2)
2624 cairo_status_t status;
2626 if (unlikely (cr->status)) {
2627 if (x1)
2628 *x1 = 0.0;
2629 if (y1)
2630 *y1 = 0.0;
2631 if (x2)
2632 *x2 = 0.0;
2633 if (y2)
2634 *y2 = 0.0;
2636 return;
2639 status = _cairo_gstate_clip_extents (cr->gstate, x1, y1, x2, y2);
2640 if (unlikely (status))
2641 _cairo_set_error (cr, status);
2644 static cairo_rectangle_list_t *
2645 _cairo_rectangle_list_create_in_error (cairo_status_t status)
2647 cairo_rectangle_list_t *list;
2649 if (status == CAIRO_STATUS_NO_MEMORY)
2650 return (cairo_rectangle_list_t*) &_cairo_rectangles_nil;
2652 list = malloc (sizeof (cairo_rectangle_list_t));
2653 if (unlikely (list == NULL)) {
2654 status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
2655 return (cairo_rectangle_list_t*) &_cairo_rectangles_nil;
2658 list->status = status;
2659 list->rectangles = NULL;
2660 list->num_rectangles = 0;
2661 return list;
2665 * cairo_copy_clip_rectangle_list:
2666 * @cr: a cairo context
2668 * Gets the current clip region as a list of rectangles in user coordinates.
2669 * Never returns %NULL.
2671 * The status in the list may be %CAIRO_STATUS_CLIP_NOT_REPRESENTABLE to
2672 * indicate that the clip region cannot be represented as a list of
2673 * user-space rectangles. The status may have other values to indicate
2674 * other errors.
2676 * Returns: the current clip region as a list of rectangles in user coordinates,
2677 * which should be destroyed using cairo_rectangle_list_destroy().
2679 * Since: 1.4
2681 cairo_rectangle_list_t *
2682 cairo_copy_clip_rectangle_list (cairo_t *cr)
2684 if (unlikely (cr->status))
2685 return _cairo_rectangle_list_create_in_error (cr->status);
2687 return _cairo_gstate_copy_clip_rectangle_list (cr->gstate);
2691 * cairo_select_font_face:
2692 * @cr: a #cairo_t
2693 * @family: a font family name, encoded in UTF-8
2694 * @slant: the slant for the font
2695 * @weight: the weight for the font
2697 * Note: The cairo_select_font_face() function call is part of what
2698 * the cairo designers call the "toy" text API. It is convenient for
2699 * short demos and simple programs, but it is not expected to be
2700 * adequate for serious text-using applications.
2702 * Selects a family and style of font from a simplified description as
2703 * a family name, slant and weight. Cairo provides no operation to
2704 * list available family names on the system (this is a "toy",
2705 * remember), but the standard CSS2 generic family names, ("serif",
2706 * "sans-serif", "cursive", "fantasy", "monospace"), are likely to
2707 * work as expected.
2709 * If @family starts with the string "@cairo:", or if no native font
2710 * backends are compiled in, cairo will use an internal font family.
2711 * The internal font family recognizes many modifiers in the @family
2712 * string, most notably, it recognizes the string "monospace". That is,
2713 * the family name "@cairo:monospace" will use the monospace version of
2714 * the internal font family.
2716 * For "real" font selection, see the font-backend-specific
2717 * font_face_create functions for the font backend you are using. (For
2718 * example, if you are using the freetype-based cairo-ft font backend,
2719 * see cairo_ft_font_face_create_for_ft_face() or
2720 * cairo_ft_font_face_create_for_pattern().) The resulting font face
2721 * could then be used with cairo_scaled_font_create() and
2722 * cairo_set_scaled_font().
2724 * Similarly, when using the "real" font support, you can call
2725 * directly into the underlying font system, (such as fontconfig or
2726 * freetype), for operations such as listing available fonts, etc.
2728 * It is expected that most applications will need to use a more
2729 * comprehensive font handling and text layout library, (for example,
2730 * pango), in conjunction with cairo.
2732 * If text is drawn without a call to cairo_select_font_face(), (nor
2733 * cairo_set_font_face() nor cairo_set_scaled_font()), the default
2734 * family is platform-specific, but is essentially "sans-serif".
2735 * Default slant is %CAIRO_FONT_SLANT_NORMAL, and default weight is
2736 * %CAIRO_FONT_WEIGHT_NORMAL.
2738 * This function is equivalent to a call to cairo_toy_font_face_create()
2739 * followed by cairo_set_font_face().
2741 void
2742 cairo_select_font_face (cairo_t *cr,
2743 const char *family,
2744 cairo_font_slant_t slant,
2745 cairo_font_weight_t weight)
2747 cairo_status_t status;
2749 if (unlikely (cr->status))
2750 return;
2752 status = _cairo_gstate_select_font_face (cr->gstate, family, slant, weight);
2753 if (unlikely (status))
2754 _cairo_set_error (cr, status);
2758 * cairo_font_extents:
2759 * @cr: a #cairo_t
2760 * @extents: a #cairo_font_extents_t object into which the results
2761 * will be stored.
2763 * Gets the font extents for the currently selected font.
2765 void
2766 cairo_font_extents (cairo_t *cr,
2767 cairo_font_extents_t *extents)
2769 cairo_status_t status;
2771 extents->ascent = 0.0;
2772 extents->descent = 0.0;
2773 extents->height = 0.0;
2774 extents->max_x_advance = 0.0;
2775 extents->max_y_advance = 0.0;
2777 if (unlikely (cr->status))
2778 return;
2780 status = _cairo_gstate_get_font_extents (cr->gstate, extents);
2781 if (unlikely (status))
2782 _cairo_set_error (cr, status);
2786 * cairo_set_font_face:
2787 * @cr: a #cairo_t
2788 * @font_face: a #cairo_font_face_t, or %NULL to restore to the default font
2790 * Replaces the current #cairo_font_face_t object in the #cairo_t with
2791 * @font_face. The replaced font face in the #cairo_t will be
2792 * destroyed if there are no other references to it.
2794 void
2795 cairo_set_font_face (cairo_t *cr,
2796 cairo_font_face_t *font_face)
2798 cairo_status_t status;
2800 if (unlikely (cr->status))
2801 return;
2803 status = _cairo_gstate_set_font_face (cr->gstate, font_face);
2804 if (unlikely (status))
2805 _cairo_set_error (cr, status);
2809 * cairo_get_font_face:
2810 * @cr: a #cairo_t
2812 * Gets the current font face for a #cairo_t.
2814 * Return value: the current font face. This object is owned by
2815 * cairo. To keep a reference to it, you must call
2816 * cairo_font_face_reference().
2818 * This function never returns %NULL. If memory cannot be allocated, a
2819 * special "nil" #cairo_font_face_t object will be returned on which
2820 * cairo_font_face_status() returns %CAIRO_STATUS_NO_MEMORY. Using
2821 * this nil object will cause its error state to propagate to other
2822 * objects it is passed to, (for example, calling
2823 * cairo_set_font_face() with a nil font will trigger an error that
2824 * will shutdown the #cairo_t object).
2826 cairo_font_face_t *
2827 cairo_get_font_face (cairo_t *cr)
2829 cairo_status_t status;
2830 cairo_font_face_t *font_face;
2832 if (unlikely (cr->status))
2833 return (cairo_font_face_t*) &_cairo_font_face_nil;
2835 status = _cairo_gstate_get_font_face (cr->gstate, &font_face);
2836 if (unlikely (status)) {
2837 _cairo_set_error (cr, status);
2838 return (cairo_font_face_t*) &_cairo_font_face_nil;
2841 return font_face;
2845 * cairo_set_font_size:
2846 * @cr: a #cairo_t
2847 * @size: the new font size, in user space units
2849 * Sets the current font matrix to a scale by a factor of @size, replacing
2850 * any font matrix previously set with cairo_set_font_size() or
2851 * cairo_set_font_matrix(). This results in a font size of @size user space
2852 * units. (More precisely, this matrix will result in the font's
2853 * em-square being a @size by @size square in user space.)
2855 * If text is drawn without a call to cairo_set_font_size(), (nor
2856 * cairo_set_font_matrix() nor cairo_set_scaled_font()), the default
2857 * font size is 10.0.
2859 void
2860 cairo_set_font_size (cairo_t *cr, double size)
2862 cairo_status_t status;
2864 if (unlikely (cr->status))
2865 return;
2867 status = _cairo_gstate_set_font_size (cr->gstate, size);
2868 if (unlikely (status))
2869 _cairo_set_error (cr, status);
2871 slim_hidden_def (cairo_set_font_size);
2874 * cairo_set_font_matrix
2875 * @cr: a #cairo_t
2876 * @matrix: a #cairo_matrix_t describing a transform to be applied to
2877 * the current font.
2879 * Sets the current font matrix to @matrix. The font matrix gives a
2880 * transformation from the design space of the font (in this space,
2881 * the em-square is 1 unit by 1 unit) to user space. Normally, a
2882 * simple scale is used (see cairo_set_font_size()), but a more
2883 * complex font matrix can be used to shear the font
2884 * or stretch it unequally along the two axes
2886 void
2887 cairo_set_font_matrix (cairo_t *cr,
2888 const cairo_matrix_t *matrix)
2890 cairo_status_t status;
2892 if (unlikely (cr->status))
2893 return;
2895 status = _cairo_gstate_set_font_matrix (cr->gstate, matrix);
2896 if (unlikely (status))
2897 _cairo_set_error (cr, status);
2901 * cairo_get_font_matrix
2902 * @cr: a #cairo_t
2903 * @matrix: return value for the matrix
2905 * Stores the current font matrix into @matrix. See
2906 * cairo_set_font_matrix().
2908 void
2909 cairo_get_font_matrix (cairo_t *cr, cairo_matrix_t *matrix)
2911 if (unlikely (cr->status)) {
2912 cairo_matrix_init_identity (matrix);
2913 return;
2916 _cairo_gstate_get_font_matrix (cr->gstate, matrix);
2920 * cairo_set_font_options:
2921 * @cr: a #cairo_t
2922 * @options: font options to use
2924 * Sets a set of custom font rendering options for the #cairo_t.
2925 * Rendering options are derived by merging these options with the
2926 * options derived from underlying surface; if the value in @options
2927 * has a default value (like %CAIRO_ANTIALIAS_DEFAULT), then the value
2928 * from the surface is used.
2930 void
2931 cairo_set_font_options (cairo_t *cr,
2932 const cairo_font_options_t *options)
2934 cairo_status_t status;
2936 if (unlikely (cr->status))
2937 return;
2939 status = cairo_font_options_status ((cairo_font_options_t *) options);
2940 if (unlikely (status)) {
2941 _cairo_set_error (cr, status);
2942 return;
2945 _cairo_gstate_set_font_options (cr->gstate, options);
2947 slim_hidden_def (cairo_set_font_options);
2950 * cairo_get_font_options:
2951 * @cr: a #cairo_t
2952 * @options: a #cairo_font_options_t object into which to store
2953 * the retrieved options. All existing values are overwritten
2955 * Retrieves font rendering options set via #cairo_set_font_options.
2956 * Note that the returned options do not include any options derived
2957 * from the underlying surface; they are literally the options
2958 * passed to cairo_set_font_options().
2960 void
2961 cairo_get_font_options (cairo_t *cr,
2962 cairo_font_options_t *options)
2964 /* check that we aren't trying to overwrite the nil object */
2965 if (cairo_font_options_status (options))
2966 return;
2968 if (unlikely (cr->status)) {
2969 _cairo_font_options_init_default (options);
2970 return;
2973 _cairo_gstate_get_font_options (cr->gstate, options);
2977 * cairo_set_scaled_font:
2978 * @cr: a #cairo_t
2979 * @scaled_font: a #cairo_scaled_font_t
2981 * Replaces the current font face, font matrix, and font options in
2982 * the #cairo_t with those of the #cairo_scaled_font_t. Except for
2983 * some translation, the current CTM of the #cairo_t should be the
2984 * same as that of the #cairo_scaled_font_t, which can be accessed
2985 * using cairo_scaled_font_get_ctm().
2987 * Since: 1.2
2989 void
2990 cairo_set_scaled_font (cairo_t *cr,
2991 const cairo_scaled_font_t *scaled_font)
2993 cairo_status_t status;
2994 cairo_bool_t was_previous;
2996 if (unlikely (cr->status))
2997 return;
2999 if (scaled_font == NULL) {
3000 status = _cairo_error (CAIRO_STATUS_NULL_POINTER);
3001 goto BAIL;
3004 status = scaled_font->status;
3005 if (unlikely (status))
3006 goto BAIL;
3008 if (scaled_font == cr->gstate->scaled_font)
3009 return;
3011 was_previous = scaled_font == cr->gstate->previous_scaled_font;
3013 status = _cairo_gstate_set_font_face (cr->gstate, scaled_font->font_face);
3014 if (unlikely (status))
3015 goto BAIL;
3017 status = _cairo_gstate_set_font_matrix (cr->gstate, &scaled_font->font_matrix);
3018 if (unlikely (status))
3019 goto BAIL;
3021 _cairo_gstate_set_font_options (cr->gstate, &scaled_font->options);
3023 if (was_previous)
3024 cr->gstate->scaled_font = cairo_scaled_font_reference ((cairo_scaled_font_t *) scaled_font);
3026 return;
3028 BAIL:
3029 _cairo_set_error (cr, status);
3033 * cairo_get_scaled_font:
3034 * @cr: a #cairo_t
3036 * Gets the current scaled font for a #cairo_t.
3038 * Return value: the current scaled font. This object is owned by
3039 * cairo. To keep a reference to it, you must call
3040 * cairo_scaled_font_reference().
3042 * This function never returns %NULL. If memory cannot be allocated, a
3043 * special "nil" #cairo_scaled_font_t object will be returned on which
3044 * cairo_scaled_font_status() returns %CAIRO_STATUS_NO_MEMORY. Using
3045 * this nil object will cause its error state to propagate to other
3046 * objects it is passed to, (for example, calling
3047 * cairo_set_scaled_font() with a nil font will trigger an error that
3048 * will shutdown the #cairo_t object).
3050 * Since: 1.4
3052 cairo_scaled_font_t *
3053 cairo_get_scaled_font (cairo_t *cr)
3055 cairo_status_t status;
3056 cairo_scaled_font_t *scaled_font;
3058 if (unlikely (cr->status))
3059 return _cairo_scaled_font_create_in_error (cr->status);
3061 status = _cairo_gstate_get_scaled_font (cr->gstate, &scaled_font);
3062 if (unlikely (status)) {
3063 _cairo_set_error (cr, status);
3064 return _cairo_scaled_font_create_in_error (status);
3067 return scaled_font;
3071 * cairo_text_extents:
3072 * @cr: a #cairo_t
3073 * @utf8: a NUL-terminated string of text encoded in UTF-8, or %NULL
3074 * @extents: a #cairo_text_extents_t object into which the results
3075 * will be stored
3077 * Gets the extents for a string of text. The extents describe a
3078 * user-space rectangle that encloses the "inked" portion of the text,
3079 * (as it would be drawn by cairo_show_text()). Additionally, the
3080 * x_advance and y_advance values indicate the amount by which the
3081 * current point would be advanced by cairo_show_text().
3083 * Note that whitespace characters do not directly contribute to the
3084 * size of the rectangle (extents.width and extents.height). They do
3085 * contribute indirectly by changing the position of non-whitespace
3086 * characters. In particular, trailing whitespace characters are
3087 * likely to not affect the size of the rectangle, though they will
3088 * affect the x_advance and y_advance values.
3090 void
3091 cairo_text_extents (cairo_t *cr,
3092 const char *utf8,
3093 cairo_text_extents_t *extents)
3095 cairo_status_t status;
3096 cairo_glyph_t *glyphs = NULL;
3097 int num_glyphs;
3098 double x, y;
3100 extents->x_bearing = 0.0;
3101 extents->y_bearing = 0.0;
3102 extents->width = 0.0;
3103 extents->height = 0.0;
3104 extents->x_advance = 0.0;
3105 extents->y_advance = 0.0;
3107 if (unlikely (cr->status))
3108 return;
3110 if (utf8 == NULL)
3111 return;
3113 cairo_get_current_point (cr, &x, &y);
3115 status = _cairo_gstate_text_to_glyphs (cr->gstate,
3116 x, y,
3117 utf8, strlen (utf8),
3118 &glyphs, &num_glyphs,
3119 NULL, NULL,
3120 NULL);
3122 if (status == CAIRO_STATUS_SUCCESS)
3123 status = _cairo_gstate_glyph_extents (cr->gstate,
3124 glyphs, num_glyphs,
3125 extents);
3126 cairo_glyph_free (glyphs);
3128 if (unlikely (status))
3129 _cairo_set_error (cr, status);
3133 * cairo_glyph_extents:
3134 * @cr: a #cairo_t
3135 * @glyphs: an array of #cairo_glyph_t objects
3136 * @num_glyphs: the number of elements in @glyphs
3137 * @extents: a #cairo_text_extents_t object into which the results
3138 * will be stored
3140 * Gets the extents for an array of glyphs. The extents describe a
3141 * user-space rectangle that encloses the "inked" portion of the
3142 * glyphs, (as they would be drawn by cairo_show_glyphs()).
3143 * Additionally, the x_advance and y_advance values indicate the
3144 * amount by which the current point would be advanced by
3145 * cairo_show_glyphs().
3147 * Note that whitespace glyphs do not contribute to the size of the
3148 * rectangle (extents.width and extents.height).
3150 void
3151 cairo_glyph_extents (cairo_t *cr,
3152 const cairo_glyph_t *glyphs,
3153 int num_glyphs,
3154 cairo_text_extents_t *extents)
3156 cairo_status_t status;
3158 extents->x_bearing = 0.0;
3159 extents->y_bearing = 0.0;
3160 extents->width = 0.0;
3161 extents->height = 0.0;
3162 extents->x_advance = 0.0;
3163 extents->y_advance = 0.0;
3165 if (unlikely (cr->status))
3166 return;
3168 if (num_glyphs == 0)
3169 return;
3171 if (num_glyphs < 0) {
3172 _cairo_set_error (cr, CAIRO_STATUS_NEGATIVE_COUNT);
3173 return;
3176 if (glyphs == NULL) {
3177 _cairo_set_error (cr, CAIRO_STATUS_NULL_POINTER);
3178 return;
3181 status = _cairo_gstate_glyph_extents (cr->gstate, glyphs, num_glyphs,
3182 extents);
3183 if (unlikely (status))
3184 _cairo_set_error (cr, status);
3188 * cairo_show_text:
3189 * @cr: a cairo context
3190 * @utf8: a NUL-terminated string of text encoded in UTF-8, or %NULL
3192 * A drawing operator that generates the shape from a string of UTF-8
3193 * characters, rendered according to the current font_face, font_size
3194 * (font_matrix), and font_options.
3196 * This function first computes a set of glyphs for the string of
3197 * text. The first glyph is placed so that its origin is at the
3198 * current point. The origin of each subsequent glyph is offset from
3199 * that of the previous glyph by the advance values of the previous
3200 * glyph.
3202 * After this call the current point is moved to the origin of where
3203 * the next glyph would be placed in this same progression. That is,
3204 * the current point will be at the origin of the final glyph offset
3205 * by its advance values. This allows for easy display of a single
3206 * logical string with multiple calls to cairo_show_text().
3208 * Note: The cairo_show_text() function call is part of what the cairo
3209 * designers call the "toy" text API. It is convenient for short demos
3210 * and simple programs, but it is not expected to be adequate for
3211 * serious text-using applications. See cairo_show_glyphs() for the
3212 * "real" text display API in cairo.
3214 void
3215 cairo_show_text (cairo_t *cr, const char *utf8)
3217 cairo_text_extents_t extents;
3218 cairo_status_t status;
3219 cairo_glyph_t *glyphs, *last_glyph;
3220 cairo_text_cluster_t *clusters;
3221 int utf8_len, num_glyphs, num_clusters;
3222 cairo_text_cluster_flags_t cluster_flags;
3223 double x, y;
3224 cairo_bool_t has_show_text_glyphs;
3225 cairo_glyph_t stack_glyphs[CAIRO_STACK_ARRAY_LENGTH (cairo_glyph_t)];
3226 cairo_text_cluster_t stack_clusters[CAIRO_STACK_ARRAY_LENGTH (cairo_text_cluster_t)];
3228 if (unlikely (cr->status))
3229 return;
3231 if (utf8 == NULL)
3232 return;
3234 cairo_get_current_point (cr, &x, &y);
3236 utf8_len = strlen (utf8);
3238 has_show_text_glyphs =
3239 cairo_surface_has_show_text_glyphs (cairo_get_target (cr));
3241 glyphs = stack_glyphs;
3242 num_glyphs = ARRAY_LENGTH (stack_glyphs);
3244 if (has_show_text_glyphs) {
3245 clusters = stack_clusters;
3246 num_clusters = ARRAY_LENGTH (stack_clusters);
3247 } else {
3248 clusters = NULL;
3249 num_clusters = 0;
3252 status = _cairo_gstate_text_to_glyphs (cr->gstate,
3253 x, y,
3254 utf8, utf8_len,
3255 &glyphs, &num_glyphs,
3256 has_show_text_glyphs ? &clusters : NULL, &num_clusters,
3257 &cluster_flags);
3258 if (unlikely (status))
3259 goto BAIL;
3261 if (num_glyphs == 0)
3262 return;
3264 status = _cairo_gstate_show_text_glyphs (cr->gstate,
3265 utf8, utf8_len,
3266 glyphs, num_glyphs,
3267 clusters, num_clusters,
3268 cluster_flags);
3269 if (unlikely (status))
3270 goto BAIL;
3272 last_glyph = &glyphs[num_glyphs - 1];
3273 status = _cairo_gstate_glyph_extents (cr->gstate,
3274 last_glyph, 1,
3275 &extents);
3276 if (unlikely (status))
3277 goto BAIL;
3279 x = last_glyph->x + extents.x_advance;
3280 y = last_glyph->y + extents.y_advance;
3281 cairo_move_to (cr, x, y);
3283 BAIL:
3284 if (glyphs != stack_glyphs)
3285 cairo_glyph_free (glyphs);
3286 if (clusters != stack_clusters)
3287 cairo_text_cluster_free (clusters);
3289 if (unlikely (status))
3290 _cairo_set_error (cr, status);
3294 * cairo_show_glyphs:
3295 * @cr: a cairo context
3296 * @glyphs: array of glyphs to show
3297 * @num_glyphs: number of glyphs to show
3299 * A drawing operator that generates the shape from an array of glyphs,
3300 * rendered according to the current font face, font size
3301 * (font matrix), and font options.
3303 void
3304 cairo_show_glyphs (cairo_t *cr, const cairo_glyph_t *glyphs, int num_glyphs)
3306 cairo_status_t status;
3308 if (unlikely (cr->status))
3309 return;
3311 if (num_glyphs == 0)
3312 return;
3314 if (num_glyphs < 0) {
3315 _cairo_set_error (cr, CAIRO_STATUS_NEGATIVE_COUNT);
3316 return;
3319 if (glyphs == NULL) {
3320 _cairo_set_error (cr, CAIRO_STATUS_NULL_POINTER);
3321 return;
3324 status = _cairo_gstate_show_text_glyphs (cr->gstate,
3325 NULL, 0,
3326 glyphs, num_glyphs,
3327 NULL, 0,
3328 FALSE);
3329 if (unlikely (status))
3330 _cairo_set_error (cr, status);
3334 * cairo_show_text_glyphs:
3335 * @cr: a cairo context
3336 * @utf8: a string of text encoded in UTF-8
3337 * @utf8_len: length of @utf8 in bytes, or -1 if it is NUL-terminated
3338 * @glyphs: array of glyphs to show
3339 * @num_glyphs: number of glyphs to show
3340 * @clusters: array of cluster mapping information
3341 * @num_clusters: number of clusters in the mapping
3342 * @cluster_flags: cluster mapping flags
3344 * This operation has rendering effects similar to cairo_show_glyphs()
3345 * but, if the target surface supports it, uses the provided text and
3346 * cluster mapping to embed the text for the glyphs shown in the output.
3347 * If the target does not support the extended attributes, this function
3348 * acts like the basic cairo_show_glyphs() as if it had been passed
3349 * @glyphs and @num_glyphs.
3351 * The mapping between @utf8 and @glyphs is provided by an array of
3352 * <firstterm>clusters</firstterm>. Each cluster covers a number of
3353 * text bytes and glyphs, and neighboring clusters cover neighboring
3354 * areas of @utf8 and @glyphs. The clusters should collectively cover @utf8
3355 * and @glyphs in entirety.
3357 * The first cluster always covers bytes from the beginning of @utf8.
3358 * If @cluster_flags do not have the %CAIRO_TEXT_CLUSTER_FLAG_BACKWARD
3359 * set, the first cluster also covers the beginning
3360 * of @glyphs, otherwise it covers the end of the @glyphs array and
3361 * following clusters move backward.
3363 * See #cairo_text_cluster_t for constraints on valid clusters.
3365 * Since: 1.8
3367 void
3368 cairo_show_text_glyphs (cairo_t *cr,
3369 const char *utf8,
3370 int utf8_len,
3371 const cairo_glyph_t *glyphs,
3372 int num_glyphs,
3373 const cairo_text_cluster_t *clusters,
3374 int num_clusters,
3375 cairo_text_cluster_flags_t cluster_flags)
3377 cairo_status_t status;
3379 if (unlikely (cr->status))
3380 return;
3382 /* A slew of sanity checks */
3384 /* Special case for NULL and -1 */
3385 if (utf8 == NULL && utf8_len == -1)
3386 utf8_len = 0;
3388 /* No NULLs for non-zeros */
3389 if ((num_glyphs && glyphs == NULL) ||
3390 (utf8_len && utf8 == NULL) ||
3391 (num_clusters && clusters == NULL)) {
3392 _cairo_set_error (cr, CAIRO_STATUS_NULL_POINTER);
3393 return;
3396 /* A -1 for utf8_len means NUL-terminated */
3397 if (utf8_len == -1)
3398 utf8_len = strlen (utf8);
3400 /* Apart from that, no negatives */
3401 if (num_glyphs < 0 || utf8_len < 0 || num_clusters < 0) {
3402 _cairo_set_error (cr, CAIRO_STATUS_NEGATIVE_COUNT);
3403 return;
3406 /* Make sure clusters cover the entire glyphs and utf8 arrays,
3407 * and that cluster boundaries are UTF-8 boundaries. */
3408 status = _cairo_validate_text_clusters (utf8, utf8_len,
3409 glyphs, num_glyphs,
3410 clusters, num_clusters, cluster_flags);
3411 if (status == CAIRO_STATUS_INVALID_CLUSTERS) {
3412 /* Either got invalid UTF-8 text, or cluster mapping is bad.
3413 * Differentiate those. */
3415 cairo_status_t status2;
3417 status2 = _cairo_utf8_to_ucs4 (utf8, utf8_len, NULL, NULL);
3418 if (status2)
3419 status = status2;
3421 _cairo_set_error (cr, status);
3422 return;
3425 if (num_glyphs == 0 && utf8_len == 0)
3426 return;
3428 status = _cairo_gstate_show_text_glyphs (cr->gstate,
3429 utf8, utf8_len,
3430 glyphs, num_glyphs,
3431 clusters, num_clusters, cluster_flags);
3432 if (unlikely (status))
3433 _cairo_set_error (cr, status);
3437 * cairo_text_path:
3438 * @cr: a cairo context
3439 * @utf8: a NUL-terminated string of text encoded in UTF-8, or %NULL
3441 * Adds closed paths for text to the current path. The generated
3442 * path if filled, achieves an effect similar to that of
3443 * cairo_show_text().
3445 * Text conversion and positioning is done similar to cairo_show_text().
3447 * Like cairo_show_text(), After this call the current point is
3448 * moved to the origin of where the next glyph would be placed in
3449 * this same progression. That is, the current point will be at
3450 * the origin of the final glyph offset by its advance values.
3451 * This allows for chaining multiple calls to to cairo_text_path()
3452 * without having to set current point in between.
3454 * Note: The cairo_text_path() function call is part of what the cairo
3455 * designers call the "toy" text API. It is convenient for short demos
3456 * and simple programs, but it is not expected to be adequate for
3457 * serious text-using applications. See cairo_glyph_path() for the
3458 * "real" text path API in cairo.
3460 void
3461 cairo_text_path (cairo_t *cr, const char *utf8)
3463 cairo_status_t status;
3464 cairo_text_extents_t extents;
3465 cairo_glyph_t stack_glyphs[CAIRO_STACK_ARRAY_LENGTH (cairo_glyph_t)];
3466 cairo_glyph_t *glyphs, *last_glyph;
3467 int num_glyphs;
3468 double x, y;
3470 if (unlikely (cr->status))
3471 return;
3473 if (utf8 == NULL)
3474 return;
3476 cairo_get_current_point (cr, &x, &y);
3478 glyphs = stack_glyphs;
3479 num_glyphs = ARRAY_LENGTH (stack_glyphs);
3481 status = _cairo_gstate_text_to_glyphs (cr->gstate,
3482 x, y,
3483 utf8, strlen (utf8),
3484 &glyphs, &num_glyphs,
3485 NULL, NULL,
3486 NULL);
3488 if (unlikely (status))
3489 goto BAIL;
3491 if (num_glyphs == 0)
3492 return;
3494 status = _cairo_gstate_glyph_path (cr->gstate,
3495 glyphs, num_glyphs,
3496 cr->path);
3498 if (unlikely (status))
3499 goto BAIL;
3501 last_glyph = &glyphs[num_glyphs - 1];
3502 status = _cairo_gstate_glyph_extents (cr->gstate,
3503 last_glyph, 1,
3504 &extents);
3506 if (unlikely (status))
3507 goto BAIL;
3509 x = last_glyph->x + extents.x_advance;
3510 y = last_glyph->y + extents.y_advance;
3511 cairo_move_to (cr, x, y);
3513 BAIL:
3514 if (glyphs != stack_glyphs)
3515 cairo_glyph_free (glyphs);
3517 if (unlikely (status))
3518 _cairo_set_error (cr, status);
3522 * cairo_glyph_path:
3523 * @cr: a cairo context
3524 * @glyphs: array of glyphs to show
3525 * @num_glyphs: number of glyphs to show
3527 * Adds closed paths for the glyphs to the current path. The generated
3528 * path if filled, achieves an effect similar to that of
3529 * cairo_show_glyphs().
3531 void
3532 cairo_glyph_path (cairo_t *cr, const cairo_glyph_t *glyphs, int num_glyphs)
3534 cairo_status_t status;
3536 if (unlikely (cr->status))
3537 return;
3539 if (num_glyphs == 0)
3540 return;
3542 if (num_glyphs < 0) {
3543 _cairo_set_error (cr, CAIRO_STATUS_NEGATIVE_COUNT);
3544 return;
3547 if (glyphs == NULL) {
3548 _cairo_set_error (cr, CAIRO_STATUS_NULL_POINTER);
3549 return;
3552 status = _cairo_gstate_glyph_path (cr->gstate,
3553 glyphs, num_glyphs,
3554 cr->path);
3555 if (unlikely (status))
3556 _cairo_set_error (cr, status);
3560 * cairo_get_operator:
3561 * @cr: a cairo context
3563 * Gets the current compositing operator for a cairo context.
3565 * Return value: the current compositing operator.
3567 cairo_operator_t
3568 cairo_get_operator (cairo_t *cr)
3570 if (unlikely (cr->status))
3571 return CAIRO_GSTATE_OPERATOR_DEFAULT;
3573 return _cairo_gstate_get_operator (cr->gstate);
3577 * cairo_get_tolerance:
3578 * @cr: a cairo context
3580 * Gets the current tolerance value, as set by cairo_set_tolerance().
3582 * Return value: the current tolerance value.
3584 double
3585 cairo_get_tolerance (cairo_t *cr)
3587 if (unlikely (cr->status))
3588 return CAIRO_GSTATE_TOLERANCE_DEFAULT;
3590 return _cairo_gstate_get_tolerance (cr->gstate);
3592 slim_hidden_def (cairo_get_tolerance);
3595 * cairo_get_antialias:
3596 * @cr: a cairo context
3598 * Gets the current shape antialiasing mode, as set by cairo_set_shape_antialias().
3600 * Return value: the current shape antialiasing mode.
3602 cairo_antialias_t
3603 cairo_get_antialias (cairo_t *cr)
3605 if (unlikely (cr->status))
3606 return CAIRO_ANTIALIAS_DEFAULT;
3608 return _cairo_gstate_get_antialias (cr->gstate);
3612 * cairo_has_current_point:
3613 * @cr: a cairo context
3615 * Returns whether a current point is defined on the current path.
3616 * See cairo_get_current_point() for details on the current point.
3618 * Return value: whether a current point is defined.
3620 * Since: 1.6
3622 cairo_bool_t
3623 cairo_has_current_point (cairo_t *cr)
3625 if (unlikely (cr->status))
3626 return FALSE;
3628 return cr->path->has_current_point;
3632 * cairo_get_current_point:
3633 * @cr: a cairo context
3634 * @x: return value for X coordinate of the current point
3635 * @y: return value for Y coordinate of the current point
3637 * Gets the current point of the current path, which is
3638 * conceptually the final point reached by the path so far.
3640 * The current point is returned in the user-space coordinate
3641 * system. If there is no defined current point or if @cr is in an
3642 * error status, @x and @y will both be set to 0.0. It is possible to
3643 * check this in advance with cairo_has_current_point().
3645 * Most path construction functions alter the current point. See the
3646 * following for details on how they affect the current point:
3647 * cairo_new_path(), cairo_new_sub_path(),
3648 * cairo_append_path(), cairo_close_path(),
3649 * cairo_move_to(), cairo_line_to(), cairo_curve_to(),
3650 * cairo_rel_move_to(), cairo_rel_line_to(), cairo_rel_curve_to(),
3651 * cairo_arc(), cairo_arc_negative(), cairo_rectangle(),
3652 * cairo_text_path(), cairo_glyph_path(), cairo_stroke_to_path().
3654 * Some functions use and alter the current point but do not
3655 * otherwise change current path:
3656 * cairo_show_text().
3658 * Some functions unset the current path and as a result, current point:
3659 * cairo_fill(), cairo_stroke().
3661 void
3662 cairo_get_current_point (cairo_t *cr, double *x_ret, double *y_ret)
3664 cairo_fixed_t x_fixed, y_fixed;
3665 double x, y;
3667 if (cr->status == CAIRO_STATUS_SUCCESS &&
3668 _cairo_path_fixed_get_current_point (cr->path, &x_fixed, &y_fixed))
3670 x = _cairo_fixed_to_double (x_fixed);
3671 y = _cairo_fixed_to_double (y_fixed);
3672 _cairo_gstate_backend_to_user (cr->gstate, &x, &y);
3674 else
3676 x = 0.0;
3677 y = 0.0;
3680 if (x_ret)
3681 *x_ret = x;
3682 if (y_ret)
3683 *y_ret = y;
3685 slim_hidden_def(cairo_get_current_point);
3688 * cairo_get_fill_rule:
3689 * @cr: a cairo context
3691 * Gets the current fill rule, as set by cairo_set_fill_rule().
3693 * Return value: the current fill rule.
3695 cairo_fill_rule_t
3696 cairo_get_fill_rule (cairo_t *cr)
3698 if (unlikely (cr->status))
3699 return CAIRO_GSTATE_FILL_RULE_DEFAULT;
3701 return _cairo_gstate_get_fill_rule (cr->gstate);
3705 * cairo_get_line_width:
3706 * @cr: a cairo context
3708 * This function returns the current line width value exactly as set by
3709 * cairo_set_line_width(). Note that the value is unchanged even if
3710 * the CTM has changed between the calls to cairo_set_line_width() and
3711 * cairo_get_line_width().
3713 * Return value: the current line width.
3715 double
3716 cairo_get_line_width (cairo_t *cr)
3718 if (unlikely (cr->status))
3719 return CAIRO_GSTATE_LINE_WIDTH_DEFAULT;
3721 return _cairo_gstate_get_line_width (cr->gstate);
3723 slim_hidden_def (cairo_get_line_width);
3726 * cairo_get_line_cap:
3727 * @cr: a cairo context
3729 * Gets the current line cap style, as set by cairo_set_line_cap().
3731 * Return value: the current line cap style.
3733 cairo_line_cap_t
3734 cairo_get_line_cap (cairo_t *cr)
3736 if (unlikely (cr->status))
3737 return CAIRO_GSTATE_LINE_CAP_DEFAULT;
3739 return _cairo_gstate_get_line_cap (cr->gstate);
3743 * cairo_get_line_join:
3744 * @cr: a cairo context
3746 * Gets the current line join style, as set by cairo_set_line_join().
3748 * Return value: the current line join style.
3750 cairo_line_join_t
3751 cairo_get_line_join (cairo_t *cr)
3753 if (unlikely (cr->status))
3754 return CAIRO_GSTATE_LINE_JOIN_DEFAULT;
3756 return _cairo_gstate_get_line_join (cr->gstate);
3760 * cairo_get_miter_limit:
3761 * @cr: a cairo context
3763 * Gets the current miter limit, as set by cairo_set_miter_limit().
3765 * Return value: the current miter limit.
3767 double
3768 cairo_get_miter_limit (cairo_t *cr)
3770 if (unlikely (cr->status))
3771 return CAIRO_GSTATE_MITER_LIMIT_DEFAULT;
3773 return _cairo_gstate_get_miter_limit (cr->gstate);
3777 * cairo_get_matrix:
3778 * @cr: a cairo context
3779 * @matrix: return value for the matrix
3781 * Stores the current transformation matrix (CTM) into @matrix.
3783 void
3784 cairo_get_matrix (cairo_t *cr, cairo_matrix_t *matrix)
3786 if (unlikely (cr->status)) {
3787 cairo_matrix_init_identity (matrix);
3788 return;
3791 _cairo_gstate_get_matrix (cr->gstate, matrix);
3793 slim_hidden_def (cairo_get_matrix);
3796 * cairo_get_target:
3797 * @cr: a cairo context
3799 * Gets the target surface for the cairo context as passed to
3800 * cairo_create().
3802 * This function will always return a valid pointer, but the result
3803 * can be a "nil" surface if @cr is already in an error state,
3804 * (ie. cairo_status() <literal>!=</literal> %CAIRO_STATUS_SUCCESS).
3805 * A nil surface is indicated by cairo_surface_status()
3806 * <literal>!=</literal> %CAIRO_STATUS_SUCCESS.
3808 * Return value: the target surface. This object is owned by cairo. To
3809 * keep a reference to it, you must call cairo_surface_reference().
3811 cairo_surface_t *
3812 cairo_get_target (cairo_t *cr)
3814 if (unlikely (cr->status))
3815 return _cairo_surface_create_in_error (cr->status);
3817 return _cairo_gstate_get_original_target (cr->gstate);
3819 slim_hidden_def (cairo_get_target);
3822 * cairo_get_group_target:
3823 * @cr: a cairo context
3825 * Gets the current destination surface for the context. This is either
3826 * the original target surface as passed to cairo_create() or the target
3827 * surface for the current group as started by the most recent call to
3828 * cairo_push_group() or cairo_push_group_with_content().
3830 * This function will always return a valid pointer, but the result
3831 * can be a "nil" surface if @cr is already in an error state,
3832 * (ie. cairo_status() <literal>!=</literal> %CAIRO_STATUS_SUCCESS).
3833 * A nil surface is indicated by cairo_surface_status()
3834 * <literal>!=</literal> %CAIRO_STATUS_SUCCESS.
3836 * Return value: the target surface. This object is owned by cairo. To
3837 * keep a reference to it, you must call cairo_surface_reference().
3839 * Since: 1.2
3841 cairo_surface_t *
3842 cairo_get_group_target (cairo_t *cr)
3844 if (unlikely (cr->status))
3845 return _cairo_surface_create_in_error (cr->status);
3847 return _cairo_gstate_get_target (cr->gstate);
3851 * cairo_copy_path:
3852 * @cr: a cairo context
3854 * Creates a copy of the current path and returns it to the user as a
3855 * #cairo_path_t. See #cairo_path_data_t for hints on how to iterate
3856 * over the returned data structure.
3858 * This function will always return a valid pointer, but the result
3859 * will have no data (<literal>data==%NULL</literal> and
3860 * <literal>num_data==0</literal>), if either of the following
3861 * conditions hold:
3863 * <orderedlist>
3864 * <listitem>If there is insufficient memory to copy the path. In this
3865 * case <literal>path->status</literal> will be set to
3866 * %CAIRO_STATUS_NO_MEMORY.</listitem>
3867 * <listitem>If @cr is already in an error state. In this case
3868 * <literal>path->status</literal> will contain the same status that
3869 * would be returned by cairo_status().</listitem>
3870 * </orderedlist>
3872 * Return value: the copy of the current path. The caller owns the
3873 * returned object and should call cairo_path_destroy() when finished
3874 * with it.
3876 cairo_path_t *
3877 cairo_copy_path (cairo_t *cr)
3879 if (unlikely (cr->status))
3880 return _cairo_path_create_in_error (cr->status);
3882 return _cairo_path_create (cr->path, cr->gstate);
3886 * cairo_copy_path_flat:
3887 * @cr: a cairo context
3889 * Gets a flattened copy of the current path and returns it to the
3890 * user as a #cairo_path_t. See #cairo_path_data_t for hints on
3891 * how to iterate over the returned data structure.
3893 * This function is like cairo_copy_path() except that any curves
3894 * in the path will be approximated with piecewise-linear
3895 * approximations, (accurate to within the current tolerance
3896 * value). That is, the result is guaranteed to not have any elements
3897 * of type %CAIRO_PATH_CURVE_TO which will instead be replaced by a
3898 * series of %CAIRO_PATH_LINE_TO elements.
3900 * This function will always return a valid pointer, but the result
3901 * will have no data (<literal>data==%NULL</literal> and
3902 * <literal>num_data==0</literal>), if either of the following
3903 * conditions hold:
3905 * <orderedlist>
3906 * <listitem>If there is insufficient memory to copy the path. In this
3907 * case <literal>path->status</literal> will be set to
3908 * %CAIRO_STATUS_NO_MEMORY.</listitem>
3909 * <listitem>If @cr is already in an error state. In this case
3910 * <literal>path->status</literal> will contain the same status that
3911 * would be returned by cairo_status().</listitem>
3912 * </orderedlist>
3914 * Return value: the copy of the current path. The caller owns the
3915 * returned object and should call cairo_path_destroy() when finished
3916 * with it.
3918 cairo_path_t *
3919 cairo_copy_path_flat (cairo_t *cr)
3921 if (unlikely (cr->status))
3922 return _cairo_path_create_in_error (cr->status);
3924 return _cairo_path_create_flat (cr->path, cr->gstate);
3928 * cairo_append_path:
3929 * @cr: a cairo context
3930 * @path: path to be appended
3932 * Append the @path onto the current path. The @path may be either the
3933 * return value from one of cairo_copy_path() or
3934 * cairo_copy_path_flat() or it may be constructed manually. See
3935 * #cairo_path_t for details on how the path data structure should be
3936 * initialized, and note that <literal>path->status</literal> must be
3937 * initialized to %CAIRO_STATUS_SUCCESS.
3939 void
3940 cairo_append_path (cairo_t *cr,
3941 const cairo_path_t *path)
3943 cairo_status_t status;
3945 if (unlikely (cr->status))
3946 return;
3948 if (path == NULL) {
3949 _cairo_set_error (cr, CAIRO_STATUS_NULL_POINTER);
3950 return;
3953 if (path->status) {
3954 if (path->status > CAIRO_STATUS_SUCCESS &&
3955 path->status <= CAIRO_STATUS_LAST_STATUS)
3956 _cairo_set_error (cr, path->status);
3957 else
3958 _cairo_set_error (cr, CAIRO_STATUS_INVALID_STATUS);
3959 return;
3962 if (path->num_data == 0)
3963 return;
3965 if (path->data == NULL) {
3966 _cairo_set_error (cr, CAIRO_STATUS_NULL_POINTER);
3967 return;
3970 status = _cairo_path_append_to_context (path, cr);
3971 if (unlikely (status))
3972 _cairo_set_error (cr, status);
3976 * cairo_status:
3977 * @cr: a cairo context
3979 * Checks whether an error has previously occurred for this context.
3981 * Returns: the current status of this context, see #cairo_status_t
3983 cairo_status_t
3984 cairo_status (cairo_t *cr)
3986 return cr->status;
3988 slim_hidden_def (cairo_status);