| OLD | NEW |
| 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 12 matching lines...) Expand all Loading... |
| 23 | 23 |
| 24 #include "core/rendering/svg/RenderSVGInlineText.h" | 24 #include "core/rendering/svg/RenderSVGInlineText.h" |
| 25 #include "core/rendering/svg/RenderSVGText.h" | 25 #include "core/rendering/svg/RenderSVGText.h" |
| 26 #include "platform/text/TextPath.h" | 26 #include "platform/text/TextPath.h" |
| 27 | 27 |
| 28 namespace WebCore { | 28 namespace WebCore { |
| 29 | 29 |
| 30 SVGTextMetricsBuilder::SVGTextMetricsBuilder() | 30 SVGTextMetricsBuilder::SVGTextMetricsBuilder() |
| 31 : m_text(0) | 31 : m_text(0) |
| 32 , m_run(static_cast<const UChar*>(0), 0) | 32 , m_run(static_cast<const UChar*>(0), 0) |
| 33 , m_textPosition(0) | |
| 34 , m_isComplexText(false) | 33 , m_isComplexText(false) |
| 35 , m_totalWidth(0) | 34 , m_totalWidth(0) |
| 36 { | 35 { |
| 37 } | 36 } |
| 38 | 37 |
| 39 inline bool SVGTextMetricsBuilder::currentCharacterStartsSurrogatePair() const | 38 inline bool SVGTextMetricsBuilder::currentCharacterStartsSurrogatePair(unsigned
textPosition) const |
| 40 { | 39 { |
| 41 return U16_IS_LEAD(m_run[m_textPosition]) && int(m_textPosition + 1) < m_run
.charactersLength() && U16_IS_TRAIL(m_run[m_textPosition + 1]); | 40 return U16_IS_LEAD(m_run[textPosition]) && int(textPosition + 1) < m_run.cha
ractersLength() && U16_IS_TRAIL(m_run[textPosition + 1]); |
| 42 } | 41 } |
| 43 | 42 |
| 44 SVGTextMetrics SVGTextMetricsBuilder::computeMetricsForCurrentCharacterSimple() | 43 SVGTextMetrics SVGTextMetricsBuilder::computeMetricsForCurrentCharacterSimple(un
signed textPosition) |
| 45 { | 44 { |
| 46 GlyphBuffer glyphBuffer; | 45 GlyphBuffer glyphBuffer; |
| 47 unsigned metricsLength = m_simpleWidthIterator->advance(m_textPosition + 1,
&glyphBuffer); | 46 unsigned metricsLength = m_simpleWidthIterator->advance(textPosition + 1, &g
lyphBuffer); |
| 48 if (!metricsLength) | 47 if (!metricsLength) |
| 49 return SVGTextMetrics(); | 48 return SVGTextMetrics(); |
| 50 | 49 |
| 51 float currentWidth = m_simpleWidthIterator->runWidthSoFar() - m_totalWidth; | 50 float currentWidth = m_simpleWidthIterator->runWidthSoFar() - m_totalWidth; |
| 52 m_totalWidth = m_simpleWidthIterator->runWidthSoFar(); | 51 m_totalWidth = m_simpleWidthIterator->runWidthSoFar(); |
| 53 | 52 |
| 54 Glyph glyphId = glyphBuffer.glyphAt(0); | 53 Glyph glyphId = glyphBuffer.glyphAt(0); |
| 55 return SVGTextMetrics(m_text, m_textPosition, metricsLength, currentWidth, g
lyphId); | 54 return SVGTextMetrics(m_text, textPosition, metricsLength, currentWidth, gly
phId); |
| 56 } | 55 } |
| 57 | 56 |
| 58 SVGTextMetrics SVGTextMetricsBuilder::computeMetricsForCurrentCharacterComplex() | 57 SVGTextMetrics SVGTextMetricsBuilder::computeMetricsForCurrentCharacterComplex(u
nsigned textPosition) |
| 59 { | 58 { |
| 60 unsigned metricsLength = currentCharacterStartsSurrogatePair() ? 2 : 1; | 59 unsigned metricsLength = currentCharacterStartsSurrogatePair(textPosition) ?
2 : 1; |
| 61 SVGTextMetrics metrics = SVGTextMetrics::measureCharacterRange(m_text, m_tex
tPosition, metricsLength); | 60 SVGTextMetrics metrics = SVGTextMetrics::measureCharacterRange(m_text, textP
osition, metricsLength); |
| 62 ASSERT(metrics.length() == metricsLength); | 61 ASSERT(metrics.length() == metricsLength); |
| 63 | 62 |
| 64 SVGTextMetrics complexStartToCurrentMetrics = SVGTextMetrics::measureCharact
erRange(m_text, 0, m_textPosition + metricsLength); | 63 SVGTextMetrics complexStartToCurrentMetrics = SVGTextMetrics::measureCharact
erRange(m_text, 0, textPosition + metricsLength); |
| 65 // Frequent case for Arabic text: when measuring a single character the arab
ic isolated form is taken | 64 // Frequent case for Arabic text: when measuring a single character the arab
ic isolated form is taken |
| 66 // when rendering the glyph "in context" (with it's surrounding characters)
it changes due to shaping. | 65 // when rendering the glyph "in context" (with it's surrounding characters)
it changes due to shaping. |
| 67 // So whenever currentWidth != currentMetrics.width(), we are processing a t
ext run whose length is | 66 // So whenever currentWidth != currentMetrics.width(), we are processing a t
ext run whose length is |
| 68 // not equal to the sum of the individual lengths of the glyphs, when measur
ing them isolated. | 67 // not equal to the sum of the individual lengths of the glyphs, when measur
ing them isolated. |
| 69 float currentWidth = complexStartToCurrentMetrics.width() - m_totalWidth; | 68 float currentWidth = complexStartToCurrentMetrics.width() - m_totalWidth; |
| 70 if (currentWidth != metrics.width()) | 69 if (currentWidth != metrics.width()) |
| 71 metrics.setWidth(currentWidth); | 70 metrics.setWidth(currentWidth); |
| 72 | 71 |
| 73 m_totalWidth = complexStartToCurrentMetrics.width(); | 72 m_totalWidth = complexStartToCurrentMetrics.width(); |
| 74 return metrics; | 73 return metrics; |
| 75 } | 74 } |
| 76 | 75 |
| 77 SVGTextMetrics SVGTextMetricsBuilder::computeMetricsForCurrentCharacter() | 76 SVGTextMetrics SVGTextMetricsBuilder::computeMetricsForCurrentCharacter(unsigned
textPosition) |
| 78 { | 77 { |
| 79 if (m_isComplexText) | 78 if (m_isComplexText) |
| 80 return computeMetricsForCurrentCharacterComplex(); | 79 return computeMetricsForCurrentCharacterComplex(textPosition); |
| 81 | 80 |
| 82 return computeMetricsForCurrentCharacterSimple(); | 81 return computeMetricsForCurrentCharacterSimple(textPosition); |
| 83 } | 82 } |
| 84 | 83 |
| 85 void SVGTextMetricsBuilder::initializeMeasurementWithTextRenderer(RenderSVGInlin
eText* text) | 84 void SVGTextMetricsBuilder::initializeMeasurementWithTextRenderer(RenderSVGInlin
eText* text) |
| 86 { | 85 { |
| 87 m_text = text; | 86 m_text = text; |
| 88 m_textPosition = 0; | |
| 89 m_totalWidth = 0; | 87 m_totalWidth = 0; |
| 90 | 88 |
| 91 const Font& scaledFont = text->scaledFont(); | 89 const Font& scaledFont = text->scaledFont(); |
| 92 m_run = SVGTextMetrics::constructTextRun(text, 0, text->textLength()); | 90 m_run = SVGTextMetrics::constructTextRun(text, 0, text->textLength()); |
| 93 CodePath codePath = scaledFont.codePath(m_run); | 91 CodePath codePath = scaledFont.codePath(m_run); |
| 94 m_isComplexText = codePath == ComplexPath; | 92 m_isComplexText = codePath == ComplexPath; |
| 95 m_run.setCharacterScanForCodePath(!m_isComplexText); | 93 m_run.setCharacterScanForCodePath(!m_isComplexText); |
| 96 | 94 |
| 97 if (m_isComplexText) | 95 if (m_isComplexText) |
| 98 m_simpleWidthIterator.clear(); | 96 m_simpleWidthIterator.clear(); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 123 if (data->allCharactersMap) | 121 if (data->allCharactersMap) |
| 124 attributes->clear(); | 122 attributes->clear(); |
| 125 else | 123 else |
| 126 textMetricsValues->clear(); | 124 textMetricsValues->clear(); |
| 127 } | 125 } |
| 128 | 126 |
| 129 initializeMeasurementWithTextRenderer(text); | 127 initializeMeasurementWithTextRenderer(text); |
| 130 bool preserveWhiteSpace = text->style()->whiteSpace() == PRE; | 128 bool preserveWhiteSpace = text->style()->whiteSpace() == PRE; |
| 131 unsigned surrogatePairCharacters = 0; | 129 unsigned surrogatePairCharacters = 0; |
| 132 unsigned skippedCharacters = 0; | 130 unsigned skippedCharacters = 0; |
| 131 unsigned textPosition = 0; |
| 133 unsigned textLength = static_cast<unsigned>(m_run.charactersLength()); | 132 unsigned textLength = static_cast<unsigned>(m_run.charactersLength()); |
| 134 | 133 |
| 135 SVGTextMetrics currentMetrics; | 134 SVGTextMetrics currentMetrics; |
| 136 for (; m_textPosition < textLength; m_textPosition += currentMetrics.length(
)) { | 135 for (; textPosition < textLength; textPosition += currentMetrics.length()) { |
| 137 currentMetrics = computeMetricsForCurrentCharacter(); | 136 currentMetrics = computeMetricsForCurrentCharacter(textPosition); |
| 138 if (!currentMetrics.length()) | 137 if (!currentMetrics.length()) |
| 139 break; | 138 break; |
| 140 | 139 |
| 141 bool characterIsWhiteSpace = m_run[m_textPosition] == ' '; | 140 bool characterIsWhiteSpace = m_run[textPosition] == ' '; |
| 142 if (characterIsWhiteSpace && !preserveWhiteSpace && data->lastCharacterW
asWhiteSpace) { | 141 if (characterIsWhiteSpace && !preserveWhiteSpace && data->lastCharacterW
asWhiteSpace) { |
| 143 if (processRenderer) | 142 if (processRenderer) |
| 144 textMetricsValues->append(SVGTextMetrics(SVGTextMetrics::Skipped
SpaceMetrics)); | 143 textMetricsValues->append(SVGTextMetrics(SVGTextMetrics::Skipped
SpaceMetrics)); |
| 145 if (data->allCharactersMap) | 144 if (data->allCharactersMap) |
| 146 skippedCharacters += currentMetrics.length(); | 145 skippedCharacters += currentMetrics.length(); |
| 147 continue; | 146 continue; |
| 148 } | 147 } |
| 149 | 148 |
| 150 if (processRenderer) { | 149 if (processRenderer) { |
| 151 if (data->allCharactersMap) { | 150 if (data->allCharactersMap) { |
| 152 const SVGCharacterDataMap::const_iterator it = data->allCharacte
rsMap->find(data->valueListPosition + m_textPosition - skippedCharacters - surro
gatePairCharacters + 1); | 151 const SVGCharacterDataMap::const_iterator it = data->allCharacte
rsMap->find(data->valueListPosition + textPosition - skippedCharacters - surroga
tePairCharacters + 1); |
| 153 if (it != data->allCharactersMap->end()) | 152 if (it != data->allCharactersMap->end()) |
| 154 attributes->characterDataMap().set(m_textPosition + 1, it->v
alue); | 153 attributes->characterDataMap().set(textPosition + 1, it->val
ue); |
| 155 } | 154 } |
| 156 textMetricsValues->append(currentMetrics); | 155 textMetricsValues->append(currentMetrics); |
| 157 } | 156 } |
| 158 | 157 |
| 159 if (data->allCharactersMap && currentCharacterStartsSurrogatePair()) | 158 if (data->allCharactersMap && currentCharacterStartsSurrogatePair(textPo
sition)) |
| 160 surrogatePairCharacters++; | 159 surrogatePairCharacters++; |
| 161 | 160 |
| 162 data->lastCharacterWasWhiteSpace = characterIsWhiteSpace; | 161 data->lastCharacterWasWhiteSpace = characterIsWhiteSpace; |
| 163 } | 162 } |
| 164 | 163 |
| 165 if (!data->allCharactersMap) | 164 if (!data->allCharactersMap) |
| 166 return; | 165 return; |
| 167 | 166 |
| 168 data->valueListPosition += m_textPosition - skippedCharacters; | 167 data->valueListPosition += textPosition - skippedCharacters; |
| 169 } | 168 } |
| 170 | 169 |
| 171 void SVGTextMetricsBuilder::walkTree(RenderObject* start, RenderSVGInlineText* s
topAtLeaf, MeasureTextData* data) | 170 void SVGTextMetricsBuilder::walkTree(RenderObject* start, RenderSVGInlineText* s
topAtLeaf, MeasureTextData* data) |
| 172 { | 171 { |
| 173 RenderObject* child = start->firstChild(); | 172 RenderObject* child = start->firstChild(); |
| 174 while (child) { | 173 while (child) { |
| 175 if (child->isSVGInlineText()) { | 174 if (child->isSVGInlineText()) { |
| 176 RenderSVGInlineText* text = toRenderSVGInlineText(child); | 175 RenderSVGInlineText* text = toRenderSVGInlineText(child); |
| 177 measureTextRenderer(text, data, !stopAtLeaf || stopAtLeaf == text); | 176 measureTextRenderer(text, data, !stopAtLeaf || stopAtLeaf == text); |
| 178 if (stopAtLeaf && stopAtLeaf == text) | 177 if (stopAtLeaf && stopAtLeaf == text) |
| (...skipping 22 matching lines...) Expand all Loading... |
| 201 } | 200 } |
| 202 | 201 |
| 203 void SVGTextMetricsBuilder::buildMetricsAndLayoutAttributes(RenderSVGText* textR
oot, RenderSVGInlineText* stopAtLeaf, SVGCharacterDataMap& allCharactersMap) | 202 void SVGTextMetricsBuilder::buildMetricsAndLayoutAttributes(RenderSVGText* textR
oot, RenderSVGInlineText* stopAtLeaf, SVGCharacterDataMap& allCharactersMap) |
| 204 { | 203 { |
| 205 ASSERT(textRoot); | 204 ASSERT(textRoot); |
| 206 MeasureTextData data(&allCharactersMap); | 205 MeasureTextData data(&allCharactersMap); |
| 207 walkTree(textRoot, stopAtLeaf, &data); | 206 walkTree(textRoot, stopAtLeaf, &data); |
| 208 } | 207 } |
| 209 | 208 |
| 210 } | 209 } |
| OLD | NEW |