Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added helper macros. #16

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/package.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
image-surface-get-stride image-surface-create-from-png
image-surface-create-from-png-callback
image-surface-create-from-png-stream
surface-write-to-png with-png-surface
with-context-from-surface with-surface-and-context
surface-write-to-png with-surface with-png-surface
create-surface-from-foreign
surface-flush surface-finish surface-mark-dirty

Expand Down
27 changes: 26 additions & 1 deletion src/surface.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,31 @@ Otherwise, return the copy of the image data along with the pointer."
(with-cairo-object (surface pointer)
(cairo_image_surface_get_stride pointer)))

(defmacro with-surface ((surface &optional surface-name) &body body)
(let ((var-name (or surface-name '*surface*)))
`(let ((,var-name ,surface))
(unwind-protect
(progn ,@body)

(progn (surface-finish ,var-name)
(destroy ,var-name))))))

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like this should optionally not destroy the surface (maybe use &key not &optional for further extension). Also, there's no (defvar *surface*) afaict, which there probably should be, even if it's not really used elsewhere else (yet!).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. I must have defvared surface somewhere in cl-pango. I also wanted to discuss with-context. It seems like the context should be destroyed at the end. Whatever the case, with-context and with-surface should work the same to limit surprises.

(defmacro with-context-from-surface ((surface) &body body)
(let ((context (gensym "context")))
`(let ((,context (create-context ,surface)))
(unwind-protect
(with-context (,context)
,@body)
(destroy ,context)))))


(defmacro with-surface-and-context ((surface &optional surface-name) &body body)
(let ((var-name (or surface-name '*surface*)))
`(with-surface (,surface ,var-name)
(with-context-from-surface (,var-name)
,@body))))


;;;;
;;;; PNG surfaces
;;;;
Expand Down Expand Up @@ -372,7 +397,7 @@ single argument which is the amount of data that to be retrieved."
pointer (namestring (merge-pathnames filename)))))
(unless (eq (lookup-cairo-enum status table-status) :success)
(warn "function returned with status ~a." status)))))

(defmacro with-png-surface ((png-file surface-name) &body body)
`(let ((,surface-name (image-surface-create-from-png ,png-file)))
(unwind-protect (progn ,@body)
Expand Down