/
ASIWebPageThumbnailGenerator.m
241 lines (196 loc) · 6.14 KB
/
ASIWebPageThumbnailGenerator.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
//
// ASIWebPageThumbnailGenerator.m
//
// Created by Ben Copsey on 05/12/2008.
// Copyright 2008 All-Seeing Interactive. All rights reserved.
//
#import "ASIWebPageThumbnailGenerator.h"
@implementation ASIWebPageThumbnailGenerator
- (id)init
{
self = [super init];
[self setEnableJava:NO];
[self setEnablePlugins:YES];
[self setEnableJavaScript:YES];
[self setDestinationWidth:0];
[self setDestinationHeight:0];
[self setSourceWidth:0];
[self setSourceHeight:0];
[self setSourceX:0];
[self setSourceY:0];
[self setPageWidth:0];
[self setPageHeight:0];
return self;
}
- (void)dealloc
{
[url release];
[thumbnailSavePath release];
[webView release];
[window release];
[pageTitle release];
[super dealloc];
}
- (void)go
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
// Make an offscreen window
NSRect r = NSMakeRect(-2500,-2500,800,600);
[self setWindow:[[[NSWindow alloc] initWithContentRect:r styleMask:NSBorderlessWindowMask backing:NSBackingStoreBuffered defer:NO] autorelease]];
[self setWebView:[[[WebView alloc] initWithFrame:r frameName:nil groupName:nil] autorelease]];
WebPreferences *prefs = [[[WebPreferences alloc] initWithIdentifier:@"ThumbnailGenerator"] autorelease];
[prefs setJavaEnabled:[self enableJava]];
[prefs setJavaScriptEnabled:[self enableJavaScript]];
[prefs setPlugInsEnabled:[self enablePlugins]];
[webView setPreferences:prefs];
[window setContentView:webView];
[webView setHostWindow:window];
[[[webView mainFrame] frameView] setAllowsScrolling:NO];
[[webView mainFrame] loadRequest:[NSURLRequest requestWithURL:url]];
[webView setFrameLoadDelegate:self];
NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
// Record when the request started, so we can timeout if nothing happens
NSDate *startTime = [[NSDate date] retain];
// Wait for the request to finish
while (!complete) {
NSDate *now = [NSDate date];
// See if we need to timeout
if (timeoutSeconds > 0) {
if ([now timeIntervalSinceDate:startTime] > timeoutSeconds) {
[self timeoutRequest];
break;
}
}
[runLoop runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.5]];
[pool release];
pool = [[NSAutoreleasePool alloc] init];
}
[startTime release];
[pool release];
pool = nil;
}
- (void)timeoutRequest
{
if ([webView estimatedProgress] > 0.6) {
[self webView:webView didFinishLoadForFrame:[webView mainFrame]];
return;
}
failed = YES;
[self cleanUpWebPreview];
}
- (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame
{
if (frame != [sender mainFrame]) {
return;
}
NSView *docView = [[[sender mainFrame] frameView] documentView];
NSSize size = [docView bounds].size;
if (pageWidth > 0 && pageHeight > 0) {
size = NSMakeSize(pageWidth,pageHeight);
} else if (pageWidth > 0) {
size = NSMakeSize(pageWidth, size.height);
} else if (pageHeight > 0) {
size = NSMakeSize(size.width, pageHeight);
} else {
size = NSMakeSize(size.width+40,size.height+40);
}
if (size.width == 0 || size.height == 0) {
[self cleanUpWebPreview];
failed = YES;
[sender stopLoading:nil];
return;
}
[window setFrameOrigin:NSMakePoint(0-size.width-300, 0-size.height-300)];
[window setContentSize:size];
[[[sender mainFrame] frameView] scrollPoint:NSMakePoint(0,[docView bounds].size.height-size.height)];
[window display];
[window orderFront:nil];
[sender lockFocus];
NSBitmapImageRep *rep = [[[NSBitmapImageRep alloc] initWithFocusedViewRect:[sender bounds]] autorelease];
[sender unlockFocus];
if (rep) {
NSImage *img = [[[NSImage alloc] initWithData:[rep TIFFRepresentation]] autorelease];
[img setScalesWhenResized:YES];
[img lockFocus];
[[NSGraphicsContext currentContext] setImageInterpolation: NSImageInterpolationHigh];
[img unlockFocus];
float sW = sourceWidth;
if (sW == 0) {
sW = [img size].width;
}
float sH = sourceHeight;
if (sH == 0) {
sH = [img size].height;
}
double scale = 1;
NSSize thumbnailSize;
if (destinationWidth > 0 && destinationHeight > 0) {
thumbnailSize = NSMakeSize(destinationWidth,destinationHeight);
scale = destinationWidth/sW;
} else {
if (destinationWidth > 0) {
scale = destinationWidth/sW;
} else if (destinationHeight > 0) {
scale = destinationHeight/sH;
}
thumbnailSize = NSMakeSize(sW*scale,sH*scale);
}
NSImage *destinationImage = [[[NSImage alloc] initWithSize:thumbnailSize] autorelease];
[destinationImage lockFocus];
[[NSGraphicsContext currentContext] setImageInterpolation: NSImageInterpolationHigh];
NSRect r = NSMakeRect(sourceX, ([img size].height-(thumbnailSize.height/scale))-sourceY, thumbnailSize.width/scale, thumbnailSize.height/scale);
[img drawInRect:NSMakeRect(0,0,thumbnailSize.width,thumbnailSize.height) fromRect:r operation:NSCompositeSourceOver fraction:1.0];
[destinationImage unlockFocus];
NSData *data = [[NSBitmapImageRep imageRepWithData:[destinationImage TIFFRepresentation]] representationUsingType:NSPNGFileType properties:nil];
[data writeToFile:thumbnailSavePath atomically:NO];
}
[self cleanUpWebPreview];
}
- (void)webView:(WebView *)sender didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame
{
if (frame != [sender mainFrame]) {
return;
}
failed = YES;
[self cleanUpWebPreview];
}
- (void)webView:(WebView *)sender didFailLoadWithError:(NSError *)error forFrame:(WebFrame *)frame
{
if (frame != [sender mainFrame]) {
return;
}
failed = YES;
[self cleanUpWebPreview];
}
- (void)webView:(WebView *)sender didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame
{
[self setPageTitle:title];
}
- (void)cleanUpWebPreview
{
if (window) {
[webView stopLoading:nil];
[window orderOut:nil];
}
complete = YES;
}
@synthesize failed;
@synthesize complete;
@synthesize url;
@synthesize thumbnailSavePath;
@synthesize window;
@synthesize timeoutSeconds;
@synthesize webView;
@synthesize enableJava;
@synthesize enableJavaScript;
@synthesize enablePlugins;
@synthesize pageTitle;
@synthesize destinationWidth;
@synthesize destinationHeight;
@synthesize sourceWidth;
@synthesize sourceHeight;
@synthesize sourceX;
@synthesize sourceY;
@synthesize pageWidth;
@synthesize pageHeight;
@end