/src/mozilla-central/dom/svg/SVGPreserveAspectRatio.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
2 | | /* vim: set ts=8 sts=2 et sw=2 tw=80: */ |
3 | | /* This Source Code Form is subject to the terms of the Mozilla Public |
4 | | * License, v. 2.0. If a copy of the MPL was not distributed with this |
5 | | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
6 | | |
7 | | #include "SVGPreserveAspectRatio.h" |
8 | | |
9 | | #include "mozilla/dom/SVGPreserveAspectRatioBinding.h" |
10 | | #include "nsContentUtils.h" |
11 | | #include "nsWhitespaceTokenizer.h" |
12 | | #include "SVGAnimatedPreserveAspectRatio.h" |
13 | | |
14 | | using namespace mozilla; |
15 | | using namespace dom; |
16 | | using namespace SVGPreserveAspectRatio_Binding; |
17 | | |
18 | | NS_SVG_VAL_IMPL_CYCLE_COLLECTION_WRAPPERCACHED(DOMSVGPreserveAspectRatio, mSVGElement) |
19 | | |
20 | | NS_IMPL_CYCLE_COLLECTING_ADDREF(DOMSVGPreserveAspectRatio) |
21 | | NS_IMPL_CYCLE_COLLECTING_RELEASE(DOMSVGPreserveAspectRatio) |
22 | | |
23 | 0 | NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(DOMSVGPreserveAspectRatio) |
24 | 0 | NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY |
25 | 0 | NS_INTERFACE_MAP_ENTRY(nsISupports) |
26 | 0 | NS_INTERFACE_MAP_END |
27 | | |
28 | | static const char *sAlignStrings[] = |
29 | | { "none", "xMinYMin", "xMidYMin", "xMaxYMin", "xMinYMid", "xMidYMid", |
30 | | "xMaxYMid", "xMinYMax", "xMidYMax", "xMaxYMax" }; |
31 | | |
32 | | static const char *sMeetOrSliceStrings[] = { "meet", "slice" }; |
33 | | |
34 | | static uint16_t |
35 | | GetAlignForString(const nsAString &aAlignString) |
36 | 0 | { |
37 | 0 | for (uint32_t i = 0 ; i < ArrayLength(sAlignStrings) ; i++) { |
38 | 0 | if (aAlignString.EqualsASCII(sAlignStrings[i])) { |
39 | 0 | return (i + SVG_ALIGN_MIN_VALID); |
40 | 0 | } |
41 | 0 | } |
42 | 0 |
|
43 | 0 | return SVG_PRESERVEASPECTRATIO_UNKNOWN; |
44 | 0 | } |
45 | | |
46 | | static uint16_t |
47 | | GetMeetOrSliceForString(const nsAString &aMeetOrSlice) |
48 | 0 | { |
49 | 0 | for (uint32_t i = 0 ; i < ArrayLength(sMeetOrSliceStrings) ; i++) { |
50 | 0 | if (aMeetOrSlice.EqualsASCII(sMeetOrSliceStrings[i])) { |
51 | 0 | return (i + SVG_MEETORSLICE_MIN_VALID); |
52 | 0 | } |
53 | 0 | } |
54 | 0 |
|
55 | 0 | return SVG_MEETORSLICE_UNKNOWN; |
56 | 0 | } |
57 | | |
58 | | /* static */ nsresult |
59 | | SVGPreserveAspectRatio::FromString(const nsAString& aString, |
60 | | SVGPreserveAspectRatio* aValue) |
61 | 0 | { |
62 | 0 | nsWhitespaceTokenizerTemplate<nsContentUtils::IsHTMLWhitespace> |
63 | 0 | tokenizer(aString); |
64 | 0 | if (tokenizer.whitespaceBeforeFirstToken() || |
65 | 0 | !tokenizer.hasMoreTokens()) { |
66 | 0 | return NS_ERROR_DOM_SYNTAX_ERR; |
67 | 0 | } |
68 | 0 | const nsAString &token = tokenizer.nextToken(); |
69 | 0 |
|
70 | 0 | nsresult rv; |
71 | 0 | SVGPreserveAspectRatio val; |
72 | 0 |
|
73 | 0 | rv = val.SetAlign(GetAlignForString(token)); |
74 | 0 |
|
75 | 0 | if (NS_FAILED(rv)) { |
76 | 0 | return NS_ERROR_DOM_SYNTAX_ERR; |
77 | 0 | } |
78 | 0 | |
79 | 0 | if (tokenizer.hasMoreTokens()) { |
80 | 0 | rv = val.SetMeetOrSlice(GetMeetOrSliceForString(tokenizer.nextToken())); |
81 | 0 | if (NS_FAILED(rv)) { |
82 | 0 | return NS_ERROR_DOM_SYNTAX_ERR; |
83 | 0 | } |
84 | 0 | } else { |
85 | 0 | val.SetMeetOrSlice(SVG_MEETORSLICE_MEET); |
86 | 0 | } |
87 | 0 |
|
88 | 0 | if (tokenizer.whitespaceAfterCurrentToken()) { |
89 | 0 | return NS_ERROR_DOM_SYNTAX_ERR; |
90 | 0 | } |
91 | 0 | |
92 | 0 | *aValue = val; |
93 | 0 | return NS_OK; |
94 | 0 | } |
95 | | |
96 | | void |
97 | | SVGPreserveAspectRatio::ToString(nsAString& aValueAsString) const |
98 | 0 | { |
99 | 0 | MOZ_ASSERT(mAlign >= SVG_ALIGN_MIN_VALID && mAlign <= SVG_ALIGN_MAX_VALID, |
100 | 0 | "Unknown align"); |
101 | 0 | aValueAsString.AssignASCII(sAlignStrings[mAlign - SVG_ALIGN_MIN_VALID]); |
102 | 0 |
|
103 | 0 | if (mAlign != uint8_t(SVG_PRESERVEASPECTRATIO_NONE)) { |
104 | 0 | MOZ_ASSERT(mMeetOrSlice >= SVG_MEETORSLICE_MIN_VALID && |
105 | 0 | mMeetOrSlice <= SVG_MEETORSLICE_MAX_VALID, |
106 | 0 | "Unknown meetOrSlice"); |
107 | 0 | aValueAsString.Append(' '); |
108 | 0 | aValueAsString.AppendASCII(sMeetOrSliceStrings[mMeetOrSlice - SVG_MEETORSLICE_MIN_VALID]); |
109 | 0 | } |
110 | 0 | } |
111 | | |
112 | | bool |
113 | | SVGPreserveAspectRatio::operator==(const SVGPreserveAspectRatio& aOther) const |
114 | 0 | { |
115 | 0 | return mAlign == aOther.mAlign && |
116 | 0 | mMeetOrSlice == aOther.mMeetOrSlice; |
117 | 0 | } |
118 | | |
119 | | JSObject* |
120 | | DOMSVGPreserveAspectRatio::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) |
121 | 0 | { |
122 | 0 | return mozilla::dom::SVGPreserveAspectRatio_Binding::Wrap(aCx, this, aGivenProto); |
123 | 0 | } |
124 | | |
125 | | uint16_t |
126 | | DOMSVGPreserveAspectRatio::Align() |
127 | 0 | { |
128 | 0 | if (mIsBaseValue) { |
129 | 0 | return mVal->GetBaseValue().GetAlign(); |
130 | 0 | } |
131 | 0 | |
132 | 0 | mSVGElement->FlushAnimations(); |
133 | 0 | return mVal->GetAnimValue().GetAlign(); |
134 | 0 | } |
135 | | |
136 | | void |
137 | | DOMSVGPreserveAspectRatio::SetAlign(uint16_t aAlign, ErrorResult& rv) |
138 | 0 | { |
139 | 0 | if (!mIsBaseValue) { |
140 | 0 | rv.Throw(NS_ERROR_DOM_NO_MODIFICATION_ALLOWED_ERR); |
141 | 0 | return; |
142 | 0 | } |
143 | 0 | rv = mVal->SetBaseAlign(aAlign, mSVGElement); |
144 | 0 | } |
145 | | |
146 | | uint16_t |
147 | | DOMSVGPreserveAspectRatio::MeetOrSlice() |
148 | 0 | { |
149 | 0 | if (mIsBaseValue) { |
150 | 0 | return mVal->GetBaseValue().GetMeetOrSlice(); |
151 | 0 | } |
152 | 0 | |
153 | 0 | mSVGElement->FlushAnimations(); |
154 | 0 | return mVal->GetAnimValue().GetMeetOrSlice(); |
155 | 0 | } |
156 | | |
157 | | void |
158 | | DOMSVGPreserveAspectRatio::SetMeetOrSlice(uint16_t aMeetOrSlice, ErrorResult& rv) |
159 | 0 | { |
160 | 0 | if (!mIsBaseValue) { |
161 | 0 | rv.Throw(NS_ERROR_DOM_NO_MODIFICATION_ALLOWED_ERR); |
162 | 0 | return; |
163 | 0 | } |
164 | 0 | rv = mVal->SetBaseMeetOrSlice(aMeetOrSlice, mSVGElement); |
165 | 0 | } |
166 | | |