Skip to content

Commit

Permalink
Moved path splitting into ART.SVG.Text.
Browse files Browse the repository at this point in the history
Uses the visitor pattern for simpler code.
Not common enough to warrant an ART.Path API for it.
  • Loading branch information
sebmarkbage committed Jan 19, 2011
1 parent 499a682 commit 670d635
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 38 deletions.
36 changes: 1 addition & 35 deletions Source/ART.Path.js
Expand Up @@ -231,42 +231,8 @@ ART.Path = new Class({
return this.push('z');
},

/* split each continuous line into individual paths */

splitContinuous: function(){
var parts = this.path, newPaths = [], path = new ART.Path();

var X = 0, Y = 0, inX, inY;
for (var i = 0, k = parts.length; i < k; i++){
var v = parts[i], f = v[0], l = f.toLowerCase();

if (l != 'm' && inX == null){ inX = X; inY = Y; }

if (l != f){ X = 0; Y = 0; }

if (l == 'm' || l == 'l' || l == 't'){ X += v[1]; Y += v[2]; }
else if (l == 'c'){ X += v[5]; Y += v[6]; }
else if (l == 's' || l == 'q'){ X += v[3]; Y += v[4]; }
else if (l == 'a'){ X += v[6]; Y += v[7]; }
else if (l == 'h'){ X += v[1]; }
else if (l == 'v'){ Y += v[1]; }
else if (l == 'z' && inX != null){
X = inX; Y = inY;
inX = null;
}
/* visitor */

if (path.path.length > 0 && (l == 'm' || l == 'z')){
newPaths.push(path);
path = new ART.Path().push('M', X, Y);
} else {
path.path.push(v);
}
}

newPaths.push(path);
return newPaths;
},

visit: function(lineTo, curveTo, arcTo, moveTo, close){
var reflect = function(sx, sy, ex, ey){
return [ex * 2 - sx, ey * 2 - sy];
Expand Down
27 changes: 24 additions & 3 deletions Source/ART.SVG.js
Expand Up @@ -406,6 +406,23 @@ ART.SVG.Image = new Class({
var fontAnchors = { left: 'start', center: 'middle', right: 'end' },
fontAnchorOffsets = { middle: '50%', end: '100%' };

/* split each continuous line into individual paths */

var splitPaths, splitPath;

function splitMove(sx, sy, x, y){
if (splitPath.length > 4) splitPaths.push(splitPath);
splitPath = ['M', x, ',', y];
};

function splitLine(sx, sy, x, y){
splitPath.push('L', x, ',', y);
};

function splitCurve(sx, sy, p1x, p1y, p2x, p2y, x, y){
splitPath.push('C', p1x, ',', p1y, ',', p2x, ',', p2y, ',', x, ',', y);
};

ART.SVG.Text = new Class({

Extends: ART.SVG.Base,
Expand Down Expand Up @@ -539,11 +556,15 @@ ART.SVG.Text = new Class({
_createPaths: function(path){
this._ejectPaths();
var id = 'p' + String.uniqueID() + '-';
var paths = path.splitContinuous();

splitPaths = []; splitPath = ['M', 0, ',', 0];
path.visit(splitLine, splitCurve, null, splitMove);
splitPaths.push(splitPath);

var result = [];
for (var i = 0, l = paths.length; i < l; i++){
for (var i = 0, l = splitPaths.length; i < l; i++){
var p = createElement('path');
p.setAttribute('d', paths[i].toSVG());
p.setAttribute('d', splitPaths[i].join(''));
p.setAttribute('id', id + i);
result.push(p);
}
Expand Down

0 comments on commit 670d635

Please sign in to comment.