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

Side by Side Diff: Source/core/layout/svg/SVGTextLayoutEngine.cpp

Issue 1160623002: Avoid resetting the metrics-list/character offset for each text box (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Rebase. Created 5 years, 7 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 | Annotate | Revision Log
« no previous file with comments | « Source/core/layout/svg/SVGTextLayoutEngine.h ('k') | 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 19 matching lines...) Expand all
30 #include "core/svg/SVGLengthContext.h" 30 #include "core/svg/SVGLengthContext.h"
31 #include "core/svg/SVGTextContentElement.h" 31 #include "core/svg/SVGTextContentElement.h"
32 32
33 namespace blink { 33 namespace blink {
34 34
35 SVGTextLayoutEngine::SVGTextLayoutEngine(Vector<SVGTextLayoutAttributes*>& layou tAttributes) 35 SVGTextLayoutEngine::SVGTextLayoutEngine(Vector<SVGTextLayoutAttributes*>& layou tAttributes)
36 : m_layoutAttributes(layoutAttributes) 36 : m_layoutAttributes(layoutAttributes)
37 , m_layoutAttributesPosition(0) 37 , m_layoutAttributesPosition(0)
38 , m_logicalCharacterOffset(0) 38 , m_logicalCharacterOffset(0)
39 , m_logicalMetricsListOffset(0) 39 , m_logicalMetricsListOffset(0)
40 , m_visualCharacterOffset(0)
41 , m_visualMetricsListOffset(0)
42 , m_x(0) 40 , m_x(0)
43 , m_y(0) 41 , m_y(0)
44 , m_dx(0) 42 , m_dx(0)
45 , m_dy(0) 43 , m_dy(0)
46 , m_isVerticalText(false) 44 , m_isVerticalText(false)
47 , m_inPathLayout(false) 45 , m_inPathLayout(false)
48 , m_textPathCalculator(0) 46 , m_textPathCalculator(0)
49 , m_textPathLength(0) 47 , m_textPathLength(0)
50 , m_textPathCurrentOffset(0) 48 , m_textPathCurrentOffset(0)
51 , m_textPathSpacing(0) 49 , m_textPathSpacing(0)
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
103 m_dy += dy; 101 m_dy += dy;
104 } 102 }
105 103
106 return; 104 return;
107 } 105 }
108 106
109 m_dx = dx; 107 m_dx = dx;
110 m_dy = dy; 108 m_dy = dy;
111 } 109 }
112 110
113 void SVGTextLayoutEngine::recordTextFragment(SVGInlineTextBox* textBox, const Ve ctor<SVGTextMetrics>& textMetricsValues) 111 void SVGTextLayoutEngine::recordTextFragment(SVGInlineTextBox* textBox)
114 { 112 {
115 ASSERT(!m_currentTextFragment.length); 113 ASSERT(!m_currentTextFragment.length);
116 ASSERT(m_visualMetricsListOffset > 0);
117 114
118 // Figure out length of fragment. 115 // Figure out length of fragment.
119 m_currentTextFragment.length = m_visualCharacterOffset - m_currentTextFragme nt.characterOffset; 116 m_currentTextFragment.length = m_visualMetricsIterator.characterOffset() - m _currentTextFragment.characterOffset;
120 117
121 // Figure out fragment metrics. 118 // Figure out fragment metrics.
122 const SVGTextMetrics& lastCharacterMetrics = textMetricsValues.at(m_visualMe tricsListOffset - 1); 119 const unsigned visualMetricsListOffset = m_visualMetricsIterator.metricsList Offset();
120 const Vector<SVGTextMetrics>& textMetricsValues = m_visualMetricsIterator.me tricsList();
121 const SVGTextMetrics& lastCharacterMetrics = textMetricsValues.at(visualMetr icsListOffset - 1);
123 m_currentTextFragment.width = lastCharacterMetrics.width(); 122 m_currentTextFragment.width = lastCharacterMetrics.width();
124 m_currentTextFragment.height = lastCharacterMetrics.height(); 123 m_currentTextFragment.height = lastCharacterMetrics.height();
125 124
126 if (m_currentTextFragment.length > 1) { 125 if (m_currentTextFragment.length > 1) {
127 // SVGTextLayoutAttributesBuilder assures that the length of the range i s equal to the sum of the individual lengths of the glyphs. 126 // SVGTextLayoutAttributesBuilder assures that the length of the range i s equal to the sum of the individual lengths of the glyphs.
128 float length = 0; 127 float length = 0;
129 if (m_isVerticalText) { 128 if (m_isVerticalText) {
130 for (unsigned i = m_currentTextFragment.metricsListOffset; i < m_vis ualMetricsListOffset; ++i) 129 for (unsigned i = m_currentTextFragment.metricsListOffset; i < visua lMetricsListOffset; ++i)
131 length += textMetricsValues.at(i).height(); 130 length += textMetricsValues.at(i).height();
132 m_currentTextFragment.height = length; 131 m_currentTextFragment.height = length;
133 } else { 132 } else {
134 for (unsigned i = m_currentTextFragment.metricsListOffset; i < m_vis ualMetricsListOffset; ++i) 133 for (unsigned i = m_currentTextFragment.metricsListOffset; i < visua lMetricsListOffset; ++i)
135 length += textMetricsValues.at(i).width(); 134 length += textMetricsValues.at(i).width();
136 m_currentTextFragment.width = length; 135 m_currentTextFragment.width = length;
137 } 136 }
138 } 137 }
139 138
140 textBox->textFragments().append(m_currentTextFragment); 139 textBox->textFragments().append(m_currentTextFragment);
141 m_currentTextFragment = SVGTextFragment(); 140 m_currentTextFragment = SVGTextFragment();
142 } 141 }
143 142
144 bool SVGTextLayoutEngine::parentDefinesTextLength(LayoutObject* parent) const 143 bool SVGTextLayoutEngine::parentDefinesTextLength(LayoutObject* parent) const
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after
253 if (m_inPathLayout) { 252 if (m_inPathLayout) {
254 m_pathLayoutBoxes.append(textBox); 253 m_pathLayoutBoxes.append(textBox);
255 return; 254 return;
256 } 255 }
257 256
258 m_lineLayoutBoxes.append(textBox); 257 m_lineLayoutBoxes.append(textBox);
259 } 258 }
260 259
261 void SVGTextLayoutEngine::finishLayout() 260 void SVGTextLayoutEngine::finishLayout()
262 { 261 {
262 m_visualMetricsIterator = SVGInlineTextMetricsIterator();
263
263 // After all text fragments are stored in their correpsonding SVGInlineTextB oxes, we can layout individual text chunks. 264 // After all text fragments are stored in their correpsonding SVGInlineTextB oxes, we can layout individual text chunks.
264 // Chunk layouting is only performed for line layout boxes, not for path lay out, where it has already been done. 265 // Chunk layouting is only performed for line layout boxes, not for path lay out, where it has already been done.
265 SVGTextChunkBuilder chunkLayoutBuilder; 266 SVGTextChunkBuilder chunkLayoutBuilder;
266 chunkLayoutBuilder.layoutTextChunks(m_lineLayoutBoxes); 267 chunkLayoutBuilder.layoutTextChunks(m_lineLayoutBoxes);
267 268
268 // Finalize transform matrices, after the chunk layout corrections have been applied, and all fragment x/y positions are finalized. 269 // Finalize transform matrices, after the chunk layout corrections have been applied, and all fragment x/y positions are finalized.
269 if (!m_lineLayoutBoxes.isEmpty()) { 270 if (!m_lineLayoutBoxes.isEmpty()) {
270 chunkLayoutBuilder.finalizeTransformMatrices(m_lineLayoutBoxes); 271 chunkLayoutBuilder.finalizeTransformMatrices(m_lineLayoutBoxes);
271 m_lineLayoutBoxes.clear(); 272 m_lineLayoutBoxes.clear();
272 } 273 }
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
321 } 322 }
322 323
323 // Stop if we found the next valid logical text metrics object. 324 // Stop if we found the next valid logical text metrics object.
324 return true; 325 return true;
325 } 326 }
326 327
327 ASSERT_NOT_REACHED(); 328 ASSERT_NOT_REACHED();
328 return true; 329 return true;
329 } 330 }
330 331
331 bool SVGTextLayoutEngine::currentVisualCharacterMetrics(SVGInlineTextBox* textBo x, const Vector<SVGTextMetrics>& visualMetricsValues, SVGTextMetrics& visualMetr ics)
332 {
333 ASSERT(!visualMetricsValues.isEmpty());
334 unsigned textMetricsSize = visualMetricsValues.size();
335 unsigned boxStart = textBox->start();
336 unsigned boxLength = textBox->len();
337
338 while (m_visualMetricsListOffset < textMetricsSize) {
339 // Advance to text box start location.
340 if (m_visualCharacterOffset < boxStart) {
341 advanceToNextVisualCharacter(visualMetricsValues[m_visualMetricsList Offset]);
342 continue;
343 }
344
345 // Stop if we've finished processing this text box.
346 if (m_visualCharacterOffset >= boxStart + boxLength)
347 return false;
348
349 visualMetrics = visualMetricsValues[m_visualMetricsListOffset];
350 return true;
351 }
352
353 return false;
354 }
355
356 void SVGTextLayoutEngine::advanceToNextLogicalCharacter(const SVGTextMetrics& lo gicalMetrics) 332 void SVGTextLayoutEngine::advanceToNextLogicalCharacter(const SVGTextMetrics& lo gicalMetrics)
357 { 333 {
358 ++m_logicalMetricsListOffset; 334 ++m_logicalMetricsListOffset;
359 m_logicalCharacterOffset += logicalMetrics.length(); 335 m_logicalCharacterOffset += logicalMetrics.length();
360 } 336 }
361 337
362 void SVGTextLayoutEngine::advanceToNextVisualCharacter(const SVGTextMetrics& vis ualMetrics)
363 {
364 ++m_visualMetricsListOffset;
365 m_visualCharacterOffset += visualMetrics.length();
366 }
367
368 void SVGTextLayoutEngine::layoutTextOnLineOrPath(SVGInlineTextBox* textBox, cons t LayoutSVGInlineText& text, const ComputedStyle& style) 338 void SVGTextLayoutEngine::layoutTextOnLineOrPath(SVGInlineTextBox* textBox, cons t LayoutSVGInlineText& text, const ComputedStyle& style)
369 { 339 {
370 if (m_inPathLayout && !m_textPathCalculator) 340 if (m_inPathLayout && !m_textPathCalculator)
371 return; 341 return;
372 342
373 LayoutObject* textParent = text.parent(); 343 LayoutObject* textParent = text.parent();
374 bool definesTextLength = textParent ? parentDefinesTextLength(textParent) : false; 344 bool definesTextLength = textParent ? parentDefinesTextLength(textParent) : false;
375 345
376 const SVGComputedStyle& svgStyle = style.svgStyle(); 346 const SVGComputedStyle& svgStyle = style.svgStyle();
377 347
378 m_visualMetricsListOffset = 0; 348 // Find the start of the current text box in the metrics list.
379 m_visualCharacterOffset = 0; 349 m_visualMetricsIterator.advanceToTextStart(&text, textBox->start());
380
381 const Vector<SVGTextMetrics>& visualMetricsValues = text.layoutAttributes()- >textMetricsValues();
382 ASSERT(!visualMetricsValues.isEmpty());
383 350
384 const Font& font = style.font(); 351 const Font& font = style.font();
385 352
386 SVGTextLayoutEngineSpacing spacingLayout(font, style.effectiveZoom()); 353 SVGTextLayoutEngineSpacing spacingLayout(font, style.effectiveZoom());
387 SVGTextLayoutEngineBaseline baselineLayout(font, style.effectiveZoom()); 354 SVGTextLayoutEngineBaseline baselineLayout(font, style.effectiveZoom());
388 355
389 bool didStartTextFragment = false; 356 bool didStartTextFragment = false;
390 bool applySpacingToNextCharacter = false; 357 bool applySpacingToNextCharacter = false;
391 358
392 float lastAngle = 0; 359 float lastAngle = 0;
393 float baselineShift = baselineLayout.calculateBaselineShift(style); 360 float baselineShift = baselineLayout.calculateBaselineShift(style);
394 baselineShift -= baselineLayout.calculateAlignmentBaselineShift(m_isVertical Text, &text); 361 baselineShift -= baselineLayout.calculateAlignmentBaselineShift(m_isVertical Text, &text);
395 362
396 // Main layout algorithm. 363 // Main layout algorithm.
397 while (true) { 364 const unsigned boxEndOffset = textBox->start() + textBox->len();
398 // Find the start of the current text box in this list, respecting ligat ures. 365 while (m_visualMetricsIterator.characterOffset() < boxEndOffset) {
399 SVGTextMetrics visualMetrics(SVGTextMetrics::SkippedSpaceMetrics); 366 const SVGTextMetrics& visualMetrics = m_visualMetricsIterator.metrics();
400 if (!currentVisualCharacterMetrics(textBox, visualMetricsValues, visualM etrics))
401 break;
402
403 if (visualMetrics.isEmpty()) { 367 if (visualMetrics.isEmpty()) {
404 advanceToNextVisualCharacter(visualMetrics); 368 m_visualMetricsIterator.next();
405 continue; 369 continue;
406 } 370 }
407 371
408 SVGTextLayoutAttributes* logicalAttributes = 0; 372 SVGTextLayoutAttributes* logicalAttributes = 0;
409 if (!currentLogicalCharacterAttributes(logicalAttributes)) 373 if (!currentLogicalCharacterAttributes(logicalAttributes))
410 break; 374 break;
411 375
412 ASSERT(logicalAttributes); 376 ASSERT(logicalAttributes);
413 SVGTextMetrics logicalMetrics(SVGTextMetrics::SkippedSpaceMetrics); 377 SVGTextMetrics logicalMetrics(SVGTextMetrics::SkippedSpaceMetrics);
414 if (!currentLogicalCharacterMetrics(logicalAttributes, logicalMetrics)) 378 if (!currentLogicalCharacterMetrics(logicalAttributes, logicalMetrics))
415 break; 379 break;
416 380
417 SVGCharacterDataMap& characterDataMap = logicalAttributes->characterData Map(); 381 SVGCharacterDataMap& characterDataMap = logicalAttributes->characterData Map();
418 SVGCharacterData data; 382 SVGCharacterData data;
419 SVGCharacterDataMap::iterator it = characterDataMap.find(m_logicalCharac terOffset + 1); 383 SVGCharacterDataMap::iterator it = characterDataMap.find(m_logicalCharac terOffset + 1);
420 if (it != characterDataMap.end()) 384 if (it != characterDataMap.end())
421 data = it->value; 385 data = it->value;
422 386
423 float x = data.x; 387 float x = data.x;
424 float y = data.y; 388 float y = data.y;
425 389
426 // When we've advanced to the box start offset, determine using the orig inal x/y values, 390 // When we've advanced to the box start offset, determine using the orig inal x/y values,
427 // whether this character starts a new text chunk, before doing any furt her processing. 391 // whether this character starts a new text chunk, before doing any furt her processing.
428 if (m_visualCharacterOffset == textBox->start()) 392 if (m_visualMetricsIterator.characterOffset() == textBox->start())
429 textBox->setStartsNewTextChunk(logicalAttributes->context()->charact erStartsNewTextChunk(m_logicalCharacterOffset)); 393 textBox->setStartsNewTextChunk(logicalAttributes->context()->charact erStartsNewTextChunk(m_logicalCharacterOffset));
430 394
431 float angle = SVGTextLayoutAttributes::isEmptyValue(data.rotate) ? 0 : d ata.rotate; 395 float angle = SVGTextLayoutAttributes::isEmptyValue(data.rotate) ? 0 : d ata.rotate;
432 396
433 // Calculate glyph orientation angle. 397 // Calculate glyph orientation angle.
434 UChar currentCharacter = text.characterAt(m_visualCharacterOffset); 398 UChar currentCharacter = text.characterAt(m_visualMetricsIterator.charac terOffset());
435 float orientationAngle = baselineLayout.calculateGlyphOrientationAngle(m _isVerticalText, svgStyle, currentCharacter); 399 float orientationAngle = baselineLayout.calculateGlyphOrientationAngle(m _isVerticalText, svgStyle, currentCharacter);
436 400
437 // Calculate glyph advance & x/y orientation shifts. 401 // Calculate glyph advance & x/y orientation shifts.
438 float xOrientationShift = 0; 402 float xOrientationShift = 0;
439 float yOrientationShift = 0; 403 float yOrientationShift = 0;
440 float glyphAdvance = baselineLayout.calculateGlyphAdvanceAndOrientation( m_isVerticalText, visualMetrics, orientationAngle, xOrientationShift, yOrientati onShift); 404 float glyphAdvance = baselineLayout.calculateGlyphAdvanceAndOrientation( m_isVerticalText, visualMetrics, orientationAngle, xOrientationShift, yOrientati onShift);
441 405
442 // Assign current text position to x/y values, if needed. 406 // Assign current text position to x/y values, if needed.
443 updateCharacterPositionIfNeeded(x, y); 407 updateCharacterPositionIfNeeded(x, y);
444 408
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
477 441
478 // Calculate current offset along path. 442 // Calculate current offset along path.
479 textPathOffset = m_textPathCurrentOffset + scaledGlyphAdvance / 2; 443 textPathOffset = m_textPathCurrentOffset + scaledGlyphAdvance / 2;
480 444
481 // Move to next character. 445 // Move to next character.
482 m_textPathCurrentOffset += scaledGlyphAdvance + m_textPathSpacing + spacing * m_textPathScaling; 446 m_textPathCurrentOffset += scaledGlyphAdvance + m_textPathSpacing + spacing * m_textPathScaling;
483 447
484 // Skip character, if we're before the path. 448 // Skip character, if we're before the path.
485 if (textPathOffset < 0) { 449 if (textPathOffset < 0) {
486 advanceToNextLogicalCharacter(logicalMetrics); 450 advanceToNextLogicalCharacter(logicalMetrics);
487 advanceToNextVisualCharacter(visualMetrics); 451 m_visualMetricsIterator.next();
488 continue; 452 continue;
489 } 453 }
490 454
491 // Stop processing, if the next character lies behind the path. 455 // Stop processing, if the next character lies behind the path.
492 if (textPathOffset > m_textPathLength) 456 if (textPathOffset > m_textPathLength)
493 break; 457 break;
494 458
495 FloatPoint point; 459 FloatPoint point;
496 bool ok = m_textPathCalculator->pointAndNormalAtLength(textPathOffse t, point, angle); 460 bool ok = m_textPathCalculator->pointAndNormalAtLength(textPathOffse t, point, angle);
497 ASSERT_UNUSED(ok, ok); 461 ASSERT_UNUSED(ok, ok);
(...skipping 14 matching lines...) Expand all
512 y += m_dy; 476 y += m_dy;
513 } 477 }
514 478
515 // Determine whether we have to start a new fragment. 479 // Determine whether we have to start a new fragment.
516 bool shouldStartNewFragment = m_dx || m_dy || m_isVerticalText || m_inPa thLayout || angle || angle != lastAngle 480 bool shouldStartNewFragment = m_dx || m_dy || m_isVerticalText || m_inPa thLayout || angle || angle != lastAngle
517 || orientationAngle || applySpacingToNextCharacter || definesTextLen gth; 481 || orientationAngle || applySpacingToNextCharacter || definesTextLen gth;
518 482
519 // If we already started a fragment, close it now. 483 // If we already started a fragment, close it now.
520 if (didStartTextFragment && shouldStartNewFragment) { 484 if (didStartTextFragment && shouldStartNewFragment) {
521 applySpacingToNextCharacter = false; 485 applySpacingToNextCharacter = false;
522 recordTextFragment(textBox, visualMetricsValues); 486 recordTextFragment(textBox);
523 } 487 }
524 488
525 // Eventually start a new fragment, if not yet done. 489 // Eventually start a new fragment, if not yet done.
526 if (!didStartTextFragment || shouldStartNewFragment) { 490 if (!didStartTextFragment || shouldStartNewFragment) {
527 ASSERT(!m_currentTextFragment.characterOffset); 491 ASSERT(!m_currentTextFragment.characterOffset);
528 ASSERT(!m_currentTextFragment.length); 492 ASSERT(!m_currentTextFragment.length);
529 493
530 didStartTextFragment = true; 494 didStartTextFragment = true;
531 m_currentTextFragment.characterOffset = m_visualCharacterOffset; 495 m_currentTextFragment.characterOffset = m_visualMetricsIterator.char acterOffset();
532 m_currentTextFragment.metricsListOffset = m_visualMetricsListOffset; 496 m_currentTextFragment.metricsListOffset = m_visualMetricsIterator.me tricsListOffset();
533 m_currentTextFragment.x = x; 497 m_currentTextFragment.x = x;
534 m_currentTextFragment.y = y; 498 m_currentTextFragment.y = y;
535 499
536 // Build fragment transformation. 500 // Build fragment transformation.
537 if (angle) 501 if (angle)
538 m_currentTextFragment.transform.rotate(angle); 502 m_currentTextFragment.transform.rotate(angle);
539 503
540 if (xOrientationShift || yOrientationShift) 504 if (xOrientationShift || yOrientationShift)
541 m_currentTextFragment.transform.translate(xOrientationShift, yOr ientationShift); 505 m_currentTextFragment.transform.translate(xOrientationShift, yOr ientationShift);
542 506
(...skipping 22 matching lines...) Expand all
565 529
566 if (m_isVerticalText) 530 if (m_isVerticalText)
567 xNew -= baselineShift; 531 xNew -= baselineShift;
568 else 532 else
569 yNew += baselineShift; 533 yNew += baselineShift;
570 534
571 updateCurrentTextPosition(xNew, yNew, glyphAdvance + spacing); 535 updateCurrentTextPosition(xNew, yNew, glyphAdvance + spacing);
572 } 536 }
573 537
574 advanceToNextLogicalCharacter(logicalMetrics); 538 advanceToNextLogicalCharacter(logicalMetrics);
575 advanceToNextVisualCharacter(visualMetrics); 539 m_visualMetricsIterator.next();
576 lastAngle = angle; 540 lastAngle = angle;
577 } 541 }
578 542
579 if (!didStartTextFragment) 543 if (!didStartTextFragment)
580 return; 544 return;
581 545
582 // Close last open fragment, if needed. 546 // Close last open fragment, if needed.
583 recordTextFragment(textBox, visualMetricsValues); 547 recordTextFragment(textBox);
584 } 548 }
585 549
586 } 550 }
OLDNEW
« no previous file with comments | « Source/core/layout/svg/SVGTextLayoutEngine.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698