Skip to content

Allow a format and explicit Buffer as backing store #1172

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: v1.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 56 additions & 7 deletions src/Canvas.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,14 @@ Canvas::Initialize(Nan::ADDON_REGISTER_FUNCTION_ARGS_TYPE target) {
// Prototype
Local<ObjectTemplate> proto = ctor->PrototypeTemplate();
Nan::SetPrototypeMethod(ctor, "toBuffer", ToBuffer);
Nan::SetPrototypeMethod(ctor, "flush", Flush);
Nan::SetPrototypeMethod(ctor, "streamPNGSync", StreamPNGSync);
Nan::SetPrototypeMethod(ctor, "streamPDFSync", StreamPDFSync);
#ifdef HAVE_JPEG
Nan::SetPrototypeMethod(ctor, "streamJPEGSync", StreamJPEGSync);
#endif
Nan::SetAccessor(proto, Nan::New("type").ToLocalChecked(), GetType);
Nan::SetAccessor(proto, Nan::New("format").ToLocalChecked(), GetFormat);
Nan::SetAccessor(proto, Nan::New("stride").ToLocalChecked(), GetStride);
Nan::SetAccessor(proto, Nan::New("width").ToLocalChecked(), GetWidth, SetWidth);
Nan::SetAccessor(proto, Nan::New("height").ToLocalChecked(), GetHeight, SetHeight);
Expand All @@ -57,6 +59,16 @@ Canvas::Initialize(Nan::ADDON_REGISTER_FUNCTION_ARGS_TYPE target) {
Nan::SetTemplate(proto, "PNG_FILTER_PAETH", Nan::New<Uint32>(PNG_FILTER_PAETH));
Nan::SetTemplate(proto, "PNG_ALL_FILTERS", Nan::New<Uint32>(PNG_ALL_FILTERS));

ctor->Set(Nan::New("CAIRO_FORMAT_INVALID").ToLocalChecked(), Nan::New<Uint32>(CAIRO_FORMAT_INVALID));
ctor->Set(Nan::New("CAIRO_FORMAT_ARGB32").ToLocalChecked(), Nan::New<Uint32>(CAIRO_FORMAT_ARGB32));
ctor->Set(Nan::New("CAIRO_FORMAT_RGB24").ToLocalChecked(), Nan::New<Uint32>(CAIRO_FORMAT_RGB24));
ctor->Set(Nan::New("CAIRO_FORMAT_A8").ToLocalChecked(), Nan::New<Uint32>(CAIRO_FORMAT_A8));
ctor->Set(Nan::New("CAIRO_FORMAT_A1").ToLocalChecked(), Nan::New<Uint32>(CAIRO_FORMAT_A1));
ctor->Set(Nan::New("CAIRO_FORMAT_RGB16_565").ToLocalChecked(), Nan::New<Uint32>(CAIRO_FORMAT_RGB16_565));
#ifdef CAIRO_FORMAT_RGB30
ctor->Set(Nan::New("CAIRO_FORMAT_RGB30").ToLocalChecked(), Nan::New<Uint32>(CAIRO_FORMAT_RGB30));
#endif

Nan::Set(target, Nan::New("Canvas").ToLocalChecked(), ctor->GetFunction());
}

Expand All @@ -70,15 +82,25 @@ NAN_METHOD(Canvas::New) {
}

int width = 0, height = 0;
unsigned char* data = nullptr;
cairo_format_t format = CAIRO_FORMAT_ARGB32;
canvas_type_t type = CANVAS_TYPE_IMAGE;
if (info[0]->IsNumber()) width = info[0]->Uint32Value();
if (info[1]->IsNumber()) height = info[1]->Uint32Value();
if (info[2]->IsString()) type = !strcmp("pdf", *String::Utf8Value(info[2]))
? CANVAS_TYPE_PDF
: !strcmp("svg", *String::Utf8Value(info[2]))
? CANVAS_TYPE_SVG
: CANVAS_TYPE_IMAGE;
Canvas *canvas = new Canvas(width, height, type);
if (info[2]->IsString()) {
type = !strcmp("pdf", *String::Utf8Value(info[2]))
? CANVAS_TYPE_PDF
: !strcmp("svg", *String::Utf8Value(info[2]))
? CANVAS_TYPE_SVG
: CANVAS_TYPE_IMAGE;
} else if (node::Buffer::HasInstance(info[2])) {
type = CANVAS_TYPE_IMAGE;
data = reinterpret_cast<unsigned char*>(node::Buffer::Data(info[2]));
}
if (info[3]->IsNumber()) {
format = static_cast<cairo_format_t>(info[3]->Uint32Value());
}
Canvas *canvas = new Canvas(width, height, type, data, format);
canvas->Wrap(info.This());
info.GetReturnValue().Set(info.This());
}
Expand All @@ -92,6 +114,15 @@ NAN_GETTER(Canvas::GetType) {
info.GetReturnValue().Set(Nan::New<String>(canvas->isPDF() ? "pdf" : canvas->isSVG() ? "svg" : "image").ToLocalChecked());
}

