1/*
2 * Copyright (C) 2006, 2007, 2008 Nikolas Zimmermann <zimmermann@kde.org>
3 * Copyright (C) 2009 Jeff Schiller <codedread@gmail.com>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public License
16 * along with this library; see the file COPYING.LIB.  If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 */
20
21#include "config.h"
22
23#if ENABLE(SVG)
24#include "JSSVGMatrix.h"
25
26#include "AffineTransform.h"
27#include "SVGException.h"
28#include <runtime/Error.h>
29
30using namespace JSC;
31
32namespace WebCore {
33
34JSValue JSSVGMatrix::multiply(ExecState* exec, const ArgList& args)
35{
36    if (args.size() < 1)
37        return throwError(exec, SyntaxError, "Not enough arguments");
38
39    if (!args.at(0).inherits(&JSSVGMatrix::s_info))
40        return throwError(exec, TypeError, "secondMatrix argument was not a SVGMatrix");
41
42    JSSVGMatrix* matrixObj = static_cast<JSSVGMatrix*>(asObject(args.at(0)));
43
44    AffineTransform m1(*impl());
45    AffineTransform m2(*(matrixObj->impl()));
46
47    SVGElement* context = JSSVGContextCache::svgContextForDOMObject(this);
48    return toJS(exec, globalObject(), JSSVGStaticPODTypeWrapper<AffineTransform>::create(m1.multLeft(m2)).get(), context);
49}
50
51JSValue JSSVGMatrix::inverse(ExecState* exec, const ArgList&)
52{
53    AffineTransform imp(*impl());
54
55    SVGElement* context = JSSVGContextCache::svgContextForDOMObject(this);
56    JSValue result = toJS(exec, globalObject(), JSSVGStaticPODTypeWrapper<AffineTransform>::create(imp.inverse()).get(), context);
57
58    if (!imp.isInvertible())
59        setDOMException(exec, SVGException::SVG_MATRIX_NOT_INVERTABLE);
60
61    return result;
62}
63
64JSValue JSSVGMatrix::rotateFromVector(ExecState* exec, const ArgList& args)
65{
66    AffineTransform imp(*impl());
67
68    float x = args.at(0).toFloat(exec);
69    float y = args.at(1).toFloat(exec);
70
71    SVGElement* context = JSSVGContextCache::svgContextForDOMObject(this);
72    JSValue result = toJS(exec, globalObject(), JSSVGStaticPODTypeWrapper<AffineTransform>::create(imp.rotateFromVector(x, y)).get(), context);
73
74    if (x == 0.0 || y == 0.0)
75        setDOMException(exec, SVGException::SVG_INVALID_VALUE_ERR);
76
77    return result;
78}
79
80}
81
82#endif // ENABLE(SVG)
83