Chromium Code Reviews| 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, con st NSAttributedString* 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 NSFont* font = [attributes objectForKey:NSFontAttributeName]; | |
| 125 if (font) | |
|
esprehn
2015/09/18 02:34:00
We normally write this as a single statement.
if
Shu Chen
2015/09/18 02:39:36
Done.
| |
| 126 stringPoint.move(0, ceil(-[font descender])); | |
| 127 } | |
| 128 return stringPoint; | |
| 129 } | |
| 130 | |
| 113 namespace blink { | 131 namespace blink { |
| 114 | 132 |
| 115 NSAttributedString* WebSubstringUtil::attributedWordAtPoint(WebView* view, WebPo int point, WebPoint& baselinePoint) | 133 NSAttributedString* WebSubstringUtil::attributedWordAtPoint(WebView* view, WebPo int point, WebPoint& baselinePoint) |
| 116 { | 134 { |
| 117 HitTestResult result = static_cast<WebViewImpl*>(view)->coreHitTestResultAt( point); | 135 HitTestResult result = static_cast<WebViewImpl*>(view)->coreHitTestResultAt( point); |
| 118 if (!result.innerNode()) | 136 if (!result.innerNode()) |
| 119 return nil; | 137 return nil; |
| 120 LocalFrame* frame = result.innerNode()->document().frame(); | 138 LocalFrame* frame = result.innerNode()->document().frame(); |
| 121 FrameView* frameView = frame->view(); | |
| 122 | |
| 123 EphemeralRange range = frame->rangeForPoint(result.roundedPointInInnerNodeFr ame()); | 139 EphemeralRange range = frame->rangeForPoint(result.roundedPointInInnerNodeFr ame()); |
| 124 if (range.isNull()) | 140 if (range.isNull()) |
| 125 return nil; | 141 return nil; |
| 126 | 142 |
| 127 // Expand to word under point. | 143 // Expand to word under point. |
| 128 VisibleSelection selection(range); | 144 VisibleSelection selection(range); |
| 129 selection.expandUsingGranularity(WordGranularity); | 145 selection.expandUsingGranularity(WordGranularity); |
| 130 const EphemeralRange wordRange = selection.toNormalizedEphemeralRange(); | 146 const EphemeralRange wordRange = selection.toNormalizedEphemeralRange(); |
| 131 | 147 |
| 132 // Convert to NSAttributedString. | 148 // Convert to NSAttributedString. |
| 133 NSAttributedString* string = attributedSubstringFromRange(wordRange); | 149 NSAttributedString* string = attributedSubstringFromRange(wordRange); |
| 134 | 150 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; | 151 return string; |
| 151 } | 152 } |
| 152 | 153 |
| 153 NSAttributedString* WebSubstringUtil::attributedSubstringInRange(WebLocalFrame* webFrame, size_t location, size_t length) | 154 NSAttributedString* WebSubstringUtil::attributedSubstringInRange(WebLocalFrame* webFrame, size_t location, size_t length) |
| 154 { | 155 { |
| 156 return WebSubstringUtil::attributedSubstringInRange(webFrame, location, leng th, nil); | |
| 157 } | |
| 158 | |
| 159 NSAttributedString* WebSubstringUtil::attributedSubstringInRange(WebLocalFrame* webFrame, size_t location, size_t length, WebPoint* baselinePoint) | |
| 160 { | |
| 155 LocalFrame* frame = toWebLocalFrameImpl(webFrame)->frame(); | 161 LocalFrame* frame = toWebLocalFrameImpl(webFrame)->frame(); |
| 156 if (frame->view()->needsLayout()) | 162 if (frame->view()->needsLayout()) |
| 157 frame->view()->layout(); | 163 frame->view()->layout(); |
| 158 | 164 |
| 159 Element* editable = frame->selection().rootEditableElementOrDocumentElement( ); | 165 Element* editable = frame->selection().rootEditableElementOrDocumentElement( ); |
| 160 if (!editable) | 166 if (!editable) |
| 161 return nil; | 167 return nil; |
| 162 const EphemeralRange ephemeralRange(PlainTextRange(location, location + leng th).createRange(*editable)); | 168 const EphemeralRange ephemeralRange(PlainTextRange(location, location + leng th).createRange(*editable)); |
| 163 if (ephemeralRange.isNull()) | 169 if (ephemeralRange.isNull()) |
| 164 return nil; | 170 return nil; |
| 165 | 171 |
| 166 return attributedSubstringFromRange(ephemeralRange); | 172 NSAttributedString* ret = attributedSubstringFromRange(ephemeralRange); |
|
esprehn
2015/09/18 02:34:00
don't abbreviate.
NSAttributedString* result = .
Shu Chen
2015/09/18 02:39:36
Done.
| |
| 173 if (baselinePoint) | |
| 174 *baselinePoint = getBaselinePoint(frame->view(), ephemeralRange, ret); | |
| 175 return ret; | |
| 167 } | 176 } |
| 168 | 177 |
| 169 } // namespace blink | 178 } // namespace blink |
| OLD | NEW |