From fe537b41f21e568aab984f711887467ca3254b4d Mon Sep 17 00:00:00 2001 From: Michael Aschauer Date: Wed, 25 Jan 2017 12:47:57 +0100 Subject: [PATCH] add Tajima/DST support (thanks Peter Barnes) --- stitchcode/gui.js | 29 +++++- stitchcode/lang-de.js | 12 ++- stitchcode/turtleShepherd.js | 177 ++++++++++++++++++++++++++++++++++- 3 files changed, 208 insertions(+), 10 deletions(-) diff --git a/stitchcode/gui.js b/stitchcode/gui.js index de9a0775..e7889758 100644 --- a/stitchcode/gui.js +++ b/stitchcode/gui.js @@ -812,12 +812,18 @@ IDE_Morph.prototype.createStatusDisplay = function () { var downloadEXPButton = new PushButtonMorph( null, function () { myself.downloadEXP(); }, - 'Export as EXP' + 'Export as Melco/EXP' ); - downloadEXPButton.newLines = 1; + downloadEXPButton.newLines = 1.7; elements.push(downloadEXPButton); - + var downloadDSTButton = new PushButtonMorph( + null, + function () { myself.downloadDST(); }, + 'Export as Tajima/DST' + ); + downloadDSTButton.newLines = 1.7; + elements.push(downloadDSTButton); elements.forEach(function(each) { myself.statusDisplay.addElement(each); }); }; @@ -906,6 +912,13 @@ IDE_Morph.prototype.downloadEXP = function() { saveAs(blob, (this.projectName ? this.projectName : 'turtlestitch') + '.exp'); }; +// DST export +IDE_Morph.prototype.downloadDST = function() { + expUintArr = this.stage.turtleShepherd.toDST(); + blob = new Blob([expUintArr], {type: 'application/octet-stream'}); + saveAs(blob, (this.projectName ? this.projectName : 'turtlestitch') + '.dst'); +}; + IDE_Morph.prototype.saveToDisk = function() { var myself = this; if (!this.projectName) { @@ -1442,9 +1455,15 @@ IDE_Morph.prototype.projectMenu = function () { 'Export current drawing as SVG Vector file' ); menu.addItem( - 'Export as EXP', + 'Export as Melco/EXP', function() { myself.downloadEXP(); }, - 'Export current drawing as EXP Embroidery file' + 'Export current drawing as EXP/Melco Embroidery file' + ); + + menu.addItem( + 'Export as Tajima/DST', + function() { myself.downloadDST(); }, + 'Export current drawing as DST/Tajima Embroidery file' ); /* menu.addLine(); diff --git a/stitchcode/lang-de.js b/stitchcode/lang-de.js index 9c665da5..ee725de9 100644 --- a/stitchcode/lang-de.js +++ b/stitchcode/lang-de.js @@ -30,12 +30,16 @@ tempDict = { 'l\u00f6schen', 'Export as SVG': 'Zeichnung als SVG exportieren', - 'Export as EXP': - 'Zeichnung als EXP exportieren', + 'Export as Melco/EXP': + 'Zeichnung als Melco/EXP exportieren', 'Export current drawing as SVG Vector file': 'Exportiert die aktuelle Zeichnung als Vektorgrafik im SVG Format', - 'Export current drawing as EXP Embroidery file': - 'Exportiert die aktuelle Zeichnung als Stickmuster im EXP Format', + 'Export current drawing as EXP/Melco Embroidery file': + 'Exportiert die aktuelle Zeichnung als Stickmuster im EXP/Melco Format', + 'Export as Tajima/DST': + 'Zeichnung als Tajima/DST exportieren', + 'Export current drawing as DST/Tajima Embroidery file': + 'Exportiert die aktuelle Zeichnung als Stickmuster im Tajima/DST Format' }; // Add attributes to original SnapTranslator.dict.de diff --git a/stitchcode/turtleShepherd.js b/stitchcode/turtleShepherd.js index b5946ec5..52e10ca8 100644 --- a/stitchcode/turtleShepherd.js +++ b/stitchcode/turtleShepherd.js @@ -187,7 +187,7 @@ TurtleShepherd.prototype.toSVG = function() { return svgStr; }; -TurtleShepherd.prototype.toEXP= function() { +TurtleShepherd.prototype.toEXP = function() { var expArr = []; pixels_per_millimeter = 5; scale = 10 / pixels_per_millimeter; @@ -243,6 +243,181 @@ TurtleShepherd.prototype.toEXP= function() { return expUintArr; }; + +TurtleShepherd.prototype.toDST = function() { + var expArr = []; + + pixels_per_millimeter = 5; + scale = 10 / pixels_per_millimeter; + + function encodeTajimaStitch(dx, dy, jump) { + b1 = 0; + b2 = 0; + b3 = 0; + + if (dx > 40) { + b3 |= 0x04; + dx -= 81; + } + + if (dx < -40) { + b3 |= 0x08; + dx += 81; + } + + if (dy > 40) { + b3 |= 0x20; + dy -= 81; + } + + if (dy < -40) { + b3 |= 0x10; + dy += 81; + } + + if (dx > 13) { + b2 |= 0x04; + dx -= 27; + } + + if (dx < -13) { + b2 |= 0x08; + dx += 27; + } + + if (dy > 13) { + b2 |= 0x20; + dy -= 27; + } + + if (dy < -13) { + b2 |= 0x10; + dy += 27; + } + + if (dx > 4) { + b1 |= 0x04; + dx -= 9; + } + + if (dx < -4) { + b1 |= 0x08; + dx += 9; + } + + if (dy > 4) { + b1 |= 0x20; + dy -= 9; + } + + if (dy < -4) { + b1 |= 0x10; + dy += 9; + } + + if (dx > 1) { + b2 |= 0x01; + dx -= 3; + } + + if (dx < -1) { + b2 |= 0x02; + dx += 3; + } + + if (dy > 1) { + b2 |= 0x80; + dy -= 3; + } + + if (dy < -1) { + b2 |= 0x40; + dy += 3; + } + + if (dx > 0) { + b1 |= 0x01; + dx -= 1; + } + + if (dx < 0) { + b1 |= 0x02; + dx += 1; + } + + if (dy > 0) { + b1 |= 0x80; + dy -= 1; + } + + if (dy < 0) { + b1 |= 0x40; + dy += 1; + } + + expArr.push(b1); + expArr.push(b2); + if (jump) { + expArr.push(b3 | 0x83); + } else { + expArr.push(b3 | 0x03); + } + } + + // Print empty header + for (var i=0; i<512; i++) { + expArr.push(0x00); + } + + for (i=1; i< this.cache.length; i++) { + x1 = Math.round(this.cache[i].x * scale); + y1 = Math.round(this.cache[i].y * scale); + x0 = Math.round(this.cache[i-1].x * scale); + y0 = Math.round(this.cache[i-1].y * scale); + + sum_x = 0; + sum_y = 0; + dmax = Math.max(Math.abs(x1 - x0), Math.abs(y1 - y0)); + dsteps = Math.abs(dmax / 127) + 1; + if (dsteps == 1) { + encodeTajimaStitch((x1 - x0), (y1 - y0), + !this.cache[i].penDown); + } else { + for(j=0;j