Initially just a copy of the Opal backend.

This commit is contained in:
Gregory John Casamento 2019-10-09 11:45:00 -04:00
parent c6919519c6
commit 3876d7299e
11 changed files with 2507 additions and 0 deletions

View file

@ -0,0 +1,116 @@
/*
OpalBridge.m
Copyright (C) 2017 Free Software Foundation, Inc.
Author: Daniel Ferreira <dtf@stanford.edu>
Date: July 2017
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
version 2 of the License, or (at your option) any later version.
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.
*/
#import <AppKit/NSColor.h>
#import <AppKit/NSImage.h>
#import <AppKit/NSColorSpace.h>
#import <AppKit/NSGraphics.h>
#import <CoreGraphics/CGColor.h>
#import <CoreGraphics/CGImage.h>
#import <objc/runtime.h>
@implementation NSColor (GSQuartz)
/*
* FIXME:
* NOTE 1: GNUstep-GUI does not allow an NSColor to be created with a custom
* NSColorSpace. If this were allowed, we'd have to:
* 1) implement a bridge for -[NSColorSpace CGColorSpace]
* 2) for each color, extract that color space and generate a CGColorRef.
* NOTE 2: GNUstep-GUI makes no distinction of device and generic color spaces.
* If this ever ceases to be the case, some adjustment might be necessary here.
*/
- (CGColorRef)CGColor
{
NSString *name = [self colorSpaceName];
// FIXME: we should handle black color spaces here, which we currently
// ignore in the implementation.
CFStringRef cgColorSpaceName = NULL;
if ([name isEqualToString: NSCalibratedRGBColorSpace]
|| [name isEqualToString: NSDeviceRGBColorSpace])
cgColorSpaceName = kCGColorSpaceSRGB;
else if ([name isEqualToString: NSCalibratedBlackColorSpace]
|| [name isEqualToString: NSCalibratedWhiteColorSpace]
|| [name isEqualToString: NSDeviceBlackColorSpace]
|| [name isEqualToString: NSDeviceWhiteColorSpace])
cgColorSpaceName = kCGColorSpaceGenericGray;
else if ([name isEqualToString: NSDeviceCMYKColorSpace])
cgColorSpaceName = kCGColorSpaceGenericCMYK;
else if ([name isEqualToString: NSNamedColorSpace])
return [[self colorUsingColorSpaceName: NSDeviceRGBColorSpace] CGColor];
if (cgColorSpaceName == NULL)
return NULL;
CGColorSpaceRef colorSpace = CGColorSpaceCreateWithName(cgColorSpaceName);
CGFloat values[10];
[self getComponents: values];
CGColorRef color = CGColorCreate(colorSpace, values);
CFRelease(colorSpace);
return (CGColorRef)[(id)color autorelease];
}
@end
@implementation NSImageRep (GSQuartz)
- (CGImageRef)CGImageForProposedRect: (NSRect *)proposedDestRect
context: (NSGraphicsContext *)referenceContext
hints: (NSDictionary *)hints
{
/*
* FIXME Must implement this.
* A note for future implementors:
> Apparently each NSImageRep subclass implements this method, with the
> base implementation being[1], as I understand it (which may be wrong):
> i. create a new blank context with *proposedDestRect.size and set it
> as the current context. This context should theoretically be
> constructed with properties extracted from `referenceContext` and/or
> `hints`, although I suppose our first implementation can go without
> that.
> ii. call [self draw];
> iii. adjust *proposedDestRect to round half-pixels
> iv. extract a CGImage from the bitmap context
> If NSImage.size == *proposedDestRect.size, we should just CGImageCreate
> directly from our representation data.
*/
return NULL;
}
@end
@implementation NSImage (GSQuartz)
- (CGImageRef)CGImageForProposedRect: (NSRect *)proposedDestRect
context: (NSGraphicsContext *)referenceContext
hints: (NSDictionary *)hints
{
// FIXME: Must implement this.
// This should pick the best NSImageRep for this NSImage and call
// -[NSImageRep CGImageForProposedRect:...].
return NULL;
}
@end

View file

