-
Notifications
You must be signed in to change notification settings - Fork 91
/
GenerateThumbnailForURL.m
99 lines (82 loc) · 3.54 KB
/
GenerateThumbnailForURL.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
/* Copyright Nathaniel Gray */
#include <CoreFoundation/CoreFoundation.h>
#include <CoreServices/CoreServices.h>
#include <QuickLook/QuickLook.h>
#include <WebKit/WebKit.h>
#import "Common.h"
#define minSize 32
/* -----------------------------------------------------------------------------
Generate a thumbnail for file
This function's job is to create thumbnail for designated file as fast as possible
----------------------------------------------------------------------------- */
OSStatus
GenerateThumbnailForURL(void *thisInterface,
QLThumbnailRequestRef thumbnail,
CFURLRef url,
CFStringRef contentTypeUTI,
CFDictionaryRef options,
CGSize maxSize)
{
n8log(@"Generating Thumbnail");
// For some reason we seem to get called for small thumbnails even though
// we put a min size in our .plist file...
if (maxSize.width < minSize || maxSize.height < minSize)
return noErr;
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
#ifdef DEBUG
NSDate *startDate = [NSDate date];
#endif
// Render as though there is an 600x800 window, and fill the thumbnail
// vertically. This code could be more general. I'm assuming maxSize is
// a square, though nothing horrible should happen if it isn't.
NSRect renderRect = NSMakeRect(0.0, 0.0, 600.0, 800.0);
float scale = maxSize.height/800.0;
NSSize scaleSize = NSMakeSize(scale, scale);
CGSize thumbSize = NSSizeToCGSize(
NSMakeSize((maxSize.width * (600.0/800.0)),
maxSize.height));
/* Based on example code from quicklook-dev mailing list */
// NSSize previewSize = NSSizeFromCGSize(maxSize);
int status;
CFBundleRef bundle = QLThumbnailRequestGetGeneratorBundle(thumbnail);
NSData *data = colorizeURL(bundle, url, &status, 1);
//NSLog(@"%s", [data bytes]);
n8log(@"Generated thumbnail html page in %.3f sec", -[startDate timeIntervalSinceNow] );
if (status != 0) {
#ifndef DEBUG
goto done;
#endif
}
//NSRect previewRect;
//previewRect.size = previewSize;
WebView* webView = [[WebView alloc] initWithFrame:renderRect];
[webView scaleUnitSquareToSize:scaleSize];
[[[webView mainFrame] frameView] setAllowsScrolling:NO];
[[webView mainFrame] loadData:data MIMEType:@"text/html"
textEncodingName:@"UTF-8" baseURL:nil];
while([webView isLoading]) {
CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0, true);
}
// Get a context to render into
CGContextRef context =
QLThumbnailRequestCreateContext(thumbnail, thumbSize, false, NULL);
if(context != NULL) {
NSGraphicsContext* nsContext =
[NSGraphicsContext
graphicsContextWithGraphicsPort:(void *)context
flipped:[webView isFlipped]];
[webView displayRectIgnoringOpacity:[webView bounds]
inContext:nsContext];
QLThumbnailRequestFlushContext(thumbnail, context);
CFRelease(context);
}
done:
n8log(@"Finished thumbnail after %.3f sec\n\n", -[startDate timeIntervalSinceNow] );
[pool release];
return noErr;
}
void CancelThumbnailGeneration(void* thisInterface,
QLThumbnailRequestRef thumbnail)
{
// implement only if supported
}