KHTML
SVGResourceMarker.cpp
Go to the documentation of this file.
00001 /* 00002 * Copyright (C) 2006 Nikolas Zimmermann <zimmermann@kde.org> 00003 * 00004 * Redistribution and use in source and binary forms, with or without 00005 * modification, are permitted provided that the following conditions 00006 * are met: 00007 * 1. Redistributions of source code must retain the above copyright 00008 * notice, this list of conditions and the following disclaimer. 00009 * 2. Redistributions in binary form must reproduce the above copyright 00010 * notice, this list of conditions and the following disclaimer in the 00011 * documentation and/or other materials provided with the distribution. 00012 * 00013 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY 00014 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00015 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 00016 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR 00017 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 00018 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 00019 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00020 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 00021 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00022 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00023 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00024 */ 00025 00026 #include "config.h" 00027 00028 #if ENABLE(SVG) 00029 #include "SVGResourceMarker.h" 00030 00031 #include "AffineTransform.h" 00032 #include "GraphicsContext.h" 00033 #include "RenderSVGViewportContainer.h" 00034 #include "TextStream.h" 00035 00036 namespace WebCore { 00037 00038 SVGResourceMarker::SVGResourceMarker() 00039 : SVGResource() 00040 , m_refX(0.0) 00041 , m_refY(0.0) 00042 , m_angle(-1) // just like using setAutoAngle() 00043 , m_marker(0) 00044 , m_useStrokeWidth(true) 00045 { 00046 } 00047 00048 SVGResourceMarker::~SVGResourceMarker() 00049 { 00050 } 00051 00052 void SVGResourceMarker::setMarker(RenderSVGViewportContainer* marker) 00053 { 00054 m_marker = marker; 00055 } 00056 00057 void SVGResourceMarker::setRef(double refX, double refY) 00058 { 00059 m_refX = refX; 00060 m_refY = refY; 00061 } 00062 00063 void SVGResourceMarker::draw(GraphicsContext* context, const FloatRect& rect, double x, double y, double strokeWidth, double angle) 00064 { 00065 if (!m_marker) 00066 return; 00067 00068 static HashSet<SVGResourceMarker*> currentlyDrawingMarkers; 00069 00070 // avoid drawing circular marker references 00071 if (currentlyDrawingMarkers.contains(this)) 00072 return; 00073 00074 currentlyDrawingMarkers.add(this); 00075 00076 AffineTransform transform; 00077 transform.translate(x, y); 00078 transform.rotate(m_angle > -1 ? m_angle : angle); 00079 00080 // refX and refY are given in coordinates relative to the viewport established by the marker, yet they affect 00081 // the translation performed on the viewport itself. 00082 AffineTransform viewportTransform; 00083 if (m_useStrokeWidth) 00084 viewportTransform.scale(strokeWidth, strokeWidth); 00085 viewportTransform *= m_marker->viewportTransform(); 00086 double refX, refY; 00087 viewportTransform.map(m_refX, m_refY, &refX, &refY); 00088 transform.translate(-refX, -refY); 00089 00090 if (m_useStrokeWidth) 00091 transform.scale(strokeWidth, strokeWidth); 00092 00093 // FIXME: PaintInfo should be passed into this method instead of being created here 00094 // FIXME: bounding box fractions are lost 00095 RenderObject::PaintInfo info(context, enclosingIntRect(rect), PaintPhaseForeground, 0, 0, 0); 00096 00097 context->save(); 00098 context->concatCTM(transform); 00099 m_marker->setDrawsContents(true); 00100 m_marker->paint(info, 0, 0); 00101 m_marker->setDrawsContents(false); 00102 context->restore(); 00103 00104 m_cachedBounds = transform.mapRect(m_marker->absoluteClippedOverflowRect()); 00105 00106 currentlyDrawingMarkers.remove(this); 00107 } 00108 00109 FloatRect SVGResourceMarker::cachedBounds() const 00110 { 00111 return m_cachedBounds; 00112 } 00113 00114 TextStream& SVGResourceMarker::externalRepresentation(TextStream& ts) const 00115 { 00116 ts << "[type=MARKER]" 00117 << " [angle="; 00118 00119 if (angle() == -1) 00120 ts << "auto" << "]"; 00121 else 00122 ts << angle() << "]"; 00123 00124 ts << " [ref x=" << refX() << " y=" << refY() << "]"; 00125 return ts; 00126 } 00127 00128 SVGResourceMarker* getMarkerById(Document* document, const AtomicString& id) 00129 { 00130 SVGResource* resource = getResourceById(document, id); 00131 if (resource && resource->isMarker()) 00132 return static_cast<SVGResourceMarker*>(resource); 00133 00134 return 0; 00135 } 00136 00137 } // namespace WebCore 00138 00139 #endif
KDE 4.6 API Reference