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

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

Issue 1683903004: Factor out the <textPath> positioning mapping code into a helper class (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase. Created 4 years, 10 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 | « third_party/WebKit/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 27 matching lines...) Expand all
38 , m_layoutAttributesPosition(0) 38 , m_layoutAttributesPosition(0)
39 , m_logicalCharacterOffset(0) 39 , m_logicalCharacterOffset(0)
40 , m_logicalMetricsListOffset(0) 40 , m_logicalMetricsListOffset(0)
41 , m_x(0) 41 , m_x(0)
42 , m_y(0) 42 , m_y(0)
43 , m_dx(0) 43 , m_dx(0)
44 , m_dy(0) 44 , m_dy(0)
45 , m_isVerticalText(false) 45 , m_isVerticalText(false)
46 , m_inPathLayout(false) 46 , m_inPathLayout(false)
47 , m_textLengthSpacingInEffect(false) 47 , m_textLengthSpacingInEffect(false)
48 , m_textPathCalculator(nullptr) 48 , m_textPath(nullptr)
49 , m_textPathLength(0)
50 , m_textPathCurrentOffset(0) 49 , m_textPathCurrentOffset(0)
51 , m_textPathSpacing(0) 50 , m_textPathSpacing(0)
52 , m_textPathScaling(1) 51 , m_textPathScaling(1)
53 { 52 {
54 ASSERT(!m_layoutAttributes.isEmpty()); 53 ASSERT(!m_layoutAttributes.isEmpty());
55 } 54 }
56 55
56 SVGTextLayoutEngine::~SVGTextLayoutEngine() = default;
57
57 void SVGTextLayoutEngine::updateCharacterPositionIfNeeded(float& x, float& y) 58 void SVGTextLayoutEngine::updateCharacterPositionIfNeeded(float& x, float& y)
58 { 59 {
59 if (m_inPathLayout) 60 if (m_inPathLayout)
60 return; 61 return;
61 62
62 // Replace characters x/y position, with the current text position plus any 63 // Replace characters x/y position, with the current text position plus any
63 // relative adjustments, if it doesn't specify an absolute position itself. 64 // relative adjustments, if it doesn't specify an absolute position itself.
64 if (SVGTextLayoutAttributes::isEmptyValue(x)) 65 if (SVGTextLayoutAttributes::isEmptyValue(x))
65 x = m_x + m_dx; 66 x = m_x + m_dx;
66 67
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after
167 { 168 {
168 // Build text chunks for all <textPath> children, using the line layout algo rithm. 169 // Build text chunks for all <textPath> children, using the line layout algo rithm.
169 // This is needeed as text-anchor is just an additional startOffset for text paths. 170 // This is needeed as text-anchor is just an additional startOffset for text paths.
170 SVGTextLayoutEngine lineLayout(m_layoutAttributes); 171 SVGTextLayoutEngine lineLayout(m_layoutAttributes);
171 lineLayout.m_textLengthSpacingInEffect = m_textLengthSpacingInEffect; 172 lineLayout.m_textLengthSpacingInEffect = m_textLengthSpacingInEffect;
172 lineLayout.layoutCharactersInTextBoxes(flowBox); 173 lineLayout.layoutCharactersInTextBoxes(flowBox);
173 174
174 m_inPathLayout = true; 175 m_inPathLayout = true;
175 LineLayoutSVGTextPath textPath = LineLayoutSVGTextPath(flowBox->lineLayoutIt em()); 176 LineLayoutSVGTextPath textPath = LineLayoutSVGTextPath(flowBox->lineLayoutIt em());
176 177
177 Path path = textPath.layoutPath(); 178 m_textPath = textPath.layoutPath();
178 if (path.isEmpty()) 179 if (!m_textPath)
179 return; 180 return;
180 m_textPathCalculator = new Path::PositionCalculator(path); 181 m_textPathStartOffset = textPath.calculateStartOffset(m_textPath->length());
181 m_textPathLength = path.length();
182 m_textPathStartOffset = textPath.calculateStartOffset(m_textPathLength);
183 182
184 SVGTextPathChunkBuilder textPathChunkLayoutBuilder; 183 SVGTextPathChunkBuilder textPathChunkLayoutBuilder;
185 textPathChunkLayoutBuilder.processTextChunks(lineLayout.m_lineLayoutBoxes); 184 textPathChunkLayoutBuilder.processTextChunks(lineLayout.m_lineLayoutBoxes);
186 185
187 m_textPathStartOffset += textPathChunkLayoutBuilder.totalTextAnchorShift(); 186 m_textPathStartOffset += textPathChunkLayoutBuilder.totalTextAnchorShift();
188 m_textPathCurrentOffset = m_textPathStartOffset; 187 m_textPathCurrentOffset = m_textPathStartOffset;
189 188
190 // Eventually handle textLength adjustments. 189 // Eventually handle textLength adjustments.
191 SVGLengthAdjustType lengthAdjust = SVGLengthAdjustUnknown; 190 SVGLengthAdjustType lengthAdjust = SVGLengthAdjustUnknown;
192 float desiredTextLength = 0; 191 float desiredTextLength = 0;
(...skipping 13 matching lines...) Expand all
206 float totalLength = textPathChunkLayoutBuilder.totalLength(); 205 float totalLength = textPathChunkLayoutBuilder.totalLength();
207 if (lengthAdjust == SVGLengthAdjustSpacing) 206 if (lengthAdjust == SVGLengthAdjustSpacing)
208 m_textPathSpacing = (desiredTextLength - totalLength) / textPathChunkLay outBuilder.totalCharacters(); 207 m_textPathSpacing = (desiredTextLength - totalLength) / textPathChunkLay outBuilder.totalCharacters();
209 else 208 else
210 m_textPathScaling = desiredTextLength / totalLength; 209 m_textPathScaling = desiredTextLength / totalLength;
211 } 210 }
212 211
213 void SVGTextLayoutEngine::endTextPathLayout() 212 void SVGTextLayoutEngine::endTextPathLayout()
214 { 213 {
215 m_inPathLayout = false; 214 m_inPathLayout = false;
216 delete m_textPathCalculator; 215 m_textPath = nullptr;
217 m_textPathCalculator = 0;
218 m_textPathLength = 0;
219 m_textPathStartOffset = 0; 216 m_textPathStartOffset = 0;
220 m_textPathCurrentOffset = 0; 217 m_textPathCurrentOffset = 0;
221 m_textPathSpacing = 0; 218 m_textPathSpacing = 0;
222 m_textPathScaling = 1; 219 m_textPathScaling = 1;
223 } 220 }
224 221
225 void SVGTextLayoutEngine::layoutInlineTextBox(SVGInlineTextBox* textBox) 222 void SVGTextLayoutEngine::layoutInlineTextBox(SVGInlineTextBox* textBox)
226 { 223 {
227 ASSERT(textBox); 224 ASSERT(textBox);
228 225
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after
343 } 340 }
344 341
345 void SVGTextLayoutEngine::advanceToNextLogicalCharacter(const SVGTextMetrics& lo gicalMetrics) 342 void SVGTextLayoutEngine::advanceToNextLogicalCharacter(const SVGTextMetrics& lo gicalMetrics)
346 { 343 {
347 ++m_logicalMetricsListOffset; 344 ++m_logicalMetricsListOffset;
348 m_logicalCharacterOffset += logicalMetrics.length(); 345 m_logicalCharacterOffset += logicalMetrics.length();
349 } 346 }
350 347
351 void SVGTextLayoutEngine::layoutTextOnLineOrPath(SVGInlineTextBox* textBox, Line LayoutSVGInlineText textLineLayout, const ComputedStyle& style) 348 void SVGTextLayoutEngine::layoutTextOnLineOrPath(SVGInlineTextBox* textBox, Line LayoutSVGInlineText textLineLayout, const ComputedStyle& style)
352 { 349 {
353 if (m_inPathLayout && !m_textPathCalculator) 350 if (m_inPathLayout && !m_textPath)
354 return; 351 return;
355 352
356 // Find the start of the current text box in the metrics list. 353 // Find the start of the current text box in the metrics list.
357 m_visualMetricsIterator.advanceToTextStart(textLineLayout, textBox->start()) ; 354 m_visualMetricsIterator.advanceToTextStart(textLineLayout, textBox->start()) ;
358 355
359 const Font& font = style.font(); 356 const Font& font = style.font();
360 357
361 SVGTextLayoutEngineSpacing spacingLayout(font, style.effectiveZoom()); 358 SVGTextLayoutEngineSpacing spacingLayout(font, style.effectiveZoom());
362 SVGTextLayoutEngineBaseline baselineLayout(font, style.effectiveZoom()); 359 SVGTextLayoutEngineBaseline baselineLayout(font, style.effectiveZoom());
363 360
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
415 412
416 // Assign current text position to x/y values, if needed. 413 // Assign current text position to x/y values, if needed.
417 updateCharacterPositionIfNeeded(x, y); 414 updateCharacterPositionIfNeeded(x, y);
418 415
419 // Apply dx/dy value adjustments to current text position, if needed. 416 // Apply dx/dy value adjustments to current text position, if needed.
420 updateRelativePositionAdjustmentsIfNeeded(data.dx, data.dy); 417 updateRelativePositionAdjustmentsIfNeeded(data.dx, data.dy);
421 418
422 // Calculate CSS 'letter-spacing' and 'word-spacing' for next character, if needed. 419 // Calculate CSS 'letter-spacing' and 'word-spacing' for next character, if needed.
423 float spacing = spacingLayout.calculateCSSSpacing(currentCharacter); 420 float spacing = spacingLayout.calculateCSSSpacing(currentCharacter);
424 421
425 float textPathOffset = 0;
426 float textPathShiftX = 0; 422 float textPathShiftX = 0;
427 float textPathShiftY = 0; 423 float textPathShiftY = 0;
428 if (m_inPathLayout) { 424 if (m_inPathLayout) {
429 float scaledGlyphAdvance = glyphAdvance * m_textPathScaling; 425 float scaledGlyphAdvance = glyphAdvance * m_textPathScaling;
430 if (m_isVerticalText) { 426 if (m_isVerticalText) {
431 // If there's an absolute y position available, it marks the beg inning of a new position along the path. 427 // If there's an absolute y position available, it marks the beg inning of a new position along the path.
432 if (!SVGTextLayoutAttributes::isEmptyValue(y)) 428 if (!SVGTextLayoutAttributes::isEmptyValue(y))
433 m_textPathCurrentOffset = y + m_textPathStartOffset; 429 m_textPathCurrentOffset = y + m_textPathStartOffset;
434 430
435 m_textPathCurrentOffset += m_dy; 431 m_textPathCurrentOffset += m_dy;
436 m_dy = 0; 432 m_dy = 0;
437 433
438 // Apply dx/dy correction and setup translations that move to th e glyph midpoint. 434 // Apply dx/dy correction and setup translations that move to th e glyph midpoint.
439 textPathShiftX += m_dx + baselineShift; 435 textPathShiftX += m_dx + baselineShift;
440 textPathShiftY -= scaledGlyphAdvance / 2; 436 textPathShiftY -= scaledGlyphAdvance / 2;
441 } else { 437 } else {
442 // If there's an absolute x position available, it marks the beg inning of a new position along the path. 438 // If there's an absolute x position available, it marks the beg inning of a new position along the path.
443 if (!SVGTextLayoutAttributes::isEmptyValue(x)) 439 if (!SVGTextLayoutAttributes::isEmptyValue(x))
444 m_textPathCurrentOffset = x + m_textPathStartOffset; 440 m_textPathCurrentOffset = x + m_textPathStartOffset;
445 441
446 m_textPathCurrentOffset += m_dx; 442 m_textPathCurrentOffset += m_dx;
447 m_dx = 0; 443 m_dx = 0;
448 444
449 // Apply dx/dy correction and setup translations that move to th e glyph midpoint. 445 // Apply dx/dy correction and setup translations that move to th e glyph midpoint.
450 textPathShiftX -= scaledGlyphAdvance / 2; 446 textPathShiftX -= scaledGlyphAdvance / 2;
451 textPathShiftY += m_dy - baselineShift; 447 textPathShiftY += m_dy - baselineShift;
452 } 448 }
453 449
454 // Calculate current offset along path. 450 // Calculate current offset along path.
455 textPathOffset = m_textPathCurrentOffset + scaledGlyphAdvance / 2; 451 float textPathOffset = m_textPathCurrentOffset + scaledGlyphAdvance / 2;
456 452
457 // Move to next character. 453 // Move to next character.
458 m_textPathCurrentOffset += scaledGlyphAdvance + m_textPathSpacing + spacing * m_textPathScaling; 454 m_textPathCurrentOffset += scaledGlyphAdvance + m_textPathSpacing + spacing * m_textPathScaling;
459 455
456 FloatPoint point;
457 PathPositionMapper::PositionType positionType = m_textPath->pointAnd NormalAtLength(textPathOffset, point, angle);
458
460 // Skip character, if we're before the path. 459 // Skip character, if we're before the path.
461 if (textPathOffset < 0) { 460 if (positionType == PathPositionMapper::BeforePath) {
462 advanceToNextLogicalCharacter(logicalMetrics); 461 advanceToNextLogicalCharacter(logicalMetrics);
463 m_visualMetricsIterator.next(); 462 m_visualMetricsIterator.next();
464 continue; 463 continue;
465 } 464 }
466 465
467 // Stop processing, if the next character lies behind the path. 466 // Stop processing if the next character lies behind the path.
468 if (textPathOffset > m_textPathLength) 467 if (positionType == PathPositionMapper::AfterPath)
469 break; 468 break;
470 469
471 FloatPoint point;
472 m_textPathCalculator->pointAndNormalAtLength(textPathOffset, point, angle);
473 x = point.x(); 470 x = point.x();
474 y = point.y(); 471 y = point.y();
475 472
476 // For vertical text on path, the actual angle has to be rotated 90 degrees anti-clockwise, not the orientation angle! 473 // For vertical text on path, the actual angle has to be rotated 90 degrees anti-clockwise, not the orientation angle!
477 if (m_isVerticalText) 474 if (m_isVerticalText)
478 angle -= 90; 475 angle -= 90;
479 } else { 476 } else {
480 // Apply all previously calculated shift values. 477 // Apply all previously calculated shift values.
481 if (m_isVerticalText) 478 if (m_isVerticalText)
482 x += baselineShift; 479 x += baselineShift;
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
551 } 548 }
552 549
553 if (!didStartTextFragment) 550 if (!didStartTextFragment)
554 return; 551 return;
555 552
556 // Close last open fragment, if needed. 553 // Close last open fragment, if needed.
557 recordTextFragment(textBox); 554 recordTextFragment(textBox);
558 } 555 }
559 556
560 } // namespace blink 557 } // namespace blink
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/core/layout/svg/SVGTextLayoutEngine.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698