Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

78 lines (59 sloc) 2.311 kb
#import <QuickLook/QuickLook.h>
#import <Cocoa/Cocoa.h>
#import <WebKit/WebKit.h>
#include "RestConverter.h"
/* -----------------------------------------------------------------------------
Generate a thumbnail for file
This function's job is to create thumbnail for designated file as fast as
possible
-------------------------------------------------------------------------- */
//The minimum aspect ratio (width / height) of a thumbnail.
#define MINIMUM_ASPECT_RATIO (1.0/2.0)
OSStatus GenerateThumbnailForURL(void *thisInterface,
QLThumbnailRequestRef thumbnail,
CFURLRef url, CFStringRef contentTypeUTI,
CFDictionaryRef options, CGSize maxSize)
{
// TODO: Fallback to plaintext rendering if rest rendering fails?
NSString *source = [NSString stringWithContentsOfFile:[url path] encoding:NSUTF8StringEncoding error:nil];
NSData *data = renderRest(source);
NSString *mimetype = @"text/html";
if (!data) {
data = (CFDataRef) [source dataUsingEncoding:NSUTF8StringEncoding];
mimetype = @"text/plain";
}
NSRect viewRect = 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));
WebView* webView = [[WebView alloc] initWithFrame: viewRect];
[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);
}
[webView display];
CGContextRef context =
QLThumbnailRequestCreateContext(thumbnail, thumbSize, false, NULL);
if (context) {
NSGraphicsContext* nsContext =
[NSGraphicsContext
graphicsContextWithGraphicsPort: (void*) context
flipped: [webView isFlipped]];
[webView displayRectIgnoringOpacity: [webView bounds]
inContext: nsContext];
QLThumbnailRequestFlushContext(thumbnail, context);
CFRelease(context);
}
return noErr;
}
void CancelThumbnailGeneration(void* thisInterface, QLThumbnailRequestRef thumbnail)
{
// implement only if supported
}
Jump to Line
Something went wrong with that request. Please try again.