34e93a02ef7432e3c26ef2360d1cf3fd437e416e
[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     Vector<SVGTextLayoutAttributes*>& layoutAttributes = textRoot->layoutAttributes();
77     if (layoutAttributes.isEmpty())
78         return;
79
80     if (textRoot->needsReordering())
81         reorderValueLists(layoutAttributes);
82
83     // Perform SVG text layout phase two (see SVGTextLayoutEngine for details).
84     SVGTextLayoutEngine characterLayout(layoutAttributes);
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     FloatRect 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                 SVGTextLayoutEngine lineLayout(characterLayout.layoutAttributes());
120                 layoutCharactersInTextBoxes(flowBox, lineLayout);
121
122                 characterLayout.beginTextPathLayout(child->renderer(), lineLayout);
123             }
124
125             layoutCharactersInTextBoxes(flowBox, characterLayout);
126
127             if (isTextPath)
128                 characterLayout.endTextPathLayout();
129         }
130     }
131 }
132
133 void SVGRootInlineBox::layoutChildBoxes(InlineFlowBox* start, FloatRect* childRect)
134 {
135     for (InlineBox* child = start->firstChild(); child; child = child->nextOnLine()) {
136         FloatRect boxRect;
137         if (child->isSVGInlineTextBox()) {
138             ASSERT(child->renderer());
139             ASSERT(child->renderer()->isSVGInlineText());
140
141             SVGInlineTextBox* textBox = static_cast<SVGInlineTextBox*>(child);
142             boxRect = textBox->calculateBoundaries();
143             textBox->setX(boxRect.x());
144             textBox->setY(boxRect.y());
145             textBox->setLogicalWidth(boxRect.width());
146             textBox->setLogicalHeight(boxRect.height());
147         } else {
148             // Skip generated content.
149             if (!child->renderer()->node())
150                 continue;
151
152             ASSERT(child->isInlineFlowBox());
153
154             SVGInlineFlowBox* flowBox = static_cast<SVGInlineFlowBox*>(child);
155             layoutChildBoxes(flowBox);
156
157             boxRect = flowBox->calculateBoundaries();
158             flowBox->setX(boxRect.x());
159             flowBox->setY(boxRect.y());
160             flowBox->setLogicalWidth(boxRect.width());
161             flowBox->setLogicalHeight(boxRect.height());
162         }
163         if (childRect)
164             childRect->unite(boxRect);
165     }
166 }
167
168 void SVGRootInlineBox::layoutRootBox(const FloatRect& childRect)
169 {
170     RenderBlock* parentBlock = block();
171     ASSERT(parentBlock);
172
173     // Finally, assign the root block position, now that all content is laid out.
174     IntRect roundedChildRect = enclosingIntRect(childRect);
175     parentBlock->setLocation(roundedChildRect.location());
176     parentBlock->setSize(roundedChildRect.size());
177
178     // Position all children relative to the parent block.
179     for (InlineBox* child = firstChild(); child; child = child->nextOnLine()) {
180         // Skip generated content.
181         if (!child->renderer()->node())
182             continue;
183         child->adjustPosition(-childRect.x(), -childRect.y());
184     }
185
186     // Position ourselves.
187     setX(0);
188     setY(0);
189     setLogicalWidth(childRect.width());
190     setLogicalHeight(childRect.height());
191     setLineTopBottomPositions(0, roundedChildRect.height(), 0, roundedChildRect.height());
192 }
193
194 InlineBox* SVGRootInlineBox::closestLeafChildForPosition(const LayoutPoint& point)
195 {
196     InlineBox* firstLeaf = firstLeafChild();
197     InlineBox* lastLeaf = lastLeafChild();
198     if (firstLeaf == lastLeaf)
199         return firstLeaf;
200
201     // FIXME: Check for vertical text!
202     InlineBox* closestLeaf = 0;
203     for (InlineBox* leaf = firstLeaf; leaf; leaf = leaf->nextLeafChild()) {
204         if (!leaf->isSVGInlineTextBox())
205             continue;
206         if (point.y() < leaf->y())
207             continue;
208         if (point.y() > leaf->y() + leaf->virtualLogicalHeight())
209             continue;
210
211         closestLeaf = leaf;
212         if (point.x() < leaf->left() + leaf->logicalWidth())
213             return leaf;
214     }
215
216     return closestLeaf ? closestLeaf : lastLeaf;
217 }
218
219 static inline void swapItemsInLayoutAttributes(SVGTextLayoutAttributes* firstAttributes, SVGTextLayoutAttributes* lastAttributes, unsigned firstPosition, unsigned lastPosition)
220 {
221     SVGCharacterDataMap::iterator itFirst = firstAttributes->characterDataMap().find(firstPosition + 1);
222     SVGCharacterDataMap::iterator itLast = lastAttributes->characterDataMap().find(lastPosition + 1);
223     bool firstPresent = itFirst != firstAttributes->characterDataMap().end();
224     bool lastPresent = itLast != lastAttributes->characterDataMap().end();
225     if (!firstPresent && !lastPresent)
226         return;
227
228     if (firstPresent && lastPresent) {
229         std::swap(itFirst->second, itLast->second);
230         return;
231     }
232
233     if (firstPresent && !lastPresent) {
234         lastAttributes->characterDataMap().set(lastPosition + 1, itFirst->second);
235         return;
236     }
237
238     // !firstPresent && lastPresent
239     firstAttributes->characterDataMap().set(firstPosition + 1, itLast->second);
240 }
241
242 static inline void findFirstAndLastAttributesInVector(Vector<SVGTextLayoutAttributes*>& attributes, RenderSVGInlineText* firstContext, RenderSVGInlineText* lastContext,
243                                                       SVGTextLayoutAttributes*& first, SVGTextLayoutAttributes*& last)
244 {
245     first = 0;
246     last = 0;
247
248     unsigned attributesSize = attributes.size();
249     for (unsigned i = 0; i < attributesSize; ++i) {
250         SVGTextLayoutAttributes* current = attributes[i];
251         if (!first && firstContext == current->context())
252             first = current;
253         if (!last && lastContext == current->context())
254             last = current;
255         if (first && last)
256             break;
257     }
258
259     ASSERT(first);
260     ASSERT(last);
261 }
262
263 static inline void reverseInlineBoxRangeAndValueListsIfNeeded(void* userData, Vector<InlineBox*>::iterator first, Vector<InlineBox*>::iterator last)
264 {
265     ASSERT(userData);
266     Vector<SVGTextLayoutAttributes*>& attributes = *reinterpret_cast<Vector<SVGTextLayoutAttributes*>*>(userData);
267
268     // 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.
269     while (true)  {
270         if (first == last || first == --last)
271             return;
272
273         if (!(*last)->isSVGInlineTextBox() || !(*first)->isSVGInlineTextBox()) {
274             InlineBox* temp = *first;
275             *first = *last;
276             *last = temp;
277             ++first;
278             continue;
279         }
280
281         SVGInlineTextBox* firstTextBox = static_cast<SVGInlineTextBox*>(*first);
282         SVGInlineTextBox* lastTextBox = static_cast<SVGInlineTextBox*>(*last);
283
284         // Reordering is only necessary for BiDi text that is _absolutely_ positioned.
285         if (firstTextBox->len() == 1 && firstTextBox->len() == lastTextBox->len()) {
286             RenderSVGInlineText* firstContext = toRenderSVGInlineText(firstTextBox->textRenderer());
287             RenderSVGInlineText* lastContext = toRenderSVGInlineText(lastTextBox->textRenderer());
288
289             SVGTextLayoutAttributes* firstAttributes = 0;
290             SVGTextLayoutAttributes* lastAttributes = 0;
291             findFirstAndLastAttributesInVector(attributes, firstContext, lastContext, firstAttributes, lastAttributes);
292             swapItemsInLayoutAttributes(firstAttributes, lastAttributes, firstTextBox->start(), lastTextBox->start());
293         }
294
295         InlineBox* temp = *first;
296         *first = *last;
297         *last = temp;
298
299         ++first;
300     }
301 }
302
303 void SVGRootInlineBox::reorderValueLists(Vector<SVGTextLayoutAttributes*>& attributes)
304 {
305     Vector<InlineBox*> leafBoxesInLogicalOrder;
306     collectLeafBoxesInLogicalOrder(leafBoxesInLogicalOrder, reverseInlineBoxRangeAndValueListsIfNeeded, &attributes);
307 }
308
309 } // namespace WebCore
310
311 #endif // ENABLE(SVG)