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"
28 #include "core/rendering/svg/RenderSVGInlineText.h"
29 #include "core/rendering/svg/RenderSVGText.h"
30 #include "core/rendering/svg/SVGInlineFlowBox.h"
31 #include "core/rendering/svg/SVGInlineTextBox.h"
32 #include "core/rendering/svg/SVGRenderingContext.h"
36 void SVGRootInlineBox::paint(PaintInfo& paintInfo, const LayoutPoint& paintOffset, LayoutUnit, LayoutUnit)
38 ASSERT(paintInfo.phase == PaintPhaseForeground || paintInfo.phase == PaintPhaseSelection);
39 ASSERT(!paintInfo.context->paintingDisabled());
41 RenderObject* boxRenderer = renderer();
44 bool isPrinting = renderer()->document().printing();
45 bool hasSelection = !isPrinting && selectionState() != RenderObject::SelectionNone;
47 PaintInfo childPaintInfo(paintInfo);
49 for (InlineBox* child = firstChild(); child; child = child->nextOnLine()) {
50 if (child->isSVGInlineTextBox())
51 toSVGInlineTextBox(child)->paintSelectionBackground(childPaintInfo);
52 else if (child->isSVGInlineFlowBox())
53 toSVGInlineFlowBox(child)->paintSelectionBackground(childPaintInfo);
57 SVGRenderingContext renderingContext(boxRenderer, paintInfo, SVGRenderingContext::SaveGraphicsContext);
58 if (renderingContext.isRenderingPrepared()) {
59 for (InlineBox* child = firstChild(); child; child = child->nextOnLine())
60 child->paint(paintInfo, paintOffset, 0, 0);
64 void SVGRootInlineBox::markDirty(bool dirty)
67 for (InlineBox* child = firstChild(); child; child = child->nextOnLine())
68 child->markDirty(true);
69 RootInlineBox::markDirty(dirty);
72 void SVGRootInlineBox::computePerCharacterLayoutInformation()
74 RenderSVGText* textRoot = toRenderSVGText(block());
77 Vector<SVGTextLayoutAttributes*>& layoutAttributes = textRoot->layoutAttributes();
78 if (layoutAttributes.isEmpty())
81 if (textRoot->needsReordering())
82 reorderValueLists(layoutAttributes);
84 // Perform SVG text layout phase two (see SVGTextLayoutEngine for details).
85 SVGTextLayoutEngine characterLayout(layoutAttributes);
86 layoutCharactersInTextBoxes(this, characterLayout);
88 // Perform SVG text layout phase three (see SVGTextChunkBuilder for details).
89 characterLayout.finishLayout();
91 // Perform SVG text layout phase four
92 // Position & resize all SVGInlineText/FlowBoxes in the inline box tree, resize the root box as well as the RenderSVGText parent block.
94 layoutChildBoxes(this, &childRect);
95 layoutRootBox(childRect);
98 void SVGRootInlineBox::layoutCharactersInTextBoxes(InlineFlowBox* start, SVGTextLayoutEngine& characterLayout)
100 for (InlineBox* child = start->firstChild(); child; child = child->nextOnLine()) {
101 if (child->isSVGInlineTextBox()) {
102 ASSERT(child->renderer());
103 ASSERT(child->renderer()->isSVGInlineText());
104 characterLayout.layoutInlineTextBox(toSVGInlineTextBox(child));
106 // Skip generated content.
107 Node* node = child->renderer()->node();
111 SVGInlineFlowBox* flowBox = toSVGInlineFlowBox(child);
112 bool isTextPath = node->hasTagName(SVGNames::textPathTag);
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);
119 characterLayout.beginTextPathLayout(child->renderer(), lineLayout);
122 layoutCharactersInTextBoxes(flowBox, characterLayout);
125 characterLayout.endTextPathLayout();
130 void SVGRootInlineBox::layoutChildBoxes(InlineFlowBox* start, FloatRect* childRect)
132 for (InlineBox* child = start->firstChild(); child; child = child->nextOnLine()) {
134 if (child->isSVGInlineTextBox()) {
135 ASSERT(child->renderer());
136 ASSERT(child->renderer()->isSVGInlineText());
138 SVGInlineTextBox* textBox = toSVGInlineTextBox(child);
139 boxRect = textBox->calculateBoundaries();
140 textBox->setX(boxRect.x());
141 textBox->setY(boxRect.y());
142 textBox->setLogicalWidth(boxRect.width());
143 textBox->setLogicalHeight(boxRect.height());
145 // Skip generated content.
146 if (!child->renderer()->node())
149 SVGInlineFlowBox* flowBox = toSVGInlineFlowBox(child);
150 layoutChildBoxes(flowBox);
152 boxRect = flowBox->calculateBoundaries();
153 flowBox->setX(boxRect.x());
154 flowBox->setY(boxRect.y());
155 flowBox->setLogicalWidth(boxRect.width());
156 flowBox->setLogicalHeight(boxRect.height());
159 childRect->unite(boxRect);
163 void SVGRootInlineBox::layoutRootBox(const FloatRect& childRect)
165 RenderBlockFlow* parentBlock = block();
168 // Finally, assign the root block position, now that all content is laid out.
169 LayoutRect boundingRect = enclosingLayoutRect(childRect);
170 parentBlock->setLocation(boundingRect.location());
171 parentBlock->setSize(boundingRect.size());
173 // Position all children relative to the parent block.
174 for (InlineBox* child = firstChild(); child; child = child->nextOnLine()) {
175 // Skip generated content.
176 if (!child->renderer()->node())
178 child->adjustPosition(-childRect.x(), -childRect.y());
181 // Position ourselves.
184 setLogicalWidth(childRect.width());
185 setLogicalHeight(childRect.height());
186 setLineTopBottomPositions(0, boundingRect.height(), 0, boundingRect.height());
189 InlineBox* SVGRootInlineBox::closestLeafChildForPosition(const LayoutPoint& point)
191 InlineBox* firstLeaf = firstLeafChild();
192 InlineBox* lastLeaf = lastLeafChild();
193 if (firstLeaf == lastLeaf)
196 // FIXME: Check for vertical text!
197 InlineBox* closestLeaf = 0;
198 for (InlineBox* leaf = firstLeaf; leaf; leaf = leaf->nextLeafChild()) {
199 if (!leaf->isSVGInlineTextBox())
201 if (point.y() < leaf->y())
203 if (point.y() > leaf->y() + leaf->virtualLogicalHeight())
207 if (point.x() < leaf->left() + leaf->logicalWidth())
211 return closestLeaf ? closestLeaf : lastLeaf;
214 static inline void swapItemsInLayoutAttributes(SVGTextLayoutAttributes* firstAttributes, SVGTextLayoutAttributes* lastAttributes, unsigned firstPosition, unsigned lastPosition)
216 SVGCharacterDataMap::iterator itFirst = firstAttributes->characterDataMap().find(firstPosition + 1);
217 SVGCharacterDataMap::iterator itLast = lastAttributes->characterDataMap().find(lastPosition + 1);
218 bool firstPresent = itFirst != firstAttributes->characterDataMap().end();
219 bool lastPresent = itLast != lastAttributes->characterDataMap().end();
220 if (!firstPresent && !lastPresent)
223 if (firstPresent && lastPresent) {
224 std::swap(itFirst->value, itLast->value);
228 if (firstPresent && !lastPresent) {
229 lastAttributes->characterDataMap().set(lastPosition + 1, itFirst->value);
233 // !firstPresent && lastPresent
234 firstAttributes->characterDataMap().set(firstPosition + 1, itLast->value);
237 static inline void findFirstAndLastAttributesInVector(Vector<SVGTextLayoutAttributes*>& attributes, RenderSVGInlineText* firstContext, RenderSVGInlineText* lastContext,
238 SVGTextLayoutAttributes*& first, SVGTextLayoutAttributes*& last)
243 unsigned attributesSize = attributes.size();
244 for (unsigned i = 0; i < attributesSize; ++i) {
245 SVGTextLayoutAttributes* current = attributes[i];
246 if (!first && firstContext == current->context())
248 if (!last && lastContext == current->context())
258 static inline void reverseInlineBoxRangeAndValueListsIfNeeded(void* userData, Vector<InlineBox*>::iterator first, Vector<InlineBox*>::iterator last)
261 Vector<SVGTextLayoutAttributes*>& attributes = *reinterpret_cast<Vector<SVGTextLayoutAttributes*>*>(userData);
263 // 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.
265 if (first == last || first == --last)
268 if (!(*last)->isSVGInlineTextBox() || !(*first)->isSVGInlineTextBox()) {
269 InlineBox* temp = *first;
276 SVGInlineTextBox* firstTextBox = toSVGInlineTextBox(*first);
277 SVGInlineTextBox* lastTextBox = toSVGInlineTextBox(*last);
279 // Reordering is only necessary for BiDi text that is _absolutely_ positioned.
280 if (firstTextBox->len() == 1 && firstTextBox->len() == lastTextBox->len()) {
281 RenderSVGInlineText* firstContext = toRenderSVGInlineText(firstTextBox->textRenderer());
282 RenderSVGInlineText* lastContext = toRenderSVGInlineText(lastTextBox->textRenderer());
284 SVGTextLayoutAttributes* firstAttributes = 0;
285 SVGTextLayoutAttributes* lastAttributes = 0;
286 findFirstAndLastAttributesInVector(attributes, firstContext, lastContext, firstAttributes, lastAttributes);
287 swapItemsInLayoutAttributes(firstAttributes, lastAttributes, firstTextBox->start(), lastTextBox->start());
290 InlineBox* temp = *first;
298 void SVGRootInlineBox::reorderValueLists(Vector<SVGTextLayoutAttributes*>& attributes)
300 Vector<InlineBox*> leafBoxesInLogicalOrder;
301 collectLeafBoxesInLogicalOrder(leafBoxesInLogicalOrder, reverseInlineBoxRangeAndValueListsIfNeeded, &attributes);
304 } // namespace WebCore