Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / rendering / svg / RenderSVGText.cpp
1 /*
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.
10  *
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.
15  *
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.
20  *
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.
25  */
26
27 #include "config.h"
28
29 #include "core/rendering/svg/RenderSVGText.h"
30
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"
53
54 namespace WebCore {
55
56 RenderSVGText::RenderSVGText(SVGTextElement* node)
57     : RenderSVGBlock(node)
58     , m_needsReordering(false)
59     , m_needsPositioningValuesUpdate(false)
60     , m_needsTransformUpdate(true)
61     , m_needsTextMetricsUpdate(false)
62 {
63 }
64
65 RenderSVGText::~RenderSVGText()
66 {
67     ASSERT(m_layoutAttributes.isEmpty());
68 }
69
70 bool RenderSVGText::isChildAllowed(RenderObject* child, RenderStyle*) const
71 {
72     return child->isSVGInline() || (child->isText() && SVGRenderSupport::isRenderableTextNode(child));
73 }
74
75 RenderSVGText* RenderSVGText::locateRenderSVGTextAncestor(RenderObject* start)
76 {
77     ASSERT(start);
78     while (start && !start->isSVGText())
79         start = start->parent();
80     if (!start || !start->isSVGText())
81         return 0;
82     return toRenderSVGText(start);
83 }
84
85 const RenderSVGText* RenderSVGText::locateRenderSVGTextAncestor(const RenderObject* start)
86 {
87     ASSERT(start);
88     while (start && !start->isSVGText())
89         start = start->parent();
90     if (!start || !start->isSVGText())
91         return 0;
92     return toRenderSVGText(start);
93 }
94
95 void RenderSVGText::computeRectForRepaint(const RenderLayerModelObject* repaintContainer, LayoutRect& rect, bool fixed) const
96 {
97     FloatRect repaintRect = rect;
98     computeFloatRectForRepaint(repaintContainer, repaintRect, fixed);
99     rect = enclosingLayoutRect(repaintRect);
100 }
101
102 static inline void collectLayoutAttributes(RenderObject* text, Vector<SVGTextLayoutAttributes*>& attributes)
103 {
104     for (RenderObject* descendant = text; descendant; descendant = descendant->nextInPreOrder(text)) {
105         if (descendant->isSVGInlineText())
106             attributes.append(toRenderSVGInlineText(descendant)->layoutAttributes());
107     }
108 }
109
110 static inline bool findPreviousAndNextAttributes(RenderObject* root, RenderSVGInlineText* locateElement, SVGTextLayoutAttributes*& previous, SVGTextLayoutAttributes*& next)
111 {
112     ASSERT(root);
113     ASSERT(locateElement);
114     bool stopAfterNext = false;
115     RenderObject* current = root->firstChild();
116     while (current) {
117         if (current->isSVGInlineText()) {
118             RenderSVGInlineText* text = toRenderSVGInlineText(current);
119             if (locateElement != text) {
120                 if (stopAfterNext) {
121                     next = text->layoutAttributes();
122                     return true;
123                 }
124
125                 previous = text->layoutAttributes();
126             } else {
127                 stopAfterNext = true;
128             }
129         } else if (current->isSVGInline()) {
130             // Descend into text content (if possible).
131             if (RenderObject* child = current->firstChild()) {
132                 current = child;
133                 continue;
134             }
135         }
136
137         current = current->nextInPreOrderAfterChildren(root);
138     }
139     return false;
140 }
141
142 inline bool RenderSVGText::shouldHandleSubtreeMutations() const
143 {
144     if (beingDestroyed() || !everHadLayout()) {
145         ASSERT(m_layoutAttributes.isEmpty());
146         ASSERT(!m_layoutAttributesBuilder.numberOfTextPositioningElements());
147         return false;
148     }
149     return true;
150 }
151
152 void RenderSVGText::subtreeChildWasAdded(RenderObject* child)
153 {
154     ASSERT(child);
155     if (!shouldHandleSubtreeMutations() || documentBeingDestroyed())
156         return;
157
158     // Always protect the cache before clearing text positioning elements when the cache will subsequently be rebuilt.
159     FontCachePurgePreventer fontCachePurgePreventer;
160
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();
164
165     if (!child->isSVGInlineText() && !child->isSVGInline())
166         return;
167
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());
173         return;
174     }
175
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);
187
188             if (previous)
189                 m_layoutAttributesBuilder.buildLayoutAttributesForTextRenderer(previous->context());
190             m_layoutAttributesBuilder.buildLayoutAttributesForTextRenderer(attributes->context());
191             if (next)
192                 m_layoutAttributesBuilder.buildLayoutAttributesForTextRenderer(next->context());
193             break;
194         }
195     }
196
197 #ifndef NDEBUG
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);
201 #endif
202
203     m_layoutAttributes = newLayoutAttributes;
204 }
205
206 static inline void checkLayoutAttributesConsistency(RenderSVGText* text, Vector<SVGTextLayoutAttributes*>& expectedLayoutAttributes)
207 {
208 #ifndef NDEBUG
209     Vector<SVGTextLayoutAttributes*> newLayoutAttributes;
210     collectLayoutAttributes(text, newLayoutAttributes);
211     ASSERT(newLayoutAttributes == expectedLayoutAttributes);
212 #endif
213 }
214
215 void RenderSVGText::willBeDestroyed()
216 {
217     m_layoutAttributes.clear();
218     m_layoutAttributesBuilder.clearTextPositioningElements();
219
220     RenderSVGBlock::willBeDestroyed();
221 }
222
223 void RenderSVGText::subtreeChildWillBeRemoved(RenderObject* child, Vector<SVGTextLayoutAttributes*, 2>& affectedAttributes)
224 {
225     ASSERT(child);
226     if (!shouldHandleSubtreeMutations())
227         return;
228
229     checkLayoutAttributesConsistency(this, m_layoutAttributes);
230
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())
235         return;
236
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);
243
244     if (previous)
245         affectedAttributes.append(previous);
246     if (next)
247         affectedAttributes.append(next);
248
249     size_t position = m_layoutAttributes.find(text->layoutAttributes());
250     ASSERT(position != kNotFound);
251     m_layoutAttributes.remove(position);
252 }
253
254 void RenderSVGText::subtreeChildWasRemoved(const Vector<SVGTextLayoutAttributes*, 2>& affectedAttributes)
255 {
256     if (!shouldHandleSubtreeMutations() || documentBeingDestroyed()) {
257         ASSERT(affectedAttributes.isEmpty());
258         return;
259     }
260
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());
266 }
267
268 void RenderSVGText::subtreeStyleDidChange()
269 {
270     if (!shouldHandleSubtreeMutations() || documentBeingDestroyed())
271         return;
272
273     checkLayoutAttributesConsistency(this, m_layoutAttributes);
274
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));
281     }
282 }
283
284 void RenderSVGText::subtreeTextDidChange(RenderSVGInlineText* text)
285 {
286     ASSERT(text);
287     ASSERT(!beingDestroyed());
288     if (!everHadLayout()) {
289         ASSERT(m_layoutAttributes.isEmpty());
290         ASSERT(!m_layoutAttributesBuilder.numberOfTextPositioningElements());
291         return;
292     }
293
294     // Always protect the cache before clearing text positioning elements when the cache will subsequently be rebuilt.
295     FontCachePurgePreventer fontCachePurgePreventer;
296
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();
300
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));
305     }
306 }
307
308 static inline void updateFontInAllDescendants(RenderObject* start, SVGTextLayoutAttributesBuilder* builder = 0)
309 {
310     for (RenderObject* descendant = start; descendant; descendant = descendant->nextInPreOrder(start)) {
311         if (!descendant->isSVGInlineText())
312             continue;
313         RenderSVGInlineText* text = toRenderSVGInlineText(descendant);
314         text->updateScaledFont();
315         if (builder)
316             builder->rebuildMetricsForTextRenderer(text);
317     }
318 }
319
320 void RenderSVGText::layout()
321 {
322     ASSERT(needsLayout());
323
324     subtreeStyleDidChange();
325
326     LayoutRepainter repainter(*this, SVGRenderSupport::checkForSVGRepaintDuringLayout(this));
327
328     bool updateCachedBoundariesInParents = false;
329     if (m_needsTransformUpdate) {
330         m_localTransform = toSVGTextElement(node())->animatedLocalTransform();
331         m_needsTransformUpdate = false;
332         updateCachedBoundariesInParents = true;
333     }
334
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);
342
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;
353         }
354
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);
363
364         ASSERT(!m_needsReordering);
365         ASSERT(!m_needsPositioningValuesUpdate);
366         m_needsTextMetricsUpdate = false;
367         updateCachedBoundariesInParents = true;
368     }
369
370     checkLayoutAttributesConsistency(this, m_layoutAttributes);
371
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.
374     ASSERT(!isInline());
375     ASSERT(!simplifiedLayout());
376     ASSERT(!scrollsOverflow());
377     ASSERT(!hasControlClip());
378     ASSERT(!hasColumns());
379     ASSERT(!positionedObjects());
380     ASSERT(!m_overflow);
381     ASSERT(!isAnonymousBlock());
382
383     if (!firstChild())
384         setChildrenInline(true);
385
386     // FIXME: We need to find a way to only layout the child boxes, if needed.
387     FloatRect oldBoundaries = objectBoundingBox();
388     ASSERT(childrenInline());
389
390     rebuildFloatsFromIntruding();
391
392     LayoutUnit beforeEdge = borderBefore() + paddingBefore();
393     LayoutUnit afterEdge = borderAfter() + paddingAfter() + scrollbarLogicalHeight();
394     setLogicalHeight(beforeEdge);
395
396     LayoutUnit repaintLogicalTop = 0;
397     LayoutUnit repaintLogicalBottom = 0;
398     layoutInlineChildren(true, repaintLogicalTop, repaintLogicalBottom, afterEdge);
399
400     if (m_needsReordering)
401         m_needsReordering = false;
402
403     if (!updateCachedBoundariesInParents)
404         updateCachedBoundariesInParents = oldBoundaries != objectBoundingBox();
405
406     // Invalidate all resources of this client if our layout changed.
407     if (everHadLayout() && selfNeedsLayout())
408         SVGResourcesCache::clientLayoutChanged(this);
409
410     // If our bounds changed, notify the parents.
411     if (updateCachedBoundariesInParents)
412         RenderSVGBlock::setNeedsBoundariesUpdate();
413
414     repainter.repaintAfterLayout();
415     clearNeedsLayout();
416 }
417
418 RootInlineBox* RenderSVGText::createRootInlineBox()
419 {
420     RootInlineBox* box = new SVGRootInlineBox(*this);
421     box->setHasVirtualLogicalHeight();
422     return box;
423 }
424
425 bool RenderSVGText::nodeAtFloatPoint(const HitTestRequest& request, HitTestResult& result, const FloatPoint& pointInParent, HitTestAction hitTestAction)
426 {
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);
434
435             if (!SVGRenderSupport::pointInClippingArea(this, localPoint))
436                 return false;
437             if (hitRules.canHitBoundingBox && !objectBoundingBox().contains(localPoint))
438                 return false;
439
440             HitTestLocation hitTestLocation(LayoutPoint(flooredIntPoint(localPoint)));
441             return RenderBlock::nodeAtPoint(request, result, hitTestLocation, LayoutPoint(), hitTestAction);
442         }
443     }
444
445     return false;
446 }
447
448 PositionWithAffinity RenderSVGText::positionForPoint(const LayoutPoint& pointInContents)
449 {
450     RootInlineBox* rootBox = firstRootBox();
451     if (!rootBox)
452         return createPositionWithAffinity(0, DOWNSTREAM);
453
454     ASSERT(!rootBox->nextRootBox());
455     ASSERT(childrenInline());
456
457     InlineBox* closestBox = toSVGRootInlineBox(rootBox)->closestLeafChildForPosition(pointInContents);
458     if (!closestBox)
459         return createPositionWithAffinity(0, DOWNSTREAM);
460
461     return closestBox->renderer().positionForPoint(LayoutPoint(pointInContents.x(), closestBox->y()));
462 }
463
464 void RenderSVGText::absoluteQuads(Vector<FloatQuad>& quads, bool* wasFixed) const
465 {
466     quads.append(localToAbsoluteQuad(strokeBoundingBox(), 0 /* mode */, wasFixed));
467 }
468
469 void RenderSVGText::paint(PaintInfo& paintInfo, const LayoutPoint&)
470 {
471     if (paintInfo.context->paintingDisabled())
472         return;
473
474     if (paintInfo.phase != PaintPhaseForeground
475      && paintInfo.phase != PaintPhaseSelfOutline
476      && paintInfo.phase != PaintPhaseSelection)
477          return;
478
479     PaintInfo blockInfo(paintInfo);
480     GraphicsContextStateSaver stateSaver(*blockInfo.context, false);
481     const AffineTransform& localTransform = localToParentTransform();
482     if (!localTransform.isIdentity()) {
483         stateSaver.save();
484         blockInfo.applyTransform(localTransform, false);
485     }
486     RenderBlock::paint(blockInfo, LayoutPoint());
487 }
488
489 FloatRect RenderSVGText::strokeBoundingBox() const
490 {
491     FloatRect strokeBoundaries = objectBoundingBox();
492     const SVGRenderStyle* svgStyle = style()->svgStyle();
493     if (!svgStyle->hasStroke())
494         return strokeBoundaries;
495
496     ASSERT(node());
497     ASSERT(node()->isSVGElement());
498     SVGLengthContext lengthContext(toSVGElement(node()));
499     strokeBoundaries.inflate(svgStyle->strokeWidth()->value(lengthContext));
500     return strokeBoundaries;
501 }
502
503 FloatRect RenderSVGText::repaintRectInLocalCoordinates() const
504 {
505     FloatRect repaintRect = strokeBoundingBox();
506     SVGRenderSupport::intersectRepaintRectWithResources(this, repaintRect);
507
508     if (const ShadowList* textShadow = style()->textShadow())
509         textShadow->adjustRectForShadow(repaintRect);
510
511     return repaintRect;
512 }
513
514 void RenderSVGText::addChild(RenderObject* child, RenderObject* beforeChild)
515 {
516     RenderSVGBlock::addChild(child, beforeChild);
517
518     SVGResourcesCache::clientWasAddedToTree(child, child->style());
519     subtreeChildWasAdded(child);
520 }
521
522 void RenderSVGText::removeChild(RenderObject* child)
523 {
524     SVGResourcesCache::clientWillBeRemovedFromTree(child);
525
526     Vector<SVGTextLayoutAttributes*, 2> affectedAttributes;
527     FontCachePurgePreventer fontCachePurgePreventer;
528     subtreeChildWillBeRemoved(child, affectedAttributes);
529     RenderSVGBlock::removeChild(child);
530     subtreeChildWasRemoved(affectedAttributes);
531 }
532
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
536 {
537     return 0;
538 }
539
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()
543 {
544 }
545
546 }