This repository has been archived by the owner on Feb 17, 2021. It is now read-only.
forked from toland/qlmarkdown
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GenerateThumbnailForURL.m
70 lines (54 loc) · 2.11 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
#import <QuickLook/QuickLook.h>
#import <Cocoa/Cocoa.h>
#import <WebKit/WebKit.h>
#include "markdown.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)
{
NSData *data = renderMarkdown((NSURL*) url);
if (data) {
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
}