Package: cairo

Macro cairo:with-script-surface

Lambda List

cairo:with-script-surface ((surface path content width height) &body body)

Syntax

(cairo:with-script-surface (surface path content width height) body) => result

Arguments

surface -- a newly allocated cairo:surface-t instance
path -- a path or namestring for the file to write the script to
content -- a cairo:content-t value
width -- a number coerced to a double float for the width in pixels
height -- a number coerced to a double float for the height in pixels

Details

The cairo:with-script-surface macro allocates a new cairo:surface-t instance for a newly created cairo:device-t instance of :script type and executes the body that uses the Cairo script surface. After execution of the body the allocated memory for the Cairo surface and the Cairo device is released. This macro calls the cairo:script-create function to create the device and the cairo:script-surface-create function to create the surface.

Examples

From the examples for the Cairo library, which shows how to use a script surface.
;; Draw a rectangle on a Cairo context
(defun draw-stroke (context width height)
  (cairo:save context)
  ;; Clear surface
  (cairo:set-source-rgb context 1.0 1.0 1.0)
  (cairo:paint context)
  ;; Example is in 1.0 x 1.0 coordinate space
  (cairo:scale context width height)
  ;; Drawing code goes here
  (setf (cairo:line-width context) 0.1)
  (cairo:set-source-rgb context 1.0 0.0 0.0)
  (cairo:rectangle context 0.25 0.25 0.5 0.5)
  (cairo:stroke context)
  (cairo:restore context))

;; Generate a script surface and call a draw function (defun demo-script-draw (&optional (drawfunc #'draw-stroke)) (let ((path (sys-path "out/script-draw.script")) (width 200) (height 200)) (cairo:with-script-surface (surface path :color width height) (cairo:with-context (context surface) (funcall drawfunc context width height) (cairo:surface-show-page surface)))))
This is the output of this example.
%!CairoScript
<< /content //COLOR /width 200 /height 200 >> surface context
1 g set-source
paint
n 50 50 100 100 rectangle
1 0 0 rgb set-source
200 200 scale
0.1 set-line-width
stroke+
show-page
pop    
 

See also

2025-1-29