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.
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.
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.
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.
25 #include "core/rendering/svg/SVGRootInlineBox.h"
27 #include "core/rendering/svg/RenderSVGInlineText.h"
28 #include "core/rendering/svg/RenderSVGText.h"
29 #include "core/rendering/svg/SVGInlineFlowBox.h"
30 #include "core/rendering/svg/SVGInlineTextBox.h"
31 #include "core/rendering/svg/SVGRenderingContext.h"
35 void SVGRootInlineBox::paint(PaintInfo& paintInfo, const LayoutPoint& paintOffset, LayoutUnit, LayoutUnit)
37 ASSERT(paintInfo.phase == PaintPhaseForeground || paintInfo.phase == PaintPhaseSelection);
39 bool isPrinting = renderer().document().printing();
40 bool hasSelection = !isPrinting && selectionState() != RenderObject::SelectionNone;
42 PaintInfo childPaintInfo(paintInfo);
44 for (InlineBox* child = firstChild(); child; child = child->nextOnLine()) {
45 if (child->isSVGInlineTextBox())
46 toSVGInlineTextBox(child)->paintSelectionBackground(childPaintInfo);
47 else if (child->isSVGInlineFlowBox())
48 toSVGInlineFlowBox(child)->paintSelectionBackground(childPaintInfo);
52 GraphicsContextStateSaver stateSaver(*paintInfo.context);
53 SVGRenderingContext renderingContext(&renderer(), paintInfo);
54 if (renderingContext.isRenderingPrepared()) {
55 for (InlineBox* child = firstChild(); child; child = child->nextOnLine())
56 child->paint(paintInfo, paintOffset, 0, 0);
60 void SVGRootInlineBox::markDirty()
62 for (InlineBox* child = firstChild(); child; child = child->nextOnLine())
64 RootInlineBox::markDirty();
67 void SVGRootInlineBox::computePerCharacterLayoutInformation()
69 RenderSVGText& textRoot = toRenderSVGText(block());
71 Vector<SVGTextLayoutAttributes*>& layoutAttributes = textRoot.layoutAttributes();
72 if (layoutAttributes.isEmpty())
75 if (textRoot.needsReordering())
76 reorderValueLists(layoutAttributes);
78 // Perform SVG text layout phase two (see SVGTextLayoutEngine for details).
79 SVGTextLayoutEngine characterLayout(layoutAttributes);
80 layoutCharactersInTextBoxes(this, characterLayout);
82 // Perform SVG text layout phase three (see SVGTextChunkBuilder for details).
83 characterLayout.finishLayout();
85 // Perform SVG text layout phase four
86 // Position & resize all SVGInlineText/FlowBoxes in the inline box tree, resize the root box as well as the RenderSVGText parent block.
88 layoutChildBoxes(this, &childRect);
89 layoutRootBox(childRect);
92 void SVGRootInlineBox::layoutCharactersInTextBoxes(InlineFlowBox* start, SVGTextLayoutEngine& characterLayout)
94 for (InlineBox* child = start->firstChild(); child; child = child->nextOnLine()) {
95 if (child->isSVGInlineTextBox()) {
96 ASSERT(child->renderer().isSVGInlineText());
97 characterLayout.layoutInlineTextBox(toSVGInlineTextBox(child));
99 // Skip generated content.
100 Node* node = child->renderer().node();
104 SVGInlineFlowBox* flowBox = toSVGInlineFlowBox(child);
105 bool isTextPath = isSVGTextPathElement(*node);
107 // Build text chunks for all <textPath> children, using the line layout algorithm.
108 // This is needeed as text-anchor is just an additional startOffset for text paths.
109 SVGTextLayoutEngine lineLayout(characterLayout.layoutAttributes());
110 layoutCharactersInTextBoxes(flowBox, lineLayout);
112 characterLayout.beginTextPathLayout(&child->renderer(), lineLayout);
115 layoutCharactersInTextBoxes(flowBox, characterLayout);
118 characterLayout.endTextPathLayout();
123 void SVGRootInlineBox::layoutChildBoxes(InlineFlowBox* start, FloatRect* childRect)
125 for (InlineBox* child = start->firstChild(); child; child = child->nextOnLine()) {
127 if (child->isSVGInlineTextBox()) {
128 ASSERT(child->renderer().isSVGInlineText());
130 SVGInlineTextBox* textBox = toSVGInlineTextBox(child);
131 boxRect = textBox->calculateBoundaries();
132 textBox->setX(boxRect.x());
133 textBox->setY(boxRect.y());
134 textBox->setLogicalWidth(boxRect.width());
135 textBox->setLogicalHeight(boxRect.height());
137 // Skip generated content.
138 if (!child->renderer().node())
141 SVGInlineFlowBox* flowBox = toSVGInlineFlowBox(child);
142 layoutChildBoxes(flowBox);
144 boxRect = flowBox->calculateBoundaries();
145 flowBox->setX(boxRect.x());
146 flowBox->setY(boxRect.y());
147 flowBox->setLogicalWidth(boxRect.width());
148 flowBox->setLogicalHeight(boxRect.height());
151 childRect->unite(boxRect);
155 void SVGRootInlineBox::layoutRootBox(const FloatRect& childRect)
157 RenderBlockFlow& parentBlock = block();
159 // Finally, assign the root block position, now that all content is laid out.
160 LayoutRect boundingRect = enclosingLayoutRect(childRect);
161 parentBlock.setLocation(boundingRect.location());
162 parentBlock.setSize(boundingRect.size());
164 // Position all children relative to the parent block.
165 for (InlineBox* child = firstChild(); child; child = child->nextOnLine()) {
166 // Skip generated content.
167 if (!child->renderer().node())
169 child->adjustPosition(-childRect.x(), -childRect.y());
172 // Position ourselves.
175 setLogicalWidth(childRect.width());
176 setLogicalHeight(childRect.height());
177 setLineTopBottomPositions(0, boundingRect.height(), 0, boundingRect.height());
180 InlineBox* SVGRootInlineBox::closestLeafChildForPosition(const LayoutPoint& point)
182 InlineBox* firstLeaf = firstLeafChild();
183 InlineBox* lastLeaf = lastLeafChild();
184 if (firstLeaf == lastLeaf)
187 // FIXME: Check for vertical text!
188 InlineBox* closestLeaf = 0;
189 for (InlineBox* leaf = firstLeaf; leaf; leaf = leaf->nextLeafChild()) {
190 if (!leaf->isSVGInlineTextBox())
192 if (point.y() < leaf->y())
194 if (point.y() > leaf->y() + leaf->virtualLogicalHeight())
198 if (point.x() < leaf->left() + leaf->logicalWidth())
202 return closestLeaf ? closestLeaf : lastLeaf;
205 static inline void swapItemsInLayoutAttributes(SVGTextLayoutAttributes* firstAttributes, SVGTextLayoutAttributes* lastAttributes, unsigned firstPosition, unsigned lastPosition)
207 SVGCharacterDataMap::iterator itFirst = firstAttributes->characterDataMap().find(firstPosition + 1);
208 SVGCharacterDataMap::iterator itLast = lastAttributes->characterDataMap().find(lastPosition + 1);
209 bool firstPresent = itFirst != firstAttributes->characterDataMap().end();
210 bool lastPresent = itLast != lastAttributes->characterDataMap().end();
211 // We only want to perform the swap if both inline boxes are absolutely
213 if (!firstPresent || !lastPresent)
215 std::swap(itFirst->value, itLast->value);
218 static inline void findFirstAndLastAttributesInVector(Vector<SVGTextLayoutAttributes*>& attributes, RenderSVGInlineText* firstContext, RenderSVGInlineText* lastContext,
219 SVGTextLayoutAttributes*& first, SVGTextLayoutAttributes*& last)
224 unsigned attributesSize = attributes.size();
225 for (unsigned i = 0; i < attributesSize; ++i) {
226 SVGTextLayoutAttributes* current = attributes[i];
227 if (!first && firstContext == current->context())
229 if (!last && lastContext == current->context())
239 static inline void reverseInlineBoxRangeAndValueListsIfNeeded(void* userData, Vector<InlineBox*>::iterator first, Vector<InlineBox*>::iterator last)
242 Vector<SVGTextLayoutAttributes*>& attributes = *reinterpret_cast<Vector<SVGTextLayoutAttributes*>*>(userData);
244 // 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.
246 if (first == last || first == --last)
249 if (!(*last)->isSVGInlineTextBox() || !(*first)->isSVGInlineTextBox()) {
250 InlineBox* temp = *first;
257 SVGInlineTextBox* firstTextBox = toSVGInlineTextBox(*first);
258 SVGInlineTextBox* lastTextBox = toSVGInlineTextBox(*last);
260 // Reordering is only necessary for BiDi text that is _absolutely_ positioned.
261 if (firstTextBox->len() == 1 && firstTextBox->len() == lastTextBox->len()) {
262 RenderSVGInlineText& firstContext = toRenderSVGInlineText(firstTextBox->renderer());
263 RenderSVGInlineText& lastContext = toRenderSVGInlineText(lastTextBox->renderer());
265 SVGTextLayoutAttributes* firstAttributes = 0;
266 SVGTextLayoutAttributes* lastAttributes = 0;
267 findFirstAndLastAttributesInVector(attributes, &firstContext, &lastContext, firstAttributes, lastAttributes);
268 swapItemsInLayoutAttributes(firstAttributes, lastAttributes, firstTextBox->start(), lastTextBox->start());
271 InlineBox* temp = *first;
279 void SVGRootInlineBox::reorderValueLists(Vector<SVGTextLayoutAttributes*>& attributes)
281 Vector<InlineBox*> leafBoxesInLogicalOrder;
282 collectLeafBoxesInLogicalOrder(leafBoxesInLogicalOrder, reverseInlineBoxRangeAndValueListsIfNeeded, &attributes);