2004-08-02 21:50:17 +00:00
|
|
|
/*
|
2007-10-29 23:25:10 +00:00
|
|
|
CairoContext.m
|
|
|
|
|
|
|
|
Copyright (C) 2003 Free Software Foundation, Inc.
|
|
|
|
|
|
|
|
August 31, 2003
|
|
|
|
Written by Banlu Kemiyatorn <object at gmail dot com>
|
|
|
|
|
|
|
|
This file is part of GNUstep.
|
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Lesser General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
2008-06-10 04:12:46 +00:00
|
|
|
version 2 of the License, or (at your option) any later version.
|
2007-10-29 23:25:10 +00:00
|
|
|
|
|
|
|
This library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
|
|
License along with this library; see the file COPYING.LIB.
|
|
|
|
If not, see <http://www.gnu.org/licenses/> or write to the
|
|
|
|
Free Software Foundation, 51 Franklin Street, Fifth Floor,
|
|
|
|
Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
2008-11-06 09:33:13 +00:00
|
|
|
#include <AppKit/NSBitmapImageRep.h>
|
|
|
|
#include <AppKit/NSGraphics.h>
|
2011-12-23 19:01:45 +00:00
|
|
|
#include <AppKit/NSPrintInfo.h>
|
|
|
|
#include <AppKit/NSPrintOperation.h>
|
2008-11-06 09:33:13 +00:00
|
|
|
|
2004-08-02 21:50:17 +00:00
|
|
|
#include "cairo/CairoContext.h"
|
2006-03-12 21:50:17 +00:00
|
|
|
#include "cairo/CairoSurface.h"
|
2007-04-27 12:23:28 +00:00
|
|
|
#include "cairo/CairoPSSurface.h"
|
2007-09-12 15:46:55 +00:00
|
|
|
#include "cairo/CairoPDFSurface.h"
|
2005-07-27 23:25:32 +00:00
|
|
|
#include "cairo/CairoFontInfo.h"
|
|
|
|
#include "cairo/CairoFontEnumerator.h"
|
2006-03-12 21:50:17 +00:00
|
|
|
#include "config.h"
|
2004-08-02 21:50:17 +00:00
|
|
|
|
2006-03-12 21:50:17 +00:00
|
|
|
#define CGSTATE ((CairoGState *)gstate)
|
2004-08-02 21:50:17 +00:00
|
|
|
|
2008-03-04 10:14:29 +00:00
|
|
|
#if BUILD_SERVER == SERVER_x11
|
2012-08-08 22:22:36 +00:00
|
|
|
# include "cairo/CairoGState.h"
|
2008-03-04 10:14:29 +00:00
|
|
|
# include "x11/XGServer.h"
|
2012-08-08 22:22:36 +00:00
|
|
|
# define _CAIRO_GSTATE_CLASSNAME CairoGState
|
2008-03-04 10:14:29 +00:00
|
|
|
# ifdef USE_GLITZ
|
|
|
|
# define _CAIRO_SURFACE_CLASSNAME XGCairoGlitzSurface
|
|
|
|
# include "cairo/XGCairoGlitzSurface.h"
|
|
|
|
# else
|
2011-09-18 20:37:00 +00:00
|
|
|
//# define _CAIRO_SURFACE_CLASSNAME XGCairoSurface
|
|
|
|
//# include "cairo/XGCairoSurface.h"
|
2011-10-13 21:37:44 +00:00
|
|
|
//# define _CAIRO_SURFACE_CLASSNAME XGCairoXImageSurface
|
|
|
|
//# include "cairo/XGCairoXImageSurface.h"
|
|
|
|
# define _CAIRO_SURFACE_CLASSNAME XGCairoModernSurface
|
|
|
|
# include "cairo/XGCairoModernSurface.h"
|
2008-03-04 10:14:29 +00:00
|
|
|
# endif /* USE_GLITZ */
|
|
|
|
# include "x11/XGServerWindow.h"
|
|
|
|
# include "x11/XWindowBuffer.h"
|
|
|
|
#elif BUILD_SERVER == SERVER_win32
|
2012-08-08 22:22:36 +00:00
|
|
|
# include "cairo/Win32CairoGState.h"
|
2008-03-04 10:14:29 +00:00
|
|
|
# include <windows.h>
|
2013-07-21 13:22:59 +00:00
|
|
|
# include "win32/WIN32Server.h"
|
|
|
|
# include "win32/WIN32Geometry.h"
|
2012-08-08 22:22:36 +00:00
|
|
|
# define _CAIRO_GSTATE_CLASSNAME Win32CairoGState
|
2008-03-04 10:14:29 +00:00
|
|
|
# ifdef USE_GLITZ
|
|
|
|
# define _CAIRO_SURFACE_CLASSNAME Win32CairoGlitzSurface
|
|
|
|
# include "cairo/Win32CairoGlitzSurface.h"
|
|
|
|
# else
|
|
|
|
# define _CAIRO_SURFACE_CLASSNAME Win32CairoSurface
|
|
|
|
# include "cairo/Win32CairoSurface.h"
|
|
|
|
# endif /* USE_GLITZ */
|
|
|
|
#else
|
|
|
|
# error Invalid server for Cairo backend : non implemented
|
|
|
|
#endif /* BUILD_SERVER */
|
|
|
|
|
2004-08-02 21:50:17 +00:00
|
|
|
@implementation CairoContext
|
|
|
|
|
|
|
|
+ (void) initializeBackend
|
|
|
|
{
|
2006-03-12 21:50:17 +00:00
|
|
|
[NSGraphicsContext setDefaultContextClass: self];
|
2004-08-02 21:50:17 +00:00
|
|
|
|
2005-07-27 23:25:32 +00:00
|
|
|
[GSFontEnumerator setDefaultClass: [CairoFontEnumerator class]];
|
|
|
|
[GSFontInfo setDefaultClass: [CairoFontInfo class]];
|
2004-08-02 21:50:17 +00:00
|
|
|
}
|
|
|
|
|
2008-01-19 13:12:03 +00:00
|
|
|
+ (Class) GStateClass
|
2004-08-02 21:50:17 +00:00
|
|
|
{
|
2012-08-08 22:22:36 +00:00
|
|
|
return [_CAIRO_GSTATE_CLASSNAME class];
|
2008-01-19 13:12:03 +00:00
|
|
|
}
|
2004-08-02 21:50:17 +00:00
|
|
|
|
2008-01-19 13:12:03 +00:00
|
|
|
+ (BOOL) handlesPS
|
|
|
|
{
|
|
|
|
return YES;
|
2004-08-02 21:50:17 +00:00
|
|
|
}
|
|
|
|
|
2010-06-01 11:04:36 +00:00
|
|
|
- (BOOL) supportsDrawGState
|
|
|
|
{
|
2011-06-14 22:27:58 +00:00
|
|
|
return YES;
|
2010-06-01 11:04:36 +00:00
|
|
|
}
|
|
|
|
|
2012-11-21 20:53:22 +00:00
|
|
|
- (BOOL) isDrawingToScreen
|
|
|
|
{
|
|
|
|
CairoSurface *surface = nil;
|
|
|
|
[CGSTATE GSCurrentSurface: &surface : NULL : NULL];
|
|
|
|
return [surface isDrawingToScreen];
|
|
|
|
}
|
|
|
|
|
2004-08-02 21:50:17 +00:00
|
|
|
- (void) flushGraphics
|
|
|
|
{
|
2011-10-13 21:37:44 +00:00
|
|
|
// FIXME: Why is this here? When is it called?
|
2013-07-20 16:32:35 +00:00
|
|
|
// Comments added 07/20/2013 to address the above question after
|
|
|
|
// further debugging cairo/MSwindows non-retained backing store type:
|
|
|
|
// This is called from NSWindow/flushWindow for the non-retained backing
|
|
|
|
// store case. This code originally seems to have been added due to the
|
2015-07-14 22:36:43 +00:00
|
|
|
// non-retained backing store type NOT showing up on XWindows/Cairo
|
|
|
|
// combination and added this XFlush call to handle this case.
|
2013-07-20 16:32:35 +00:00
|
|
|
|
|
|
|
// For MSWindows a similar problem seems to occur. However, the only
|
|
|
|
// equivalent to XFlush on MSWindows is GdiFlush, which doesn't cause
|
|
|
|
// the window to re-display properly. So, on MSWindows, the non-retained
|
|
|
|
// cairo backend surface is currently excluded from the build and we
|
|
|
|
// handle it as a buffered window expose event for now.
|
|
|
|
// FIXME: Anyone know how this can be handled/fixed correctly...
|
|
|
|
|
2008-03-04 10:14:29 +00:00
|
|
|
#if BUILD_SERVER == SERVER_x11
|
2005-08-28 00:48:12 +00:00
|
|
|
XFlush([(XGServer *)server xDisplay]);
|
2013-07-20 16:32:35 +00:00
|
|
|
#elif BUILD_SERVER == SERVER_win32
|
2015-07-14 22:36:43 +00:00
|
|
|
CairoSurface *surface = nil;
|
|
|
|
|
2013-07-20 16:32:35 +00:00
|
|
|
[CGSTATE GSCurrentSurface: &surface : NULL : NULL];
|
2015-07-14 22:36:43 +00:00
|
|
|
if ((surface != nil) && ([surface surface] != NULL))
|
2013-07-20 16:32:35 +00:00
|
|
|
{
|
|
|
|
// Non-retained backing store types currently unsupported on MSWindows...
|
|
|
|
// Currently handling such windows as buffered and invoking the handle
|
|
|
|
// expose event processing...
|
|
|
|
RECT rect;
|
|
|
|
HWND hwnd = surface->gsDevice;
|
|
|
|
GetClientRect(hwnd, &rect);
|
|
|
|
NSRect frame = MSWindowRectToGS((WIN32Server*)GSCurrentServer(), hwnd, rect);
|
|
|
|
[[self class] handleExposeRect: frame forDriver: surface];
|
2013-08-09 14:21:15 +00:00
|
|
|
}
|
2013-07-20 16:32:35 +00:00
|
|
|
#endif
|
2004-08-02 21:50:17 +00:00
|
|
|
}
|
|
|
|
|
2008-03-04 10:14:29 +00:00
|
|
|
|
2006-12-26 13:26:40 +00:00
|
|
|
/* Private backend methods */
|
2007-10-18 16:51:56 +00:00
|
|
|
+ (void) handleExposeRect: (NSRect)rect forDriver: (void *)driver
|
2006-12-26 13:26:40 +00:00
|
|
|
{
|
2012-08-08 22:22:36 +00:00
|
|
|
#if BUILD_SERVER == SERVER_x11
|
2011-10-13 21:37:44 +00:00
|
|
|
if ([(id)driver isKindOfClass: [XWindowBuffer class]])
|
|
|
|
{
|
|
|
|
// For XGCairoXImageSurface
|
|
|
|
[(XWindowBuffer *)driver _exposeRect: rect];
|
|
|
|
}
|
2012-08-08 22:22:36 +00:00
|
|
|
else
|
|
|
|
#endif
|
|
|
|
if ([(id)driver isKindOfClass: [CairoSurface class]])
|
2011-10-13 21:37:44 +00:00
|
|
|
{
|
|
|
|
// For XGCairoModernSurface
|
|
|
|
[(CairoSurface *)driver handleExposeRect: rect];
|
|
|
|
}
|
2006-12-26 13:26:40 +00:00
|
|
|
}
|
|
|
|
|
2012-08-08 22:22:36 +00:00
|
|
|
#if BUILD_SERVER == SERVER_x11
|
|
|
|
|
2006-12-26 13:26:40 +00:00
|
|
|
#ifdef XSHM
|
|
|
|
|
2007-10-18 16:51:56 +00:00
|
|
|
+ (void) _gotShmCompletion: (Drawable)d
|
2006-12-26 13:26:40 +00:00
|
|
|
{
|
|
|
|
[XWindowBuffer _gotShmCompletion: d];
|
|
|
|
}
|
|
|
|
|
2007-10-18 16:51:56 +00:00
|
|
|
- (void) gotShmCompletion: (Drawable)d
|
2006-12-26 13:26:40 +00:00
|
|
|
{
|
|
|
|
[XWindowBuffer _gotShmCompletion: d];
|
|
|
|
}
|
|
|
|
|
2008-03-04 10:14:29 +00:00
|
|
|
#endif // XSHM
|
|
|
|
|
|
|
|
#endif // BUILD_SERVER = SERVER_x11
|
2006-12-26 13:26:40 +00:00
|
|
|
|
2004-08-02 21:50:17 +00:00
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation CairoContext (Ops)
|
|
|
|
|
2013-08-09 14:21:15 +00:00
|
|
|
- (BOOL) isCompatibleBitmap: (NSBitmapImageRep*)bitmap
|
|
|
|
{
|
|
|
|
NSString *colorSpaceName;
|
|
|
|
|
|
|
|
if ([bitmap bitmapFormat] != 0)
|
|
|
|
{
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ([bitmap isPlanar])
|
|
|
|
{
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ([bitmap bitsPerSample] != 8)
|
|
|
|
{
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
colorSpaceName = [bitmap colorSpaceName];
|
|
|
|
if (![colorSpaceName isEqualToString: NSDeviceRGBColorSpace] &&
|
|
|
|
![colorSpaceName isEqualToString: NSCalibratedRGBColorSpace])
|
|
|
|
{
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-08-02 21:50:17 +00:00
|
|
|
- (void) GSCurrentDevice: (void **)device : (int *)x : (int *)y
|
|
|
|
{
|
2007-04-27 12:23:28 +00:00
|
|
|
CairoSurface *surface;
|
|
|
|
|
|
|
|
[CGSTATE GSCurrentSurface: &surface : x : y];
|
|
|
|
if (device)
|
|
|
|
{
|
|
|
|
*device = surface->gsDevice;
|
|
|
|
}
|
2004-08-02 21:50:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) GSSetDevice: (void *)device : (int)x : (int)y
|
|
|
|
{
|
2007-04-27 12:23:28 +00:00
|
|
|
CairoSurface *surface;
|
2004-08-02 21:50:17 +00:00
|
|
|
|
2008-03-04 10:14:29 +00:00
|
|
|
surface = [[_CAIRO_SURFACE_CLASSNAME alloc] initWithDevice: device];
|
2007-04-04 14:33:10 +00:00
|
|
|
|
2007-04-27 12:23:28 +00:00
|
|
|
[CGSTATE GSSetSurface: surface : x : y];
|
2008-05-23 04:12:37 +00:00
|
|
|
[surface release];
|
2007-04-04 14:33:10 +00:00
|
|
|
}
|
|
|
|
|
2007-06-29 11:31:53 +00:00
|
|
|
- (void) beginPrologueBBox: (NSRect)boundingBox
|
|
|
|
creationDate: (NSString*)dateCreated
|
|
|
|
createdBy: (NSString*)anApplication
|
|
|
|
fonts: (NSString*)fontNames
|
|
|
|
forWhom: (NSString*)user
|
|
|
|
pages: (int)numPages
|
|
|
|
title: (NSString*)aTitle
|
|
|
|
{
|
2007-09-12 15:46:55 +00:00
|
|
|
CairoSurface *surface;
|
|
|
|
NSSize size;
|
|
|
|
NSString *contextType;
|
|
|
|
|
2011-12-23 19:01:45 +00:00
|
|
|
NSPrintOperation *printOp = [NSPrintOperation currentOperation];
|
|
|
|
NSPrintInfo *printInfo = [printOp printInfo];
|
|
|
|
|
|
|
|
if (printInfo != nil)
|
|
|
|
{
|
|
|
|
size = [printInfo paperSize];
|
2011-12-24 18:44:53 +00:00
|
|
|
|
|
|
|
// FIXME: This is confusing..
|
|
|
|
// When an 8.5x11 page is set to landscape,
|
|
|
|
// NSPrintInfo also swaps the paperSize to be 11x8.5,
|
|
|
|
// but gui also adds a 90 degree rotation as if it will
|
|
|
|
// be drawing on a 8.5x11 page. So, swap 11x8.5 back to
|
|
|
|
// 8.5x11 here.
|
|
|
|
if ([printInfo orientation] == NSLandscapeOrientation)
|
|
|
|
{
|
|
|
|
size = NSMakeSize(size.height, size.width);
|
|
|
|
}
|
2011-12-23 19:01:45 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
[NSException raise: NSInternalInconsistencyException
|
|
|
|
format: @"current print operation printInfo is nil in %@",
|
|
|
|
NSStringFromSelector(_cmd)];
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-09-12 15:46:55 +00:00
|
|
|
contextType = [context_info objectForKey:
|
|
|
|
NSGraphicsContextRepresentationFormatAttributeName];
|
|
|
|
|
|
|
|
if (contextType)
|
|
|
|
{
|
|
|
|
if ([contextType isEqual: NSGraphicsContextPSFormat])
|
|
|
|
{
|
|
|
|
surface = [[CairoPSSurface alloc] initWithDevice: context_info];
|
|
|
|
[surface setSize: size];
|
|
|
|
// This strange setting is needed because of the way GUI handles offset.
|
|
|
|
[CGSTATE GSSetSurface: surface : 0.0 : size.height];
|
|
|
|
RELEASE(surface);
|
|
|
|
}
|
|
|
|
else if ([contextType isEqual: NSGraphicsContextPDFFormat])
|
|
|
|
{
|
|
|
|
surface = [[CairoPDFSurface alloc] initWithDevice: context_info];
|
|
|
|
[surface setSize: size];
|
|
|
|
// This strange setting is needed because of the way GUI handles offset.
|
|
|
|
[CGSTATE GSSetSurface: surface : 0.0 : size.height];
|
|
|
|
RELEASE(surface);
|
|
|
|
}
|
|
|
|
}
|
2007-06-29 11:31:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) showPage
|
|
|
|
{
|
|
|
|
[CGSTATE showPage];
|
|
|
|
}
|
|
|
|
|
2004-08-02 21:50:17 +00:00
|
|
|
@end
|
2008-03-04 10:14:29 +00:00
|
|
|
|
|
|
|
#undef _CAIRO_SURFACE_CLASSNAME
|
2012-11-21 20:53:22 +00:00
|
|
|
#undef _CAIRO_GSTATE_CLASSNAME
|
|
|
|
|