• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * This file is part of the WebKit project.
3  *
4  * Copyright (C) 2006 Apple Computer, Inc.
5  *               2006 Alexander Kellett <lypanov@kde.org>
6  *               2006 Oliver Hunt <ojh16@student.canterbury.ac.nz>
7  *               2007 Nikolas Zimmermann <zimmermann@kde.org>
8  *               2008 Rob Buis <buis@kde.org>
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Library General Public
12  * License as published by the Free Software Foundation; either
13  * version 2 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Library General Public License for more details.
19  *
20  * You should have received a copy of the GNU Library General Public License
21  * along with this library; see the file COPYING.LIB.  If not, write to
22  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23  * Boston, MA 02110-1301, USA.
24  *
25  */
26 
27 #include "config.h"
28 
29 #if ENABLE(SVG)
30 #include "RenderSVGText.h"
31 
32 #include "FloatConversion.h"
33 #include "FloatQuad.h"
34 #include "GraphicsContext.h"
35 #include "PointerEventsHitRules.h"
36 #include "RenderLayer.h"
37 #include "RenderSVGRoot.h"
38 #include "SVGLengthList.h"
39 #include "SVGRenderSupport.h"
40 #include "SVGResourceFilter.h"
41 #include "SVGRootInlineBox.h"
42 #include "SVGTextElement.h"
43 #include "SVGTransformList.h"
44 #include "SVGURIReference.h"
45 #include "SimpleFontData.h"
46 
47 namespace WebCore {
48 
RenderSVGText(SVGTextElement * node)49 RenderSVGText::RenderSVGText(SVGTextElement* node)
50     : RenderSVGBlock(node)
51 {
52 }
53 
clippedOverflowRectForRepaint(RenderBoxModelObject * repaintContainer)54 IntRect RenderSVGText::clippedOverflowRectForRepaint(RenderBoxModelObject* repaintContainer)
55 {
56     return SVGRenderBase::clippedOverflowRectForRepaint(this, repaintContainer);
57 }
58 
computeRectForRepaint(RenderBoxModelObject * repaintContainer,IntRect & repaintRect,bool fixed)59 void RenderSVGText::computeRectForRepaint(RenderBoxModelObject* repaintContainer, IntRect& repaintRect, bool fixed)
60 {
61     SVGRenderBase::computeRectForRepaint(this, repaintContainer, repaintRect, fixed);
62 }
63 
mapLocalToContainer(RenderBoxModelObject * repaintContainer,bool fixed,bool useTransforms,TransformState & transformState) const64 void RenderSVGText::mapLocalToContainer(RenderBoxModelObject* repaintContainer, bool fixed , bool useTransforms, TransformState& transformState) const
65 {
66     SVGRenderBase::mapLocalToContainer(this, repaintContainer, fixed, useTransforms, transformState);
67 }
68 
layout()69 void RenderSVGText::layout()
70 {
71     ASSERT(needsLayout());
72 
73     // FIXME: This is a hack to avoid the RenderBlock::layout() partial repainting code which is not (yet) SVG aware
74     setNeedsLayout(true);
75 
76     LayoutRepainter repainter(*this, checkForRepaintDuringLayout());
77 
78     // Best guess for a relative starting point
79     SVGTextElement* text = static_cast<SVGTextElement*>(node());
80     int xOffset = (int)(text->x()->getFirst().value(text));
81     int yOffset = (int)(text->y()->getFirst().value(text));
82     setLocation(xOffset, yOffset);
83 
84     m_localTransform = text->animatedLocalTransform();
85 
86     RenderBlock::layout();
87 
88     repainter.repaintAfterLayout();
89     setNeedsLayout(false);
90 }
91 
createRootInlineBox()92 RootInlineBox* RenderSVGText::createRootInlineBox()
93 {
94     RootInlineBox* box = new (renderArena()) SVGRootInlineBox(this);
95     box->setHasVirtualHeight();
96     return box;
97 }
98 
nodeAtFloatPoint(const HitTestRequest & request,HitTestResult & result,const FloatPoint & pointInParent,HitTestAction hitTestAction)99 bool RenderSVGText::nodeAtFloatPoint(const HitTestRequest& request, HitTestResult& result, const FloatPoint& pointInParent, HitTestAction hitTestAction)
100 {
101     PointerEventsHitRules hitRules(PointerEventsHitRules::SVG_TEXT_HITTESTING, style()->pointerEvents());
102     bool isVisible = (style()->visibility() == VISIBLE);
103     if (isVisible || !hitRules.requireVisible) {
104         if ((hitRules.canHitStroke && (style()->svgStyle()->hasStroke() || !hitRules.requireStroke))
105             || (hitRules.canHitFill && (style()->svgStyle()->hasFill() || !hitRules.requireFill))) {
106             FloatPoint localPoint = localToParentTransform().inverse().mapPoint(pointInParent);
107             return RenderBlock::nodeAtPoint(request, result, (int)localPoint.x(), (int)localPoint.y(), 0, 0, hitTestAction);
108         }
109     }
110 
111     return false;
112 }
113 
nodeAtPoint(const HitTestRequest &,HitTestResult &,int,int,int,int,HitTestAction)114 bool RenderSVGText::nodeAtPoint(const HitTestRequest&, HitTestResult&, int, int, int, int, HitTestAction)
115 {
116     ASSERT_NOT_REACHED();
117     return false;
118 }
119 
absoluteRects(Vector<IntRect> & rects,int,int)120 void RenderSVGText::absoluteRects(Vector<IntRect>& rects, int, int)
121 {
122     RenderSVGRoot* root = findSVGRootObject(parent());
123     if (!root)
124         return;
125 
126     // Don't use objectBoundingBox here, as it's unites the selection rects. Makes it hard
127     // to spot errors, if there are any using WebInspector. Individually feed them into 'rects'.
128     for (InlineRunBox* runBox = firstLineBox(); runBox; runBox = runBox->nextLineBox()) {
129         ASSERT(runBox->isInlineFlowBox());
130 
131         InlineFlowBox* flowBox = static_cast<InlineFlowBox*>(runBox);
132         for (InlineBox* box = flowBox->firstChild(); box; box = box->nextOnLine()) {
133             FloatRect boxRect(box->x(), box->y(), box->width(), box->height());
134             // FIXME: crawling up the parent chain to map each rect is very inefficient
135             // we should compute the absoluteTransform outside this loop first.
136             rects.append(enclosingIntRect(localToAbsoluteQuad(boxRect).boundingBox()));
137         }
138     }
139 }
140 
absoluteQuads(Vector<FloatQuad> & quads)141 void RenderSVGText::absoluteQuads(Vector<FloatQuad>& quads)
142 {
143     RenderSVGRoot* root = findSVGRootObject(parent());
144     if (!root)
145         return;
146 
147     // Don't use objectBoundingBox here, as it's unites the selection rects. Makes it hard
148     // to spot errors, if there are any using WebInspector. Individually feed them into 'rects'.
149     for (InlineRunBox* runBox = firstLineBox(); runBox; runBox = runBox->nextLineBox()) {
150         ASSERT(runBox->isInlineFlowBox());
151 
152         InlineFlowBox* flowBox = static_cast<InlineFlowBox*>(runBox);
153         for (InlineBox* box = flowBox->firstChild(); box; box = box->nextOnLine()) {
154             FloatRect boxRect(box->x(), box->y(), box->width(), box->height());
155             // FIXME: crawling up the parent chain to map each quad is very inefficient
156             // we should compute the absoluteTransform outside this loop first.
157             quads.append(localToAbsoluteQuad(boxRect));
158         }
159     }
160 }
161 
paint(PaintInfo & paintInfo,int,int)162 void RenderSVGText::paint(PaintInfo& paintInfo, int, int)
163 {
164     PaintInfo pi(paintInfo);
165     pi.context->save();
166     applyTransformToPaintInfo(pi, localToParentTransform());
167     RenderBlock::paint(pi, 0, 0);
168     pi.context->restore();
169 }
170 
objectBoundingBox() const171 FloatRect RenderSVGText::objectBoundingBox() const
172 {
173     FloatRect boundingBox;
174 
175     for (InlineRunBox* runBox = firstLineBox(); runBox; runBox = runBox->nextLineBox()) {
176         ASSERT(runBox->isInlineFlowBox());
177 
178         InlineFlowBox* flowBox = static_cast<InlineFlowBox*>(runBox);
179         for (InlineBox* box = flowBox->firstChild(); box; box = box->nextOnLine())
180             boundingBox.unite(FloatRect(box->x(), box->y(), box->width(), box->height()));
181     }
182 
183     boundingBox.move(x(), y());
184     return boundingBox;
185 }
186 
repaintRectInLocalCoordinates() const187 FloatRect RenderSVGText::repaintRectInLocalCoordinates() const
188 {
189     FloatRect repaintRect = objectBoundingBox();
190 
191     // SVG needs to include the strokeWidth(), not the textStrokeWidth().
192     if (style()->svgStyle()->hasStroke()) {
193         float strokeWidth = SVGRenderStyle::cssPrimitiveToLength(this, style()->svgStyle()->strokeWidth(), 0.0f);
194 
195 #if ENABLE(SVG_FONTS)
196         const Font& font = style()->font();
197         if (font.primaryFont()->isSVGFont()) {
198             float scale = font.unitsPerEm() > 0 ? font.size() / font.unitsPerEm() : 0.0f;
199 
200             if (scale != 0.0f)
201                 strokeWidth /= scale;
202         }
203 #endif
204 
205         repaintRect.inflate(strokeWidth);
206     }
207 
208     repaintRect.unite(filterBoundingBoxForRenderer(this));
209 
210     return repaintRect;
211 }
212 
213 }
214 
215 #endif // ENABLE(SVG)
216 
217 // vim:ts=4:noet
218