1 /*
2 * Copyright (C) 2006 Nikolas Zimmermann <zimmermann@kde.org>
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26 #include "config.h"
27
28 #if ENABLE(SVG)
29 #include "SVGResourceMarker.h"
30
31 #include "TransformationMatrix.h"
32 #include "GraphicsContext.h"
33 #include "RenderSVGViewportContainer.h"
34 #include "TextStream.h"
35 #include <wtf/StdLibExtras.h>
36
37 namespace WebCore {
38
SVGResourceMarker()39 SVGResourceMarker::SVGResourceMarker()
40 : SVGResource()
41 , m_refX(0.0)
42 , m_refY(0.0)
43 , m_angle(-1) // just like using setAutoAngle()
44 , m_marker(0)
45 , m_useStrokeWidth(true)
46 {
47 }
48
~SVGResourceMarker()49 SVGResourceMarker::~SVGResourceMarker()
50 {
51 }
52
setMarker(RenderSVGViewportContainer * marker)53 void SVGResourceMarker::setMarker(RenderSVGViewportContainer* marker)
54 {
55 m_marker = marker;
56 }
57
setRef(double refX,double refY)58 void SVGResourceMarker::setRef(double refX, double refY)
59 {
60 m_refX = refX;
61 m_refY = refY;
62 }
63
draw(GraphicsContext * context,const FloatRect & rect,double x,double y,double strokeWidth,double angle)64 void SVGResourceMarker::draw(GraphicsContext* context, const FloatRect& rect, double x, double y, double strokeWidth, double angle)
65 {
66 if (!m_marker)
67 return;
68
69 DEFINE_STATIC_LOCAL(HashSet<SVGResourceMarker*>, currentlyDrawingMarkers, ());
70
71 // avoid drawing circular marker references
72 if (currentlyDrawingMarkers.contains(this))
73 return;
74
75 currentlyDrawingMarkers.add(this);
76
77 TransformationMatrix transform;
78 transform.translate(x, y);
79 transform.rotate(m_angle > -1 ? m_angle : angle);
80
81 // refX and refY are given in coordinates relative to the viewport established by the marker, yet they affect
82 // the translation performed on the viewport itself.
83 TransformationMatrix viewportTransform;
84 if (m_useStrokeWidth)
85 viewportTransform.scaleNonUniform(strokeWidth, strokeWidth);
86 viewportTransform *= m_marker->viewportTransform();
87 double refX, refY;
88 viewportTransform.map(m_refX, m_refY, refX, refY);
89 transform.translate(-refX, -refY);
90
91 if (m_useStrokeWidth)
92 transform.scaleNonUniform(strokeWidth, strokeWidth);
93
94 // FIXME: PaintInfo should be passed into this method instead of being created here
95 // FIXME: bounding box fractions are lost
96 RenderObject::PaintInfo info(context, enclosingIntRect(rect), PaintPhaseForeground, 0, 0, 0);
97
98 context->save();
99 context->concatCTM(transform);
100 m_marker->setDrawsContents(true);
101 m_marker->paint(info, 0, 0);
102 m_marker->setDrawsContents(false);
103 context->restore();
104
105 m_cachedBounds = transform.mapRect(m_marker->absoluteClippedOverflowRect());
106
107 currentlyDrawingMarkers.remove(this);
108 }
109
cachedBounds() const110 FloatRect SVGResourceMarker::cachedBounds() const
111 {
112 return m_cachedBounds;
113 }
114
externalRepresentation(TextStream & ts) const115 TextStream& SVGResourceMarker::externalRepresentation(TextStream& ts) const
116 {
117 ts << "[type=MARKER]"
118 << " [angle=";
119
120 if (angle() == -1)
121 ts << "auto" << "]";
122 else
123 ts << angle() << "]";
124
125 ts << " [ref x=" << refX() << " y=" << refY() << "]";
126 return ts;
127 }
128
getMarkerById(Document * document,const AtomicString & id)129 SVGResourceMarker* getMarkerById(Document* document, const AtomicString& id)
130 {
131 SVGResource* resource = getResourceById(document, id);
132 if (resource && resource->isMarker())
133 return static_cast<SVGResourceMarker*>(resource);
134
135 return 0;
136 }
137
138 } // namespace WebCore
139
140 #endif
141