| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2005, 2007, 2008 Apple Inc. All rights reserved. | 2 * Copyright (C) 2005, 2007, 2008 Apple Inc. All rights reserved. |
| 3 * Copyright (C) 2011 Google Inc. All rights reserved. | 3 * Copyright (C) 2011 Google Inc. All rights reserved. |
| 4 * | 4 * |
| 5 * Redistribution and use in source and binary forms, with or without | 5 * Redistribution and use in source and binary forms, with or without |
| 6 * modification, are permitted provided that the following conditions are | 6 * modification, are permitted provided that the following conditions are |
| 7 * met: | 7 * met: |
| 8 * | 8 * |
| 9 * * Redistributions of source code must retain the above copyright | 9 * * Redistributions of source code must retain the above copyright |
| 10 * notice, this list of conditions and the following disclaimer. | 10 * notice, this list of conditions and the following disclaimer. |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 103 [[[NSString alloc] initWithCharacters:characters.data() | 103 [[[NSString alloc] initWithCharacters:characters.data() |
| 104 length:characters.size()] autorelease
]; | 104 length:characters.size()] autorelease
]; |
| 105 [string replaceCharactersInRange:NSMakeRange(position, 0) | 105 [string replaceCharactersInRange:NSMakeRange(position, 0) |
| 106 withString:substring]; | 106 withString:substring]; |
| 107 [string setAttributes:attrs range:NSMakeRange(position, numCharacters)]; | 107 [string setAttributes:attrs range:NSMakeRange(position, numCharacters)]; |
| 108 position += numCharacters; | 108 position += numCharacters; |
| 109 } | 109 } |
| 110 return [string autorelease]; | 110 return [string autorelease]; |
| 111 } | 111 } |
| 112 | 112 |
| 113 WebPoint getBaselinePoint(FrameView* frameView, const EphemeralRange& range, NSA
ttributedString* string) |
| 114 { |
| 115 // Compute bottom left corner and convert to AppKit coordinates. |
| 116 // TODO(yosin) We shold avoid to create |Range| object. See crbug.com/529985
. |
| 117 IntRect stringRect = enclosingIntRect(createRange(range)->boundingRect()); |
| 118 IntPoint stringPoint = stringRect.minXMaxYCorner(); |
| 119 stringPoint.setY(frameView->height() - stringPoint.y()); |
| 120 |
| 121 // Adjust for the font's descender. AppKit wants the baseline point. |
| 122 if ([string length]) { |
| 123 NSDictionary* attributes = [string attributesAtIndex:0 effectiveRange:NU
LL]; |
| 124 if (NSFont* font = [attributes objectForKey:NSFontAttributeName]) |
| 125 stringPoint.move(0, ceil(-[font descender])); |
| 126 } |
| 127 return stringPoint; |
| 128 } |
| 129 |
| 113 namespace blink { | 130 namespace blink { |
| 114 | 131 |
| 115 NSAttributedString* WebSubstringUtil::attributedWordAtPoint(WebView* view, WebPo
int point, WebPoint& baselinePoint) | 132 NSAttributedString* WebSubstringUtil::attributedWordAtPoint(WebView* view, WebPo
int point, WebPoint& baselinePoint) |
| 116 { | 133 { |
| 117 HitTestResult result = static_cast<WebViewImpl*>(view)->coreHitTestResultAt(
point); | 134 HitTestResult result = static_cast<WebViewImpl*>(view)->coreHitTestResultAt(
point); |
| 118 if (!result.innerNode()) | 135 if (!result.innerNode()) |
| 119 return nil; | 136 return nil; |
| 120 LocalFrame* frame = result.innerNode()->document().frame(); | 137 LocalFrame* frame = result.innerNode()->document().frame(); |
| 121 FrameView* frameView = frame->view(); | |
| 122 | |
| 123 EphemeralRange range = frame->rangeForPoint(result.roundedPointInInnerNodeFr
ame()); | 138 EphemeralRange range = frame->rangeForPoint(result.roundedPointInInnerNodeFr
ame()); |
| 124 if (range.isNull()) | 139 if (range.isNull()) |
| 125 return nil; | 140 return nil; |
| 126 | 141 |
| 127 // Expand to word under point. | 142 // Expand to word under point. |
| 128 VisibleSelection selection(range); | 143 VisibleSelection selection(range); |
| 129 selection.expandUsingGranularity(WordGranularity); | 144 selection.expandUsingGranularity(WordGranularity); |
| 130 const EphemeralRange wordRange = selection.toNormalizedEphemeralRange(); | 145 const EphemeralRange wordRange = selection.toNormalizedEphemeralRange(); |
| 131 | 146 |
| 132 // Convert to NSAttributedString. | 147 // Convert to NSAttributedString. |
| 133 NSAttributedString* string = attributedSubstringFromRange(wordRange); | 148 NSAttributedString* string = attributedSubstringFromRange(wordRange); |
| 134 | 149 baselinePoint = getBaselinePoint(frame->view(), wordRange, string); |
| 135 // Compute bottom left corner and convert to AppKit coordinates. | |
| 136 // TODO(yosin) We shold avoid to create |Range| object. | |
| 137 IntRect stringRect = enclosingIntRect(createRange(wordRange)->boundingRect()
); | |
| 138 IntPoint stringPoint = stringRect.minXMaxYCorner(); | |
| 139 stringPoint.setY(frameView->height() - stringPoint.y()); | |
| 140 | |
| 141 // Adjust for the font's descender. AppKit wants the baseline point. | |
| 142 if ([string length]) { | |
| 143 NSDictionary* attributes = [string attributesAtIndex:0 effectiveRange:NU
LL]; | |
| 144 NSFont* font = [attributes objectForKey:NSFontAttributeName]; | |
| 145 if (font) | |
| 146 stringPoint.move(0, ceil(-[font descender])); | |
| 147 } | |
| 148 | |
| 149 baselinePoint = stringPoint; | |
| 150 return string; | 150 return string; |
| 151 } | 151 } |
| 152 | 152 |
| 153 NSAttributedString* WebSubstringUtil::attributedSubstringInRange(WebLocalFrame*
webFrame, size_t location, size_t length) | 153 NSAttributedString* WebSubstringUtil::attributedSubstringInRange(WebLocalFrame*
webFrame, size_t location, size_t length) |
| 154 { | 154 { |
| 155 return WebSubstringUtil::attributedSubstringInRange(webFrame, location, leng
th, nil); |
| 156 } |
| 157 |
| 158 NSAttributedString* WebSubstringUtil::attributedSubstringInRange(WebLocalFrame*
webFrame, size_t location, size_t length, WebPoint* baselinePoint) |
| 159 { |
| 155 LocalFrame* frame = toWebLocalFrameImpl(webFrame)->frame(); | 160 LocalFrame* frame = toWebLocalFrameImpl(webFrame)->frame(); |
| 156 if (frame->view()->needsLayout()) | 161 if (frame->view()->needsLayout()) |
| 157 frame->view()->layout(); | 162 frame->view()->layout(); |
| 158 | 163 |
| 159 Element* editable = frame->selection().rootEditableElementOrDocumentElement(
); | 164 Element* editable = frame->selection().rootEditableElementOrDocumentElement(
); |
| 160 if (!editable) | 165 if (!editable) |
| 161 return nil; | 166 return nil; |
| 162 const EphemeralRange ephemeralRange(PlainTextRange(location, location + leng
th).createRange(*editable)); | 167 const EphemeralRange ephemeralRange(PlainTextRange(location, location + leng
th).createRange(*editable)); |
| 163 if (ephemeralRange.isNull()) | 168 if (ephemeralRange.isNull()) |
| 164 return nil; | 169 return nil; |
| 165 | 170 |
| 166 return attributedSubstringFromRange(ephemeralRange); | 171 NSAttributedString* result = attributedSubstringFromRange(ephemeralRange); |
| 172 if (baselinePoint) |
| 173 *baselinePoint = getBaselinePoint(frame->view(), ephemeralRange, result)
; |
| 174 return result; |
| 167 } | 175 } |
| 168 | 176 |
| 169 } // namespace blink | 177 } // namespace blink |
| OLD | NEW |