anchor.diff:    (declare (ignore class))
     #'%surface-destroy)
   
  -(defbinding %surface-set-user-data (surface key data-id) status
  +(defbinding %surface-set-user-data (surface key data-id) status-signal
     (surface pointer)
     ((quark-intern key) pointer-data)
     (data-id pointer-data)
  
positive.code:   (x-pixels-per-inch double-float)
    (y-pixels-per-inch double-float))
  
  (define-callback stream-write-func status 
      ((stream-id pointer-data) (data pointer) (length unsigned-int))
    (let ((stream (find-user-data stream-id)))
      (typecase stream
        (stream
         (map-c-vector 'nil #'(lambda (octet) (write-byte octet stream))
  	data '(unsigned-byte 8) length))
        ((or symbol function)
         (funcall stream 
  	(map-c-vector 'vector #'identity data '(unsigned-byte 8) length)))))
    :success)
  
  (define-callback stream-read-func status 
      ((stream-id pointer-data) (data pointer) (length unsigned-int))
    (block stream-read
      (let ((stream (find-user-data stream-id)))
        (typecase stream
  	(stream
  	 (loop for i below length do
  	  (let ((byte (read-byte stream nil)))
  	    (if byte
  		(setf (gffi::ref-uint-8 data i) byte)
  	      (return-from stream-read :read-error)))))
  	((or symbol function) (funcall stream data length))))
      :success))
  
  
  (defmacro with-surface ((surface cr) &body body)
    `(let ((,cr (make-instance 'context :target ,surface)))
  
negative.code: 
  ;; Image Surface
  
  (defmethod allocate-foreign ((surface image-surface) &key source type
  			     width height stride format)
    (etypecase source
     (stream
      (let ((stream-id (register-user-data source)))
        (unwind-protect
  	   (cond
  	     ((member type '("png" "image/png") :test #'equal)
  	      (%image-surface-create-from-png-stream stream-id))
  	     ((member type '("jpeg" "image/jpeg") :test #'equal)
  	      (%image-surface-create-from-jpeg-stream stream-id))
  	     ((not type) (error "Image type must be specified"))
  	     ((error "Can't handle image type ~A" type)))
  	(destroy-user-data stream-id))))
     ((or string pathname)
      (cond 
        ((member type '("png" "image/png") :test #'equal)
         (%image-surface-create-from-png source))
        ((member type '("jpeg" "image/jpeg") :test #'equal)
         (%image-surface-create-from-jpeg source))
        ((not type) (error "Image type must be specified"))
        ((error "Can't handle image type ~A" type))))
     (pointer
      (%image-surface-create-for-data source format width height 
       (or stride (format-stride-for-width format width))))
     (null (%image-surface-create format width height))))
  
  #?(pkg-exists-p "cairo" :atleast-version "1.6")
  (defbinding format-stride-for-width () int
    surface-format (width int))
  
  #?-(pkg-exists-p "cairo" :atleast-version "1.6")
  (defun format-stride-for-width (format width)
    (let ((element-size (cdr (assoc format '((:argb32 . 4) (:rgb24 . 4) (:a8 . 1) (:a1 1/8))))))
      (ceiling (* width element-size))))
  
  
  (defbinding %image-surface-create () pointer
  