File indexing completed on 2024-05-12 11:51:56

0001 /*
0002  * Copyright (C) 2006 Nikolas Zimmermann <zimmermann@kde.org>
0003  *
0004  * Redistribution and use in source and binary forms, with or without
0005  * modification, are permitted provided that the following conditions
0006  * are met:
0007  * 1. Redistributions of source code must retain the above copyright
0008  *    notice, this list of conditions and the following disclaimer.
0009  * 2. Redistributions in binary form must reproduce the above copyright
0010  *    notice, this list of conditions and the following disclaimer in the
0011  *    documentation and/or other materials provided with the distribution.
0012  *
0013  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
0014  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0015  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
0016  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
0017  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
0018  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
0019  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
0020  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
0021  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
0022  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
0023  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0024  */
0025 
0026 #if ENABLE(SVG)
0027 #include "SVGResourceMarker.h"
0028 
0029 #include "AffineTransform.h"
0030 #include "GraphicsContext.h"
0031 #include "RenderSVGViewportContainer.h"
0032 #include "TextStream.h"
0033 
0034 namespace WebCore
0035 {
0036 
0037 SVGResourceMarker::SVGResourceMarker()
0038     : SVGResource()
0039     , m_refX(0.0)
0040     , m_refY(0.0)
0041     , m_angle(-1) // just like using setAutoAngle()
0042     , m_marker(0)
0043     , m_useStrokeWidth(true)
0044 {
0045 }
0046 
0047 SVGResourceMarker::~SVGResourceMarker()
0048 {
0049 }
0050 
0051 void SVGResourceMarker::setMarker(RenderSVGViewportContainer *marker)
0052 {
0053     m_marker = marker;
0054 }
0055 
0056 void SVGResourceMarker::setRef(double refX, double refY)
0057 {
0058     m_refX = refX;
0059     m_refY = refY;
0060 }
0061 
0062 void SVGResourceMarker::draw(GraphicsContext *context, const FloatRect &rect, double x, double y, double strokeWidth, double angle)
0063 {
0064     if (!m_marker) {
0065         return;
0066     }
0067 
0068     static HashSet<SVGResourceMarker *> currentlyDrawingMarkers;
0069 
0070     // avoid drawing circular marker references
0071     if (currentlyDrawingMarkers.contains(this)) {
0072         return;
0073     }
0074 
0075     currentlyDrawingMarkers.add(this);
0076 
0077     AffineTransform transform;
0078     transform.translate(x, y);
0079     transform.rotate(m_angle > -1 ? m_angle : angle);
0080 
0081     // refX and refY are given in coordinates relative to the viewport established by the marker, yet they affect
0082     // the translation performed on the viewport itself.
0083     AffineTransform viewportTransform;
0084     if (m_useStrokeWidth) {
0085         viewportTransform.scale(strokeWidth, strokeWidth);
0086     }
0087     viewportTransform *= m_marker->viewportTransform();
0088     double refX, refY;
0089     viewportTransform.map(m_refX, m_refY, &refX, &refY);
0090     transform.translate(-refX, -refY);
0091 
0092     if (m_useStrokeWidth) {
0093         transform.scale(strokeWidth, strokeWidth);
0094     }
0095 
0096     // FIXME: PaintInfo should be passed into this method instead of being created here
0097     // FIXME: bounding box fractions are lost
0098     RenderObject::PaintInfo info(context, enclosingIntRect(rect), PaintPhaseForeground, 0, 0, 0);
0099 
0100     context->save();
0101     context->concatCTM(transform);
0102     m_marker->setDrawsContents(true);
0103     m_marker->paint(info, 0, 0);
0104     m_marker->setDrawsContents(false);
0105     context->restore();
0106 
0107     m_cachedBounds = transform.mapRect(m_marker->absoluteClippedOverflowRect());
0108 
0109     currentlyDrawingMarkers.remove(this);
0110 }
0111 
0112 FloatRect SVGResourceMarker::cachedBounds() const
0113 {
0114     return m_cachedBounds;
0115 }
0116 
0117 TextStream &SVGResourceMarker::externalRepresentation(TextStream &ts) const
0118 {
0119     ts << "[type=MARKER]"
0120        << " [angle=";
0121 
0122     if (angle() == -1) {
0123         ts << "auto" << "]";
0124     } else {
0125         ts << angle() << "]";
0126     }
0127 
0128     ts << " [ref x=" << refX() << " y=" << refY() << "]";
0129     return ts;
0130 }
0131 
0132 SVGResourceMarker *getMarkerById(Document *document, const AtomicString &id)
0133 {
0134     SVGResource *resource = getResourceById(document, id);
0135     if (resource && resource->isMarker()) {
0136         return static_cast<SVGResourceMarker *>(resource);
0137     }
0138 
0139     return 0;
0140 }
0141 
0142 } // namespace WebCore
0143 
0144 #endif