1 #import "HTMLRenderer.h"
6 #define Log(fmt, ...) NSLog(fmt, ##__VA_ARGS__)
11 @implementation HTMLRenderer
12 + (NSAttributedString *)attributedStringWithURL:(NSURL *)url {
13 HTMLRenderer *htmlRenderer = [[HTMLRenderer alloc] init];
14 NSAttributedString *attributedString = [htmlRenderer attributedStringWithURL:url];
15 [htmlRenderer release];
16 return attributedString;
20 if((self = [super init]) == nil)
23 renderLock = [[NSConditionLock alloc] initWithCondition:0];
24 finishedRendering = 0;
25 renderWebView = [[WebView alloc] initWithFrame:NSMakeRect(0., 0., 2000., 100.) frameName:nil groupName:nil];
26 [renderWebView setFrameLoadDelegate:self];
31 - (NSAttributedString *)attributedStringWithURL:(NSURL *)url {
32 NSMutableAttributedString *result = nil;
34 finishedRendering = 0;
35 [self renderHTMLFragment:url];
37 if(finishedRendering == 1)
38 return [(id <WebDocumentText>)[[[renderWebView mainFrame] frameView] documentView] attributedString];
43 - (void)renderHTMLFragment:(NSURL *)url {
44 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
45 NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
47 [NSThread setThreadPriority:1.0];
50 [[renderWebView mainFrame] loadRequest:[NSURLRequest requestWithURL:url]];
52 while(!finishedRendering)
53 [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.05]];
55 [renderLock unlockWithCondition:1];
61 [renderWebView release];
65 - (void)webView:(WebView *)sender didFailLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
66 finishedRendering = -1;
69 - (void)webView:(WebView *)sender didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
70 finishedRendering = -1;
73 - (void)webView:(WebView *) sender didFinishLoadForFrame:(WebFrame *) frame {
74 finishedRendering = 1;