04d584dee8fe78bd1a6dc87000b93f985fcd8ab8
[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             if (is<SVGInlineTextBox>(*child))
71                 SVGInlineFlowBox::computeTextMatchMarkerRectForRenderer(&downcast<SVGInlineTextBox>(*child).renderer());
72
73             child->paint(paintInfo, paintOffset, 0, 0);
74         }
75     }
76 }
77
78 void SVGRootInlineBox::computePerCharacterLayoutInformation()
79 {
80     auto& textRoot = downcast<RenderSVGText>(blockFlow());
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 (is<SVGInlineTextBox>(*child)) {
107             ASSERT(is<RenderSVGInlineText>(child->renderer()));
108             characterLayout.layoutInlineTextBox(downcast<SVGInlineTextBox>(child));
109         } else {
110             // Skip generated content.
111             Node* node = child->renderer().node();
112             if (!node)
113                 continue;
114
115             auto& flowBox = downcast<SVGInlineFlowBox>(*child);
116             bool isTextPath = node->hasTagName(SVGNames::textPathTag);
117             if (isTextPath) {
118                 // Build text chunks for all <textPath> children, using the line layout algorithm.
119                 // This is needeed as text-anchor is just an additional startOffset for text paths.
120                 SVGTextLayoutEngine lineLayout(characterLayout.layoutAttributes());
121                 layoutCharactersInTextBoxes(&flowBox, lineLayout);
122
123                 characterLayout.beginTextPathLayout(downcast<RenderSVGTextPath>(child->renderer()), lineLayout);
124             }
125
126             layoutCharactersInTextBoxes(&flowBox, characterLayout);
127
128             if (isTextPath)
129                 characterLayout.endTextPathLayout();
130         }
131     }
132 }
133
134 void SVGRootInlineBox::layoutChildBoxes(InlineFlowBox* start, FloatRect* childRect)
135 {
136     for (InlineBox* child = start->firstChild(); child; child = child->nextOnLine()) {
137         FloatRect boxRect;
138         if (is<SVGInlineTextBox>(*child)) {
139             ASSERT(is<RenderSVGInlineText>(child->renderer()));
140
141             auto& textBox = downcast<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             auto& flowBox = downcast<SVGInlineFlowBox>(*child);
153             layoutChildBoxes(&flowBox);
154
155             boxRect = flowBox.calculateBoundaries();
156             flowBox.setX(boxRect.x());
157             flowBox.setY(boxRect.y());
158             flowBox.setLogicalWidth(boxRect.width());
159             flowBox.setLogicalHeight(boxRect.height());
160         }
161         if (childRect)
162             childRect->unite(boxRect);
163     }
164 }
165
166 void SVGRootInlineBox::layoutRootBox(const FloatRect& childRect)
167 {
168     RenderSVGText& parentBlock = renderSVGText();
169
170     // Finally, assign the root block position, now that all content is laid out.
171     LayoutRect boundingRect = enclosingLayoutRect(childRect);
172     parentBlock.setLocation(boundingRect.location());
173     parentBlock.setSize(boundingRect.size());
174
175     // Position all children relative to the parent block.
176     for (InlineBox* child = firstChild(); child; child = child->nextOnLine()) {
177         // Skip generated content.
178         if (!child->renderer().node())
179             continue;
180         child->adjustPosition(-childRect.x(), -childRect.y());
181     }
182
183     // Position ourselves.
184     setX(0);
185     setY(0);
186     setLogicalWidth(childRect.width());
187     setLogicalHeight(childRect.height());
188     setLineTopBottomPositions(0, boundingRect.height(), 0, boundingRect.height());
189 }
190
191 InlineBox* SVGRootInlineBox::closestLeafChildForPosition(const LayoutPoint& point)
192 {
193     InlineBox* firstLeaf = firstLeafChild();
194     InlineBox* lastLeaf = lastLeafChild();
195     if (firstLeaf == lastLeaf)
196         return firstLeaf;
197
198     // FIXME: Check for vertical text!
199     InlineBox* closestLeaf = nullptr;
200     for (InlineBox* leaf = firstLeaf; leaf; leaf = leaf->nextLeafChild()) {
201         if (!leaf->isSVGInlineTextBox())
202             continue;
203         if (point.y() < leaf->y())
204             continue;
205         if (point.y() > leaf->y() + leaf->virtualLogicalHeight())
206             continue;
207
208         closestLeaf = leaf;
209         if (point.x() < leaf->left() + leaf->logicalWidth())
210             return leaf;
211     }
212
213     return closestLeaf ? closestLeaf : lastLeaf;
214 }
215
216 static inline void swapItemsInLayoutAttributes(SVGTextLayoutAttributes* firstAttributes, SVGTextLayoutAttributes* lastAttributes, unsigned firstPosition, unsigned lastPosition)
217 {
218     SVGCharacterDataMap::iterator itFirst = firstAttributes->characterDataMap().find(firstPosition + 1);
219     SVGCharacterDataMap::iterator itLast = lastAttributes->characterDataMap().find(lastPosition + 1);
220     bool firstPresent = itFirst != firstAttributes->characterDataMap().end();
221     bool lastPresent = itLast != lastAttributes->characterDataMap().end();
222     if (!firstPresent && !lastPresent)
223         return;
224
225     if (firstPresent && lastPresent) {
226         std::swap(itFirst->value, itLast->value);
227         return;
228     }
229
230     if (firstPresent && !lastPresent) {
231         lastAttributes->characterDataMap().set(lastPosition + 1, itFirst->value);
232         return;
233     }
234
235     // !firstPresent && lastPresent
236     firstAttributes->characterDataMap().set(firstPosition + 1, itLast->value);
237 }
238
239 static inline void findFirstAndLastAttributesInVector(Vector<SVGTextLayoutAttributes*>& attributes, RenderSVGInlineText* firstContext, RenderSVGInlineText* lastContext,
240                                                       SVGTextLayoutAttributes*& first, SVGTextLayoutAttributes*& last)
241 {
242     first = nullptr;
243     last = nullptr;
244
245     unsigned attributesSize = attributes.size();
246     for (unsigned i = 0; i < attributesSize; ++i) {
247         SVGTextLayoutAttributes* current = attributes[i];
248         if (!first && firstContext == &current->context())
249             first = current;
250         if (!last && lastContext == &current->context())
251             last = current;
252         if (first && last)
253             break;
254     }
255
256     ASSERT(first);
257     ASSERT(last);
258 }
259
260 static inline void reverseInlineBoxRangeAndValueListsIfNeeded(void* userData, Vector<InlineBox*>::iterator first, Vector<InlineBox*>::iterator last)
261 {
262     ASSERT(userData);
263     Vector<SVGTextLayoutAttributes*>& attributes = *reinterpret_cast<Vector<SVGTextLayoutAttributes*>*>(userData);
264
265     // 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.
266     while (true)  {
267         if (first == last || first == --last)
268             return;
269
270         if (!is<SVGInlineTextBox>(**last) || !is<SVGInlineTextBox>(**first)) {
271             InlineBox* temp = *first;
272             *first = *last;
273             *last = temp;
274             ++first;
275             continue;
276         }
277
278         auto& firstTextBox = downcast<SVGInlineTextBox>(**first);
279         auto& lastTextBox = downcast<SVGInlineTextBox>(**last);
280
281         // Reordering is only necessary for BiDi text that is _absolutely_ positioned.
282         if (firstTextBox.len() == 1 && firstTextBox.len() == lastTextBox.len()) {
283             RenderSVGInlineText& firstContext = firstTextBox.renderer();
284             RenderSVGInlineText& lastContext = lastTextBox.renderer();
285
286             SVGTextLayoutAttributes* firstAttributes = nullptr;
287             SVGTextLayoutAttributes* lastAttributes = nullptr;
288             findFirstAndLastAttributesInVector(attributes, &firstContext, &lastContext, firstAttributes, lastAttributes);
289             swapItemsInLayoutAttributes(firstAttributes, lastAttributes, firstTextBox.start(), lastTextBox.start());
290         }
291
292         InlineBox* temp = *first;
293         *first = *last;
294         *last = temp;
295
296         ++first;
297     }
298 }
299
300 void SVGRootInlineBox::reorderValueLists(Vector<SVGTextLayoutAttributes*>& attributes)
301 {
302     Vector<InlineBox*> leafBoxesInLogicalOrder;
303     collectLeafBoxesInLogicalOrder(leafBoxesInLogicalOrder, reverseInlineBoxRangeAndValueListsIfNeeded, &attributes);
304 }
305
306 } // namespace WebCore