2 * Copyright (C) Research In Motion Limited 2010-2012. All rights reserved.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public License
15 * along with this library; see the file COPYING.LIB. If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
22 #include "core/rendering/svg/SVGTextLayoutEngine.h"
24 #include "core/rendering/svg/RenderSVGInlineText.h"
25 #include "core/rendering/svg/RenderSVGTextPath.h"
26 #include "core/rendering/svg/SVGInlineTextBox.h"
27 #include "core/rendering/svg/SVGTextLayoutEngineBaseline.h"
28 #include "core/rendering/svg/SVGTextLayoutEngineSpacing.h"
29 #include "core/svg/SVGElement.h"
30 #include "core/svg/SVGLengthContext.h"
32 // Set to a value > 0 to dump the text fragments
33 #define DUMP_TEXT_FRAGMENTS 0
37 SVGTextLayoutEngine::SVGTextLayoutEngine(Vector<SVGTextLayoutAttributes*>& layoutAttributes)
38 : m_layoutAttributes(layoutAttributes)
39 , m_layoutAttributesPosition(0)
40 , m_logicalCharacterOffset(0)
41 , m_logicalMetricsListOffset(0)
42 , m_visualCharacterOffset(0)
43 , m_visualMetricsListOffset(0)
48 , m_isVerticalText(false)
49 , m_inPathLayout(false)
50 , m_textPathCalculator(0)
52 , m_textPathCurrentOffset(0)
53 , m_textPathSpacing(0)
54 , m_textPathScaling(1)
56 ASSERT(!m_layoutAttributes.isEmpty());
59 void SVGTextLayoutEngine::updateCharacerPositionIfNeeded(float& x, float& y)
64 // Replace characters x/y position, with the current text position plus any
65 // relative adjustments, if it doesn't specify an absolute position itself.
66 if (x == SVGTextLayoutAttributes::emptyValue())
69 if (y == SVGTextLayoutAttributes::emptyValue())
76 void SVGTextLayoutEngine::updateCurrentTextPosition(float x, float y, float glyphAdvance)
78 // Update current text position after processing the character.
79 if (m_isVerticalText) {
81 m_y = y + glyphAdvance;
83 m_x = x + glyphAdvance;
88 void SVGTextLayoutEngine::updateRelativePositionAdjustmentsIfNeeded(float dx, float dy)
90 // Update relative positioning information.
91 if (dx == SVGTextLayoutAttributes::emptyValue() && dy == SVGTextLayoutAttributes::emptyValue())
94 if (dx == SVGTextLayoutAttributes::emptyValue())
96 if (dy == SVGTextLayoutAttributes::emptyValue())
100 if (m_isVerticalText) {
115 void SVGTextLayoutEngine::recordTextFragment(SVGInlineTextBox* textBox, Vector<SVGTextMetrics>& textMetricsValues)
117 ASSERT(!m_currentTextFragment.length);
118 ASSERT(m_visualMetricsListOffset > 0);
120 // Figure out length of fragment.
121 m_currentTextFragment.length = m_visualCharacterOffset - m_currentTextFragment.characterOffset;
123 // Figure out fragment metrics.
124 SVGTextMetrics& lastCharacterMetrics = textMetricsValues.at(m_visualMetricsListOffset - 1);
125 m_currentTextFragment.width = lastCharacterMetrics.width();
126 m_currentTextFragment.height = lastCharacterMetrics.height();
128 if (m_currentTextFragment.length > 1) {
129 // SVGTextLayoutAttributesBuilder assures that the length of the range is equal to the sum of the individual lengths of the glyphs.
131 if (m_isVerticalText) {
132 for (unsigned i = m_currentTextFragment.metricsListOffset; i < m_visualMetricsListOffset; ++i)
133 length += textMetricsValues.at(i).height();
134 m_currentTextFragment.height = length;
136 for (unsigned i = m_currentTextFragment.metricsListOffset; i < m_visualMetricsListOffset; ++i)
137 length += textMetricsValues.at(i).width();
138 m_currentTextFragment.width = length;
142 textBox->textFragments().append(m_currentTextFragment);
143 m_currentTextFragment = SVGTextFragment();
146 bool SVGTextLayoutEngine::parentDefinesTextLength(RenderObject* parent) const
148 RenderObject* currentParent = parent;
149 while (currentParent) {
150 if (SVGTextContentElement* textContentElement = SVGTextContentElement::elementFromRenderer(currentParent)) {
151 SVGLengthContext lengthContext(textContentElement);
152 if (textContentElement->lengthAdjust()->currentValue()->enumValue() == SVGLengthAdjustSpacing && textContentElement->textLengthIsSpecifiedByUser())
156 if (currentParent->isSVGText())
159 currentParent = currentParent->parent();
162 ASSERT_NOT_REACHED();
166 void SVGTextLayoutEngine::beginTextPathLayout(RenderObject* object, SVGTextLayoutEngine& lineLayout)
170 m_inPathLayout = true;
171 RenderSVGTextPath* textPath = toRenderSVGTextPath(object);
173 Path path = textPath->layoutPath();
176 m_textPathCalculator = new Path::PositionCalculator(path);
177 m_textPathStartOffset = textPath->startOffset();
178 m_textPathLength = path.length();
179 if (m_textPathStartOffset > 0 && m_textPathStartOffset <= 1)
180 m_textPathStartOffset *= m_textPathLength;
182 float totalLength = 0;
183 unsigned totalCharacters = 0;
185 lineLayout.m_chunkLayoutBuilder.buildTextChunks(lineLayout.m_lineLayoutBoxes);
186 const Vector<SVGTextChunk>& textChunks = lineLayout.m_chunkLayoutBuilder.textChunks();
188 unsigned size = textChunks.size();
189 for (unsigned i = 0; i < size; ++i) {
190 const SVGTextChunk& chunk = textChunks.at(i);
193 unsigned characters = 0;
194 chunk.calculateLength(length, characters);
196 // Handle text-anchor as additional start offset for text paths.
197 m_textPathStartOffset += chunk.calculateTextAnchorShift(length);
199 totalLength += length;
200 totalCharacters += characters;
203 m_textPathCurrentOffset = m_textPathStartOffset;
205 // Eventually handle textLength adjustments.
206 SVGLengthAdjustType lengthAdjust = SVGLengthAdjustUnknown;
207 float desiredTextLength = 0;
209 if (SVGTextContentElement* textContentElement = SVGTextContentElement::elementFromRenderer(textPath)) {
210 SVGLengthContext lengthContext(textContentElement);
211 lengthAdjust = textContentElement->lengthAdjust()->currentValue()->enumValue();
212 if (textContentElement->textLengthIsSpecifiedByUser())
213 desiredTextLength = textContentElement->textLength()->currentValue()->value(lengthContext);
215 desiredTextLength = 0;
218 if (!desiredTextLength)
221 if (lengthAdjust == SVGLengthAdjustSpacing)
222 m_textPathSpacing = (desiredTextLength - totalLength) / totalCharacters;
224 m_textPathScaling = desiredTextLength / totalLength;
227 void SVGTextLayoutEngine::endTextPathLayout()
229 m_inPathLayout = false;
230 delete m_textPathCalculator;
231 m_textPathCalculator = 0;
232 m_textPathLength = 0;
233 m_textPathStartOffset = 0;
234 m_textPathCurrentOffset = 0;
235 m_textPathSpacing = 0;
236 m_textPathScaling = 1;
239 void SVGTextLayoutEngine::layoutInlineTextBox(SVGInlineTextBox* textBox)
243 RenderSVGInlineText& text = toRenderSVGInlineText(textBox->textRenderer());
244 ASSERT(text.parent());
245 ASSERT(text.parent()->node());
246 ASSERT(text.parent()->node()->isSVGElement());
248 const RenderStyle* style = text.style();
251 textBox->clearTextFragments();
252 m_isVerticalText = style->svgStyle()->isVerticalWritingMode();
253 layoutTextOnLineOrPath(textBox, &text, style);
255 if (m_inPathLayout) {
256 m_pathLayoutBoxes.append(textBox);
260 m_lineLayoutBoxes.append(textBox);
263 #if DUMP_TEXT_FRAGMENTS > 0
264 static inline void dumpTextBoxes(Vector<SVGInlineTextBox*>& boxes)
266 unsigned boxCount = boxes.size();
267 fprintf(stderr, "Dumping all text fragments in text sub tree, %i boxes\n", boxCount);
269 for (unsigned boxPosition = 0; boxPosition < boxCount; ++boxPosition) {
270 SVGInlineTextBox* textBox = boxes.at(boxPosition);
271 Vector<SVGTextFragment>& fragments = textBox->textFragments();
272 fprintf(stderr, "-> Box %i: Dumping text fragments for SVGInlineTextBox, textBox=%p, textRenderer=%p\n", boxPosition, textBox, textBox->textRenderer());
273 fprintf(stderr, " textBox properties, start=%i, len=%i, box direction=%i\n", textBox->start(), textBox->len(), textBox->direction());
274 fprintf(stderr, " textRenderer properties, textLength=%i\n", textBox->textRenderer()->textLength());
276 String characters = textBox->textRenderer()->text();
278 unsigned fragmentCount = fragments.size();
279 for (unsigned i = 0; i < fragmentCount; ++i) {
280 SVGTextFragment& fragment = fragments.at(i);
281 String fragmentString = characters.substring(fragment.characterOffset, fragment.length);
282 fprintf(stderr, " -> Fragment %i, x=%lf, y=%lf, width=%lf, height=%lf, characterOffset=%i, length=%i, characters='%s'\n"
283 , i, fragment.x, fragment.y, fragment.width, fragment.height, fragment.characterOffset, fragment.length, fragmentString.utf8().data());
289 void SVGTextLayoutEngine::finalizeTransformMatrices(Vector<SVGInlineTextBox*>& boxes)
291 unsigned boxCount = boxes.size();
295 AffineTransform textBoxTransformation;
296 for (unsigned boxPosition = 0; boxPosition < boxCount; ++boxPosition) {
297 SVGInlineTextBox* textBox = boxes.at(boxPosition);
298 Vector<SVGTextFragment>& fragments = textBox->textFragments();
300 unsigned fragmentCount = fragments.size();
301 for (unsigned i = 0; i < fragmentCount; ++i) {
302 m_chunkLayoutBuilder.transformationForTextBox(textBox, textBoxTransformation);
303 if (textBoxTransformation.isIdentity())
305 ASSERT(fragments[i].lengthAdjustTransform.isIdentity());
306 fragments[i].lengthAdjustTransform = textBoxTransformation;
313 void SVGTextLayoutEngine::finishLayout()
315 // After all text fragments are stored in their correpsonding SVGInlineTextBoxes, we can layout individual text chunks.
316 // Chunk layouting is only performed for line layout boxes, not for path layout, where it has already been done.
317 m_chunkLayoutBuilder.layoutTextChunks(m_lineLayoutBoxes);
319 // Finalize transform matrices, after the chunk layout corrections have been applied, and all fragment x/y positions are finalized.
320 if (!m_lineLayoutBoxes.isEmpty()) {
321 #if DUMP_TEXT_FRAGMENTS > 0
322 fprintf(stderr, "Line layout: ");
323 dumpTextBoxes(m_lineLayoutBoxes);
326 finalizeTransformMatrices(m_lineLayoutBoxes);
329 if (!m_pathLayoutBoxes.isEmpty()) {
330 #if DUMP_TEXT_FRAGMENTS > 0
331 fprintf(stderr, "Path layout: ");
332 dumpTextBoxes(m_pathLayoutBoxes);
335 finalizeTransformMatrices(m_pathLayoutBoxes);
339 bool SVGTextLayoutEngine::currentLogicalCharacterAttributes(SVGTextLayoutAttributes*& logicalAttributes)
341 if (m_layoutAttributesPosition == m_layoutAttributes.size())
344 logicalAttributes = m_layoutAttributes[m_layoutAttributesPosition];
345 ASSERT(logicalAttributes);
347 if (m_logicalCharacterOffset != logicalAttributes->context()->textLength())
350 ++m_layoutAttributesPosition;
351 if (m_layoutAttributesPosition == m_layoutAttributes.size())
354 logicalAttributes = m_layoutAttributes[m_layoutAttributesPosition];
355 m_logicalMetricsListOffset = 0;
356 m_logicalCharacterOffset = 0;
360 bool SVGTextLayoutEngine::currentLogicalCharacterMetrics(SVGTextLayoutAttributes*& logicalAttributes, SVGTextMetrics& logicalMetrics)
362 Vector<SVGTextMetrics>* textMetricsValues = &logicalAttributes->textMetricsValues();
363 unsigned textMetricsSize = textMetricsValues->size();
365 if (m_logicalMetricsListOffset == textMetricsSize) {
366 if (!currentLogicalCharacterAttributes(logicalAttributes))
369 textMetricsValues = &logicalAttributes->textMetricsValues();
370 textMetricsSize = textMetricsValues->size();
374 ASSERT(textMetricsSize);
375 ASSERT(m_logicalMetricsListOffset < textMetricsSize);
376 logicalMetrics = textMetricsValues->at(m_logicalMetricsListOffset);
377 if (logicalMetrics.isEmpty() || (!logicalMetrics.width() && !logicalMetrics.height())) {
378 advanceToNextLogicalCharacter(logicalMetrics);
382 // Stop if we found the next valid logical text metrics object.
386 ASSERT_NOT_REACHED();
390 bool SVGTextLayoutEngine::currentVisualCharacterMetrics(SVGInlineTextBox* textBox, Vector<SVGTextMetrics>& visualMetricsValues, SVGTextMetrics& visualMetrics)
392 ASSERT(!visualMetricsValues.isEmpty());
393 unsigned textMetricsSize = visualMetricsValues.size();
394 unsigned boxStart = textBox->start();
395 unsigned boxLength = textBox->len();
397 while (m_visualMetricsListOffset < textMetricsSize) {
398 // Advance to text box start location.
399 if (m_visualCharacterOffset < boxStart) {
400 advanceToNextVisualCharacter(visualMetricsValues[m_visualMetricsListOffset]);
404 // Stop if we've finished processing this text box.
405 if (m_visualCharacterOffset >= boxStart + boxLength)
408 visualMetrics = visualMetricsValues[m_visualMetricsListOffset];
415 void SVGTextLayoutEngine::advanceToNextLogicalCharacter(const SVGTextMetrics& logicalMetrics)
417 ++m_logicalMetricsListOffset;
418 m_logicalCharacterOffset += logicalMetrics.length();
421 void SVGTextLayoutEngine::advanceToNextVisualCharacter(const SVGTextMetrics& visualMetrics)
423 ++m_visualMetricsListOffset;
424 m_visualCharacterOffset += visualMetrics.length();
427 void SVGTextLayoutEngine::layoutTextOnLineOrPath(SVGInlineTextBox* textBox, RenderSVGInlineText* text, const RenderStyle* style)
429 if (m_inPathLayout && !m_textPathCalculator)
432 SVGElement* lengthContext = toSVGElement(text->parent()->node());
434 RenderObject* textParent = text->parent();
435 bool definesTextLength = textParent ? parentDefinesTextLength(textParent) : false;
437 const SVGRenderStyle* svgStyle = style->svgStyle();
440 m_visualMetricsListOffset = 0;
441 m_visualCharacterOffset = 0;
443 Vector<SVGTextMetrics>& visualMetricsValues = text->layoutAttributes()->textMetricsValues();
444 ASSERT(!visualMetricsValues.isEmpty());
446 const Font& font = style->font();
448 SVGTextLayoutEngineSpacing spacingLayout(font);
449 SVGTextLayoutEngineBaseline baselineLayout(font);
451 bool didStartTextFragment = false;
452 bool applySpacingToNextCharacter = false;
455 float baselineShift = baselineLayout.calculateBaselineShift(svgStyle, lengthContext);
456 baselineShift -= baselineLayout.calculateAlignmentBaselineShift(m_isVerticalText, text);
458 // Main layout algorithm.
460 // Find the start of the current text box in this list, respecting ligatures.
461 SVGTextMetrics visualMetrics(SVGTextMetrics::SkippedSpaceMetrics);
462 if (!currentVisualCharacterMetrics(textBox, visualMetricsValues, visualMetrics))
465 if (visualMetrics.isEmpty()) {
466 advanceToNextVisualCharacter(visualMetrics);
470 SVGTextLayoutAttributes* logicalAttributes = 0;
471 if (!currentLogicalCharacterAttributes(logicalAttributes))
474 ASSERT(logicalAttributes);
475 SVGTextMetrics logicalMetrics(SVGTextMetrics::SkippedSpaceMetrics);
476 if (!currentLogicalCharacterMetrics(logicalAttributes, logicalMetrics))
479 SVGCharacterDataMap& characterDataMap = logicalAttributes->characterDataMap();
480 SVGCharacterData data;
481 SVGCharacterDataMap::iterator it = characterDataMap.find(m_logicalCharacterOffset + 1);
482 if (it != characterDataMap.end())
488 // When we've advanced to the box start offset, determine using the original x/y values,
489 // whether this character starts a new text chunk, before doing any further processing.
490 if (m_visualCharacterOffset == textBox->start())
491 textBox->setStartsNewTextChunk(logicalAttributes->context()->characterStartsNewTextChunk(m_logicalCharacterOffset));
493 float angle = data.rotate == SVGTextLayoutAttributes::emptyValue() ? 0 : data.rotate;
495 // Calculate glyph orientation angle.
496 UChar currentCharacter = text->characterAt(m_visualCharacterOffset);
497 float orientationAngle = baselineLayout.calculateGlyphOrientationAngle(m_isVerticalText, svgStyle, currentCharacter);
499 // Calculate glyph advance & x/y orientation shifts.
500 float xOrientationShift = 0;
501 float yOrientationShift = 0;
502 float glyphAdvance = baselineLayout.calculateGlyphAdvanceAndOrientation(m_isVerticalText, visualMetrics, orientationAngle, xOrientationShift, yOrientationShift);
504 // Assign current text position to x/y values, if needed.
505 updateCharacerPositionIfNeeded(x, y);
507 // Apply dx/dy value adjustments to current text position, if needed.
508 updateRelativePositionAdjustmentsIfNeeded(data.dx, data.dy);
510 // Calculate SVG Fonts kerning, if needed.
511 float kerning = spacingLayout.calculateSVGKerning(m_isVerticalText, visualMetrics.glyph());
513 // Calculate CSS 'letter-spacing' and 'word-spacing' for next character, if needed.
514 float spacing = spacingLayout.calculateCSSSpacing(currentCharacter);
516 float textPathOffset = 0;
517 if (m_inPathLayout) {
518 float scaledGlyphAdvance = glyphAdvance * m_textPathScaling;
519 if (m_isVerticalText) {
520 // If there's an absolute y position available, it marks the beginning of a new position along the path.
521 if (y != SVGTextLayoutAttributes::emptyValue())
522 m_textPathCurrentOffset = y + m_textPathStartOffset;
524 m_textPathCurrentOffset += m_dy - kerning;
527 // Apply dx/dy correction and setup translations that move to the glyph midpoint.
528 xOrientationShift += m_dx + baselineShift;
529 yOrientationShift -= scaledGlyphAdvance / 2;
531 // If there's an absolute x position available, it marks the beginning of a new position along the path.
532 if (x != SVGTextLayoutAttributes::emptyValue())
533 m_textPathCurrentOffset = x + m_textPathStartOffset;
535 m_textPathCurrentOffset += m_dx - kerning;
538 // Apply dx/dy correction and setup translations that move to the glyph midpoint.
539 xOrientationShift -= scaledGlyphAdvance / 2;
540 yOrientationShift += m_dy - baselineShift;
543 // Calculate current offset along path.
544 textPathOffset = m_textPathCurrentOffset + scaledGlyphAdvance / 2;
546 // Move to next character.
547 m_textPathCurrentOffset += scaledGlyphAdvance + m_textPathSpacing + spacing * m_textPathScaling;
549 // Skip character, if we're before the path.
550 if (textPathOffset < 0) {
551 advanceToNextLogicalCharacter(logicalMetrics);
552 advanceToNextVisualCharacter(visualMetrics);
556 // Stop processing, if the next character lies behind the path.
557 if (textPathOffset > m_textPathLength)
561 bool ok = m_textPathCalculator->pointAndNormalAtLength(textPathOffset, point, angle);
562 ASSERT_UNUSED(ok, ok);
566 // For vertical text on path, the actual angle has to be rotated 90 degrees anti-clockwise, not the orientation angle!
567 if (m_isVerticalText)
570 // Apply all previously calculated shift values.
571 if (m_isVerticalText) {
583 // Determine whether we have to start a new fragment.
584 bool shouldStartNewFragment = m_dx || m_dy || m_isVerticalText || m_inPathLayout || angle || angle != lastAngle
585 || orientationAngle || kerning || applySpacingToNextCharacter || definesTextLength;
587 // If we already started a fragment, close it now.
588 if (didStartTextFragment && shouldStartNewFragment) {
589 applySpacingToNextCharacter = false;
590 recordTextFragment(textBox, visualMetricsValues);
593 // Eventually start a new fragment, if not yet done.
594 if (!didStartTextFragment || shouldStartNewFragment) {
595 ASSERT(!m_currentTextFragment.characterOffset);
596 ASSERT(!m_currentTextFragment.length);
598 didStartTextFragment = true;
599 m_currentTextFragment.characterOffset = m_visualCharacterOffset;
600 m_currentTextFragment.metricsListOffset = m_visualMetricsListOffset;
601 m_currentTextFragment.x = x;
602 m_currentTextFragment.y = y;
604 // Build fragment transformation.
606 m_currentTextFragment.transform.rotate(angle);
608 if (xOrientationShift || yOrientationShift)
609 m_currentTextFragment.transform.translate(xOrientationShift, yOrientationShift);
611 if (orientationAngle)
612 m_currentTextFragment.transform.rotate(orientationAngle);
614 m_currentTextFragment.isTextOnPath = m_inPathLayout && m_textPathScaling != 1;
615 if (m_currentTextFragment.isTextOnPath) {
616 if (m_isVerticalText)
617 m_currentTextFragment.lengthAdjustTransform.scaleNonUniform(1, m_textPathScaling);
619 m_currentTextFragment.lengthAdjustTransform.scaleNonUniform(m_textPathScaling, 1);
623 // Update current text position, after processing of the current character finished.
625 updateCurrentTextPosition(x, y, glyphAdvance);
627 // Apply CSS 'kerning', 'letter-spacing' and 'word-spacing' to next character, if needed.
629 applySpacingToNextCharacter = true;
631 float xNew = x - m_dx;
632 float yNew = y - m_dy;
634 if (m_isVerticalText)
635 xNew -= baselineShift;
637 yNew += baselineShift;
639 updateCurrentTextPosition(xNew, yNew, glyphAdvance + spacing);
642 advanceToNextLogicalCharacter(logicalMetrics);
643 advanceToNextVisualCharacter(visualMetrics);
647 if (!didStartTextFragment)
650 // Close last open fragment, if needed.
651 recordTextFragment(textBox, visualMetricsValues);