]> git.saurik.com Git - winterboard.git/blobdiff - WBMarkup.mm
Pretend to implement this correctly, just for fun.
[winterboard.git] / WBMarkup.mm
index 9550114884d79a9be578008fc21d748bb4b2d888..ff7c98ab9b3f1aeede4a00988c498c372357da0d 100644 (file)
@@ -1,14 +1,17 @@
 #include "WBMarkup.h"
 
+#include <substrate.h>
+
+MSClassHook(UIWebDocumentView)
+
 @class WKView;
 
-extern "C" {
-    void WebThreadLock();
-    CGContextRef WKGetCurrentGraphicsContext();
-    void WKViewLockFocus(WKView *);
-    void WKViewUnlockFocus(WKView *);
-    void WKViewDisplayRect(WKView *, CGRect);
-}
+extern "C" void WebThreadLock();
+extern "C" CGContextRef WKGetCurrentGraphicsContext();
+
+static void (*WKViewLockFocus$)(WKView *);
+static void (*WKViewUnlockFocus$)(WKView *);
+static void (*WKViewDisplayRect$)(WKView *, CGRect);
 
 @interface DOMElement : NSObject
 - (void) setInnerHTML:(NSString *)value;
@@ -43,7 +46,11 @@ extern "C" {
 - (DOMDocument *) DOMDocument;
 @end
 
-@interface WebView : NSObject
+@interface WAKView : NSObject
+- (void) _drawRect:(CGRect)rect context:(CGContext *)context lockFocus:(bool)focus;
+@end
+
+@interface WebView : WAKView
 - (id) initWithFrame:(CGRect)frame;
 - (WebFrame *) mainFrame;
 - (void) setDrawsBackground:(BOOL)value;
@@ -56,10 +63,28 @@ extern "C" {
 - (void) setContentView:(WebView *)view;
 @end
 
+@interface UIWebDocumentView : NSObject
+- (WebView *) webView;
+@end
+
 static WBMarkup *SharedMarkup_;
 
 @implementation WBMarkup
 
++ (void) initialize {
+    MSImageRef WebCore(MSGetImageByName("/System/Library/PrivateFrameworks/WebCore.framework/WebCore"));
+    MSHookSymbol(WKViewLockFocus$, "_WKViewLockFocus", WebCore);
+    MSHookSymbol(WKViewUnlockFocus$, "_WKViewUnlockFocus", WebCore);
+    MSHookSymbol(WKViewDisplayRect$, "_WKViewDisplayRect", WebCore);
+
+    MSImageRef JavaScriptCore(MSGetImageByName("/System/Library/Frameworks/JavaScriptCore.framework/JavaScriptCore"));
+
+    void (*_ZN3JSC19initializeThreadingEv)();
+    MSHookSymbol(_ZN3JSC19initializeThreadingEv, "__ZN3JSC19initializeThreadingEv", JavaScriptCore);
+    if (_ZN3JSC19initializeThreadingEv != NULL)
+        (*_ZN3JSC19initializeThreadingEv)();
+}
+
 + (BOOL) isSharedMarkupCreated {
     return SharedMarkup_ != nil;
 }
@@ -74,9 +99,11 @@ static WBMarkup *SharedMarkup_;
     if ((self = [super init]) != nil) {
         WebThreadLock();
 
-        SharedMarkup_ = self;
+        if ($UIWebDocumentView == Nil)
+            view_ = [[WebView alloc] initWithFrame:CGRectMake(0, 0, 640, 5000)];
+        else
+            view_ = [[[$UIWebDocumentView alloc] initWithFrame:CGRectMake(0, 0, 640, 5000)] webView];
 
-        view_ = [[WebView alloc] initWithFrame:CGRectMake(0, 0, 640, 5000)];
         [view_ setDrawsBackground:NO];
 
         WebPreferences *preferences([[WebPreferences alloc] initWithIdentifier:@"com.apple.webkit.webmarkup"]);
@@ -101,6 +128,25 @@ static WBMarkup *SharedMarkup_;
     [super dealloc];
 }
 
+- (void) drawRect:(CGRect)rect {
+    [text_ setScrollXOffset:origin_.x scrollYOffset:origin_.y];
+
+    CGRect draw(CGRectMake(0, 0, rect.size.width - rect.origin.x, rect.size.height - rect.origin.y));
+
+    CGContextSaveGState(context_); {
+        CGContextTranslateCTM(context_, rect.origin.x, rect.origin.y);
+
+        if (kCFCoreFoundationVersionNumber > 700)
+            [view_ _drawRect:draw context:context_ lockFocus:YES];
+        else {
+            WKView *view([view_ _viewRef]);
+            WKViewLockFocus$(view); {
+                WKViewDisplayRect$(view, draw);
+            } WKViewUnlockFocus$(view);
+        }
+    } CGContextRestoreGState(context_);
+}
+
 - (WebView *) _webView {
     return view_;
 }
@@ -113,7 +159,7 @@ static WBMarkup *SharedMarkup_;
     origin_ = CGPointZero;
 }
 
-- (CGSize) sizeOfMarkup:(NSString *)markup forWidth:(float)width {
+- (CGSize) sizeOfMarkup:(NSString *)markup forWidth:(CGFloat)width {
     WebThreadLock();
 
     if (![self _webPrepareContextForTextDrawing:NO])
@@ -130,7 +176,7 @@ static WBMarkup *SharedMarkup_;
     return [[view_ mainFrame] renderedSizeOfNode:text_ constrainedToWidth:width];
 }
 
-- (CGSize) sizeOfString:(NSString *)string withStyle:(NSString *)style forWidth:(float)width {
+- (CGSize) sizeOfString:(NSString *)string withStyle:(NSString *)style forWidth:(CGFloat)width {
     WebThreadLock();
 
     if (![self _webPrepareContextForTextDrawing:NO])
@@ -152,7 +198,7 @@ static WBMarkup *SharedMarkup_;
     return style;
 }
 
-- (void) _setupWithStyle:(NSString *)style width:(float)width height:(float)height {
+- (void) _setupWithStyle:(NSString *)style width:(CGFloat)width height:(CGFloat)height {
     WebThreadLock();
 
     if (style != nil && [style length] != 0)
@@ -222,17 +268,7 @@ static WBMarkup *SharedMarkup_;
 
     [[view_ mainFrame] forceLayoutAdjustingViewSize:YES];
 
-    [text_ setScrollXOffset:origin_.x scrollYOffset:origin_.y];
-
-    WKView *view([view_ _viewRef]);
-
-    CGContextSaveGState(context_); {
-        CGContextTranslateCTM(context_, rect.origin.x, rect.origin.y);
-
-        WKViewLockFocus(view); {
-            WKViewDisplayRect(view, CGRectMake(0, 0, rect.origin.x, rect.origin.y));
-        } WKViewUnlockFocus(view);
-    } CGContextRestoreGState(context_);
+    [self drawRect:rect];
 }
 
 - (void) drawString:(NSString *)string atPoint:(CGPoint)point withStyle:(NSString *)style {
@@ -254,17 +290,7 @@ static WBMarkup *SharedMarkup_;
     [self _setupWithStyle:style width:CGRectGetWidth(rect) height:CGRectGetHeight(rect)];
     [frame forceLayoutAdjustingViewSize:YES];
 
-    [text_ setScrollXOffset:origin_.x scrollYOffset:origin_.y];
-
-    WKView *view([view_ _viewRef]);
-
-    CGContextSaveGState(context_); {
-        CGContextTranslateCTM(context_, rect.origin.x, rect.origin.y);
-
-        WKViewLockFocus(view); {
-            WKViewDisplayRect(view, CGRectMake(0, 0, rect.size.width, rect.size.height));
-        } WKViewUnlockFocus(view);
-    } CGContextRestoreGState(context_);
+    [self drawRect:rect];
 }
 
 @end