+(defbinding %image-surface-create-from-png () pointer
+ (filename pathname))
+
+(defbinding %image-surface-create-from-png-stream (stream) pointer
+ (stream-read-func callback)
+ (stream pointer-data))
+
+(defbinding surface-write-to-png () status
+ (surface surface)
+ (filename pathname))
+
+
+(defbinding %surface-write-to-png-stream (surface stream) status
+ (surface surface)
+ (stream-write-func callback)
+ (stream pointer-data))
+
+(defun surface-write-to-png-stream (surface stream)
+ (let ((stream-id (register-user-data stream)))
+ (unwind-protect
+ (%surface-write-to-png-stream surface stream-id)
+ (destroy-user-data stream-id))))
+
+
+;;; Virtual size surface (abstract class)
+
+(defmethod initialize-instance :after ((surface vector-surface) &key
+ width height)
+ (setf (user-data surface 'width) width)
+ (setf (user-data surface 'height) height))
+
+(defmethod surface-width ((surface vector-surface))
+ (user-data surface 'width))
+
+(defmethod surface-height ((surface vector-surface))
+ (user-data surface 'height))
+
+
+(defun allocate-vector-surface (surface-create surface-create-for-stream
+ &key output filename stream width height)
+ (let ((location
+ (cond
+ ((/= (count-if #'identity (list output filename stream)) 1)
+ (error "One and only one of the arguments :OUTPUT, :FILENAME and :STREAM shoud be specified"))
+ (filename (funcall surface-create filename width height))
+ ((typep output '(or string pathname))
+ (%svg-surface-create output width height))
+ (t
+ (let* ((stream-id (register-user-data (or stream output)))
+ (location (funcall surface-create-for-stream
+ stream-id width height)))
+ (%surface-set-user-data location 'stream stream-id)
+ location)))))
+ location))
+
+
+;;; PDF Surface
+
+(defmethod allocate-foreign ((surface pdf-surface) &rest args)
+ (apply #'allocate-vector-surface
+ #'%pdf-surface-create #'%pdf-surface-create-for-stream args))
+
+(defbinding %pdf-surface-create () pointer
+ (filename pathname)
+ (width double-float)
+ (height double-float))
+
+(defbinding %pdf-surface-create-for-stream (stream width height) pointer
+ (stream-write-func callback)
+ (stream pointer-data)
+ (width double-float)
+ (height double-float))
+
+(defbinding pdf-surface-set-size () nil
+ (surface pdf-surface)
+ (width double-float)
+ (height double-float))
+
+
+;;; PS Surface
+
+(defmethod allocate-foreign ((surface ps-surface) &rest args)
+ (apply #'allocate-vector-surface
+ #'%ps-surface-create #'%ps-surface-create-for-stream args))
+
+(defbinding %ps-surface-create () pointer
+ (filename pathname)
+ (width double-float)
+ (height double-float))
+
+(defbinding %ps-surface-create-for-stream (stream width height) pointer
+ (stream-write-func callback)
+ (stream pointer-data)
+ (width double-float)
+ (height double-float))
+
+(defbinding ps-surface-set-size () nil
+ (surface ps-surface)
+ (width double-float)
+ (height double-float))