@ -0,0 +1,255 @@
/*
OpalContext.m
Copyright (C) 2013 Free Software Foundation, Inc.
Author: Ivan Vucica <ivan@vucica.net>
Date: June 2013
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
version 2 of the License, or (at your option) any later version.
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.
*/
#import <AppKit/NSBitmapImageRep.h>
#import <AppKit/NSGraphics.h>
#import "opal/OpalContext.h"
#import "opal/OpalFontInfo.h"
#import "opal/OpalFontEnumerator.h"
#import "opal/OpalSurface.h"
#import "opal/OpalGState.h"
#include "config.h"
#define OGSTATE ((OpalGState *)gstate)
#if BUILD_SERVER == SERVER_x11
# import "x11/XGServerWindow.h"
# import "x11/XWindowBuffer.h"
#endif
@implementation OpalContext
+ (void) initializeBackend
{
[NSGraphicsContext setDefaultContextClass: self];
[GSFontEnumerator setDefaultClass: [OpalFontEnumerator class]];
[GSFontInfo setDefaultClass: [OpalFontInfo class]];
}
+ (Class) GStateClass
{
return [OpalGState class];
}
- (BOOL) supportsDrawGState
{
return YES;
}
- (BOOL) isDrawingToScreen
{
#warning isDrawingToScreen returning NO to fix DPSimage
return NO;
// NOTE: This was returning NO because it was not looking at the
// return value of GSCurrentSurface. Now it returns YES, which
// seems to have broken image drawing (yellow rectangles are drawn instead)
OpalSurface *surface;
[OGSTATE GSCurrentSurface: &surface : NULL : NULL];
return [surface isDrawingToScreen];
}
- (void) flushGraphics
{
NSDebugLLog(@"OpalContext", @"%p (%@): %s", self, [self class], __PRETTY_FUNCTION__);
OpalSurface *surface;
[OGSTATE GSCurrentSurface: &surface : NULL : NULL];
CGContextFlush([surface CGContext]);
//[surface handleExposeRect: [surface size]];
}
/* Private backend methods */
/**
This handles 'expose' event notifications that arrive from
X11.
*/
+ (void) handleExposeRect: (NSRect)rect forDriver: (void *)driver
{
if ([(id)driver isKindOfClass: [OpalSurface class]])
{
[(OpalSurface *)driver handleExposeRect: rect];
}
}
#if BUILD_SERVER == SERVER_x11
#ifdef XSHM
+ (void) _gotShmCompletion: (Drawable)d
{
[XWindowBuffer _gotShmCompletion: d];
}
- (void) gotShmCompletion: (Drawable)d
{
[XWindowBuffer _gotShmCompletion: d];
}
#endif // XSHM
#endif // BUILD_SERVER = SERVER_x11
- (id) initWithGraphicsPort: (void *)port
flipped: (BOOL)flag;
{
self = [super initWithGraphicsPort: port
flipped: flag];
if (self != nil)
{
[self GSSetDevice: NULL : -1 : -1];
}
return self;
}
@end
@implementation OpalContext (Ops)
- (BOOL) isCompatibleBitmap: (NSBitmapImageRep*)bitmap
{
NSString *colorSpaceName;
if ([bitmap bitmapFormat] != 0)
{
return NO;
}
if ([bitmap isPlanar])
{
return NO;
}
if ([bitmap bitsPerSample] != 8)
{
return NO;
}
// FIXME: Allow more image types as soon as the Opal backend handles them correctly
colorSpaceName = [bitmap colorSpaceName];
if (![colorSpaceName isEqualToString: NSDeviceRGBColorSpace] &&
![colorSpaceName isEqualToString: NSCalibratedRGBColorSpace])
{
return NO;
}
else
{
return YES;
}
}
- (void) GSCurrentDevice: (void **)device : (int *)x : (int *)y
{
OpalSurface *surface;
[OGSTATE GSCurrentSurface: &surface : x : y];
if (device)
{
*device = [surface device];
}
}
- (void) GSSetDevice: (void *)device
: (int)x
: (int)y
{
OpalSurface *surface;
/*
* The "graphics port" associated to an OpalContext is necessarily a
* CGContextRef supplied by the client to back the OpalContext, instead
* of having us create the CGContextRef ourselves.
*
* Since -graphicsPort is overriden from NSGraphicsContext to compute the
* CGContextRef for an OpalSurface (which is not initialized yet), we
* get the _graphicsPort ivar directly to obtain the supplied CGContextRef
* on initialization, and use that to init our surface.
*/
CGContextRef suppliedContext = self->_graphicsPort;
surface = [[OpalSurface alloc] initWithDevice: device
context: suppliedContext];
if (x == -1 && y == -1)
{
NSSize size = [surface size];
x = 0;
y = size.height;
}
[OGSTATE GSSetSurface: surface
: x
: y];
[surface release];
}
- (void) DPSgsave
{
[OGSTATE DPSgsave];
[super DPSgsave];
}
- (void) DPSgrestore
{
[super DPSgrestore];
[OGSTATE DPSgrestore];
}
/** For information about this method, please see description of
i-var '_opGState' in OpalGState.h.
**/
- (void) DPSsetgstate: (int)gstateID
{
OPGStateRef previousGState = OPContextCopyGState([OGSTATE CGContext]);
[OGSTATE setOPGState: previousGState];
[previousGState release]; // FIXME
[super DPSsetgstate: gstateID];
OPGStateRef newGState = [OGSTATE OPGState];
if (newGState)
{
OPContextSetGState([OGSTATE CGContext], newGState);
[OGSTATE setOPGState: nil];
}
}
- (NSInteger) GSDefineGState
{
// FIXME
return [super GSDefineGState];
}
- (void *) graphicsPort
{
OpalSurface * surface;
[OGSTATE GSCurrentSurface: &surface : NULL : NULL];
return [surface CGContext];
}
@end

