Merge pull request #10 from gunyarakun/fix-invalid-return
[cocotron.git] / AppKit / NSPDFImageRep.m
blob01e70b6856f60c58abb88bde360d5ca148a3e331
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];
22    NSPDFImageRep  *pdf;
23    
24    if(data==nil)
25     return nil;
26    if((pdf=[[[self alloc] initWithData:data] autorelease])==nil)
27     return nil;
28    
29    [result addObject:pdf];
30    
31    return result;
34 -initWithData:(NSData *)data {
35    _pdf=[data retain];
36    _currentPage=0;
37    CGDataProviderRef provider=CGDataProviderCreateWithCFData((CFDataRef)_pdf);
38    _document=CGPDFDocumentCreateWithProvider(provider);
39    CGDataProviderRelease(provider);
40    return self;
43 -(void)dealloc {
44    [_pdf release];
45    CGPDFDocumentRelease(_document);
46    [super dealloc];
49 -copyWithZone:(NSZone *)zone {
50    NSPDFImageRep *result=[super copyWithZone:zone];
51    
52    result->_pdf=[_pdf copy];
53    result->_document=CGPDFDocumentRetain(_document);
54    
55    return result;
58 +(NSArray *)imageRepsWithData:(NSData *)data {
59    id result=[self imageRepWithData:data];
60    
61    if(result==nil)
62     return nil;
63   
64    return [NSArray arrayWithObject:result];
67 +imageRepWithData:(NSData *)data {
68    return [[[self alloc] initWithData:data] autorelease];
71 -(NSData *)PDFRepresentation {
72    return _pdf;
75 -(int)pageCount {
76    return CGPDFDocumentGetNumberOfPages(_document);
79 -(int)currentPage {
80    return _currentPage;
83 -(void)setCurrentPage:(int)page {
84    _currentPage=page;
87 -(NSSize)size {
88    CGPDFPageRef page=CGPDFDocumentGetPage(_document,_currentPage+1);
89    NSRect       mediaBox=CGPDFPageGetBoxRect(page,kCGPDFMediaBox);
90       
91    return mediaBox.size;
94 -(BOOL)draw {
95    CGContextRef context=[[NSGraphicsContext currentContext] graphicsPort];
96    CGPDFPageRef page=CGPDFDocumentGetPage(_document,_currentPage+1);
98    if(page==nil)
99     return NO;
100    
101    CGContextSaveGState(context);
102    CGContextDrawPDFPage(context,page);
103    CGContextRestoreGState(context);
104    return YES;
107 -(BOOL)drawInRect:(NSRect)rect {
108    CGContextRef context=[[NSGraphicsContext currentContext] graphicsPort];
109    CGPDFPageRef page=CGPDFDocumentGetPage(_document,_currentPage+1);
111    if(page==nil)
112     return NO;
113    
114    CGContextSaveGState(context);
116    CGAffineTransform xform=CGPDFPageGetDrawingTransform(page,kCGPDFMediaBox,rect,0,NO);
117    
118    CGContextConcatCTM(context,xform);
119    CGContextDrawPDFPage(context,page);
120    CGContextRestoreGState(context);
121    return YES;
124 @end