forked from coo-ona/cooViewer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCOPDFImageRep.m
109 lines (90 loc) · 2.75 KB
/
COPDFImageRep.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#import "COPDFImageRep.h"
@implementation COPDFImageRep
-(id)init
{
self = [super init];
if (self) {
linkList = nil;
}
return self;
}
- (void)dealloc
{
if (linkList) {
[linkList release];
}
[super dealloc];
}
- (BOOL)drawInRect:(NSRect)rect
{
[[NSColor whiteColor] set];
NSRectFill(rect);
return [super drawInRect:rect];
}
- (BOOL)drawInRect:(NSRect)dstSpacePortionRect fromRect:(NSRect)srcSpacePortionRect operation:(NSCompositingOperation)op fraction:(CGFloat)requestedAlpha respectFlipped:(BOOL)respectContextIsFlipped hints:(nullable NSDictionary *)hints
{
[[NSColor whiteColor] set];
NSRectFill(dstSpacePortionRect);
return [super drawInRect:dstSpacePortionRect fromRect:srcSpacePortionRect operation:NSCompositeSourceOver fraction:requestedAlpha respectFlipped:respectContextIsFlipped hints:hints];
}
-(NSInteger)pixelsWide {return [self size].width;}
-(NSInteger)pixelsHigh{return [self size].height;}
+ (id)imageRepWithContentsOfFile:(NSString *)filename
{
id rep = [super imageRepWithContentsOfFile:filename];
#if MAC_OS_X_VERSION_MAX_ALLOWED >= 1040
if([NSObject respondsToSelector:@selector(finalize)]){
NSData *data = [NSData dataWithContentsOfFile:filename];
PDFPage *page;
NSArray *annotations;
PDFDocument *pdf = [[[PDFDocument alloc] initWithData:data] autorelease];
NSMutableArray *tmpArray = [[NSMutableArray alloc] init];
int pageIndex;
for (pageIndex = 0; pageIndex < [pdf pageCount]; pageIndex++) {
NSMutableArray *tmpTmpArray = [[NSMutableArray alloc] init];
page = [pdf pageAtIndex: pageIndex];
// Get page annotations (if any).
annotations = [page annotations];
if ((annotations != NULL) && ([annotations count] > 0)) {
unsigned int count;
unsigned int i;
// Walk annotations looking for links.
count = (int)[annotations count];
for (i = 0; i < count; i++)
{
PDFAnnotation *oneAnnotation;
// Link must have a URL associated with it.
oneAnnotation = [annotations objectAtIndex: i];
if (([[oneAnnotation type] isEqualToString: @"Link"]) &&
([(PDFAnnotationLink *)oneAnnotation URL] != NULL))
{
[tmpTmpArray addObject:[NSDictionary dictionaryWithObjectsAndKeys:
[NSValue valueWithRect:[oneAnnotation bounds]],@"rect",
[(PDFAnnotationLink *)oneAnnotation URL],@"url",
nil]];
}
}
}
[tmpArray addObject:tmpTmpArray];
[tmpTmpArray release];
}
[rep setLinkList:tmpArray];
[tmpArray release];
}
#endif
return rep;
}
-(void)setLinkList:(NSArray*)array;
{
linkList = [array retain];
}
-(NSArray*)linkListAtPage:(int) p
{
if (p<0) {
p = 0;
} else if (p>=[linkList count]) {
p = (int)[linkList count]-1;
}
return [linkList objectAtIndex:p];
}
@end