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
36 * Carl D. Worth <cworth@cworth.org>
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 */
52 {{ 0 }, { 0 }}, /* gstate_tail */
53 NULL
, /* gstate_freelist */
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 */
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
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.
83 _cairo_error (cairo_status_t status
)
86 assert (_cairo_status_is_error (status
));
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.
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
));
116 /* We keep a small stash of contexts to reduce malloc pressure */
117 #define CAIRO_STASH_SIZE 4
119 cairo_t pool
[CAIRO_STASH_SIZE
];
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
];
141 _context_put (cairo_t
*cr
)
145 if (cr
< &_context_stash
.pool
[0] ||
146 cr
>= &_context_stash
.pool
[CAIRO_STASH_SIZE
])
152 avail
= ~(1 << (cr
- &_context_stash
.pool
[0]));
154 old
= _context_stash
.occupied
;
156 } while (_cairo_atomic_int_cmpxchg (&_context_stash
.occupied
, old
, new) != old
);
159 #define _context_get() malloc (sizeof (cairo_t))
160 #define _context_put(cr) free (cr)
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
187 cairo_create (cairo_surface_t
*target
)
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
);
219 slim_hidden_def (cairo_create
);
225 * Increases the reference count on @cr by one. This prevents
226 * @cr from being destroyed until a matching call to cairo_destroy()
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.
235 cairo_reference (cairo_t
*cr
)
237 if (cr
== NULL
|| CAIRO_REFERENCE_COUNT_IS_INVALID (&cr
->ref_count
))
240 assert (CAIRO_REFERENCE_COUNT_HAS_REFERENCE (&cr
->ref_count
));
242 _cairo_reference_count_inc (&cr
->ref_count
);
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().
256 cairo_destroy (cairo_t
*cr
)
258 cairo_surface_t
*surface
;
260 if (cr
== NULL
|| CAIRO_REFERENCE_COUNT_IS_INVALID (&cr
->ref_count
))
263 assert (CAIRO_REFERENCE_COUNT_HAS_REFERENCE (&cr
->ref_count
));
265 if (! _cairo_reference_count_dec_and_test (&cr
->ref_count
))
268 while (cr
->gstate
!= &cr
->gstate_tail
[0]) {
269 if (_cairo_gstate_restore (&cr
->gstate
, &cr
->gstate_freelist
))
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
);
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
;
290 _cairo_path_fixed_fini (cr
->path
);
292 _cairo_user_data_array_fini (&cr
->user_data
);
296 slim_hidden_def (cairo_destroy
);
299 * cairo_get_user_data:
301 * @key: the address of the #cairo_user_data_key_t the user data was
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.
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
,
321 * cairo_set_user_data:
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
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
333 * Return value: %CAIRO_STATUS_SUCCESS or %CAIRO_STATUS_NO_MEMORY if a
334 * slot could not be allocated for the user data.
339 cairo_set_user_data (cairo_t
*cr
,
340 const cairo_user_data_key_t
*key
,
342 cairo_destroy_func_t destroy
)
344 if (CAIRO_REFERENCE_COUNT_IS_INVALID (&cr
->ref_count
))
347 return _cairo_user_data_array_set_data (&cr
->user_data
,
348 key
, user_data
, destroy
);
352 * cairo_get_reference_count:
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.
363 cairo_get_reference_count (cairo_t
*cr
)
365 if (cr
== NULL
|| CAIRO_REFERENCE_COUNT_IS_INVALID (&cr
->ref_count
))
368 return CAIRO_REFERENCE_COUNT_GET_VALUE (&cr
->ref_count
);
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.
388 cairo_save (cairo_t
*cr
)
390 cairo_status_t status
;
392 if (unlikely (cr
->status
))
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
);
405 * Restores @cr to the state saved by a preceding call to
406 * cairo_save() and removes that state from the stack of
410 cairo_restore (cairo_t
*cr
)
412 cairo_status_t status
;
414 if (unlikely (cr
->status
))
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
);
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
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
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);
461 * cairo_pop_group_to_source (cr);
462 * cairo_paint_with_alpha (cr, alpha);
463 * </programlisting></informalexample>
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
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.
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
))
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
))
507 status
= _cairo_clip_intersect_to_rectangle (_cairo_gstate_get_clip (cr
->gstate
), &extents
);
508 if (unlikely (status
))
511 group_surface
= cairo_surface_create_similar (parent_surface
,
515 status
= cairo_surface_status (group_surface
);
516 if (unlikely (status
))
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 */
535 if (unlikely (cr
->status
))
538 status
= _cairo_gstate_redirect_target (cr
->gstate
, group_surface
);
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
);
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
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
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.
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
);
596 if (unlikely (cr
->status
)) {
597 group_pattern
= _cairo_pattern_create_in_error (cr
->status
);
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
);
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
);
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
);
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
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
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:
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.
681 cairo_set_operator (cairo_t
*cr
, cairo_operator_t op
)
683 cairo_status_t status
;
685 if (unlikely (cr
->status
))
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
);
696 _current_source_matches_solid (cairo_t
*cr
,
702 const cairo_pattern_t
*current
;
705 current
= cr
->gstate
->source
;
706 if (current
->type
!= CAIRO_PATTERN_TYPE_SOLID
)
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
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)).
737 cairo_set_source_rgb (cairo_t
*cr
, double red
, double green
, double blue
)
739 cairo_pattern_t
*pattern
;
741 if (unlikely (cr
->status
))
744 if (_current_source_matches_solid (cr
, red
, green
, blue
, 1.))
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
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)).
776 cairo_set_source_rgba (cairo_t
*cr
,
777 double red
, double green
, double blue
,
780 cairo_pattern_t
*pattern
;
782 if (unlikely (cr
->status
))
785 if (_current_source_matches_solid (cr
, red
, green
, blue
, alpha
))
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()).
820 cairo_set_source_surface (cairo_t
*cr
,
821 cairo_surface_t
*surface
,
825 cairo_pattern_t
*pattern
;
826 cairo_matrix_t matrix
;
828 if (unlikely (cr
->status
))
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
);
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,
864 cairo_set_source (cairo_t
*cr
, cairo_pattern_t
*source
)
866 cairo_status_t status
;
868 if (unlikely (cr
->status
))
871 if (source
== NULL
) {
872 _cairo_set_error (cr
, CAIRO_STATUS_NULL_POINTER
);
876 if (source
->status
) {
877 _cairo_set_error (cr
, source
->status
);
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
);
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().
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:
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.
923 cairo_set_tolerance (cairo_t
*cr
, double tolerance
)
925 cairo_status_t status
;
927 if (unlikely (cr
->status
))
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:
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().
953 cairo_set_antialias (cairo_t
*cr
, cairo_antialias_t antialias
)
955 cairo_status_t status
;
957 if (unlikely (cr
->status
))
960 status
= _cairo_gstate_set_antialias (cr
->gstate
, antialias
);
961 if (unlikely (status
))
962 _cairo_set_error (cr
, status
);
966 * cairo_set_fill_rule:
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.
979 cairo_set_fill_rule (cairo_t
*cr
, cairo_fill_rule_t fill_rule
)
981 cairo_status_t status
;
983 if (unlikely (cr
->status
))
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:
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
1015 * The default line width value is 2.0.
1018 cairo_set_line_width (cairo_t
*cr
, double width
)
1020 cairo_status_t status
;
1022 if (unlikely (cr
->status
))
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
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
1048 * The default line cap style is %CAIRO_LINE_CAP_BUTT.
1051 cairo_set_line_cap (cairo_t
*cr
, cairo_line_cap_t line_cap
)
1053 cairo_status_t status
;
1055 if (unlikely (cr
->status
))
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
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
1078 * The default line join style is %CAIRO_LINE_JOIN_MITER.
1081 cairo_set_line_join (cairo_t
*cr
, cairo_line_join_t line_join
)
1083 cairo_status_t status
;
1085 if (unlikely (cr
->status
))
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
);
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
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.
1127 cairo_set_dash (cairo_t
*cr
,
1128 const double *dashes
,
1132 cairo_status_t status
;
1134 if (unlikely (cr
->status
))
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:
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.
1157 cairo_get_dash_count (cairo_t
*cr
)
1161 if (unlikely (cr
->status
))
1164 _cairo_gstate_get_dash (cr
->gstate
, NULL
, &num_dashes
, NULL
);
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().
1182 cairo_get_dash (cairo_t
*cr
,
1186 if (unlikely (cr
->status
))
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
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
1217 * A miter limit for a desired angle can be computed as: miter limit =
1221 cairo_set_miter_limit (cairo_t
*cr
, double limit
)
1223 cairo_status_t status
;
1225 if (unlikely (cr
->status
))
1228 status
= _cairo_gstate_set_miter_limit (cr
->gstate
, limit
);
1229 if (unlikely (status
))
1230 _cairo_set_error (cr
, status
);
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.
1246 cairo_translate (cairo_t
*cr
, double tx
, double ty
)
1248 cairo_status_t status
;
1250 if (unlikely (cr
->status
))
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
);
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
1271 cairo_scale (cairo_t
*cr
, double sx
, double sy
)
1273 cairo_status_t status
;
1275 if (unlikely (cr
->status
))
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
);
1286 * @cr: a cairo context
1287 * @angle: angle (in radians) by which the user-space axes will be
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.
1297 cairo_rotate (cairo_t
*cr
, double angle
)
1299 cairo_status_t status
;
1301 if (unlikely (cr
->status
))
1304 status
= _cairo_gstate_rotate (cr
->gstate
, angle
);
1305 if (unlikely (status
))
1306 _cairo_set_error (cr
, status
);
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.
1319 cairo_transform (cairo_t
*cr
,
1320 const cairo_matrix_t
*matrix
)
1322 cairo_status_t status
;
1324 if (unlikely (cr
->status
))
1327 status
= _cairo_gstate_transform (cr
->gstate
, matrix
);
1328 if (unlikely (status
))
1329 _cairo_set_error (cr
, status
);
1331 slim_hidden_def (cairo_transform
);
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
1342 cairo_set_matrix (cairo_t
*cr
,
1343 const cairo_matrix_t
*matrix
)
1345 cairo_status_t status
;
1347 if (unlikely (cr
->status
))
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.
1366 cairo_identity_matrix (cairo_t
*cr
)
1368 if (unlikely (cr
->status
))
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
1385 cairo_user_to_device (cairo_t
*cr
, double *x
, double *y
)
1387 if (unlikely (cr
->status
))
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
1406 cairo_user_to_device_distance (cairo_t
*cr
, double *dx
, double *dy
)
1408 if (unlikely (cr
->status
))
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:
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).
1426 cairo_device_to_user (cairo_t
*cr
, double *x
, double *y
)
1428 if (unlikely (cr
->status
))
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).
1446 cairo_device_to_user_distance (cairo_t
*cr
, double *dx
, double *dy
)
1448 if (unlikely (cr
->status
))
1451 _cairo_gstate_device_to_user_distance (cr
->gstate
, dx
, dy
);
1456 * @cr: a cairo context
1458 * Clears the current path. After this call there will be no path and
1462 cairo_new_path (cairo_t
*cr
)
1464 if (unlikely (cr
->status
))
1467 _cairo_path_fixed_fini (cr
->path
);
1468 _cairo_path_fixed_init (cr
->path
);
1470 slim_hidden_def(cairo_new_path
);
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,
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
))
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
1519 cairo_new_sub_path (cairo_t
*cr
)
1521 if (unlikely (cr
->status
))
1524 _cairo_path_fixed_new_sub_path (cr
->path
);
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
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).
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
))
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
);
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).
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
))
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
,
1608 x3_fixed
, y3_fixed
);
1609 if (unlikely (status
))
1610 _cairo_set_error (cr
, status
);
1612 slim_hidden_def (cairo_curve_to
);
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 /
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>
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>
1662 cairo_arc (cairo_t
*cr
,
1663 double xc
, double yc
,
1665 double angle1
, double angle2
)
1667 if (unlikely (cr
->status
))
1670 /* Do nothing, successfully, if radius is <= 0 */
1674 while (angle2
< angle1
)
1678 xc
+ radius
* cos (angle1
),
1679 yc
+ radius
* sin (angle1
));
1681 _cairo_arc_path (cr
, xc
, yc
, radius
,
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.
1704 cairo_arc_negative (cairo_t
*cr
,
1705 double xc
, double yc
,
1707 double angle1
, double angle2
)
1709 if (unlikely (cr
->status
))
1712 /* Do nothing, successfully, if radius is <= 0 */
1716 while (angle2
> angle1
)
1720 xc
+ radius
* cos (angle1
),
1721 yc
+ radius
* sin (angle1
));
1723 _cairo_arc_path_negative (cr
, xc
, yc
, radius
,
1729 cairo_arc_to (cairo_t *cr,
1730 double x1, double y1,
1731 double x2, double y2,
1734 cairo_status_t status;
1736 if (unlikely (cr->status))
1739 status = _cairo_gstate_arc_to (cr->gstate,
1743 if (unlikely (status))
1744 _cairo_set_error (cr, status);
1749 * cairo_rel_move_to:
1750 * @cr: a cairo context
1754 * Begin a new sub-path. After this call the current point will offset
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.
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
))
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.
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
))
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.
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
))
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
);
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>
1902 cairo_rectangle (cairo_t
*cr
,
1904 double width
, double height
)
1906 if (unlikely (cr
->status
))
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
);
1919 cairo_stroke_to_path (cairo_t
*cr
)
1921 cairo_status_t status
;
1923 if (unlikely (cr
->status
))
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
);
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
1961 cairo_close_path (cairo_t
*cr
)
1963 cairo_status_t status
;
1965 if (unlikely (cr
->status
))
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().
2006 cairo_path_extents (cairo_t
*cr
,
2007 double *x1
, double *y1
, double *x2
, double *y2
)
2009 if (unlikely (cr
->status
)) {
2022 _cairo_gstate_path_extents (cr
->gstate
,
2029 * @cr: a cairo context
2031 * A drawing operator that paints the current source everywhere within
2032 * the current clip region.
2035 cairo_paint (cairo_t
*cr
)
2037 cairo_status_t status
;
2039 if (unlikely (cr
->status
))
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.
2059 cairo_paint_with_alpha (cairo_t
*cr
,
2062 cairo_status_t status
;
2063 cairo_color_t color
;
2064 cairo_solid_pattern_t pattern
;
2066 if (unlikely (cr
->status
))
2069 if (CAIRO_ALPHA_IS_OPAQUE (alpha
)) {
2074 if (CAIRO_ALPHA_IS_ZERO (alpha
)) {
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
);
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.)
2099 cairo_mask (cairo_t
*cr
,
2100 cairo_pattern_t
*pattern
)
2102 cairo_status_t status
;
2104 if (unlikely (cr
->status
))
2107 if (pattern
== NULL
) {
2108 _cairo_set_error (cr
, CAIRO_STATUS_NULL_POINTER
);
2112 if (pattern
->status
) {
2113 _cairo_set_error (cr
, pattern
->status
);
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.)
2136 cairo_mask_surface (cairo_t
*cr
,
2137 cairo_surface_t
*surface
,
2141 cairo_pattern_t
*pattern
;
2142 cairo_matrix_t matrix
;
2144 if (unlikely (cr
->status
))
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
);
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
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.
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
2207 * See cairo_set_line_width(), cairo_set_line_join(),
2208 * cairo_set_line_cap(), cairo_set_dash(), and
2209 * cairo_stroke_preserve().
2212 cairo_stroke_preserve (cairo_t
*cr
)
2214 cairo_status_t status
;
2216 if (unlikely (cr
->status
))
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
);
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().
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().
2255 cairo_fill_preserve (cairo_t
*cr
)
2257 cairo_status_t status
;
2259 if (unlikely (cr
->status
))
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
);
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.
2281 cairo_copy_page (cairo_t
*cr
)
2283 cairo_status_t status
;
2285 if (unlikely (cr
->status
))
2288 status
= _cairo_gstate_copy_page (cr
->gstate
);
2289 if (unlikely (status
))
2290 _cairo_set_error (cr
, status
);
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.
2304 cairo_show_page (cairo_t
*cr
)
2306 cairo_status_t status
;
2308 if (unlikely (cr
->status
))
2311 status
= _cairo_gstate_show_page (cr
->gstate
);
2312 if (unlikely (status
))
2313 _cairo_set_error (cr
, status
);
2318 * @cr: a cairo context
2319 * @x: X coordinate of the point to test
2320 * @y: Y coordinate of the point to test
2322 * Tests whether the given point is inside the area that would be
2323 * affected by a cairo_stroke() operation given the current path and
2324 * stroking parameters. Surface dimensions and clipping are not taken
2327 * See cairo_stroke(), cairo_set_line_width(), cairo_set_line_join(),
2328 * cairo_set_line_cap(), cairo_set_dash(), and
2329 * cairo_stroke_preserve().
2331 * Return value: A non-zero value if the point is inside, or zero if
2335 cairo_in_stroke (cairo_t
*cr
, double x
, double y
)
2337 cairo_status_t status
;
2338 cairo_bool_t inside
= FALSE
;
2340 if (unlikely (cr
->status
))
2343 status
= _cairo_gstate_in_stroke (cr
->gstate
,
2346 if (unlikely (status
))
2347 _cairo_set_error (cr
, status
);
2354 * @cr: a cairo context
2355 * @x: X coordinate of the point to test
2356 * @y: Y coordinate of the point to test
2358 * Tests whether the given point is inside the area that would be
2359 * affected by a cairo_fill() operation given the current path and
2360 * filling parameters. Surface dimensions and clipping are not taken
2363 * See cairo_fill(), cairo_set_fill_rule() and cairo_fill_preserve().
2365 * Return value: A non-zero value if the point is inside, or zero if
2369 cairo_in_fill (cairo_t
*cr
, double x
, double y
)
2371 cairo_bool_t inside
;
2373 if (unlikely (cr
->status
))
2376 _cairo_gstate_in_fill (cr
->gstate
,
2384 * cairo_stroke_extents:
2385 * @cr: a cairo context
2386 * @x1: left of the resulting extents
2387 * @y1: top of the resulting extents
2388 * @x2: right of the resulting extents
2389 * @y2: bottom of the resulting extents
2391 * Computes a bounding box in user coordinates covering the area that
2392 * would be affected, (the "inked" area), by a cairo_stroke()
2393 * operation given the current path and stroke parameters.
2394 * If the current path is empty, returns an empty rectangle ((0,0), (0,0)).
2395 * Surface dimensions and clipping are not taken into account.
2397 * Note that if the line width is set to exactly zero, then
2398 * cairo_stroke_extents() will return an empty rectangle. Contrast with
2399 * cairo_path_extents() which can be used to compute the non-empty
2400 * bounds as the line width approaches zero.
2402 * Note that cairo_stroke_extents() must necessarily do more work to
2403 * compute the precise inked areas in light of the stroke parameters,
2404 * so cairo_path_extents() may be more desirable for sake of
2405 * performance if non-inked path extents are desired.
2407 * See cairo_stroke(), cairo_set_line_width(), cairo_set_line_join(),
2408 * cairo_set_line_cap(), cairo_set_dash(), and
2409 * cairo_stroke_preserve().
2412 cairo_stroke_extents (cairo_t
*cr
,
2413 double *x1
, double *y1
, double *x2
, double *y2
)
2415 cairo_status_t status
;
2417 if (unlikely (cr
->status
)) {
2430 status
= _cairo_gstate_stroke_extents (cr
->gstate
,
2433 if (unlikely (status
))
2434 _cairo_set_error (cr
, status
);
2438 * cairo_fill_extents:
2439 * @cr: a cairo context
2440 * @x1: left of the resulting extents
2441 * @y1: top of the resulting extents
2442 * @x2: right of the resulting extents
2443 * @y2: bottom of the resulting extents
2445 * Computes a bounding box in user coordinates covering the area that
2446 * would be affected, (the "inked" area), by a cairo_fill() operation
2447 * given the current path and fill parameters. If the current path is
2448 * empty, returns an empty rectangle ((0,0), (0,0)). Surface
2449 * dimensions and clipping are not taken into account.
2451 * Contrast with cairo_path_extents(), which is similar, but returns
2452 * non-zero extents for some paths with no inked area, (such as a
2453 * simple line segment).
2455 * Note that cairo_fill_extents() must necessarily do more work to
2456 * compute the precise inked areas in light of the fill rule, so
2457 * cairo_path_extents() may be more desirable for sake of performance
2458 * if the non-inked path extents are desired.
2460 * See cairo_fill(), cairo_set_fill_rule() and cairo_fill_preserve().
2463 cairo_fill_extents (cairo_t
*cr
,
2464 double *x1
, double *y1
, double *x2
, double *y2
)
2466 cairo_status_t status
;
2468 if (unlikely (cr
->status
)) {
2481 status
= _cairo_gstate_fill_extents (cr
->gstate
,
2484 if (unlikely (status
))
2485 _cairo_set_error (cr
, status
);
2490 * @cr: a cairo context
2492 * Establishes a new clip region by intersecting the current clip
2493 * region with the current path as it would be filled by cairo_fill()
2494 * and according to the current fill rule (see cairo_set_fill_rule()).
2496 * After cairo_clip(), the current path will be cleared from the cairo
2499 * The current clip region affects all drawing operations by
2500 * effectively masking out any changes to the surface that are outside
2501 * the current clip region.
2503 * Calling cairo_clip() can only make the clip region smaller, never
2504 * larger. But the current clip is part of the graphics state, so a
2505 * temporary restriction of the clip region can be achieved by
2506 * calling cairo_clip() within a cairo_save()/cairo_restore()
2507 * pair. The only other means of increasing the size of the clip
2508 * region is cairo_reset_clip().
2511 cairo_clip (cairo_t
*cr
)
2513 cairo_clip_preserve (cr
);
2515 cairo_new_path (cr
);
2519 * cairo_clip_preserve:
2520 * @cr: a cairo context
2522 * Establishes a new clip region by intersecting the current clip
2523 * region with the current path as it would be filled by cairo_fill()
2524 * and according to the current fill rule (see cairo_set_fill_rule()).
2526 * Unlike cairo_clip(), cairo_clip_preserve() preserves the path within
2527 * the cairo context.
2529 * The current clip region affects all drawing operations by
2530 * effectively masking out any changes to the surface that are outside
2531 * the current clip region.
2533 * Calling cairo_clip_preserve() can only make the clip region smaller, never
2534 * larger. But the current clip is part of the graphics state, so a
2535 * temporary restriction of the clip region can be achieved by
2536 * calling cairo_clip_preserve() within a cairo_save()/cairo_restore()
2537 * pair. The only other means of increasing the size of the clip
2538 * region is cairo_reset_clip().
2541 cairo_clip_preserve (cairo_t
*cr
)
2543 cairo_status_t status
;
2545 if (unlikely (cr
->status
))
2548 status
= _cairo_gstate_clip (cr
->gstate
, cr
->path
);
2549 if (unlikely (status
))
2550 _cairo_set_error (cr
, status
);
2552 slim_hidden_def(cairo_clip_preserve
);
2556 * @cr: a cairo context
2558 * Reset the current clip region to its original, unrestricted
2559 * state. That is, set the clip region to an infinitely large shape
2560 * containing the target surface. Equivalently, if infinity is too
2561 * hard to grasp, one can imagine the clip region being reset to the
2562 * exact bounds of the target surface.
2564 * Note that code meant to be reusable should not call
2565 * cairo_reset_clip() as it will cause results unexpected by
2566 * higher-level code which calls cairo_clip(). Consider using
2567 * cairo_save() and cairo_restore() around cairo_clip() as a more
2568 * robust means of temporarily restricting the clip region.
2571 cairo_reset_clip (cairo_t
*cr
)
2573 cairo_status_t status
;
2575 if (unlikely (cr
->status
))
2578 status
= _cairo_gstate_reset_clip (cr
->gstate
);
2579 if (unlikely (status
))
2580 _cairo_set_error (cr
, status
);
2584 * cairo_clip_extents:
2585 * @cr: a cairo context
2586 * @x1: left of the resulting extents
2587 * @y1: top of the resulting extents
2588 * @x2: right of the resulting extents
2589 * @y2: bottom of the resulting extents
2591 * Computes a bounding box in user coordinates covering the area inside the
2597 cairo_clip_extents (cairo_t
*cr
,
2598 double *x1
, double *y1
,
2599 double *x2
, double *y2
)
2601 cairo_status_t status
;
2603 if (unlikely (cr
->status
)) {
2616 status
= _cairo_gstate_clip_extents (cr
->gstate
, x1
, y1
, x2
, y2
);
2617 if (unlikely (status
))
2618 _cairo_set_error (cr
, status
);
2621 static cairo_rectangle_list_t
*
2622 _cairo_rectangle_list_create_in_error (cairo_status_t status
)
2624 cairo_rectangle_list_t
*list
;
2626 if (status
== CAIRO_STATUS_NO_MEMORY
)
2627 return (cairo_rectangle_list_t
*) &_cairo_rectangles_nil
;
2629 list
= malloc (sizeof (cairo_rectangle_list_t
));
2630 if (unlikely (list
== NULL
)) {
2631 status
= _cairo_error (CAIRO_STATUS_NO_MEMORY
);
2632 return (cairo_rectangle_list_t
*) &_cairo_rectangles_nil
;
2635 list
->status
= status
;
2636 list
->rectangles
= NULL
;
2637 list
->num_rectangles
= 0;
2642 * cairo_copy_clip_rectangle_list:
2643 * @cr: a cairo context
2645 * Gets the current clip region as a list of rectangles in user coordinates.
2646 * Never returns %NULL.
2648 * The status in the list may be %CAIRO_STATUS_CLIP_NOT_REPRESENTABLE to
2649 * indicate that the clip region cannot be represented as a list of
2650 * user-space rectangles. The status may have other values to indicate
2653 * Returns: the current clip region as a list of rectangles in user coordinates,
2654 * which should be destroyed using cairo_rectangle_list_destroy().
2658 cairo_rectangle_list_t
*
2659 cairo_copy_clip_rectangle_list (cairo_t
*cr
)
2661 if (unlikely (cr
->status
))
2662 return _cairo_rectangle_list_create_in_error (cr
->status
);
2664 return _cairo_gstate_copy_clip_rectangle_list (cr
->gstate
);
2668 * cairo_select_font_face:
2670 * @family: a font family name, encoded in UTF-8
2671 * @slant: the slant for the font
2672 * @weight: the weight for the font
2674 * Note: The cairo_select_font_face() function call is part of what
2675 * the cairo designers call the "toy" text API. It is convenient for
2676 * short demos and simple programs, but it is not expected to be
2677 * adequate for serious text-using applications.
2679 * Selects a family and style of font from a simplified description as
2680 * a family name, slant and weight. Cairo provides no operation to
2681 * list available family names on the system (this is a "toy",
2682 * remember), but the standard CSS2 generic family names, ("serif",
2683 * "sans-serif", "cursive", "fantasy", "monospace"), are likely to
2686 * If @family starts with the string "@cairo:", or if no native font
2687 * backends are compiled in, cairo will use an internal font family.
2688 * The internal font family recognizes many modifiers in the @family
2689 * string, most notably, it recognizes the string "monospace". That is,
2690 * the family name "@cairo:monospace" will use the monospace version of
2691 * the internal font family.
2693 * For "real" font selection, see the font-backend-specific
2694 * font_face_create functions for the font backend you are using. (For
2695 * example, if you are using the freetype-based cairo-ft font backend,
2696 * see cairo_ft_font_face_create_for_ft_face() or
2697 * cairo_ft_font_face_create_for_pattern().) The resulting font face
2698 * could then be used with cairo_scaled_font_create() and
2699 * cairo_set_scaled_font().
2701 * Similarly, when using the "real" font support, you can call
2702 * directly into the underlying font system, (such as fontconfig or
2703 * freetype), for operations such as listing available fonts, etc.
2705 * It is expected that most applications will need to use a more
2706 * comprehensive font handling and text layout library, (for example,
2707 * pango), in conjunction with cairo.
2709 * If text is drawn without a call to cairo_select_font_face(), (nor
2710 * cairo_set_font_face() nor cairo_set_scaled_font()), the default
2711 * family is platform-specific, but is essentially "sans-serif".
2712 * Default slant is %CAIRO_FONT_SLANT_NORMAL, and default weight is
2713 * %CAIRO_FONT_WEIGHT_NORMAL.
2715 * This function is equivalent to a call to cairo_toy_font_face_create()
2716 * followed by cairo_set_font_face().
2719 cairo_select_font_face (cairo_t
*cr
,
2721 cairo_font_slant_t slant
,
2722 cairo_font_weight_t weight
)
2724 cairo_status_t status
;
2726 if (unlikely (cr
->status
))
2729 status
= _cairo_gstate_select_font_face (cr
->gstate
, family
, slant
, weight
);
2730 if (unlikely (status
))
2731 _cairo_set_error (cr
, status
);
2735 * cairo_font_extents:
2737 * @extents: a #cairo_font_extents_t object into which the results
2740 * Gets the font extents for the currently selected font.
2743 cairo_font_extents (cairo_t
*cr
,
2744 cairo_font_extents_t
*extents
)
2746 cairo_status_t status
;
2748 extents
->ascent
= 0.0;
2749 extents
->descent
= 0.0;
2750 extents
->height
= 0.0;
2751 extents
->max_x_advance
= 0.0;
2752 extents
->max_y_advance
= 0.0;
2754 if (unlikely (cr
->status
))
2757 status
= _cairo_gstate_get_font_extents (cr
->gstate
, extents
);
2758 if (unlikely (status
))
2759 _cairo_set_error (cr
, status
);
2763 * cairo_set_font_face:
2765 * @font_face: a #cairo_font_face_t, or %NULL to restore to the default font
2767 * Replaces the current #cairo_font_face_t object in the #cairo_t with
2768 * @font_face. The replaced font face in the #cairo_t will be
2769 * destroyed if there are no other references to it.
2772 cairo_set_font_face (cairo_t
*cr
,
2773 cairo_font_face_t
*font_face
)
2775 cairo_status_t status
;
2777 if (unlikely (cr
->status
))
2780 status
= _cairo_gstate_set_font_face (cr
->gstate
, font_face
);
2781 if (unlikely (status
))
2782 _cairo_set_error (cr
, status
);
2786 * cairo_get_font_face:
2789 * Gets the current font face for a #cairo_t.
2791 * Return value: the current font face. This object is owned by
2792 * cairo. To keep a reference to it, you must call
2793 * cairo_font_face_reference().
2795 * This function never returns %NULL. If memory cannot be allocated, a
2796 * special "nil" #cairo_font_face_t object will be returned on which
2797 * cairo_font_face_status() returns %CAIRO_STATUS_NO_MEMORY. Using
2798 * this nil object will cause its error state to propagate to other
2799 * objects it is passed to, (for example, calling
2800 * cairo_set_font_face() with a nil font will trigger an error that
2801 * will shutdown the #cairo_t object).
2804 cairo_get_font_face (cairo_t
*cr
)
2806 cairo_status_t status
;
2807 cairo_font_face_t
*font_face
;
2809 if (unlikely (cr
->status
))
2810 return (cairo_font_face_t
*) &_cairo_font_face_nil
;
2812 status
= _cairo_gstate_get_font_face (cr
->gstate
, &font_face
);
2813 if (unlikely (status
)) {
2814 _cairo_set_error (cr
, status
);
2815 return (cairo_font_face_t
*) &_cairo_font_face_nil
;
2822 * cairo_set_font_size:
2824 * @size: the new font size, in user space units
2826 * Sets the current font matrix to a scale by a factor of @size, replacing
2827 * any font matrix previously set with cairo_set_font_size() or
2828 * cairo_set_font_matrix(). This results in a font size of @size user space
2829 * units. (More precisely, this matrix will result in the font's
2830 * em-square being a @size by @size square in user space.)
2832 * If text is drawn without a call to cairo_set_font_size(), (nor
2833 * cairo_set_font_matrix() nor cairo_set_scaled_font()), the default
2834 * font size is 10.0.
2837 cairo_set_font_size (cairo_t
*cr
, double size
)
2839 cairo_status_t status
;
2841 if (unlikely (cr
->status
))
2844 status
= _cairo_gstate_set_font_size (cr
->gstate
, size
);
2845 if (unlikely (status
))
2846 _cairo_set_error (cr
, status
);
2848 slim_hidden_def (cairo_set_font_size
);
2851 * cairo_set_font_matrix
2853 * @matrix: a #cairo_matrix_t describing a transform to be applied to
2856 * Sets the current font matrix to @matrix. The font matrix gives a
2857 * transformation from the design space of the font (in this space,
2858 * the em-square is 1 unit by 1 unit) to user space. Normally, a
2859 * simple scale is used (see cairo_set_font_size()), but a more
2860 * complex font matrix can be used to shear the font
2861 * or stretch it unequally along the two axes
2864 cairo_set_font_matrix (cairo_t
*cr
,
2865 const cairo_matrix_t
*matrix
)
2867 cairo_status_t status
;
2869 if (unlikely (cr
->status
))
2872 status
= _cairo_gstate_set_font_matrix (cr
->gstate
, matrix
);
2873 if (unlikely (status
))
2874 _cairo_set_error (cr
, status
);
2878 * cairo_get_font_matrix
2880 * @matrix: return value for the matrix
2882 * Stores the current font matrix into @matrix. See
2883 * cairo_set_font_matrix().
2886 cairo_get_font_matrix (cairo_t
*cr
, cairo_matrix_t
*matrix
)
2888 if (unlikely (cr
->status
)) {
2889 cairo_matrix_init_identity (matrix
);
2893 _cairo_gstate_get_font_matrix (cr
->gstate
, matrix
);
2897 * cairo_set_font_options:
2899 * @options: font options to use
2901 * Sets a set of custom font rendering options for the #cairo_t.
2902 * Rendering options are derived by merging these options with the
2903 * options derived from underlying surface; if the value in @options
2904 * has a default value (like %CAIRO_ANTIALIAS_DEFAULT), then the value
2905 * from the surface is used.
2908 cairo_set_font_options (cairo_t
*cr
,
2909 const cairo_font_options_t
*options
)
2911 cairo_status_t status
;
2913 if (unlikely (cr
->status
))
2916 status
= cairo_font_options_status ((cairo_font_options_t
*) options
);
2917 if (unlikely (status
)) {
2918 _cairo_set_error (cr
, status
);
2922 _cairo_gstate_set_font_options (cr
->gstate
, options
);
2924 slim_hidden_def (cairo_set_font_options
);
2927 * cairo_get_font_options:
2929 * @options: a #cairo_font_options_t object into which to store
2930 * the retrieved options. All existing values are overwritten
2932 * Retrieves font rendering options set via #cairo_set_font_options.
2933 * Note that the returned options do not include any options derived
2934 * from the underlying surface; they are literally the options
2935 * passed to cairo_set_font_options().
2938 cairo_get_font_options (cairo_t
*cr
,
2939 cairo_font_options_t
*options
)
2941 /* check that we aren't trying to overwrite the nil object */
2942 if (cairo_font_options_status (options
))
2945 if (unlikely (cr
->status
)) {
2946 _cairo_font_options_init_default (options
);
2950 _cairo_gstate_get_font_options (cr
->gstate
, options
);
2954 * cairo_set_scaled_font:
2956 * @scaled_font: a #cairo_scaled_font_t
2958 * Replaces the current font face, font matrix, and font options in
2959 * the #cairo_t with those of the #cairo_scaled_font_t. Except for
2960 * some translation, the current CTM of the #cairo_t should be the
2961 * same as that of the #cairo_scaled_font_t, which can be accessed
2962 * using cairo_scaled_font_get_ctm().
2967 cairo_set_scaled_font (cairo_t
*cr
,
2968 const cairo_scaled_font_t
*scaled_font
)
2970 cairo_status_t status
;
2971 cairo_bool_t was_previous
;
2973 if (unlikely (cr
->status
))
2976 if (scaled_font
== NULL
) {
2977 status
= _cairo_error (CAIRO_STATUS_NULL_POINTER
);
2981 status
= scaled_font
->status
;
2982 if (unlikely (status
))
2985 if (scaled_font
== cr
->gstate
->scaled_font
)
2988 was_previous
= scaled_font
== cr
->gstate
->previous_scaled_font
;
2990 status
= _cairo_gstate_set_font_face (cr
->gstate
, scaled_font
->font_face
);
2991 if (unlikely (status
))
2994 status
= _cairo_gstate_set_font_matrix (cr
->gstate
, &scaled_font
->font_matrix
);
2995 if (unlikely (status
))
2998 _cairo_gstate_set_font_options (cr
->gstate
, &scaled_font
->options
);
3001 cr
->gstate
->scaled_font
= cairo_scaled_font_reference ((cairo_scaled_font_t
*) scaled_font
);
3006 _cairo_set_error (cr
, status
);
3010 * cairo_get_scaled_font:
3013 * Gets the current scaled font for a #cairo_t.
3015 * Return value: the current scaled font. This object is owned by
3016 * cairo. To keep a reference to it, you must call
3017 * cairo_scaled_font_reference().
3019 * This function never returns %NULL. If memory cannot be allocated, a
3020 * special "nil" #cairo_scaled_font_t object will be returned on which
3021 * cairo_scaled_font_status() returns %CAIRO_STATUS_NO_MEMORY. Using
3022 * this nil object will cause its error state to propagate to other
3023 * objects it is passed to, (for example, calling
3024 * cairo_set_scaled_font() with a nil font will trigger an error that
3025 * will shutdown the #cairo_t object).
3029 cairo_scaled_font_t
*
3030 cairo_get_scaled_font (cairo_t
*cr
)
3032 cairo_status_t status
;
3033 cairo_scaled_font_t
*scaled_font
;
3035 if (unlikely (cr
->status
))
3036 return _cairo_scaled_font_create_in_error (cr
->status
);
3038 status
= _cairo_gstate_get_scaled_font (cr
->gstate
, &scaled_font
);
3039 if (unlikely (status
)) {
3040 _cairo_set_error (cr
, status
);
3041 return _cairo_scaled_font_create_in_error (status
);
3048 * cairo_text_extents:
3050 * @utf8: a NUL-terminated string of text encoded in UTF-8, or %NULL
3051 * @extents: a #cairo_text_extents_t object into which the results
3054 * Gets the extents for a string of text. The extents describe a
3055 * user-space rectangle that encloses the "inked" portion of the text,
3056 * (as it would be drawn by cairo_show_text()). Additionally, the
3057 * x_advance and y_advance values indicate the amount by which the
3058 * current point would be advanced by cairo_show_text().
3060 * Note that whitespace characters do not directly contribute to the
3061 * size of the rectangle (extents.width and extents.height). They do
3062 * contribute indirectly by changing the position of non-whitespace
3063 * characters. In particular, trailing whitespace characters are
3064 * likely to not affect the size of the rectangle, though they will
3065 * affect the x_advance and y_advance values.
3068 cairo_text_extents (cairo_t
*cr
,
3070 cairo_text_extents_t
*extents
)
3072 cairo_status_t status
;
3073 cairo_glyph_t
*glyphs
= NULL
;
3077 extents
->x_bearing
= 0.0;
3078 extents
->y_bearing
= 0.0;
3079 extents
->width
= 0.0;
3080 extents
->height
= 0.0;
3081 extents
->x_advance
= 0.0;
3082 extents
->y_advance
= 0.0;
3084 if (unlikely (cr
->status
))
3090 cairo_get_current_point (cr
, &x
, &y
);
3092 status
= _cairo_gstate_text_to_glyphs (cr
->gstate
,
3094 utf8
, strlen (utf8
),
3095 &glyphs
, &num_glyphs
,
3099 if (status
== CAIRO_STATUS_SUCCESS
)
3100 status
= _cairo_gstate_glyph_extents (cr
->gstate
,
3103 cairo_glyph_free (glyphs
);
3105 if (unlikely (status
))
3106 _cairo_set_error (cr
, status
);
3110 * cairo_glyph_extents:
3112 * @glyphs: an array of #cairo_glyph_t objects
3113 * @num_glyphs: the number of elements in @glyphs
3114 * @extents: a #cairo_text_extents_t object into which the results
3117 * Gets the extents for an array of glyphs. The extents describe a
3118 * user-space rectangle that encloses the "inked" portion of the
3119 * glyphs, (as they would be drawn by cairo_show_glyphs()).
3120 * Additionally, the x_advance and y_advance values indicate the
3121 * amount by which the current point would be advanced by
3122 * cairo_show_glyphs().
3124 * Note that whitespace glyphs do not contribute to the size of the
3125 * rectangle (extents.width and extents.height).
3128 cairo_glyph_extents (cairo_t
*cr
,
3129 const cairo_glyph_t
*glyphs
,
3131 cairo_text_extents_t
*extents
)
3133 cairo_status_t status
;
3135 extents
->x_bearing
= 0.0;
3136 extents
->y_bearing
= 0.0;
3137 extents
->width
= 0.0;
3138 extents
->height
= 0.0;
3139 extents
->x_advance
= 0.0;
3140 extents
->y_advance
= 0.0;
3142 if (unlikely (cr
->status
))
3145 if (num_glyphs
== 0)
3148 if (num_glyphs
< 0) {
3149 _cairo_set_error (cr
, CAIRO_STATUS_NEGATIVE_COUNT
);
3153 if (glyphs
== NULL
) {
3154 _cairo_set_error (cr
, CAIRO_STATUS_NULL_POINTER
);
3158 status
= _cairo_gstate_glyph_extents (cr
->gstate
, glyphs
, num_glyphs
,
3160 if (unlikely (status
))
3161 _cairo_set_error (cr
, status
);
3166 * @cr: a cairo context
3167 * @utf8: a NUL-terminated string of text encoded in UTF-8, or %NULL
3169 * A drawing operator that generates the shape from a string of UTF-8
3170 * characters, rendered according to the current font_face, font_size
3171 * (font_matrix), and font_options.
3173 * This function first computes a set of glyphs for the string of
3174 * text. The first glyph is placed so that its origin is at the
3175 * current point. The origin of each subsequent glyph is offset from
3176 * that of the previous glyph by the advance values of the previous
3179 * After this call the current point is moved to the origin of where
3180 * the next glyph would be placed in this same progression. That is,
3181 * the current point will be at the origin of the final glyph offset
3182 * by its advance values. This allows for easy display of a single
3183 * logical string with multiple calls to cairo_show_text().
3185 * Note: The cairo_show_text() function call is part of what the cairo
3186 * designers call the "toy" text API. It is convenient for short demos
3187 * and simple programs, but it is not expected to be adequate for
3188 * serious text-using applications. See cairo_show_glyphs() for the
3189 * "real" text display API in cairo.
3192 cairo_show_text (cairo_t
*cr
, const char *utf8
)
3194 cairo_text_extents_t extents
;
3195 cairo_status_t status
;
3196 cairo_glyph_t
*glyphs
, *last_glyph
;
3197 cairo_text_cluster_t
*clusters
;
3198 int utf8_len
, num_glyphs
, num_clusters
;
3199 cairo_text_cluster_flags_t cluster_flags
;
3201 cairo_bool_t has_show_text_glyphs
;
3202 cairo_glyph_t stack_glyphs
[CAIRO_STACK_ARRAY_LENGTH (cairo_glyph_t
)];
3203 cairo_text_cluster_t stack_clusters
[CAIRO_STACK_ARRAY_LENGTH (cairo_text_cluster_t
)];
3205 if (unlikely (cr
->status
))
3211 cairo_get_current_point (cr
, &x
, &y
);
3213 utf8_len
= strlen (utf8
);
3215 has_show_text_glyphs
=
3216 cairo_surface_has_show_text_glyphs (cairo_get_target (cr
));
3218 glyphs
= stack_glyphs
;
3219 num_glyphs
= ARRAY_LENGTH (stack_glyphs
);
3221 if (has_show_text_glyphs
) {
3222 clusters
= stack_clusters
;
3223 num_clusters
= ARRAY_LENGTH (stack_clusters
);
3229 status
= _cairo_gstate_text_to_glyphs (cr
->gstate
,
3232 &glyphs
, &num_glyphs
,
3233 has_show_text_glyphs
? &clusters
: NULL
, &num_clusters
,
3235 if (unlikely (status
))
3238 if (num_glyphs
== 0)
3241 status
= _cairo_gstate_show_text_glyphs (cr
->gstate
,
3244 clusters
, num_clusters
,
3246 if (unlikely (status
))
3249 last_glyph
= &glyphs
[num_glyphs
- 1];
3250 status
= _cairo_gstate_glyph_extents (cr
->gstate
,
3253 if (unlikely (status
))
3256 x
= last_glyph
->x
+ extents
.x_advance
;
3257 y
= last_glyph
->y
+ extents
.y_advance
;
3258 cairo_move_to (cr
, x
, y
);
3261 if (glyphs
!= stack_glyphs
)
3262 cairo_glyph_free (glyphs
);
3263 if (clusters
!= stack_clusters
)
3264 cairo_text_cluster_free (clusters
);
3266 if (unlikely (status
))
3267 _cairo_set_error (cr
, status
);
3271 * cairo_show_glyphs:
3272 * @cr: a cairo context
3273 * @glyphs: array of glyphs to show
3274 * @num_glyphs: number of glyphs to show
3276 * A drawing operator that generates the shape from an array of glyphs,
3277 * rendered according to the current font face, font size
3278 * (font matrix), and font options.
3281 cairo_show_glyphs (cairo_t
*cr
, const cairo_glyph_t
*glyphs
, int num_glyphs
)
3283 cairo_status_t status
;
3285 if (unlikely (cr
->status
))
3288 if (num_glyphs
== 0)
3291 if (num_glyphs
< 0) {
3292 _cairo_set_error (cr
, CAIRO_STATUS_NEGATIVE_COUNT
);
3296 if (glyphs
== NULL
) {
3297 _cairo_set_error (cr
, CAIRO_STATUS_NULL_POINTER
);
3301 status
= _cairo_gstate_show_text_glyphs (cr
->gstate
,
3306 if (unlikely (status
))
3307 _cairo_set_error (cr
, status
);
3311 * cairo_show_text_glyphs:
3312 * @cr: a cairo context
3313 * @utf8: a string of text encoded in UTF-8
3314 * @utf8_len: length of @utf8 in bytes, or -1 if it is NUL-terminated
3315 * @glyphs: array of glyphs to show
3316 * @num_glyphs: number of glyphs to show
3317 * @clusters: array of cluster mapping information
3318 * @num_clusters: number of clusters in the mapping
3319 * @cluster_flags: cluster mapping flags
3321 * This operation has rendering effects similar to cairo_show_glyphs()
3322 * but, if the target surface supports it, uses the provided text and
3323 * cluster mapping to embed the text for the glyphs shown in the output.
3324 * If the target does not support the extended attributes, this function
3325 * acts like the basic cairo_show_glyphs() as if it had been passed
3326 * @glyphs and @num_glyphs.
3328 * The mapping between @utf8 and @glyphs is provided by an array of
3329 * <firstterm>clusters</firstterm>. Each cluster covers a number of
3330 * text bytes and glyphs, and neighboring clusters cover neighboring
3331 * areas of @utf8 and @glyphs. The clusters should collectively cover @utf8
3332 * and @glyphs in entirety.
3334 * The first cluster always covers bytes from the beginning of @utf8.
3335 * If @cluster_flags do not have the %CAIRO_TEXT_CLUSTER_FLAG_BACKWARD
3336 * set, the first cluster also covers the beginning
3337 * of @glyphs, otherwise it covers the end of the @glyphs array and
3338 * following clusters move backward.
3340 * See #cairo_text_cluster_t for constraints on valid clusters.
3345 cairo_show_text_glyphs (cairo_t
*cr
,
3348 const cairo_glyph_t
*glyphs
,
3350 const cairo_text_cluster_t
*clusters
,
3352 cairo_text_cluster_flags_t cluster_flags
)
3354 cairo_status_t status
;
3356 if (unlikely (cr
->status
))
3359 /* A slew of sanity checks */
3361 /* Special case for NULL and -1 */
3362 if (utf8
== NULL
&& utf8_len
== -1)
3365 /* No NULLs for non-zeros */
3366 if ((num_glyphs
&& glyphs
== NULL
) ||
3367 (utf8_len
&& utf8
== NULL
) ||
3368 (num_clusters
&& clusters
== NULL
)) {
3369 _cairo_set_error (cr
, CAIRO_STATUS_NULL_POINTER
);
3373 /* A -1 for utf8_len means NUL-terminated */
3375 utf8_len
= strlen (utf8
);
3377 /* Apart from that, no negatives */
3378 if (num_glyphs
< 0 || utf8_len
< 0 || num_clusters
< 0) {
3379 _cairo_set_error (cr
, CAIRO_STATUS_NEGATIVE_COUNT
);
3383 /* Make sure clusters cover the entire glyphs and utf8 arrays,
3384 * and that cluster boundaries are UTF-8 boundaries. */
3385 status
= _cairo_validate_text_clusters (utf8
, utf8_len
,
3387 clusters
, num_clusters
, cluster_flags
);
3388 if (status
== CAIRO_STATUS_INVALID_CLUSTERS
) {
3389 /* Either got invalid UTF-8 text, or cluster mapping is bad.
3390 * Differentiate those. */
3392 cairo_status_t status2
;
3394 status2
= _cairo_utf8_to_ucs4 (utf8
, utf8_len
, NULL
, NULL
);
3398 _cairo_set_error (cr
, status
);
3402 if (num_glyphs
== 0 && utf8_len
== 0)
3405 status
= _cairo_gstate_show_text_glyphs (cr
->gstate
,
3408 clusters
, num_clusters
, cluster_flags
);
3409 if (unlikely (status
))
3410 _cairo_set_error (cr
, status
);
3415 * @cr: a cairo context
3416 * @utf8: a NUL-terminated string of text encoded in UTF-8, or %NULL
3418 * Adds closed paths for text to the current path. The generated
3419 * path if filled, achieves an effect similar to that of
3420 * cairo_show_text().
3422 * Text conversion and positioning is done similar to cairo_show_text().
3424 * Like cairo_show_text(), After this call the current point is
3425 * moved to the origin of where the next glyph would be placed in
3426 * this same progression. That is, the current point will be at
3427 * the origin of the final glyph offset by its advance values.
3428 * This allows for chaining multiple calls to to cairo_text_path()
3429 * without having to set current point in between.
3431 * Note: The cairo_text_path() function call is part of what the cairo
3432 * designers call the "toy" text API. It is convenient for short demos
3433 * and simple programs, but it is not expected to be adequate for
3434 * serious text-using applications. See cairo_glyph_path() for the
3435 * "real" text path API in cairo.
3438 cairo_text_path (cairo_t
*cr
, const char *utf8
)
3440 cairo_status_t status
;
3441 cairo_text_extents_t extents
;
3442 cairo_glyph_t stack_glyphs
[CAIRO_STACK_ARRAY_LENGTH (cairo_glyph_t
)];
3443 cairo_glyph_t
*glyphs
, *last_glyph
;
3447 if (unlikely (cr
->status
))
3453 cairo_get_current_point (cr
, &x
, &y
);
3455 glyphs
= stack_glyphs
;
3456 num_glyphs
= ARRAY_LENGTH (stack_glyphs
);
3458 status
= _cairo_gstate_text_to_glyphs (cr
->gstate
,
3460 utf8
, strlen (utf8
),
3461 &glyphs
, &num_glyphs
,
3465 if (unlikely (status
))
3468 if (num_glyphs
== 0)
3471 status
= _cairo_gstate_glyph_path (cr
->gstate
,
3475 if (unlikely (status
))
3478 last_glyph
= &glyphs
[num_glyphs
- 1];
3479 status
= _cairo_gstate_glyph_extents (cr
->gstate
,
3483 if (unlikely (status
))
3486 x
= last_glyph
->x
+ extents
.x_advance
;
3487 y
= last_glyph
->y
+ extents
.y_advance
;
3488 cairo_move_to (cr
, x
, y
);
3491 if (glyphs
!= stack_glyphs
)
3492 cairo_glyph_free (glyphs
);
3494 if (unlikely (status
))
3495 _cairo_set_error (cr
, status
);
3500 * @cr: a cairo context
3501 * @glyphs: array of glyphs to show
3502 * @num_glyphs: number of glyphs to show
3504 * Adds closed paths for the glyphs to the current path. The generated
3505 * path if filled, achieves an effect similar to that of
3506 * cairo_show_glyphs().
3509 cairo_glyph_path (cairo_t
*cr
, const cairo_glyph_t
*glyphs
, int num_glyphs
)
3511 cairo_status_t status
;
3513 if (unlikely (cr
->status
))
3516 if (num_glyphs
== 0)
3519 if (num_glyphs
< 0) {
3520 _cairo_set_error (cr
, CAIRO_STATUS_NEGATIVE_COUNT
);
3524 if (glyphs
== NULL
) {
3525 _cairo_set_error (cr
, CAIRO_STATUS_NULL_POINTER
);
3529 status
= _cairo_gstate_glyph_path (cr
->gstate
,
3532 if (unlikely (status
))
3533 _cairo_set_error (cr
, status
);
3537 * cairo_get_operator:
3538 * @cr: a cairo context
3540 * Gets the current compositing operator for a cairo context.
3542 * Return value: the current compositing operator.
3545 cairo_get_operator (cairo_t
*cr
)
3547 if (unlikely (cr
->status
))
3548 return CAIRO_GSTATE_OPERATOR_DEFAULT
;
3550 return _cairo_gstate_get_operator (cr
->gstate
);
3554 * cairo_get_tolerance:
3555 * @cr: a cairo context
3557 * Gets the current tolerance value, as set by cairo_set_tolerance().
3559 * Return value: the current tolerance value.
3562 cairo_get_tolerance (cairo_t
*cr
)
3564 if (unlikely (cr
->status
))
3565 return CAIRO_GSTATE_TOLERANCE_DEFAULT
;
3567 return _cairo_gstate_get_tolerance (cr
->gstate
);
3569 slim_hidden_def (cairo_get_tolerance
);
3572 * cairo_get_antialias:
3573 * @cr: a cairo context
3575 * Gets the current shape antialiasing mode, as set by cairo_set_shape_antialias().
3577 * Return value: the current shape antialiasing mode.
3580 cairo_get_antialias (cairo_t
*cr
)
3582 if (unlikely (cr
->status
))
3583 return CAIRO_ANTIALIAS_DEFAULT
;
3585 return _cairo_gstate_get_antialias (cr
->gstate
);
3589 * cairo_has_current_point:
3590 * @cr: a cairo context
3592 * Returns whether a current point is defined on the current path.
3593 * See cairo_get_current_point() for details on the current point.
3595 * Return value: whether a current point is defined.
3600 cairo_has_current_point (cairo_t
*cr
)
3602 if (unlikely (cr
->status
))
3605 return cr
->path
->has_current_point
;
3609 * cairo_get_current_point:
3610 * @cr: a cairo context
3611 * @x: return value for X coordinate of the current point
3612 * @y: return value for Y coordinate of the current point
3614 * Gets the current point of the current path, which is
3615 * conceptually the final point reached by the path so far.
3617 * The current point is returned in the user-space coordinate
3618 * system. If there is no defined current point or if @cr is in an
3619 * error status, @x and @y will both be set to 0.0. It is possible to
3620 * check this in advance with cairo_has_current_point().
3622 * Most path construction functions alter the current point. See the
3623 * following for details on how they affect the current point:
3624 * cairo_new_path(), cairo_new_sub_path(),
3625 * cairo_append_path(), cairo_close_path(),
3626 * cairo_move_to(), cairo_line_to(), cairo_curve_to(),
3627 * cairo_rel_move_to(), cairo_rel_line_to(), cairo_rel_curve_to(),
3628 * cairo_arc(), cairo_arc_negative(), cairo_rectangle(),
3629 * cairo_text_path(), cairo_glyph_path(), cairo_stroke_to_path().
3631 * Some functions use and alter the current point but do not
3632 * otherwise change current path:
3633 * cairo_show_text().
3635 * Some functions unset the current path and as a result, current point:
3636 * cairo_fill(), cairo_stroke().
3639 cairo_get_current_point (cairo_t
*cr
, double *x_ret
, double *y_ret
)
3641 cairo_fixed_t x_fixed
, y_fixed
;
3644 if (cr
->status
== CAIRO_STATUS_SUCCESS
&&
3645 _cairo_path_fixed_get_current_point (cr
->path
, &x_fixed
, &y_fixed
))
3647 x
= _cairo_fixed_to_double (x_fixed
);
3648 y
= _cairo_fixed_to_double (y_fixed
);
3649 _cairo_gstate_backend_to_user (cr
->gstate
, &x
, &y
);
3662 slim_hidden_def(cairo_get_current_point
);
3665 * cairo_get_fill_rule:
3666 * @cr: a cairo context
3668 * Gets the current fill rule, as set by cairo_set_fill_rule().
3670 * Return value: the current fill rule.
3673 cairo_get_fill_rule (cairo_t
*cr
)
3675 if (unlikely (cr
->status
))
3676 return CAIRO_GSTATE_FILL_RULE_DEFAULT
;
3678 return _cairo_gstate_get_fill_rule (cr
->gstate
);
3682 * cairo_get_line_width:
3683 * @cr: a cairo context
3685 * This function returns the current line width value exactly as set by
3686 * cairo_set_line_width(). Note that the value is unchanged even if
3687 * the CTM has changed between the calls to cairo_set_line_width() and
3688 * cairo_get_line_width().
3690 * Return value: the current line width.
3693 cairo_get_line_width (cairo_t
*cr
)
3695 if (unlikely (cr
->status
))
3696 return CAIRO_GSTATE_LINE_WIDTH_DEFAULT
;
3698 return _cairo_gstate_get_line_width (cr
->gstate
);
3700 slim_hidden_def (cairo_get_line_width
);
3703 * cairo_get_line_cap:
3704 * @cr: a cairo context
3706 * Gets the current line cap style, as set by cairo_set_line_cap().
3708 * Return value: the current line cap style.
3711 cairo_get_line_cap (cairo_t
*cr
)
3713 if (unlikely (cr
->status
))
3714 return CAIRO_GSTATE_LINE_CAP_DEFAULT
;
3716 return _cairo_gstate_get_line_cap (cr
->gstate
);
3720 * cairo_get_line_join:
3721 * @cr: a cairo context
3723 * Gets the current line join style, as set by cairo_set_line_join().
3725 * Return value: the current line join style.
3728 cairo_get_line_join (cairo_t
*cr
)
3730 if (unlikely (cr
->status
))
3731 return CAIRO_GSTATE_LINE_JOIN_DEFAULT
;
3733 return _cairo_gstate_get_line_join (cr
->gstate
);
3737 * cairo_get_miter_limit:
3738 * @cr: a cairo context
3740 * Gets the current miter limit, as set by cairo_set_miter_limit().
3742 * Return value: the current miter limit.
3745 cairo_get_miter_limit (cairo_t
*cr
)
3747 if (unlikely (cr
->status
))
3748 return CAIRO_GSTATE_MITER_LIMIT_DEFAULT
;
3750 return _cairo_gstate_get_miter_limit (cr
->gstate
);
3755 * @cr: a cairo context
3756 * @matrix: return value for the matrix
3758 * Stores the current transformation matrix (CTM) into @matrix.
3761 cairo_get_matrix (cairo_t
*cr
, cairo_matrix_t
*matrix
)
3763 if (unlikely (cr
->status
)) {
3764 cairo_matrix_init_identity (matrix
);
3768 _cairo_gstate_get_matrix (cr
->gstate
, matrix
);
3770 slim_hidden_def (cairo_get_matrix
);
3774 * @cr: a cairo context
3776 * Gets the target surface for the cairo context as passed to
3779 * This function will always return a valid pointer, but the result
3780 * can be a "nil" surface if @cr is already in an error state,
3781 * (ie. cairo_status() <literal>!=</literal> %CAIRO_STATUS_SUCCESS).
3782 * A nil surface is indicated by cairo_surface_status()
3783 * <literal>!=</literal> %CAIRO_STATUS_SUCCESS.
3785 * Return value: the target surface. This object is owned by cairo. To
3786 * keep a reference to it, you must call cairo_surface_reference().
3789 cairo_get_target (cairo_t
*cr
)
3791 if (unlikely (cr
->status
))
3792 return _cairo_surface_create_in_error (cr
->status
);
3794 return _cairo_gstate_get_original_target (cr
->gstate
);
3796 slim_hidden_def (cairo_get_target
);
3799 * cairo_get_group_target:
3800 * @cr: a cairo context
3802 * Gets the current destination surface for the context. This is either
3803 * the original target surface as passed to cairo_create() or the target
3804 * surface for the current group as started by the most recent call to
3805 * cairo_push_group() or cairo_push_group_with_content().
3807 * This function will always return a valid pointer, but the result
3808 * can be a "nil" surface if @cr is already in an error state,
3809 * (ie. cairo_status() <literal>!=</literal> %CAIRO_STATUS_SUCCESS).
3810 * A nil surface is indicated by cairo_surface_status()
3811 * <literal>!=</literal> %CAIRO_STATUS_SUCCESS.
3813 * Return value: the target surface. This object is owned by cairo. To
3814 * keep a reference to it, you must call cairo_surface_reference().
3819 cairo_get_group_target (cairo_t
*cr
)
3821 if (unlikely (cr
->status
))
3822 return _cairo_surface_create_in_error (cr
->status
);
3824 return _cairo_gstate_get_target (cr
->gstate
);
3829 * @cr: a cairo context
3831 * Creates a copy of the current path and returns it to the user as a
3832 * #cairo_path_t. See #cairo_path_data_t for hints on how to iterate
3833 * over the returned data structure.
3835 * This function will always return a valid pointer, but the result
3836 * will have no data (<literal>data==%NULL</literal> and
3837 * <literal>num_data==0</literal>), if either of the following
3841 * <listitem>If there is insufficient memory to copy the path. In this
3842 * case <literal>path->status</literal> will be set to
3843 * %CAIRO_STATUS_NO_MEMORY.</listitem>
3844 * <listitem>If @cr is already in an error state. In this case
3845 * <literal>path->status</literal> will contain the same status that
3846 * would be returned by cairo_status().</listitem>
3849 * Return value: the copy of the current path. The caller owns the
3850 * returned object and should call cairo_path_destroy() when finished
3854 cairo_copy_path (cairo_t
*cr
)
3856 if (unlikely (cr
->status
))
3857 return _cairo_path_create_in_error (cr
->status
);
3859 return _cairo_path_create (cr
->path
, cr
->gstate
);
3863 * cairo_copy_path_flat:
3864 * @cr: a cairo context
3866 * Gets a flattened copy of the current path and returns it to the
3867 * user as a #cairo_path_t. See #cairo_path_data_t for hints on
3868 * how to iterate over the returned data structure.
3870 * This function is like cairo_copy_path() except that any curves
3871 * in the path will be approximated with piecewise-linear
3872 * approximations, (accurate to within the current tolerance
3873 * value). That is, the result is guaranteed to not have any elements
3874 * of type %CAIRO_PATH_CURVE_TO which will instead be replaced by a
3875 * series of %CAIRO_PATH_LINE_TO elements.
3877 * This function will always return a valid pointer, but the result
3878 * will have no data (<literal>data==%NULL</literal> and
3879 * <literal>num_data==0</literal>), if either of the following
3883 * <listitem>If there is insufficient memory to copy the path. In this
3884 * case <literal>path->status</literal> will be set to
3885 * %CAIRO_STATUS_NO_MEMORY.</listitem>
3886 * <listitem>If @cr is already in an error state. In this case
3887 * <literal>path->status</literal> will contain the same status that
3888 * would be returned by cairo_status().</listitem>
3891 * Return value: the copy of the current path. The caller owns the
3892 * returned object and should call cairo_path_destroy() when finished
3896 cairo_copy_path_flat (cairo_t
*cr
)
3898 if (unlikely (cr
->status
))
3899 return _cairo_path_create_in_error (cr
->status
);
3901 return _cairo_path_create_flat (cr
->path
, cr
->gstate
);
3905 * cairo_append_path:
3906 * @cr: a cairo context
3907 * @path: path to be appended
3909 * Append the @path onto the current path. The @path may be either the
3910 * return value from one of cairo_copy_path() or
3911 * cairo_copy_path_flat() or it may be constructed manually. See
3912 * #cairo_path_t for details on how the path data structure should be
3913 * initialized, and note that <literal>path->status</literal> must be
3914 * initialized to %CAIRO_STATUS_SUCCESS.
3917 cairo_append_path (cairo_t
*cr
,
3918 const cairo_path_t
*path
)
3920 cairo_status_t status
;
3922 if (unlikely (cr
->status
))
3926 _cairo_set_error (cr
, CAIRO_STATUS_NULL_POINTER
);
3931 if (path
->status
> CAIRO_STATUS_SUCCESS
&&
3932 path
->status
<= CAIRO_STATUS_LAST_STATUS
)
3933 _cairo_set_error (cr
, path
->status
);
3935 _cairo_set_error (cr
, CAIRO_STATUS_INVALID_STATUS
);
3939 if (path
->num_data
== 0)
3942 if (path
->data
== NULL
) {
3943 _cairo_set_error (cr
, CAIRO_STATUS_NULL_POINTER
);
3947 status
= _cairo_path_append_to_context (path
, cr
);
3948 if (unlikely (status
))
3949 _cairo_set_error (cr
, status
);
3954 * @cr: a cairo context
3956 * Checks whether an error has previously occurred for this context.
3958 * Returns: the current status of this context, see #cairo_status_t
3961 cairo_status (cairo_t
*cr
)
3965 slim_hidden_def (cairo_status
);