2011-06-02 Levi Weintraub <leviw@chromium.org>
[WebKit-https.git] / Source / WebCore / rendering / svg / SVGRootInlineBox.cpp
1 /*
2  * Copyright (C) 2006 Oliver Hunt <ojh16@student.canterbury.ac.nz>
3  * Copyright (C) 2006 Apple Computer Inc.
4  * Copyright (C) 2007 Nikolas Zimmermann <zimmermann@kde.org>
5  * Copyright (C) Research In Motion Limited 2010. All rights reserved.
6  * Copyright (C) 2011 Torch Mobile (Beijing) CO. Ltd. All rights reserved.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public License
19  * along with this library; see the file COPYING.LIB.  If not, write to
20  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21  * Boston, MA 02110-1301, USA.
22  */
23
24 #include "config.h"
25 #include "SVGRootInlineBox.h"
26
27 #if ENABLE(SVG)
28 #include "GraphicsContext.h"
29 #include "RenderSVGInlineText.h"
30 #include "RenderSVGText.h"
31 #include "SVGInlineFlowBox.h"
32 #include "SVGInlineTextBox.h"
33 #include "SVGNames.h"
34 #include "SVGRenderSupport.h"
35 #include "SVGTextPositioningElement.h"
36
37 namespace WebCore {
38
39 void SVGRootInlineBox::paint(PaintInfo& paintInfo, const IntPoint&, int, int)
40 {
41     ASSERT(paintInfo.phase == PaintPhaseForeground || paintInfo.phase == PaintPhaseSelection);
42     ASSERT(!paintInfo.context->paintingDisabled());
43
44     RenderObject* boxRenderer = renderer();
45     ASSERT(boxRenderer);
46
47     bool isPrinting = renderer()->document()->printing();
48     bool hasSelection = !isPrinting && selectionState() != RenderObject::SelectionNone;
49
50     PaintInfo childPaintInfo(paintInfo);
51     if (hasSelection) {
52         for (InlineBox* child = firstChild(); child; child = child->nextOnLine()) {
53             if (child->isSVGInlineTextBox())
54                 static_cast<SVGInlineTextBox*>(child)->paintSelectionBackground(childPaintInfo);
55             else if (child->isSVGInlineFlowBox())
56                 static_cast<SVGInlineFlowBox*>(child)->paintSelectionBackground(childPaintInfo);
57         }
58     }
59
60     GraphicsContextStateSaver stateSaver(*childPaintInfo.context);
61
62     if (SVGRenderSupport::prepareToRenderSVGContent(boxRenderer, childPaintInfo)) {
63         for (InlineBox* child = firstChild(); child; child = child->nextOnLine()) {
64             if (child->isSVGInlineTextBox())
65                 SVGInlineFlowBox::computeTextMatchMarkerRectForRenderer(toRenderSVGInlineText(static_cast<SVGInlineTextBox*>(child)->textRenderer()));
66
67             child->paint(childPaintInfo, IntPoint(), 0, 0);
68         }
69     }
70
71     SVGRenderSupport::finishRenderSVGContent(boxRenderer, childPaintInfo, paintInfo.context);
72 }
73
74 void SVGRootInlineBox::computePerCharacterLayoutInformation()
75 {
76     RenderSVGText* parentBlock = toRenderSVGText(block());
77     ASSERT(parentBlock);
78
79     Vector<SVGTextLayoutAttributes>& attributes = parentBlock->layoutAttributes();
80     if (parentBlock->needsReordering())
81         reorderValueLists(attributes);
82
83     // Perform SVG text layout phase two (see SVGTextLayoutEngine for details).
84     SVGTextLayoutEngine characterLayout(attributes);
85     layoutCharactersInTextBoxes(this, characterLayout);
86
87     // Perform SVG text layout phase three (see SVGTextChunkBuilder for details).
88     characterLayout.finishLayout();
89
90     // Perform SVG text layout phase four
91     // Position & resize all SVGInlineText/FlowBoxes in the inline box tree, resize the root box as well as the RenderSVGText parent block.
92     IntRect childRect;
93     layoutChildBoxes(this, &childRect);
94     layoutRootBox(childRect);
95 }
96
97 void SVGRootInlineBox::layoutCharactersInTextBoxes(InlineFlowBox* start, SVGTextLayoutEngine& characterLayout)
98 {
99     for (InlineBox* child = start->firstChild(); child; child = child->nextOnLine()) {
100         if (child->isSVGInlineTextBox()) {
101             ASSERT(child->renderer());
102             ASSERT(child->renderer()->isSVGInlineText());
103
104             SVGInlineTextBox* textBox = static_cast<SVGInlineTextBox*>(child);
105             characterLayout.layoutInlineTextBox(textBox);
106         } else {
107             // Skip generated content.
108             Node* node = child->renderer()->node();
109             if (!node)
110                 continue;
111
112             ASSERT(child->isInlineFlowBox());
113
114             SVGInlineFlowBox* flowBox = static_cast<SVGInlineFlowBox*>(child);
115             bool isTextPath = node->hasTagName(SVGNames::textPathTag);
116             if (isTextPath) {
117                 // Build text chunks for all <textPath> children, using the line layout algorithm.
118                 // This is needeed as text-anchor is just an additional startOffset for text paths.
119                 RenderSVGText* parentBlock = toRenderSVGText(block());
120                 ASSERT(parentBlock);
121
122                 SVGTextLayoutEngine lineLayout(parentBlock->layoutAttributes());
123                 layoutCharactersInTextBoxes(flowBox, lineLayout);
124
125                 characterLayout.beginTextPathLayout(child->renderer(), lineLayout);
126             }
127
128             layoutCharactersInTextBoxes(flowBox, characterLayout);
129
130             if (isTextPath)
131                 characterLayout.endTextPathLayout();
132         }
133     }
134 }
135
136 void SVGRootInlineBox::layoutChildBoxes(InlineFlowBox* start, IntRect* childRect)
137 {
138     for (InlineBox* child = start->firstChild(); child; child = child->nextOnLine()) {
139         IntRect boxRect;
140         if (child->isSVGInlineTextBox()) {
141             ASSERT(child->renderer());
142             ASSERT(child->renderer()->isSVGInlineText());
143
144             SVGInlineTextBox* textBox = static_cast<SVGInlineTextBox*>(child);
145             boxRect = textBox->calculateBoundaries();
146             textBox->setX(boxRect.x());
147             textBox->setY(boxRect.y());
148             textBox->setLogicalWidth(boxRect.width());
149             textBox->setLogicalHeight(boxRect.height());
150         } else {
151             // Skip generated content.
152             if (!child->renderer()->node())
153                 continue;
154
155             ASSERT(child->isInlineFlowBox());
156
157             SVGInlineFlowBox* flowBox = static_cast<SVGInlineFlowBox*>(child);
158             layoutChildBoxes(flowBox);
159
160             boxRect = flowBox->calculateBoundaries();
161             flowBox->setX(boxRect.x());
162             flowBox->setY(boxRect.y());
163             flowBox->setLogicalWidth(boxRect.width());
164             flowBox->setLogicalHeight(boxRect.height());
165         }
166         if (childRect)
167             childRect->unite(boxRect);
168     }
169 }
170
171 void SVGRootInlineBox::layoutRootBox(const IntRect& childRect)
172 {
173     RenderBlock* parentBlock = block();
174     ASSERT(parentBlock);
175
176     int widthBlock = childRect.width();
177     int heightBlock = childRect.height();
178
179     // Finally, assign the root block position, now that all content is laid out.
180     parentBlock->setLocation(childRect.location());
181     parentBlock->setSize(childRect.size());
182
183     // Position all children relative to the parent block.
184     for (InlineBox* child = firstChild(); child; child = child->nextOnLine()) {
185         // Skip generated content.
186         if (!child->renderer()->node())
187             continue;
188         child->adjustPosition(-childRect.x(), -childRect.y());
189     }
190
191     // Position ourselves.
192     setX(0);
193     setY(0);
194     setLogicalWidth(widthBlock);
195     setLogicalHeight(heightBlock);
196     setBlockLogicalHeight(heightBlock);
197     setLineTopBottomPositions(0, heightBlock);
198 }
199
200 InlineBox* SVGRootInlineBox::closestLeafChildForPosition(const IntPoint& point)
201 {
202     InlineBox* firstLeaf = firstLeafChild();
203     InlineBox* lastLeaf = lastLeafChild();
204     if (firstLeaf == lastLeaf)
205         return firstLeaf;
206
207     // FIXME: Check for vertical text!
208     InlineBox* closestLeaf = 0;
209     for (InlineBox* leaf = firstLeaf; leaf; leaf = leaf->nextLeafChild()) {
210         if (!leaf->isSVGInlineTextBox())
211             continue;
212         if (point.y() < leaf->y())
213             continue;
214         if (point.y() > leaf->y() + leaf->virtualLogicalHeight())
215             continue;
216
217         closestLeaf = leaf;
218         if (point.x() < leaf->left() + leaf->logicalWidth())
219             return leaf;
220     }
221
222     return closestLeaf ? closestLeaf : lastLeaf;
223 }
224  
225 static inline void swapItemsInVector(Vector<float>& firstVector, Vector<float>& lastVector, unsigned first, unsigned last)
226 {
227     float temp = firstVector.at(first);
228     firstVector.at(first) = lastVector.at(last);
229     lastVector.at(last) = temp;
230 }
231
232 static inline void swapItemsInLayoutAttributes(SVGTextLayoutAttributes& firstAttributes, SVGTextLayoutAttributes& lastAttributes, unsigned firstPosition, unsigned lastPosition)
233 {
234     swapItemsInVector(firstAttributes.xValues(), lastAttributes.xValues(), firstPosition, lastPosition);
235     swapItemsInVector(firstAttributes.yValues(), lastAttributes.yValues(), firstPosition, lastPosition);
236     swapItemsInVector(firstAttributes.dxValues(), lastAttributes.dxValues(), firstPosition, lastPosition);
237     swapItemsInVector(firstAttributes.dyValues(), lastAttributes.dyValues(), firstPosition, lastPosition);
238     swapItemsInVector(firstAttributes.rotateValues(), lastAttributes.rotateValues(), firstPosition, lastPosition);
239 }
240
241 static inline void findFirstAndLastAttributesInVector(Vector<SVGTextLayoutAttributes>& attributes, RenderSVGInlineText* firstContext, RenderSVGInlineText* lastContext,
242                                                       SVGTextLayoutAttributes*& first, SVGTextLayoutAttributes*& last)
243 {
244     first = 0;
245     last = 0;
246
247     unsigned attributesSize = attributes.size();
248     for (unsigned i = 0; i < attributesSize; ++i) {
249         SVGTextLayoutAttributes& current = attributes.at(i);
250         if (!first && firstContext == current.context())
251             first = &current;
252         if (!last && lastContext == current.context())
253             last = &current;
254         if (first && last)
255             break;
256     }
257
258     ASSERT(first);
259     ASSERT(last);
260 }
261
262 static inline void reverseInlineBoxRangeAndValueListsIfNeeded(void* userData, Vector<InlineBox*>::iterator first, Vector<InlineBox*>::iterator last)
263 {
264     ASSERT(userData);
265     Vector<SVGTextLayoutAttributes>& attributes = *reinterpret_cast<Vector<SVGTextLayoutAttributes>*>(userData);
266
267     // This is a copy of std::reverse(first, last). It additionally assure that the value lists within the InlineBoxes are reordered as well.
268     while (true)  {
269         if (first == last || first == --last)
270             return;
271
272         if (!(*last)->isSVGInlineTextBox() || !(*first)->isSVGInlineTextBox()) {
273             InlineBox* temp = *first;
274             *first = *last;
275             *last = temp;
276             ++first;
277             continue;
278         }
279
280         SVGInlineTextBox* firstTextBox = static_cast<SVGInlineTextBox*>(*first);
281         SVGInlineTextBox* lastTextBox = static_cast<SVGInlineTextBox*>(*last);
282
283         // Reordering is only necessary for BiDi text that is _absolutely_ positioned.
284         if (firstTextBox->len() == 1 && firstTextBox->len() == lastTextBox->len()) {
285             RenderSVGInlineText* firstContext = toRenderSVGInlineText(firstTextBox->textRenderer());
286             RenderSVGInlineText* lastContext = toRenderSVGInlineText(lastTextBox->textRenderer());
287
288             SVGTextLayoutAttributes* firstAttributes = 0;
289             SVGTextLayoutAttributes* lastAttributes = 0;
290             findFirstAndLastAttributesInVector(attributes, firstContext, lastContext, firstAttributes, lastAttributes);
291
292             unsigned firstBoxPosition = firstTextBox->start();
293             unsigned firstBoxEnd = firstTextBox->end();
294
295             unsigned lastBoxPosition = lastTextBox->start();
296             unsigned lastBoxEnd = lastTextBox->end();
297             for (; firstBoxPosition <= firstBoxEnd && lastBoxPosition <= lastBoxEnd; ++lastBoxPosition, ++firstBoxPosition)
298                 swapItemsInLayoutAttributes(*firstAttributes, *lastAttributes, firstBoxPosition, lastBoxPosition);
299         }
300
301         InlineBox* temp = *first;
302         *first = *last;
303         *last = temp;
304
305         ++first;
306     }
307 }
308
309 void SVGRootInlineBox::reorderValueLists(Vector<SVGTextLayoutAttributes>& attributes)
310 {
311     Vector<InlineBox*> leafBoxesInLogicalOrder;
312     collectLeafBoxesInLogicalOrder(leafBoxesInLogicalOrder, reverseInlineBoxRangeAndValueListsIfNeeded, &attributes);
313 }
314
315 } // namespace WebCore
316
317 #endif // ENABLE(SVG)