Browse Source

Reverted buffer concat util

v1.x
Tj Holowaychuk 14 years ago
parent
commit
681e52e0cb
  1. 22
      lib/buffer.js
  2. 21
      lib/canvas.js
  3. 36
      lib/utils.js

22
lib/buffer.js

@ -0,0 +1,22 @@
/*!
* Canvas - Buffer
* Copyright (c) 2010 LearnBoost <tj@learnboost.com>
* 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;
};

21
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;
};
/**

36
lib/utils.js

@ -1,36 +0,0 @@
/*!
* Canvas - utils
* Copyright (c) 2010 LearnBoost <tj@learnboost.com>
* 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;
};
Loading…
Cancel
Save