diff --git a/lib/buffer.js b/lib/buffer.js new file mode 100644 index 0000000..6b5fb20 --- /dev/null +++ b/lib/buffer.js @@ -0,0 +1,22 @@ + +/*! + * Canvas - Buffer + * Copyright (c) 2010 LearnBoost + * MIT Licensed + */ + +/** + * Concatenate `this` Buffer with `other`. + * + * @param {Buffer} other + * @return {Buffer} + * @api public + */ + +Buffer.prototype.concat = function(other) { + var len = this.length + , buf = new Buffer(len + other.length); + this.copy(buf, 0, 0); + other.copy(buf, len, 0); + return buf; +}; \ No newline at end of file diff --git a/lib/canvas.js b/lib/canvas.js index c4884f4..dc5a656 100644 --- a/lib/canvas.js +++ b/lib/canvas.js @@ -15,8 +15,7 @@ var canvas = require('../build/default/canvas') , Context2d = canvas.CanvasRenderingContext2d , CanvasGradient = canvas.CanvasGradient , cairoVersion = canvas.cairoVersion - , PNGStream = require('./pngstream') - , utils = require('./utils'); + , PNGStream = require('./pngstream'); /** * Export `Canvas` as the module. @@ -42,6 +41,12 @@ exports.cairoVersion = cairoVersion; var cache = {}; +/** + * Buffer extensions. + */ + +require('./buffer'); + /** * Return a function used to normalize an RGBA color `prop`. * @@ -188,16 +193,16 @@ Canvas.prototype.createSyncPNGStream = function(){ */ Canvas.prototype.toBuffer = function(){ - var bufs = [] - , lens = []; + var buf; this.streamPNGSync(function(err, chunk, len){ if (err) throw err; if (len) { - bufs.push(chunk); - lens.push(len); - }; + buf = buf + ? buf.concat(chunk) + : chunk; + } }); - return utils.concatBuffers(bufs, lens); + return buf; }; /** diff --git a/lib/utils.js b/lib/utils.js deleted file mode 100644 index bed53ff..0000000 --- a/lib/utils.js +++ /dev/null @@ -1,36 +0,0 @@ - -/*! - * Canvas - utils - * Copyright (c) 2010 LearnBoost - * MIT Licensed - */ - -/** - * Concatenate `bufs` / `lens`. - * - * @param {Array} bufs - * @param {Array} lens - * @return {Buffer} - * @api private - */ - -exports.concatBuffers = function(bufs, lens) { - var buf - , length = 0 - , offset = 0; - - // Determine length - for (var i = 0, len = lens.length; i < len; ++i) { - length += lens[i]; - } - - // Allocate buffer - buf = new Buffer(length); - - // Copy - for (var i = 0, len = bufs.length; i < len; ++i) { - bufs[i].copy(buf, offset); - offset += lens[i]; - } - return buf; -}; \ No newline at end of file