11
11
#include < cstring>
12
12
#include < cctype>
13
13
#include < ctime>
14
- #include < glib.h>
15
14
#include " PNG.h"
16
15
#include < sstream>
17
16
#include < stdlib.h>
18
17
#include < string>
19
18
#include < unordered_set>
20
19
#include " Util.h"
21
20
#include < vector>
22
- #include " node_buffer.h"
23
21
#include " FontParser.h"
24
-
25
- #ifdef HAVE_JPEG
26
22
#include " JPEGStream.h"
27
- #endif
28
23
29
24
#define GENERIC_FACE_ERROR \
30
25
" The second argument to registerFont is required, and should be an object " \
@@ -49,9 +44,7 @@ Canvas::Initialize(Napi::Env& env, Napi::Object& exports) {
49
44
InstanceMethod<&Canvas::ToBuffer>(" toBuffer" , napi_default_method),
50
45
InstanceMethod<&Canvas::StreamPNGSync>(" streamPNGSync" , napi_default_method),
51
46
InstanceMethod<&Canvas::StreamPDFSync>(" streamPDFSync" , napi_default_method),
52
- #ifdef HAVE_JPEG
53
47
InstanceMethod<&Canvas::StreamJPEGSync>(" streamJPEGSync" , napi_default_method),
54
- #endif
55
48
InstanceAccessor<&Canvas::GetType>(" type" , napi_default_jsproperty),
56
49
InstanceAccessor<&Canvas::GetStride>(" stride" , napi_default_jsproperty),
57
50
InstanceAccessor<&Canvas::GetWidth, &Canvas::SetWidth>(" width" , napi_default_jsproperty),
@@ -84,8 +77,8 @@ Canvas::Canvas(const Napi::CallbackInfo& info) : Napi::ObjectWrap<Canvas>(info),
84
77
this ->width = 0 ;
85
78
this ->height = 0 ;
86
79
87
- int32_t width;
88
- int32_t height;
80
+ uint32_t width;
81
+ uint32_t height;
89
82
90
83
if (info[0 ].IsNumber ()) {
91
84
width = info[0 ].As <Napi::Number>().Int32Value ();
@@ -224,13 +217,11 @@ Canvas::ToPngBufferAsync(Closure* base) {
224
217
closure);
225
218
}
226
219
227
- #ifdef HAVE_JPEG
228
220
void
229
221
Canvas::ToJpegBufferAsync (Closure* base) {
230
222
JpegClosure* closure = static_cast <JpegClosure*>(base);
231
223
write_to_jpeg_buffer (closure->canvas ->ensureSurface (), closure);
232
224
}
233
- #endif
234
225
235
226
static void
236
227
parsePNGArgs (Napi::Value arg, PngClosure& pngargs) {
@@ -275,7 +266,6 @@ parsePNGArgs(Napi::Value arg, PngClosure& pngargs) {
275
266
}
276
267
}
277
268
278
- #ifdef HAVE_JPEG
279
269
static void parseJPEGArgs (Napi::Value arg, JpegClosure& jpegargs) {
280
270
// "If Type(quality) is not Number, or if quality is outside that range, the
281
271
// user agent must use its default quality value, as if the quality argument
@@ -307,9 +297,6 @@ static void parseJPEGArgs(Napi::Value arg, JpegClosure& jpegargs) {
307
297
}
308
298
}
309
299
}
310
- #endif
311
-
312
- #if CAIRO_VERSION >= CAIRO_VERSION_ENCODE(1, 16, 0)
313
300
314
301
static inline void setPdfMetaStr (cairo_surface_t * surf, Napi::Object opts,
315
302
cairo_pdf_metadata_t t, const char * propName) {
@@ -343,8 +330,6 @@ static void setPdfMetadata(Canvas* canvas, Napi::Object opts) {
343
330
setPdfMetaDate (surf, opts, CAIRO_PDF_METADATA_MOD_DATE, " modDate" );
344
331
}
345
332
346
- #endif // CAIRO 16+
347
-
348
333
/*
349
334
* Converts/encodes data to a Buffer. Async when a callback function is passed.
350
335
@@ -380,11 +365,9 @@ Canvas::ToBuffer(const Napi::CallbackInfo& info) {
380
365
// mime type may be present, but it's not checked
381
366
PdfSvgClosure* closure = static_cast <PdfSvgClosure*>(_closure);
382
367
if (isPDF ()) {
383
- #if CAIRO_VERSION >= CAIRO_VERSION_ENCODE(1, 16, 0)
384
368
if (info[1 ].IsObject ()) { // toBuffer("application/pdf", config)
385
369
setPdfMetadata (this , info[1 ].As <Napi::Object>());
386
370
}
387
- #endif // CAIRO 16+
388
371
}
389
372
390
373
cairo_surface_t *surf = ensureSurface ();
@@ -403,10 +386,6 @@ Canvas::ToBuffer(const Napi::CallbackInfo& info) {
403
386
if (info[0 ].StrictEquals (Napi::String::New (env, " raw" ))) {
404
387
cairo_surface_t *surface = ensureSurface ();
405
388
cairo_surface_flush (surface);
406
- if (nBytes () > node::Buffer::kMaxLength ) {
407
- Napi::Error::New (env, " Data exceeds maximum buffer length." ).ThrowAsJavaScriptException ();
408
- return env.Undefined ();
409
- }
410
389
return Napi::Buffer<uint8_t >::Copy (env, cairo_image_surface_get_data (surface), nBytes ());
411
390
}
412
391
@@ -467,7 +446,6 @@ Canvas::ToBuffer(const Napi::CallbackInfo& info) {
467
446
return env.Undefined ();
468
447
}
469
448
470
- #ifdef HAVE_JPEG
471
449
// Sync JPEG
472
450
Napi::Value jpegStr = Napi::String::New (env, " image/jpeg" );
473
451
if (info[0 ].StrictEquals (jpegStr)) {
@@ -503,7 +481,6 @@ Canvas::ToBuffer(const Napi::CallbackInfo& info) {
503
481
worker->Queue ();
504
482
return env.Undefined ();
505
483
}
506
- #endif
507
484
508
485
return env.Undefined ();
509
486
}
@@ -608,11 +585,9 @@ Canvas::StreamPDFSync(const Napi::CallbackInfo& info) {
608
585
return ;
609
586
}
610
587
611
- #if CAIRO_VERSION >= CAIRO_VERSION_ENCODE(1, 16, 0)
612
588
if (info[1 ].IsObject ()) {
613
589
setPdfMetadata (this , info[1 ].As <Napi::Object>());
614
590
}
615
- #endif
616
591
617
592
cairo_surface_finish (ensureSurface ());
618
593
@@ -638,7 +613,6 @@ Canvas::StreamPDFSync(const Napi::CallbackInfo& info) {
638
613
* Stream JPEG data synchronously.
639
614
*/
640
615
641
- #ifdef HAVE_JPEG
642
616
static uint32_t getSafeBufSize (Canvas* canvas) {
643
617
// Don't allow the buffer size to exceed the size of the canvas (#674)
644
618
// TODO not sure if this is really correct, but it fixed #674
@@ -659,7 +633,6 @@ Canvas::StreamJPEGSync(const Napi::CallbackInfo& info) {
659
633
uint32_t bufsize = getSafeBufSize (this );
660
634
write_to_jpeg_stream (ensureSurface (), bufsize, &closure);
661
635
}
662
- #endif
663
636
664
637
char *
665
638
str_value (Napi::Maybe<Napi::Value> maybe, const char *fallback, bool can_be_number) {
@@ -714,16 +687,14 @@ Canvas::ParseFont(const Napi::CallbackInfo& info) {
714
687
// This returns an approximate value only, suitable for
715
688
// Napi::MemoryManagement:: AdjustExternalMemory.
716
689
// The formats that don't map to intrinsic types (RGB30, A1) round up.
717
- int32_t
690
+ uint32_t
718
691
Canvas::approxBytesPerPixel () {
719
692
switch (format) {
720
693
case CAIRO_FORMAT_ARGB32:
721
694
case CAIRO_FORMAT_RGB24:
722
695
return 4 ;
723
- #ifdef CAIRO_FORMAT_RGB30
724
696
case CAIRO_FORMAT_RGB30:
725
697
return 3 ;
726
- #endif
727
698
case CAIRO_FORMAT_RGB16_565:
728
699
return 2 ;
729
700
case CAIRO_FORMAT_A8:
0 commit comments