blob: 9423c3ac4022273ff5577fd1f7e2beec28bb9b32 [file] [log] [blame]
reed@android.com8a1c16f2008-12-17 15:59:43 +00001/* libs/graphics/svg/SkSVGSVG.cpp
2**
3** Copyright 2006, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18#include "SkSVGSVG.h"
19#include "SkParse.h"
20#include "SkRect.h"
21#include "SkSVGParser.h"
22
23const SkSVGAttribute SkSVGSVG::gAttributes[] = {
24 SVG_LITERAL_ATTRIBUTE(enable-background, f_enable_background),
25 SVG_ATTRIBUTE(height),
26 SVG_ATTRIBUTE(overflow),
27 SVG_ATTRIBUTE(width),
28 SVG_ATTRIBUTE(version),
29 SVG_ATTRIBUTE(viewBox),
reed@android.com5ee64ad2010-05-17 14:34:13 +000030 SVG_ATTRIBUTE(x),
reed@android.com8a1c16f2008-12-17 15:59:43 +000031 SVG_LITERAL_ATTRIBUTE(xml:space, f_xml_space),
32 SVG_ATTRIBUTE(xmlns),
reed@android.com5ee64ad2010-05-17 14:34:13 +000033 SVG_LITERAL_ATTRIBUTE(xmlns:xlink, f_xml_xlink),
34 SVG_ATTRIBUTE(y),
reed@android.com8a1c16f2008-12-17 15:59:43 +000035};
36
37DEFINE_SVG_INFO(SVG)
38
39
40bool SkSVGSVG::isFlushable() {
41 return false;
42}
43
44void SkSVGSVG::translate(SkSVGParser& parser, bool defState) {
45 SkScalar height, width;
46 SkScalar viewBox[4];
47 const char* hSuffix = SkParse::FindScalar(f_height.c_str(), &height);
48 if (strcmp(hSuffix, "pt") == 0)
49 height = SkScalarMulDiv(height, SK_Scalar1 * 72, SK_Scalar1 * 96);
50 const char* wSuffix = SkParse::FindScalar(f_width.c_str(), &width);
51 if (strcmp(wSuffix, "pt") == 0)
52 width = SkScalarMulDiv(width, SK_Scalar1 * 72, SK_Scalar1 * 96);
53 SkParse::FindScalars(f_viewBox.c_str(), viewBox, 4);
54 SkRect box;
55 box.fLeft = SkScalarDiv(viewBox[0], width);
56 box.fTop = SkScalarDiv(viewBox[1], height);
57 box.fRight = SkScalarDiv(viewBox[2], width);
58 box.fBottom = SkScalarDiv(viewBox[3], height);
59 if (box.fLeft == 0 && box.fTop == 0 &&
60 box.fRight == SK_Scalar1 && box.fBottom == SK_Scalar1)
61 return;
62 parser._startElement("matrix");
63 if (box.fLeft != 0) {
64 SkString x;
65 x.appendScalar(box.fLeft);
66 parser._addAttributeLen("translateX", x.c_str(), x.size());
67 }
68 if (box.fTop != 0) {
69 SkString y;
70 y.appendScalar(box.fTop);
71 parser._addAttributeLen("translateY", y.c_str(), y.size());
72 }
73 if (box.fRight != SK_Scalar1) {
74 SkString x;
75 x.appendScalar(box.fRight);
76 parser._addAttributeLen("scaleX", x.c_str(), x.size());
77 }
78 if (box.fBottom != SK_Scalar1) {
79 SkString y;
80 y.appendScalar(box.fBottom);
81 parser._addAttributeLen("scaleY", y.c_str(), y.size());
82 }
83 parser._endElement();
84}