Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(446)

Side by Side Diff: Source/web/mac/WebSubstringUtil.mm

Issue 1329103002: [blink/mac] Support getting baseline point for showing dictionary lookup bubble. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | public/web/mac/WebSubstringUtil.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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.
eae 2015/09/09 16:56:04 Could you please file a bug about this and add a l
Shu Chen 2015/09/10 01:14:07 Done.
117 IntRect stringRect = enclosingIntRect(createRange(range)->boundingRect());
118 IntPoint stringPoint = stringRect.minXMaxYCorner();
119 stringPoint.setY(frameView->height() - stringPoint.y());
eae 2015/09/09 16:56:04 You take the bottom left corner of the bounding re
Shu Chen 2015/09/10 01:14:07 Actually, I don't understand why adjust y() by the
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)
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);
173 if (baselinePoint)
174 *baselinePoint = getBaselinePoint(frame->view(), ephemeralRange, ret);
175 return ret;
167 } 176 }
168 177
169 } // namespace blink 178 } // namespace blink
OLDNEW
« no previous file with comments | « no previous file | public/web/mac/WebSubstringUtil.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698