Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
node_modules
sample
.eslintrc.json
32 changes: 29 additions & 3 deletions bin/psd.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ program
.arguments('<file...>')
.option('-c, --convert', 'Convert to PNG file named <FILENAME>.png')
.option('-t, --text', 'Extract text content to <FILENAME>.txt')
.option('-f, --font', 'Extract font information to <FILENAME>.txt (triggers -t option)')
.option('-o, --open', 'Preview file after conversion (triggers -c option)')
.action(processFiles)
.parse(process.argv);
Expand All @@ -40,7 +41,7 @@ function convertFile(filepath, psdPromise, cb) {
}

// extract text from PSD file
function extractTextFromFile(filepath, psdPromise, cb) {
function extractTextFromFile(filepath, psdPromise, cb, includeFont) {
var fileText = filepath.replace(/\.psd$/, '.txt');
var fileString = '';

Expand All @@ -56,6 +57,13 @@ function extractTextFromFile(filepath, psdPromise, cb) {
}
fileString += '\n\n' + '---';
fileString += '\n' + t.path.join(' > ');
if (includeFont) {
fileString += '\n\nFont Family: ' + t.font.family;
fileString += '\nFont Sizes: ' + t.font.sizes.map(function(size) {
return size + 'px';
}).join(', ');
fileString += '\nFont Colors: ' + t.font.colors.join(', ');
}
fileString += '\n' + '---';
fileString += '\n\n' + t.text.replace(/\r/g, '\n');
});
Expand Down Expand Up @@ -102,9 +110,9 @@ function processFiles(files, env) {
});
}
// extract text data
if (program.text) {
if (program.text || program.font) {
asyncTasks.push(function(cb) {
extractTextFromFile(filepath, psdPromise, cb);
extractTextFromFile(filepath, psdPromise, cb, program.font);
});
}

Expand Down Expand Up @@ -160,7 +168,25 @@ function PSDLayer(path, element) {
var text = [];

if (typeof element.text !== 'undefined' && element.text !== undefined) {

var colors = element.text.font.colors;

for (var i = 0; i < colors.length; i++) {
for (var j = 0; j < colors[i].length; j++) {
colors[i][j] = Number(colors[i][j]).toString(16);
if (colors[i][j].length < 2) {
colors[i][j] = '0' + colors[i][j];
}
}
colors[i] = '#' + colors[i].join('');
}

text.push({
font: {
colors: colors,
family: element.text.font.name || null,
sizes: element.text.font.sizes,
},
path: self.path,
text: element.text.value || null,
});
Expand Down