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