Skip to content

Commit 1a6dffe

Browse files
committed
Backends support
This is a stripped-down version of pull-request #571 focused only on the backends support, but adding also support for PDF and SVG backends and passing the tests. it also retain backwards compatibility with the old API, so this would be easy to merge since it's mostly to move around the PDF and SVG code and checks to independent classes. There's some code for `pdfStream` and similar methods that I think they should be moved too since they are mostly backend specific, and make more sense there. I didn't do that since it would break the API, but maybe it would be added some functions on the Canvas object that proxy the petitions to the backend to retain compatibility. I know it's a big pull-request, so I'm open for comments over it.
1 parent 7704d8d commit 1a6dffe

23 files changed

+688
-186
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ node_modules
1414
# Vim cruft
1515
*.swp
1616
*.un~
17+
npm-debug.log

binding.gyp

100755100644
Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
'conditions': [
33
['OS=="win"', {
44
'variables': {
5-
'GTK_Root%': 'C:/GTK', # Set the location of GTK all-in-one bundle
5+
'GTK_Root%': 'C:/GTK', # Set the location of GTK all-in-one bundle
66
'with_jpeg%': 'false',
77
'with_gif%': 'false'
88
}
9-
}, { # 'OS!="win"'
9+
}, { # 'OS!="win"'
1010
'variables': {
1111
'with_jpeg%': '<!(./util/has_lib.sh jpeg)',
1212
'with_gif%': '<!(./util/has_lib.sh gif)'
@@ -46,15 +46,22 @@
4646
'target_name': 'canvas',
4747
'include_dirs': ["<!(node -e \"require('nan')\")"],
4848
'sources': [
49+
'src/backend/Backend.cc',
50+
'src/backend/ImageBackend.cc',
51+
'src/backend/PdfBackend.cc',
52+
'src/backend/SvgBackend.cc',
53+
'src/Backends.cc',
4954
'src/Canvas.cc',
5055
'src/CanvasGradient.cc',
5156
'src/CanvasPattern.cc',
5257
'src/CanvasRenderingContext2d.cc',
58+
'src/closure.cc',
5359
'src/color.cc',
5460
'src/Image.cc',
5561
'src/ImageData.cc',
62+
'src/init.cc',
5663
'src/register_font.cc',
57-
'src/init.cc'
64+
'src/toBuffer.cc'
5865
],
5966
'conditions': [
6067
['OS=="win"', {
@@ -76,15 +83,17 @@
7683
'<(GTK_Root)/lib/glib-2.0/include'
7784
],
7885
'defines': [
79-
'_USE_MATH_DEFINES' # for M_PI
86+
'_USE_MATH_DEFINES' # for M_PI
8087
],
8188
'configurations': {
8289
'Debug': {
8390
'msvs_settings': {
8491
'VCCLCompilerTool': {
8592
'WarningLevel': 4,
8693
'ExceptionHandling': 1,
87-
'DisableSpecificWarnings': [4100, 4127, 4201, 4244, 4267, 4506, 4611, 4714, 4512]
94+
'DisableSpecificWarnings': [
95+
4100, 4127, 4201, 4244, 4267, 4506, 4611, 4714, 4512
96+
]
8897
}
8998
}
9099
},
@@ -93,12 +102,14 @@
93102
'VCCLCompilerTool': {
94103
'WarningLevel': 4,
95104
'ExceptionHandling': 1,
96-
'DisableSpecificWarnings': [4100, 4127, 4201, 4244, 4267, 4506, 4611, 4714, 4512]
105+
'DisableSpecificWarnings': [
106+
4100, 4127, 4201, 4244, 4267, 4506, 4611, 4714, 4512
107+
]
97108
}
98109
}
99110
}
100111
}
101-
}, { # 'OS!="win"'
112+
}, { # 'OS!="win"'
102113
'libraries': [
103114
'<!@(pkg-config pixman-1 --libs)',
104115
'<!@(pkg-config cairo --libs)',

examples/backends.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
var fs = require('fs')
2+
var resolve = require('path').resolve
3+
4+
var Canvas = require('..')
5+
6+
var imagebackend = new Canvas.backends.ImageBackend(800, 600)
7+
8+
var canvas = new Canvas(imagebackend)
9+
var ctx = canvas.getContext('2d')
10+
11+
console.log('Width: ' + canvas.width + ', Height: ' + canvas.height)
12+
13+
ctx.fillStyle = '#00FF00'
14+
ctx.fillRect(50, 50, 100, 100)
15+
16+
var outPath = resolve(__dirname, 'rectangle.png')
17+
18+
canvas.createPNGStream().pipe(fs.createWriteStream(outPath))

lib/canvas.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
*/
1212

1313
var canvas = require('./bindings')
14+
, Backends = canvas.Backends
1415
, Canvas = canvas.Canvas
1516
, Image = canvas.Image
1617
, cairoVersion = canvas.cairoVersion
@@ -28,6 +29,8 @@ var canvas = require('./bindings')
2829

2930
var Canvas = exports = module.exports = Canvas;
3031

32+
exports.backends = Backends;
33+
3134
/**
3235
* Library version.
3336
*/

src/Backends.cc

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include "Backends.h"
2+
3+
#include "backend/ImageBackend.h"
4+
#include "backend/PdfBackend.h"
5+
#include "backend/SvgBackend.h"
6+
7+
using namespace v8;
8+
9+
void Backends::Initialize(Handle<Object> target) {
10+
Nan::HandleScope scope;
11+
12+
Local<Object> obj = Nan::New<Object>();
13+
ImageBackend::Initialize(obj);
14+
PdfBackend::Initialize(obj);
15+
SvgBackend::Initialize(obj);
16+
17+
target->Set(Nan::New<String>("Backends").ToLocalChecked(), obj);
18+
}

src/Backends.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#ifndef __NODE_BACKENDS_H__
2+
#define __NODE_BACKENDS_H__
3+
4+
#include <nan.h>
5+
6+
#include "backend/Backend.h"
7+
8+
9+
class Backends : public Nan::ObjectWrap {
10+
public:
11+
static void Initialize(v8::Handle<v8::Object> target);
12+
};
13+
14+
#endif

0 commit comments

Comments
 (0)