SVG: hit testing region for <text> elements is incorrect
[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 "RenderSVGTextPath.h"
30 #include "SVGInlineFlowBox.h"
31 #include "SVGInlineTextBox.h"
32 #include "SVGNames.h"
33 #include "SVGRenderingContext.h"
34 #include "SVGTextPositioningElement.h"
35
36 namespace WebCore {
37
38 SVGRootInlineBox::SVGRootInlineBox(RenderSVGText& renderSVGText)
39     : RootInlineBox(renderSVGText)
40     , m_logicalHeight(0)
41 {
42 }
43
44 RenderSVGText& SVGRootInlineBox::renderSVGText()
45 {
46     return downcast<RenderSVGText>(blockFlow());
47 }
48
49 void SVGRootInlineBox::paint(PaintInfo& paintInfo, const LayoutPoint& paintOffset, LayoutUnit, LayoutUnit)
50 {
51     ASSERT(paintInfo.phase == PaintPhaseForeground || paintInfo.phase == PaintPhaseSelection);
52     ASSERT(!paintInfo.context().paintingDisabled());
53
54     bool isPrinting = renderSVGText().document().printing();
55     bool hasSelection = !isPrinting && selectionState() != RenderObject::SelectionNone;
56
57     PaintInfo childPaintInfo(paintInfo);
58     if (hasSelection) {
59         for (InlineBox* child = firstChild(); child; child = child->nextOnLine()) {
60             if (is<SVGInlineTextBox>(*child))
61                 downcast<SVGInlineTextBox>(*child).paintSelectionBackground(childPaintInfo);
62             else if (is<SVGInlineFlowBox>(*child))
63                 downcast<SVGInlineFlowBox>(*child).paintSelectionBackground(childPaintInfo);
64         }
65     }
66
67     SVGRenderingContext renderingContext(renderSVGText(), paintInfo, SVGRenderingContext::SaveGraphicsContext);
68     if (renderingContext.isRenderingPrepared()) {
69         for (InlineBox* child = firstChild(); child; child = child->nextOnLine())
70             child->paint(paintInfo, paintOffset, 0, 0);
71     }
72 }
73
74 void SVGRootInlineBox::computePerCharacterLayoutInformation()
75 {
76     auto& textRoot = downcast<RenderSVGText>(blockFlow());
77
78     Vector<SVGTextLayoutAttributes*>& layoutAttributes = textRoot.layoutAttributes();
79     if (layoutAttributes.isEmpty())
80         return;
81
82     if (textRoot.needsReordering())
83         reorderValueLists(layoutAttributes);
84
85     // Perform SVG text layout phase two (see SVGTextLayoutEngine for details).
86     SVGTextLayoutEngine characterLayout(layoutAttributes);
87     layoutCharactersInTextBoxes(this, characterLayout);
88
89     // Perform SVG text layout phase three (see SVGTextChunkBuilder for details).
90     characterLayout.finishLayout();
91
92     // Perform SVG text layout phase four
93     // Position & resize all SVGInlineText/FlowBoxes in the inline box tree, resize the root box as well as the RenderSVGText parent block.
94     FloatRect childRect;
95     layoutChildBoxes(this, &childRect);
96     layoutRootBox(childRect);
97 }
98
99 void SVGRootInlineBox::layoutCharactersInTextBoxes(InlineFlowBox* start, SVGTextLayoutEngine& characterLayout)
100 {
101     for (InlineBox* child = start->firstChild(); child; child = child->nextOnLine()) {
102         if (is<SVGInlineTextBox>(*child)) {
103             ASSERT(is<RenderSVGInlineText>(child->renderer()));
104             characterLayout.layoutInlineTextBox(downcast<SVGInlineTextBox>(*child));
105         } else {
106             // Skip generated content.
107             Node* node = child->renderer().node();
108             if (!node)
109                 continue;
110
111             auto& flowBox = downcast<SVGInlineFlowBox>(*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(downcast<RenderSVGTextPath>(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 (is<SVGInlineTextBox>(*child)) {
135             ASSERT(is<RenderSVGInlineText>(child->renderer()));
136
137             auto& textBox = downcast<SVGInlineTextBox>(*child);
138             boxRect = textBox.calculateBoundaries();
139             textBox.setX(boxRect.x());
140             textBox.setY(boxRect.y());
141             textBox.setLogicalWidth(boxRect.width());
142             textBox.setLogicalHeight(boxRect.height());
143         } else {
144             // Skip generated content.
145             if (!child->renderer().node())
146                 continue;
147
148             auto& flowBox = downcast<SVGInlineFlowBox>(*child);
149             layoutChildBoxes(&flowBox);
150
151             boxRect = flowBox.calculateBoundaries();
152             flowBox.setX(boxRect.x());
153             flowBox.setY(boxRect.y());
154             flowBox.setLogicalWidth(boxRect.width());
155             flowBox.setLogicalHeight(boxRect.height());
156         }
157         if (childRect)
158             childRect->unite(boxRect);
159     }
160 }
161
162 void SVGRootInlineBox::layoutRootBox(const FloatRect& childRect)
163 {
164     RenderSVGText& parentBlock = renderSVGText();
165
166     // Finally, assign the root block position, now that all content is laid out.
167     LayoutRect boundingRect = enclosingLayoutRect(childRect);
168     parentBlock.setLocation(boundingRect.location());
169     parentBlock.setSize(boundingRect.size());
170
171     // Position all children relative to the parent block.
172     for (InlineBox* child = firstChild(); child; child = child->nextOnLine()) {
173         // Skip generated content.
174         if (!child->renderer().node())
175             continue;
176         child->adjustPosition(-childRect.x(), -childRect.y());
177     }
178
179     // Position ourselves.
180     setX(0);
181     setY(0);
182     setLogicalWidth(childRect.width());
183     setLogicalHeight(childRect.height());
184     setLineTopBottomPositions(0, boundingRect.height(), 0, boundingRect.height());
185 }
186
187 InlineBox* SVGRootInlineBox::closestLeafChildForPosition(const LayoutPoint& point)
188 {
189     InlineBox* firstLeaf = firstLeafChild();
190     InlineBox* lastLeaf = lastLeafChild();
191     if (firstLeaf == lastLeaf)
192         return firstLeaf;
193
194     // FIXME: Check for vertical text!
195     InlineBox* closestLeaf = nullptr;
196     for (InlineBox* leaf = firstLeaf; leaf; leaf = leaf->nextLeafChild()) {
197         if (!leaf->isSVGInlineTextBox())
198             continue;
199         if (point.y() < leaf->y())
200             continue;
201         if (point.y() > leaf->y() + leaf->virtualLogicalHeight())
202             continue;
203
204         closestLeaf = leaf;
205         if (point.x() < leaf->left() + leaf->logicalWidth())
206             return leaf;
207     }
208
209     return closestLeaf ? closestLeaf : lastLeaf;
210 }
211
212 bool SVGRootInlineBox::nodeAtPoint(const HitTestRequest& request, HitTestResult& result, const HitTestLocation& locationInContainer, const LayoutPoint& accumulatedOffset, LayoutUnit lineTop, LayoutUnit lineBottom, HitTestAction hitTestAction)
213 {
214     for (InlineBox* leaf = firstLeafChild(); leaf; leaf = leaf->nextLeafChild()) {
215         if (!leaf->isSVGInlineTextBox())
216             continue;
217         if (leaf->nodeAtPoint(request, result, locationInContainer, accumulatedOffset, lineTop, lineBottom, hitTestAction))
218             return true;
219     }
220
221     return false;
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 = nullptr;
251     last = nullptr;
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 (!is<SVGInlineTextBox>(**last) || !is<SVGInlineTextBox>(**first)) {
279             InlineBox* temp = *first;
280             *first = *last;
281             *last = temp;
282             ++first;
283             continue;
284         }
285
286         auto& firstTextBox = downcast<SVGInlineTextBox>(**first);
287         auto& lastTextBox = downcast<SVGInlineTextBox>(**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 = nullptr;
295             SVGTextLayoutAttributes* lastAttributes = nullptr;
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