NPAPI:CoreGraphicsDrawing: Difference between revisions

Line 127: Line 127:
Don't worry!  There is a way for plugins to draw using CoreGraphics, yet remain compatible with QuickDraw-only browsers.  The idea is to use QDBeginCGContext() and QDEndCGContext() to obtain a CGContextRef for the CGrafPtr provided by the browser.  We will be shipping some sample code to demonstrate this technique, but here is a little snippet for your enjoyment:
Don't worry!  There is a way for plugins to draw using CoreGraphics, yet remain compatible with QuickDraw-only browsers.  The idea is to use QDBeginCGContext() and QDEndCGContext() to obtain a CGContextRef for the CGrafPtr provided by the browser.  We will be shipping some sample code to demonstrate this technique, but here is a little snippet for your enjoyment:


static CGContextRef beginQDPluginUpdate(NPWindow *window)
static CGContextRef beginQDPluginUpdate(NPWindow *window)
{
{
     // window->window is an NPPort* since the browser is using QuickDraw
     // window->window is an NPPort* since the browser is using QuickDraw
     NP_Port *npPort = ((NP_Port *)window->window);
     NP_Port *npPort = ((NP_Port *)window->window);  
 
   
     // Get the CGContext for the port
     // Get the CGContext for the port
     CGContextRef cgContext;
     CGContextRef cgContext;
     QDBeginCGContext(npPort->port, &cgContext);
     QDBeginCGContext(npPort->port, &cgContext);
     CGContextSaveGState(cgContext);
     CGContextSaveGState(cgContext);
 
     // Set the CG clip path to the port's clip region -- QDBeginCGContext()
     // Set the CG clip path to the port's clip region -- QDBeginCGContext() does not automatically respect the QuickDraw port's
    // does not automatically respect the QuickDraw port's clip region.
    // clip region.
     RgnHandle clipRegion = NewRgn();
     RgnHandle clipRegion = NewRgn();
     GetPortClipRegion(npPort->port, clipRegion);
     GetPortClipRegion(npPort->port, clipRegion);
Line 146: Line 143:
     ClipCGContextToRegion(cgContext, &portBounds, clipRegion);
     ClipCGContextToRegion(cgContext, &portBounds, clipRegion);
     DisposeRgn(clipRegion);
     DisposeRgn(clipRegion);
 
     // Flip the CG context vertically -- its origin is at the lower left,
   
    // but QuickDraw's origin is at the upper left.
     // Flip the CG context vertically -- its origin is at the lower left, but QuickDraw's origin is at the upper left.
     CGContextTranslateCTM(cgContext, 0.0, portBounds.bottom - portBounds.top);
     CGContextTranslateCTM(cgContext, 0.0, portBounds.bottom - portBounds.top);
     CGContextScaleCTM(cgContext, 1.0, -1.0);
     CGContextScaleCTM(cgContext, 1.0, -1.0);
     return cgContext;
     return cgContext;
}
}
 
static void endQDPluginUpdate(NPWindow *window, CGContextRef cgContext)
static void endQDPluginUpdate(NPWindow *window, CGContextRef cgContext)
{
{
     // Restore state (it was saved in beginQDPluginUpdate())
     // Restore state (it was saved in beginQDPluginUpdate())
     CGContextRestoreGState(cgContext);
     CGContextRestoreGState(cgContext);
     // If we had to prepare the CGContext for use in a QuickDraw-only browser,
     // If we had to prepare the CGContext for use in a QuickDraw-only browser,
     // restore its state and notify QD that the CG drawing sequence is over.
     // restore its state and notify QD that the CG drawing sequence is over.
     CGContextFlush(cgContext);
     CGContextFlush(cgContext);
     QDEndCGContext(((NP_Port *)window->window)->port, &cgContext);
     QDEndCGContext(((NP_Port *)window->window)->port, &cgContext);
}
}


This trick will not work once QuickDraw is removed from Mac OS X.  It is intended for plugin developers that want to draw using CoreGraphics, yet remain compatible with QuickDraw-only browsers that haven't adopted these proposed NPAPI extensions.
This trick will not work once QuickDraw is removed from Mac OS X.  It is intended for plugin developers that want to draw using CoreGraphics, yet remain compatible with QuickDraw-only browsers that haven't adopted these proposed NPAPI extensions.
Confirmed users, Bureaucrats and Sysops emeriti
1,680

edits