• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) Research In Motion Limited 2010-2011. All rights reserved.
3  *
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.
8  *
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.
13  *
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.
18  */
19 
20 #include "config.h"
21 
22 #if ENABLE(SVG)
23 #include "SVGTextLayoutAttributesBuilder.h"
24 
25 #include "RenderSVGInlineText.h"
26 #include "RenderSVGText.h"
27 #include "SVGTextPositioningElement.h"
28 
29 // Set to a value > 0 to dump the text layout attributes
30 #define DUMP_TEXT_LAYOUT_ATTRIBUTES 0
31 
32 namespace WebCore {
33 
SVGTextLayoutAttributesBuilder()34 SVGTextLayoutAttributesBuilder::SVGTextLayoutAttributesBuilder()
35 {
36 }
37 
buildLayoutAttributesForTextSubtree(RenderSVGText * textRoot)38 void SVGTextLayoutAttributesBuilder::buildLayoutAttributesForTextSubtree(RenderSVGText* textRoot)
39 {
40     ASSERT(textRoot);
41 
42     // Build list of x/y/dx/dy/rotate values for each subtree element that may define these values (tspan/textPath etc).
43     unsigned atCharacter = 0;
44     UChar lastCharacter = '\0';
45     collectTextPositioningElements(textRoot, atCharacter, lastCharacter);
46 
47     if (!atCharacter)
48         return;
49 
50     // Collect x/y/dx/dy/rotate values for each character, stored in the m_positioningLists.xValues()/etc. lists.
51     buildLayoutAttributesForAllCharacters(textRoot, atCharacter);
52 
53     // Propagate layout attributes to each RenderSVGInlineText object, and the whole list to the RenderSVGText root.
54     Vector<SVGTextLayoutAttributes>& allAttributes = textRoot->layoutAttributes();
55     allAttributes.clear();
56     atCharacter = 0;
57     lastCharacter = '\0';
58     propagateLayoutAttributes(textRoot, allAttributes, atCharacter, lastCharacter);
59 }
60 
extractFloatValuesFromSVGLengthList(SVGElement * lengthContext,const SVGLengthList & list,Vector<float> & floatValues,unsigned textContentLength)61 static inline void extractFloatValuesFromSVGLengthList(SVGElement* lengthContext, const SVGLengthList& list, Vector<float>& floatValues, unsigned textContentLength)
62 {
63     ASSERT(lengthContext);
64 
65     unsigned length = list.size();
66     if (length > textContentLength)
67         length = textContentLength;
68     floatValues.reserveCapacity(length);
69 
70     for (unsigned i = 0; i < length; ++i) {
71         const SVGLength& length = list.at(i);
72         floatValues.append(length.value(lengthContext));
73     }
74 }
75 
extractFloatValuesFromSVGNumberList(const SVGNumberList & list,Vector<float> & floatValues,unsigned textContentLength)76 static inline void extractFloatValuesFromSVGNumberList(const SVGNumberList& list, Vector<float>& floatValues, unsigned textContentLength)
77 {
78     unsigned length = list.size();
79     if (length > textContentLength)
80         length = textContentLength;
81     floatValues.reserveCapacity(length);
82 
83     for (unsigned i = 0; i < length; ++i)
84         floatValues.append(list.at(i));
85 }
86 
87 
characterIsSpace(const UChar & character)88 static inline bool characterIsSpace(const UChar& character)
89 {
90     return character == ' ';
91 }
92 
characterIsSpaceOrNull(const UChar & character)93 static inline bool characterIsSpaceOrNull(const UChar& character)
94 {
95     return character == ' ' || character == '\0';
96 }
97 
shouldPreserveAllWhiteSpace(RenderStyle * style)98 static inline bool shouldPreserveAllWhiteSpace(RenderStyle* style)
99 {
100     ASSERT(style);
101     return style->whiteSpace() == PRE;
102 }
103 
processRenderSVGInlineText(RenderSVGInlineText * text,unsigned & atCharacter,UChar & lastCharacter)104 static inline void processRenderSVGInlineText(RenderSVGInlineText* text, unsigned& atCharacter, UChar& lastCharacter)
105 {
106     if (shouldPreserveAllWhiteSpace(text->style())) {
107         atCharacter += text->textLength();
108         return;
109     }
110 
111     const UChar* characters = text->characters();
112     unsigned textLength = text->textLength();
113     for (unsigned textPosition = 0; textPosition < textLength; ++textPosition) {
114         const UChar& currentCharacter = characters[textPosition];
115         if (characterIsSpace(currentCharacter) && characterIsSpaceOrNull(lastCharacter))
116             continue;
117 
118         lastCharacter = currentCharacter;
119         ++atCharacter;
120     }
121 }
122 
collectTextPositioningElements(RenderObject * start,unsigned & atCharacter,UChar & lastCharacter)123 void SVGTextLayoutAttributesBuilder::collectTextPositioningElements(RenderObject* start, unsigned& atCharacter, UChar& lastCharacter)
124 {
125     ASSERT(!start->isSVGText() || m_textPositions.isEmpty());
126 
127     for (RenderObject* child = start->firstChild(); child; child = child->nextSibling()) {
128         if (child->isSVGInlineText()) {
129             processRenderSVGInlineText(toRenderSVGInlineText(child), atCharacter, lastCharacter);
130             continue;
131         }
132 
133         if (!child->isSVGInline())
134             continue;
135 
136         SVGTextPositioningElement* element = SVGTextPositioningElement::elementFromRenderer(child);
137         unsigned atPosition = m_textPositions.size();
138         if (element)
139             m_textPositions.append(TextPosition(element, atCharacter));
140 
141         collectTextPositioningElements(child, atCharacter, lastCharacter);
142 
143         if (!element)
144             continue;
145 
146         // Update text position, after we're back from recursion.
147         TextPosition& position = m_textPositions[atPosition];
148         ASSERT(!position.length);
149         position.length = atCharacter - position.start;
150     }
151 }
152 
buildLayoutAttributesForAllCharacters(RenderSVGText * textRoot,unsigned textLength)153 void SVGTextLayoutAttributesBuilder::buildLayoutAttributesForAllCharacters(RenderSVGText* textRoot, unsigned textLength)
154 {
155     ASSERT(textLength);
156 
157     SVGTextPositioningElement* outermostTextElement = SVGTextPositioningElement::elementFromRenderer(textRoot);
158     ASSERT(outermostTextElement);
159 
160     // Fill the lists with the special emptyValue marker.
161     m_positioningLists.fillWithEmptyValues(textLength);
162 
163     // Grab outermost <text> element value lists and insert them in the m_positioningLists.
164     TextPosition wholeTextPosition(outermostTextElement, 0, textLength);
165     fillAttributesAtPosition(wholeTextPosition);
166 
167     // Handle x/y default attributes.
168     float& xFirst = m_positioningLists.xValues.first();
169     if (xFirst == SVGTextLayoutAttributes::emptyValue())
170         xFirst = 0;
171 
172     float& yFirst = m_positioningLists.yValues.first();
173     if (yFirst == SVGTextLayoutAttributes::emptyValue())
174         yFirst = 0;
175 
176     // Fill m_positioningLists using child text positioning elements in top-down order.
177     unsigned size = m_textPositions.size();
178     for (unsigned i = 0; i < size; ++i)
179         fillAttributesAtPosition(m_textPositions[i]);
180 
181     // Now m_positioningLists.contains a x/y/dx/dy/rotate value for each character in the <text> subtree.
182 }
183 
propagateLayoutAttributes(RenderObject * start,Vector<SVGTextLayoutAttributes> & allAttributes,unsigned & atCharacter,UChar & lastCharacter) const184 void SVGTextLayoutAttributesBuilder::propagateLayoutAttributes(RenderObject* start, Vector<SVGTextLayoutAttributes>& allAttributes, unsigned& atCharacter, UChar& lastCharacter) const
185 {
186     for (RenderObject* child = start->firstChild(); child; child = child->nextSibling()) {
187         if (child->isSVGInlineText()) {
188             RenderSVGInlineText* text = toRenderSVGInlineText(child);
189             const UChar* characters = text->characters();
190             unsigned textLength = text->textLength();
191             bool preserveWhiteSpace = shouldPreserveAllWhiteSpace(text->style());
192 
193             SVGTextLayoutAttributes attributes(text);
194             attributes.reserveCapacity(textLength);
195 
196             unsigned valueListPosition = atCharacter;
197             unsigned metricsLength = 1;
198             SVGTextMetrics lastMetrics = SVGTextMetrics::emptyMetrics();
199 
200             for (unsigned textPosition = 0; textPosition < textLength; textPosition += metricsLength) {
201                 const UChar& currentCharacter = characters[textPosition];
202 
203                 SVGTextMetrics startToCurrentMetrics = SVGTextMetrics::measureCharacterRange(text, 0, textPosition + 1);
204                 SVGTextMetrics currentMetrics = SVGTextMetrics::measureCharacterRange(text, textPosition, 1);
205 
206                 // Frequent case for Arabic text: when measuring a single character the arabic isolated form is taken
207                 // when rendering the glyph "in context" (with it's surrounding characters) it changes due to shaping.
208                 // So whenever runWidthAdvance != currentMetrics.width(), we are processing a text run whose length is
209                 // not equal to the sum of the individual lengths of the glyphs, when measuring them isolated.
210                 float runWidthAdvance = startToCurrentMetrics.width() - lastMetrics.width();
211                 if (runWidthAdvance != currentMetrics.width())
212                     currentMetrics.setWidth(runWidthAdvance);
213 
214                 lastMetrics = startToCurrentMetrics;
215                 metricsLength = currentMetrics.length();
216 
217                 if (!preserveWhiteSpace && characterIsSpace(currentCharacter) && characterIsSpaceOrNull(lastCharacter)) {
218                     attributes.positioningLists().appendEmptyValues();
219                     attributes.textMetricsValues().append(SVGTextMetrics::emptyMetrics());
220                     continue;
221                 }
222 
223                 SVGTextLayoutAttributes::PositioningLists& positioningLists = attributes.positioningLists();
224                 positioningLists.appendValuesFromPosition(m_positioningLists, valueListPosition);
225                 attributes.textMetricsValues().append(currentMetrics);
226 
227                 // Pad x/y/dx/dy/rotate value lists with empty values, if the metrics span more than one character.
228                 if (metricsLength > 1) {
229                     for (unsigned i = 0; i < metricsLength - 1; ++i)
230                         positioningLists.appendEmptyValues();
231                 }
232 
233                 lastCharacter = currentCharacter;
234                 valueListPosition += metricsLength;
235             }
236 
237 #if DUMP_TEXT_LAYOUT_ATTRIBUTES > 0
238             fprintf(stderr, "\nDumping layout attributes for RenderSVGInlineText, renderer=%p, node=%p (atCharacter: %i)\n", text, text->node(), atCharacter);
239             fprintf(stderr, "BiDi properties: unicode-bidi=%i, block direction=%i\n", text->style()->unicodeBidi(), text->style()->direction());
240             attributes.dump();
241 #endif
242 
243             text->storeLayoutAttributes(attributes);
244             allAttributes.append(attributes);
245             atCharacter = valueListPosition;
246             continue;
247         }
248 
249         if (!child->isSVGInline())
250             continue;
251 
252         propagateLayoutAttributes(child, allAttributes, atCharacter, lastCharacter);
253     }
254 }
255 
fillListAtPosition(Vector<float> & allValues,Vector<float> & values,unsigned start)256 static inline void fillListAtPosition(Vector<float>& allValues, Vector<float>& values, unsigned start)
257 {
258     unsigned valuesSize = values.size();
259     for (unsigned i = 0; i < valuesSize; ++i)
260         allValues[start + i] = values[i];
261 }
262 
fillAttributesAtPosition(const TextPosition & position)263 void SVGTextLayoutAttributesBuilder::fillAttributesAtPosition(const TextPosition& position)
264 {
265     Vector<float> values;
266     extractFloatValuesFromSVGLengthList(position.element, position.element->x(), values, position.length);
267     fillListAtPosition(m_positioningLists.xValues, values, position.start);
268 
269     values.clear();
270     extractFloatValuesFromSVGLengthList(position.element, position.element->y(), values, position.length);
271     fillListAtPosition(m_positioningLists.yValues, values, position.start);
272 
273     values.clear();
274     extractFloatValuesFromSVGLengthList(position.element, position.element->dx(), values, position.length);
275     fillListAtPosition(m_positioningLists.dxValues, values, position.start);
276 
277     values.clear();
278     extractFloatValuesFromSVGLengthList(position.element, position.element->dy(), values, position.length);
279     fillListAtPosition(m_positioningLists.dyValues, values, position.start);
280 
281     values.clear();
282     extractFloatValuesFromSVGNumberList(position.element->rotate(), values, position.length);
283     fillListAtPosition(m_positioningLists.rotateValues, values, position.start);
284 
285     // The last rotation value always spans the whole scope.
286     if (values.isEmpty())
287         return;
288 
289     float lastValue = values.last();
290     for (unsigned i = values.size(); i < position.length; ++i)
291         m_positioningLists.rotateValues[position.start + i] = lastValue;
292 }
293 
294 }
295 
296 #endif // ENABLE(SVG)
297