2 * Copyright (C) 2006 Apple Computer, Inc.
3 * Copyright (C) 2006 Alexander Kellett <lypanov@kde.org>
4 * Copyright (C) 2006 Oliver Hunt <ojh16@student.canterbury.ac.nz>
5 * Copyright (C) 2007 Nikolas Zimmermann <zimmermann@kde.org>
6 * Copyright (C) 2008 Rob Buis <buis@kde.org>
7 * Copyright (C) 2009 Dirk Schulze <krit@webkit.org>
8 * Copyright (C) Research In Motion Limited 2010-2012. All rights reserved.
9 * Copyright (C) 2012 Google Inc.
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Library General Public
13 * License as published by the Free Software Foundation; either
14 * version 2 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Library General Public License for more details.
21 * You should have received a copy of the GNU Library General Public License
22 * along with this library; see the file COPYING.LIB. If not, write to
23 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 * Boston, MA 02110-1301, USA.
29 #include "core/rendering/svg/RenderSVGText.h"
31 #include "core/rendering/HitTestRequest.h"
32 #include "core/rendering/HitTestResult.h"
33 #include "core/rendering/LayoutRepainter.h"
34 #include "core/rendering/PointerEventsHitRules.h"
35 #include "core/rendering/style/ShadowList.h"
36 #include "core/rendering/svg/RenderSVGInlineText.h"
37 #include "core/rendering/svg/RenderSVGResource.h"
38 #include "core/rendering/svg/RenderSVGRoot.h"
39 #include "core/rendering/svg/SVGRenderSupport.h"
40 #include "core/rendering/svg/SVGResourcesCache.h"
41 #include "core/rendering/svg/SVGRootInlineBox.h"
42 #include "core/rendering/svg/SVGTextRunRenderingContext.h"
43 #include "core/svg/SVGLengthList.h"
44 #include "core/svg/SVGTextElement.h"
45 #include "core/svg/SVGTransformList.h"
46 #include "core/svg/SVGURIReference.h"
47 #include "platform/FloatConversion.h"
48 #include "platform/fonts/FontCache.h"
49 #include "platform/fonts/SimpleFontData.h"
50 #include "platform/geometry/FloatQuad.h"
51 #include "platform/geometry/TransformState.h"
52 #include "platform/graphics/GraphicsContextStateSaver.h"
56 RenderSVGText::RenderSVGText(SVGTextElement* node)
57 : RenderSVGBlock(node)
58 , m_needsReordering(false)
59 , m_needsPositioningValuesUpdate(false)
60 , m_needsTransformUpdate(true)
61 , m_needsTextMetricsUpdate(false)
65 RenderSVGText::~RenderSVGText()
67 ASSERT(m_layoutAttributes.isEmpty());
70 bool RenderSVGText::isChildAllowed(RenderObject* child, RenderStyle*) const
72 return child->isSVGInline() || (child->isText() && SVGRenderSupport::isRenderableTextNode(child));
75 RenderSVGText* RenderSVGText::locateRenderSVGTextAncestor(RenderObject* start)
78 while (start && !start->isSVGText())
79 start = start->parent();
80 if (!start || !start->isSVGText())
82 return toRenderSVGText(start);
85 const RenderSVGText* RenderSVGText::locateRenderSVGTextAncestor(const RenderObject* start)
88 while (start && !start->isSVGText())
89 start = start->parent();
90 if (!start || !start->isSVGText())
92 return toRenderSVGText(start);
95 void RenderSVGText::computeRectForRepaint(const RenderLayerModelObject* repaintContainer, LayoutRect& rect, bool fixed) const
97 FloatRect repaintRect = rect;
98 computeFloatRectForRepaint(repaintContainer, repaintRect, fixed);
99 rect = enclosingLayoutRect(repaintRect);
102 static inline void collectLayoutAttributes(RenderObject* text, Vector<SVGTextLayoutAttributes*>& attributes)
104 for (RenderObject* descendant = text; descendant; descendant = descendant->nextInPreOrder(text)) {
105 if (descendant->isSVGInlineText())
106 attributes.append(toRenderSVGInlineText(descendant)->layoutAttributes());
110 static inline bool findPreviousAndNextAttributes(RenderObject* root, RenderSVGInlineText* locateElement, SVGTextLayoutAttributes*& previous, SVGTextLayoutAttributes*& next)
113 ASSERT(locateElement);
114 bool stopAfterNext = false;
115 RenderObject* current = root->firstChild();
117 if (current->isSVGInlineText()) {
118 RenderSVGInlineText* text = toRenderSVGInlineText(current);
119 if (locateElement != text) {
121 next = text->layoutAttributes();
125 previous = text->layoutAttributes();
127 stopAfterNext = true;
129 } else if (current->isSVGInline()) {
130 // Descend into text content (if possible).
131 if (RenderObject* child = current->firstChild()) {
137 current = current->nextInPreOrderAfterChildren(root);
142 inline bool RenderSVGText::shouldHandleSubtreeMutations() const
144 if (beingDestroyed() || !everHadLayout()) {
145 ASSERT(m_layoutAttributes.isEmpty());
146 ASSERT(!m_layoutAttributesBuilder.numberOfTextPositioningElements());
152 void RenderSVGText::subtreeChildWasAdded(RenderObject* child)
155 if (!shouldHandleSubtreeMutations() || documentBeingDestroyed())
158 // Always protect the cache before clearing text positioning elements when the cache will subsequently be rebuilt.
159 FontCachePurgePreventer fontCachePurgePreventer;
161 // The positioning elements cache doesn't include the new 'child' yet. Clear the
162 // cache, as the next buildLayoutAttributesForTextRenderer() call rebuilds it.
163 m_layoutAttributesBuilder.clearTextPositioningElements();
165 if (!child->isSVGInlineText() && !child->isSVGInline())
168 // Detect changes in layout attributes and only measure those text parts that have changed!
169 Vector<SVGTextLayoutAttributes*> newLayoutAttributes;
170 collectLayoutAttributes(this, newLayoutAttributes);
171 if (newLayoutAttributes.isEmpty()) {
172 ASSERT(m_layoutAttributes.isEmpty());
176 // Compare m_layoutAttributes with newLayoutAttributes to figure out which attribute got added.
177 size_t size = newLayoutAttributes.size();
178 SVGTextLayoutAttributes* attributes = 0;
179 for (size_t i = 0; i < size; ++i) {
180 attributes = newLayoutAttributes[i];
181 if (m_layoutAttributes.find(attributes) == kNotFound) {
182 // Every time this is invoked, there's only a single new entry in the newLayoutAttributes list, compared to the old in m_layoutAttributes.
183 SVGTextLayoutAttributes* previous = 0;
184 SVGTextLayoutAttributes* next = 0;
185 ASSERT_UNUSED(child, attributes->context() == child);
186 findPreviousAndNextAttributes(this, attributes->context(), previous, next);
189 m_layoutAttributesBuilder.buildLayoutAttributesForTextRenderer(previous->context());
190 m_layoutAttributesBuilder.buildLayoutAttributesForTextRenderer(attributes->context());
192 m_layoutAttributesBuilder.buildLayoutAttributesForTextRenderer(next->context());
198 // Verify that m_layoutAttributes only differs by a maximum of one entry.
199 for (size_t i = 0; i < size; ++i)
200 ASSERT(m_layoutAttributes.find(newLayoutAttributes[i]) != kNotFound || newLayoutAttributes[i] == attributes);
203 m_layoutAttributes = newLayoutAttributes;
206 static inline void checkLayoutAttributesConsistency(RenderSVGText* text, Vector<SVGTextLayoutAttributes*>& expectedLayoutAttributes)
209 Vector<SVGTextLayoutAttributes*> newLayoutAttributes;
210 collectLayoutAttributes(text, newLayoutAttributes);
211 ASSERT(newLayoutAttributes == expectedLayoutAttributes);
215 void RenderSVGText::willBeDestroyed()
217 m_layoutAttributes.clear();
218 m_layoutAttributesBuilder.clearTextPositioningElements();
220 RenderSVGBlock::willBeDestroyed();
223 void RenderSVGText::subtreeChildWillBeRemoved(RenderObject* child, Vector<SVGTextLayoutAttributes*, 2>& affectedAttributes)
226 if (!shouldHandleSubtreeMutations())
229 checkLayoutAttributesConsistency(this, m_layoutAttributes);
231 // The positioning elements cache depends on the size of each text renderer in the
232 // subtree. If this changes, clear the cache. It's going to be rebuilt below.
233 m_layoutAttributesBuilder.clearTextPositioningElements();
234 if (m_layoutAttributes.isEmpty() || !child->isSVGInlineText())
237 // This logic requires that the 'text' child is still inserted in the tree.
238 RenderSVGInlineText* text = toRenderSVGInlineText(child);
239 SVGTextLayoutAttributes* previous = 0;
240 SVGTextLayoutAttributes* next = 0;
241 if (!documentBeingDestroyed())
242 findPreviousAndNextAttributes(this, text, previous, next);
245 affectedAttributes.append(previous);
247 affectedAttributes.append(next);
249 size_t position = m_layoutAttributes.find(text->layoutAttributes());
250 ASSERT(position != kNotFound);
251 m_layoutAttributes.remove(position);
254 void RenderSVGText::subtreeChildWasRemoved(const Vector<SVGTextLayoutAttributes*, 2>& affectedAttributes)
256 if (!shouldHandleSubtreeMutations() || documentBeingDestroyed()) {
257 ASSERT(affectedAttributes.isEmpty());
261 // This is called immediately after subtreeChildWillBeDestroyed, once the RenderSVGInlineText::willBeDestroyed() method
262 // passes on to the base class, which removes us from the render tree. At this point we can update the layout attributes.
263 unsigned size = affectedAttributes.size();
264 for (unsigned i = 0; i < size; ++i)
265 m_layoutAttributesBuilder.buildLayoutAttributesForTextRenderer(affectedAttributes[i]->context());
268 void RenderSVGText::subtreeStyleDidChange()
270 if (!shouldHandleSubtreeMutations() || documentBeingDestroyed())
273 checkLayoutAttributesConsistency(this, m_layoutAttributes);
275 // Only update the metrics cache, but not the text positioning element cache
276 // nor the layout attributes cached in the leaf #text renderers.
277 FontCachePurgePreventer fontCachePurgePreventer;
278 for (RenderObject* descendant = firstChild(); descendant; descendant = descendant->nextInPreOrder(this)) {
279 if (descendant->isSVGInlineText())
280 m_layoutAttributesBuilder.rebuildMetricsForTextRenderer(toRenderSVGInlineText(descendant));
284 void RenderSVGText::subtreeTextDidChange(RenderSVGInlineText* text)
287 ASSERT(!beingDestroyed());
288 if (!everHadLayout()) {
289 ASSERT(m_layoutAttributes.isEmpty());
290 ASSERT(!m_layoutAttributesBuilder.numberOfTextPositioningElements());
294 // Always protect the cache before clearing text positioning elements when the cache will subsequently be rebuilt.
295 FontCachePurgePreventer fontCachePurgePreventer;
297 // The positioning elements cache depends on the size of each text renderer in the
298 // subtree. If this changes, clear the cache. It's going to be rebuilt below.
299 m_layoutAttributesBuilder.clearTextPositioningElements();
301 checkLayoutAttributesConsistency(this, m_layoutAttributes);
302 for (RenderObject* descendant = text; descendant; descendant = descendant->nextInPreOrder(text)) {
303 if (descendant->isSVGInlineText())
304 m_layoutAttributesBuilder.buildLayoutAttributesForTextRenderer(toRenderSVGInlineText(descendant));
308 static inline void updateFontInAllDescendants(RenderObject* start, SVGTextLayoutAttributesBuilder* builder = 0)
310 for (RenderObject* descendant = start; descendant; descendant = descendant->nextInPreOrder(start)) {
311 if (!descendant->isSVGInlineText())
313 RenderSVGInlineText* text = toRenderSVGInlineText(descendant);
314 text->updateScaledFont();
316 builder->rebuildMetricsForTextRenderer(text);
320 void RenderSVGText::layout()
322 ASSERT(needsLayout());
324 subtreeStyleDidChange();
326 LayoutRepainter repainter(*this, SVGRenderSupport::checkForSVGRepaintDuringLayout(this));
328 bool updateCachedBoundariesInParents = false;
329 if (m_needsTransformUpdate) {
330 m_localTransform = toSVGTextElement(node())->animatedLocalTransform();
331 m_needsTransformUpdate = false;
332 updateCachedBoundariesInParents = true;
335 if (!everHadLayout()) {
336 // When laying out initially, collect all layout attributes, build the character data map,
337 // and propogate resulting SVGLayoutAttributes to all RenderSVGInlineText children in the subtree.
338 ASSERT(m_layoutAttributes.isEmpty());
339 collectLayoutAttributes(this, m_layoutAttributes);
340 updateFontInAllDescendants(this);
341 m_layoutAttributesBuilder.buildLayoutAttributesForForSubtree(this);
343 m_needsReordering = true;
344 m_needsTextMetricsUpdate = false;
345 m_needsPositioningValuesUpdate = false;
346 updateCachedBoundariesInParents = true;
347 } else if (m_needsPositioningValuesUpdate) {
348 // When the x/y/dx/dy/rotate lists change, recompute the layout attributes, and eventually
349 // update the on-screen font objects as well in all descendants.
350 if (m_needsTextMetricsUpdate) {
351 updateFontInAllDescendants(this);
352 m_needsTextMetricsUpdate = false;
355 m_layoutAttributesBuilder.buildLayoutAttributesForForSubtree(this);
356 m_needsReordering = true;
357 m_needsPositioningValuesUpdate = false;
358 updateCachedBoundariesInParents = true;
359 } else if (m_needsTextMetricsUpdate || SVGRenderSupport::findTreeRootObject(this)->isLayoutSizeChanged()) {
360 // If the root layout size changed (eg. window size changes) or the transform to the root
361 // context has changed then recompute the on-screen font size.
362 updateFontInAllDescendants(this, &m_layoutAttributesBuilder);
364 ASSERT(!m_needsReordering);
365 ASSERT(!m_needsPositioningValuesUpdate);
366 m_needsTextMetricsUpdate = false;
367 updateCachedBoundariesInParents = true;
370 checkLayoutAttributesConsistency(this, m_layoutAttributes);
372 // Reduced version of RenderBlock::layoutBlock(), which only takes care of SVG text.
373 // All if branches that could cause early exit in RenderBlocks layoutBlock() method are turned into assertions.
375 ASSERT(!simplifiedLayout());
376 ASSERT(!scrollsOverflow());
377 ASSERT(!hasControlClip());
378 ASSERT(!hasColumns());
379 ASSERT(!positionedObjects());
381 ASSERT(!isAnonymousBlock());
384 setChildrenInline(true);
386 // FIXME: We need to find a way to only layout the child boxes, if needed.
387 FloatRect oldBoundaries = objectBoundingBox();
388 ASSERT(childrenInline());
390 rebuildFloatsFromIntruding();
392 LayoutUnit beforeEdge = borderBefore() + paddingBefore();
393 LayoutUnit afterEdge = borderAfter() + paddingAfter() + scrollbarLogicalHeight();
394 setLogicalHeight(beforeEdge);
396 LayoutUnit repaintLogicalTop = 0;
397 LayoutUnit repaintLogicalBottom = 0;
398 layoutInlineChildren(true, repaintLogicalTop, repaintLogicalBottom, afterEdge);
400 if (m_needsReordering)
401 m_needsReordering = false;
403 if (!updateCachedBoundariesInParents)
404 updateCachedBoundariesInParents = oldBoundaries != objectBoundingBox();
406 // Invalidate all resources of this client if our layout changed.
407 if (everHadLayout() && selfNeedsLayout())
408 SVGResourcesCache::clientLayoutChanged(this);
410 // If our bounds changed, notify the parents.
411 if (updateCachedBoundariesInParents)
412 RenderSVGBlock::setNeedsBoundariesUpdate();
414 repainter.repaintAfterLayout();
418 RootInlineBox* RenderSVGText::createRootInlineBox()
420 RootInlineBox* box = new SVGRootInlineBox(*this);
421 box->setHasVirtualLogicalHeight();
425 bool RenderSVGText::nodeAtFloatPoint(const HitTestRequest& request, HitTestResult& result, const FloatPoint& pointInParent, HitTestAction hitTestAction)
427 PointerEventsHitRules hitRules(PointerEventsHitRules::SVG_TEXT_HITTESTING, request, style()->pointerEvents());
428 bool isVisible = (style()->visibility() == VISIBLE);
429 if (isVisible || !hitRules.requireVisible) {
430 if ((hitRules.canHitBoundingBox && !objectBoundingBox().isEmpty())
431 || (hitRules.canHitStroke && (style()->svgStyle()->hasStroke() || !hitRules.requireStroke))
432 || (hitRules.canHitFill && (style()->svgStyle()->hasFill() || !hitRules.requireFill))) {
433 FloatPoint localPoint = localToParentTransform().inverse().mapPoint(pointInParent);
435 if (!SVGRenderSupport::pointInClippingArea(this, localPoint))
437 if (hitRules.canHitBoundingBox && !objectBoundingBox().contains(localPoint))
440 HitTestLocation hitTestLocation(LayoutPoint(flooredIntPoint(localPoint)));
441 return RenderBlock::nodeAtPoint(request, result, hitTestLocation, LayoutPoint(), hitTestAction);
448 PositionWithAffinity RenderSVGText::positionForPoint(const LayoutPoint& pointInContents)
450 RootInlineBox* rootBox = firstRootBox();
452 return createPositionWithAffinity(0, DOWNSTREAM);
454 ASSERT(!rootBox->nextRootBox());
455 ASSERT(childrenInline());
457 InlineBox* closestBox = toSVGRootInlineBox(rootBox)->closestLeafChildForPosition(pointInContents);
459 return createPositionWithAffinity(0, DOWNSTREAM);
461 return closestBox->renderer().positionForPoint(LayoutPoint(pointInContents.x(), closestBox->y()));
464 void RenderSVGText::absoluteQuads(Vector<FloatQuad>& quads, bool* wasFixed) const
466 quads.append(localToAbsoluteQuad(strokeBoundingBox(), 0 /* mode */, wasFixed));
469 void RenderSVGText::paint(PaintInfo& paintInfo, const LayoutPoint&)
471 if (paintInfo.context->paintingDisabled())
474 if (paintInfo.phase != PaintPhaseForeground
475 && paintInfo.phase != PaintPhaseSelfOutline
476 && paintInfo.phase != PaintPhaseSelection)
479 PaintInfo blockInfo(paintInfo);
480 GraphicsContextStateSaver stateSaver(*blockInfo.context, false);
481 const AffineTransform& localTransform = localToParentTransform();
482 if (!localTransform.isIdentity()) {
484 blockInfo.applyTransform(localTransform, false);
486 RenderBlock::paint(blockInfo, LayoutPoint());
489 FloatRect RenderSVGText::strokeBoundingBox() const
491 FloatRect strokeBoundaries = objectBoundingBox();
492 const SVGRenderStyle* svgStyle = style()->svgStyle();
493 if (!svgStyle->hasStroke())
494 return strokeBoundaries;
497 ASSERT(node()->isSVGElement());
498 SVGLengthContext lengthContext(toSVGElement(node()));
499 strokeBoundaries.inflate(svgStyle->strokeWidth()->value(lengthContext));
500 return strokeBoundaries;
503 FloatRect RenderSVGText::repaintRectInLocalCoordinates() const
505 FloatRect repaintRect = strokeBoundingBox();
506 SVGRenderSupport::intersectRepaintRectWithResources(this, repaintRect);
508 if (const ShadowList* textShadow = style()->textShadow())
509 textShadow->adjustRectForShadow(repaintRect);
514 void RenderSVGText::addChild(RenderObject* child, RenderObject* beforeChild)
516 RenderSVGBlock::addChild(child, beforeChild);
518 SVGResourcesCache::clientWasAddedToTree(child, child->style());
519 subtreeChildWasAdded(child);
522 void RenderSVGText::removeChild(RenderObject* child)
524 SVGResourcesCache::clientWillBeRemovedFromTree(child);
526 Vector<SVGTextLayoutAttributes*, 2> affectedAttributes;
527 FontCachePurgePreventer fontCachePurgePreventer;
528 subtreeChildWillBeRemoved(child, affectedAttributes);
529 RenderSVGBlock::removeChild(child);
530 subtreeChildWasRemoved(affectedAttributes);
533 // Fix for <rdar://problem/8048875>. We should not render :first-line CSS Style
534 // in a SVG text element context.
535 RenderBlock* RenderSVGText::firstLineBlock() const
540 // Fix for <rdar://problem/8048875>. We should not render :first-letter CSS Style
541 // in a SVG text element context.
542 void RenderSVGText::updateFirstLetter()