-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
342 lines (312 loc) · 10.4 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
define([
"dojo/_base/kernel", "dojo/_base/lang", "./_base", "dojo/_base/window", "dojo/Deferred", "dojo/_base/sniff",
"require", "dojo/_base/config", "dcl/dcl"
], function (kernel, lang, g, win, Deferred, has, require, config, dcl) {
var gu = g.utils = {};
var classesRequired, Surface, Group;
function ensureClassesRequired() {
if (!classesRequired) {
try {
Surface = require("./shape/_SurfaceBase");
} catch (err) {
}
try {
Group = require("./shape/_GroupBase");
} catch (err) {
}
classesRequired = true;
}
}
dcl.mix(gu, {
forEach: function (/*gfx/shape.Surface|gfx/shape.Shape*/ object, /*Function|String|Array*/ f, /*Object?*/o
) {
// summary:
// Takes a shape or a surface and applies a function "f" to in the context of "o"
// (or global, if missing). If "shape" was a surface or a group, it applies the same
// function to all children recursively effectively visiting all shapes of the underlying scene
// graph.
// object:
// The gfx container to iterate.
// f:
// The function to apply.
// o:
// The scope.
ensureClassesRequired();
o = o || kernel.global;
f.call(o, object);
if (dcl.isInstanceOf(object, Surface) || dcl.isInstanceOf(object, Group)) {
object.children.forEach(function (shape) {
gu.forEach(shape, f, o);
});
}
},
serialize: function (object) {
// summary:
// Takes a shape or a surface and returns an object, which describes underlying shapes.
// object: gfx/shape.Surface|gfx/shape.Shape
// The container to serialize.
/* jshint maxcomplexity:12 */
ensureClassesRequired();
var t = {}, v, isSurface = dcl.isInstanceOf(object, Surface);
if (isSurface || dcl.isInstanceOf(object, Group)) {
t.children = object.children.map(gu.serialize);
if (isSurface) {
return t.children; // Array
}
} else {
t.shape = object.shape;
}
if (object.getTransform) {
v = object.transform;
if (v) {
t.transform = v;
}
}
if (object.getStroke) {
v = object.stroke;
if (v) {
t.stroke = v;
}
}
if (object.getFill) {
v = object.fill;
if (v) {
t.fill = v;
}
}
if (object.getFont) {
v = object.font;
if (v) {
t.font = v;
}
}
return t; // Object
},
toJson: function (object, prettyPrint) {
// summary:
// Works just like serialize() but returns a JSON string. If prettyPrint is true, the string is
// pretty-printed to make it more human-readable.
// object: gfx/shape.Surface|gfx/shape.Shape
// The container to serialize.
// prettyPrint: Boolean?
// Indicates whether the output string should be formatted.
// returns: String
return JSON.stringify(gu.serialize(object), prettyPrint ? "\t" : null); // String
},
deserialize: function (parent, object) {
// summary:
// Takes a surface or a shape and populates it with an object produced by serialize().
// parent: gfx/shape.Surface|gfx/shape.Shape
// The destination container for the deserialized shapes.
// object: gfx/shape.Shape|Array
// The shapes to deserialize.
if (object instanceof Array) {
return object.map(lang.hitch(null, gu.deserialize, parent)); // Array
}
var shape = ("shape" in object) ? parent.createShape(object.shape) : parent.createGroup();
if ("transform" in object) {
shape.transform = object.transform;
}
if ("stroke" in object) {
shape.stroke = object.stroke;
}
if ("fill" in object) {
shape.fill = object.fill;
}
if ("font" in object) {
shape.font = object.font;
}
if ("children" in object) {
object.children.forEach(lang.hitch(null, gu.deserialize, shape));
}
return shape; // gfx/shape.Shape
},
fromJson: function (parent, json) {
// summary:
// Works just like deserialize() but takes a JSON representation of the object.
// parent: gfx/shape.Surface|gfx/shape.Shape
// The destination container for the deserialized shapes.
// json: String
// The shapes to deserialize.
return gu.deserialize(parent, JSON.parse(json)); // Array|gfx/shape.Shape
},
toSvg: function (/*gfx/shape.Surface*/surface) {
// summary:
// Function to serialize a GFX surface to SVG text.
// description:
// Function to serialize a GFX surface to SVG text. The value of this output
// is that there are numerous serverside parser libraries that can render
// SVG into images in various formats. This provides a way that GFX objects
// can be captured in a known format and sent serverside for serialization
// into an image.
// surface:
// The GFX surface to serialize.
// returns:
// Deferred object that will be called when SVG serialization is complete.
//Since the init and even surface creation can be async, we need to
//return a deferred that will be called when content has serialized.
var deferred = new Deferred();
if (g.renderer === "svg") {
//If we're already in SVG mode, this is easy and quick.
try {
var svg = gu._cleanSvg(gu._innerXML(surface.rawNode));
deferred.resolve(svg);
} catch (e) {
deferred.reject(e);
}
} else {
//Okay, now we have to get creative with hidden iframes and the like to
//serialize SVG.
if (!gu._initSvgSerializerDeferred) {
gu._initSvgSerializer();
}
var jsonForm = gu.toJson(surface);
var serializer = function () {
try {
var sDim = surface.getDimensions();
var width = sDim.width;
var height = sDim.height;
//Create an attach point in the iframe for the contents.
var node = gu._gfxSvgProxy.document.createElement("div");
gu._gfxSvgProxy.document.body.appendChild(node);
//Set the node scaling.
win.withDoc(gu._gfxSvgProxy.document, function () {
node.style.width = width + "px";
node.style.height = height + "px";
}, this);
var proxyGfx = gu._gfxSvgProxy.require("gfx/gfx"); // will throw exception if not loaded
//Create temp surface to render object to and render.
var ts = proxyGfx.createSurface(node, width, height);
//It's apparently possible that a suface creation is async, so we need to use
//the whenLoaded function. Probably not needed for SVG, but making it common
var draw = function (surface) {
try {
proxyGfx.utils.fromJson(surface, jsonForm);
//Get contents and remove temp surface.
var svg = gu._cleanSvg(node.innerHTML);
surface.clear();
surface.destroy();
gu._gfxSvgProxy.document.body.removeChild(node);
deferred.resolve(svg);
} catch (e) {
deferred.reject(e);
}
};
ts.whenLoaded(null, draw);
} catch (ex) {
deferred.reject(ex);
}
};
//See if we can call it directly or pass it to the deferred to be
//called on initialization.
if (gu._initSvgSerializerDeferred.isRejected() > 0) {
serializer();
} else {
gu._initSvgSerializerDeferred.addCallback(serializer);
}
}
return deferred; //dojo.Deferred that will be called when serialization finishes.
},
//iFrame document used for handling SVG serialization.
_gfxSvgProxy: null,
//Serializer loaded.
_initSvgSerializerDeferred: null,
_svgSerializerInitialized: function () {
// summary:
// Internal function to call when the serializer init completed.
// tags:
// private
gu._initSvgSerializerDeferred.resolve(true);
},
_initSvgSerializer: function () {
// summary:
// Internal function to initialize the hidden iframe where SVG rendering
// will occur.
// tags:
// private
if (!gu._initSvgSerializerDeferred) {
gu._initSvgSerializerDeferred = new Deferred();
var f = win.doc.createElement("iframe");
f.style.display = "none";
f.style.position = "absolute";
f.style.width = "1em";
f.style.height = "1em";
f.style.top = "-10000px";
if (has("ie")) {
f.onreadystatechange = function () {
if (f.contentWindow.document.readyState === "complete") {
f.onreadystatechange = function () {
};
f.contentWindow.require(["gfx/utils"], function (/*fgu*/) {
gu._gfxSvgProxy = f.contentWindow;
gu._svgSerializerInitialized();
});
}
};
} else {
f.onload = function () {
f.onload = function () {
};
f.contentWindow.require(["gfx/utils"], function (/*fgu*/) {
console.log("INITED");
gu._gfxSvgProxy = f.contentWindow;
gu._svgSerializerInitialized();
});
};
}
//We have to load the GFX SVG proxy frame. Default is to use the one packaged in gfx.
var uri = (config.gfxSvgProxyFrameUrl || require.toUrl("gfx/resources/gfxSvgProxyFrame.html"));
f.setAttribute("src", uri.toString());
win.body().appendChild(f);
}
},
_innerXML: function (/*Node*/node) {
// summary:
// Implementation of MS's innerXML function.
// node:
// The node from which to generate the XML text representation.
// tags:
// private
if (node.innerXML) {
return node.innerXML; //String
} else if (node.xml) {
return node.xml; //String
} else if (typeof XMLSerializer !== "undefined") {
return (new XMLSerializer()).serializeToString(node); //String
}
return null;
},
_cleanSvg: function (svg) {
// summary:
// Internal function that cleans up artifacts in extracted SVG content.
// tags:
// private
if (svg) {
//Make sure the namespace is set.
if (svg.indexOf("xmlns=\"http://www.w3.org/2000/svg\"") === -1) {
svg = svg.substring(4, svg.length);
svg = "<svg xmlns=\"http://www.w3.org/2000/svg\"" + svg;
}
//Same for xmlns:xlink (missing in Chrome and Safari)
if (svg.indexOf("xmlns:xlink=\"http://www.w3.org/1999/xlink\"") === -1) {
svg = svg.substring(4, svg.length);
svg = "<svg xmlns:xlink=\"http://www.w3.org/1999/xlink\"" + svg;
}
//and add namespace to href attribute if not done yet
//(FF 5+ adds xlink:href but not the xmlns def)
if (svg.indexOf("xlink:href") === -1) {
svg = svg.replace(/href\s*=/g, "xlink:href=");
}
// in IE, <image are serialized as <img>
svg = svg.replace(/<img\b([^>]*)>/gi, "<image $1 />");
//Do some other cleanup, like stripping out the
//dojoGfx attributes and quoting ids.
svg = svg.replace(/\bdojoGfx\w*\s*=\s*(['"])\w*\1/g, "");
svg = svg.replace(/\b__gfxObject__\s*=\s*(['"])\w*\1/g, "");
svg = svg.replace(/[=]([^"']+?)(\s|>)/g, "=\"$1\"$2");
}
return svg; //Cleaned SVG text.
}
});
return gu;
});