[cairo-commit] cairo/src cairo.h, 1.78, 1.79 cairo_surface.c, 1.44, 1.45 cairoint.h, 1.104, 1.105

Carl Worth commit at pdx.freedesktop.org
Thu Mar 10 08:59:13 PST 2005


Committed by: cworth

Update of /cvs/cairo/cairo/src
In directory gabe:/tmp/cvs-serv30093/src

Modified Files:
	cairo.h cairo_surface.c cairoint.h 
Log Message:

        Originally: 2005-02-24  Carl Worth  <cworth at cworth.org>

        * src/cairo_surface.c (cairo_surface_set_user_data)
        (cairo_surface_get_user_data):
        * src/cairo.h: Add const qualifier to cairo_user_data_key_t
        arguments.

        Originally: 2005-02-15  Kristian Høgsberg  <krh at redhat.com>

        * src/cairo_surface.c (cairo_surface_get_data, cairo_surface_set_data):
        Add these two functions to set and get user data on an surface.

        * src/cairo.h: Function prototypes for new functions.

        * test/user_data.c: Test case for user data functions.


Index: cairo.h
===================================================================
RCS file: /cvs/cairo/cairo/src/cairo.h,v
retrieving revision 1.78
retrieving revision 1.79
diff -u -d -r1.78 -r1.79
--- cairo.h	9 Mar 2005 20:35:36 -0000	1.78
+++ cairo.h	10 Mar 2005 16:59:11 -0000	1.79
@@ -705,6 +705,15 @@
 const char *
 cairo_status_string (cairo_t *cr);
 
+/**
+ * cairo_destroy_func_t
+ *
+ * #cairo_destroy_func_t the type of function which is called when a
+ * data element is destroyed. It is passed the pointer to the data
+ * element and should free any memory and resources allocated for it.
+ */
+typedef void (*cairo_destroy_func_t) (void *data);
+
 /* Surface manipulation */
 /* XXX: We may want to rename this function in light of the new
    virtualized surface backends... */
@@ -759,6 +768,30 @@
 cairo_filter_t 
 cairo_surface_get_filter (cairo_surface_t *surface);
 
+
+/**
+ * cairo_user_data_key_t
+ *
+ * #cairo_user_data_key_t is used for attaching user data to cairo
+ * data structures.  The actual contents of the struct is never used,
+ * and there is no need to initialize the object; only the unique
+ * address of a #cairo_data_key_t object is used.  Typically, you
+ * would just use the address of a static #cairo_data_key_t object.
+ */
+typedef struct _cairo_user_data_key {
+    int unused;
+} cairo_user_data_key_t;
+
+void *
+cairo_surface_get_user_data (cairo_surface_t		 *surface,
+			     const cairo_user_data_key_t *key);
+
+cairo_status_t
+cairo_surface_set_user_data (cairo_surface_t		 *surface,
+			     const cairo_user_data_key_t *key,
+			     void			 *data,
+			     cairo_destroy_func_t	 destroy);
+
 /* Image-surface functions */
 
 cairo_surface_t *

Index: cairo_surface.c
===================================================================
RCS file: /cvs/cairo/cairo/src/cairo_surface.c,v
retrieving revision 1.44
retrieving revision 1.45
diff -u -d -r1.44 -r1.45
--- cairo_surface.c	4 Mar 2005 02:39:06 -0000	1.44
+++ cairo_surface.c	10 Mar 2005 16:59:11 -0000	1.45
@@ -38,6 +38,12 @@
 
 #include "cairoint.h"
 
+typedef struct {
+    const cairo_user_data_key_t *key;
+    void *user_data;
+    cairo_destroy_func_t destroy;
+} cairo_user_data_slot_t;
+
 void
 _cairo_surface_init (cairo_surface_t			*surface,
 		     const cairo_surface_backend_t	*backend)
@@ -45,6 +51,8 @@
     surface->backend = backend;
 
     surface->ref_count = 1;
