7 CGContextRef WKGetCurrentGraphicsContext();
8 void WKViewLockFocus(WKView *);
9 void WKViewUnlockFocus(WKView *);
10 void WKViewDisplayRect(WKView *, CGRect);
13 @interface DOMElement : NSObject
14 - (void) setInnerHTML:(NSString *)value;
15 - (void) setInnerText:(NSString *)value;
16 - (void) setAttribute:(NSString *)name value:(NSString *)value;
17 - (void) removeAttribute:(NSString *)name;
18 - (DOMElement *) firstChild;
19 - (void) appendChild:(DOMElement *)child;
20 - (void) removeChild:(DOMElement *)child;
21 - (void) setScrollXOffset:(float)x scrollYOffset:(float)y;
24 @interface DOMDocument : NSObject
25 - (DOMElement *) getElementById:(NSString *)id;
28 @interface WebPreferences : NSObject
29 - (id) initWithIdentifier:(NSString *)identifier;
30 - (void) setPlugInsEnabled:(BOOL)value;
33 @interface WebFrameView : NSObject
34 - (void) setAllowsScrolling:(BOOL)value;
37 @interface WebFrame : NSObject
38 - (WebFrameView *) frameView;
39 - (void) _setLoadsSynchronously:(BOOL)value;
40 - (void) loadHTMLString:(NSString *)string baseURL:(id)url;
41 - (void) forceLayoutAdjustingViewSize:(BOOL)adjust;
42 - (CGSize) renderedSizeOfNode:(DOMElement *)node constrainedToWidth:(float)width;
43 - (DOMDocument *) DOMDocument;
46 @interface WAKView : NSObject
47 - (void) _drawRect:(CGRect)rect context:(CGContext *)context lockFocus:(bool)focus;
50 @interface WebView : WAKView
51 - (id) initWithFrame:(CGRect)frame;
52 - (WebFrame *) mainFrame;
53 - (void) setDrawsBackground:(BOOL)value;
54 - (void) setPreferences:(WebPreferences *)preferences;
55 - (WKView *) _viewRef;
58 @interface WAKWindow : NSObject
59 - (id) initWithFrame:(CGRect)frame;
60 - (void) setContentView:(WebView *)view;
63 static WBMarkup *SharedMarkup_;
65 @implementation WBMarkup
67 + (BOOL) isSharedMarkupCreated {
68 return SharedMarkup_ != nil;
71 + (WBMarkup *) sharedMarkup {
72 if (SharedMarkup_ == nil)
73 SharedMarkup_ = [[WBMarkup alloc] init];
78 if ((self = [super init]) != nil) {
83 view_ = [[WebView alloc] initWithFrame:CGRectMake(0, 0, 640, 5000)];
84 [view_ setDrawsBackground:NO];
86 WebPreferences *preferences([[WebPreferences alloc] initWithIdentifier:@"com.apple.webkit.webmarkup"]);
87 [preferences setPlugInsEnabled:NO];
88 [view_ setPreferences:preferences];
89 [preferences release];
91 window_ = [[WAKWindow alloc] initWithFrame:CGRectMake(0, 0, 640, 5000)];
92 [window_ setContentView:view_];
94 WebFrame *frame([view_ mainFrame]);
95 [[frame frameView] setAllowsScrolling:NO];
96 [frame _setLoadsSynchronously:YES];
98 [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];
108 - (void) drawRect:(CGRect)rect {
109 [text_ setScrollXOffset:origin_.x scrollYOffset:origin_.y];
111 CGRect draw(CGRectMake(0, 0, rect.size.width - rect.origin.x, rect.size.height - rect.origin.y));
113 CGContextSaveGState(context_); {
114 CGContextTranslateCTM(context_, rect.origin.x, rect.origin.y);
116 if (kCFCoreFoundationVersionNumber > 700)
117 [view_ _drawRect:draw context:context_ lockFocus:YES];
119 WKView *view([view_ _viewRef]);
120 WKViewLockFocus(view); {
121 WKViewDisplayRect(view, draw);
122 } WKViewUnlockFocus(view);
124 } CGContextRestoreGState(context_);
127 - (WebView *) _webView {
131 - (void) setStringDrawingOrigin:(CGPoint)origin {
135 - (void) clearStringDrawingOrigin {
136 origin_ = CGPointZero;
139 - (CGSize) sizeOfMarkup:(NSString *)markup forWidth:(float)width {
142 if (![self _webPrepareContextForTextDrawing:NO])
145 [text_ setInnerHTML:markup];
146 [text_ removeAttribute:@"style"];
148 NSString *value([[NSString alloc] initWithFormat:[self _styleFormatString:@"width: %.0fpx; height: 5000px"], width]);
149 [size_ setAttribute:@"style" value:value];
152 [[view_ mainFrame] forceLayoutAdjustingViewSize:YES];
153 return [[view_ mainFrame] renderedSizeOfNode:text_ constrainedToWidth:width];
156 - (CGSize) sizeOfString:(NSString *)string withStyle:(NSString *)style forWidth:(float)width {
159 if (![self _webPrepareContextForTextDrawing:NO])
162 [size_ removeChild:[size_ firstChild]];
164 WebFrame *frame([view_ mainFrame]);
166 [frame forceLayoutAdjustingViewSize:YES];
167 [text_ setInnerText:string];
168 [self _setupWithStyle:style width:width height:5000];
169 [frame forceLayoutAdjustingViewSize:YES];
171 return [[view_ mainFrame] renderedSizeOfNode:text_ constrainedToWidth:width];
174 - (NSString *) _styleFormatString:(NSString *)style {
178 - (void) _setupWithStyle:(NSString *)style width:(float)width height:(float)height {
181 if (style != nil && [style length] != 0)
182 [text_ setAttribute:@"style" value:style];
184 [text_ removeAttribute:@"style"];
186 NSString *value([[NSString alloc] initWithFormat:[self _styleFormatString:@"width: %.0fpx; height: %.0fpx"], width, height]);
187 [size_ setAttribute:@"style" value:value];
190 [size_ appendChild:text_];
193 - (BOOL) _webPrepareContextForTextDrawing:(BOOL)drawing {
196 if (document_ == nil) {
197 WebFrame *frame([view_ mainFrame]);
199 document_ = [[frame DOMDocument] retain];
200 if (document_ == nil) {
201 NSLog(@"*** ERROR: no DOM document in text-drawing webview");
205 text_ = [[document_ getElementById:@"text"] retain];
206 size_ = [[document_ getElementById:@"size"] retain];
208 if (text_ == nil || size_ == nil) {
209 NSLog(@"*** ERROR: cannot find DOM element required for text drawing");
219 context_ = WKGetCurrentGraphicsContext();
220 if (context_ == NULL) {
221 NSLog(@"*** ERROR: no CGContext set for drawing");
229 - (void) drawMarkup:(NSString *)markup atPoint:(CGPoint)point {
230 [self drawMarkup:markup inRect:CGRectMake(point.x, point.y, 65535, 65535)];
233 - (void) drawMarkup:(NSString *)markup inRect:(CGRect)rect {
236 if (![self _webPrepareContextForTextDrawing:YES])
239 [text_ setInnerHTML:markup];
240 [text_ removeAttribute:@"style"];
242 NSString *value([[NSString alloc] initWithFormat:[self _styleFormatString:@"width: %.0fpx; height: %.0fpx"], CGRectGetWidth(rect), CGRectGetHeight(rect)]);
243 [size_ setAttribute:@"style" value:value];
246 [[view_ mainFrame] forceLayoutAdjustingViewSize:YES];
248 [self drawRect:rect];
251 - (void) drawString:(NSString *)string atPoint:(CGPoint)point withStyle:(NSString *)style {
252 [self drawString:string inRect:CGRectMake(point.x, point.y, 65535, 65535) withStyle:style];
255 - (void) drawString:(NSString *)string inRect:(CGRect)rect withStyle:(NSString *)style {
258 if (![self _webPrepareContextForTextDrawing:YES])
261 [size_ removeChild:[size_ firstChild]];
263 WebFrame *frame([view_ mainFrame]);
265 [frame forceLayoutAdjustingViewSize:YES];
266 [text_ setInnerText:string];
267 [self _setupWithStyle:style width:CGRectGetWidth(rect) height:CGRectGetHeight(rect)];
268 [frame forceLayoutAdjustingViewSize:YES];
270 [self drawRect:rect];