Skip to content

Commit

Permalink
Now implements pasteboard protocol and supports PDF generation
Browse files Browse the repository at this point in the history
  • Loading branch information
curthard89 committed Sep 6, 2014
1 parent 1563e26 commit fab2755
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 11 deletions.
3 changes: 2 additions & 1 deletion source/IJSVG.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ handleForeignObject:(IJSVGForeignObject *)foreignObject

@end

@interface IJSVG : NSObject {
@interface IJSVG : NSObject <NSPasteboardWriting> {

@private
IJSVGParser * _group;
Expand All @@ -50,5 +50,6 @@ handleForeignObject:(IJSVGForeignObject *)foreignObject
size:(NSSize)size;
- (void)drawInRect:(NSRect)rect;
- (NSArray *)colors;
- (NSData *)PDFData;

@end
93 changes: 83 additions & 10 deletions source/IJSVG.m
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,15 @@ + (void)setBaseColor:(NSColor *)color
{
if( _baseColor != nil )
[_baseColor release], _baseColor = nil;
_baseColor = [color retain];
if( color == nil )
_baseColor = nil;
else
_baseColor = [color retain];
}

+ (NSColor *)baseColor
{
return _baseColor;
return [[_baseColor copy] autorelease];
}

+ (id)svgNamed:(NSString *)string
Expand Down Expand Up @@ -116,6 +119,53 @@ - (NSImage *)imageWithSize:(NSSize)aSize
return im;
}

- (NSData *)PDFData
{
NSColor * oldBaseColour = [[self class] baseColor];
[[self class] setBaseColor:nil];
// store the old context
NSGraphicsContext * oldGraphicsContext = [NSGraphicsContext currentContext];

// create the data for the PDF
NSMutableData * data = [[[NSMutableData alloc] init] autorelease];

// assign the data to the consumer
CGDataConsumerRef dataConsumer = CGDataConsumerCreateWithCFData((CFMutableDataRef)data);
const CGRect box = CGRectMake( 0.f, 0.f, _group.size.width, _group.size.height );

// create the context
CGContextRef context = CGPDFContextCreate( dataConsumer, &box, NULL );
NSGraphicsContext * newContext = [NSGraphicsContext graphicsContextWithGraphicsPort:context
flipped:NO];

CGContextSaveGState(context);

// set it as the current
[NSGraphicsContext setCurrentContext:newContext];
CGContextBeginPage( context, &box );

// the context is currently upside down, doh! flip it...
CGContextScaleCTM( context, 1, -1 );
CGContextTranslateCTM( context, 0, -box.size.height);

// draw the icon
[self _drawInRect:(NSRect)box
context:context];
CGContextEndPage(context);

//clean up
CGPDFContextClose(context);
CGContextRelease(context);
CGDataConsumerRelease(dataConsumer);

CGContextRestoreGState(context);

// set the graphics context back to its original
[NSGraphicsContext setCurrentContext:oldGraphicsContext];
[[self class] setBaseColor:oldBaseColour];
return data;
}

- (NSArray *)colors
{
if( _colors == nil )
Expand All @@ -134,12 +184,17 @@ - (void)drawAtPoint:(NSPoint)point

- (void)drawInRect:(NSRect)rect
{

[self _drawInRect:rect
context:[[NSGraphicsContext currentContext] graphicsPort]];
}

- (void)_drawInRect:(NSRect)rect
context:(CGContextRef)ref
{
// prep for draw...
[self _beginDraw:rect];

// setup the transforms and scale on the main context
CGContextRef ref = [[NSGraphicsContext currentContext] graphicsPort];
CGContextSaveGState(ref);
{

Expand All @@ -156,7 +211,8 @@ - (void)drawInRect:(NSRect)rect

// begin draw
[self _drawGroup:_group
rect:rect];
rect:rect
context:ref];

}
CGContextRestoreGState(ref);
Expand Down Expand Up @@ -215,7 +271,8 @@ - (void)_prepClip:(IJSVGNode *)node
if( [clip isKindOfClass:[IJSVGGroup class]] )
{
[self _drawGroup:clip
rect:rect];
rect:rect
context:context];
} else {

// add the clip and draw
Expand All @@ -237,8 +294,8 @@ - (void)_prepClip:(IJSVGNode *)node

- (void)_drawGroup:(IJSVGGroup *)group
rect:(NSRect)rect
context:(CGContextRef)context
{
CGContextRef context = [[NSGraphicsContext currentContext] graphicsPort];
CGContextSaveGState( context );
{

Expand All @@ -256,7 +313,8 @@ - (void)_drawGroup:(IJSVGGroup *)group
dispatch_block_t block = ^(void)
{
[self _drawPath:(IJSVGPath *)child
rect:rect];
rect:rect
context:context];
};

// draw the clip
Expand All @@ -270,7 +328,8 @@ - (void)_drawGroup:(IJSVGGroup *)group
// if its a group, we recursively call this method
// to generate the paths required
[self _drawGroup:child
rect:rect];
rect:rect
context:context];
}
}

Expand Down Expand Up @@ -309,10 +368,10 @@ - (void)_applyDefaults:(CGContextRef)context

- (void)_drawPath:(IJSVGPath *)path
rect:(NSRect)rect
context:(CGContextRef)ref
{
// there should be a colour on it...
// defaults to black if not existant
CGContextRef ref = [[NSGraphicsContext currentContext] graphicsPort];
CGContextSaveGState(ref);
{
// there could be transforms per path
Expand Down Expand Up @@ -385,6 +444,20 @@ - (void)_drawPath:(IJSVGPath *)path

}

#pragma mark NSPasteboard

- (NSArray *)writableTypesForPasteboard:(NSPasteboard *)pasteboard
{
return @[NSPasteboardTypePDF];
}

- (id)pasteboardPropertyListForType:(NSString *)type
{
if( [type isEqualToString:NSPasteboardTypePDF] )
return [self PDFData];
return nil;
}

#pragma mark IJSVGParserDelegate

- (BOOL)svgParser:(IJSVGParser *)parser
Expand Down

0 comments on commit fab2755

Please sign in to comment.