/*
* Get format string.
*/

NAN_GETTER(Canvas::GetFormat) {
Canvas *canvas = Nan::ObjectWrap::Unwrap<Canvas>(info.This());
info.GetReturnValue().Set(Nan::New<Number>(canvas->format));
}

/*
* Get stride.
*/
Expand Down Expand Up @@ -236,6 +267,19 @@ Canvas::EIO_AfterToBuffer(eio_req *req) {
#endif
}

/*
* Non-standard.
* Flushes the context contents to the backing store.
* Useful when an explicit Buffer backing store was passed
* in, and you want the Buffer contents to be up-to-date
*/

NAN_METHOD(Canvas::Flush) {
Nan::HandleScope scope;
Canvas *canvas = ObjectWrap::Unwrap<Canvas>(info.This());
cairo_surface_flush(canvas->surface());
}

/*
* Convert PNG data to a node::Buffer, async when a
* callback function is passed.
Expand Down Expand Up @@ -568,10 +612,11 @@ NAN_METHOD(Canvas::StreamJPEGSync) {
* Initialize cairo surface.
*/

Canvas::Canvas(int w, int h, canvas_type_t t): Nan::ObjectWrap() {
Canvas::Canvas(int w, int h, canvas_type_t t, unsigned char* data, cairo_format_t f): Nan::ObjectWrap() {
type = t;
width = w;
height = h;
format = f;
_surface = NULL;
_closure = NULL;

Expand All @@ -587,6 +632,10 @@ Canvas::Canvas(int w, int h, canvas_type_t t): Nan::ObjectWrap() {
cairo_status_t status = closure_init((closure_t *) _closure, this, 0, PNG_NO_FILTERS);
assert(status == CAIRO_STATUS_SUCCESS);
_surface = cairo_svg_surface_create_for_stream(toBuffer, _closure, w, h);
} else if (data) {
_surface = cairo_image_surface_create_for_data(data, format, w, h,
cairo_format_stride_for_width(format, w));
assert(_surface);
} else {
_surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, w, h);
assert(_surface);
Expand Down
5 changes: 4 additions & 1 deletion src/Canvas.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,14 @@ class Canvas: public Nan::ObjectWrap {
int width;
int height;
canvas_type_t type;
cairo_format_t format;
static Nan::Persistent<FunctionTemplate> constructor;
static void Initialize(Nan::ADDON_REGISTER_FUNCTION_ARGS_TYPE target);
static NAN_METHOD(New);
static NAN_METHOD(Flush);
static NAN_METHOD(ToBuffer);
static NAN_GETTER(GetType);
static NAN_GETTER(GetFormat);
static NAN_GETTER(GetStride);
static NAN_GETTER(GetWidth);
static NAN_GETTER(GetHeight);
Expand Down Expand Up @@ -87,7 +90,7 @@ class Canvas: public Nan::ObjectWrap {
inline uint8_t *data(){ return cairo_image_surface_get_data(_surface); }
inline int stride(){ return cairo_image_surface_get_stride(_surface); }
inline int nBytes(){ return height * stride(); }
Canvas(int width, int height, canvas_type_t type);
Canvas(int width, int height, canvas_type_t type, unsigned char* data, cairo_format_t f);
void resurface(Local<Object> canvas);

private:
Expand Down