-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy path_base.js
816 lines (714 loc) · 22.2 KB
/
_base.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
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
define([
"dcl/dcl", "dojo/_base/kernel", "dojo/_base/lang", "dcolor/Color", "dojo/_base/sniff", "dojo/_base/config",
"dojo/_base/window"
], function (dcl, kernel, lang, Color, has, config, win) {
// module:
// gfx
// summary:
// This module contains common core Graphics API used by different graphics renderers.
var g = {};
g._hasClass = function (/*DomNode*/node, /*String*/classStr) {
// summary:
// Returns whether or not the specified classes are a portion of the
// class list currently applied to the node.
// return (new RegExp('(^|\\s+)'+classStr+'(\\s+|$)')).test(node.className) // Boolean
var cls = node.getAttribute("className");
return cls && (" " + cls + " ").indexOf(" " + classStr + " ") >= 0; // Boolean
};
g._addClass = function (/*DomNode*/node, /*String*/classStr) {
// summary:
// Adds the specified classes to the end of the class list on the
// passed node.
var cls = node.getAttribute("className") || "";
if (!cls || (" " + cls + " ").indexOf(" " + classStr + " ") < 0) {
node.setAttribute("className", cls + (cls ? " " : "") + classStr);
}
};
g._removeClass = function (/*DomNode*/node, /*String*/classStr) {
// summary:
// Removes classes from node.
var cls = node.getAttribute("className");
if (cls) {
node.setAttribute("className", cls.replace(new RegExp("(^|\\s+)" + classStr + "(\\s+|$)"), "$1$2"));
}
};
var createDiv = function (style) {
var div = win.doc.createElement("div");
var s = div.style;
for (var p in style) {
s[p] = style[p];
}
win.body().appendChild(div);
return div;
};
// derived from Morris John's emResized measurer
g._getFontMeasurements = function () {
// summary:
// Returns an object that has pixel equivilents of standard font
// size values.
var heights = {
"1em": 0,
"1ex": 0,
"100%": 0,
"12pt": 0,
"16px": 0,
"xx-small": 0,
"x-small": 0,
"small": 0,
"medium": 0,
"large": 0,
"x-large": 0,
"xx-large": 0
};
var p;
if (has("ie")) {
// we do a font-size fix if and only if one isn"t applied already.
// NOTE: If someone set the fontSize on the HTML Element, this will kill it.
win.doc.documentElement.style.fontSize = "100%";
}
// set up the measuring node.
var div = createDiv({
position: "absolute",
left: "0",
top: "-100px",
width: "30px",
height: "1000em",
borderWidth: "0",
margin: "0",
padding: "0",
outline: "none",
lineHeight: "1",
overflow: "hidden"
});
// do the measurements.
for (p in heights) {
div.style.fontSize = p;
heights[p] = Math.round(div.offsetHeight * 12 / 16) * 16 / 12 / 1000;
}
win.body().removeChild(div);
return heights; //object
};
var fontMeasurements = null;
g._getCachedFontMeasurements = function (recalculate) {
if (recalculate || !fontMeasurements) {
fontMeasurements = g._getFontMeasurements();
}
return fontMeasurements;
};
var measuringNode = null, empty = {};
g._getTextBox = function (/*String*/ text, /*Object*/ style, /*String?*/ className) {
var m, s, al = arguments.length;
var i, box;
if (!measuringNode) {
measuringNode = createDiv({
position: "absolute",
top: "-10000px",
left: "0",
visibility: "hidden"
});
}
m = measuringNode;
// reset styles
m.className = "";
s = m.style;
s.borderWidth = "0";
s.margin = "0";
s.padding = "0";
s.outline = "0";
// set new style
if (al > 1 && style) {
for (i in style) {
if (i in empty) {
continue;
}
s[i] = style[i];
}
}
// set classes
if (al > 2 && className) {
m.className = className;
}
// take a measure
m.innerHTML = text;
var bcr = m.getBoundingClientRect();
box =
{l: bcr.left, t: bcr.top, w: bcr.width || (bcr.right - bcr.left), h: bcr.height || (bcr.bottom - bcr.top)};
m.innerHTML = "";
return box;
};
g._computeTextLocation =
function (/*g.defaultTextShape*/textShape, /*Number*/width, /*Number*/height, /*Boolean*/fixHeight) {
var loc = {}, align = textShape.align;
switch (align) {
case "end":
loc.x = textShape.x - width;
break;
case "middle":
loc.x = textShape.x - width / 2;
break;
default:
loc.x = textShape.x;
break;
}
var c = fixHeight ? 0.75 : 1;
loc.y = textShape.y - height * c; // **rough** approximation of the ascent...
return loc;
};
g._computeTextBoundingBox = function (/*shape.Text*/s) {
// summary:
// Compute the bbox of the given shape.Text instance. Note that this method returns an
// approximation of the bbox, and should be used when the underlying renderer cannot provide precise
// metrics.
if (!g._isRendered(s)) {
return {x: 0, y: 0, width: 0, height: 0};
}
var loc, textShape = s.shape, font = s.font ||
g.defaultFont, w = s.getTextWidth(), h = g.normalizedLength(font.size);
loc = g._computeTextLocation(textShape, w, h, true);
return {
x: loc.x,
y: loc.y,
width: w,
height: h
};
};
g._isRendered = function (/*Shape*/s) {
var p = s.parent;
while (p && p.getParent) {
p = p.parent;
}
return p !== null;
};
// candidate for dojo.dom
var uniqueId = 0;
g._getUniqueId = function () {
// summary:
// returns a unique string for use with any DOM element
var id;
do {
id = kernel._scopeName + "xUnique" + (++uniqueId);
} while (win.doc.getElementById(id));
return id;
};
// Set touch-action attr for dpointer:
g._fixTouchAction = function (/*gfx/shape.Surface*/surface) {
var r = surface.rawNode;
if (r.getAttribute("touch-action") === "") {
r.setAttribute("touch-action", "none");
}
};
g._chooseRenderer = function () {
// Choose the GFX renderer based on the (deprecated) dojoConfig.gfxRenderer
// or the has("gfx-renderer") flag.
return (has("gfx-renderer") || config.gfxRenderer || "svg").split(",")[0];
};
/*=====
g.Stroke = {
// summary:
// A stroke defines stylistic properties that are used when drawing a path.
// color: String
// The color of the stroke, default value 'black'.
color: "black",
// style: String
// The style of the stroke, one of 'solid', ... . Default value 'solid'.
style: "solid",
// width: Number
// The width of a stroke, default value 1.
width: 1,
// cap: String
// The endcap style of the path. One of 'butt', 'round', ... . Default value 'butt'.
cap: "butt",
// join: Number
// The join style to use when combining path segments. Default value 4.
join: 4
};
g.Fill = {
// summary:
// Defines how to fill a shape. Four types of fills can be used: solid, linear gradient, radial gradient
// and pattern.
// See gfx.LinearGradient, gfx.RadialGradient and gfx.Pattern respectively for more information about the
// properties supported by each type.
// type: String?
// The type of fill. One of 'linear', 'radial', 'pattern' or undefined. If not specified, a solid fill is
// assumed.
type:"",
// color: String|dojo/Color?
// The color of a solid fill type.
color:null,
};
g.LinearGradient = {
// summary:
// An object defining the default stylistic properties used for Linear Gradient fills.
// Linear gradients are drawn along a virtual line, which results in appearance of a rotated pattern in a
// given direction/orientation.
// type: String
// Specifies this object is a Linear Gradient, value 'linear'
type: "linear",
// x1: Number
// The X coordinate of the start of the virtual line along which the gradient is drawn, default value 0.
x1: 0,
// y1: Number
// The Y coordinate of the start of the virtual line along which the gradient is drawn, default value 0.
y1: 0,
// x2: Number
// The X coordinate of the end of the virtual line along which the gradient is drawn, default value 100.
x2: 100,
// y2: Number
// The Y coordinate of the end of the virtual line along which the gradient is drawn, default value 100.
y2: 100,
// colors: Array
// An array of colors at given offsets (from the start of the line). The start of the line is
// defined at offest 0 with the end of the line at offset 1.
// Default value, [{ offset: 0, color: 'black'},{offset: 1, color: 'white'}], is a gradient from black to
// white.
colors: []
};
g.RadialGradient = {
// summary:
// Specifies the properties for RadialGradients using in fills patterns.
// type: String
// Specifies this is a RadialGradient, value 'radial'
type: "radial",
// cx: Number
// The X coordinate of the center of the radial gradient, default value 0.
cx: 0,
// cy: Number
// The Y coordinate of the center of the radial gradient, default value 0.
cy: 0,
// r: Number
// The radius to the end of the radial gradient, default value 100.
r: 100,
// colors: Array
// An array of colors at given offsets (from the center of the radial gradient).
// The center is defined at offest 0 with the outer edge of the gradient at offset 1.
// Default value, [{ offset: 0, color: 'black'},{offset: 1, color: 'white'}], is a gradient from black
// to white.
colors: []
};
g.Pattern = {
// summary:
// An object specifying the default properties for a Pattern using in fill operations.
// type: String
// Specifies this object is a Pattern, value 'pattern'.
type: "pattern",
// x: Number
// The X coordinate of the position of the pattern, default value is 0.
x: 0,
// y: Number
// The Y coordinate of the position of the pattern, default value is 0.
y: 0,
// width: Number
// The width of the pattern image, default value is 0.
width: 0,
// height: Number
// The height of the pattern image, default value is 0.
height: 0,
// src: String
// A url specifying the image to use for the pattern.
src: ""
};
g.Text = {
// summary:
// A keyword argument object defining both the text to be rendered in a VectorText shape,
// and specifying position, alignment, and fitting.
// text: String
// The text to be rendered.
// x: Number?
// The left coordinate for the text's bounding box.
// y: Number?
// The top coordinate for the text's bounding box.
// width: Number?
// The width of the text's bounding box.
// height: Number?
// The height of the text's bounding box.
// align: String?
// The alignment of the text, as defined in SVG. Can be "start", "end" or "middle".
// fitting: Number?
// How the text is to be fitted to the bounding box. Can be 0 (no fitting), 1 (fitting based on
// passed width of the bounding box and the size of the font), or 2 (fit text to the bounding box,
// and ignore any size parameters).
// leading: Number?
// The leading to be used between lines in the text.
// decoration: String?
// Any text decoration to be used.
};
g.Font = {
// summary:
// An object specifying the properties for a Font used in text operations.
// type: String
// Specifies this object is a Font, value 'font'.
type: "font",
// style: String
// The font style, one of 'normal', 'bold', default value 'normal'.
style: "normal",
// variant: String
// The font variant, one of 'normal', ... , default value 'normal'.
variant: "normal",
// weight: String
// The font weight, one of 'normal', ..., default value 'normal'.
weight: "normal",
// size: String
// The font size (including units), default value '10pt'.
size: "10pt",
// family: String
// The font family, one of 'serif', 'sanserif', ..., default value 'serif'.
family: "serif"
};
=====*/
dcl.mix(g, {
// summary:
// defines constants, prototypes, and utility functions for the core Graphics API
// default stylistic attributes
defaultStroke: {
// summary:
// A stroke defines stylistic properties that are used when drawing a path.
// This object defines the default Stroke prototype.
// type: String
// Specifies this object is a type of Stroke, value 'stroke'.
type: "stroke",
// color: String
// The color of the stroke, default value 'black'.
color: "black",
// style: String
// The style of the stroke, one of 'solid', ... . Default value 'solid'.
style: "solid",
// width: Number
// The width of a stroke, default value 1.
width: 1,
// cap: String
// The endcap style of the path. One of 'butt', 'round', ... . Default value 'butt'.
cap: "butt",
// join: Number
// The join style to use when combining path segments. Default value 4.
join: 4
},
defaultLinearGradient: {
// summary:
// An object defining the default stylistic properties used for Linear Gradient fills.
// Linear gradients are drawn along a virtual line, which results in appearance of a rotated pattern
// in a given direction/orientation.
// type: String
// Specifies this object is a Linear Gradient, value 'linear'
type: "linear",
// x1: Number
// The X coordinate of the start of the virtual line along which the gradient is drawn,
// default value 0.
x1: 0,
// y1: Number
// The Y coordinate of the start of the virtual line along which the gradient is drawn,
// default value 0.
y1: 0,
// x2: Number
// The X coordinate of the end of the virtual line along which the gradient is drawn,
// default value 100.
x2: 100,
// y2: Number
// The Y coordinate of the end of the virtual line along which the gradient is drawn,
// default value 100.
y2: 100,
// colors: Array
// An array of colors at given offsets (from the start of the line). The start of the line is
// defined at offest 0 with the end of the line at offset 1.
// Default value, [{ offset: 0, color: 'black'},{offset: 1, color: 'white'}], is a gradient from
// black to white.
colors: [
{ offset: 0, color: "black" },
{ offset: 1, color: "white" }
]
},
defaultRadialGradient: {
// summary:
// An object specifying the default properties for RadialGradients using in fills patterns.
// type: String
// Specifies this is a RadialGradient, value 'radial'
type: "radial",
// cx: Number
// The X coordinate of the center of the radial gradient, default value 0.
cx: 0,
// cy: Number
// The Y coordinate of the center of the radial gradient, default value 0.
cy: 0,
// r: Number
// The radius to the end of the radial gradient, default value 100.
r: 100,
// colors: Array
// An array of colors at given offsets (from the center of the radial gradient).
// The center is defined at offest 0 with the outer edge of the gradient at offset 1.
// Default value, [{ offset: 0, color: 'black'},{offset: 1, color: 'white'}], is a gradient from
// black to white.
colors: [
{ offset: 0, color: "black" },
{ offset: 1, color: "white" }
]
},
defaultPattern: {
// summary:
// An object specifying the default properties for a Pattern using in fill operations.
// type: String
// Specifies this object is a Pattern, value 'pattern'.
type: "pattern",
// x: Number
// The X coordinate of the position of the pattern, default value is 0.
x: 0,
// y: Number
// The Y coordinate of the position of the pattern, default value is 0.
y: 0,
// width: Number
// The width of the pattern image, default value is 0.
width: 0,
// height: Number
// The height of the pattern image, default value is 0.
height: 0,
// src: String
// A url specifying the image to use for the pattern.
src: ""
},
defaultFont: {
// summary:
// An object specifying the default properties for a Font used in text operations.
// type: String
// Specifies this object is a Font, value 'font'.
type: "font",
// style: String
// The font style, one of 'normal', 'bold', default value 'normal'.
style: "normal",
// variant: String
// The font variant, one of 'normal', ... , default value 'normal'.
variant: "normal",
// weight: String
// The font weight, one of 'normal', ..., default value 'normal'.
weight: "normal",
// size: String
// The font size (including units), default value '10pt'.
size: "10pt",
// family: String
// The font family, one of 'serif', 'sanserif', ..., default value 'serif'.
family: "serif"
},
getDefault: (function () {
// summary:
// Returns a function used to access default memoized prototype objects (see them defined above).
var typeCtorCache = {};
// a memoized delegate()
return function (/*String*/ type) {
var t = typeCtorCache[type];
if (t) {
/* jshint newcap:false */
return new t();
}
/* jshint evil:true */
t = typeCtorCache[type] = new Function();
t.prototype = g["default" + type];
/* jshint newcap:false */
return new t();
};
})(),
normalizeColor: function (/*dojo/Color|Array|string|Object*/ color) {
// summary:
// converts any legal color representation to normalized
// dojo/Color object
// color:
// A color representation.
return (color instanceof Color) ? color : new Color(color); // dojo/Color
},
normalizeParameters: function (existed, update) {
// summary:
// updates an existing object with properties from an 'update'
// object
// existed: Object
// the target object to be updated
// update: Object
// the 'update' object, whose properties will be used to update
// the existed object
var x;
if (update) {
var empty = {};
for (x in existed) {
if (x in update && !(x in empty)) {
existed[x] = update[x];
}
}
}
return existed; // Object
},
makeParameters: function (defaults, update) {
// summary:
// copies the original object, and all copied properties from the
// 'update' object
// defaults: Object
// the object to be cloned before updating
// update: Object
// the object, which properties are to be cloned during updating
// returns: Object
// new object with new and default properties
var i = null;
if (!update) {
// return dojo.clone(defaults);
return lang.delegate(defaults);
}
var result = {};
for (i in defaults) {
if (!(i in result)) {
result[i] = lang.clone((i in update) ? update[i] : defaults[i]);
}
}
return result; // Object
},
formatNumber: function (x, addSpace) {
// summary:
// converts a number to a string using a fixed notation
// x: Number
// number to be converted
// addSpace: Boolean
// whether to add a space before a positive number
// returns: String
// the formatted value
var val = x.toString();
if (val.indexOf("e") >= 0) {
val = x.toFixed(4);
} else {
var point = val.indexOf(".");
if (point >= 0 && val.length - point > 5) {
val = x.toFixed(4);
}
}
if (x < 0) {
return val; // String
}
return addSpace ? " " + val : val; // String
},
// font operations
makeFontString: function (font) {
// summary:
// converts a font object to a CSS font string
// font: Object
// font object (see gfx.defaultFont)
return font.style + " " + font.variant + " " + font.weight + " " + font.size + " " + font.family; // Object
},
splitFontString: function (str) {
// summary:
// converts a CSS font string to a font object
// description:
// Converts a CSS font string to a gfx font object. The CSS font
// string components should follow the W3C specified order
// (see http://www.w3.org/TR/CSS2/fonts.html#font-shorthand):
// style, variant, weight, size, optional line height (will be
// ignored), and family. Note that the Font.size attribute is limited to numeric CSS length.
// str: String
// a CSS font string.
// returns: Object
// object in gfx.defaultFont format
var font = g.getDefault("Font");
var t = str.split(/\s+/);
do {
if (t.length < 5) {
break;
}
font.style = t[0];
font.variant = t[1];
font.weight = t[2];
var i = t[3].indexOf("/");
font.size = i < 0 ? t[3] : t[3].substring(0, i);
var j = 4;
if (i < 0) {
if (t[4] === "/") {
j = 6;
} else if (t[4].charAt(0) === "/") {
j = 5;
}
}
if (j < t.length) {
font.family = t.slice(j).join(" ");
}
} while (false);
return font; // Object
},
// length operations
// cmInPt: Number
// points per centimeter (constant)
cmInPt: 72 / 2.54,
// mmInPt: Number
// points per millimeter (constant)
mmInPt: 7.2 / 2.54,
pxInPt: function () {
// summary:
// returns the current number of pixels per point.
return g._getCachedFontMeasurements()["12pt"] / 12; // Number
},
pt2px: function (len) {
// summary:
// converts points to pixels
// len: Number
// a value in points
return len * g.pxInPt(); // Number
},
px2pt: function (len) {
// summary:
// converts pixels to points
// len: Number
// a value in pixels
return len / g.pxInPt(); // Number
},
normalizedLength: function (len) {
// summary:
// converts any length value to pixels
// len: String
// a length, e.g., '12pc'
// returns: Number
// pixels
if (len.length === 0) {
return 0;
}
if (len.length > 2) {
var pxInPt = g.pxInPt();
var val = parseFloat(len);
switch (len.slice(-2)) {
case "px":
return val;
case "pt":
return val * pxInPt;
case "in":
return val * 72 * pxInPt;
case "pc":
return val * 12 * pxInPt;
case "mm":
return val * g.mmInPt * pxInPt;
case "cm":
return val * g.cmInPt * pxInPt;
}
}
return parseFloat(len); // Number
},
// pathSvgRegExp: RegExp
// a constant regular expression used to split a SVG path into primitive components
// tags:
// private
pathSvgRegExp: /([A-DF-Za-df-z])|([-+]?\d*[.]?\d+(?:[eE][-+]?\d+)?)/g,
equalSources: function (a, b) {
// summary:
// compares event sources, returns true if they are equal
// a: Object
// first event source
// b: Object
// event source to compare against a
// returns: Boolean
// true, if objects are truthy and the same
return a && b && a === b;
}
});
/*=====
g.createSurface = function(parentNode, width, height){
// summary:
// creates a surface
// parentNode: Node
// a parent node
// width: String|Number
// width of surface, e.g., "100px" or 100
// height: String|Number
// height of surface, e.g., "100px" or 100
// returns: gfx.Surface
// newly created surface
};
=====*/
return g; // defaults object api
});