-
Notifications
You must be signed in to change notification settings - Fork 1
/
PDFRendererPatch.m
150 lines (114 loc) · 3.9 KB
/
PDFRendererPatch.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
* RenderImage.m
* RenderImage
*
* Created by Christopher Wright on 1/10/09.
* Copyright (c) 2009 Kosada Incorporated. All rights reserved.
*
*/
#import "PDFRendererPatch.h"
//#import "QCImage.h"
@implementation PDFRendererPatch : QCPatch
+ (int)executionModeWithIdentifier:(id)fp8
{
return 0;
}
+ (BOOL)allowsSubpatchesWithIdentifier:(id)fp8
{
return NO;
}
+ (int)timeModeWithIdentifier:(id)fp8
{
return 0;
}
- (id)initWithIdentifier:(id)fp8
{
if(self=[super initWithIdentifier:fp8])
{
[inputWidth setIndexValue: 800];
[inputHeight setIndexValue: 600];
[inputPageNumber setIndexValue: 1];
}
return self;
}
- (id)attributes
{
NSMutableDictionary *at = [[[super attributes] mutableCopy] autorelease];
[at setObject:@"Kineme PDF Renderer" forKey:@"name"];
return at;
}
/*- (BOOL)setup:(QCOpenGLContext *)context
{
return YES;
}
- (void)cleanup:(QCOpenGLContext *)context
{
}
- (void)enable:(QCOpenGLContext *)context
{
}
- (void)disable:(QCOpenGLContext *)context
{
}*/
- (BOOL)execute:(QCOpenGLContext *)context time:(double)time arguments:(NSDictionary *)arguments
{
if([[inputPath stringValue] length] > 0 && [inputWidth indexValue] && [inputHeight indexValue])
{
CFURLRef url;
url = CFURLCreateWithFileSystemPath (NULL, (CFStringRef)KIExpandPath(self, [inputPath stringValue]), kCFURLPOSIXPathStyle, 0);
if(!url)
return YES;
CGPDFDocumentRef document = CGPDFDocumentCreateWithURL(url);
int count = CGPDFDocumentGetNumberOfPages(document);
unsigned int width = [inputWidth indexValue];
unsigned int height = [inputHeight indexValue];
if(count)
{
[outputPageCount setIndexValue: count];
[inputPageNumber setMaxIndexValue: count];
// render specified page
CGPDFPageRef page;
page = CGPDFDocumentGetPage (document, [inputPageNumber indexValue]);
CGRect artbox = CGPDFPageGetBoxRect(page, kCGPDFMediaBox);
CGRect rect;
if([inputPreserveAspect booleanValue])
{
double aspect = artbox.size.width/artbox.size.height;
double outAspect = (double)width/(double)height;
//NSLog(@"pdf size: %fx%f (%f)", artbox.size.width, artbox.size.height, aspect);
if(aspect > outAspect) // too wide, width gets trimmed
width = height * aspect;
else if (aspect < outAspect) // too tall, height gets trimmed
height = width / aspect;
//NSLog(@"new size: %fx%f (%f vs. %f)", (double)width, (double)height, (double)width/(double)height, aspect);
}
rect = CGRectMake(0, 0, width, height);
// calloc, since we need a clear context (otherwise, small contexts get garbage left in them)
void *data = calloc(width * height * 4, 1);
CGColorSpaceRef colorSpace = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB);
CGContextRef ctx = CGBitmapContextCreate(data, width, height, 8, width*4, colorSpace, kCGImageAlphaPremultipliedFirst);
CGColorSpaceRelease(colorSpace);
/* cook the transform to fill the context
Note: CGPDFPageGetDrawingTransform does NOT upscale! ("for historical reasons") -- this makes uniform up/down scaling rather
aggravating...
sample code that shouldn't suck, but does (only down-scales):
CGAffineTransform m = CGPDFPageGetDrawingTransform (page, kCGPDFMediaBox, rect, 0, [inputPreserveAspect booleanValue]);
CGContextConcatCTM(ctx, m);*/
CGContextTranslateCTM(ctx, rect.origin.x, rect.origin.y);
CGContextScaleCTM(ctx, rect.size.width / artbox.size.width, rect.size.height / artbox.size.height);
CGContextTranslateCTM(ctx, -artbox.origin.x, -artbox.origin.y);
CGContextDrawPDFPage(ctx, page);
CGImageRef cgImage = CGBitmapContextCreateImage(ctx);
CGContextRelease(ctx);
QCImage *img = [[QCImage alloc] initWithCGImage: cgImage options: nil];
[outputImage setImageValue: img];
[img release];
CGImageRelease(cgImage);
free(data);
}
CFRelease(url);
CGPDFDocumentRelease(document);
}
return YES;
}
@end