5 MSClassHook(UIWebDocumentView)
9 extern "C" void WebThreadLock();
10 extern "C" CGContextRef WKGetCurrentGraphicsContext();
12 static void (*WKViewLockFocus$)(WKView *);
13 static void (*WKViewUnlockFocus$)(WKView *);
14 static void (*WKViewDisplayRect$)(WKView *, CGRect);
16 @interface DOMElement : NSObject
17 - (void) setInnerHTML:(NSString *)value;
18 - (void) setInnerText:(NSString *)value;
19 - (void) setAttribute:(NSString *)name value:(NSString *)value;
20 - (void) removeAttribute:(NSString *)name;
21 - (DOMElement *) firstChild;
22 - (void) appendChild:(DOMElement *)child;
23 - (void) removeChild:(DOMElement *)child;
24 - (void) setScrollXOffset:(float)x scrollYOffset:(float)y;
27 @interface DOMDocument : NSObject
28 - (DOMElement *) getElementById:(NSString *)id;
31 @interface WebPreferences : NSObject
32 - (id) initWithIdentifier:(NSString *)identifier;
33 - (void) setPlugInsEnabled:(BOOL)value;
36 @interface WebFrameView : NSObject
37 - (void) setAllowsScrolling:(BOOL)value;
40 @interface WebFrame : NSObject
41 - (WebFrameView *) frameView;
42 - (void) _setLoadsSynchronously:(BOOL)value;
43 - (void) loadHTMLString:(NSString *)string baseURL:(id)url;
44 - (void) forceLayoutAdjustingViewSize:(BOOL)adjust;
45 - (CGSize) renderedSizeOfNode:(DOMElement *)node constrainedToWidth:(float)width;
46 - (DOMDocument *) DOMDocument;
49 @interface WAKView : NSObject
50 - (void) _drawRect:(CGRect)rect context:(CGContext *)context lockFocus:(bool)focus;
53 @interface WebView : WAKView
54 - (id) initWithFrame:(CGRect)frame;
55 - (WebFrame *) mainFrame;
56 - (void) setDrawsBackground:(BOOL)value;
57 - (void) setPreferences:(WebPreferences *)preferences;
58 - (WKView *) _viewRef;
61 @interface WAKWindow : NSObject
62 - (id) initWithFrame:(CGRect)frame;
63 - (void) setContentView:(WebView *)view;
66 @interface UIWebDocumentView : NSObject
67 - (WebView *) webView;
70 static WBMarkup *SharedMarkup_;
72 @implementation WBMarkup
75 MSImageRef WebCore(MSGetImageByName("/System/Library/PrivateFrameworks/WebCore.framework/WebCore"));
76 MSHookSymbol(WKViewLockFocus$, "_WKViewLockFocus", WebCore);
77 MSHookSymbol(WKViewUnlockFocus$, "_WKViewUnlockFocus", WebCore);
78 MSHookSymbol(WKViewDisplayRect$, "_WKViewDisplayRect", WebCore);
80 MSImageRef JavaScriptCore(MSGetImageByName("/System/Library/Frameworks/JavaScriptCore.framework/JavaScriptCore"));
82 void (*_ZN3JSC19initializeThreadingEv)();
83 MSHookSymbol(_ZN3JSC19initializeThreadingEv, "__ZN3JSC19initializeThreadingEv", JavaScriptCore);
84 if (_ZN3JSC19initializeThreadingEv != NULL)
85 (*_ZN3JSC19initializeThreadingEv)();
88 + (BOOL) isSharedMarkupCreated {
89 return SharedMarkup_ != nil;
92 + (WBMarkup *) sharedMarkup {
93 if (SharedMarkup_ == nil)
94 SharedMarkup_ = [[WBMarkup alloc] init];
99 if ((self = [super init]) != nil) {
102 if ($UIWebDocumentView == Nil)
103 view_ = [[WebView alloc] initWithFrame:CGRectMake(0, 0, 640, 5000)];
105 view_ = [[[$UIWebDocumentView alloc] initWithFrame:CGRectMake(0, 0, 640, 5000)] webView];
107 [view_ setDrawsBackground:NO];
109 WebPreferences *preferences([[WebPreferences alloc] initWithIdentifier:@"com.apple.webkit.webmarkup"]);
110 [preferences setPlugInsEnabled:NO];
111 [view_ setPreferences:preferences];
112 [preferences release];
114 window_ = [[WAKWindow alloc] initWithFrame:CGRectMake(0, 0, 640, 5000)];
115 [window_ setContentView:view_];
117 WebFrame *frame([view_ mainFrame]);
118 [[frame frameView] setAllowsScrolling:NO];
119 [frame _setLoadsSynchronously:YES];
121 [frame loadHTMLString:@"<html><body style='margin: 0px; word-wrap: break-word; -khtml-nbsp-mode: space; -khtml-line-break: after-white-space'><div id='size'><div id='text'></div></div></body></html>" baseURL:nil];
131 - (void) drawRect:(CGRect)rect {
132 [text_ setScrollXOffset:origin_.x scrollYOffset:origin_.y];
134 CGRect draw(CGRectMake(0, 0, rect.size.width - rect.origin.x, rect.size.height - rect.origin.y));
136 CGContextSaveGState(context_); {
137 CGContextTranslateCTM(context_, rect.origin.x, rect.origin.y);
139 if (kCFCoreFoundationVersionNumber > 700)
140 [view_ _drawRect:draw context:context_ lockFocus:YES];
142 WKView *view([view_ _viewRef]);
143 WKViewLockFocus$(view); {
144 WKViewDisplayRect$(view, draw);
145 } WKViewUnlockFocus$(view);
147 } CGContextRestoreGState(context_);
150 - (WebView *) _webView {
154 - (void) setStringDrawingOrigin:(CGPoint)origin {
158 - (void) clearStringDrawingOrigin {
159 origin_ = CGPointZero;
162 - (CGSize) sizeOfMarkup:(NSString *)markup forWidth:(CGFloat)width {
165 if (![self _webPrepareContextForTextDrawing:NO])
168 [text_ setInnerHTML:markup];
169 [text_ removeAttribute:@"style"];
171 NSString *value([[NSString alloc] initWithFormat:[self _styleFormatString:@"width: %.0fpx; height: 5000px"], width]);
172 [size_ setAttribute:@"style" value:value];
175 [[view_ mainFrame] forceLayoutAdjustingViewSize:YES];
176 return [[view_ mainFrame] renderedSizeOfNode:text_ constrainedToWidth:width];
179 - (CGSize) sizeOfString:(NSString *)string withStyle:(NSString *)style forWidth:(CGFloat)width {
182 if (![self _webPrepareContextForTextDrawing:NO])
185 [size_ removeChild:[size_ firstChild]];
187 WebFrame *frame([view_ mainFrame]);
189 [frame forceLayoutAdjustingViewSize:YES];
190 [text_ setInnerText:string];
191 [self _setupWithStyle:style width:width height:5000];
192 [frame forceLayoutAdjustingViewSize:YES];
194 return [[view_ mainFrame] renderedSizeOfNode:text_ constrainedToWidth:width];
197 - (NSString *) _styleFormatString:(NSString *)style {
201 - (void) _setupWithStyle:(NSString *)style width:(CGFloat)width height:(CGFloat)height {
204 if (style != nil && [style length] != 0)
205 [text_ setAttribute:@"style" value:style];
207 [text_ removeAttribute:@"style"];
209 NSString *value([[NSString alloc] initWithFormat:[self _styleFormatString:@"width: %.0fpx; height: %.0fpx"], width, height]);
210 [size_ setAttribute:@"style" value:value];
213 [size_ appendChild:text_];
216 - (BOOL) _webPrepareContextForTextDrawing:(BOOL)drawing {
219 if (document_ == nil) {
220 WebFrame *frame([view_ mainFrame]);
222 document_ = [[frame DOMDocument] retain];
223 if (document_ == nil) {
224 NSLog(@"*** ERROR: no DOM document in text-drawing webview");
228 text_ = [[document_ getElementById:@"text"] retain];
229 size_ = [[document_ getElementById:@"size"] retain];
231 if (text_ == nil || size_ == nil) {
232 NSLog(@"*** ERROR: cannot find DOM element required for text drawing");
242 context_ = WKGetCurrentGraphicsContext();
243 if (context_ == NULL) {
244 NSLog(@"*** ERROR: no CGContext set for drawing");
252 - (void) drawMarkup:(NSString *)markup atPoint:(CGPoint)point {
253 [self drawMarkup:markup inRect:CGRectMake(point.x, point.y, 65535, 65535)];
256 - (void) drawMarkup:(NSString *)markup inRect:(CGRect)rect {
259 if (![self _webPrepareContextForTextDrawing:YES])
262 [text_ setInnerHTML:markup];
263 [text_ removeAttribute:@"style"];
265 NSString *value([[NSString alloc] initWithFormat:[self _styleFormatString:@"width: %.0fpx; height: %.0fpx"], CGRectGetWidth(rect), CGRectGetHeight(rect)]);
266 [size_ setAttribute:@"style" value:value];
269 [[view_ mainFrame] forceLayoutAdjustingViewSize:YES];
271 [self drawRect:rect];
274 - (void) drawString:(NSString *)string atPoint:(CGPoint)point withStyle:(NSString *)style {
275 [self drawString:string inRect:CGRectMake(point.x, point.y, 65535, 65535) withStyle:style];
278 - (void) drawString:(NSString *)string inRect:(CGRect)rect withStyle:(NSString *)style {
281 if (![self _webPrepareContextForTextDrawing:YES])
284 [size_ removeChild:[size_ firstChild]];
286 WebFrame *frame([view_ mainFrame]);
288 [frame forceLayoutAdjustingViewSize:YES];
289 [text_ setInnerText:string];
290 [self _setupWithStyle:style width:CGRectGetWidth(rect) height:CGRectGetHeight(rect)];
291 [frame forceLayoutAdjustingViewSize:YES];
293 [self drawRect:rect];