[cairo] Re: add solid/gradient pattern info getters

Vladimir Vukicevic vladimirv at gmail.com
Wed Sep 13 16:36:08 PDT 2006


> > cairo_status_t
> > cairo_linear_gradient_pattern_get_endpoints (cairo_pattern_t *pattern,
> >                                              double *x0, double *y0, double *r0,
> >                                              double *x1, double *y1, double *r1);

This should be "cairo_radial_gradient_get_circles".

Right patch attached, also trying to avoid the base64-ing that gmail
seems to do...

    - Vlad
-------------- next part --------------
diff --git a/src/cairo.h b/src/cairo.h
index ca8c375..0629c9e 100644
--- a/src/cairo.h
+++ b/src/cairo.h
@@ -199,7 +199,8 @@ typedef enum _cairo_status {
     CAIRO_STATUS_INVALID_VISUAL,
     CAIRO_STATUS_FILE_NOT_FOUND,
     CAIRO_STATUS_INVALID_DASH,
-    CAIRO_STATUS_INVALID_DSC_COMMENT
+    CAIRO_STATUS_INVALID_DSC_COMMENT,
+    CAIRO_STATUS_INVALID_INDEX
 } cairo_status_t;
 
 /**
@@ -1549,6 +1550,25 @@ cairo_pattern_set_filter (cairo_pattern_
 cairo_public cairo_filter_t
 cairo_pattern_get_filter (cairo_pattern_t *pattern);
 
+cairo_public cairo_status_t
+cairo_solid_pattern_get_color (cairo_pattern_t *pattern,
+			       double *r, double *g, double *b, double *a);
+
+cairo_public cairo_status_t
+cairo_gradient_pattern_get_color_stop (cairo_pattern_t *pattern,
+				       int stop_index, double *offset,
+				       double *r, double *g, double *b, double *a);
+
+cairo_public cairo_status_t
+cairo_linear_gradient_pattern_get_endpoints (cairo_pattern_t *pattern,
+					     double *x0, double *y0,
+					     double *x1, double *y1);
+
+cairo_public cairo_status_t
+cairo_radial_gradient_pattern_get_circles (cairo_pattern_t *pattern,
+                                           double *x0, double *y0, double *r0,
+                                           double *x1, double *y1, double *r1);
+
 /* Matrix functions */
 
 cairo_public void
