diff --git a/src/modules/cairo/generator.js b/src/modules/cairo/generator.js index 7ba4610f..abb3750b 100644 --- a/src/modules/cairo/generator.js +++ b/src/modules/cairo/generator.js @@ -416,6 +416,9 @@ function getInArgumentSource(p, n) { if (typeName === 'const char *') return `Nan::Utf8String ${p.name}__value(info[${n}].As()); auto ${p.name} = *${p.name}__value;` + + if (typeName === 'unsigned char *' || typeName === 'const unsigned char *') + return (`if (!node::Buffer::HasInstance(info[${n}])) return Nan::ThrowTypeError("buffer expected"); auto ${p.name} = (unsigned char *) node::Buffer::Data(info[${n}]);`) if (baseName in ENUM_TYPE) return `auto ${p.name} = (${typeName}) Nan::To<${ENUM_TYPE[typeName]}>(info[${n}].As()).ToChecked();` diff --git a/src/modules/cairo/surface.cc b/src/modules/cairo/surface.cc index 79b3579e..4ec928d8 100644 --- a/src/modules/cairo/surface.cc +++ b/src/modules/cairo/surface.cc @@ -142,6 +142,7 @@ void ImageSurface::SetupTemplate(Local parentTpl) { auto ctor = Nan::GetFunction (tpl).ToLocalChecked(); SET_METHOD(ctor, createFromPng); + SET_METHOD(ctor, createForData); constructorTemplate.Reset(tpl); constructor.Reset(ctor); @@ -642,6 +643,24 @@ NAN_METHOD(ImageSurface::createFromPng) { info.GetReturnValue().Set(returnValue); } +NAN_METHOD(ImageSurface::createForData) { + // in-arguments + if (!node::Buffer::HasInstance(info[0])) return Nan::ThrowTypeError("buffer expected"); auto data = (unsigned char *) node::Buffer::Data(info[0]); + auto format = (cairo_format_t) Nan::To(info[1].As()).ToChecked(); + auto width = Nan::To(info[2].As()).ToChecked(); + auto height = Nan::To(info[3].As()).ToChecked(); + auto stride = Nan::To(info[4].As()).ToChecked(); + + // function call + cairo_surface_t * result = cairo_image_surface_create_for_data (data, format, width, height, stride); + + // return + Local args[] = { Nan::New (result) }; + Local constructor = Nan::New (Surface::constructor); + Local returnValue = Nan::NewInstance(constructor, 1, args).ToLocalChecked(); + info.GetReturnValue().Set(returnValue); +} + NAN_METHOD(ImageSurface::getData) { auto self = info.This(); auto surface = Nan::ObjectWrap::Unwrap(self)->_data; diff --git a/src/modules/cairo/surface.h b/src/modules/cairo/surface.h index c89224f5..d27de7be 100644 --- a/src/modules/cairo/surface.h +++ b/src/modules/cairo/surface.h @@ -70,6 +70,7 @@ class ImageSurface: public Surface { static NAN_METHOD(New); static NAN_METHOD(createFromPng); + static NAN_METHOD(createForData); static NAN_METHOD(getData); static NAN_METHOD(getFormat); static NAN_METHOD(getWidth); diff --git a/src/modules/cairo/surface.nid b/src/modules/cairo/surface.nid index 9c59f3fb..cdbb8baf 100644 --- a/src/modules/cairo/surface.nid +++ b/src/modules/cairo/surface.nid @@ -60,7 +60,7 @@ namespace ImageSurface { [[constructor]] cairo_surface_t * cairo_image_surface_create (cairo_format_t format, int width, int height); [[static]] cairo_surface_t * cairo_image_surface_create_from_png (const char *filename); -/* cairo_surface_t * cairo_image_surface_create_for_data (unsigned char *data, cairo_format_t format, int width, int height, int stride); */ +[[static]] cairo_surface_t * cairo_image_surface_create_for_data (unsigned char *data, cairo_format_t format, int width, int height, int stride); [[returns(uint8_t *)]] unsigned char * cairo_image_surface_get_data (cairo_surface_t *surface); cairo_format_t cairo_image_surface_get_format (cairo_surface_t *surface); int cairo_image_surface_get_width (cairo_surface_t *surface);