blob: 1678fc168efe32ebbff9c082955a44ce0e0af3fc [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),
30 SVG_LITERAL_ATTRIBUTE(xml:space, f_xml_space),
31 SVG_ATTRIBUTE(xmlns),
32 SVG_LITERAL_ATTRIBUTE(xmlns:xlink, f_xml_xlink)
33};
34
35DEFINE_SVG_INFO(SVG)
36
37
38bool SkSVGSVG::isFlushable() {
39 return false;
40}
41
42void SkSVGSVG::translate(SkSVGParser& parser, bool defState) {
43 SkScalar height, width;
44 SkScalar viewBox[4];
45 const char* hSuffix = SkParse::FindScalar(f_height.c_str(), &height);
46 if (strcmp(hSuffix, "pt") == 0)
47 height = SkScalarMulDiv(height, SK_Scalar1 * 72, SK_Scalar1 * 96);
48 const char* wSuffix = SkParse::FindScalar(f_width.c_str(), &width);
49 if (strcmp(wSuffix, "pt") == 0)
50 width = SkScalarMulDiv(width, SK_Scalar1 * 72, SK_Scalar1 * 96);
51 SkParse::FindScalars(f_viewBox.c_str(), viewBox, 4);
52 SkRect box;
53 box.fLeft = SkScalarDiv(viewBox[0], width);
54 box.fTop = SkScalarDiv(viewBox[1], height);
55 box.fRight = SkScalarDiv(viewBox[2], width);
56 box.fBottom = SkScalarDiv(viewBox[3], height);
57 if (box.fLeft == 0 && box.fTop == 0 &&
58 box.fRight == SK_Scalar1 && box.fBottom == SK_Scalar1)
59 return;
60 parser._startElement("matrix");
61 if (box.fLeft != 0) {
62 SkString x;
63 x.appendScalar(box.fLeft);
64 parser._addAttributeLen("translateX", x.c_str(), x.size());
65 }
66 if (box.fTop != 0) {
67 SkString y;
68 y.appendScalar(box.fTop);
69 parser._addAttributeLen("translateY", y.c_str(), y.size());
70 }
71 if (box.fRight != SK_Scalar1) {
72 SkString x;
73 x.appendScalar(box.fRight);
74 parser._addAttributeLen("scaleX", x.c_str(), x.size());
75 }
76 if (box.fBottom != SK_Scalar1) {
77 SkString y;
78 y.appendScalar(box.fBottom);
79 parser._addAttributeLen("scaleY", y.c_str(), y.size());
80 }
81 parser._endElement();
82}