summaryrefslogtreecommitdiffstats
path: root/dist/svg.js
diff options
context:
space:
mode:
authorUlrich-Matthias Schäfer <ulima.ums@googlemail.com>2017-03-27 19:19:38 +0200
committerUlrich-Matthias Schäfer <ulima.ums@googlemail.com>2017-03-27 19:19:38 +0200
commitebf7436261c8fc3197a48dc6b21dff9edd9271e3 (patch)
treebbd97ed3092819cf778acefafc76ed3bb155ab5c /dist/svg.js
parent06c2f7b06e15c6e0a12005febf07c8026cf06ac7 (diff)
downloadsvg.js-ebf7436261c8fc3197a48dc6b21dff9edd9271e3.tar.gz
svg.js-ebf7436261c8fc3197a48dc6b21dff9edd9271e3.zip
make path parsing even faster
Diffstat (limited to 'dist/svg.js')
-rw-r--r--dist/svg.js171
1 files changed, 91 insertions, 80 deletions
diff --git a/dist/svg.js b/dist/svg.js
index a480d3a..39b21f7 100644
--- a/dist/svg.js
+++ b/dist/svg.js
@@ -6,7 +6,7 @@
* @copyright Wout Fierens <wout@mick-wout.com>
* @license MIT
*
-* BUILT: Mon Mar 27 2017 13:30:36 GMT+0200 (Mitteleuropäische Sommerzeit)
+* BUILT: Mon Mar 27 2017 19:11:54 GMT+0200 (Mitteleuropäische Sommerzeit)
*/;
(function(root, factory) {
if (typeof define === 'function' && define.amd) {
@@ -607,6 +607,77 @@ SVG.extend(SVG.PointArray, {
}
})
+var pathHandlers = {
+ M: function(c, p, p0) {
+ p.x = p0.x = c[0]
+ p.y = p0.y = c[1]
+
+ return ['M', p.x, p.y]
+ },
+ L: function(c, p) {
+ p.x = c[0]
+ p.y = c[1]
+ return ['L', c[0], c[1]]
+ },
+ H: function(c, p) {
+ p.x = c[0]
+ return ['H', c[0]]
+ },
+ V: function(c, p) {
+ p.y = c[0]
+ return ['V', c[0]]
+ },
+ C: function(c, p) {
+ p.x = c[4]
+ p.y = c[5]
+ return ['C', c[0], c[1], c[2], c[3], c[4], c[5]]
+ },
+ S: function(c, p) {
+ p.x = c[2]
+ p.y = c[3]
+ return ['S', c[0], c[1], c[2], c[3]]
+ },
+ Q: function(c, p) {
+ p.x = c[2]
+ p.y = c[3]
+ return ['Q', c[0], c[1], c[2], c[3]]
+ },
+ T: function(c, p) {
+ p.x = c[0]
+ p.y = c[1]
+ return ['T', c[0], c[1]]
+ },
+ Z: function(c, p, p0) {
+ p.x = p0.x
+ p.y = p0.y
+ return ['Z']
+ },
+ A: function(c, p) {
+ p.x = c[5]
+ p.y = c[6]
+ return ['A', c[0], c[1], c[2], c[3], c[4], c[5], c[6]]
+ }
+}
+
+var mlhvqtcsa = 'mlhvqtcsaz'.split('')
+
+for(var i = 0, il = mlhvqtcsa.length; i < il; ++i){
+ pathHandlers[mlhvqtcsa[i]] = (function(i){return function(c, p, p0) {
+ if(i == 'H') c[0] = c[0] + p.x
+ else if(i == 'V') c[0] = c[0] + p.y
+ else if(i == 'A'){
+ c[5] = c[5] + p.x,
+ c[6] = c[6] + p.y
+ }
+ else
+ for(var j = 0, jl = c.length; j < jl; ++j) {
+ c[j] = c[j] + (j%2 ? p.y : p.x)
+ }
+
+ return pathHandlers[i](c, p, p0)
+ }})(mlhvqtcsa[i].toUpperCase())
+}
+
// Path points array
SVG.PathArray = function(array, fallback) {
SVG.Array.call(this, array, fallback || [['M', 0, 0]])
@@ -794,15 +865,17 @@ SVG.extend(SVG.PathArray, {
}
// array now is an array containing all parts of a path e.g. ['M', '0', '0', 'L', '30', '30' ...]
-
var arr = []
+ , p = new SVG.Point()
+ , p0 = new SVG.Point()
+ , index = 0
+ , len = array.length
do{
-
// Test if we have a path letter
- if(SVG.regex.isPathLetter.test(array[0])){
- s = array[0]
- array.shift()
+ if(SVG.regex.isPathLetter.test(array[index])){
+ s = array[index]
+ ++index
// If last letter was a move command and we got no new, it defaults to [L]ine
}else if(s == 'M'){
s = 'L'
@@ -810,80 +883,13 @@ SVG.extend(SVG.PathArray, {
s = 'l'
}
- // add path letter as first element
- seg = [s.toUpperCase()]
-
- // push all necessary parameters to segment
- for(i = 0; i < paramCnt[seg[0]]; ++i){
- seg.push(parseFloat(array.shift()))
- }
-
- // upper case
- if(s == seg[0]){
-
- if(s == 'M' || s == 'L' || s == 'C' || s == 'Q' || s == 'S' || s == 'T'){
- x = seg[paramCnt[seg[0]]-1]
- y = seg[paramCnt[seg[0]]]
- }else if(s == 'V'){
- y = seg[1]
- }else if(s == 'H'){
- x = seg[1]
- }else if(s == 'A'){
- x = seg[6]
- y = seg[7]
- }
-
- // lower case
- }else{
-
- // convert relative to absolute values
- if(s == 'm' || s == 'l' || s == 'c' || s == 's' || s == 'q' || s == 't'){
-
- seg[1] += x
- seg[2] += y
-
- if(seg[3] != null){
- seg[3] += x
- seg[4] += y
- }
-
- if(seg[5] != null){
- seg[5] += x
- seg[6] += y
- }
-
- // move pointer
- x = seg[paramCnt[seg[0]]-1]
- y = seg[paramCnt[seg[0]]]
-
- }else if(s == 'v'){
- seg[1] += y
- y = seg[1]
- }else if(s == 'h'){
- seg[1] += x
- x = seg[1]
- }else if(s == 'a'){
- seg[6] += x
- seg[7] += y
- x = seg[6]
- y = seg[7]
- }
-
- }
-
- if(seg[0] == 'M'){
- x0 = x
- y0 = y
- }
-
- if(seg[0] == 'Z'){
- x = x0
- y = y0
- }
-
- arr.push(seg)
+ arr.push(pathHandlers[s].call(null,
+ array.slice(index, (index = index + paramCnt[s.toUpperCase()])).map(parseFloat),
+ p, p0
+ )
+ )
- }while(array.length)
+ }while(len > index)
return arr
@@ -2591,7 +2597,12 @@ SVG.Point = SVG.invent({
, transform: function(matrix) {
return new SVG.Point(this.native().matrixTransform(matrix.native()))
}
-
+ , reflectAt: function(p) {
+ return new SVG.Point(
+ p.x + p.x - this.x,
+ p.y + p.y - this.y
+ )
+ }
}
})