1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 : * vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ :
3 : * ***** BEGIN LICENSE BLOCK *****
4 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 : *
6 : * The contents of this file are subject to the Mozilla Public License Version
7 : * 1.1 (the "License"); you may not use this file except in compliance with
8 : * the License. You may obtain a copy of the License at
9 : * http://www.mozilla.org/MPL/
10 : *
11 : * Software distributed under the License is distributed on an "AS IS" basis,
12 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 : * for the specific language governing rights and limitations under the
14 : * License.
15 : *
16 : * The Original Code is Mozilla SVG Project code.
17 : *
18 : * The Initial Developer of the Original Code is the Mozilla Foundation.
19 : * Portions created by the Initial Developer are Copyright (C) 2011
20 : * the Initial Developer. All Rights Reserved.
21 : *
22 : * Contributor(s):
23 : * Brian Birtles <birtles@gmail.com>
24 : *
25 : * Alternatively, the contents of this file may be used under the terms of
26 : * either the GNU General Public License Version 2 or later (the "GPL"), or
27 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 : * in which case the provisions of the GPL or the LGPL are applicable instead
29 : * of those above. If you wish to allow use of your version of this file only
30 : * under the terms of either the GPL or the LGPL, and not to allow others to
31 : * use your version of this file under the terms of the MPL, indicate your
32 : * decision by deleting the provisions above and replace them with the notice
33 : * and other provisions required by the GPL or the LGPL. If you do not delete
34 : * the provisions above, a recipient may use your version of this file under
35 : * the terms of any one of the MPL, the GPL or the LGPL.
36 : *
37 : * ***** END LICENSE BLOCK ***** */
38 :
39 : #include "SVGTransformList.h"
40 : #include "SVGTransformListParser.h"
41 : #include "nsString.h"
42 : #include "nsDOMError.h"
43 :
44 : namespace mozilla {
45 :
46 : gfxMatrix
47 0 : SVGTransformList::GetConsolidationMatrix() const
48 : {
49 : // To benefit from Return Value Optimization and avoid copy constructor calls
50 : // due to our use of return-by-value, we must return the exact same object
51 : // from ALL return points. This function must only return THIS variable:
52 0 : gfxMatrix result;
53 :
54 0 : if (mItems.IsEmpty())
55 0 : return result;
56 :
57 0 : result = mItems[0].Matrix();
58 :
59 0 : if (mItems.Length() == 1)
60 0 : return result;
61 :
62 0 : for (PRUint32 i = 1; i < mItems.Length(); ++i) {
63 0 : result.PreMultiply(mItems[i].Matrix());
64 : }
65 :
66 0 : return result;
67 : }
68 :
69 : nsresult
70 0 : SVGTransformList::CopyFrom(const SVGTransformList& rhs)
71 : {
72 0 : return CopyFrom(rhs.mItems);
73 : }
74 :
75 : nsresult
76 0 : SVGTransformList::CopyFrom(const nsTArray<SVGTransform>& aTransformArray)
77 : {
78 0 : if (!mItems.SetCapacity(aTransformArray.Length())) {
79 : // Yes, we do want fallible alloc here
80 0 : return NS_ERROR_OUT_OF_MEMORY;
81 : }
82 0 : mItems = aTransformArray;
83 0 : return NS_OK;
84 : }
85 :
86 : void
87 0 : SVGTransformList::GetValueAsString(nsAString& aValue) const
88 : {
89 0 : aValue.Truncate();
90 0 : PRUint32 last = mItems.Length() - 1;
91 0 : for (PRUint32 i = 0; i < mItems.Length(); ++i) {
92 0 : nsAutoString length;
93 0 : mItems[i].GetValueAsString(length);
94 : // We ignore OOM, since it's not useful for us to return an error.
95 0 : aValue.Append(length);
96 0 : if (i != last) {
97 0 : aValue.Append(' ');
98 : }
99 : }
100 0 : }
101 :
102 : nsresult
103 0 : SVGTransformList::SetValueFromString(const nsAString& aValue)
104 : {
105 0 : SVGTransformListParser parser;
106 0 : nsresult rv = parser.Parse(aValue);
107 :
108 0 : if (NS_FAILED(rv)) {
109 : // there was a parse error.
110 0 : return NS_ERROR_DOM_SYNTAX_ERR;
111 : }
112 :
113 0 : return CopyFrom(parser.GetTransformList());
114 : }
115 :
116 : } // namespace mozilla
|