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