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 WebView : NSObject
47 - (id) initWithFrame:(CGRect)frame;
48 - (WebFrame *) mainFrame;
49 - (void) setDrawsBackground:(BOOL)value;
50 - (void) setPreferences:(WebPreferences *)preferences;
51 - (WKView *) _viewRef;
54 @interface WAKWindow : NSObject
55 - (id) initWithFrame:(CGRect)frame;
56 - (void) setContentView:(WebView *)view;
59 static WBMarkup *SharedMarkup_;
61 @implementation WBMarkup
63 + (BOOL) isSharedMarkupCreated {
64 return SharedMarkup_ != nil;
67 + (WBMarkup *) sharedMarkup {
68 if (SharedMarkup_ == nil)
69 SharedMarkup_ = [[WBMarkup alloc] init];
74 if ((self = [super init]) != nil) {
79 view_ = [[WebView alloc] initWithFrame:CGRectMake(0, 0, 640, 5000)];
80 [view_ setDrawsBackground:NO];
82 WebPreferences *preferences([[WebPreferences alloc] initWithIdentifier:@"com.apple.webkit.webmarkup"]);
83 [preferences setPlugInsEnabled:NO];
84 [view_ setPreferences:preferences];
85 [preferences release];
87 window_ = [[WAKWindow alloc] initWithFrame:CGRectMake(0, 0, 640, 5000)];
88 [window_ setContentView:view_];
90 WebFrame *frame([view_ mainFrame]);
91 [[frame frameView] setAllowsScrolling:NO];
92 [frame _setLoadsSynchronously:YES];
94 [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];
104 - (WebView *) _webView {
108 - (void) setStringDrawingOrigin:(CGPoint)origin {
112 - (void) clearStringDrawingOrigin {
113 origin_ = CGPointZero;
116 - (CGSize) sizeOfMarkup:(NSString *)markup forWidth:(float)width {
119 if (![self _webPrepareContextForTextDrawing:NO])
122 [text_ setInnerHTML:markup];
123 [text_ removeAttribute:@"style"];
125 NSString *value([[NSString alloc] initWithFormat:[self _styleFormatString:@"width: %.0fpx; height: 5000px"], width]);
126 [size_ setAttribute:@"style" value:value];
129 [[view_ mainFrame] forceLayoutAdjustingViewSize:YES];
130 return [[view_ mainFrame] renderedSizeOfNode:text_ constrainedToWidth:width];
133 - (CGSize) sizeOfString:(NSString *)string withStyle:(NSString *)style forWidth:(float)width {
136 if (![self _webPrepareContextForTextDrawing:NO])
139 [size_ removeChild:[size_ firstChild]];
141 WebFrame *frame([view_ mainFrame]);
143 [frame forceLayoutAdjustingViewSize:YES];
144 [text_ setInnerText:string];
145 [self _setupWithStyle:style width:width height:5000];
146 [frame forceLayoutAdjustingViewSize:YES];
148 return [[view_ mainFrame] renderedSizeOfNode:text_ constrainedToWidth:width];
151 - (NSString *) _styleFormatString:(NSString *)style {
155 - (void) _setupWithStyle:(NSString *)style width:(float)width height:(float)height {
158 if (style != nil && [style length] != 0)
159 [text_ setAttribute:@"style" value:style];
161 [text_ removeAttribute:@"style"];
163 NSString *value([[NSString alloc] initWithFormat:[self _styleFormatString:@"width: %.0fpx; height: %.0fpx"], width, height]);
164 [size_ setAttribute:@"style" value:value];
167 [size_ appendChild:text_];
170 - (BOOL) _webPrepareContextForTextDrawing:(BOOL)drawing {
173 if (document_ == nil) {
174 WebFrame *frame([view_ mainFrame]);
176 document_ = [[frame DOMDocument] retain];
177 if (document_ == nil) {
178 NSLog(@"*** ERROR: no DOM document in text-drawing webview");
182 text_ = [[document_ getElementById:@"text"] retain];
183 size_ = [[document_ getElementById:@"size"] retain];
185 if (text_ == nil || size_ == nil) {
186 NSLog(@"*** ERROR: cannot find DOM element required for text drawing");
196 context_ = WKGetCurrentGraphicsContext();
197 if (context_ == NULL) {
198 NSLog(@"*** ERROR: no CGContext set for drawing");
206 - (void) drawMarkup:(NSString *)markup atPoint:(CGPoint)point {
207 [self drawMarkup:markup inRect:CGRectMake(point.x, point.y, 65535, 65535)];
210 - (void) drawMarkup:(NSString *)markup inRect:(CGRect)rect {
213 if (![self _webPrepareContextForTextDrawing:YES])
216 [text_ setInnerHTML:markup];
217 [text_ removeAttribute:@"style"];
219 NSString *value([[NSString alloc] initWithFormat:[self _styleFormatString:@"width: %.0fpx; height: %.0fpx"], CGRectGetWidth(rect), CGRectGetHeight(rect)]);
220 [size_ setAttribute:@"style" value:value];
223 [[view_ mainFrame] forceLayoutAdjustingViewSize:YES];
225 [text_ setScrollXOffset:origin_.x scrollYOffset:origin_.y];
227 WKView *view([view_ _viewRef]);
229 CGContextSaveGState(context_); {
230 CGContextTranslateCTM(context_, rect.origin.x, rect.origin.y);
232 WKViewLockFocus(view); {
233 WKViewDisplayRect(view, CGRectMake(0, 0, rect.origin.x, rect.origin.y));
234 } WKViewUnlockFocus(view);
235 } CGContextRestoreGState(context_);
238 - (void) drawString:(NSString *)string atPoint:(CGPoint)point withStyle:(NSString *)style {
239 [self drawString:string inRect:CGRectMake(point.x, point.y, 65535, 65535) withStyle:style];
242 - (void) drawString:(NSString *)string inRect:(CGRect)rect withStyle:(NSString *)style {
245 if (![self _webPrepareContextForTextDrawing:YES])
248 [size_ removeChild:[size_ firstChild]];
250 WebFrame *frame([view_ mainFrame]);
252 [frame forceLayoutAdjustingViewSize:YES];
253 [text_ setInnerText:string];
254 [self _setupWithStyle:style width:CGRectGetWidth(rect) height:CGRectGetHeight(rect)];
255 [frame forceLayoutAdjustingViewSize:YES];
257 [text_ setScrollXOffset:origin_.x scrollYOffset:origin_.y];
259 WKView *view([view_ _viewRef]);
261 CGContextSaveGState(context_); {
262 CGContextTranslateCTM(context_, rect.origin.x, rect.origin.y);
264 WKViewLockFocus(view); {
265 WKViewDisplayRect(view, CGRectMake(0, 0, rect.size.width, rect.size.height));
266 } WKViewUnlockFocus(view);
267 } CGContextRestoreGState(context_);