Skip to content

Commit

Permalink
Fix for Node 4
Browse files Browse the repository at this point in the history
  • Loading branch information
nfroidure committed Sep 12, 2015
1 parent 6c778fa commit 5f4a918
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 27 deletions.
53 changes: 36 additions & 17 deletions csrc/addon.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ void Convert(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = Isolate::GetCurrent();
HandleScope scope(isolate);

// Checking arguments
if (args.Length() < 1) {
isolate->ThrowException(Exception::TypeError(
String::NewFromUtf8(isolate, "Wrong number of arguments")));
Expand All @@ -22,43 +23,61 @@ void Convert(const FunctionCallbackInfo<Value>& args) {
return;
}

// Read the given buffer
Local<Object> inputBuffer = args[0]->ToObject();

if (!node::Buffer::HasInstance(inputBuffer)) {
isolate->ThrowException(Exception::TypeError(
String::NewFromUtf8(isolate, "First arg should be a Buffer")));
return;
}

size_t input_length = inputBuffer->GetIndexedPropertiesExternalArrayDataLength();
size_t input_length = node::Buffer::Length(inputBuffer);
char* input_data = static_cast<char*>(
inputBuffer->GetIndexedPropertiesExternalArrayData());
node::Buffer::Data(inputBuffer)
);

size_t output_length = woff2::MaxWOFF2CompressedSize(
// Determine the maximum needed length
size_t max_output_length = woff2::MaxWOFF2CompressedSize(
reinterpret_cast<const uint8_t*>(input_data), input_length);
char* output_data[output_length];
size_t actual_output_length = max_output_length;

char* output_data = static_cast<char*>(malloc(max_output_length));
if (output_data == nullptr) {
free(output_data);
args.GetReturnValue().Set(Local<Object>());
return;
}

// Create the Woff2 font
if (!woff2::ConvertTTFToWOFF2(
reinterpret_cast<const uint8_t*>(input_data), input_length,
reinterpret_cast<uint8_t*>(output_data), &output_length
reinterpret_cast<uint8_t*>(output_data), &actual_output_length
)) {
free(output_data);
isolate->ThrowException(Exception::TypeError(
String::NewFromUtf8(isolate, "Could not convert the given font.")));
return;
}

Local<Object> slowBuffer = node::Buffer::New(isolate, output_length);
memcpy(node::Buffer::Data(slowBuffer), output_data, output_length);
// Reallocate the buffer data if needed
if (actual_output_length < max_output_length) {
output_data = static_cast<char*>(realloc(output_data, actual_output_length));
if (output_data == nullptr) {
free(output_data);
args.GetReturnValue().Set(Local<Object>());
return;
}
}

Local<Object> outputBuffer;
if (!node::Buffer::New(isolate, output_data, actual_output_length).ToLocal(&outputBuffer)) {
free(output_data);
args.GetReturnValue().Set(Local<Object>());
return;
}

Local<Object> globalObj = isolate->GetCurrentContext()->Global();
Local<Function> bufferConstructor =
Local<Function>::Cast(globalObj->Get(String::NewFromUtf8(isolate, "Buffer")));
Handle<Value> constructorArgs[3] = {
slowBuffer,
Number::New(isolate, static_cast<int>(output_length)),
Number::New(isolate, 0)
};
Local<Object> actualBuffer = bufferConstructor->NewInstance(3, constructorArgs);
args.GetReturnValue().Set(actualBuffer);
args.GetReturnValue().Set(outputBuffer);
}

void Init(Handle<Object> exports) {
Expand Down
15 changes: 8 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ttf2woff2",
"version": "1.2.3",
"version": "1.2.2",
"description": "Convert TTF files to WOFF2 ones.",
"main": "src/index.js",
"browser": "jssrc/index.js",
Expand All @@ -17,6 +17,7 @@
"cli": "env NPM_RUN_CLI=1",
"configure": "node-gyp configure",
"lint": "eslint src/*.js tests/*.src jssrc/index.js",
"preversion": "npm run lint && npm test",
"make": "node-gyp build",
"emcc": "miniquery -p \"targets.#.sources.#\" ./binding.gyp | grep -v \"csrc/addon.cc\" | xargs emcc -o jssrc/ttf2woff2.js -O3 --memory-init-file 0 -s EXPORTED_FUNCTIONS=\"['_convertTTFToWOFF2','_freeTTFToWOFF2']\" -s \"TOTAL_MEMORY=536870912\" -s \"ALLOW_MEMORY_GROWTH=1\" --post-js jssrc/post.js csrc/fallback.cc",
"emcc-debug": "miniquery -p \"targets.#.sources.#\" ./binding.gyp | grep -v \"csrc/addon.cc\" | xargs emcc -o jssrc/ttf2woff2.js -s EXPORTED_FUNCTIONS=\"['_convertTTFToWOFF2','_freeTTFToWOFF2']\" -s \"ALLOW_MEMORY_GROWTH=1\" -s \"ASSERTIONS=1\" --post-js jssrc/post.js csrc/fallback.cc",
Expand All @@ -38,16 +39,16 @@
},
"homepage": "https://github.com/nfroidure/ttf2woff2",
"dependencies": {
"bufferstreams": "^1.0.2",
"bufferstreams": "^1.1.0",
"bindings": "^1.2.1",
"nan": "^1.8.4",
"node-gyp": "^2.0.1"
"nan": "^2.0.9",
"node-gyp": "^3.0.2"
},
"devDependencies": {
"coveralls": "^2.11.2",
"istanbul": "^0.3.16",
"coveralls": "^2.11.4",
"istanbul": "^0.3.19",
"miniquery": "^1.1.1",
"mocha": "^2.2.5",
"mocha": "^2.3.2",
"mocha-lcov-reporter": "^0.0.2"
},
"preferGlobal": "true",
Expand Down
18 changes: 15 additions & 3 deletions tests/tests.mocha.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@ describe('ttf2woff2', function() {
this.timeout(10000);
var ttf2woff2 = require('../src/index');
var inputContent = fs.readFileSync(__dirname + '/expected/iconsfont.ttf');
var outputContent = ttf2woff2(inputContent);

assert.equal(outputContent.length, 1072);
assert.equal(outputContent[1071], 0);
assert.deepEqual(
ttf2woff2(inputContent),
outputContent,
fs.readFileSync(__dirname + '/expected/iconsfont.woff2')
);
done();
Expand All @@ -17,8 +21,12 @@ describe('ttf2woff2', function() {
it('should work from the native build', function(done) {
var ttf2woff2 = require('bindings')('addon.node').convert;
var inputContent = fs.readFileSync(__dirname + '/expected/iconsfont.ttf');
var outputContent = ttf2woff2(inputContent);

assert.equal(outputContent.length, 1072);
assert.equal(outputContent[1071], 0);
assert.deepEqual(
ttf2woff2(inputContent),
outputContent,
fs.readFileSync(__dirname + '/expected/iconsfont.woff2')
);
done();
Expand All @@ -28,8 +36,12 @@ describe('ttf2woff2', function() {
this.timeout(10000);
var ttf2woff2 = require('../jssrc/index.js');
var inputContent = fs.readFileSync(__dirname + '/expected/iconsfont.ttf');
var outputContent = ttf2woff2(inputContent);

assert.equal(outputContent.length, 1072);
assert.equal(outputContent[1071], 0);
assert.deepEqual(
ttf2woff2(inputContent),
outputContent,
fs.readFileSync(__dirname + '/expected/iconsfont.woff2')
);
done();
Expand Down

0 comments on commit 5f4a918

Please sign in to comment.