View file

@ -0,0 +1,62 @@
/*
OpalFaceInfo.m
Copyright (C) 2013 Free Software Foundation, Inc.
Author: Ivan Vucica <ivan@vucica.net>
Date: September 2013
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
version 2 of the License, or (at your option) any later version.
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.
*/
#import "opal/OpalFaceInfo.h"
@implementation OpalFaceInfo
- (void) dealloc
{
if (_fontFace)
{
CGFontRelease(_fontFace);
}
[super dealloc];
}
- (void *)fontFace
{
if (!_fontFace)
{
FcPattern *resolved;
resolved = [self matchedPattern];
_fontFace = OPFontCreateWithFcPattern(resolved);
FcPatternDestroy(resolved);
if (!_fontFace)
{
NSLog(@"Creating a font face failed %@", _familyName);
return NULL;
}
}
return _fontFace;
}
@end

View file

@ -0,0 +1,44 @@
/*
OpalFontEnumerator.m
Copyright (C) 2013 Free Software Foundation, Inc.
Author: Ivan Vucica <ivan@vucica.net>
Date: September 2013
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
version 2 of the License, or (at your option) any later version.
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.
*/
#import "opal/OpalFontEnumerator.h"
#import "opal/OpalFontInfo.h"
@implementation OpalFontEnumerator
+ (Class) faceInfoClass
{
return [OpalFaceInfo class];
}
+ (OpalFaceInfo *) fontWithName: (NSString *) name
{
return (OpalFaceInfo *) [super fontWithName: name];
}
@end

View file

