Switch remaining SVG Rendering methods to LayoutUnits
[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 LayoutPoint&, LayoutUnit, LayoutUnit)
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, LayoutPoint(), 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 (attributes.isEmpty())
81         return;
82
83     if (parentBlock->needsReordering())
84         reorderValueLists(attributes);
85
86     // Perform SVG text layout phase two (see SVGTextLayoutEngine for details).
87     SVGTextLayoutEngine characterLayout(attributes);
88     layoutCharactersInTextBoxes(this, characterLayout);
89
90     // Perform SVG text layout phase three (see SVGTextChunkBuilder for details).
91     characterLayout.finishLayout();
92
93     // Perform SVG text layout phase four
94     // Position & resize all SVGInlineText/FlowBoxes in the inline box tree, resize the root box as well as the RenderSVGText parent block.
95     LayoutRect childRect;
96     layoutChildBoxes(this, &childRect);
97     layoutRootBox(childRect);
98 }
99
100 void SVGRootInlineBox::layoutCharactersInTextBoxes(InlineFlowBox* start, SVGTextLayoutEngine& characterLayout)
101 {
102     for (InlineBox* child = start->firstChild(); child; child = child->nextOnLine()) {
103         if (child->isSVGInlineTextBox()) {
104             ASSERT(child->renderer());
105             ASSERT(child->renderer()->isSVGInlineText());
106
107             SVGInlineTextBox* textBox = static_cast<SVGInlineTextBox*>(child);
108             characterLayout.layoutInlineTextBox(textBox);
109         } else {
110             // Skip generated content.
111             Node* node = child->renderer()->node();
112             if (!node)
113                 continue;
114
115             ASSERT(child->isInlineFlowBox());
116
117             SVGInlineFlowBox* flowBox = static_cast<SVGInlineFlowBox*>(child);
118             bool isTextPath = node->hasTagName(SVGNames::textPathTag);
119             if (isTextPath) {
120                 // Build text chunks for all <textPath> children, using the line layout algorithm.
121                 // This is needeed as text-anchor is just an additional startOffset for text paths.
122                 RenderSVGText* parentBlock = toRenderSVGText(block());
123                 ASSERT(parentBlock);
124
125                 SVGTextLayoutEngine lineLayout(parentBlock->layoutAttributes());
126                 layoutCharactersInTextBoxes(flowBox, lineLayout);
127
128                 characterLayout.beginTextPathLayout(child->renderer(), lineLayout);
129             }
130
131             layoutCharactersInTextBoxes(flowBox, characterLayout);
132
133             if (isTextPath)
134                 characterLayout.endTextPathLayout();
135         }
136     }
137 }
138
139 void SVGRootInlineBox::layoutChildBoxes(InlineFlowBox* start, LayoutRect* childRect)
140 {
141     for (InlineBox* child = start->firstChild(); child; child = child->nextOnLine()) {
142         LayoutRect boxRect;
143         if (child->isSVGInlineTextBox()) {
144             ASSERT(child->renderer());
145             ASSERT(child->renderer()->isSVGInlineText());
146
147             SVGInlineTextBox* textBox = static_cast<SVGInlineTextBox*>(child);
148             boxRect = textBox->calculateBoundaries();
149             textBox->setX(boxRect.x());
150             textBox->setY(boxRect.y());
151             textBox->setLogicalWidth(boxRect.width());
152             textBox->setLogicalHeight(boxRect.height());
153         } else {
154             // Skip generated content.
155             if (!child->renderer()->node())
156                 continue;
157
158             ASSERT(child->isInlineFlowBox());
159
160             SVGInlineFlowBox* flowBox = static_cast<SVGInlineFlowBox*>(child);
161             layoutChildBoxes(flowBox);
162
163             boxRect = flowBox->calculateBoundaries();
164             flowBox->setX(boxRect.x());
165             flowBox->setY(boxRect.y());
166             flowBox->setLogicalWidth(boxRect.width());
167             flowBox->setLogicalHeight(boxRect.height());
168         }
169         if (childRect)
170             childRect->unite(boxRect);
171     }
172 }
173
174 void SVGRootInlineBox::layoutRootBox(const LayoutRect& childRect)
175 {
176     RenderBlock* parentBlock = block();
177     ASSERT(parentBlock);
178
179     LayoutUnit widthBlock = childRect.width();
180     LayoutUnit heightBlock = childRect.height();
181
182     // Finally, assign the root block position, now that all content is laid out.
183     parentBlock->setLocation(childRect.location());
184     parentBlock->setSize(childRect.size());
185
186     // Position all children relative to the parent block.
187     for (InlineBox* child = firstChild(); child; child = child->nextOnLine()) {
188         // Skip generated content.
189         if (!child->renderer()->node())
190             continue;
191         child->adjustPosition(-childRect.x(), -childRect.y());
192     }
193
194     // Position ourselves.
195     setX(0);
196     setY(0);
197     setLogicalWidth(widthBlock);
198     setLogicalHeight(heightBlock);
199     setBlockLogicalHeight(heightBlock);
200     setLineTopBottomPositions(0, heightBlock);
201 }
202
203 InlineBox* SVGRootInlineBox::closestLeafChildForPosition(const LayoutPoint& point)
204 {
205     InlineBox* firstLeaf = firstLeafChild();
206     InlineBox* lastLeaf = lastLeafChild();
207     if (firstLeaf == lastLeaf)
208         return firstLeaf;
209
210     // FIXME: Check for vertical text!
211     InlineBox* closestLeaf = 0;
212     for (InlineBox* leaf = firstLeaf; leaf; leaf = leaf->nextLeafChild()) {
213         if (!leaf->isSVGInlineTextBox())
214             continue;
215         if (point.y() < leaf->y())
216             continue;
217         if (point.y() > leaf->y() + leaf->virtualLogicalHeight())
218             continue;
219
220         closestLeaf = leaf;
221         if (point.x() < leaf->left() + leaf->logicalWidth())
222             return leaf;
223     }
224
225     return closestLeaf ? closestLeaf : lastLeaf;
226 }
227  
228 static inline void swapItemsInVector(Vector<float>& firstVector, Vector<float>& lastVector, unsigned first, unsigned last)
229 {
230     float temp = firstVector.at(first);
231     firstVector.at(first) = lastVector.at(last);
232     lastVector.at(last) = temp;
233 }
234
235 static inline void swapItemsInLayoutAttributes(SVGTextLayoutAttributes& firstAttributes, SVGTextLayoutAttributes& lastAttributes, unsigned firstPosition, unsigned lastPosition)
236 {
237     swapItemsInVector(firstAttributes.xValues(), lastAttributes.xValues(), firstPosition, lastPosition);
238     swapItemsInVector(firstAttributes.yValues(), lastAttributes.yValues(), firstPosition, lastPosition);
239     swapItemsInVector(firstAttributes.dxValues(), lastAttributes.dxValues(), firstPosition, lastPosition);
240     swapItemsInVector(firstAttributes.dyValues(), lastAttributes.dyValues(), firstPosition, lastPosition);
241     swapItemsInVector(firstAttributes.rotateValues(), lastAttributes.rotateValues(), firstPosition, lastPosition);
242 }
243
244 static inline void findFirstAndLastAttributesInVector(Vector<SVGTextLayoutAttributes>& attributes, RenderSVGInlineText* firstContext, RenderSVGInlineText* lastContext,
245                                                       SVGTextLayoutAttributes*& first, SVGTextLayoutAttributes*& last)
246 {
247     first = 0;
248     last = 0;
249
250     unsigned attributesSize = attributes.size();
251     for (unsigned i = 0; i < attributesSize; ++i) {
252         SVGTextLayoutAttributes& current = attributes.at(i);
253         if (!first && firstContext == current.context())
254             first = &current;
255         if (!last && lastContext == current.context())
256             last = &current;
257         if (first && last)
258             break;
259     }
260
261     ASSERT(first);
262     ASSERT(last);
263 }
264
265 static inline void reverseInlineBoxRangeAndValueListsIfNeeded(void* userData, Vector<InlineBox*>::iterator first, Vector<InlineBox*>::iterator last)
266 {
267     ASSERT(userData);
268     Vector<SVGTextLayoutAttributes>& attributes = *reinterpret_cast<Vector<SVGTextLayoutAttributes>*>(userData);
269
270     // This is a copy of std::reverse(first, last). It additionally assure that the value lists within the InlineBoxes are reordered as well.
271     while (true)  {
272         if (first == last || first == --last)
273             return;
274
275         if (!(*last)->isSVGInlineTextBox() || !(*first)->isSVGInlineTextBox()) {
276             InlineBox* temp = *first;
277             *first = *last;
278             *last = temp;
279             ++first;
280             continue;
281         }
282
283         SVGInlineTextBox* firstTextBox = static_cast<SVGInlineTextBox*>(*first);
284         SVGInlineTextBox* lastTextBox = static_cast<SVGInlineTextBox*>(*last);
285
286         // Reordering is only necessary for BiDi text that is _absolutely_ positioned.
287         if (firstTextBox->len() == 1 && firstTextBox->len() == lastTextBox->len()) {
288             RenderSVGInlineText* firstContext = toRenderSVGInlineText(firstTextBox->textRenderer());
289             RenderSVGInlineText* lastContext = toRenderSVGInlineText(lastTextBox->textRenderer());
290
291             SVGTextLayoutAttributes* firstAttributes = 0;
292             SVGTextLayoutAttributes* lastAttributes = 0;
293             findFirstAndLastAttributesInVector(attributes, firstContext, lastContext, firstAttributes, lastAttributes);
294
295             unsigned firstBoxPosition = firstTextBox->start();
296             unsigned firstBoxEnd = firstTextBox->end();
297
298             unsigned lastBoxPosition = lastTextBox->start();
299             unsigned lastBoxEnd = lastTextBox->end();
300             for (; firstBoxPosition <= firstBoxEnd && lastBoxPosition <= lastBoxEnd; ++lastBoxPosition, ++firstBoxPosition)
301                 swapItemsInLayoutAttributes(*firstAttributes, *lastAttributes, firstBoxPosition, lastBoxPosition);
302         }
303
304         InlineBox* temp = *first;
305         *first = *last;
306         *last = temp;
307
308         ++first;
309     }
310 }
311
312 void SVGRootInlineBox::reorderValueLists(Vector<SVGTextLayoutAttributes>& attributes)
313 {
314     Vector<InlineBox*> leafBoxesInLogicalOrder;
315     collectLeafBoxesInLogicalOrder(leafBoxesInLogicalOrder, reverseInlineBoxRangeAndValueListsIfNeeded, &attributes);
316 }
317
318 } // namespace WebCore
319
320 #endif // ENABLE(SVG)