diff --git a/bin/psd.js b/bin/psd.js index 0abe0f7..cf98397 100755 --- a/bin/psd.js +++ b/bin/psd.js @@ -16,6 +16,7 @@ program .version(require('../package.json').version) .arguments('') .option('-c, --convert', 'Convert to PNG file named .png') + .option('-l, --layer', 'Convert layer to PNG file named /.png') .option('-t, --text', 'Extract text content to .txt') .option('-o, --open', 'Preview file after conversion (triggers -c option)') .action(processFiles) @@ -39,11 +40,37 @@ function convertFile(filepath, psdPromise, cb) { }); } +function extractLayerFromFile(filepath, psdPromise, cb) { + var filedir = filepath.replace(/\.psd$/, ''); + var start = new Date(); + if (!fs.existsSync(filedir)){ + fs.mkdirSync(filedir); + } + + psdPromise.then(function(psd) { + psd.tree().descendants().forEach(function (node) { + if ( node.isGroup() ) return true; + if (typeof node.text !== 'undefined' ) return true; + if ( node.visible() ) { + node.saveAsPng(filedir+"/" + node.name.replace(/[^a-z0-9]/gi, '_').replace(/(_{2,})/gi, '_')+ ".png").catch(function (err) { + console.log(err.stack); + }); + } + }); + }).then(function () { + console.log("Finished in " + ((new Date()) - start) + "ms"); + }).catch(function (err) { + console.log(err.stack); + }); +} + // extract text from PSD file function extractTextFromFile(filepath, psdPromise, cb) { var fileText = filepath.replace(/\.psd$/, '.txt'); var fileString = ''; - + var summarizeFonts = {}; + var summarizeFontsColor = {}; + var summarizeFontsSizes = {}; psdPromise.then(function(psd) { psd.tree().export().children.forEach(function(child) { @@ -55,8 +82,16 @@ function extractTextFromFile(filepath, psdPromise, cb) { fileString += '\n' + t.path.join(' > '); fileString += '\n' + '---'; fileString += '\n\n' + t.text.replace(/\r/g, '\n'); + fileString += '\n\n' + t.fontinfo.replace(/\r/g, '\n'); + summarizeFonts[t.font.name] = t.font.name; + summarizeFontsColor[t.font.colors] = t.font.colors; + summarizeFontsSizes[t.font.sizes] = t.font.sizes; }); }); + fileString += '\n\n' + '===== SUMMARY ====='; + fileString += '\n' + 'FONTS: '+Object.keys(summarizeFonts).join(", "); + fileString += '\n' + 'FONTS COLORS: '+Object.keys(summarizeFontsColor).join(" "); + fileString += '\n' + 'FONTS SIZES: '+Object.keys(summarizeFontsSizes).join(" "); fs.writeFile(fileText, fileString, function(err) { if (err) { @@ -98,6 +133,14 @@ function processFiles(files, env) { convertFile(filepath, psdPromise, cb); }); } + + // convert layer to PNG + if (program.layer) { + asyncTasks.push(function(cb) { + extractLayerFromFile(filepath, psdPromise, cb); + }); + } + // extract text data if (program.text) { asyncTasks.push(function(cb) { @@ -146,23 +189,43 @@ function getCommandLine() { } + function PSDLayer(path, element) { this.path = path.slice(); this.path.push(element.name); var self = this; + var convertPtToPx= function (pt) { + var px = pt * 96 / 72; + return Math.round((px*100))*0.01; + }; return { extractText: function() { var text = []; if (typeof element.text !== 'undefined' && element.text !== undefined) { + +/* +samples: +{"name":"Novecentosanswide-Bold","sizes":[12.5,12.5,12.5,12.5],"colors":[[19,62,89,255],[19,62,89,255],[19,62,89,255],[19,62,89,255]],"alignment":["center","center","center","center"]} +*/ + var f = element.text.font; + font = {}; + font.name = f.name ; + font.sizes = convertPtToPx(f.sizes[0]); + font.colors = 'rgba('+f.colors[0].join(', ')+')'; + font.alignment = f.alignment[0]; + text.push({ path: self.path, text: element.text.value || null, + font: font, + fontinfo: JSON.stringify(font) || null }); + } - + if (typeof(element.children) !== 'undefined') { element.children.forEach(function(child) { var layer = new PSDLayer(self.path, child);