@ -0,0 +1,558 @@
/*
OpalFontInfo.m
Copyright (C) 2013 Free Software Foundation, Inc.
Author: Ivan Vucica <ivan@vucica.net>
Date: September 2013
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
version 2 of the License, or (at your option) any later version.
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.
*/
#include "GNUstepBase/Unicode.h"
#include <AppKit/NSAffineTransform.h>
#include <AppKit/NSBezierPath.h>
#include "opal/OpalFontInfo.h"
#include "opal/OpalFontEnumerator.h"
#include <math.h>
/*
#include <cairo-ft.h>
*/
@implementation OpalFontInfo
- (CGFloat) _fontUnitToUserSpace: (CGFloat)fontDimension
{
CGFontRef face = [_faceInfo fontFace];
CGFloat unitsPerEm = CGFontGetUnitsPerEm(face);
CGFloat pointSize = matrix[0]; // from GSFontInfo
return (fontDimension / unitsPerEm) * pointSize;
}
- (BOOL) setupAttributes
{
/*
cairo_font_extents_t font_extents;
cairo_font_face_t *face;
cairo_matrix_t font_matrix;
cairo_matrix_t ctm;
cairo_font_options_t *options;
*/
CGFontRef face;
CGSize maximumAdvancementCG;
CGRect fontBBoxCG;
if (![super setupAttributes])
{
return NO;
}
#if 0
/* setting GSFontInfo:
* xHeight, pix_width, pix_height
*/
cairo_matrix_init(&font_matrix, matrix[0], matrix[1], -matrix[2],
matrix[3], matrix[4], matrix[5]);
//cairo_matrix_scale(&font_matrix, 0.9, 0.9);
cairo_matrix_init_identity(&ctm);
#endif
face = [_faceInfo fontFace];
if (!face)
{
return NO;
}
ascender = [self _fontUnitToUserSpace: CGFontGetAscent(face)];
descender = [self _fontUnitToUserSpace: CGFontGetDescent(face)];
xHeight = [self _fontUnitToUserSpace: CGFontGetXHeight(face)];
CGFloat pointSize = matrix[0];
maximumAdvancementCG = OPFontGetMaximumAdvancement(face);
maximumAdvancement = NSMakeSize(maximumAdvancementCG.width * pointSize,
maximumAdvancementCG.height * pointSize);
fontBBoxCG = CGFontGetFontBBox(face);
fontBBox = NSMakeRect([self _fontUnitToUserSpace: fontBBoxCG.origin.x],
[self _fontUnitToUserSpace: fontBBoxCG.origin.y],
[self _fontUnitToUserSpace: fontBBoxCG.size.width],
[self _fontUnitToUserSpace: fontBBoxCG.size.height]);
CGFloat leading = [self _fontUnitToUserSpace: CGFontGetLeading(face)];
if (xHeight == 0.0)
xHeight = ascender * 0.6;
// derived from code calculating CGFontGetLeading() value.
// we may instead want to extend Opal to include OPFontGetLineHeight(),
// containing this code:
// cairo_scaled_font_extents(_scaled, &font_extents);
// lineHeight = font_extents.height
// alternatively: line spacing = (ascent + descent + "external leading")
// (internal discussion between ivucica and ericwa, 2013-09-17)
lineHeight = leading + ascender + descender;
#if 0
// Get default font options
options = cairo_font_options_create();
if (cairo_font_options_status(options) != CAIRO_STATUS_SUCCESS)
{
return NO;
}
// We must not leave the hinting settings as their defaults,
// because if we did, that would mean using the surface defaults
// which might or might not use hinting (xlib does by default.)
//
// Since we make measurements outside of the context of a surface
// (-advancementForGlyph:), we need to ensure that the same
// hinting settings are used there as when we draw. For now,
// just force hinting to be off.
cairo_font_options_set_hint_metrics(options, CAIRO_HINT_METRICS_ON);
cairo_font_options_set_hint_style(options, CAIRO_HINT_STYLE_NONE);
_scaled = cairo_scaled_font_create(face, &font_matrix, &ctm, options);
cairo_font_options_destroy(options);
if (cairo_scaled_font_status(_scaled) != CAIRO_STATUS_SUCCESS)
{
return NO;
}
cairo_scaled_font_extents(_scaled, &font_extents);
if (cairo_scaled_font_status(_scaled) != CAIRO_STATUS_SUCCESS)
{
return NO;
}
ascender = font_extents.ascent;
descender = -font_extents.descent;
xHeight = ascender * 0.6;
lineHeight = font_extents.height;
maximumAdvancement = NSMakeSize(font_extents.max_x_advance,
font_extents.max_y_advance);
fontBBox = NSMakeRect(0, descender,
maximumAdvancement.width, ascender - descender);
/*
NSLog(@"Font matrix (%g, %g, %g, %g, %g, %g) type %d",
matrix[0], matrix[1], matrix[2],
matrix[3], matrix[4], matrix[5], cairo_scaled_font_get_type(_scaled));
NSLog(@"(%@) h=%g a=%g d=%g max=(%g %g) (%g %g)+(%g %g)\n", fontName,
xHeight, ascender, descender,
maximumAdvancement.width, maximumAdvancement.height,
fontBBox.origin.x, fontBBox.origin.y,
fontBBox.size.width, fontBBox.size.height);
*/
#endif
return YES;
}
- (id) initWithFontName: (NSString *)name
matrix: (const CGFloat *)fmatrix
screenFont: (BOOL)p_screenFont
{
self = [super init];
if (!self)
return nil;
_screenFont = p_screenFont;
fontName = [name copy];
memcpy(matrix, fmatrix, sizeof(matrix));
if (_screenFont)
{
/* Round up; makes the text more legible. */
matrix[0] = ceil(matrix[0]);
if (matrix[3] < 0.0)
matrix[3] = floor(matrix[3]);
else
matrix[3] = ceil(matrix[3]);
}
if (![self setupAttributes])
{
RELEASE(self);
return nil;
}
return self;
}
- (void) dealloc
{
#if 0
if (_scaled)
{
cairo_scaled_font_destroy(_scaled);
}
#endif
[super dealloc];
}
- (BOOL) glyphIsEncoded: (NSGlyph)glyph
{
CGFontRef face = [_faceInfo fontFace];
size_t numGlyphs = CGFontGetNumberOfGlyphs(face);
return glyph < numGlyphs;
}
#if 0
static
BOOL _cairo_extents_for_NSGlyph(cairo_scaled_font_t *scaled_font, NSGlyph glyph,
cairo_text_extents_t *ctext)
{
unichar ustr[2];
char str[4];
unsigned char *b;
unsigned int size = 4;
int length = 1;
ustr[0] = glyph;
ustr[1] = 0;
b = (unsigned char *)str;
if (!GSFromUnicode(&b, &size, ustr, length,
NSUTF8StringEncoding, NULL, GSUniTerminate))
{
NSLog(@"Conversion failed for %@",
[NSString stringWithCharacters: ustr length: length]);
return NO;
}
cairo_scaled_font_text_extents(scaled_font, str, ctext);
return cairo_scaled_font_status(scaled_font) == CAIRO_STATUS_SUCCESS;
}
#endif
- (NSSize) advancementForGlyph: (NSGlyph)glyph
{
CGFontRef face = [_faceInfo fontFace];
int advance = 0;
CGGlyph cgglyph = glyph;
CGFontGetGlyphAdvances(face, &cgglyph, 1, &advance);
CGFloat advanceUserSpace = [self _fontUnitToUserSpace: advance];
return NSMakeSize(advanceUserSpace, 0);
#if 0
cairo_text_extents_t ctext;
if (_cachedSizes)
{
int entry = glyph % _cacheSize;
if (_cachedGlyphs[entry] == glyph)
{
return _cachedSizes[entry];
}
if (_cairo_extents_for_NSGlyph(_scaled, glyph, &ctext))
{
_cachedGlyphs[entry] = glyph;
_cachedSizes[entry] = NSMakeSize(ctext.x_advance, ctext.y_advance);
return _cachedSizes[entry];
}
}
else
{
if (_cairo_extents_for_NSGlyph(_scaled, glyph, &ctext))
{
return NSMakeSize(ctext.x_advance, ctext.y_advance);
}
}
#endif
return NSZeroSize;
}
- (NSGlyph) glyphForCharacter: (unichar)theChar
{
CGFontRef face = [_faceInfo fontFace];
CGGlyph result = OPFontGetGlyphWithCharacter(face, theChar);
//NSLog(@"%s: Mapped '%@' to glyph # %d", __PRETTY_FUNCTION__, str, (int)result);
return result;
}
- (NSGlyph) glyphWithName: (NSString *) glyphName
{
CGFontRef face = [_faceInfo fontFace];
CGGlyph result = CGFontGetGlyphWithGlyphName(face, glyphName);
// NSLog(@"%s: Mapped '%@' to glyph # %d", __PRETTY_FUNCTION__, glyphName, (int)result);
return result;
}
- (NSRect) boundingRectForGlyph: (NSGlyph)glyph
{
#if 0
cairo_text_extents_t ctext;
if (_cairo_extents_for_NSGlyph(_scaled, glyph, &ctext))
{
return NSMakeRect(ctext.x_bearing, ctext.y_bearing,
ctext.width, ctext.height);
}
#endif
return NSMakeRect(0,0,10,10);
}
- (CGFloat) widthOfString: (NSString *)string
{
#if 0
cairo_text_extents_t ctext;
if (!string)
{
return 0.0;
}
cairo_scaled_font_text_extents(_scaled, [string UTF8String], &ctext);
if (cairo_scaled_font_status(_scaled) == CAIRO_STATUS_SUCCESS)
{
return ctext.width;
}
#endif
return 100.0;
}
- (void) appendBezierPathWithGlyphs: (NSGlyph *)glyphs
count: (int)length
toBezierPath: (NSBezierPath *)path
{
#if 0
cairo_format_t format = CAIRO_FORMAT_ARGB32;
cairo_surface_t *isurface;
cairo_t *ct;
int ix = 400;
int iy = 400;
unsigned char *cdata;
int i;
unichar ustr[length+1];
char str[3*length+1];
unsigned char *b;
unsigned int size = 3*length+1;
cairo_status_t status;
cairo_matrix_t font_matrix;
for (i = 0; i < length; i++)
{
ustr[i] = glyphs[i];
}
ustr[length] = 0;
b = (unsigned char *)str;
if (!GSFromUnicode(&b, &size, ustr, length,
NSUTF8StringEncoding, NULL, GSUniTerminate))
{
NSLog(@"Conversion failed for %@",
[NSString stringWithCharacters: ustr length: length]);
return;
}
cdata = malloc(sizeof(char) * 4 * ix * iy);
if (!cdata)
{
NSLog(@"Could not allocate drawing space for glyphs");
return;
}
isurface = cairo_image_surface_create_for_data(cdata, format, ix, iy, 4*ix);
status = cairo_surface_status(isurface);
if (status != CAIRO_STATUS_SUCCESS)
{
NSLog(@"Error while creating surface: %s",
cairo_status_to_string(status));
cairo_surface_destroy(isurface);
free(cdata);
return;
}
ct = cairo_create(isurface);
if (cairo_status(ct) != CAIRO_STATUS_SUCCESS)
{
NSLog(@"Error while creating context: %s",
cairo_status_to_string(cairo_status(ct)));
cairo_destroy(ct);
cairo_surface_destroy(isurface);
free(cdata);
return;
}
// Use flip matrix
cairo_matrix_init(&font_matrix, matrix[0], matrix[1], matrix[2],
-matrix[3], matrix[4], matrix[5]);
cairo_set_font_matrix(ct, &font_matrix);
if (cairo_status(ct) != CAIRO_STATUS_SUCCESS)
{
NSLog(@"Error while setting font matrix: %s",
cairo_status_to_string(cairo_status(ct)));
cairo_destroy(ct);
cairo_surface_destroy(isurface);
free(cdata);
return;
}
cairo_set_font_face(ct, [_faceInfo fontFace]);
if (cairo_status(ct) != CAIRO_STATUS_SUCCESS)
{
NSLog(@"Error while setting font face: %s",
cairo_status_to_string(cairo_status(ct)));
cairo_destroy(ct);
cairo_surface_destroy(isurface);
free(cdata);
return;
}
// Set font options from the scaled font
// FIXME: Instead of setting the matrix, setting the face, and setting
// the options, we should be using cairo_set_scaled_font
{
cairo_font_options_t *options = cairo_font_options_create();
cairo_scaled_font_get_font_options(_scaled, options);
cairo_set_font_options(ct, options);
cairo_font_options_destroy(options);
}
if (cairo_status(ct) != CAIRO_STATUS_SUCCESS)
{
NSLog(@"Error while setting font options: %s",
cairo_status_to_string(cairo_status(ct)));
cairo_destroy(ct);
cairo_surface_destroy(isurface);
free(cdata);
return;
}
if ([path elementCount] > 0)
{
NSPoint p;
p = [path currentPoint];
cairo_move_to(ct, floorf(p.x), floorf(p.y));
}
cairo_text_path(ct, str);
if (cairo_status(ct) == CAIRO_STATUS_SUCCESS)
{
cairo_path_t *cpath;
cairo_path_data_t *data;
cpath = cairo_copy_path(ct);
for (i = 0; i < cpath->num_data; i += cpath->data[i].header.length)
{
data = &cpath->data[i];
switch (data->header.type)
{
case CAIRO_PATH_MOVE_TO:
[path moveToPoint: NSMakePoint(data[1].point.x, data[1].point.y)];
break;
case CAIRO_PATH_LINE_TO:
[path lineToPoint: NSMakePoint(data[1].point.x, data[1].point.y)];
break;
case CAIRO_PATH_CURVE_TO:
[path curveToPoint: NSMakePoint(data[3].point.x, data[3].point.y)
controlPoint1: NSMakePoint(data[1].point.x, data[1].point.y)
controlPoint2: NSMakePoint(data[2].point.x, data[2].point.y)];
break;
case CAIRO_PATH_CLOSE_PATH:
[path closePath];
break;
}
}
cairo_path_destroy(cpath);
}
cairo_destroy(ct);
cairo_surface_destroy(isurface);
free(cdata);
#endif
}
#if 0
- (void) drawGlyphs: (const NSGlyph*)glyphs
length: (int)length
on: (cairo_t*)ct
{
cairo_matrix_t font_matrix;
unichar ustr[length+1];
char str[3*length+1];
unsigned char *b;
int i;
unsigned int size = 3*length+1;
for (i = 0; i < length; i++)
{
ustr[i] = glyphs[i];
}
ustr[length] = 0;
b = (unsigned char *)str;
if (!GSFromUnicode(&b, &size, ustr, length,
NSUTF8StringEncoding, NULL, GSUniTerminate))
{
NSLog(@"Conversion failed for %@",
[NSString stringWithCharacters: ustr length: length]);
return;
}
cairo_matrix_init(&font_matrix, matrix[0], matrix[1], -matrix[2],
matrix[3], matrix[4], matrix[5]);
cairo_set_font_matrix(ct, &font_matrix);
if (cairo_status(ct) != CAIRO_STATUS_SUCCESS)
{
NSLog(@"Error while setting font matrix: %s",
cairo_status_to_string(cairo_status(ct)));
return;
}
cairo_set_font_face(ct, [_faceInfo fontFace]);
if (cairo_status(ct) != CAIRO_STATUS_SUCCESS)
{
NSLog(@"Error while setting font face: %s",
cairo_status_to_string(cairo_status(ct)));
return;
}
// Set font options from the scaled font
// FIXME: Instead of setting the matrix, setting the face, and setting
// the options, we should be using cairo_set_scaled_font
{
cairo_font_options_t *options = cairo_font_options_create();
cairo_scaled_font_get_font_options(_scaled, options);
cairo_set_font_options(ct, options);
cairo_font_options_destroy(options);
}
if (cairo_status(ct) != CAIRO_STATUS_SUCCESS)
{
NSLog(@"Error while setting font options: %s",
cairo_status_to_string(cairo_status(ct)));
return;
}
cairo_show_text(ct, str);
if (cairo_status(ct) != CAIRO_STATUS_SUCCESS)
{
NSLog(@"Error drawing string: '%s' for string %s",
cairo_status_to_string(cairo_status(ct)), str);
}
}
#endif
@end

