Skip to content

Commit

Permalink
Fixed several issues of the paths parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
bjornd committed Dec 2, 2012
1 parent b77d99a commit 14302bf
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
2 changes: 1 addition & 1 deletion index.html
Expand Up @@ -13,7 +13,7 @@
<script src="js/app.js"></script> <script src="js/app.js"></script>
<script> <script>
$(function(){ $(function(){
$.get('map-louvre.svg', function(response){ $.get('map-sample.svg', function(response){
$('#input-source').val(response); $('#input-source').val(response);
}, 'text'); }, 'text');
app(); app();
Expand Down
22 changes: 20 additions & 2 deletions js/app.js
Expand Up @@ -80,15 +80,31 @@ var app = (function(){
SvgUtils.applyTransformToPath = function(path, matrix){ SvgUtils.applyTransformToPath = function(path, matrix){
var re = /([MmLlHhVvCcSsZz])([^MmLlHhVvCcSsZz]*)/g, var re = /([MmLlHhVvCcSsZz])([^MmLlHhVvCcSsZz]*)/g,
coords, coords,
coordsTmp,
i, i,
cmdIndex = 0, cmdIndex = 0,
m = matrix.m, m = matrix.m,
tPath = '', tPath = '',
point; point,
relativeMatrix = new Matrix([[m[0][0], m[0][1], 0], [m[1][0], m[1][1], 0], [0, 0, 1]]); relativeMatrix = new Matrix([[m[0][0], m[0][1], 0], [m[1][0], m[1][1], 0], [0, 0, 1]]);


//prepare path to parse it with regexp easily
path = path.replace(/(\d)-/g, '$1,-')
while ((match = re.exec(path)) !== null) { while ((match = re.exec(path)) !== null) {
coords = $.trim(match[2]).split(/[, ]+/g); coords = $.trim(match[2]).split(/[, ]+/g);
if (match[1].toLowerCase() === 'h') {
coordsTmp = [];
for (i = 0; i < coords.length; i++) {
coordsTmp.push(coords[i], 0);
}
coords = coordsTmp;
} else if (match[1].toLowerCase() === 'v') {
coordsTmp = [];
for (i = 0; i < coords.length; i++) {
coordsTmp.push(0, coords[i]);
}
coords = coordsTmp;
}
tCoords = []; tCoords = [];
if (coords.length >= 2) { if (coords.length >= 2) {
for (i = 0; i < coords.length; i += 2) { for (i = 0; i < coords.length; i += 2) {
Expand Down Expand Up @@ -188,7 +204,9 @@ var app = (function(){
i; i;


$(this).parents().add(this).each(function(){ $(this).parents().add(this).each(function(){
fullTransform += ' '+$(this).attr('transform'); if ($(this).attr('transform')) {
fullTransform += ' '+$(this).attr('transform');
}
}); });
if (this.tagName.toLowerCase() == 'polygon') { if (this.tagName.toLowerCase() == 'polygon') {
points = $.trim( $(this).attr('points') ).split(/[\s,]+/); points = $.trim( $(this).attr('points') ).split(/[\s,]+/);
Expand Down

0 comments on commit 14302bf

Please sign in to comment.