1 /* Copyright (c) 2006-2007 Christopher J. W. Lloyd
3 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
5 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
7 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
9 #import <AppKit/NSPDFImageRep.h>
10 #import <AppKit/NSGraphicsContext.h>
11 #import <ApplicationServices/ApplicationServices.h>
13 @implementation NSPDFImageRep
15 +(NSArray *)imageUnfilteredFileTypes {
16 return [NSArray arrayWithObjects:@"pdf",nil];
19 +(NSArray *)imageRepsWithContentsOfFile:(NSString *)path {
20 NSMutableArray *result=[NSMutableArray array];
21 NSData *data=[NSData dataWithContentsOfFile:path];
26 if((pdf=[[[self alloc] initWithData:data] autorelease])==nil)
29 [result addObject:pdf];
34 -initWithData:(NSData *)data {
37 CGDataProviderRef provider=CGDataProviderCreateWithCFData((CFDataRef)_pdf);
38 _document=CGPDFDocumentCreateWithProvider(provider);
39 CGDataProviderRelease(provider);
45 CGPDFDocumentRelease(_document);
49 -copyWithZone:(NSZone *)zone {
50 NSPDFImageRep *result=[super copyWithZone:zone];
52 result->_pdf=[_pdf copy];
53 result->_document=CGPDFDocumentRetain(_document);
58 +(NSArray *)imageRepsWithData:(NSData *)data {
59 id result=[self imageRepWithData:data];
64 return [NSArray arrayWithObject:result];
67 +imageRepWithData:(NSData *)data {
68 return [[[self alloc] initWithData:data] autorelease];
71 -(NSData *)PDFRepresentation {
76 return CGPDFDocumentGetNumberOfPages(_document);
83 -(void)setCurrentPage:(int)page {
88 CGPDFPageRef page=CGPDFDocumentGetPage(_document,_currentPage+1);
89 NSRect mediaBox=CGPDFPageGetBoxRect(page,kCGPDFMediaBox);
95 CGContextRef context=[[NSGraphicsContext currentContext] graphicsPort];
96 CGPDFPageRef page=CGPDFDocumentGetPage(_document,_currentPage+1);
101 CGContextSaveGState(context);
102 CGContextDrawPDFPage(context,page);
103 CGContextRestoreGState(context);
107 -(BOOL)drawInRect:(NSRect)rect {
108 CGContextRef context=[[NSGraphicsContext currentContext] graphicsPort];
109 CGPDFPageRef page=CGPDFDocumentGetPage(_document,_currentPage+1);
114 CGContextSaveGState(context);
116 CGAffineTransform xform=CGPDFPageGetDrawingTransform(page,kCGPDFMediaBox,rect,0,NO);
118 CGContextConcatCTM(context,xform);
119 CGContextDrawPDFPage(context,page);
120 CGContextRestoreGState(context);