@shqld/canvas 2.11.2-rc.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Readme.md +600 -0
- package/binding.gyp +230 -0
- package/browser.js +35 -0
- package/index.js +94 -0
- package/lib/DOMMatrix.js +620 -0
- package/lib/bindings.js +80 -0
- package/lib/canvas.js +113 -0
- package/lib/context2d.js +14 -0
- package/lib/image.js +96 -0
- package/lib/jpegstream.js +41 -0
- package/lib/parse-font.js +101 -0
- package/lib/pattern.js +17 -0
- package/lib/pdfstream.js +35 -0
- package/lib/pngstream.js +42 -0
- package/package.json +71 -0
- package/scripts/install.js +24 -0
- package/src/Backends.cc +18 -0
- package/src/Backends.h +10 -0
- package/src/Canvas.cc +965 -0
- package/src/Canvas.h +96 -0
- package/src/CanvasError.h +23 -0
- package/src/CanvasGradient.cc +123 -0
- package/src/CanvasGradient.h +22 -0
- package/src/CanvasPattern.cc +136 -0
- package/src/CanvasPattern.h +37 -0
- package/src/CanvasRenderingContext2d.cc +3360 -0
- package/src/CanvasRenderingContext2d.h +225 -0
- package/src/Image.cc +1434 -0
- package/src/Image.h +127 -0
- package/src/ImageData.cc +146 -0
- package/src/ImageData.h +27 -0
- package/src/JPEGStream.h +167 -0
- package/src/PNG.h +292 -0
- package/src/Point.h +11 -0
- package/src/Util.h +9 -0
- package/src/backend/Backend.cc +112 -0
- package/src/backend/Backend.h +69 -0
- package/src/backend/ImageBackend.cc +74 -0
- package/src/backend/ImageBackend.h +26 -0
- package/src/backend/PdfBackend.cc +53 -0
- package/src/backend/PdfBackend.h +24 -0
- package/src/backend/SvgBackend.cc +61 -0
- package/src/backend/SvgBackend.h +24 -0
- package/src/bmp/BMPParser.cc +457 -0
- package/src/bmp/BMPParser.h +60 -0
- package/src/bmp/LICENSE.md +24 -0
- package/src/closure.cc +26 -0
- package/src/closure.h +81 -0
- package/src/color.cc +779 -0
- package/src/color.h +30 -0
- package/src/dll_visibility.h +20 -0
- package/src/init.cc +94 -0
- package/src/register_font.cc +408 -0
- package/src/register_font.h +7 -0
- package/types/index.d.ts +484 -0
- package/util/has_lib.js +119 -0
- package/util/win_jpeg_lookup.js +21 -0
package/src/Canvas.h
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
// Copyright (c) 2010 LearnBoost <tj@learnboost.com>
|
|
2
|
+
|
|
3
|
+
#pragma once
|
|
4
|
+
|
|
5
|
+
#include "backend/Backend.h"
|
|
6
|
+
#include <cairo.h>
|
|
7
|
+
#include "dll_visibility.h"
|
|
8
|
+
#include <nan.h>
|
|
9
|
+
#include <pango/pangocairo.h>
|
|
10
|
+
#include <v8.h>
|
|
11
|
+
#include <vector>
|
|
12
|
+
#include <cstddef>
|
|
13
|
+
|
|
14
|
+
/*
|
|
15
|
+
* FontFace describes a font file in terms of one PangoFontDescription that
|
|
16
|
+
* will resolve to it and one that the user describes it as (like @font-face)
|
|
17
|
+
*/
|
|
18
|
+
class FontFace {
|
|
19
|
+
public:
|
|
20
|
+
PangoFontDescription *sys_desc = nullptr;
|
|
21
|
+
PangoFontDescription *user_desc = nullptr;
|
|
22
|
+
unsigned char file_path[1024];
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
enum text_baseline_t : uint8_t {
|
|
26
|
+
TEXT_BASELINE_ALPHABETIC = 0,
|
|
27
|
+
TEXT_BASELINE_TOP = 1,
|
|
28
|
+
TEXT_BASELINE_BOTTOM = 2,
|
|
29
|
+
TEXT_BASELINE_MIDDLE = 3,
|
|
30
|
+
TEXT_BASELINE_IDEOGRAPHIC = 4,
|
|
31
|
+
TEXT_BASELINE_HANGING = 5
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
enum text_align_t : int8_t {
|
|
35
|
+
TEXT_ALIGNMENT_LEFT = -1,
|
|
36
|
+
TEXT_ALIGNMENT_CENTER = 0,
|
|
37
|
+
TEXT_ALIGNMENT_RIGHT = 1,
|
|
38
|
+
// Currently same as LEFT and RIGHT without RTL support:
|
|
39
|
+
TEXT_ALIGNMENT_START = -2,
|
|
40
|
+
TEXT_ALIGNMENT_END = 2
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
enum canvas_draw_mode_t : uint8_t {
|
|
44
|
+
TEXT_DRAW_PATHS,
|
|
45
|
+
TEXT_DRAW_GLYPHS
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
/*
|
|
49
|
+
* Canvas.
|
|
50
|
+
*/
|
|
51
|
+
|
|
52
|
+
class Canvas: public Nan::ObjectWrap {
|
|
53
|
+
public:
|
|
54
|
+
static Nan::Persistent<v8::FunctionTemplate> constructor;
|
|
55
|
+
static void Initialize(Nan::ADDON_REGISTER_FUNCTION_ARGS_TYPE target);
|
|
56
|
+
static NAN_METHOD(New);
|
|
57
|
+
static NAN_METHOD(ToBuffer);
|
|
58
|
+
static NAN_GETTER(GetType);
|
|
59
|
+
static NAN_GETTER(GetStride);
|
|
60
|
+
static NAN_GETTER(GetWidth);
|
|
61
|
+
static NAN_GETTER(GetHeight);
|
|
62
|
+
static NAN_SETTER(SetWidth);
|
|
63
|
+
static NAN_SETTER(SetHeight);
|
|
64
|
+
static NAN_METHOD(StreamPNGSync);
|
|
65
|
+
static NAN_METHOD(StreamPDFSync);
|
|
66
|
+
static NAN_METHOD(StreamJPEGSync);
|
|
67
|
+
static NAN_METHOD(RegisterFont);
|
|
68
|
+
static NAN_METHOD(DeregisterAllFonts);
|
|
69
|
+
static v8::Local<v8::Value> Error(cairo_status_t status);
|
|
70
|
+
static void ToPngBufferAsync(uv_work_t *req);
|
|
71
|
+
static void ToJpegBufferAsync(uv_work_t *req);
|
|
72
|
+
static void ToBufferAsyncAfter(uv_work_t *req);
|
|
73
|
+
static PangoWeight GetWeightFromCSSString(const char *weight);
|
|
74
|
+
static PangoStyle GetStyleFromCSSString(const char *style);
|
|
75
|
+
static PangoFontDescription *ResolveFontDescription(const PangoFontDescription *desc);
|
|
76
|
+
|
|
77
|
+
DLL_PUBLIC inline Backend* backend() { return _backend; }
|
|
78
|
+
DLL_PUBLIC inline cairo_surface_t* surface(){ return backend()->getSurface(); }
|
|
79
|
+
cairo_t* createCairoContext();
|
|
80
|
+
|
|
81
|
+
DLL_PUBLIC inline uint8_t *data(){ return cairo_image_surface_get_data(surface()); }
|
|
82
|
+
DLL_PUBLIC inline int stride(){ return cairo_image_surface_get_stride(surface()); }
|
|
83
|
+
DLL_PUBLIC inline std::size_t nBytes(){
|
|
84
|
+
return static_cast<std::size_t>(getHeight()) * stride();
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
DLL_PUBLIC inline int getWidth() { return backend()->getWidth(); }
|
|
88
|
+
DLL_PUBLIC inline int getHeight() { return backend()->getHeight(); }
|
|
89
|
+
|
|
90
|
+
Canvas(Backend* backend);
|
|
91
|
+
void resurface(v8::Local<v8::Object> canvas);
|
|
92
|
+
|
|
93
|
+
private:
|
|
94
|
+
~Canvas();
|
|
95
|
+
Backend* _backend;
|
|
96
|
+
};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
#include <string>
|
|
4
|
+
|
|
5
|
+
class CanvasError {
|
|
6
|
+
public:
|
|
7
|
+
std::string message;
|
|
8
|
+
std::string syscall;
|
|
9
|
+
std::string path;
|
|
10
|
+
int cerrno = 0;
|
|
11
|
+
void set(const char* iMessage = NULL, const char* iSyscall = NULL, int iErrno = 0, const char* iPath = NULL) {
|
|
12
|
+
if (iMessage) message.assign(iMessage);
|
|
13
|
+
if (iSyscall) syscall.assign(iSyscall);
|
|
14
|
+
cerrno = iErrno;
|
|
15
|
+
if (iPath) path.assign(iPath);
|
|
16
|
+
}
|
|
17
|
+
void reset() {
|
|
18
|
+
message.clear();
|
|
19
|
+
syscall.clear();
|
|
20
|
+
path.clear();
|
|
21
|
+
cerrno = 0;
|
|
22
|
+
}
|
|
23
|
+
};
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
// Copyright (c) 2010 LearnBoost <tj@learnboost.com>
|
|
2
|
+
|
|
3
|
+
#include "CanvasGradient.h"
|
|
4
|
+
|
|
5
|
+
#include "Canvas.h"
|
|
6
|
+
#include "color.h"
|
|
7
|
+
|
|
8
|
+
using namespace v8;
|
|
9
|
+
|
|
10
|
+
Nan::Persistent<FunctionTemplate> Gradient::constructor;
|
|
11
|
+
|
|
12
|
+
/*
|
|
13
|
+
* Initialize CanvasGradient.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
void
|
|
17
|
+
Gradient::Initialize(Nan::ADDON_REGISTER_FUNCTION_ARGS_TYPE target) {
|
|
18
|
+
Nan::HandleScope scope;
|
|
19
|
+
|
|
20
|
+
// Constructor
|
|
21
|
+
Local<FunctionTemplate> ctor = Nan::New<FunctionTemplate>(Gradient::New);
|
|
22
|
+
constructor.Reset(ctor);
|
|
23
|
+
ctor->InstanceTemplate()->SetInternalFieldCount(1);
|
|
24
|
+
ctor->SetClassName(Nan::New("CanvasGradient").ToLocalChecked());
|
|
25
|
+
|
|
26
|
+
// Prototype
|
|
27
|
+
Nan::SetPrototypeMethod(ctor, "addColorStop", AddColorStop);
|
|
28
|
+
Local<Context> ctx = Nan::GetCurrentContext();
|
|
29
|
+
Nan::Set(target,
|
|
30
|
+
Nan::New("CanvasGradient").ToLocalChecked(),
|
|
31
|
+
ctor->GetFunction(ctx).ToLocalChecked());
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/*
|
|
35
|
+
* Initialize a new CanvasGradient.
|
|
36
|
+
*/
|
|
37
|
+
|
|
38
|
+
NAN_METHOD(Gradient::New) {
|
|
39
|
+
if (!info.IsConstructCall()) {
|
|
40
|
+
return Nan::ThrowTypeError("Class constructors cannot be invoked without 'new'");
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// Linear
|
|
44
|
+
if (4 == info.Length()) {
|
|
45
|
+
Gradient *grad = new Gradient(
|
|
46
|
+
Nan::To<double>(info[0]).FromMaybe(0)
|
|
47
|
+
, Nan::To<double>(info[1]).FromMaybe(0)
|
|
48
|
+
, Nan::To<double>(info[2]).FromMaybe(0)
|
|
49
|
+
, Nan::To<double>(info[3]).FromMaybe(0));
|
|
50
|
+
grad->Wrap(info.This());
|
|
51
|
+
info.GetReturnValue().Set(info.This());
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// Radial
|
|
56
|
+
if (6 == info.Length()) {
|
|
57
|
+
Gradient *grad = new Gradient(
|
|
58
|
+
Nan::To<double>(info[0]).FromMaybe(0)
|
|
59
|
+
, Nan::To<double>(info[1]).FromMaybe(0)
|
|
60
|
+
, Nan::To<double>(info[2]).FromMaybe(0)
|
|
61
|
+
, Nan::To<double>(info[3]).FromMaybe(0)
|
|
62
|
+
, Nan::To<double>(info[4]).FromMaybe(0)
|
|
63
|
+
, Nan::To<double>(info[5]).FromMaybe(0));
|
|
64
|
+
grad->Wrap(info.This());
|
|
65
|
+
info.GetReturnValue().Set(info.This());
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
return Nan::ThrowTypeError("invalid arguments");
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/*
|
|
73
|
+
* Add color stop.
|
|
74
|
+
*/
|
|
75
|
+
|
|
76
|
+
NAN_METHOD(Gradient::AddColorStop) {
|
|
77
|
+
if (!info[0]->IsNumber())
|
|
78
|
+
return Nan::ThrowTypeError("offset required");
|
|
79
|
+
if (!info[1]->IsString())
|
|
80
|
+
return Nan::ThrowTypeError("color string required");
|
|
81
|
+
|
|
82
|
+
Gradient *grad = Nan::ObjectWrap::Unwrap<Gradient>(info.This());
|
|
83
|
+
short ok;
|
|
84
|
+
Nan::Utf8String str(info[1]);
|
|
85
|
+
uint32_t rgba = rgba_from_string(*str, &ok);
|
|
86
|
+
|
|
87
|
+
if (ok) {
|
|
88
|
+
rgba_t color = rgba_create(rgba);
|
|
89
|
+
cairo_pattern_add_color_stop_rgba(
|
|
90
|
+
grad->pattern()
|
|
91
|
+
, Nan::To<double>(info[0]).FromMaybe(0)
|
|
92
|
+
, color.r
|
|
93
|
+
, color.g
|
|
94
|
+
, color.b
|
|
95
|
+
, color.a);
|
|
96
|
+
} else {
|
|
97
|
+
return Nan::ThrowTypeError("parse color failed");
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/*
|
|
102
|
+
* Initialize linear gradient.
|
|
103
|
+
*/
|
|
104
|
+
|
|
105
|
+
Gradient::Gradient(double x0, double y0, double x1, double y1) {
|
|
106
|
+
_pattern = cairo_pattern_create_linear(x0, y0, x1, y1);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/*
|
|
110
|
+
* Initialize radial gradient.
|
|
111
|
+
*/
|
|
112
|
+
|
|
113
|
+
Gradient::Gradient(double x0, double y0, double r0, double x1, double y1, double r1) {
|
|
114
|
+
_pattern = cairo_pattern_create_radial(x0, y0, r0, x1, y1, r1);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/*
|
|
118
|
+
* Destroy the pattern.
|
|
119
|
+
*/
|
|
120
|
+
|
|
121
|
+
Gradient::~Gradient() {
|
|
122
|
+
cairo_pattern_destroy(_pattern);
|
|
123
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
// Copyright (c) 2010 LearnBoost <tj@learnboost.com>
|
|
2
|
+
|
|
3
|
+
#pragma once
|
|
4
|
+
|
|
5
|
+
#include <nan.h>
|
|
6
|
+
#include <v8.h>
|
|
7
|
+
#include <cairo.h>
|
|
8
|
+
|
|
9
|
+
class Gradient: public Nan::ObjectWrap {
|
|
10
|
+
public:
|
|
11
|
+
static Nan::Persistent<v8::FunctionTemplate> constructor;
|
|
12
|
+
static void Initialize(Nan::ADDON_REGISTER_FUNCTION_ARGS_TYPE target);
|
|
13
|
+
static NAN_METHOD(New);
|
|
14
|
+
static NAN_METHOD(AddColorStop);
|
|
15
|
+
Gradient(double x0, double y0, double x1, double y1);
|
|
16
|
+
Gradient(double x0, double y0, double r0, double x1, double y1, double r1);
|
|
17
|
+
inline cairo_pattern_t *pattern(){ return _pattern; }
|
|
18
|
+
|
|
19
|
+
private:
|
|
20
|
+
~Gradient();
|
|
21
|
+
cairo_pattern_t *_pattern;
|
|
22
|
+
};
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
// Copyright (c) 2010 LearnBoost <tj@learnboost.com>
|
|
2
|
+
|
|
3
|
+
#include "CanvasPattern.h"
|
|
4
|
+
|
|
5
|
+
#include "Canvas.h"
|
|
6
|
+
#include "Image.h"
|
|
7
|
+
|
|
8
|
+
using namespace v8;
|
|
9
|
+
|
|
10
|
+
const cairo_user_data_key_t *pattern_repeat_key;
|
|
11
|
+
|
|
12
|
+
Nan::Persistent<FunctionTemplate> Pattern::constructor;
|
|
13
|
+
Nan::Persistent<Function> Pattern::_DOMMatrix;
|
|
14
|
+
|
|
15
|
+
/*
|
|
16
|
+
* Initialize CanvasPattern.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
void
|
|
20
|
+
Pattern::Initialize(Nan::ADDON_REGISTER_FUNCTION_ARGS_TYPE target) {
|
|
21
|
+
Nan::HandleScope scope;
|
|
22
|
+
|
|
23
|
+
// Constructor
|
|
24
|
+
Local<FunctionTemplate> ctor = Nan::New<FunctionTemplate>(Pattern::New);
|
|
25
|
+
constructor.Reset(ctor);
|
|
26
|
+
ctor->InstanceTemplate()->SetInternalFieldCount(1);
|
|
27
|
+
ctor->SetClassName(Nan::New("CanvasPattern").ToLocalChecked());
|
|
28
|
+
Nan::SetPrototypeMethod(ctor, "setTransform", SetTransform);
|
|
29
|
+
|
|
30
|
+
// Prototype
|
|
31
|
+
Local<Context> ctx = Nan::GetCurrentContext();
|
|
32
|
+
Nan::Set(target, Nan::New("CanvasPattern").ToLocalChecked(), ctor->GetFunction(ctx).ToLocalChecked());
|
|
33
|
+
Nan::Set(target, Nan::New("CanvasPatternInit").ToLocalChecked(), Nan::New<Function>(SaveExternalModules));
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/*
|
|
37
|
+
* Save some external modules as private references.
|
|
38
|
+
*/
|
|
39
|
+
|
|
40
|
+
NAN_METHOD(Pattern::SaveExternalModules) {
|
|
41
|
+
_DOMMatrix.Reset(Nan::To<Function>(info[0]).ToLocalChecked());
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/*
|
|
45
|
+
* Initialize a new CanvasPattern.
|
|
46
|
+
*/
|
|
47
|
+
|
|
48
|
+
NAN_METHOD(Pattern::New) {
|
|
49
|
+
if (!info.IsConstructCall()) {
|
|
50
|
+
return Nan::ThrowTypeError("Class constructors cannot be invoked without 'new'");
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
cairo_surface_t *surface;
|
|
54
|
+
|
|
55
|
+
Local<Object> obj = Nan::To<Object>(info[0]).ToLocalChecked();
|
|
56
|
+
|
|
57
|
+
// Image
|
|
58
|
+
if (Nan::New(Image::constructor)->HasInstance(obj)) {
|
|
59
|
+
Image *img = Nan::ObjectWrap::Unwrap<Image>(obj);
|
|
60
|
+
if (!img->isComplete()) {
|
|
61
|
+
return Nan::ThrowError("Image given has not completed loading");
|
|
62
|
+
}
|
|
63
|
+
surface = img->surface();
|
|
64
|
+
|
|
65
|
+
// Canvas
|
|
66
|
+
} else if (Nan::New(Canvas::constructor)->HasInstance(obj)) {
|
|
67
|
+
Canvas *canvas = Nan::ObjectWrap::Unwrap<Canvas>(obj);
|
|
68
|
+
surface = canvas->surface();
|
|
69
|
+
// Invalid
|
|
70
|
+
} else {
|
|
71
|
+
return Nan::ThrowTypeError("Image or Canvas expected");
|
|
72
|
+
}
|
|
73
|
+
repeat_type_t repeat = REPEAT;
|
|
74
|
+
if (0 == strcmp("no-repeat", *Nan::Utf8String(info[1]))) {
|
|
75
|
+
repeat = NO_REPEAT;
|
|
76
|
+
} else if (0 == strcmp("repeat-x", *Nan::Utf8String(info[1]))) {
|
|
77
|
+
repeat = REPEAT_X;
|
|
78
|
+
} else if (0 == strcmp("repeat-y", *Nan::Utf8String(info[1]))) {
|
|
79
|
+
repeat = REPEAT_Y;
|
|
80
|
+
}
|
|
81
|
+
Pattern *pattern = new Pattern(surface, repeat);
|
|
82
|
+
pattern->Wrap(info.This());
|
|
83
|
+
info.GetReturnValue().Set(info.This());
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/*
|
|
87
|
+
* Set the pattern-space to user-space transform.
|
|
88
|
+
*/
|
|
89
|
+
NAN_METHOD(Pattern::SetTransform) {
|
|
90
|
+
Pattern *pattern = Nan::ObjectWrap::Unwrap<Pattern>(info.This());
|
|
91
|
+
Local<Context> ctx = Nan::GetCurrentContext();
|
|
92
|
+
Local<Object> mat = Nan::To<Object>(info[0]).ToLocalChecked();
|
|
93
|
+
|
|
94
|
+
#if NODE_MAJOR_VERSION >= 8
|
|
95
|
+
if (!mat->InstanceOf(ctx, _DOMMatrix.Get(Isolate::GetCurrent())).ToChecked()) {
|
|
96
|
+
return Nan::ThrowTypeError("Expected DOMMatrix");
|
|
97
|
+
}
|
|
98
|
+
#endif
|
|
99
|
+
|
|
100
|
+
cairo_matrix_t matrix;
|
|
101
|
+
cairo_matrix_init(&matrix,
|
|
102
|
+
Nan::To<double>(Nan::Get(mat, Nan::New("a").ToLocalChecked()).ToLocalChecked()).FromMaybe(1),
|
|
103
|
+
Nan::To<double>(Nan::Get(mat, Nan::New("b").ToLocalChecked()).ToLocalChecked()).FromMaybe(0),
|
|
104
|
+
Nan::To<double>(Nan::Get(mat, Nan::New("c").ToLocalChecked()).ToLocalChecked()).FromMaybe(0),
|
|
105
|
+
Nan::To<double>(Nan::Get(mat, Nan::New("d").ToLocalChecked()).ToLocalChecked()).FromMaybe(1),
|
|
106
|
+
Nan::To<double>(Nan::Get(mat, Nan::New("e").ToLocalChecked()).ToLocalChecked()).FromMaybe(0),
|
|
107
|
+
Nan::To<double>(Nan::Get(mat, Nan::New("f").ToLocalChecked()).ToLocalChecked()).FromMaybe(0)
|
|
108
|
+
);
|
|
109
|
+
|
|
110
|
+
cairo_matrix_invert(&matrix);
|
|
111
|
+
cairo_pattern_set_matrix(pattern->_pattern, &matrix);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
/*
|
|
116
|
+
* Initialize pattern.
|
|
117
|
+
*/
|
|
118
|
+
|
|
119
|
+
Pattern::Pattern(cairo_surface_t *surface, repeat_type_t repeat) {
|
|
120
|
+
_pattern = cairo_pattern_create_for_surface(surface);
|
|
121
|
+
_repeat = repeat;
|
|
122
|
+
cairo_pattern_set_user_data(_pattern, pattern_repeat_key, &_repeat, NULL);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
repeat_type_t Pattern::get_repeat_type_for_cairo_pattern(cairo_pattern_t *pattern) {
|
|
126
|
+
void *ud = cairo_pattern_get_user_data(pattern, pattern_repeat_key);
|
|
127
|
+
return *reinterpret_cast<repeat_type_t*>(ud);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/*
|
|
131
|
+
* Destroy the pattern.
|
|
132
|
+
*/
|
|
133
|
+
|
|
134
|
+
Pattern::~Pattern() {
|
|
135
|
+
cairo_pattern_destroy(_pattern);
|
|
136
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
// Copyright (c) 2011 LearnBoost <tj@learnboost.com>
|
|
2
|
+
|
|
3
|
+
#pragma once
|
|
4
|
+
|
|
5
|
+
#include <cairo.h>
|
|
6
|
+
#include <nan.h>
|
|
7
|
+
#include <v8.h>
|
|
8
|
+
|
|
9
|
+
/*
|
|
10
|
+
* Canvas types.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
typedef enum {
|
|
14
|
+
NO_REPEAT, // match CAIRO_EXTEND_NONE
|
|
15
|
+
REPEAT, // match CAIRO_EXTEND_REPEAT
|
|
16
|
+
REPEAT_X, // needs custom processing
|
|
17
|
+
REPEAT_Y // needs custom processing
|
|
18
|
+
} repeat_type_t;
|
|
19
|
+
|
|
20
|
+
extern const cairo_user_data_key_t *pattern_repeat_key;
|
|
21
|
+
|
|
22
|
+
class Pattern: public Nan::ObjectWrap {
|
|
23
|
+
public:
|
|
24
|
+
static Nan::Persistent<v8::FunctionTemplate> constructor;
|
|
25
|
+
static Nan::Persistent<v8::Function> _DOMMatrix;
|
|
26
|
+
static void Initialize(Nan::ADDON_REGISTER_FUNCTION_ARGS_TYPE target);
|
|
27
|
+
static NAN_METHOD(New);
|
|
28
|
+
static NAN_METHOD(SaveExternalModules);
|
|
29
|
+
static NAN_METHOD(SetTransform);
|
|
30
|
+
static repeat_type_t get_repeat_type_for_cairo_pattern(cairo_pattern_t *pattern);
|
|
31
|
+
Pattern(cairo_surface_t *surface, repeat_type_t repeat);
|
|
32
|
+
inline cairo_pattern_t *pattern(){ return _pattern; }
|
|
33
|
+
private:
|
|
34
|
+
~Pattern();
|
|
35
|
+
cairo_pattern_t *_pattern;
|
|
36
|
+
repeat_type_t _repeat;
|
|
37
|
+
};
|