diff --git a/src/cairo-pattern.c b/src/cairo-pattern.c
index a859e52..4ae2a1a 100644
--- a/src/cairo-pattern.c
+++ b/src/cairo-pattern.c
@@ -1,2 +1,3 @@
+/* -*- Mode: c; c-basic-offset: 4; indent-tabs-mode: t; tab-width: 8; -*- */
 /* cairo - a vector graphics library with display and print output
  *
@@ -460,10 +461,10 @@ cairo_pattern_create_linear (double x0, 
  * cairo_pattern_create_radial:
  * @cx0: x coordinate for the center of the start circle
  * @cy0: y coordinate for the center of the start circle
- * @radius0: radius of the start cirle
+ * @radius0: radius of the start circle
  * @cx1: x coordinate for the center of the end circle
  * @cy1: y coordinate for the center of the end circle
- * @radius1: radius of the end cirle
+ * @radius1: radius of the end circle
  *
  * Creates a new radial gradient cairo_pattern_t between the two
  * circles defined by (x0, y0, c0) and (x1, y1, c0).  Before using the
@@ -1475,3 +1476,148 @@ _cairo_pattern_get_extents (cairo_patter
 
     return CAIRO_STATUS_SUCCESS;
 }
+
+/**
+ * cairo_solid_pattern_get_color
+ * @pattern: a #cairo_pattern_t
+ * @r, @g, @b, @a: a double to return a color value in. must not be NULL.
+ *
+ * Gets the solid color for a solid color pattern.
+ *
+ * Return value: CAIRO_STATUS_SUCCESS, or
+ * CAIRO_STATUS_PATTERN_TYPE_MISMATCH if the pattern is not a solid
+ * color pattern.
+ *
+ * Since: 1.4
+ **/
+cairo_status_t
+cairo_solid_pattern_get_color (cairo_pattern_t *pattern,
+			       double *r, double *g, double *b, double *a)
+{
+    cairo_solid_pattern_t *solid = (cairo_solid_pattern_t*) pattern;
+
+    assert(r && g && b && a);
+
+    if (pattern->type != CAIRO_PATTERN_TYPE_SOLID)
+	return CAIRO_STATUS_PATTERN_TYPE_MISMATCH;
+
+    _cairo_color_get_rgba (&solid->color, r, g, b, a);
+
+    return CAIRO_STATUS_SUCCESS;
+}
+
+/**
+ * cairo_gradient_pattern_get_color_stop
+ * @pattern: a #cairo_pattern_t
+ * @stop_index: a number from 0 to 1 minus the number of color stops
+ * @offset: a double representing the color stop offset
+ * @r, @g, @b, @a: a double to return a color value in. must not be NULL.
+ *
+ * Gets the color stop from a gradient pattern.	 The caller should
+ * keep increasing stop_index until this function returns CAIRO_STATUS_INVALID_INDEX
+ *
+ * Return value: CAIRO_STATUS_SUCCESS, or CAIRO_STATUS_INVALID_INDEX if there
+ * is no stop at the given index.  If the pattern is not a gradient pattern,
+ * CAIRO_STATUS_PATTERN_TYPE_MISMATCH is returned.
+ *
+ * Since: 1.4
+ **/
+cairo_status_t
+cairo_gradient_pattern_get_color_stop (cairo_pattern_t *pattern,
+				       int stop_index, double *offset,
+				       double *r, double *g, double *b, double *a)
+{
+    cairo_gradient_pattern_t *gradient = (cairo_gradient_pattern_t*) pattern;
+
+    if (!(offset && r && g && b && a))
+	return CAIRO_STATUS_NULL_POINTER;
+
+    if (pattern->type != CAIRO_PATTERN_TYPE_LINEAR ||
+	pattern->type != CAIRO_PATTERN_TYPE_RADIAL)
+	return CAIRO_STATUS_PATTERN_TYPE_MISMATCH;
+
+    if (stop_index < 0 || stop_index >= gradient->n_stops)
+	return CAIRO_STATUS_INVALID_INDEX;
+
+    *offset = _cairo_fixed_to_double(gradient->stops[stop_index].x);
+    *r = gradient->stops[stop_index].color.red / (double) 0xffff;
+    *g = gradient->stops[stop_index].color.green / (double) 0xffff;
+    *b = gradient->stops[stop_index].color.blue / (double) 0xffff;
+    *a = gradient->stops[stop_index].color.alpha / (double) 0xffff;
+
+    return CAIRO_STATUS_SUCCESS;
+}
+
+/**
+ * cairo_linear_gradient_pattern_get_endpoints
+ * @pattern: a #cairo_pattern_t
+ * @x0, @y0, @x1, @y1: a double to return the endpoint values in. must
+ * not be NULL.
+ *
+ * Gets the gradient endpoints for a linear gradient.
+ *
+ * Return value: CAIRO_STATUS_SUCESS unless the operation fails.  If
+ * the pattern is not a linear gradient pattern,
+ * CAIRO_STATUS_PATTERN_TYPE_MISMATCH is returned.  If any of x0, y0,
+ * x1, y1 are NULL, CAIRO_STATUS_NULL_POINTER is returned.
+ *
+ * Since: 1.4
+ **/
+cairo_status_t
+cairo_linear_gradient_pattern_get_endpoints (cairo_pattern_t *pattern,
+					     double *x0, double *y0,
+					     double *x1, double *y1)
+{
+    cairo_linear_pattern_t *linear = (cairo_linear_pattern_t*) pattern;
+
+    if (!(x0 && y0 && x1 && y1))
+	return CAIRO_STATUS_NULL_POINTER;
+
+    if (pattern->type != CAIRO_PATTERN_TYPE_LINEAR)
+	return CAIRO_STATUS_PATTERN_TYPE_MISMATCH;
+
+    *x0 = _cairo_fixed_to_double (linear->gradient.p1.x);
+    *y0 = _cairo_fixed_to_double (linear->gradient.p1.y);
+    *x1 = _cairo_fixed_to_double (linear->gradient.p2.x);
+    *y1 = _cairo_fixed_to_double (linear->gradient.p2.y);
+
+    return CAIRO_STATUS_SUCCESS;
+}
+
+/**
+ * cairo_radial_gradient_pattern_get_circles
+ * @pattern: a #cairo_pattern_t
+ * @x0, @y0, @r0: doubles to return the center and radius of first (inner) circle
+ * @x1, @y1, @r1: doubles to return the center and radius of second (outer) circle
+ *
+ * Gets the circle endpoints for a radial gradient.
+ *
+ * Return value: CAIRO_STATUS_SUCESS unless the operation fails.  If
+ * the pattern is not a radial gradient pattern,
+ * CAIRO_STATUS_PATTERN_TYPE_MISMATCH is returned.  If any of x0, y0,
+ * r0, x1, y1 r1, are NULL, CAIRO_STATUS_NULL_POINTER is returned.
+ *
+ * Since: 1.4
+ **/
+cairo_status_t
+cairo_radial_gradient_pattern_get_circles (cairo_pattern_t *pattern,
+					   double *x0, double *y0, double *r0,
+					   double *x1, double *y1, double *r1)
+{
+    cairo_radial_pattern_t *radial = (cairo_radial_pattern_t*) pattern;
+
+    if (!(x0 && y0 && r0 && x1 && y1 && r1))
+	return CAIRO_STATUS_NULL_POINTER;
+
+    if (pattern->type != CAIRO_PATTERN_TYPE_LINEAR)
+	return CAIRO_STATUS_PATTERN_TYPE_MISMATCH;
+
+    *x0 = _cairo_fixed_to_double (radial->gradient.inner.x);
+    *y0 = _cairo_fixed_to_double (radial->gradient.inner.y);
+    *r0 = _cairo_fixed_to_double (radial->gradient.inner.radius);
+    *x1 = _cairo_fixed_to_double (radial->gradient.outer.x);
+    *y1 = _cairo_fixed_to_double (radial->gradient.outer.y);
+    *r1 = _cairo_fixed_to_double (radial->gradient.outer.radius);
+
+    return CAIRO_STATUS_SUCCESS;
+}


More information about the cairo mailing list