forked from adobe-research/svgObjectModelGenerator
/
utils.js
162 lines (145 loc) · 5.96 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// Copyright (c) 2014 Adobe Systems Incorporated. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/*jslint vars: true, plusplus: true, devel: true, nomen: true, indent: 4, bitwise: true */
/*global define: true, require: true, module: true */
/* Help write the SVG */
(function () {
"use strict";
var Buffer = require('buffer').Buffer,
guidID = 1,
svgWriterIDs = require("./svgWriterIDs.js");
function Utils() {
var self = this;
self.px = function (ctx, length) {
if (typeof length === "number")
return length;
// Consider adding string conversion when needed.
if (typeof length !== "object" || !length.units)
return 0;
switch (length.units) {
case "pointsUnit":
return self.round1k(length.value * ctx.pxToInchRatio / 72);
case "millimetersUnit":
return self.round1k(length.value * ctx.pxToInchRatio / 25.4);
case "rulerCm":
return self.round1k(length.value * ctx.pxToInchRatio / 2.54);
case "rulerInches":
return self.round1k(length.value * ctx.pxToInchRatio);
case "rulerPicas":
return self.round1k(length.value * ctx.pxToInchRatio / 6);
}
return 0;
};
self.round2 = function (x) {
return +(+x).toFixed(2);
};
self.round1k = function (x) {
return +(+x).toFixed(3);
};
self.round10k = function (x) {
return +(+x).toFixed(4);
};
self.roundUp = function (x) {
return Math.ceil(x);
};
self.roundDown = function (x) {
return Math.round(x);
};
/** jQuery-style extend
* https://github.com/jquery/jquery/blob/master/src/core.js
*/
var class2type = {
"[object Boolean]": "boolean",
"[object Number]": "number",
"[object String]": "string",
"[object Function]": "function",
"[object Array]": "array",
"[object Date]": "date",
"[object RegExp]": "regexp",
"[object Object]": "object"
},
jQueryLike = {
isFunction: function (obj) {
return jQueryLike.type(obj) === "function";
},
isArray: Array.isArray,
type: function (obj) {
return obj == null ? String(obj) : class2type[String(obj)] || "object";
},
isPlainObject: function (obj) {
if (!obj || jQueryLike.type(obj) !== "object" || obj.nodeType) {
return false;
}
try {
if (obj.constructor && !obj.hasOwnProperty("constructor") && !obj.constructor.prototype.hasOwnProperty("isPrototypeOf")) {
return false;
}
} catch (e) {
return false;
}
var key;
for (key in obj) {}
return key === undefined || obj.hasOwnProperty(key);
}
};
self.extend = function (deep, target, source) {
var options, name, src, copy, copyIsArray, clone, target = arguments[0] || {},
i = 1,
length = arguments.length,
deep = false,
toString = Object.prototype.toString,
hasOwn = Object.prototype.hasOwnProperty,
push = Array.prototype.push,
slice = Array.prototype.slice,
trim = String.prototype.trim,
indexOf = Array.prototype.indexOf;
if (typeof target === "boolean") {
deep = target;
target = arguments[1] || {};
i = 2;
}
if (typeof target !== "object" && !jQueryLike.isFunction(target)) {
target = {};
}
for (i; i < length; i++) {
if ((options = arguments[i]) != null) {
for (name in options) {
src = target[name];
copy = options[name];
if (target === copy) {
continue;
}
if (deep && copy && (jQueryLike.isPlainObject(copy) || (copyIsArray = jQueryLike.isArray(copy)))) {
if (copyIsArray) {
copyIsArray = false;
clone = src && jQueryLike.isArray(src) ? src : [];
} else {
clone = src && jQueryLike.isPlainObject(src) ? src : {};
}
target[name] = self.extend(deep, clone, copy);
} else if (copy !== undefined) {
target[name] = copy;
}
}
}
}
return target;
};
self.toBase64 = function (string) {
var buf = new Buffer(string);
return buf.toString("base64");
};
}
module.exports = new Utils();
}());