+    _cairo_array_init (&surface->user_data_slots,
+		       sizeof (cairo_user_data_slot_t));
 
     _cairo_matrix_init (&surface->matrix);
     surface->filter = CAIRO_FILTER_NEAREST;
@@ -145,6 +153,88 @@
 }
 slim_hidden_def(cairo_surface_destroy);
 
+/**
+ * cairo_surface_get_user_data:
+ * @surface: a #cairo_surface_t
+ * @key: the address of the #cairo_user_data_key_t the user data was
+ * attached to
+ * 
+ * Return user data previously attached to @surface using the specified
+ * key.  If no user data has been attached with the given key this
+ * function returns %NULL.
+ * 
+ * Return value: the user data previously attached or %NULL.
+ **/
+void *
+cairo_surface_get_user_data (cairo_surface_t		 *surface,
+			     const cairo_user_data_key_t *key)
+{
+    int i, num_slots;
+    cairo_user_data_slot_t *slots;
+
+    num_slots = surface->user_data_slots.num_elements;
+    slots = (cairo_user_data_slot_t *) surface->user_data_slots.elements;
+    for (i = 0; i < num_slots; i++) {
+	if (slots[i].key == key)
+	    return slots[i].user_data;
+    }
+
+    return NULL;
+}
+
+/**
+ * cairo_surface_set_user_data:
+ * @surface: a #cairo_surface_t
+ * @key: the address of a #cairo_user_data_key_t to attach the user data to
+ * @user_data: the user data to attach to the surface
+ * @destroy: a #cairo_destroy_func_t which will be called when the
+ * surface is destroyed or when new user data is attached using the
+ * same key.
+ * 
+ * Attach user data to @surface.  To remove user data from a surface,
+ * call this function with the key that was used to set it and %NULL
+ * for @data.
+ *
+ * Return value: %CAIRO_STATUS_SUCCESS or %CAIRO_STATUS_NO_MEMORY if a
+ * slot could not be allocated for the user data.
+ **/
+cairo_status_t
+cairo_surface_set_user_data (cairo_surface_t		 *surface,
+			     const cairo_user_data_key_t *key,
+			     void			 *user_data,
+			     cairo_destroy_func_t	 destroy)
+{
+    int i, num_slots;
+    cairo_user_data_slot_t *slots, *s;
+
+    s = NULL;
+    num_slots = surface->user_data_slots.num_elements;
+    slots = (cairo_user_data_slot_t *) surface->user_data_slots.elements;
+    for (i = 0; i < num_slots; i++) {
+	if (slots[i].key == key) {
+	    if (slots[i].user_data != NULL)
+		slots[i].destroy (slots[i].user_data);
+	    s = &slots[i];
+	    break;
+	}
+	if (slots[i].user_data == NULL) {
+	    s = &slots[i];
+	    break;
+	}
+    }
+
+    if (s == NULL)
+	s = _cairo_array_append (&surface->user_data_slots, NULL, 1);
+    if (s == NULL)
+	return CAIRO_STATUS_NO_MEMORY;
+
+    s->key = key;
+    s->user_data = user_data;
+    s->destroy = destroy;
+
+    return CAIRO_STATUS_SUCCESS;
+}
+
 double
 _cairo_surface_pixels_per_inch (cairo_surface_t *surface)
 {

Index: cairoint.h
===================================================================
RCS file: /cvs/cairo/cairo/src/cairoint.h,v
retrieving revision 1.104
retrieving revision 1.105
diff -u -d -r1.104 -r1.105
--- cairoint.h	9 Mar 2005 20:35:36 -0000	1.104
+++ cairoint.h	10 Mar 2005 16:59:11 -0000	1.105
@@ -666,6 +666,7 @@
     const cairo_surface_backend_t *backend;
 
     unsigned int ref_count;
+    cairo_array_t user_data_slots;
 
     cairo_matrix_t matrix;
     cairo_filter_t filter;




More information about the cairo-commit mailing list