File diff suppressed because it is too large Load diff

View file

View file

View file

@ -0,0 +1,244 @@
/*
OpalSurface.m
Copyright (C) 2013 Free Software Foundation, Inc.
Author: Ivan Vucica <ivan@vucica.net>
Date: June 2013
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
version 2 of the License, or (at your option) any later version.
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.
*/
#import "opal/OpalSurface.h"
#import "x11/XGServerWindow.h"
/* TODO: expose these from within opal */
extern CGContextRef OPX11ContextCreate(Display *display, Drawable drawable);
extern void OPContextSetSize(CGContextRef ctx, CGSize s);
/* Taken from GSQuartzCore's CABackingStore */
static CGContextRef createCGBitmapContext(int pixelsWide,
int pixelsHigh)
{
CGContextRef context = NULL;
CGColorSpaceRef colorSpace;
int bitmapBytesPerRow;
bitmapBytesPerRow = (pixelsWide * 4);
colorSpace = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB);
// Let CGBitmapContextCreate() allocate the memory.
// This should be good under Cocoa too.
context = CGBitmapContextCreate(NULL,
pixelsWide,
pixelsHigh,
8, // bits per component
bitmapBytesPerRow,
colorSpace,
kCGBitmapByteOrder32Host | kCGImageAlphaPremultipliedFirst);
// Note: our use of premultiplied alpha means that we need to
// do alpha blending using:
// GL_SRC_ALPHA, GL_ONE
CGColorSpaceRelease(colorSpace);
if (context == NULL)
{
NSLog(@"Context not created!");
return NULL;
}
return context;
}
@implementation OpalSurface
- (void) createCGContextsWithSuppliedBackingContext: (CGContextRef)ctx
{
int pixelsWide;
int pixelsHigh;
// FIXME: this method and class presumes we are being passed
// a window device.
if (_x11CGContext || _backingCGContext)
{
NSLog(@"FIXME: Replacement of OpalSurface %p's CGContexts (x11=%p,backing=%p) without transfer of gstate", self, _x11CGContext, _backingCGContext);
}
if (ctx)
{
_x11CGContext = ctx;
pixelsWide = CGBitmapContextGetWidth(ctx);
pixelsHigh = CGBitmapContextGetHeight(ctx);
}
else
{
Display * display = _gsWindowDevice->display;
Window window = _gsWindowDevice->ident;
_x11CGContext = OPX11ContextCreate(display, window);
pixelsWide = _gsWindowDevice->buffer_width;
pixelsHigh = _gsWindowDevice->buffer_height;
// Ask XGServerWindow to call +[OpalContext handleExposeRect:forDriver:]
// to let us handle the back buffer -> front buffer copy using Opal.
_gsWindowDevice->gdriverProtocol |= GDriverHandlesExpose | GDriverHandlesBacking;
_gsWindowDevice->gdriver = self;
}
#if 0
if (_gsWindowDevice->type == NSBackingStoreNonretained)
{
// Don't double-buffer:
// use the window surface as the drawing destination.
}
else
#else
#warning All windows have to be doublebuffered
#endif
{
// Do double-buffer:
// Create a similar surface to the window which supports alpha
_backingCGContext = createCGBitmapContext(pixelsWide, pixelsHigh);
}
NSDebugLLog(@"OpalSurface", @"Created CGContexts: X11=%p, backing=%p, width=%d height=%d",
_x11CGContext, _backingCGContext, pixelsWide, pixelsHigh);
}
// FIXME: *VERY* bad things will happen if a non-bitmap
// context is passed here.
- (id) initWithDevice: (void *)device context: (CGContextRef)ctx
{
self = [super init];
if (!self)
return nil;
// FIXME: this method and class presumes we are being passed
// a window device.
_gsWindowDevice = (gswindow_device_t *) device;
[self createCGContextsWithSuppliedBackingContext: ctx];
return self;
}
- (void *) device
{
return _gsWindowDevice;
}
- (CGContextRef) CGContext
{
return _backingCGContext ? _backingCGContext : _x11CGContext;
}
- (CGContextRef) backingCGContext
{
return _backingCGContext;
}
- (CGContextRef) x11CGContext
{
return _x11CGContext;
}
- (void) handleExposeRect: (NSRect)rect
{
NSDebugLLog(@"OpalSurface", @"handleExposeRect %@", NSStringFromRect(rect));
if (!_backingCGContext)
{
return;
}
CGImageRef backingImage = CGBitmapContextCreateImage(_backingCGContext);
if (!backingImage) // FIXME: writing a nil image fails with Opal
return;
CGRect cgRect = CGRectMake(rect.origin.x, rect.origin.y,
rect.size.width, rect.size.height);
cgRect = CGRectIntegral(cgRect);
cgRect = CGRectIntersection(cgRect, CGRectMake(0, 0, CGImageGetWidth(backingImage), CGImageGetHeight(backingImage)));
CGRect subimageCGRect = cgRect;
CGImageRef subImage = CGImageCreateWithImageInRect(backingImage, subimageCGRect);
CGContextSaveGState(_x11CGContext);
OPContextResetClip(_x11CGContext);
OPContextSetIdentityCTM(_x11CGContext);
cgRect.origin.y = [self size].height - cgRect.origin.y - cgRect.size.height;
NSDebugLLog(@"OpalSurface", @" ... actually from %@ to %@", NSStringFromRect(*(NSRect *)&subimageCGRect), NSStringFromRect(*(NSRect *)&cgRect));
CGContextDrawImage(_x11CGContext, cgRect, subImage);
#if 0
#warning Saving debug images
[self _saveImage: backingImage withPrefix:@"/tmp/opalback-backing-" size: CGSizeZero];
[self _saveImage: subImage withPrefix:@"/tmp/opalback-subimage-" size: subimageCGRect.size ];
#endif
CGImageRelease(backingImage);
CGImageRelease(subImage);
CGContextRestoreGState(_x11CGContext);
}
- (void) _saveImage: (CGImageRef) img withPrefix: (NSString *) prefix size: (CGSize) size
{
#if 1
#warning Opal bug: cannot properly save subimage created with CGImageCreateWithImageInRect()
if (size.width != 0 || size.height != 0)
{
CGContextRef tmp = createCGBitmapContext(size.width, size.height);
CGContextDrawImage(tmp, CGRectMake(0, 0, size.width, size.height), img);
img = CGBitmapContextCreateImage(tmp);
[(id)img autorelease];
}
#endif
// FIXME: Opal tries to access -path from CFURLRef
//CFURLRef fileUrl = CFURLCreateWithFileSystemPath(NULL, @"/tmp/opalback.jpg", kCFURLPOSIXPathStyle, NO);
NSString * path = [NSString stringWithFormat: @"%@%dx%d.png", prefix, CGImageGetWidth(img), CGImageGetHeight(img)];
CFURLRef fileUrl = (CFURLRef)[[NSURL fileURLWithPath: path] retain];
NSLog(@"FileURL %@", fileUrl);
//CGImageDestinationRef outfile = CGImageDestinationCreateWithURL(fileUrl, @"public.jpeg"/*kUTTypeJPEG*/, 1, NULL);
CGImageDestinationRef outfile = CGImageDestinationCreateWithURL(fileUrl, @"public.png"/*kUTTypePNG*/, 1, NULL);
CGImageDestinationAddImage(outfile, img, NULL);
CGImageDestinationFinalize(outfile);
CFRelease(fileUrl);
CFRelease(outfile);
}
- (BOOL) isDrawingToScreen
{
// TODO: stub
return YES;
}
- (NSSize) size
{
return NSMakeSize(CGBitmapContextGetWidth(_backingCGContext),
CGBitmapContextGetHeight(_backingCGContext));
}
@end

