Skip to content

Commit

Permalink
Cache the outputEntry sizes and the line sizes
Browse files Browse the repository at this point in the history
  • Loading branch information
sunesimonsen committed Sep 10, 2015
1 parent 9952fb3 commit a31b0fd
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,41 @@ var utils = {
},

calculateOutputEntrySize: function (outputEntry) {
if (outputEntry.size) {
return outputEntry.size;
}

var size;
switch (outputEntry.style) {
case 'text':
return { width: String(outputEntry.args.content).length, height: 1 };
size = { width: String(outputEntry.args.content).length, height: 1 };
break;
case 'block':
return utils.calculateSize(outputEntry.args);
size = utils.calculateSize(outputEntry.args);
break;
case 'raw':
var arg = outputEntry.args;
return { width: arg.width, height: arg.height };
default: return { width: 0, height: 0 };
size = { width: arg.width, height: arg.height };
break;
default: size = { width: 0, height: 0 };
}

outputEntry.size = size;
return size;
},

calculateLineSize: function (line) {
if (line.size) {
return line.size;
}

var size = { height: 1, width: 0 };
line.forEach(function (outputEntry) {
var outputEntrySize = utils.calculateOutputEntrySize(outputEntry);
size.width += outputEntrySize.width;
size.height = Math.max(outputEntrySize.height, size.height);
});
line.size = size;
return size;
},

Expand Down

0 comments on commit a31b0fd

Please sign in to comment.