Skip to content

Commit

Permalink
expose cairo_image_surface_create_for_data
Browse files Browse the repository at this point in the history
  • Loading branch information
mildsunrise committed Mar 12, 2022
1 parent 3051382 commit 70411b3
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/modules/cairo/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,9 @@ function getInArgumentSource(p, n) {

if (typeName === 'const char *')
return `Nan::Utf8String ${p.name}__value(info[${n}].As<String>()); 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<Number>()).ToChecked();`
Expand Down
19 changes: 19 additions & 0 deletions src/modules/cairo/surface.cc
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ void ImageSurface::SetupTemplate(Local<FunctionTemplate> parentTpl) {
auto ctor = Nan::GetFunction (tpl).ToLocalChecked();

SET_METHOD(ctor, createFromPng);
SET_METHOD(ctor, createForData);

constructorTemplate.Reset(tpl);
constructor.Reset(ctor);
Expand Down Expand Up @@ -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<int64_t>(info[1].As<Number>()).ToChecked();
auto width = Nan::To<int64_t>(info[2].As<Number>()).ToChecked();
auto height = Nan::To<int64_t>(info[3].As<Number>()).ToChecked();
auto stride = Nan::To<int64_t>(info[4].As<Number>()).ToChecked();

// function call
cairo_surface_t * result = cairo_image_surface_create_for_data (data, format, width, height, stride);

// return
Local<Value> args[] = { Nan::New<External> (result) };
Local<Function> constructor = Nan::New<Function> (Surface::constructor);
Local<Value> returnValue = Nan::NewInstance(constructor, 1, args).ToLocalChecked();
info.GetReturnValue().Set(returnValue);
}

NAN_METHOD(ImageSurface::getData) {
auto self = info.This();
auto surface = Nan::ObjectWrap::Unwrap<ImageSurface>(self)->_data;
Expand Down
1 change: 1 addition & 0 deletions src/modules/cairo/surface.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/modules/cairo/surface.nid
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 70411b3

Please sign in to comment.