1 /* 2 * Copyright (C) 2005, 2007, 2008 Apple Inc. All rights reserved. 3 * Copyright (C) 2011 Google Inc. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are 7 * met: 8 * 9 * * Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * * Redistributions in binary form must reproduce the above 12 * copyright notice, this list of conditions and the following disclaimer 13 * in the documentation and/or other materials provided with the 14 * distribution. 15 * * Neither the name of Google Inc. nor the names of its 16 * contributors may be used to endorse or promote products derived from 17 * this software without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 #include "config.h" 33 #include "public/web/mac/WebSubstringUtil.h" 34 35 #import <Cocoa/Cocoa.h> 36 37 #include "bindings/core/v8/ExceptionStatePlaceholder.h" 38 #include "core/dom/Document.h" 39 #include "core/dom/Element.h" 40 #include "core/dom/Node.h" 41 #include "core/dom/Range.h" 42 #include "core/editing/FrameSelection.h" 43 #include "core/editing/PlainTextRange.h" 44 #include "core/editing/TextIterator.h" 45 #include "core/frame/FrameView.h" 46 #include "core/frame/LocalFrame.h" 47 #include "core/html/HTMLElement.h" 48 #include "core/rendering/HitTestResult.h" 49 #include "core/rendering/RenderObject.h" 50 #include "core/rendering/style/RenderStyle.h" 51 #include "platform/fonts/Font.h" 52 #include "platform/mac/ColorMac.h" 53 #include "public/platform/WebRect.h" 54 #include "public/web/WebHitTestResult.h" 55 #include "public/web/WebRange.h" 56 #include "public/web/WebView.h" 57 #include "web/WebLocalFrameImpl.h" 58 #include "web/WebViewImpl.h" 59 60 using namespace blink; 61 62 static NSAttributedString* attributedSubstringFromRange(const Range* range) 63 { 64 NSMutableAttributedString* string = [[NSMutableAttributedString alloc] init]; 65 NSMutableDictionary* attrs = [NSMutableDictionary dictionary]; 66 size_t length = range->endOffset() - range->startOffset(); 67 68 unsigned position = 0; 69 for (TextIterator it(range); !it.atEnd() && [string length] < length; it.advance()) { 70 unsigned numCharacters = it.length(); 71 if (!numCharacters) 72 continue; 73 74 Node* container = it.startContainer(); 75 RenderObject* renderer = container->renderer(); 76 ASSERT(renderer); 77 if (!renderer) 78 continue; 79 80 RenderStyle* style = renderer->style(); 81 NSFont* font = style->font().primaryFont()->getNSFont(); 82 // If the platform font can't be loaded, it's likely that the site is 83 // using a web font. For now, just use the default font instead. 84 // TODO(rsesek): Change the font activation flags to allow other processes 85 // to use the font. 86 if (!font) 87 font = [NSFont systemFontOfSize:style->font().fontDescription().computedSize()]; 88 [attrs setObject:font forKey:NSFontAttributeName]; 89 90 if (style->visitedDependentColor(CSSPropertyColor).alpha()) 91 [attrs setObject:nsColor(style->visitedDependentColor(CSSPropertyColor)) forKey:NSForegroundColorAttributeName]; 92 else 93 [attrs removeObjectForKey:NSForegroundColorAttributeName]; 94 if (style->visitedDependentColor(CSSPropertyBackgroundColor).alpha()) 95 [attrs setObject:nsColor(style->visitedDependentColor(CSSPropertyBackgroundColor)) forKey:NSBackgroundColorAttributeName]; 96 else 97 [attrs removeObjectForKey:NSBackgroundColorAttributeName]; 98 99 Vector<UChar> characters; 100 it.appendTextTo(characters); 101 NSString* substring = 102 [[[NSString alloc] initWithCharacters:characters.data() 103 length:characters.size()] autorelease]; 104 [string replaceCharactersInRange:NSMakeRange(position, 0) 105 withString:substring]; 106 [string setAttributes:attrs range:NSMakeRange(position, numCharacters)]; 107 position += numCharacters; 108 } 109 return [string autorelease]; 110 } 111 112 namespace blink { 113 114 NSAttributedString* WebSubstringUtil::attributedWordAtPoint(WebView* view, WebPoint point, WebPoint& baselinePoint) 115 { 116 HitTestResult result = static_cast<WebViewImpl*>(view)->coreHitTestResultAt(point); 117 if (!result.innerNode()) 118 return nil; 119 LocalFrame* frame = result.innerNode()->document().frame(); 120 FrameView* frameView = frame->view(); 121 122 RefPtrWillBeRawPtr<Range> range = frame->rangeForPoint(result.roundedPointInInnerNodeFrame()); 123 if (!range) 124 return nil; 125 126 // Expand to word under point. 127 VisibleSelection selection(range.get()); 128 selection.expandUsingGranularity(WordGranularity); 129 RefPtrWillBeRawPtr<Range> wordRange = selection.toNormalizedRange(); 130 131 // Convert to NSAttributedString. 132 NSAttributedString* string = attributedSubstringFromRange(wordRange.get()); 133 134 // Compute bottom left corner and convert to AppKit coordinates. 135 IntRect stringRect = enclosingIntRect(wordRange->boundingRect()); 136 IntPoint stringPoint = stringRect.minXMaxYCorner(); 137 stringPoint.setY(frameView->height() - stringPoint.y()); 138 139 // Adjust for the font's descender. AppKit wants the baseline point. 140 if ([string length]) { 141 NSDictionary* attributes = [string attributesAtIndex:0 effectiveRange:NULL]; 142 NSFont* font = [attributes objectForKey:NSFontAttributeName]; 143 if (font) 144 stringPoint.move(0, ceil(-[font descender])); 145 } 146 147 baselinePoint = stringPoint; 148 return string; 149 } 150 151 NSAttributedString* WebSubstringUtil::attributedSubstringInRange(WebLocalFrame* webFrame, size_t location, size_t length) 152 { 153 LocalFrame* frame = toWebLocalFrameImpl(webFrame)->frame(); 154 if (frame->view()->needsLayout()) 155 frame->view()->layout(); 156 157 Element* editable = frame->selection().rootEditableElementOrDocumentElement(); 158 if (!editable) 159 return nil; 160 RefPtrWillBeRawPtr<Range> range(PlainTextRange(location, location + length).createRange(*editable)); 161 if (!range) 162 return nil; 163 164 return attributedSubstringFromRange(range.get()); 165 } 166 167 } // namespace blink 168