SVGFitToViewBoxImpl.cpp 3.75 KB
Newer Older
eseidel's avatar
eseidel committed
1
/*
eseidel's avatar
eseidel committed
2
    Copyright (C) 2004, 2005 Nikolas Zimmermann <wildfox@kde.org>
eseidel's avatar
eseidel committed
3
                  2004, 2005 Rob Buis <buis@kde.org>
eseidel's avatar
eseidel committed
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

    This file is part of the KDE project

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
    Boston, MA 02111-1307, USA.
*/

#include <qstringlist.h>

eseidel's avatar
eseidel committed
25
26
#include <kdom/core/AttrImpl.h>
#include <kdom/core/DOMStringImpl.h>
eseidel's avatar
eseidel committed
27
28
29
30
31
32
33
34
35
36
37
38
39

#include "svgattrs.h"
#include "SVGRectImpl.h"
#include "SVGSVGElementImpl.h"
#include "SVGAnimatedRectImpl.h"
#include "SVGFitToViewBoxImpl.h"
#include "SVGPreserveAspectRatioImpl.h"
#include "SVGAnimatedPreserveAspectRatioImpl.h"

using namespace KSVG;

SVGFitToViewBoxImpl::SVGFitToViewBoxImpl()
{
eseidel's avatar
eseidel committed
40
41
    m_viewBox = 0;
    m_preserveAspectRatio = 0;
eseidel's avatar
eseidel committed
42
43
44
45
}

SVGFitToViewBoxImpl::~SVGFitToViewBoxImpl()
{
eseidel's avatar
eseidel committed
46
47
48
49
    if(m_viewBox)
        m_viewBox->deref();
    if(m_preserveAspectRatio)
        m_preserveAspectRatio->deref();
eseidel's avatar
eseidel committed
50
51
52
53
}

SVGAnimatedRectImpl *SVGFitToViewBoxImpl::viewBox() const
{
eseidel's avatar
eseidel committed
54
55
56
57
58
59
60
61
    if(!m_viewBox)
    {
        const SVGStyledElementImpl *context = dynamic_cast<const SVGStyledElementImpl *>(this);
        m_viewBox = new SVGAnimatedRectImpl(context);
        m_viewBox->ref();
    }

    return m_viewBox;
eseidel's avatar
eseidel committed
62
63
64
65
}

SVGAnimatedPreserveAspectRatioImpl *SVGFitToViewBoxImpl::preserveAspectRatio() const
{
eseidel's avatar
eseidel committed
66
67
68
69
70
71
72
73
    if(!m_preserveAspectRatio)
    {
        const SVGStyledElementImpl *context = dynamic_cast<const SVGStyledElementImpl *>(this);
        m_preserveAspectRatio = new SVGAnimatedPreserveAspectRatioImpl(context);
        m_preserveAspectRatio->ref();
    }

    return m_preserveAspectRatio;
eseidel's avatar
eseidel committed
74
75
76
77
}

void SVGFitToViewBoxImpl::parseViewBox(KDOM::DOMStringImpl *str)
{
eseidel's avatar
eseidel committed
78
79
80
81
82
    // allow for viewbox def with ',' or whitespace
    QString viewbox(str->unicode(), str->length());
    QStringList points = QStringList::split(' ', viewbox.replace(',', ' ').simplifyWhiteSpace());

    if (points.count() == 4) {
eseidel's avatar
eseidel committed
83
84
85
86
        viewBox()->baseVal()->setX(points[0].toDouble());
        viewBox()->baseVal()->setY(points[1].toDouble());
        viewBox()->baseVal()->setWidth(points[2].toDouble());
        viewBox()->baseVal()->setHeight(points[3].toDouble());
eseidel's avatar
eseidel committed
87
88
89
    } else {
        fprintf(stderr, "WARNING: Malformed viewbox string: %s (l: %i)", viewbox.ascii(), viewbox.length());
    }
eseidel's avatar
eseidel committed
90
91
92
93
}

SVGMatrixImpl *SVGFitToViewBoxImpl::viewBoxToViewTransform(float viewWidth, float viewHeight) const
{
eseidel's avatar
eseidel committed
94
95
96
    SVGRectImpl *viewBoxRect = viewBox()->baseVal();
    if(viewBoxRect->width() == 0 || viewBoxRect->height() == 0)
        return SVGSVGElementImpl::createSVGMatrix();
eseidel's avatar
eseidel committed
97

eseidel's avatar
eseidel committed
98
99
100
    return preserveAspectRatio()->baseVal()->getCTM(viewBoxRect->x(),
            viewBoxRect->y(), viewBoxRect->width(), viewBoxRect->height(),
            0, 0, viewWidth, viewHeight);
eseidel's avatar
eseidel committed
101
102
103
104
}

bool SVGFitToViewBoxImpl::parseAttribute(KDOM::AttributeImpl *attr)
{
eseidel's avatar
eseidel committed
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
    int id = (attr->id() & NodeImpl_IdLocalMask);
    switch(id)
    {
        case ATTR_VIEWBOX:
        {
            parseViewBox(attr->value());
            return true;
        }
        case ATTR_PRESERVEASPECTRATIO:
        {
            preserveAspectRatio()->baseVal()->parsePreserveAspectRatio(attr->value());
            return true;
        }
    }

    return false;
eseidel's avatar
eseidel committed
121
122
123
}

// vim:ts=4:noet