Re: [Pyobjc-dev] using a CGContextRef
Brought to you by:
ronaldoussoren
|
From: Ronald O. <ron...@ma...> - 2007-11-14 07:02:08
|
On 14 Nov, 2007, at 2:10, Bobby Powers wrote:
> Hi, I'm relatively new to this, but I've been trying for the past 2
> days to get this working and have scoured the internet, so I figured
> its time to try the mailing list (I've looked through some of these
> archives, too).
>
> I am trying to add support to pyCairo for Cairo's Quartz backend.
> Specifically I am working on a wrapper function that needs the
> CGContextRef for the current screen to create the Cairo surface.
>
> In the test program (reproduced below), i get the context using
> NSGraphicsContext.currentContext().graphicsPort() and pass this to
> the function to create the surface, which is written in C. (As an
> aside, I read in some older mails to import Quartz when the
> applicationDidFinishLaunching method in a delagate was invoked.
> drawRect for me was being called before that, so I switched it to
> the view's init function and it seems to work fine). It took me a
> while to figure out the format strings in PyArg_ParseTuple and I may
> still not be doing them right... but the long and short of it is I
> can't get the CGContextRef in C from Python. I tried typecasting
> the PyObject to CGContextRef, and I've tried PyCObject_AsVoidPtr
> (but the PyObject passed is of the type 'CGContext' and not a
> PyCObject... so I'm not sure what to do.
>
> I am new, so if something like this is explained somewhere (or there
> is a better place to go for help), please let me know. Thanks!
> (and I hope this email formatting comes through alright...)
someContext.__cobject__() returns a PyCObject, PyCObject_AsVoidPtr can
then be used to extract the CGContextRef from C.
Ronald
>
> yours,
> Bobby
>
> ----------
>
> simplified test class:
> from Foundation import *
> from AppKit import *
>
> import cairo
>
> class CairoView(NSView):
> def initWithFrame_(self, frame):
> self = super(CairoView, self).initWithFrame_(frame)
> if self:
> import Quartz
> return self
>
> def drawRect_(self, rect):
> context = NSGraphicsContext.currentContext().graphicsPort()
> surface = cairo.QuartzSurface(context, 200, 200)
>
>
> wrapper function:
> static PyObject *
> quartz_surface_new (PyTypeObject *type, PyObject *args, PyObject
> *kwds)
> {
>
> PyObject *pyContext;
> float width, height;
>
> if (!PyArg_ParseTuple (args, "Off:QuartzSurface.__new__",
> &pyContext, &width, &height))
> return NULL;
>
> CGContextRef ctx = (CGContextRef)(void *)pyContext;
>
> // Make the CGContext coordinate system sane, as expected by Cairo
> CGContextTranslateCTM (ctx, 0.0, height);
> CGContextScaleCTM (ctx, 1.0, -1.0);
>
>
> return PycairoSurface_FromSurface (
>
> cairo_quartz_surface_create_for_cg_context (ctx, width, height),
> NULL);
> }
>
>
> -------------------------------------------------------------------------
> This SF.net email is sponsored by: Splunk Inc.
> Still grepping through log files to find problems? Stop.
> Now Search log events and configuration files using AJAX and a
> browser.
> Download your FREE copy of Splunk now >> http://get.splunk.com/_______________________________________________
> Pyobjc-dev mailing list
> Pyo...@li...
> https://lists.sourceforge.net/lists/listinfo/pyobjc-dev
|