Browse Source

Added sync PNGStream support

v1.x
Tj Holowaychuk 14 years ago
parent
commit
3e6b1cc882
  1. 13
      lib/canvas.js
  2. 11
      lib/pngstream.js
  3. 6
      src/canvas.cc
  4. 2
      src/canvas.h
  5. 2
      test/canvas.test.js

13
lib/canvas.js

@ -174,6 +174,17 @@ Canvas.prototype.createPNGStream = function(){
return new PNGStream(this);
};
/**
* Create a synchronous `PNGStream` for `this` canvas.
*
* @return {PNGStream}
* @api public
*/
Canvas.prototype.createSyncPNGStream = function(){
return new PNGStream(this, true);
};
/**
* Return a `Buffer` instance consisting of the PNG image data.
*
@ -183,7 +194,7 @@ Canvas.prototype.createPNGStream = function(){
Canvas.prototype.toBuffer = function(){
var buf;
this.streamPNG(function(err, chunk, len){
this.streamPNGSync(function(err, chunk, len){
if (err) throw err;
if (len) {
buf = buf

11
lib/pngstream.js

@ -30,14 +30,19 @@ var EventEmitter = require('events').EventEmitter;
* });
*
* @param {Canvas} canvas
* @param {Boolean} sync
* @api public
*/
var PNGStream = module.exports = function PNGStream(canvas) {
var self = this;
var PNGStream = module.exports = function PNGStream(canvas, sync) {
var self = this
, method = sync
? 'streamPNGSync'
: 'streamPNG';
this.sync = sync;
this.canvas = canvas;
process.nextTick(function(){
canvas.streamPNG(function(err, chunk, len){
canvas[method](function(err, chunk, len){
if (err) {
self.emit('error', err);
} else if (len) {

6
src/canvas.cc

@ -31,7 +31,7 @@ Canvas::Initialize(Handle<Object> target) {
t->InstanceTemplate()->SetInternalFieldCount(1);
t->SetClassName(String::NewSymbol("Canvas"));
NODE_SET_PROTOTYPE_METHOD(t, "streamPNG", StreamPNG);
NODE_SET_PROTOTYPE_METHOD(t, "streamPNGSync", StreamPNGSync);
NODE_SET_PROTOTYPE_METHOD(t, "savePNG", SavePNG);
target->Set(String::NewSymbol("Canvas"), t->GetFunction());
}
@ -71,11 +71,11 @@ writeToBuffer(void *c, const uint8_t *data, unsigned len) {
}
/*
* Return a node Buffer containing PNG data.
* Stream PNG data synchronously.
*/
Handle<Value>
Canvas::StreamPNG(const Arguments &args) {
Canvas::StreamPNGSync(const Arguments &args) {
HandleScope scope;
// TODO: async
if (!args[0]->IsFunction())

2
src/canvas.h

@ -51,7 +51,7 @@ class Canvas: public node::ObjectWrap {
static void Initialize(Handle<Object> target);
static Handle<Value> New(const Arguments &args);
static Handle<Value> SavePNG(const Arguments &args);
static Handle<Value> StreamPNG(const Arguments &args);
static Handle<Value> StreamPNGSync(const Arguments &args);
static Handle<Value> Error(cairo_status_t status);
inline cairo_surface_t *getSurface(){ return _surface; }
static void blur(cairo_surface_t *surface, int radius);

2
test/canvas.test.js

@ -630,7 +630,7 @@ module.exports = {
}
var out = fs.createWriteStream(path)
, stream = canvas.createPNGStream();
, stream = canvas.createSyncPNGStream();
out.on('close', function(){
assertChecksumOf(

Loading…
Cancel
Save