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

Side by Side Diff: third_party/WebKit/Source/core/layout/svg/SVGTextQuery.cpp

Issue 1822673002: Make the findMetricsForCharacter SVGTextQuery-helper return an iterator (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@svg-textquery--cleanup-1
Patch Set: Created 4 years, 9 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 | no next file » | 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) Research In Motion Limited 2010-2012. All rights reserved. 2 * Copyright (C) Research In Motion Limited 2010-2012. All rights reserved.
3 * 3 *
4 * This library is free software; you can redistribute it and/or 4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public 5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either 6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version. 7 * version 2 of the License, or (at your option) any later version.
8 * 8 *
9 * This library is distributed in the hope that it will be useful, 9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after
161 collectTextBoxesInLogicalOrder(textLineLayout, textBoxes); 161 collectTextBoxesInLogicalOrder(textLineLayout, textBoxes);
162 162
163 for (const SVGInlineTextBox* textBox : textBoxes) { 163 for (const SVGInlineTextBox* textBox : textBoxes) {
164 if (queryTextBox(queryData, textBox, fragmentCallback)) 164 if (queryTextBox(queryData, textBox, fragmentCallback))
165 return; 165 return;
166 queryData->currentOffset += textBox->len(); 166 queryData->currentOffset += textBox->len();
167 } 167 }
168 } 168 }
169 } 169 }
170 170
171 static void modifyStartEndPositionsRespectingLigatures(const QueryData* queryDat a, const SVGTextFragment& fragment, int& startPosition, int& endPosition)
172 {
173 const Vector<SVGTextMetrics>& textMetricsValues = queryData->textLineLayout. layoutAttributes()->textMetricsValues();
174
175 unsigned textMetricsOffset = fragment.metricsListOffset;
176 int fragmentOffset = 0;
177 int fragmentEnd = static_cast<int>(fragment.length);
178
179 // Find the text metrics cell that start at or contain the character startPo sition.
180 while (fragmentOffset < fragmentEnd) {
181 const SVGTextMetrics& metrics = textMetricsValues[textMetricsOffset];
182 int glyphEnd = fragmentOffset + metrics.length();
183 if (startPosition < glyphEnd)
184 break;
185 fragmentOffset = glyphEnd;
186 textMetricsOffset++;
187 }
188
189 startPosition = fragmentOffset;
190
191 // Find the text metrics cell that contain or ends at the character endPosit ion.
192 while (fragmentOffset < fragmentEnd) {
193 const SVGTextMetrics& metrics = textMetricsValues[textMetricsOffset];
194 fragmentOffset += metrics.length();
195 if (fragmentOffset >= endPosition)
196 break;
197 textMetricsOffset++;
198 }
199
200 endPosition = fragmentOffset;
201 }
202
203 static bool mapStartEndPositionsIntoFragmentCoordinates(const QueryData* queryDa ta, const SVGTextFragment& fragment, int& startPosition, int& endPosition) 171 static bool mapStartEndPositionsIntoFragmentCoordinates(const QueryData* queryDa ta, const SVGTextFragment& fragment, int& startPosition, int& endPosition)
204 { 172 {
205 unsigned boxStart = queryData->currentOffset; 173 unsigned boxStart = queryData->currentOffset;
206 174
207 // Make <startPosition, endPosition> offsets relative to the current text bo x. 175 // Make <startPosition, endPosition> offsets relative to the current text bo x.
208 startPosition -= boxStart; 176 startPosition -= boxStart;
209 endPosition -= boxStart; 177 endPosition -= boxStart;
210 178
211 // Reuse the same logic used for text selection & painting, to map our 179 // Reuse the same logic used for text selection & painting, to map our
212 // query start/length into start/endPositions of the current text fragment. 180 // query start/length into start/endPositions of the current text fragment.
213 if (!queryData->textBox->mapStartEndPositionsIntoFragmentCoordinates(fragmen t, startPosition, endPosition)) 181 return queryData->textBox->mapStartEndPositionsIntoFragmentCoordinates(fragm ent, startPosition, endPosition);
214 return false;
215
216 modifyStartEndPositionsRespectingLigatures(queryData, fragment, startPositio n, endPosition);
217 ASSERT(startPosition < endPosition);
218 return true;
219 } 182 }
220 183
221 // numberOfCharacters() implementation 184 // numberOfCharacters() implementation
222 static bool numberOfCharactersCallback(QueryData*, const SVGTextFragment&) 185 static bool numberOfCharactersCallback(QueryData*, const SVGTextFragment&)
223 { 186 {
224 // no-op 187 // no-op
225 return false; 188 return false;
226 } 189 }
227 190
228 unsigned SVGTextQuery::numberOfCharacters() const 191 unsigned SVGTextQuery::numberOfCharacters() const
(...skipping 20 matching lines...) Expand all
249 return false; 212 return false;
250 } 213 }
251 214
252 float SVGTextQuery::textLength() const 215 float SVGTextQuery::textLength() const
253 { 216 {
254 TextLengthData data; 217 TextLengthData data;
255 logicalQuery(m_queryRootLayoutObject, &data, textLengthCallback); 218 logicalQuery(m_queryRootLayoutObject, &data, textLengthCallback);
256 return data.textLength; 219 return data.textLength;
257 } 220 }
258 221
259 const SVGTextMetrics& findMetricsForCharacter(const Vector<SVGTextMetrics>& text MetricsValues, const SVGTextFragment& fragment, unsigned startInFragment) 222 using MetricsList = Vector<SVGTextMetrics>;
223
224 MetricsList::const_iterator findMetricsForCharacter(const MetricsList& metricsLi st, const SVGTextFragment& fragment, unsigned startInFragment)
260 { 225 {
261 // Find the text metrics cell that starts at or contains the character at |s tartInFragment|. 226 // Find the text metrics cell that starts at or contains the character at |s tartInFragment|.
262 unsigned textMetricsOffset = fragment.metricsListOffset; 227 MetricsList::const_iterator metrics = metricsList.begin() + fragment.metrics ListOffset;
263 unsigned fragmentOffset = 0; 228 unsigned fragmentOffset = 0;
264 while (fragmentOffset < fragment.length) { 229 while (fragmentOffset < fragment.length) {
265 const SVGTextMetrics& metrics = textMetricsValues[textMetricsOffset++]; 230 fragmentOffset += metrics->length();
266 unsigned glyphEnd = fragmentOffset + metrics.length(); 231 if (startInFragment < fragmentOffset)
267 if (startInFragment < glyphEnd)
268 break; 232 break;
269 fragmentOffset = glyphEnd; 233 ++metrics;
270 } 234 }
271 return textMetricsValues[textMetricsOffset - 1]; 235 ASSERT(metrics <= metricsList.end());
236 return metrics;
272 } 237 }
273 238
274 static float calculateGlyphRange(const QueryData* queryData, const SVGTextFragme nt& fragment, unsigned start, unsigned end) 239 static float calculateGlyphRange(const QueryData* queryData, const SVGTextFragme nt& fragment, unsigned start, unsigned end)
275 { 240 {
241 const MetricsList& metricsList = queryData->textLineLayout.layoutAttributes( )->textMetricsValues();
242 auto metrics = findMetricsForCharacter(metricsList, fragment, start);
243 auto endMetrics = findMetricsForCharacter(metricsList, fragment, end);
276 float glyphRange = 0; 244 float glyphRange = 0;
277 const Vector<SVGTextMetrics>& textMetricsValues = queryData->textLineLayout. layoutAttributes()->textMetricsValues(); 245 for (; metrics != endMetrics; ++metrics)
278 for (unsigned character = start; character < end; character++) { 246 glyphRange += queryData->isVerticalText ? metrics->height() : metrics->w idth();
279 const SVGTextMetrics& metrics = findMetricsForCharacter(textMetricsValue s, fragment, character);
280 glyphRange += queryData->isVerticalText ? metrics.height() : metrics.wid th();
281 }
282 return glyphRange; 247 return glyphRange;
283 } 248 }
284 249
285 // subStringLength() implementation 250 // subStringLength() implementation
286 struct SubStringLengthData : QueryData { 251 struct SubStringLengthData : QueryData {
287 SubStringLengthData(unsigned queryStartPosition, unsigned queryLength) 252 SubStringLengthData(unsigned queryStartPosition, unsigned queryLength)
288 : startPosition(queryStartPosition) 253 : startPosition(queryStartPosition)
289 , length(queryLength) 254 , length(queryLength)
290 , subStringLength(0) 255 , subStringLength(0)
291 { 256 {
(...skipping 29 matching lines...) Expand all
321 struct StartPositionOfCharacterData : QueryData { 286 struct StartPositionOfCharacterData : QueryData {
322 StartPositionOfCharacterData(unsigned queryPosition) 287 StartPositionOfCharacterData(unsigned queryPosition)
323 : position(queryPosition) 288 : position(queryPosition)
324 { 289 {
325 } 290 }
326 291
327 unsigned position; 292 unsigned position;
328 FloatPoint startPosition; 293 FloatPoint startPosition;
329 }; 294 };
330 295
331 static FloatPoint calculateGlyphPositionWithoutTransform(const QueryData* queryD ata, const SVGTextFragment& fragment, int offsetInFragment) 296 static FloatPoint calculateGlyphPositionWithoutTransform(const QueryData* queryD ata, const SVGTextFragment& fragment, unsigned offsetInFragment)
332 { 297 {
333 float glyphOffsetInDirection = calculateGlyphRange(queryData, fragment, 0, o ffsetInFragment); 298 float glyphOffsetInDirection = calculateGlyphRange(queryData, fragment, 0, o ffsetInFragment);
334 299
335 if (!queryData->textBox->isLeftToRightDirection()) { 300 if (!queryData->textBox->isLeftToRightDirection()) {
336 float fragmentExtent = queryData->isVerticalText ? fragment.height : fra gment.width; 301 float fragmentExtent = queryData->isVerticalText ? fragment.height : fra gment.width;
337 glyphOffsetInDirection = fragmentExtent - glyphOffsetInDirection; 302 glyphOffsetInDirection = fragmentExtent - glyphOffsetInDirection;
338 } 303 }
339 304
340 FloatPoint glyphPosition(fragment.x, fragment.y); 305 FloatPoint glyphPosition(fragment.x, fragment.y);
341 if (queryData->isVerticalText) 306 if (queryData->isVerticalText)
342 glyphPosition.move(0, glyphOffsetInDirection); 307 glyphPosition.move(0, glyphOffsetInDirection);
343 else 308 else
344 glyphPosition.move(glyphOffsetInDirection, 0); 309 glyphPosition.move(glyphOffsetInDirection, 0);
345 310
346 return glyphPosition; 311 return glyphPosition;
347 } 312 }
348 313
349 static FloatPoint calculateGlyphPosition(const QueryData* queryData, const SVGTe xtFragment& fragment, int offsetInFragment) 314 static FloatPoint calculateGlyphPosition(const QueryData* queryData, const SVGTe xtFragment& fragment, unsigned offsetInFragment)
350 { 315 {
351 FloatPoint glyphPosition = calculateGlyphPositionWithoutTransform(queryData, fragment, offsetInFragment); 316 FloatPoint glyphPosition = calculateGlyphPositionWithoutTransform(queryData, fragment, offsetInFragment);
352 if (fragment.isTransformed()) { 317 if (fragment.isTransformed()) {
353 AffineTransform fragmentTransform = fragment.buildFragmentTransform(SVGT extFragment::TransformIgnoringTextLength); 318 AffineTransform fragmentTransform = fragment.buildFragmentTransform(SVGT extFragment::TransformIgnoringTextLength);
354 glyphPosition = fragmentTransform.mapPoint(glyphPosition); 319 glyphPosition = fragmentTransform.mapPoint(glyphPosition);
355 } 320 }
356 return glyphPosition; 321 return glyphPosition;
357 } 322 }
358 323
359 static bool startPositionOfCharacterCallback(QueryData* queryData, const SVGText Fragment& fragment) 324 static bool startPositionOfCharacterCallback(QueryData* queryData, const SVGText Fragment& fragment)
(...skipping 29 matching lines...) Expand all
389 354
390 static bool endPositionOfCharacterCallback(QueryData* queryData, const SVGTextFr agment& fragment) 355 static bool endPositionOfCharacterCallback(QueryData* queryData, const SVGTextFr agment& fragment)
391 { 356 {
392 EndPositionOfCharacterData* data = static_cast<EndPositionOfCharacterData*>( queryData); 357 EndPositionOfCharacterData* data = static_cast<EndPositionOfCharacterData*>( queryData);
393 358
394 int startPosition = data->position; 359 int startPosition = data->position;
395 int endPosition = startPosition + 1; 360 int endPosition = startPosition + 1;
396 if (!mapStartEndPositionsIntoFragmentCoordinates(queryData, fragment, startP osition, endPosition)) 361 if (!mapStartEndPositionsIntoFragmentCoordinates(queryData, fragment, startP osition, endPosition))
397 return false; 362 return false;
398 363
399 // TODO(fs): mapStartEndPositionsIntoFragmentCoordinates(...) above applies 364 data->endPosition = calculateGlyphPosition(queryData, fragment, endPosition) ;
400 // some heuristics for ligatures, so why not just use endPosition here?
401 // (rather than startPosition+1)
402 data->endPosition = calculateGlyphPosition(queryData, fragment, startPositio n + 1);
403 return true; 365 return true;
404 } 366 }
405 367
406 FloatPoint SVGTextQuery::endPositionOfCharacter(unsigned position) const 368 FloatPoint SVGTextQuery::endPositionOfCharacter(unsigned position) const
407 { 369 {
408 EndPositionOfCharacterData data(position); 370 EndPositionOfCharacterData data(position);
409 logicalQuery(m_queryRootLayoutObject, &data, endPositionOfCharacterCallback) ; 371 logicalQuery(m_queryRootLayoutObject, &data, endPositionOfCharacterCallback) ;
410 return data.endPosition; 372 return data.endPosition;
411 } 373 }
412 374
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
463 { 425 {
464 float scalingFactor = queryData->textLineLayout.scalingFactor(); 426 float scalingFactor = queryData->textLineLayout.scalingFactor();
465 ASSERT(scalingFactor); 427 ASSERT(scalingFactor);
466 428
467 FloatPoint glyphPosition = calculateGlyphPositionWithoutTransform(queryData, fragment, startPosition); 429 FloatPoint glyphPosition = calculateGlyphPositionWithoutTransform(queryData, fragment, startPosition);
468 glyphPosition.move(0, -queryData->textLineLayout.scaledFont().getFontMetrics ().floatAscent() / scalingFactor); 430 glyphPosition.move(0, -queryData->textLineLayout.scaledFont().getFontMetrics ().floatAscent() / scalingFactor);
469 431
470 // Use the SVGTextMetrics computed by SVGTextMetricsBuilder (which spends 432 // Use the SVGTextMetrics computed by SVGTextMetricsBuilder (which spends
471 // time attempting to compute more correct glyph bounds already, handling 433 // time attempting to compute more correct glyph bounds already, handling
472 // cursive scripts to some degree.) 434 // cursive scripts to some degree.)
473 const Vector<SVGTextMetrics>& textMetricsValues = queryData->textLineLayout. layoutAttributes()->textMetricsValues(); 435 const MetricsList& textMetricsValues = queryData->textLineLayout.layoutAttri butes()->textMetricsValues();
474 const SVGTextMetrics& metrics = findMetricsForCharacter(textMetricsValues, f ragment, startPosition); 436 auto metrics = findMetricsForCharacter(textMetricsValues, fragment, startPos ition);
475 437
476 // TODO(fs): Negative glyph extents seems kind of weird to have, but 438 // TODO(fs): Negative glyph extents seems kind of weird to have, but
477 // presently it can occur in some cases (like Arabic.) 439 // presently it can occur in some cases (like Arabic.)
478 FloatSize glyphSize(std::max<float>(metrics.width(), 0), std::max<float>(met rics.height(), 0)); 440 FloatSize glyphSize(std::max<float>(metrics->width(), 0), std::max<float>(me trics->height(), 0));
479 441
480 // If RTL, adjust the starting point to align with the LHS of the glyph boun ding box. 442 // If RTL, adjust the starting point to align with the LHS of the glyph boun ding box.
481 if (!queryData->textBox->isLeftToRightDirection()) { 443 if (!queryData->textBox->isLeftToRightDirection()) {
482 if (queryData->isVerticalText) 444 if (queryData->isVerticalText)
483 glyphPosition.move(0, -glyphSize.height()); 445 glyphPosition.move(0, -glyphSize.height());
484 else 446 else
485 glyphPosition.move(-glyphSize.width(), 0); 447 glyphPosition.move(-glyphSize.width(), 0);
486 } 448 }
487 449
488 FloatRect extent(glyphPosition, glyphSize); 450 FloatRect extent(glyphPosition, glyphSize);
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after
604 } 566 }
605 567
606 int SVGTextQuery::characterNumberAtPosition(const FloatPoint& position) const 568 int SVGTextQuery::characterNumberAtPosition(const FloatPoint& position) const
607 { 569 {
608 CharacterNumberAtPositionData data(position); 570 CharacterNumberAtPositionData data(position);
609 spatialQuery(m_queryRootLayoutObject, &data, characterNumberAtPositionCallba ck); 571 spatialQuery(m_queryRootLayoutObject, &data, characterNumberAtPositionCallba ck);
610 return data.characterNumberWithin(m_queryRootLayoutObject); 572 return data.characterNumberWithin(m_queryRootLayoutObject);
611 } 573 }
612 574
613 } // namespace blink 575 } // namespace blink
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698