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) 2018-2019 Apple Inc. 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 | #pragma once |
23 | |
24 | #include "FloatRect.h" |
25 | #include "QualifiedName.h" |
26 | #include "SVGAnimatedPropertyImpl.h" |
27 | #include "SVGNames.h" |
28 | #include "SVGPreserveAspectRatio.h" |
29 | #include "SVGPropertyOwnerRegistry.h" |
30 | #include <wtf/HashSet.h> |
31 | |
32 | namespace WebCore { |
33 | |
34 | class AffineTransform; |
35 | |
36 | class SVGFitToViewBox { |
37 | WTF_MAKE_NONCOPYABLE(SVGFitToViewBox); |
38 | public: |
39 | static AffineTransform viewBoxToViewTransform(const FloatRect& viewBoxRect, const SVGPreserveAspectRatioValue&, float viewWidth, float viewHeight); |
40 | |
41 | using PropertyRegistry = SVGPropertyOwnerRegistry<SVGFitToViewBox>; |
42 | |
43 | const FloatRect& viewBox() const { return m_viewBox->currentValue(); } |
44 | const SVGPreserveAspectRatioValue& preserveAspectRatio() const { return m_preserveAspectRatio->currentValue(); } |
45 | |
46 | SVGAnimatedRect& viewBoxAnimated() { return m_viewBox; } |
47 | SVGAnimatedPreserveAspectRatio& preserveAspectRatioAnimated() { return m_preserveAspectRatio; } |
48 | |
49 | void setViewBox(const FloatRect&); |
50 | void resetViewBox(); |
51 | |
52 | void setPreserveAspectRatio(const SVGPreserveAspectRatioValue& preserveAspectRatio) { m_preserveAspectRatio->setBaseValInternal(preserveAspectRatio); } |
53 | void resetPreserveAspectRatio() { m_preserveAspectRatio->setBaseValInternal({ }); } |
54 | |
55 | String viewBoxString() const { return SVGPropertyTraits<FloatRect>::toString(viewBox()); } |
56 | String preserveAspectRatioString() const { return preserveAspectRatio().valueAsString(); } |
57 | |
58 | bool hasValidViewBox() const { return m_isViewBoxValid; } |
59 | bool hasEmptyViewBox() const { return m_isViewBoxValid && viewBox().isEmpty(); } |
60 | |
61 | protected: |
62 | SVGFitToViewBox(SVGElement* contextElement, SVGPropertyAccess = SVGPropertyAccess::ReadWrite); |
63 | |
64 | static bool isKnownAttribute(const QualifiedName& attributeName) { return PropertyRegistry::isKnownAttribute(attributeName); } |
65 | |
66 | void reset(); |
67 | bool parseAttribute(const QualifiedName&, const AtomicString&); |
68 | bool parseViewBox(const AtomicString& value, FloatRect& viewBox); |
69 | bool parseViewBox(const UChar*& start, const UChar* end, FloatRect& viewBox, bool validate = true); |
70 | |
71 | private: |
72 | Ref<SVGAnimatedRect> m_viewBox; |
73 | Ref<SVGAnimatedPreserveAspectRatio> m_preserveAspectRatio; |
74 | bool m_isViewBoxValid { false }; |
75 | }; |
76 | |
77 | } // namespace WebCore |
78 | |