1/*
2 * Copyright (C) 2004, 2005, 2008 Nikolas Zimmermann <zimmermann@kde.org>
3 * Copyright (C) 2004, 2005, 2006, 2007, 2010 Rob Buis <buis@kde.org>
4 * Copyright (C) 2014 Samsung Electronics. All rights reserved.
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 * Library General Public License for more details.
15 *
16 * You should have received a copy of the GNU Library General Public License
17 * along with this library; see the file COPYING.LIB.  If not, write to
18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
20 */
21
22#ifndef SVGFitToViewBox_h
23#define SVGFitToViewBox_h
24
25#include "core/SVGNames.h"
26#include "core/dom/Document.h"
27#include "core/dom/QualifiedName.h"
28#include "core/svg/SVGAnimatedPreserveAspectRatio.h"
29#include "core/svg/SVGAnimatedRect.h"
30#include "core/svg/SVGDocumentExtensions.h"
31#include "core/svg/SVGParsingError.h"
32#include "core/svg/SVGPreserveAspectRatio.h"
33#include "core/svg/SVGRect.h"
34#include "wtf/HashSet.h"
35
36namespace blink {
37
38class AffineTransform;
39class Document;
40
41class SVGFitToViewBox {
42public:
43    enum PropertyMapPolicy {
44        PropertyMapPolicyAdd,
45        PropertyMapPolicySkip,
46    };
47
48    static AffineTransform viewBoxToViewTransform(const FloatRect& viewBoxRect, PassRefPtr<SVGPreserveAspectRatio>, float viewWidth, float viewHeight);
49
50    static bool isKnownAttribute(const QualifiedName&);
51    static void addSupportedAttributes(HashSet<QualifiedName>&);
52
53    bool parseAttribute(const QualifiedName& name, const AtomicString& value, Document& document, SVGParsingError& parseError)
54    {
55        if (name == SVGNames::viewBoxAttr) {
56            m_viewBox->setBaseValueAsString(value, parseError);
57            if (m_viewBox->baseValue()->width() < 0.0f) {
58                document.accessSVGExtensions().reportError("A negative value for ViewBox width is not allowed");
59                m_viewBox->baseValue()->setInvalid();
60            }
61            if (m_viewBox->baseValue()->height() < 0.0f) {
62                document.accessSVGExtensions().reportError("A negative value for ViewBox height is not allowed");
63                m_viewBox->baseValue()->setInvalid();
64            }
65            return true;
66        }
67        if (name == SVGNames::preserveAspectRatioAttr) {
68            m_preserveAspectRatio->setBaseValueAsString(value, parseError);
69            return true;
70        }
71        return false;
72    }
73
74    bool hasEmptyViewBox() const { return m_viewBox->currentValue()->isValid() && m_viewBox->currentValue()->value().isEmpty(); }
75
76    // JS API
77    SVGAnimatedRect* viewBox() const { return m_viewBox.get(); }
78    SVGAnimatedPreserveAspectRatio* preserveAspectRatio() const { return m_preserveAspectRatio.get(); }
79
80protected:
81    explicit SVGFitToViewBox(SVGElement*, PropertyMapPolicy = PropertyMapPolicyAdd);
82    void updateViewBox(const FloatRect&);
83    void clearViewBox() { m_viewBox = nullptr; }
84    void clearPreserveAspectRatio() { m_preserveAspectRatio = nullptr; }
85
86private:
87    RefPtr<SVGAnimatedRect> m_viewBox;
88    RefPtr<SVGAnimatedPreserveAspectRatio> m_preserveAspectRatio;
89};
90
91} // namespace blink
92
93#endif // SVGFitToViewBox_h
94