Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / 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 "core/rendering/svg/SVGRootInlineBox.h"
26
27 #include "SVGNames.h"
28 #include "core/rendering/svg/RenderSVGInlineText.h"
29 #include "core/rendering/svg/RenderSVGText.h"
30 #include "core/rendering/svg/SVGInlineFlowBox.h"
31 #include "core/rendering/svg/SVGInlineTextBox.h"
32 #include "core/rendering/svg/SVGRenderingContext.h"
33
34 namespace WebCore {
35
36 void SVGRootInlineBox::paint(PaintInfo& paintInfo, const LayoutPoint& paintOffset, LayoutUnit, LayoutUnit)
37 {
38     ASSERT(paintInfo.phase == PaintPhaseForeground || paintInfo.phase == PaintPhaseSelection);
39     ASSERT(!paintInfo.context->paintingDisabled());
40
41     RenderObject* boxRenderer = renderer();
42     ASSERT(boxRenderer);
43
44     bool isPrinting = renderer()->document().printing();
45     bool hasSelection = !isPrinting && selectionState() != RenderObject::SelectionNone;
46
47     PaintInfo childPaintInfo(paintInfo);
48     if (hasSelection) {
49         for (InlineBox* child = firstChild(); child; child = child->nextOnLine()) {
50             if (child->isSVGInlineTextBox())
51                 toSVGInlineTextBox(child)->paintSelectionBackground(childPaintInfo);
52             else if (child->isSVGInlineFlowBox())
53                 toSVGInlineFlowBox(child)->paintSelectionBackground(childPaintInfo);
54         }
55     }
56
57     SVGRenderingContext renderingContext(boxRenderer, paintInfo, SVGRenderingContext::SaveGraphicsContext);
58     if (renderingContext.isRenderingPrepared()) {
59         for (InlineBox* child = firstChild(); child; child = child->nextOnLine())
60             child->paint(paintInfo, paintOffset, 0, 0);
61     }
62 }
63
64 void SVGRootInlineBox::markDirty(bool dirty)
65 {
66     if (dirty)
67         for (InlineBox* child = firstChild(); child; child = child->nextOnLine())
68             child->markDirty(true);
69     RootInlineBox::markDirty(dirty);
70 }
71
72 void SVGRootInlineBox::computePerCharacterLayoutInformation()
73 {
74     RenderSVGText* textRoot = toRenderSVGText(block());
75     ASSERT(textRoot);
76
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             characterLayout.layoutInlineTextBox(toSVGInlineTextBox(child));
105         } else {
106             // Skip generated content.
107             Node* node = child->renderer()->node();
108             if (!node)
109                 continue;
110
111             SVGInlineFlowBox* flowBox = toSVGInlineFlowBox(child);
112             bool isTextPath = node->hasTagName(SVGNames::textPathTag);
113             if (isTextPath) {
114                 // Build text chunks for all <textPath> children, using the line layout algorithm.
115                 // This is needeed as text-anchor is just an additional startOffset for text paths.
116                 SVGTextLayoutEngine lineLayout(characterLayout.layoutAttributes());
117                 layoutCharactersInTextBoxes(flowBox, lineLayout);
118
119                 characterLayout.beginTextPathLayout(child->renderer(), lineLayout);
120             }
121
122             layoutCharactersInTextBoxes(flowBox, characterLayout);
123
124             if (isTextPath)
125                 characterLayout.endTextPathLayout();
126         }
127     }
128 }
129
130 void SVGRootInlineBox::layoutChildBoxes(InlineFlowBox* start, FloatRect* childRect)
131 {
132     for (InlineBox* child = start->firstChild(); child; child = child->nextOnLine()) {
133         FloatRect boxRect;
134         if (child->isSVGInlineTextBox()) {
135             ASSERT(child->renderer());
136             ASSERT(child->renderer()->isSVGInlineText());
137
138             SVGInlineTextBox* textBox = toSVGInlineTextBox(child);
139             boxRect = textBox->calculateBoundaries();
140             textBox->setX(boxRect.x());
141             textBox->setY(boxRect.y());
142             textBox->setLogicalWidth(boxRect.width());
143             textBox->setLogicalHeight(boxRect.height());
144         } else {
145             // Skip generated content.
146             if (!child->renderer()->node())
147                 continue;
148
149             SVGInlineFlowBox* flowBox = toSVGInlineFlowBox(child);
150             layoutChildBoxes(flowBox);
151
152             boxRect = flowBox->calculateBoundaries();
153             flowBox->setX(boxRect.x());
154             flowBox->setY(boxRect.y());
155             flowBox->setLogicalWidth(boxRect.width());
156             flowBox->setLogicalHeight(boxRect.height());
157         }
158         if (childRect)
159             childRect->unite(boxRect);
160     }
161 }
162
163 void SVGRootInlineBox::layoutRootBox(const FloatRect& childRect)
164 {
165     RenderBlockFlow* parentBlock = block();
166     ASSERT(parentBlock);
167
168     // Finally, assign the root block position, now that all content is laid out.
169     LayoutRect boundingRect = enclosingLayoutRect(childRect);
170     parentBlock->setLocation(boundingRect.location());
171     parentBlock->setSize(boundingRect.size());
172
173     // Position all children relative to the parent block.
174     for (InlineBox* child = firstChild(); child; child = child->nextOnLine()) {
175         // Skip generated content.
176         if (!child->renderer()->node())
177             continue;
178         child->adjustPosition(-childRect.x(), -childRect.y());
179     }
180
181     // Position ourselves.
182     setX(0);
183     setY(0);
184     setLogicalWidth(childRect.width());
185     setLogicalHeight(childRect.height());
186     setLineTopBottomPositions(0, boundingRect.height(), 0, boundingRect.height());
187 }
188
189 InlineBox* SVGRootInlineBox::closestLeafChildForPosition(const LayoutPoint& point)
190 {
191     InlineBox* firstLeaf = firstLeafChild();
192     InlineBox* lastLeaf = lastLeafChild();
193     if (firstLeaf == lastLeaf)
194         return firstLeaf;
195
196     // FIXME: Check for vertical text!
197     InlineBox* closestLeaf = 0;
198     for (InlineBox* leaf = firstLeaf; leaf; leaf = leaf->nextLeafChild()) {
199         if (!leaf->isSVGInlineTextBox())
200             continue;
201         if (point.y() < leaf->y())
202             continue;
203         if (point.y() > leaf->y() + leaf->virtualLogicalHeight())
204             continue;
205
206         closestLeaf = leaf;
207         if (point.x() < leaf->left() + leaf->logicalWidth())
208             return leaf;
209     }
210
211     return closestLeaf ? closestLeaf : lastLeaf;
212 }
213
214 static inline void swapItemsInLayoutAttributes(SVGTextLayoutAttributes* firstAttributes, SVGTextLayoutAttributes* lastAttributes, unsigned firstPosition, unsigned lastPosition)
215 {
216     SVGCharacterDataMap::iterator itFirst = firstAttributes->characterDataMap().find(firstPosition + 1);
217     SVGCharacterDataMap::iterator itLast = lastAttributes->characterDataMap().find(lastPosition + 1);
218     bool firstPresent = itFirst != firstAttributes->characterDataMap().end();
219     bool lastPresent = itLast != lastAttributes->characterDataMap().end();
220     if (!firstPresent && !lastPresent)
221         return;
222
223     if (firstPresent && lastPresent) {
224         std::swap(itFirst->value, itLast->value);
225         return;
226     }
227
228     if (firstPresent && !lastPresent) {
229         lastAttributes->characterDataMap().set(lastPosition + 1, itFirst->value);
230         return;
231     }
232
233     // !firstPresent && lastPresent
234     firstAttributes->characterDataMap().set(firstPosition + 1, itLast->value);
235 }
236
237 static inline void findFirstAndLastAttributesInVector(Vector<SVGTextLayoutAttributes*>& attributes, RenderSVGInlineText* firstContext, RenderSVGInlineText* lastContext,
238                                                       SVGTextLayoutAttributes*& first, SVGTextLayoutAttributes*& last)
239 {
240     first = 0;
241     last = 0;
242
243     unsigned attributesSize = attributes.size();
244     for (unsigned i = 0; i < attributesSize; ++i) {
245         SVGTextLayoutAttributes* current = attributes[i];
246         if (!first && firstContext == current->context())
247             first = current;
248         if (!last && lastContext == current->context())
249             last = current;
250         if (first && last)
251             break;
252     }
253
254     ASSERT(first);
255     ASSERT(last);
256 }
257
258 static inline void reverseInlineBoxRangeAndValueListsIfNeeded(void* userData, Vector<InlineBox*>::iterator first, Vector<InlineBox*>::iterator last)
259 {
260     ASSERT(userData);
261     Vector<SVGTextLayoutAttributes*>& attributes = *reinterpret_cast<Vector<SVGTextLayoutAttributes*>*>(userData);
262
263     // 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.
264     while (true)  {
265         if (first == last || first == --last)
266             return;
267
268         if (!(*last)->isSVGInlineTextBox() || !(*first)->isSVGInlineTextBox()) {
269             InlineBox* temp = *first;
270             *first = *last;
271             *last = temp;
272             ++first;
273             continue;
274         }
275
276         SVGInlineTextBox* firstTextBox = toSVGInlineTextBox(*first);
277         SVGInlineTextBox* lastTextBox = toSVGInlineTextBox(*last);
278
279         // Reordering is only necessary for BiDi text that is _absolutely_ positioned.
280         if (firstTextBox->len() == 1 && firstTextBox->len() == lastTextBox->len()) {
281             RenderSVGInlineText* firstContext = toRenderSVGInlineText(firstTextBox->textRenderer());
282             RenderSVGInlineText* lastContext = toRenderSVGInlineText(lastTextBox->textRenderer());
283
284             SVGTextLayoutAttributes* firstAttributes = 0;
285             SVGTextLayoutAttributes* lastAttributes = 0;
286             findFirstAndLastAttributesInVector(attributes, firstContext, lastContext, firstAttributes, lastAttributes);
287             swapItemsInLayoutAttributes(firstAttributes, lastAttributes, firstTextBox->start(), lastTextBox->start());
288         }
289
290         InlineBox* temp = *first;
291         *first = *last;
292         *last = temp;
293
294         ++first;
295     }
296 }
297
298 void SVGRootInlineBox::reorderValueLists(Vector<SVGTextLayoutAttributes*>& attributes)
299 {
300     Vector<InlineBox*> leafBoxesInLogicalOrder;
301     collectLeafBoxesInLogicalOrder(leafBoxesInLogicalOrder, reverseInlineBoxRangeAndValueListsIfNeeded, &attributes);
302 }
303
304 } // namespace WebCore