forked from kamicane/art
/
ART.VML.js
588 lines (441 loc) · 13.5 KB
/
ART.VML.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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
/*
---
name: ART.VML
description: "VML implementation for ART"
authors: ["[Simo Kinnunen](http://twitter.com/sorccu)", "[Valerio Proietti](http://mad4milk.net)", "[Sebastian Markbåge](http://calyptus.eu/)"]
provides: [ART.VML, ART.VML.Group, ART.VML.Shape]
requires: [ART, ART.Element, ART.Container, ART.Path]
...
*/
(function(){
var precision = 100, UID = 0;
// VML Base Class
ART.VML = new Class({
Extends: ART.Element,
Implements: ART.Container,
initialize: function(width, height){
this.vml = document.createElement('vml');
this.element = document.createElement('av:group');
this.vml.appendChild(this.element);
this.children = [];
if (width != null && height != null) this.resize(width, height);
},
inject: function(element){
if (element.element) element = element.element;
element.appendChild(this.vml);
},
resize: function(width, height){
this.width = width;
this.height = height;
var style = this.vml.style;
style.pixelWidth = width;
style.pixelHeight = height;
style = this.element.style;
style.width = width;
style.height = height;
var halfPixel = (0.5 * precision);
this.element.coordorigin = halfPixel + ',' + halfPixel;
this.element.coordsize = (width * precision) + ',' + (height * precision);
this.children.each(function(child){
child._transform();
});
return this;
},
toElement: function(){
return this.vml;
}
});
// VML Initialization
var VMLCSS = 'behavior:url(#default#VML);display:inline-block;position:absolute;left:0px;top:0px;';
var styleSheet, styledTags = {}, styleTag = function(tag){
if (styleSheet) styledTags[tag] = styleSheet.addRule('av\\:' + tag, VMLCSS);
};
ART.VML.init = function(document){
var namespaces = document.namespaces;
if (!namespaces) return false;
namespaces.add('av', 'urn:schemas-microsoft-com:vml');
namespaces.add('ao', 'urn:schemas-microsoft-com:office:office');
styleSheet = document.createStyleSheet();
styleSheet.addRule('vml', 'display:inline-block;position:relative;overflow:hidden;');
styleTag('fill');
styleTag('stroke');
styleTag('path');
styleTag('textpath');
styleTag('group');
return true;
};
// VML Element Class
ART.VML.Element = new Class({
Extends: ART.Element,
initialize: function(tag){
this.uid = ART.uniqueID();
if (!(tag in styledTags)) styleTag(tag);
var element = this.element = document.createElement('av:' + tag);
element.setAttribute('id', 'e' + this.uid);
this.transform = {translate: [0, 0], scale: [1, 1], rotate: [0, 0, 0]};
},
/* dom */
inject: function(container){
this.eject();
this.container = container;
container.children.include(this);
this._transform();
this.parent(container);
return this;
},
eject: function(){
if (this.container){
this.container.children.erase(this);
this.container = null;
this.parent();
}
return this;
},
/* transform */
_transform: function(){
var l = this.left || 0, t = this.top || 0,
w = this.width, h = this.height;
if (w == null || h == null) return;
var tn = this.transform,
tt = tn.translate,
ts = tn.scale,
tr = tn.rotate;
var cw = w, ch = h,
cl = l, ct = t,
pl = tt[0], pt = tt[1],
rotation = tr[0],
rx = tr[1], ry = tr[2];
// rotation offset
var theta = rotation / 180 * Math.PI,
sin = Math.sin(theta), cos = Math.cos(theta);
var dx = w / 2 - rx,
dy = h / 2 - ry;
pl -= cos * -(dx + l) + sin * (dy + t) + dx;
pt -= cos * -(dy + t) - sin * (dx + l) + dy;
// scale
cw /= ts[0];
ch /= ts[1];
cl /= ts[0];
ct /= ts[1];
// transform into multiplied precision space
cw *= precision;
ch *= precision;
cl *= precision;
ct *= precision;
pl *= precision;
pt *= precision;
w *= precision;
h *= precision;
var element = this.element;
element.coordorigin = cl + ',' + ct;
element.coordsize = cw + ',' + ch;
element.style.left = pl;
element.style.top = pt;
element.style.width = w;
element.style.height = h;
element.style.rotation = rotation;
},
// transformations
translate: function(x, y){
this.transform.translate = [x, y];
this._transform();
return this;
},
scale: function(x, y){
if (y == null) y = x;
this.transform.scale = [x, y];
this._transform();
return this;
},
rotate: function(deg, x, y){
if (x == null || y == null){
var box = this.measure(precision);
x = box.left + box.width / 2; y = box.top + box.height / 2;
}
this.transform.rotate = [deg, x, y];
this._transform();
return this;
},
// visibility
hide: function(){
this.element.style.display = 'none';
return this;
},
show: function(){
this.element.style.display = '';
return this;
}
});
// VML Group Class
ART.VML.Group = new Class({
Extends: ART.VML.Element,
Implements: ART.Container,
initialize: function(){
this.parent('group');
this.children = [];
},
/* dom */
inject: function(container){
this.parent(container);
this.width = container.width;
this.height = container.height;
this._transform();
return this;
},
eject: function(){
this.parent();
this.width = this.height = null;
return this;
}
});
// VML Base Shape Class
ART.VML.Base = new Class({
Extends: ART.VML.Element,
initialize: function(tag){
this.parent(tag);
var element = this.element;
var fill = this.fillElement = document.createElement('av:fill');
fill.on = false;
element.appendChild(fill);
var stroke = this.strokeElement = document.createElement('av:stroke');
stroke.on = false;
element.appendChild(stroke);
},
/* styles */
_createGradient: function(style, stops){
var fill = this.fillElement;
// Temporarily eject the fill from the DOM
this.element.removeChild(fill);
fill.type = style;
fill.method = 'none';
fill.rotate = true;
var colors = [], color1, color2;
var addColor = function(offset, color){
color = Color.detach(color);
if (color1 == null) color1 = color;
else color2 = color;
colors.push(offset + ' ' + color[0]);
};
// Enumerate stops, assumes offsets are enumerated in order
if ('length' in stops) for (var i = 0, l = stops.length - 1; i <= l; i++) addColor(i / l, stops[i]);
else for (var offset in stops) addColor(offset, stops[offset]);
fill.color = color1[0];
fill.color2 = color2[0];
//if (fill.colors) fill.colors.value = colors; else
fill.colors = colors;
// Opacity order gets flipped when color stops are specified
fill.opacity = color2[1];
fill['ao:opacity2'] = color1[1];
fill.on = true;
this.element.appendChild(fill);
return fill;
},
_setColor: function(type, color){
var element = this[type + 'Element'];
if (color == null){
element.on = false;
} else {
color = Color.detach(color);
element.color = color[0];
element.opacity = color[1];
element.on = true;
}
},
fill: function(color){
if (arguments.length > 1){
this.fillLinear(arguments);
} else {
var fill = this.fillElement;
fill.type = 'solid';
fill.color2 = '';
fill['ao:opacity2'] = '';
if (fill.colors) fill.colors.value = '';
this._setColor('fill', color);
}
return this;
},
fillRadial: function(stops, focusX, focusY, radius){
var fill = this._createGradient('gradientradial', stops);
fill.focus = 50;
fill.focussize = '0 0';
fill.focusposition = (focusX == null ? 0.5 : focusX) + ',' + (focusY == null ? 0.5 : focusY);
fill.focus = (radius == null || radius > 0.5) ? '100%' : (Math.round(radius * 200) + '%');
return this;
},
fillLinear: function(stops, angle){
var fill = this._createGradient('gradient', stops);
fill.focus = '100%';
fill.angle = (angle == null) ? 0 : (90 + angle) % 360;
return this;
},
/* stroke */
stroke: function(color, width, cap, join){
var stroke = this.strokeElement;
stroke.weight = (width != null) ? (width / 2) + 'pt' : 1;
stroke.endcap = (cap != null) ? ((cap == 'butt') ? 'flat' : cap) : 'round';
stroke.joinstyle = (join != null) ? join : 'round';
this._setColor('stroke', color);
return this;
}
});
// VML Shape Class
ART.VML.Shape = new Class({
Extends: ART.VML.Base,
initialize: function(path){
this.parent('shape');
var p = this.pathElement = document.createElement('av:path');
p.gradientshapeok = true;
this.element.appendChild(p);
if (path != null) this.draw(path);
},
getPath: function(){
return this.currentPath;
},
// SVG to VML
draw: function(path){
this.currentPath = (path instanceof ART.Path) ? path : new ART.Path(path);
this.currentVML = this.currentPath.toVML(precision);
var size = this.currentPath.measure(precision);
this.right = size.right;
this.bottom = size.bottom;
this.top = size.top;
this.left = size.left;
this.height = size.height;
this.width = size.width;
this._transform();
this._redraw(this._radial);
return this;
},
measure: function(){
return this.getPath().measure();
},
// radial gradient workaround
_redraw: function(radial){
var vml = this.currentVML || '';
this._radial = radial;
if (radial){
var cx = Math.round((this.left + this.width * radial.x) * precision),
cy = Math.round((this.top + this.height * radial.y) * precision),
rx = Math.round(this.width * radial.r * precision),
ry = Math.round(this.height * radial.r * precision),
arc = ['wa', cx - rx, cy - ry, cx + rx, cy + ry].join(' ');
vml = [
// Resolve rendering bug
'm', cx, cy - ry, 'l', cx, cy - ry,
// Merge existing path
vml,
// Draw an ellipse around the path to force an elliptical gradient on any shape
'm', cx, cy - ry,
arc, cx, cy - ry, cx, cy + ry, arc, cx, cy + ry, cx, cy - ry,
arc, cx, cy - ry, cx, cy + ry, arc, cx, cy + ry, cx, cy - ry,
// Don't stroke the path with the extra ellipse, redraw the stroked path separately
'ns e', vml, 'nf'
].join(' ');
}
this.element.path = vml + 'e';
},
fill: function(){
this._redraw();
return this.parent.apply(this, arguments);
},
fillLinear: function(){
this._redraw();
return this.parent.apply(this, arguments);
},
fillRadial: function(stops, focusX, focusY, radius, centerX, centerY){
this.parent.apply(this, arguments);
if (focusX == null) focusX = 0.5;
if (focusY == null) focusY = 0.5;
if (radius == null) radius = 0.5;
if (centerX == null) centerX = focusX;
if (centerY == null) centerY = focusY;
centerX += centerX - focusX;
centerY += centerY - focusY;
// Compensation not needed when focusposition is applied out of document
//focusX = (focusX - centerX) / (radius * 4) + 0.5;
//focusY = (focusY - centerY) / (radius * 4) + 0.5;
this.fillElement.focus = '50%';
//this.fillElement.focusposition = focusX + ',' + focusY;
this._redraw({x: centerX, y: centerY, r: radius * 2});
return this;
}
});
var fontAnchors = { start: 'left', middle: 'center', end: 'right' };
ART.VML.Text = new Class({
Extends: ART.VML.Base,
initialize: function(text, font, alignment, path){
this.parent('shape');
var p = this.pathElement = document.createElement('av:path');
p.textpathok = true;
this.element.appendChild(p);
p = this.textPathElement = document.createElement("av:textpath");
p.on = true;
p.style['v-text-align'] = 'left';
this.element.appendChild(p);
this.draw.apply(this, arguments);
},
draw: function(text, font, alignment, path){
var element = this.element,
textPath = this.textPathElement,
style = textPath.style;
textPath.string = text;
if (font){
if (typeof font == 'string'){
style.font = font;
} else {
for (var key in font){
var ckey = key.camelCase ? key.camelCase() : key;
if (ckey == 'fontFamily') style[ckey] = "'" + font[key] + "'";
// NOT UNIVERSALLY SUPPORTED OPTIONS
// else if (ckey == 'kerning') style['v-text-kern'] = !!font[key];
// else if (ckey == 'rotateGlyphs') style['v-rotate-letters'] = !!font[key];
// else if (ckey == 'letterSpacing') style['v-text-spacing'] = Number(font[key]) + '';
else style[ckey] = font[key];
}
}
}
if (alignment) style['v-text-align'] = fontAnchors[alignment] || alignment;
if (path){
this.currentPath = path = new ART.Path(path);
this.element.path = path.toVML(precision);
} else if (!this.currentPath){
var i = -1, offsetRows = '\n';
while ((i = text.indexOf('\n', i + 1)) > -1) offsetRows += '\n';
textPath.string = offsetRows + textPath.string;
this.element.path = 'm0,0l1,0';
}
// Measuring the bounding box is currently necessary for gradients etc.
// Clone element because the element is dead once it has been in the DOM
element = element.cloneNode(true);
style = element.style;
// Reset coordinates while measuring
element.coordorigin = '0,0';
element.coordsize = '10000,10000';
style.left = '0px';
style.top = '0px';
style.width = '10000px';
style.height = '10000px';
style.rotation = 0;
// Inject the clone into the document
var canvas = new ART.VML(1, 1),
group = new ART.VML.Group(), // Wrapping it in a group seems to alleviate some client rect weirdness
body = element.ownerDocument.body;
canvas.inject(body);
group.element.appendChild(element);
group.inject(canvas);
var ebb = element.getBoundingClientRect(),
cbb = canvas.toElement().getBoundingClientRect();
canvas.eject();
this.left = ebb.left - cbb.left;
this.top = ebb.top - cbb.top;
this.width = ebb.right - ebb.left;
this.height = ebb.bottom - ebb.top;
this.right = ebb.right - cbb.left;
this.bottom = ebb.bottom - cbb.top;
this._transform();
},
measure: function(){
return { left: this.left, top: this.top, width: this.width, height: this.height, right: this.right, bottom: this.bottom };
}
});
})();