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>
|
|
|
|
|
2004-08-02 21:50:17 +00:00
|
|
|
#include "cairo/CairoContext.h"
|
|
|
|
#include "cairo/CairoGState.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
|
|
|
|
# include "x11/XGServer.h"
|
|
|
|
# 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
|
|
|
|
# include <windows.h>
|
|
|
|
# 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 */
|
|
|
|
|
2008-11-06 09:33:13 +00:00
|
|
|
@interface NSBitmapImageRep (GSPrivate)
|
|
|
|
- (NSBitmapImageRep *) _convertToFormatBitsPerSample: (int)bps
|
|
|
|
samplesPerPixel: (int)spp
|
|
|
|
hasAlpha: (BOOL)alpha
|
|
|
|
isPlanar: (BOOL)isPlanar
|
|
|
|
colorSpaceName: (NSString*)colorSpaceName
|
|
|
|
bitmapFormat: (NSBitmapFormat)bitmapFormat
|
|
|
|
bytesPerRow: (int)rowBytes
|
|
|
|
bitsPerPixel: (int)pixelBits;
|
|
|
|
@end
|
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
|
|
|
{
|
2008-01-19 13:12:03 +00:00
|
|
|
return [CairoGState class];
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
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?
|
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]);
|
2008-03-04 10:14:29 +00:00
|
|
|
#endif // BUILD_SERVER = SERVER_x11
|
2004-08-02 21:50:17 +00:00
|
|
|
}
|
|
|
|
|
2008-03-04 10:14:29 +00:00
|
|
|
#if BUILD_SERVER == SERVER_x11
|
|
|
|
|
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
|
|
|
{
|
2011-10-13 21:37:44 +00:00
|
|
|
if ([(id)driver isKindOfClass: [XWindowBuffer class]])
|
|
|
|
{
|
|
|
|
// For XGCairoXImageSurface
|
|
|
|
[(XWindowBuffer *)driver _exposeRect: rect];
|
|
|
|
}
|
|
|
|
else if ([(id)driver isKindOfClass: [CairoSurface class]])
|
|
|
|
{
|
|
|
|
// For XGCairoModernSurface
|
|
|
|
[(CairoSurface *)driver handleExposeRect: rect];
|
|
|
|
}
|
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)
|
|
|
|
|
2008-11-06 09:33:13 +00:00
|
|
|
- (void) GSDrawImage: (NSRect)rect: (void *)imageref
|
|
|
|
{
|
|
|
|
NSBitmapImageRep *bitmap;
|
|
|
|
const unsigned char *data[5];
|
|
|
|
NSString *colorSpaceName;
|
|
|
|
|
|
|
|
bitmap = (NSBitmapImageRep*)imageref;
|
|
|
|
colorSpaceName = [bitmap colorSpaceName];
|
|
|
|
if ([bitmap isPlanar] || ([bitmap bitmapFormat] != 0)
|
2008-12-11 21:03:51 +00:00
|
|
|
|| ([bitmap bitsPerSample] != 8) ||
|
2008-11-06 09:33:13 +00:00
|
|
|
(![colorSpaceName isEqualToString: NSDeviceRGBColorSpace] &&
|
|
|
|
![colorSpaceName isEqualToString: NSCalibratedRGBColorSpace]))
|
|
|
|
{
|
|
|
|
int bitsPerSample = 8;
|
|
|
|
BOOL isPlanar = NO;
|
|
|
|
int samplesPerPixel = [bitmap hasAlpha] ? 4 : 3;
|
|
|
|
NSString *colorSpaceName = NSCalibratedRGBColorSpace;
|
|
|
|
NSBitmapImageRep *new;
|
|
|
|
|
2008-12-11 21:03:51 +00:00
|
|
|
new = [bitmap _convertToFormatBitsPerSample: bitsPerSample
|
2008-11-06 09:33:13 +00:00
|
|
|
samplesPerPixel: samplesPerPixel
|
|
|
|
hasAlpha: [bitmap hasAlpha]
|
|
|
|
isPlanar: isPlanar
|
|
|
|
colorSpaceName: colorSpaceName
|
|
|
|
bitmapFormat: 0
|
|
|
|
bytesPerRow: 0
|
|
|
|
bitsPerPixel: 0];
|
|
|
|
|
|
|
|
if (new == nil)
|
|
|
|
{
|
|
|
|
NSLog(@"Could not convert bitmap data");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
bitmap = new;
|
|
|
|
}
|
|
|
|
|
|
|
|
[bitmap getBitmapDataPlanes: (unsigned char **)&data];
|
|
|
|
[self NSDrawBitmap: rect : [bitmap pixelsWide] : [bitmap pixelsHigh]
|
|
|
|
: [bitmap bitsPerSample] : [bitmap samplesPerPixel]
|
|
|
|
: [bitmap bitsPerPixel] : [bitmap bytesPerRow] : [bitmap isPlanar]
|
|
|
|
: [bitmap hasAlpha] : [bitmap colorSpaceName]
|
|
|
|
: data];
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
contextType = [context_info objectForKey:
|
|
|
|
NSGraphicsContextRepresentationFormatAttributeName];
|
|
|
|
|
|
|
|
if (contextType)
|
|
|
|
{
|
|
|
|
if ([contextType isEqual: NSGraphicsContextPSFormat])
|
|
|
|
{
|
|
|
|
size = boundingBox.size;
|
|
|
|
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])
|
|
|
|
{
|
|
|
|
size = boundingBox.size;
|
|
|
|
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
|