@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/PNG.h
ADDED
|
@@ -0,0 +1,292 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
#include <cairo.h>
|
|
4
|
+
#include "closure.h"
|
|
5
|
+
#include <cmath> // round
|
|
6
|
+
#include <cstdlib>
|
|
7
|
+
#include <cstring>
|
|
8
|
+
#include <png.h>
|
|
9
|
+
#include <pngconf.h>
|
|
10
|
+
|
|
11
|
+
#if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__)
|
|
12
|
+
#define likely(expr) (__builtin_expect (!!(expr), 1))
|
|
13
|
+
#define unlikely(expr) (__builtin_expect (!!(expr), 0))
|
|
14
|
+
#else
|
|
15
|
+
#define likely(expr) (expr)
|
|
16
|
+
#define unlikely(expr) (expr)
|
|
17
|
+
#endif
|
|
18
|
+
|
|
19
|
+
static void canvas_png_flush(png_structp png_ptr) {
|
|
20
|
+
/* Do nothing; fflush() is said to be just a waste of energy. */
|
|
21
|
+
(void) png_ptr; /* Stifle compiler warning */
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/* Converts native endian xRGB => RGBx bytes */
|
|
25
|
+
static void canvas_convert_data_to_bytes(png_structp png, png_row_infop row_info, png_bytep data) {
|
|
26
|
+
unsigned int i;
|
|
27
|
+
|
|
28
|
+
for (i = 0; i < row_info->rowbytes; i += 4) {
|
|
29
|
+
uint8_t *b = &data[i];
|
|
30
|
+
uint32_t pixel;
|
|
31
|
+
|
|
32
|
+
memcpy(&pixel, b, sizeof (uint32_t));
|
|
33
|
+
|
|
34
|
+
b[0] = (pixel & 0xff0000) >> 16;
|
|
35
|
+
b[1] = (pixel & 0x00ff00) >> 8;
|
|
36
|
+
b[2] = (pixel & 0x0000ff) >> 0;
|
|
37
|
+
b[3] = 0;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/* Unpremultiplies data and converts native endian ARGB => RGBA bytes */
|
|
42
|
+
static void canvas_unpremultiply_data(png_structp png, png_row_infop row_info, png_bytep data) {
|
|
43
|
+
unsigned int i;
|
|
44
|
+
|
|
45
|
+
for (i = 0; i < row_info->rowbytes; i += 4) {
|
|
46
|
+
uint8_t *b = &data[i];
|
|
47
|
+
uint32_t pixel;
|
|
48
|
+
uint8_t alpha;
|
|
49
|
+
|
|
50
|
+
memcpy(&pixel, b, sizeof (uint32_t));
|
|
51
|
+
alpha = (pixel & 0xff000000) >> 24;
|
|
52
|
+
if (alpha == 0) {
|
|
53
|
+
b[0] = b[1] = b[2] = b[3] = 0;
|
|
54
|
+
} else {
|
|
55
|
+
b[0] = (((pixel & 0xff0000) >> 16) * 255 + alpha / 2) / alpha;
|
|
56
|
+
b[1] = (((pixel & 0x00ff00) >> 8) * 255 + alpha / 2) / alpha;
|
|
57
|
+
b[2] = (((pixel & 0x0000ff) >> 0) * 255 + alpha / 2) / alpha;
|
|
58
|
+
b[3] = alpha;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/* Converts RGB16_565 format data to RGBA32 */
|
|
64
|
+
static void canvas_convert_565_to_888(png_structp png, png_row_infop row_info, png_bytep data) {
|
|
65
|
+
// Loop in reverse to unpack in-place.
|
|
66
|
+
for (ptrdiff_t col = row_info->width - 1; col >= 0; col--) {
|
|
67
|
+
uint8_t* src = &data[col * sizeof(uint16_t)];
|
|
68
|
+
uint8_t* dst = &data[col * 3];
|
|
69
|
+
uint16_t pixel;
|
|
70
|
+
|
|
71
|
+
memcpy(&pixel, src, sizeof(uint16_t));
|
|
72
|
+
|
|
73
|
+
// Convert and rescale to the full 0-255 range
|
|
74
|
+
// See http://stackoverflow.com/a/29326693
|
|
75
|
+
const uint8_t red5 = (pixel & 0xF800) >> 11;
|
|
76
|
+
const uint8_t green6 = (pixel & 0x7E0) >> 5;
|
|
77
|
+
const uint8_t blue5 = (pixel & 0x001F);
|
|
78
|
+
|
|
79
|
+
dst[0] = ((red5 * 255 + 15) / 31);
|
|
80
|
+
dst[1] = ((green6 * 255 + 31) / 63);
|
|
81
|
+
dst[2] = ((blue5 * 255 + 15) / 31);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
struct canvas_png_write_closure_t {
|
|
86
|
+
cairo_write_func_t write_func;
|
|
87
|
+
PngClosure* closure;
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
#ifdef PNG_SETJMP_SUPPORTED
|
|
91
|
+
bool setjmp_wrapper(png_structp png) {
|
|
92
|
+
return setjmp(png_jmpbuf(png));
|
|
93
|
+
}
|
|
94
|
+
#endif
|
|
95
|
+
|
|
96
|
+
static cairo_status_t canvas_write_png(cairo_surface_t *surface, png_rw_ptr write_func, canvas_png_write_closure_t *closure) {
|
|
97
|
+
unsigned int i;
|
|
98
|
+
cairo_status_t status = CAIRO_STATUS_SUCCESS;
|
|
99
|
+
uint8_t *data;
|
|
100
|
+
png_structp png;
|
|
101
|
+
png_infop info;
|
|
102
|
+
png_bytep *volatile rows = NULL;
|
|
103
|
+
png_color_16 white;
|
|
104
|
+
int png_color_type;
|
|
105
|
+
int bpc;
|
|
106
|
+
unsigned int width = cairo_image_surface_get_width(surface);
|
|
107
|
+
unsigned int height = cairo_image_surface_get_height(surface);
|
|
108
|
+
|
|
109
|
+
data = cairo_image_surface_get_data(surface);
|
|
110
|
+
if (data == NULL) {
|
|
111
|
+
status = CAIRO_STATUS_SURFACE_TYPE_MISMATCH;
|
|
112
|
+
return status;
|
|
113
|
+
}
|
|
114
|
+
cairo_surface_flush(surface);
|
|
115
|
+
|
|
116
|
+
if (width == 0 || height == 0) {
|
|
117
|
+
status = CAIRO_STATUS_WRITE_ERROR;
|
|
118
|
+
return status;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
rows = (png_bytep *) malloc(height * sizeof (png_byte*));
|
|
122
|
+
if (unlikely(rows == NULL)) {
|
|
123
|
+
status = CAIRO_STATUS_NO_MEMORY;
|
|
124
|
+
return status;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
int stride = cairo_image_surface_get_stride(surface);
|
|
128
|
+
for (i = 0; i < height; i++) {
|
|
129
|
+
rows[i] = (png_byte *) data + i * stride;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
#ifdef PNG_USER_MEM_SUPPORTED
|
|
133
|
+
png = png_create_write_struct_2(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL, NULL, NULL, NULL);
|
|
134
|
+
#else
|
|
135
|
+
png = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
|
|
136
|
+
#endif
|
|
137
|
+
|
|
138
|
+
if (unlikely(png == NULL)) {
|
|
139
|
+
status = CAIRO_STATUS_NO_MEMORY;
|
|
140
|
+
free(rows);
|
|
141
|
+
return status;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
info = png_create_info_struct (png);
|
|
145
|
+
if (unlikely(info == NULL)) {
|
|
146
|
+
status = CAIRO_STATUS_NO_MEMORY;
|
|
147
|
+
png_destroy_write_struct(&png, &info);
|
|
148
|
+
free(rows);
|
|
149
|
+
return status;
|
|
150
|
+
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
#ifdef PNG_SETJMP_SUPPORTED
|
|
154
|
+
if (setjmp_wrapper(png)) {
|
|
155
|
+
png_destroy_write_struct(&png, &info);
|
|
156
|
+
free(rows);
|
|
157
|
+
return status;
|
|
158
|
+
}
|
|
159
|
+
#endif
|
|
160
|
+
|
|
161
|
+
png_set_write_fn(png, closure, write_func, canvas_png_flush);
|
|
162
|
+
png_set_compression_level(png, closure->closure->compressionLevel);
|
|
163
|
+
png_set_filter(png, 0, closure->closure->filters);
|
|
164
|
+
if (closure->closure->resolution != 0) {
|
|
165
|
+
uint32_t res = static_cast<uint32_t>(round(static_cast<double>(closure->closure->resolution) * 39.3701));
|
|
166
|
+
png_set_pHYs(png, info, res, res, PNG_RESOLUTION_METER);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
cairo_format_t format = cairo_image_surface_get_format(surface);
|
|
170
|
+
|
|
171
|
+
switch (format) {
|
|
172
|
+
case CAIRO_FORMAT_ARGB32:
|
|
173
|
+
bpc = 8;
|
|
174
|
+
png_color_type = PNG_COLOR_TYPE_RGB_ALPHA;
|
|
175
|
+
break;
|
|
176
|
+
#ifdef CAIRO_FORMAT_RGB30
|
|
177
|
+
case CAIRO_FORMAT_RGB30:
|
|
178
|
+
bpc = 10;
|
|
179
|
+
png_color_type = PNG_COLOR_TYPE_RGB;
|
|
180
|
+
break;
|
|
181
|
+
#endif
|
|
182
|
+
case CAIRO_FORMAT_RGB24:
|
|
183
|
+
bpc = 8;
|
|
184
|
+
png_color_type = PNG_COLOR_TYPE_RGB;
|
|
185
|
+
break;
|
|
186
|
+
case CAIRO_FORMAT_A8:
|
|
187
|
+
bpc = 8;
|
|
188
|
+
png_color_type = PNG_COLOR_TYPE_GRAY;
|
|
189
|
+
break;
|
|
190
|
+
case CAIRO_FORMAT_A1:
|
|
191
|
+
bpc = 1;
|
|
192
|
+
png_color_type = PNG_COLOR_TYPE_GRAY;
|
|
193
|
+
#ifndef WORDS_BIGENDIAN
|
|
194
|
+
png_set_packswap(png);
|
|
195
|
+
#endif
|
|
196
|
+
break;
|
|
197
|
+
case CAIRO_FORMAT_RGB16_565:
|
|
198
|
+
bpc = 8; // 565 gets upconverted to 888
|
|
199
|
+
png_color_type = PNG_COLOR_TYPE_RGB;
|
|
200
|
+
break;
|
|
201
|
+
case CAIRO_FORMAT_INVALID:
|
|
202
|
+
default:
|
|
203
|
+
status = CAIRO_STATUS_INVALID_FORMAT;
|
|
204
|
+
png_destroy_write_struct(&png, &info);
|
|
205
|
+
free(rows);
|
|
206
|
+
return status;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
if ((format == CAIRO_FORMAT_A8 || format == CAIRO_FORMAT_A1) &&
|
|
210
|
+
closure->closure->palette != NULL) {
|
|
211
|
+
png_color_type = PNG_COLOR_TYPE_PALETTE;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
png_set_IHDR(png, info, width, height, bpc, png_color_type, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
|
|
215
|
+
|
|
216
|
+
if (png_color_type == PNG_COLOR_TYPE_PALETTE) {
|
|
217
|
+
size_t nColors = closure->closure->nPaletteColors;
|
|
218
|
+
uint8_t* colors = closure->closure->palette;
|
|
219
|
+
uint8_t backgroundIndex = closure->closure->backgroundIndex;
|
|
220
|
+
png_colorp pngPalette = (png_colorp)png_malloc(png, nColors * sizeof(png_colorp));
|
|
221
|
+
png_bytep transparency = (png_bytep)png_malloc(png, nColors * sizeof(png_bytep));
|
|
222
|
+
for (i = 0; i < nColors; i++) {
|
|
223
|
+
pngPalette[i].red = colors[4 * i];
|
|
224
|
+
pngPalette[i].green = colors[4 * i + 1];
|
|
225
|
+
pngPalette[i].blue = colors[4 * i + 2];
|
|
226
|
+
transparency[i] = colors[4 * i + 3];
|
|
227
|
+
}
|
|
228
|
+
png_set_PLTE(png, info, pngPalette, nColors);
|
|
229
|
+
png_set_tRNS(png, info, transparency, nColors, NULL);
|
|
230
|
+
png_set_packing(png); // pack pixels
|
|
231
|
+
// have libpng free palette and trans:
|
|
232
|
+
png_data_freer(png, info, PNG_DESTROY_WILL_FREE_DATA, PNG_FREE_PLTE | PNG_FREE_TRNS);
|
|
233
|
+
png_color_16 bkg;
|
|
234
|
+
bkg.index = backgroundIndex;
|
|
235
|
+
png_set_bKGD(png, info, &bkg);
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
if (png_color_type != PNG_COLOR_TYPE_PALETTE) {
|
|
239
|
+
white.gray = (1 << bpc) - 1;
|
|
240
|
+
white.red = white.blue = white.green = white.gray;
|
|
241
|
+
png_set_bKGD(png, info, &white);
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/* We have to call png_write_info() before setting up the write
|
|
245
|
+
* transformation, since it stores data internally in 'png'
|
|
246
|
+
* that is needed for the write transformation functions to work.
|
|
247
|
+
*/
|
|
248
|
+
png_write_info(png, info);
|
|
249
|
+
if (png_color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
|
|
250
|
+
png_set_write_user_transform_fn(png, canvas_unpremultiply_data);
|
|
251
|
+
} else if (format == CAIRO_FORMAT_RGB16_565) {
|
|
252
|
+
png_set_write_user_transform_fn(png, canvas_convert_565_to_888);
|
|
253
|
+
} else if (png_color_type == PNG_COLOR_TYPE_RGB) {
|
|
254
|
+
png_set_write_user_transform_fn(png, canvas_convert_data_to_bytes);
|
|
255
|
+
png_set_filler(png, 0, PNG_FILLER_AFTER);
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
png_write_image(png, rows);
|
|
259
|
+
png_write_end(png, info);
|
|
260
|
+
|
|
261
|
+
png_destroy_write_struct(&png, &info);
|
|
262
|
+
free(rows);
|
|
263
|
+
return status;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
static void canvas_stream_write_func(png_structp png, png_bytep data, png_size_t size) {
|
|
267
|
+
cairo_status_t status;
|
|
268
|
+
struct canvas_png_write_closure_t *png_closure;
|
|
269
|
+
|
|
270
|
+
png_closure = (struct canvas_png_write_closure_t *) png_get_io_ptr(png);
|
|
271
|
+
status = png_closure->write_func(png_closure->closure, data, size);
|
|
272
|
+
if (unlikely(status)) {
|
|
273
|
+
cairo_status_t *error = (cairo_status_t *) png_get_error_ptr(png);
|
|
274
|
+
if (*error == CAIRO_STATUS_SUCCESS) {
|
|
275
|
+
*error = status;
|
|
276
|
+
}
|
|
277
|
+
png_error(png, NULL);
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
static cairo_status_t canvas_write_to_png_stream(cairo_surface_t *surface, cairo_write_func_t write_func, PngClosure* closure) {
|
|
282
|
+
struct canvas_png_write_closure_t png_closure;
|
|
283
|
+
|
|
284
|
+
if (cairo_surface_status(surface)) {
|
|
285
|
+
return cairo_surface_status(surface);
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
png_closure.write_func = write_func;
|
|
289
|
+
png_closure.closure = closure;
|
|
290
|
+
|
|
291
|
+
return canvas_write_png(surface, canvas_stream_write_func, &png_closure);
|
|
292
|
+
}
|
package/src/Point.h
ADDED
package/src/Util.h
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
#include <cctype>
|
|
4
|
+
|
|
5
|
+
inline bool streq_casein(std::string& str1, std::string& str2) {
|
|
6
|
+
return str1.size() == str2.size() && std::equal(str1.begin(), str1.end(), str2.begin(), [](char& c1, char& c2) {
|
|
7
|
+
return c1 == c2 || std::toupper(c1) == std::toupper(c2);
|
|
8
|
+
});
|
|
9
|
+
}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
#include "Backend.h"
|
|
2
|
+
#include <string>
|
|
3
|
+
|
|
4
|
+
Backend::Backend(std::string name, int width, int height)
|
|
5
|
+
: name(name)
|
|
6
|
+
, width(width)
|
|
7
|
+
, height(height)
|
|
8
|
+
{}
|
|
9
|
+
|
|
10
|
+
Backend::~Backend()
|
|
11
|
+
{
|
|
12
|
+
this->destroySurface();
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
void Backend::init(const Nan::FunctionCallbackInfo<v8::Value> &info) {
|
|
16
|
+
int width = 0;
|
|
17
|
+
int height = 0;
|
|
18
|
+
if (info[0]->IsNumber()) width = Nan::To<uint32_t>(info[0]).FromMaybe(0);
|
|
19
|
+
if (info[1]->IsNumber()) height = Nan::To<uint32_t>(info[1]).FromMaybe(0);
|
|
20
|
+
|
|
21
|
+
Backend *backend = construct(width, height);
|
|
22
|
+
|
|
23
|
+
backend->Wrap(info.This());
|
|
24
|
+
info.GetReturnValue().Set(info.This());
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
void Backend::setCanvas(Canvas* _canvas)
|
|
28
|
+
{
|
|
29
|
+
this->canvas = _canvas;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
cairo_surface_t* Backend::recreateSurface()
|
|
34
|
+
{
|
|
35
|
+
this->destroySurface();
|
|
36
|
+
|
|
37
|
+
return this->createSurface();
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
DLL_PUBLIC cairo_surface_t* Backend::getSurface() {
|
|
41
|
+
if (!surface) createSurface();
|
|
42
|
+
return surface;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
void Backend::destroySurface()
|
|
46
|
+
{
|
|
47
|
+
if(this->surface)
|
|
48
|
+
{
|
|
49
|
+
cairo_surface_destroy(this->surface);
|
|
50
|
+
this->surface = NULL;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
std::string Backend::getName()
|
|
56
|
+
{
|
|
57
|
+
return name;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
int Backend::getWidth()
|
|
61
|
+
{
|
|
62
|
+
return this->width;
|
|
63
|
+
}
|
|
64
|
+
void Backend::setWidth(int width_)
|
|
65
|
+
{
|
|
66
|
+
this->width = width_;
|
|
67
|
+
this->recreateSurface();
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
int Backend::getHeight()
|
|
71
|
+
{
|
|
72
|
+
return this->height;
|
|
73
|
+
}
|
|
74
|
+
void Backend::setHeight(int height_)
|
|
75
|
+
{
|
|
76
|
+
this->height = height_;
|
|
77
|
+
this->recreateSurface();
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
bool Backend::isSurfaceValid(){
|
|
81
|
+
bool hadSurface = surface != NULL;
|
|
82
|
+
bool isValid = true;
|
|
83
|
+
|
|
84
|
+
cairo_status_t status = cairo_surface_status(getSurface());
|
|
85
|
+
|
|
86
|
+
if (status != CAIRO_STATUS_SUCCESS) {
|
|
87
|
+
error = cairo_status_to_string(status);
|
|
88
|
+
isValid = false;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
if (!hadSurface)
|
|
92
|
+
destroySurface();
|
|
93
|
+
|
|
94
|
+
return isValid;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
BackendOperationNotAvailable::BackendOperationNotAvailable(Backend* backend,
|
|
99
|
+
std::string operation_name)
|
|
100
|
+
: backend(backend)
|
|
101
|
+
, operation_name(operation_name)
|
|
102
|
+
{
|
|
103
|
+
msg = "operation " + operation_name +
|
|
104
|
+
" not supported by backend " + backend->getName();
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
BackendOperationNotAvailable::~BackendOperationNotAvailable() throw() {};
|
|
108
|
+
|
|
109
|
+
const char* BackendOperationNotAvailable::what() const throw()
|
|
110
|
+
{
|
|
111
|
+
return msg.c_str();
|
|
112
|
+
};
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
#include <cairo.h>
|
|
4
|
+
#include "../dll_visibility.h"
|
|
5
|
+
#include <exception>
|
|
6
|
+
#include <nan.h>
|
|
7
|
+
#include <string>
|
|
8
|
+
#include <v8.h>
|
|
9
|
+
|
|
10
|
+
class Canvas;
|
|
11
|
+
|
|
12
|
+
class Backend : public Nan::ObjectWrap
|
|
13
|
+
{
|
|
14
|
+
private:
|
|
15
|
+
const std::string name;
|
|
16
|
+
const char* error = NULL;
|
|
17
|
+
|
|
18
|
+
protected:
|
|
19
|
+
int width;
|
|
20
|
+
int height;
|
|
21
|
+
cairo_surface_t* surface = nullptr;
|
|
22
|
+
Canvas* canvas = nullptr;
|
|
23
|
+
|
|
24
|
+
Backend(std::string name, int width, int height);
|
|
25
|
+
static void init(const Nan::FunctionCallbackInfo<v8::Value> &info);
|
|
26
|
+
static Backend *construct(int width, int height){ return nullptr; }
|
|
27
|
+
|
|
28
|
+
public:
|
|
29
|
+
virtual ~Backend();
|
|
30
|
+
|
|
31
|
+
void setCanvas(Canvas* canvas);
|
|
32
|
+
|
|
33
|
+
virtual cairo_surface_t* createSurface() = 0;
|
|
34
|
+
virtual cairo_surface_t* recreateSurface();
|
|
35
|
+
|
|
36
|
+
DLL_PUBLIC cairo_surface_t* getSurface();
|
|
37
|
+
virtual void destroySurface();
|
|
38
|
+
|
|
39
|
+
DLL_PUBLIC std::string getName();
|
|
40
|
+
|
|
41
|
+
DLL_PUBLIC int getWidth();
|
|
42
|
+
virtual void setWidth(int width);
|
|
43
|
+
|
|
44
|
+
DLL_PUBLIC int getHeight();
|
|
45
|
+
virtual void setHeight(int height);
|
|
46
|
+
|
|
47
|
+
// Overridden by ImageBackend. SVG and PDF thus always return INVALID.
|
|
48
|
+
virtual cairo_format_t getFormat() {
|
|
49
|
+
return CAIRO_FORMAT_INVALID;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
bool isSurfaceValid();
|
|
53
|
+
inline const char* getError(){ return error; }
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
class BackendOperationNotAvailable: public std::exception
|
|
58
|
+
{
|
|
59
|
+
private:
|
|
60
|
+
Backend* backend;
|
|
61
|
+
std::string operation_name;
|
|
62
|
+
std::string msg;
|
|
63
|
+
|
|
64
|
+
public:
|
|
65
|
+
BackendOperationNotAvailable(Backend* backend, std::string operation_name);
|
|
66
|
+
~BackendOperationNotAvailable() throw();
|
|
67
|
+
|
|
68
|
+
const char* what() const throw();
|
|
69
|
+
};
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
#include "ImageBackend.h"
|
|
2
|
+
|
|
3
|
+
using namespace v8;
|
|
4
|
+
|
|
5
|
+
ImageBackend::ImageBackend(int width, int height)
|
|
6
|
+
: Backend("image", width, height)
|
|
7
|
+
{}
|
|
8
|
+
|
|
9
|
+
Backend *ImageBackend::construct(int width, int height){
|
|
10
|
+
return new ImageBackend(width, height);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
// This returns an approximate value only, suitable for Nan::AdjustExternalMemory.
|
|
14
|
+
// The formats that don't map to intrinsic types (RGB30, A1) round up.
|
|
15
|
+
int32_t ImageBackend::approxBytesPerPixel() {
|
|
16
|
+
switch (format) {
|
|
17
|
+
case CAIRO_FORMAT_ARGB32:
|
|
18
|
+
case CAIRO_FORMAT_RGB24:
|
|
19
|
+
return 4;
|
|
20
|
+
#ifdef CAIRO_FORMAT_RGB30
|
|
21
|
+
case CAIRO_FORMAT_RGB30:
|
|
22
|
+
return 3;
|
|
23
|
+
#endif
|
|
24
|
+
case CAIRO_FORMAT_RGB16_565:
|
|
25
|
+
return 2;
|
|
26
|
+
case CAIRO_FORMAT_A8:
|
|
27
|
+
case CAIRO_FORMAT_A1:
|
|
28
|
+
return 1;
|
|
29
|
+
default:
|
|
30
|
+
return 0;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
cairo_surface_t* ImageBackend::createSurface() {
|
|
35
|
+
assert(!surface);
|
|
36
|
+
surface = cairo_image_surface_create(format, width, height);
|
|
37
|
+
assert(surface);
|
|
38
|
+
Nan::AdjustExternalMemory(approxBytesPerPixel() * width * height);
|
|
39
|
+
return surface;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
void ImageBackend::destroySurface() {
|
|
43
|
+
if (surface) {
|
|
44
|
+
cairo_surface_destroy(surface);
|
|
45
|
+
surface = nullptr;
|
|
46
|
+
Nan::AdjustExternalMemory(-approxBytesPerPixel() * width * height);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
cairo_format_t ImageBackend::getFormat() {
|
|
51
|
+
return format;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
void ImageBackend::setFormat(cairo_format_t _format) {
|
|
55
|
+
this->format = _format;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
Nan::Persistent<FunctionTemplate> ImageBackend::constructor;
|
|
59
|
+
|
|
60
|
+
void ImageBackend::Initialize(Local<Object> target) {
|
|
61
|
+
Nan::HandleScope scope;
|
|
62
|
+
|
|
63
|
+
Local<FunctionTemplate> ctor = Nan::New<FunctionTemplate>(ImageBackend::New);
|
|
64
|
+
ImageBackend::constructor.Reset(ctor);
|
|
65
|
+
ctor->InstanceTemplate()->SetInternalFieldCount(1);
|
|
66
|
+
ctor->SetClassName(Nan::New<String>("ImageBackend").ToLocalChecked());
|
|
67
|
+
Nan::Set(target,
|
|
68
|
+
Nan::New<String>("ImageBackend").ToLocalChecked(),
|
|
69
|
+
Nan::GetFunction(ctor).ToLocalChecked()).Check();
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
NAN_METHOD(ImageBackend::New) {
|
|
73
|
+
init(info);
|
|
74
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
#include "Backend.h"
|
|
4
|
+
#include <v8.h>
|
|
5
|
+
|
|
6
|
+
class ImageBackend : public Backend
|
|
7
|
+
{
|
|
8
|
+
private:
|
|
9
|
+
cairo_surface_t* createSurface();
|
|
10
|
+
void destroySurface();
|
|
11
|
+
cairo_format_t format = DEFAULT_FORMAT;
|
|
12
|
+
|
|
13
|
+
public:
|
|
14
|
+
ImageBackend(int width, int height);
|
|
15
|
+
static Backend *construct(int width, int height);
|
|
16
|
+
|
|
17
|
+
cairo_format_t getFormat();
|
|
18
|
+
void setFormat(cairo_format_t format);
|
|
19
|
+
|
|
20
|
+
int32_t approxBytesPerPixel();
|
|
21
|
+
|
|
22
|
+
static Nan::Persistent<v8::FunctionTemplate> constructor;
|
|
23
|
+
static void Initialize(v8::Local<v8::Object> target);
|
|
24
|
+
static NAN_METHOD(New);
|
|
25
|
+
const static cairo_format_t DEFAULT_FORMAT = CAIRO_FORMAT_ARGB32;
|
|
26
|
+
};
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
#include "PdfBackend.h"
|
|
2
|
+
|
|
3
|
+
#include <cairo-pdf.h>
|
|
4
|
+
#include "../Canvas.h"
|
|
5
|
+
#include "../closure.h"
|
|
6
|
+
|
|
7
|
+
using namespace v8;
|
|
8
|
+
|
|
9
|
+
PdfBackend::PdfBackend(int width, int height)
|
|
10
|
+
: Backend("pdf", width, height) {
|
|
11
|
+
createSurface();
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
PdfBackend::~PdfBackend() {
|
|
15
|
+
cairo_surface_finish(surface);
|
|
16
|
+
if (_closure) delete _closure;
|
|
17
|
+
destroySurface();
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
Backend *PdfBackend::construct(int width, int height){
|
|
21
|
+
return new PdfBackend(width, height);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
cairo_surface_t* PdfBackend::createSurface() {
|
|
25
|
+
if (!_closure) _closure = new PdfSvgClosure(canvas);
|
|
26
|
+
surface = cairo_pdf_surface_create_for_stream(PdfSvgClosure::writeVec, _closure, width, height);
|
|
27
|
+
return surface;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
cairo_surface_t* PdfBackend::recreateSurface() {
|
|
31
|
+
cairo_pdf_surface_set_size(surface, width, height);
|
|
32
|
+
|
|
33
|
+
return surface;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
Nan::Persistent<FunctionTemplate> PdfBackend::constructor;
|
|
38
|
+
|
|
39
|
+
void PdfBackend::Initialize(Local<Object> target) {
|
|
40
|
+
Nan::HandleScope scope;
|
|
41
|
+
|
|
42
|
+
Local<FunctionTemplate> ctor = Nan::New<FunctionTemplate>(PdfBackend::New);
|
|
43
|
+
PdfBackend::constructor.Reset(ctor);
|
|
44
|
+
ctor->InstanceTemplate()->SetInternalFieldCount(1);
|
|
45
|
+
ctor->SetClassName(Nan::New<String>("PdfBackend").ToLocalChecked());
|
|
46
|
+
Nan::Set(target,
|
|
47
|
+
Nan::New<String>("PdfBackend").ToLocalChecked(),
|
|
48
|
+
Nan::GetFunction(ctor).ToLocalChecked()).Check();
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
NAN_METHOD(PdfBackend::New) {
|
|
52
|
+
init(info);
|
|
53
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
#include "Backend.h"
|
|
4
|
+
#include "../closure.h"
|
|
5
|
+
#include <v8.h>
|
|
6
|
+
|
|
7
|
+
class PdfBackend : public Backend
|
|
8
|
+
{
|
|
9
|
+
private:
|
|
10
|
+
cairo_surface_t* createSurface();
|
|
11
|
+
cairo_surface_t* recreateSurface();
|
|
12
|
+
|
|
13
|
+
public:
|
|
14
|
+
PdfSvgClosure* _closure = NULL;
|
|
15
|
+
inline PdfSvgClosure* closure() { return _closure; }
|
|
16
|
+
|
|
17
|
+
PdfBackend(int width, int height);
|
|
18
|
+
~PdfBackend();
|
|
19
|
+
static Backend *construct(int width, int height);
|
|
20
|
+
|
|
21
|
+
static Nan::Persistent<v8::FunctionTemplate> constructor;
|
|
22
|
+
static void Initialize(v8::Local<v8::Object> target);
|
|
23
|
+
static NAN_METHOD(New);
|
|
24
|
+
};
|