Skip to content

Commit 2c6e34c

Browse files
committed
N-API mapnik.CairoSurface + tests (WIP) [skip ci]
1 parent ea297ad commit 2c6e34c

File tree

5 files changed

+75
-110
lines changed

5 files changed

+75
-110
lines changed

binding.gyp

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
"src/mapnik_datasource.cpp",
4444
"src/mapnik_featureset.cpp",
4545
"src/mapnik_expression.cpp",
46-
#"src/mapnik_cairo_surface.cpp",
46+
"src/mapnik_cairo_surface.cpp",
4747
#"src/mapnik_vector_tile.cpp"
4848
],
4949
'include_dirs': [

src/mapnik_cairo_surface.cpp

+28-58
Original file line numberDiff line numberDiff line change
@@ -4,93 +4,63 @@
44

55
Napi::FunctionReference CairoSurface::constructor;
66

7-
void CairoSurface::Initialize(Napi::Object target) {
8-
Napi::HandleScope scope(env);
9-
10-
Napi::FunctionReference lcons = Napi::Function::New(env, CairoSurface::New);
11-
12-
lcons->SetClassName(Napi::String::New(env, "CairoSurface"));
13-
InstanceMethod("width", &width),
14-
InstanceMethod("height", &height),
15-
InstanceMethod("getData", &getData),
16-
(target).Set(Napi::String::New(env, "CairoSurface"), Napi::GetFunction(lcons));
17-
constructor.Reset(lcons);
18-
}
19-
20-
CairoSurface::CairoSurface(std::string const& format, unsigned int width, unsigned int height) : Napi::ObjectWrap<CairoSurface>(),
21-
ss_(),
22-
width_(width),
23-
height_(height),
24-
format_(format)
25-
{
26-
}
27-
28-
CairoSurface::~CairoSurface()
7+
Napi::Object CairoSurface::Initialize(Napi::Env env, Napi::Object exports)
298
{
9+
Napi::Function func = DefineClass(env, "CairoSurface", {
10+
InstanceMethod<&CairoSurface::width>("width"),
11+
InstanceMethod<&CairoSurface::height>("height"),
12+
InstanceMethod<&CairoSurface::getData>("getData")
13+
});
14+
constructor = Napi::Persistent(func);
15+
constructor.SuppressDestruct();
16+
exports.Set("CairoSurface", func);
17+
return exports;
3018
}
3119

32-
Napi::Value CairoSurface::New(Napi::CallbackInfo const& info)
20+
// ctor
21+
CairoSurface::CairoSurface(Napi::CallbackInfo const& info)
22+
: Napi::ObjectWrap<CairoSurface>(info)
3323
{
34-
if (!info.IsConstructCall())
35-
{
36-
Napi::Error::New(env, "Cannot call constructor as function, you need to use 'new' keyword").ThrowAsJavaScriptException();
37-
return env.Null();
38-
}
39-
40-
if (info[0].IsExternal())
41-
{
42-
// Currently there is no C++ that executes this call
43-
/* LCOV_EXCL_START */
44-
Napi::External ext = info[0].As<Napi::External>();
45-
void* ptr = ext->Value();
46-
CairoSurface* im = static_cast<CairoSurface*>(ptr);
47-
im->Wrap(info.This());
48-
return info.This();
49-
return;
50-
/* LCOV_EXCL_STOP */
51-
}
52-
24+
Napi::Env env = info.Env();
5325
if (info.Length() == 3)
5426
{
5527
if (!info[0].IsString())
5628
{
5729
Napi::TypeError::New(env, "CairoSurface 'format' must be a string").ThrowAsJavaScriptException();
58-
return env.Null();
30+
return;
5931
}
60-
std::string format = TOSTR(info[0]);
32+
format_ = info[0].As<Napi::String>();
6133
if (!info[1].IsNumber() || !info[2].IsNumber())
6234
{
6335
Napi::TypeError::New(env, "CairoSurface 'width' and 'height' must be a integers").ThrowAsJavaScriptException();
64-
return env.Null();
36+
return;
6537
}
66-
CairoSurface* im = new CairoSurface(format, info[1].As<Napi::Number>().Int32Value(), info[2].As<Napi::Number>().Int32Value());
67-
im->Wrap(info.This());
68-
return info.This();
69-
return;
38+
width_ = info[1].As<Napi::Number>().Int32Value();
39+
height_ = info[2].As<Napi::Number>().Int32Value();
7040
}
7141
else
7242
{
7343
Napi::Error::New(env, "CairoSurface requires three arguments: format, width, and height").ThrowAsJavaScriptException();
74-
return env.Null();
7544
}
76-
return;
7745
}
7846

7947
Napi::Value CairoSurface::width(Napi::CallbackInfo const& info)
8048
{
81-
CairoSurface* im = info.Holder().Unwrap<CairoSurface>();
82-
return Napi::New(env, im->width());
49+
Napi::Env env = info.Env();
50+
return Napi::Number::New(env, width_);
8351
}
8452

8553
Napi::Value CairoSurface::height(Napi::CallbackInfo const& info)
8654
{
87-
CairoSurface* im = info.Holder().Unwrap<CairoSurface>();
88-
return Napi::New(env, im->height());
55+
Napi::Env env = info.Env();
56+
return Napi::Number::New(env, height_);
8957
}
9058

9159
Napi::Value CairoSurface::getData(Napi::CallbackInfo const& info)
9260
{
93-
CairoSurface* surface = info.Holder().Unwrap<CairoSurface>();
94-
std::string s = surface->ss_.str();
95-
return Napi::Buffer::Copy(env, (char*)s.data(), s.size());
61+
Napi::Env env = info.Env();
62+
Napi::EscapableHandleScope scope(env);
63+
std::string str = stream_.str();
64+
//return scope.Escape(Napi::Buffer<char>::Copy(env, const_cast<char*>(str.data()), str.size()));
65+
return scope.Escape(Napi::String::New(env, str));
9666
}

src/mapnik_cairo_surface.hpp

+25-29
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,9 @@
1-
#ifndef __NODE_MAPNIK_CAIRO_H__
2-
#define __NODE_MAPNIK_CAIRO_H__
1+
#pragma once
32

4-
#pragma GCC diagnostic push
5-
#pragma GCC diagnostic ignored "-Wunused-parameter"
6-
#pragma GCC diagnostic ignored "-Wshadow"
73
#include <napi.h>
8-
#include <uv.h>
9-
#pragma GCC diagnostic pop
104

11-
#include <string>
5+
//#include <string>
126
#include <sstream>
13-
147
// cairo
158
#if defined(HAVE_CAIRO)
169
#include <cairo.h>
@@ -19,21 +12,24 @@
1912
#endif
2013

2114

22-
23-
class CairoSurface : public Napi::ObjectWrap<CairoSurface> {
15+
class CairoSurface : public Napi::ObjectWrap<CairoSurface>
16+
{
2417
public:
25-
typedef std::stringstream i_stream;
26-
static Napi::FunctionReference constructor;
27-
static void Initialize(Napi::Object target);
28-
static Napi::Value New(Napi::CallbackInfo const& info);
29-
static Napi::Value getData(Napi::CallbackInfo const& info);
30-
static Napi::Value width(Napi::CallbackInfo const& info);
31-
static Napi::Value height(Napi::CallbackInfo const& info);
18+
//typedef std::stringstream i_stream;
19+
// initializer
20+
static Napi::Object Initialize(Napi::Env env, Napi::Object exports);
21+
// ctor
22+
explicit CairoSurface(Napi::CallbackInfo const& info);
23+
// methods
24+
Napi::Value getData(Napi::CallbackInfo const& info);
25+
Napi::Value width(Napi::CallbackInfo const& info);
26+
Napi::Value height(Napi::CallbackInfo const& info);
3227

33-
using Napi::ObjectWrap::Ref;
34-
using Napi::ObjectWrap::Unref;
28+
//using Napi::ObjectWrap::Ref;
29+
//using Napi::ObjectWrap::Unref;
3530

36-
CairoSurface(std::string const& format, unsigned int width, unsigned int height);
31+
//CairoSurface(std::string const& format, unsigned int width, unsigned int height);
32+
/*
3733
static cairo_status_t write_callback(void *closure,
3834
const unsigned char *data,
3935
unsigned int length)
@@ -45,9 +41,9 @@ class CairoSurface : public Napi::ObjectWrap<CairoSurface> {
4541
// class it is unlikely that this could ever be reached unless something was very wrong
4642
// and went out of scope, aka it was improperly programmed. Therefore, it is not possible
4743
// to reach this point with standard testing.
48-
/* LCOV_EXCL_START */
44+
// LCOV_EXCL_START
4945
return CAIRO_STATUS_WRITE_ERROR;
50-
/* LCOV_EXCL_STOP */
46+
// LCOV_EXCL_STOP
5147
}
5248
i_stream* fin = reinterpret_cast<i_stream*>(closure);
5349
*fin << std::string((const char*)data,(size_t)length);
@@ -56,14 +52,14 @@ class CairoSurface : public Napi::ObjectWrap<CairoSurface> {
5652
return 11; // CAIRO_STATUS_WRITE_ERROR
5753
#endif
5854
}
59-
unsigned width() { return width_; }
60-
unsigned height() { return height_; }
61-
mutable i_stream ss_;
55+
*/
56+
//unsigned width() { return width_; }
57+
//unsigned height() { return height_; }
58+
//mutable i_stream ss_;
6259
private:
60+
static Napi::FunctionReference constructor;
6361
unsigned width_;
6462
unsigned height_;
6563
std::string format_;
66-
~CairoSurface();
64+
std::stringstream stream_;
6765
};
68-
69-
#endif

src/node_mapnik.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
//#include "mapnik_memory_datasource.hpp"
1919
#include "mapnik_image.hpp"
2020
#include "mapnik_image_view.hpp"
21-
//#include "mapnik_cairo_surface.hpp"
21+
#include "mapnik_cairo_surface.hpp"
2222
#if defined(GRID_RENDERER)
2323
//#include "mapnik_grid.hpp"
2424
//#include "mapnik_grid_view.hpp"
@@ -321,6 +321,7 @@ Napi::Object init(Napi::Env env, Napi::Object exports)
321321
Map::Initialize(env, exports);
322322
Expression::Initialize(env, exports);
323323
Logger::Initialize(env, exports);
324+
CairoSurface::Initialize(env, exports);
324325
// enums
325326
init_image_types(env, exports);
326327
init_image_scalings(env, exports);

test/cairo_surface.test.js

+19-21
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,26 @@
11
"use strict";
22

3+
var test = require('tape');
34
var mapnik = require('../');
4-
var assert = require('assert');
55

6-
describe('mapnik.CairoSurface ', function() {
7-
8-
it('should throw with invalid usage', function() {
9-
// no 'new' keyword
10-
assert.throws(function() { mapnik.CairoSurface(1, 1); });
6+
test('should throw with invalid usage', (assert) => {
7+
// no 'new' keyword
8+
assert.throws(function() { mapnik.CairoSurface(1, 1); });
119

12-
// invalid args
13-
assert.throws(function() { new mapnik.CairoSurface(); });
14-
assert.throws(function() { new mapnik.CairoSurface(1); });
15-
assert.throws(function() { new mapnik.CairoSurface('foo'); });
16-
assert.throws(function() { new mapnik.CairoSurface('a', 'b', 'c'); });
17-
assert.throws(function() { new mapnik.CairoSurface(1, 'b', 'c'); });
18-
});
19-
20-
it('should be initialized properly', function() {
21-
var im = new mapnik.CairoSurface('SVG',256, 256);
22-
assert.ok(im instanceof mapnik.CairoSurface);
23-
assert.equal(im.width(), 256);
24-
assert.equal(im.height(), 256);
25-
assert.equal(im.getData(), '');
26-
});
10+
// invalid args
11+
assert.throws(function() { new mapnik.CairoSurface(); });
12+
assert.throws(function() { new mapnik.CairoSurface(1); });
13+
assert.throws(function() { new mapnik.CairoSurface('foo'); });
14+
assert.throws(function() { new mapnik.CairoSurface('a', 'b', 'c'); });
15+
assert.throws(function() { new mapnik.CairoSurface(1, 'b', 'c'); });
16+
assert.end();
17+
});
2718

19+
test('should be initialized properly', (assert) => {
20+
var im = new mapnik.CairoSurface('SVG',256, 256);
21+
assert.ok(im instanceof mapnik.CairoSurface);
22+
assert.equal(im.width(), 256);
23+
assert.equal(im.height(), 256);
24+
assert.equal(im.getData(), '');
25+
assert.end();
2826
});

0 commit comments

Comments
 (0)