This repository has been archived by the owner on Apr 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 235
/
BorderRenderer.js
280 lines (249 loc) · 13.2 KB
/
BorderRenderer.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
/**
* Renderer for element borders.
* @constructor
* @param {Element} el The target element
* @param {Object} styleInfos The StyleInfo objects
* @param {PIE.RootRenderer} parent
*/
PIE.BorderRenderer = PIE.RendererBase.newRenderer( {
shapeZIndex: 4,
/**
* Single definition of arguments for use by the per-side creation loop in
* getBorderSegmentsInfo. Arguments are, in order:
* centerX1, centerY1, outerX1, outerY1, centerX2, centerY2, outerX2, outerY2, baseAngle
*/
sideArgs: {
't': [ 2, 1, 0, 3, 4, 7, 6, 5, 90 ],
'r': [ 4, 7, 6, 5, 10, 9, 8, 11, 0 ],
'b': [ 10, 9, 8, 11, 12, 15, 14, 13, 270 ],
'l': [ 12, 15, 14, 13, 2, 1, 0, 3, 180 ]
},
dashedStyles: {
'dotted': 1,
'dashed': 1
},
needsUpdate: function() {
var si = this.styleInfos;
return si.borderInfo.changed() || si.borderRadiusInfo.changed();
},
isActive: function() {
var si = this.styleInfos;
return ( si.borderRadiusInfo.isActive() ||
si.backgroundInfo.isActive() ) &&
!si.borderImageInfo.isActive() &&
si.borderInfo.isActive(); //check BorderStyleInfo last because it's the most expensive
},
/**
* Draw the border shape(s)
*/
draw: function() {
var me = this,
el = me.targetElement,
props = me.styleInfos.borderInfo.getProps(),
bounds = me.boundsInfo.getBounds(),
shape, segmentsInfo, i, len;
if( props ) {
me.hideBorder();
segmentsInfo = me.getBorderSegmentsInfo();
for( i = 0, len = segmentsInfo.length; i < len; i += 2) {
shape = me.getShape( 'border' + i, me.shapeZIndex );
shape.setSize( bounds.w, bounds.h );
shape.setAttrs(
'path', segmentsInfo[ i ]
);
shape.setFillAttrs(
'color', segmentsInfo[ i + 1 ].colorValue( el )
);
}
// remove any previously-created border shapes which didn't get used above
while( me.deleteShape( 'border' + i++ ) ) {}
}
},
/**
* Adds rectangular sub-paths at intervals along a given side which serve to "cut out"
* those areas, forming the spaces in a dashed or dotted border.
* @param {Array.<string>} path The path string array to which the extra sub-paths will be added
* @param {number} startCoord The x or y coordinate at which the dashing starts
* @param {number} endCoord The x or y coordinate at which the dashing ends
* @param {number} sideWidth The width of the border on the target side
* @param {number} shift A shift of the perpendicular coordinate
* @param {boolean} isVertical True if this is a vertical border (left or right)
* @param {string} style The border style, either 'dotted' or 'dashed'
*/
dashify: function( path, startCoord, endCoord, sideWidth, shift, isVertical, style ) {
var dashLength = sideWidth * ( style === 'dashed' ? 3 : 1 ),
shift2 = shift + sideWidth,
dashEndCoord;
// If dash is longer than the box edge, don't make any cutouts
if( dashLength < endCoord - startCoord ) {
// adjust the start to keep the dash pattern centered on the box edge, favoring full
// spaces over full dashes, like WebKit does.
startCoord += ( endCoord - startCoord - dashLength ) / 2 % dashLength;
// add rectangular sub-paths to cut out each dash's space
while( startCoord < endCoord ) {
dashEndCoord = Math.min( startCoord + dashLength, endCoord );
path.push(
isVertical ? (
'm' + shift + ',' + startCoord +
'l' + shift + ',' + dashEndCoord +
'l' + shift2 + ',' + dashEndCoord +
'l' + shift2 + ',' + startCoord + 'x'
) : (
'm' + startCoord + ',' + shift +
'l' + dashEndCoord + ',' + shift +
'l' + dashEndCoord + ',' + shift2 +
'l' + startCoord + ',' + shift2 + 'x'
)
);
startCoord += dashLength * 2;
}
}
},
/**
* Get the VML path definitions for the border segment(s).
* @return {Array.<string>} Pairs of segment info: 1st item in each pair is the path string, 2nd is the fill color
*/
getBorderSegmentsInfo: function() {
var me = this,
borderInfo = me.styleInfos.borderInfo,
segmentsInfo = [];
if( borderInfo.isActive() ) {
var mult = 2,
el = me.targetElement,
bounds = me.boundsInfo.getBounds(),
borderProps = borderInfo.getProps(),
widths = borderProps.widths,
styles = borderProps.styles,
colors = borderProps.colors,
M = Math,
abs = M.abs,
round = M.round,
wT = round( widths['t'].pixels( el ) ),
wR = round( widths['r'].pixels( el ) ),
wB = round( widths['b'].pixels( el ) ),
wL = round( widths['l'].pixels( el ) ),
path = [],
innerCoords, outerCoords, doubleOuterCoords, doubleInnerCoords,
sideArgs = me.sideArgs,
side,
deg = 65535,
dashedStyles = me.dashedStyles,
style;
// When the border has uniform color and style all the way around, we can get
// away with a single VML path shape, otherwise we need four separate shapes.
if ( borderProps.stylesSame && borderProps.colorsSame ) {
if( colors['t'].alpha() > 0 ) {
// Outer path
path[ 0 ] = me.getBoxPath( 0, 0, 0, 0, mult );
// If double style, add the middle cutout sub-paths
style = styles[ 't' ];
if( style === 'double' ) {
path.push(
me.getBoxPath( wT / 3, wR / 3, wB / 3, wL / 3, mult ) +
me.getBoxPath( wT * 2 / 3, wR * 2 / 3, wB * 2 / 3, wL * 2 / 3, mult )
);
}
// If dashed, add the dash cutout sub-paths
else if( style in dashedStyles ) {
innerCoords = me.getBoxPathCoords( wT, wR, wB, wL, mult );
me.dashify( path, innerCoords[ 2 ], innerCoords[ 4 ], wT * mult, 0, 0, styles[ 't' ] );
me.dashify( path, innerCoords[ 7 ], innerCoords[ 9 ], wR * mult, ( bounds.w - wR ) * mult, 1, styles[ 'r' ] );
me.dashify( path, innerCoords[ 12 ], innerCoords[ 10 ], wB * mult, ( bounds.h - wB ) * mult, 0, styles[ 'b' ] );
me.dashify( path, innerCoords[ 1 ], innerCoords[ 15 ], wL * mult, 0, 1, styles[ 'l' ] );
}
// Inner path
path.push( me.getBoxPath( wT, wR, wB, wL, mult ) );
segmentsInfo.push( path.join( '' ), colors['t'] );
}
}
else {
outerCoords = me.getBoxPathCoords( 0, 0, 0, 0, mult );
innerCoords = me.getBoxPathCoords( wT, wR, wB, wL, mult );
// Build the segment for each side
for( side in sideArgs ) {
if ( sideArgs.hasOwnProperty( side ) ) {
var args = sideArgs[ side ],
centerX1 = args[ 0 ],
centerY1 = args[ 1 ],
outerX1 = args[ 2 ],
outerY1 = args[ 3 ],
centerX2 = args[ 4 ],
centerY2 = args[ 5 ],
outerX2 = args[ 6 ],
outerY2 = args[ 7 ],
baseAngle = args[ 8 ];
style = styles[ side ];
// Outer edge
path[ 0 ] = 'al' + outerCoords[ centerX1 ] + ',' + outerCoords[ centerY1 ] + ',' +
abs( outerCoords[ outerX1 ] - outerCoords[ centerX1 ] ) + ',' +
abs( outerCoords[ outerY1 ] - outerCoords[ centerY1 ] ) + ',' +
( baseAngle + 45 ) * deg + ',' + -45 * deg +
'ae' + outerCoords[ centerX2 ] + ',' + outerCoords[ centerY2 ] + ',' +
abs( outerCoords[ outerX2 ] - outerCoords[ centerX2 ] ) + ',' +
abs( outerCoords[ outerY2 ] - outerCoords[ centerY2 ] ) + ',' +
baseAngle * deg + ',' + -45 * deg;
// If double style, add the middle cutout sub-paths
if( style === 'double' ) {
if( !doubleOuterCoords ) {
doubleOuterCoords = me.getBoxPathCoords( wT / 3, wR / 3, wB / 3, wL / 3, mult );
doubleInnerCoords = me.getBoxPathCoords( wT * 2 / 3, wR * 2 / 3, wB * 2 / 3, wL * 2 / 3, mult );
}
path.push(
'ae' + doubleOuterCoords[ centerX2 ] + ',' + doubleOuterCoords[ centerY2 ] + ',' +
abs( doubleOuterCoords[ outerX2 ] - doubleOuterCoords[ centerX2 ] ) + ',' +
abs( doubleOuterCoords[ outerY2 ] - doubleOuterCoords[ centerY2 ] ) + ',' +
( baseAngle - 45 ) * deg + ',' + 45 * deg +
'ae' + doubleOuterCoords[ centerX1 ] + ',' + doubleOuterCoords[ centerY1 ] + ',' +
abs( doubleOuterCoords[ outerX1 ] - doubleOuterCoords[ centerX1 ] ) + ',' +
abs( doubleOuterCoords[ outerY1 ] - doubleOuterCoords[ centerY1 ] ) + ',' +
baseAngle * deg + ',' + 45 * deg +
'x' +
'al' + doubleInnerCoords[ centerX1 ] + ',' + doubleInnerCoords[ centerY1 ] + ',' +
abs( doubleInnerCoords[ outerX1 ] - doubleInnerCoords[ centerX1 ] ) + ',' +
abs( doubleInnerCoords[ outerY1 ] - doubleInnerCoords[ centerY1 ] ) + ',' +
( baseAngle + 45 ) * deg + ',' + -45 * deg +
'ae' + doubleInnerCoords[ centerX2 ] + ',' + doubleInnerCoords[ centerY2 ] + ',' +
abs( doubleInnerCoords[ outerX2 ] - doubleInnerCoords[ centerX2 ] ) + ',' +
abs( doubleInnerCoords[ outerY2 ] - doubleInnerCoords[ centerY2 ] ) + ',' +
baseAngle * deg + ',' + -45 * deg
);
}
// Inner edge
path.push(
'ae' + innerCoords[ centerX2 ] + ',' + innerCoords[ centerY2 ] + ',' +
abs( innerCoords[ outerX2 ] - innerCoords[ centerX2 ] ) + ',' +
abs( innerCoords[ outerY2 ] - innerCoords[ centerY2 ] ) + ',' +
( baseAngle - 45 ) * deg + ',' + 45 * deg +
'ae' + innerCoords[ centerX1 ] + ',' + innerCoords[ centerY1 ] + ',' +
abs( innerCoords[ outerX1 ] - innerCoords[ centerX1 ] ) + ',' +
abs( innerCoords[ outerY1 ] - innerCoords[ centerY1 ] ) + ',' +
baseAngle * deg + ',' + 45 * deg +
'x'
);
// For dashed/dotted styles, add the dash cutout sub-paths
if ( style in dashedStyles ) {
side === 't' ?
me.dashify( path, innerCoords[ 2 ], innerCoords[ 4 ], wT * mult, 0, 0, style ) :
side === 'r' ?
me.dashify( path, innerCoords[ 7 ], innerCoords[ 9 ], wR * mult, ( bounds.w - wR ) * mult, 1, style ) :
side === 'b' ?
me.dashify( path, innerCoords[ 12 ], innerCoords[ 10 ], wB * mult, ( bounds.h - wB ) * mult, 0, style ) :
//side === 'l' ?
me.dashify( path, innerCoords[ 1 ], innerCoords[ 15 ], wL * mult, 0, 1, style );
}
segmentsInfo.push( path.join( '' ), colors[ side ] );
path.length = 0; //reuse same array for next loop
}
}
}
}
return segmentsInfo;
},
destroy: function() {
var me = this;
if (me.finalized || !me.styleInfos.borderImageInfo.isActive()) {
me.targetElement.runtimeStyle.borderColor = '';
}
PIE.RendererBase.destroy.call( me );
}
} );