View file

@ -0,0 +1,69 @@
# Copyright (C) 2013 Free Software Foundation, Inc.
#
# Author: Ivan Vucica <ivan@vucica.net>
#
# This file is part of the GNUstep Backend.
#
# 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
# version 2 of the License, or (at your option) any later version.
#
# 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.
PACKAGE_NAME = gnustep-back
GNUSTEP_LOCAL_ADDITIONAL_MAKEFILES=../../back.make
include $(GNUSTEP_MAKEFILES)/common.make
include ../../config.make
SUBPROJECT_NAME=opal
# The Objective-C source files to be compiled
opal_OBJC_FILES = OpalSurface.m \
OpalFontInfo.m \
OpalGState.m \
OpalContext.m \
OpalFontEnumerator.m \
OpalFaceInfo.m \
OpalPSSurface.m \
OpalPDFSurface.m \
OpalBridge.m \
../fontconfig/FCFaceInfo.m \
../fontconfig/FCFontEnumerator.m \
../fontconfig/FCFontInfo.m \
ifeq ($(BUILD_SERVER),x11DISABLED)
ifeq ($(WITH_GLITZ),yes)
opal_OBJC_FILES += XGCairoGlitzSurface.m
else
opal_OBJC_FILES += XGCairoSurface.m XGCairoXImageSurface.m XGCairoModernSurface.m
endif
else
ifeq ($(BUILD_GRAPHICS),opalDISABLED)
ifeq ($(WITH_GLITZ),yes)
opal_OBJC_FILES += Win32CairoGlitzSurface.m
else
opal_OBJC_FILES += Win32CairoSurface.m Win32CairoGState.m
# Win32CairoXImageSurface.m
endif
endif
endif
opal_OBJC_FILES +=
-include GNUmakefile.preamble
include $(GNUSTEP_MAKEFILES)/subproject.make
-include GNUmakefile.postamble

View file

@ -0,0 +1,52 @@
#
# GNUmakefile.preamble
#
# Copyright (C) 2002 Free Software Foundation, Inc.
#
# Author: Adam Fedor <fedor@gnu.org>
#
# This file is part of the GNUstep Backend.
#
# 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
# version 2 of the License, or (at your option) any later version.
#
# 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.
#
# Flags dealing with compiling and linking
#
# Additional flags to pass to the preprocessor
ADDITIONAL_CPPFLAGS += -Wall $(CONFIG_SYSTEM_DEFS)
# Additional flags to pass to the Objective-C compiler
#ADDITIONAL_OBJCFLAGS =
# Additional flags to pass to the C compiler
#ADDITIONAL_CFLAGS =
# Additional include directories the compiler should search
ADDITIONAL_INCLUDE_DIRS = -I../../Headers \
-I../$(GNUSTEP_TARGET_DIR) $(GRAPHIC_CFLAGS) \
# Additional LDFLAGS to pass to the linker
#ADDITIONAL_LDFLAGS =
# Additional library directories the linker should search
#ADDITIONAL_LIB_DIRS =
#
# Flags dealing with installing and uninstalling
#