4d796ca85fb1a66a409d11c314dd9496f52ff88f
[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 SVGRootInlineBox::SVGRootInlineBox(RenderSVGText& renderSVGText)
40     : RootInlineBox(renderSVGText)
41     , m_logicalHeight(0)
42 {
43 }
44
45 RenderSVGText& SVGRootInlineBox::renderSVGText()
46 {
47     return toRenderSVGText(blockFlow());
48 }
49
50 void SVGRootInlineBox::paint(PaintInfo& paintInfo, const LayoutPoint&, LayoutUnit, LayoutUnit)
51 {
52     ASSERT(paintInfo.phase == PaintPhaseForeground || paintInfo.phase == PaintPhaseSelection);
53     ASSERT(!paintInfo.context->paintingDisabled());
54
55     bool isPrinting = renderSVGText().document().printing();
56     bool hasSelection = !isPrinting && selectionState() != RenderObject::SelectionNone;
57
58     PaintInfo childPaintInfo(paintInfo);
59     if (hasSelection) {
60         for (InlineBox* child = firstChild(); child; child = child->nextOnLine()) {
61             if (child->isSVGInlineTextBox())
62                 toSVGInlineTextBox(child)->paintSelectionBackground(childPaintInfo);
63             else if (child->isSVGInlineFlowBox())
64                 toSVGInlineFlowBox(child)->paintSelectionBackground(childPaintInfo);
65         }
66     }
67
68     SVGRenderingContext renderingContext(&renderSVGText(), paintInfo, SVGRenderingContext::SaveGraphicsContext);
69     if (renderingContext.isRenderingPrepared()) {
70         for (InlineBox* child = firstChild(); child; child = child->nextOnLine()) {
71             if (child->isSVGInlineTextBox())
72                 SVGInlineFlowBox::computeTextMatchMarkerRectForRenderer(&toSVGInlineTextBox(child)->renderer());
73
74             child->paint(paintInfo, LayoutPoint(), 0, 0);
75         }
76     }
77 }
78
79 void SVGRootInlineBox::computePerCharacterLayoutInformation()
80 {
81     RenderSVGText* textRoot = toRenderSVGText(&blockFlow());
82     ASSERT(textRoot);
83
84     Vector<SVGTextLayoutAttributes*>& layoutAttributes = textRoot->layoutAttributes();
85     if (layoutAttributes.isEmpty())
86         return;
87
88     if (textRoot->needsReordering())
89         reorderValueLists(layoutAttributes);
90
91     // Perform SVG text layout phase two (see SVGTextLayoutEngine for details).
92     SVGTextLayoutEngine characterLayout(layoutAttributes);
93     layoutCharactersInTextBoxes(this, characterLayout);
94
95     // Perform SVG text layout phase three (see SVGTextChunkBuilder for details).
96     characterLayout.finishLayout();
97
98     // Perform SVG text layout phase four
99     // Position & resize all SVGInlineText/FlowBoxes in the inline box tree, resize the root box as well as the RenderSVGText parent block.
100     FloatRect childRect;
101     layoutChildBoxes(this, &childRect);
102     layoutRootBox(childRect);
103 }
104
105 void SVGRootInlineBox::layoutCharactersInTextBoxes(InlineFlowBox* start, SVGTextLayoutEngine& characterLayout)
106 {
107     for (InlineBox* child = start->firstChild(); child; child = child->nextOnLine()) {
108         if (child->isSVGInlineTextBox()) {
109             ASSERT(child->renderer().isSVGInlineText());
110
111             SVGInlineTextBox* textBox = toSVGInlineTextBox(child);
112             characterLayout.layoutInlineTextBox(textBox);
113         } else {
114             // Skip generated content.
115             Node* node = child->renderer().node();
116             if (!node)
117                 continue;
118
119             ASSERT_WITH_SECURITY_IMPLICATION(child->isInlineFlowBox());
120
121             SVGInlineFlowBox* flowBox = toSVGInlineFlowBox(child);
122             bool isTextPath = node->hasTagName(SVGNames::textPathTag);
123             if (isTextPath) {
124                 // Build text chunks for all <textPath> children, using the line layout algorithm.
125                 // This is needeed as text-anchor is just an additional startOffset for text paths.
126                 SVGTextLayoutEngine lineLayout(characterLayout.layoutAttributes());
127                 layoutCharactersInTextBoxes(flowBox, lineLayout);
128
129                 characterLayout.beginTextPathLayout(&child->renderer(), lineLayout);
130             }
131
132             layoutCharactersInTextBoxes(flowBox, characterLayout);
133
134             if (isTextPath)
135                 characterLayout.endTextPathLayout();
136         }
137     }
138 }
139
140 void SVGRootInlineBox::layoutChildBoxes(InlineFlowBox* start, FloatRect* childRect)
141 {
142     for (InlineBox* child = start->firstChild(); child; child = child->nextOnLine()) {
143         FloatRect boxRect;
144         if (child->isSVGInlineTextBox()) {
145             ASSERT(child->renderer().isSVGInlineText());
146
147             SVGInlineTextBox* textBox = toSVGInlineTextBox(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_WITH_SECURITY_IMPLICATION(child->isInlineFlowBox());
159
160             SVGInlineFlowBox* flowBox = toSVGInlineFlowBox(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 FloatRect& childRect)
175 {
176     RenderSVGText& parentBlock = renderSVGText();
177
178     // Finally, assign the root block position, now that all content is laid out.
179     LayoutRect boundingRect = enclosingLayoutRect(childRect);
180     parentBlock.setLocation(boundingRect.location());
181     parentBlock.setSize(boundingRect.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(childRect.width());
195     setLogicalHeight(childRect.height());
196     setLineTopBottomPositions(0, boundingRect.height(), 0, boundingRect.height());
197 }
198
199 InlineBox* SVGRootInlineBox::closestLeafChildForPosition(const LayoutPoint& point)
200 {
201     InlineBox* firstLeaf = firstLeafChild();
202     InlineBox* lastLeaf = lastLeafChild();
203     if (firstLeaf == lastLeaf)
204         return firstLeaf;
205
206     // FIXME: Check for vertical text!
207     InlineBox* closestLeaf = 0;
208     for (InlineBox* leaf = firstLeaf; leaf; leaf = leaf->nextLeafChild()) {
209         if (!leaf->isSVGInlineTextBox())
210             continue;
211         if (point.y() < leaf->y())
212             continue;
213         if (point.y() > leaf->y() + leaf->virtualLogicalHeight())
214             continue;
215
216         closestLeaf = leaf;
217         if (point.x() < leaf->left() + leaf->logicalWidth())
218             return leaf;
219     }
220
221     return closestLeaf ? closestLeaf : lastLeaf;
222 }
223
224 static inline void swapItemsInLayoutAttributes(SVGTextLayoutAttributes* firstAttributes, SVGTextLayoutAttributes* lastAttributes, unsigned firstPosition, unsigned lastPosition)
225 {
226     SVGCharacterDataMap::iterator itFirst = firstAttributes->characterDataMap().find(firstPosition + 1);
227     SVGCharacterDataMap::iterator itLast = lastAttributes->characterDataMap().find(lastPosition + 1);
228     bool firstPresent = itFirst != firstAttributes->characterDataMap().end();
229     bool lastPresent = itLast != lastAttributes->characterDataMap().end();
230     if (!firstPresent && !lastPresent)
231         return;
232
233     if (firstPresent && lastPresent) {
234         std::swap(itFirst->value, itLast->value);
235         return;
236     }
237
238     if (firstPresent && !lastPresent) {
239         lastAttributes->characterDataMap().set(lastPosition + 1, itFirst->value);
240         return;
241     }
242
243     // !firstPresent && lastPresent
244     firstAttributes->characterDataMap().set(firstPosition + 1, itLast->value);
245 }
246
247 static inline void findFirstAndLastAttributesInVector(Vector<SVGTextLayoutAttributes*>& attributes, RenderSVGInlineText* firstContext, RenderSVGInlineText* lastContext,
248                                                       SVGTextLayoutAttributes*& first, SVGTextLayoutAttributes*& last)
249 {
250     first = 0;
251     last = 0;
252
253     unsigned attributesSize = attributes.size();
254     for (unsigned i = 0; i < attributesSize; ++i) {
255         SVGTextLayoutAttributes* current = attributes[i];
256         if (!first && firstContext == current->context())
257             first = current;
258         if (!last && lastContext == current->context())
259             last = current;
260         if (first && last)
261             break;
262     }
263
264     ASSERT(first);
265     ASSERT(last);
266 }
267
268 static inline void reverseInlineBoxRangeAndValueListsIfNeeded(void* userData, Vector<InlineBox*>::iterator first, Vector<InlineBox*>::iterator last)
269 {
270     ASSERT(userData);
271     Vector<SVGTextLayoutAttributes*>& attributes = *reinterpret_cast<Vector<SVGTextLayoutAttributes*>*>(userData);
272
273     // 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.
274     while (true)  {
275         if (first == last || first == --last)
276             return;
277
278         if (!(*last)->isSVGInlineTextBox() || !(*first)->isSVGInlineTextBox()) {
279             InlineBox* temp = *first;
280             *first = *last;
281             *last = temp;
282             ++first;
283             continue;
284         }
285
286         SVGInlineTextBox* firstTextBox = toSVGInlineTextBox(*first);
287         SVGInlineTextBox* lastTextBox = toSVGInlineTextBox(*last);
288
289         // Reordering is only necessary for BiDi text that is _absolutely_ positioned.
290         if (firstTextBox->len() == 1 && firstTextBox->len() == lastTextBox->len()) {
291             RenderSVGInlineText& firstContext = firstTextBox->renderer();
292             RenderSVGInlineText& lastContext = lastTextBox->renderer();
293
294             SVGTextLayoutAttributes* firstAttributes = 0;
295             SVGTextLayoutAttributes* lastAttributes = 0;
296             findFirstAndLastAttributesInVector(attributes, &firstContext, &lastContext, firstAttributes, lastAttributes);
297             swapItemsInLayoutAttributes(firstAttributes, lastAttributes, firstTextBox->start(), lastTextBox->start());
298         }
299
300         InlineBox* temp = *first;
301         *first = *last;
302         *last = temp;
303
304         ++first;
305     }
306 }
307
308 void SVGRootInlineBox::reorderValueLists(Vector<SVGTextLayoutAttributes*>& attributes)
309 {
310     Vector<InlineBox*> leafBoxesInLogicalOrder;
311     collectLeafBoxesInLogicalOrder(leafBoxesInLogicalOrder, reverseInlineBoxRangeAndValueListsIfNeeded, &attributes);
312 }
313
314 } // namespace WebCore
315
316 #endif // ENABLE(SVG)