7 extern "C" void WebThreadLock();
8 extern "C" CGContextRef WKGetCurrentGraphicsContext();
10 static void (*WKViewLockFocus$)(WKView *);
11 static void (*WKViewUnlockFocus$)(WKView *);
12 static void (*WKViewDisplayRect$)(WKView *, CGRect);
14 @interface DOMElement : NSObject
15 - (void) setInnerHTML:(NSString *)value;
16 - (void) setInnerText:(NSString *)value;
17 - (void) setAttribute:(NSString *)name value:(NSString *)value;
18 - (void) removeAttribute:(NSString *)name;
19 - (DOMElement *) firstChild;
20 - (void) appendChild:(DOMElement *)child;
21 - (void) removeChild:(DOMElement *)child;
22 - (void) setScrollXOffset:(float)x scrollYOffset:(float)y;
25 @interface DOMDocument : NSObject
26 - (DOMElement *) getElementById:(NSString *)id;
29 @interface WebPreferences : NSObject
30 - (id) initWithIdentifier:(NSString *)identifier;
31 - (void) setPlugInsEnabled:(BOOL)value;
34 @interface WebFrameView : NSObject
35 - (void) setAllowsScrolling:(BOOL)value;
38 @interface WebFrame : NSObject
39 - (WebFrameView *) frameView;
40 - (void) _setLoadsSynchronously:(BOOL)value;
41 - (void) loadHTMLString:(NSString *)string baseURL:(id)url;
42 - (void) forceLayoutAdjustingViewSize:(BOOL)adjust;
43 - (CGSize) renderedSizeOfNode:(DOMElement *)node constrainedToWidth:(float)width;
44 - (DOMDocument *) DOMDocument;
47 @interface WAKView : NSObject
48 - (void) _drawRect:(CGRect)rect context:(CGContext *)context lockFocus:(bool)focus;
51 @interface WebView : WAKView
52 - (id) initWithFrame:(CGRect)frame;
53 - (WebFrame *) mainFrame;
54 - (void) setDrawsBackground:(BOOL)value;
55 - (void) setPreferences:(WebPreferences *)preferences;
56 - (WKView *) _viewRef;
59 @interface WAKWindow : NSObject
60 - (id) initWithFrame:(CGRect)frame;
61 - (void) setContentView:(WebView *)view;
64 static WBMarkup *SharedMarkup_;
66 @implementation WBMarkup
69 MSImageRef WebCore(MSGetImageByName("/System/Library/PrivateFrameworks/WebCore.framework/WebCore"));
70 MSHookSymbol(WKViewLockFocus$, "WKViewLockFocus", WebCore);
71 MSHookSymbol(WKViewUnlockFocus$, "WKViewUnlockFocus", WebCore);
72 MSHookSymbol(WKViewDisplayRect$, "WKViewDisplayRect", WebCore);
75 + (BOOL) isSharedMarkupCreated {
76 return SharedMarkup_ != nil;
79 + (WBMarkup *) sharedMarkup {
80 if (SharedMarkup_ == nil)
81 SharedMarkup_ = [[WBMarkup alloc] init];
86 if ((self = [super init]) != nil) {
91 view_ = [[WebView alloc] initWithFrame:CGRectMake(0, 0, 640, 5000)];
92 [view_ setDrawsBackground:NO];
94 WebPreferences *preferences([[WebPreferences alloc] initWithIdentifier:@"com.apple.webkit.webmarkup"]);
95 [preferences setPlugInsEnabled:NO];
96 [view_ setPreferences:preferences];
97 [preferences release];
99 window_ = [[WAKWindow alloc] initWithFrame:CGRectMake(0, 0, 640, 5000)];
100 [window_ setContentView:view_];
102 WebFrame *frame([view_ mainFrame]);
103 [[frame frameView] setAllowsScrolling:NO];
104 [frame _setLoadsSynchronously:YES];
106 [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];
116 - (void) drawRect:(CGRect)rect {
117 [text_ setScrollXOffset:origin_.x scrollYOffset:origin_.y];
119 CGRect draw(CGRectMake(0, 0, rect.size.width - rect.origin.x, rect.size.height - rect.origin.y));
121 CGContextSaveGState(context_); {
122 CGContextTranslateCTM(context_, rect.origin.x, rect.origin.y);
124 if (kCFCoreFoundationVersionNumber > 700)
125 [view_ _drawRect:draw context:context_ lockFocus:YES];
127 WKView *view([view_ _viewRef]);
128 WKViewLockFocus$(view); {
129 WKViewDisplayRect$(view, draw);
130 } WKViewUnlockFocus$(view);
132 } CGContextRestoreGState(context_);
135 - (WebView *) _webView {
139 - (void) setStringDrawingOrigin:(CGPoint)origin {
143 - (void) clearStringDrawingOrigin {
144 origin_ = CGPointZero;
147 - (CGSize) sizeOfMarkup:(NSString *)markup forWidth:(float)width {
150 if (![self _webPrepareContextForTextDrawing:NO])
153 [text_ setInnerHTML:markup];
154 [text_ removeAttribute:@"style"];
156 NSString *value([[NSString alloc] initWithFormat:[self _styleFormatString:@"width: %.0fpx; height: 5000px"], width]);
157 [size_ setAttribute:@"style" value:value];
160 [[view_ mainFrame] forceLayoutAdjustingViewSize:YES];
161 return [[view_ mainFrame] renderedSizeOfNode:text_ constrainedToWidth:width];
164 - (CGSize) sizeOfString:(NSString *)string withStyle:(NSString *)style forWidth:(float)width {
167 if (![self _webPrepareContextForTextDrawing:NO])
170 [size_ removeChild:[size_ firstChild]];
172 WebFrame *frame([view_ mainFrame]);
174 [frame forceLayoutAdjustingViewSize:YES];
175 [text_ setInnerText:string];
176 [self _setupWithStyle:style width:width height:5000];
177 [frame forceLayoutAdjustingViewSize:YES];
179 return [[view_ mainFrame] renderedSizeOfNode:text_ constrainedToWidth:width];
182 - (NSString *) _styleFormatString:(NSString *)style {
186 - (void) _setupWithStyle:(NSString *)style width:(float)width height:(float)height {
189 if (style != nil && [style length] != 0)
190 [text_ setAttribute:@"style" value:style];
192 [text_ removeAttribute:@"style"];
194 NSString *value([[NSString alloc] initWithFormat:[self _styleFormatString:@"width: %.0fpx; height: %.0fpx"], width, height]);
195 [size_ setAttribute:@"style" value:value];
198 [size_ appendChild:text_];
201 - (BOOL) _webPrepareContextForTextDrawing:(BOOL)drawing {
204 if (document_ == nil) {
205 WebFrame *frame([view_ mainFrame]);
207 document_ = [[frame DOMDocument] retain];
208 if (document_ == nil) {
209 NSLog(@"*** ERROR: no DOM document in text-drawing webview");
213 text_ = [[document_ getElementById:@"text"] retain];
214 size_ = [[document_ getElementById:@"size"] retain];
216 if (text_ == nil || size_ == nil) {
217 NSLog(@"*** ERROR: cannot find DOM element required for text drawing");
227 context_ = WKGetCurrentGraphicsContext();
228 if (context_ == NULL) {
229 NSLog(@"*** ERROR: no CGContext set for drawing");
237 - (void) drawMarkup:(NSString *)markup atPoint:(CGPoint)point {
238 [self drawMarkup:markup inRect:CGRectMake(point.x, point.y, 65535, 65535)];
241 - (void) drawMarkup:(NSString *)markup inRect:(CGRect)rect {
244 if (![self _webPrepareContextForTextDrawing:YES])
247 [text_ setInnerHTML:markup];
248 [text_ removeAttribute:@"style"];
250 NSString *value([[NSString alloc] initWithFormat:[self _styleFormatString:@"width: %.0fpx; height: %.0fpx"], CGRectGetWidth(rect), CGRectGetHeight(rect)]);
251 [size_ setAttribute:@"style" value:value];
254 [[view_ mainFrame] forceLayoutAdjustingViewSize:YES];
256 [self drawRect:rect];
259 - (void) drawString:(NSString *)string atPoint:(CGPoint)point withStyle:(NSString *)style {
260 [self drawString:string inRect:CGRectMake(point.x, point.y, 65535, 65535) withStyle:style];
263 - (void) drawString:(NSString *)string inRect:(CGRect)rect withStyle:(NSString *)style {
266 if (![self _webPrepareContextForTextDrawing:YES])
269 [size_ removeChild:[size_ firstChild]];
271 WebFrame *frame([view_ mainFrame]);
273 [frame forceLayoutAdjustingViewSize:YES];
274 [text_ setInnerText:string];
275 [self _setupWithStyle:style width:CGRectGetWidth(rect) height:CGRectGetHeight(rect)];
276 [frame forceLayoutAdjustingViewSize:YES];
278 [self drawRect:rect];