From a31b0fd7b6365a54b84dbb848dcf5055d677af6e Mon Sep 17 00:00:00 2001 From: Sune Simonsen Date: Thu, 10 Sep 2015 22:58:52 +0200 Subject: [PATCH] Cache the outputEntry sizes and the line sizes --- lib/utils.js | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/lib/utils.js b/lib/utils.js index 26481e6..698895b 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -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; },