1111#include < cstring>
1212#include < cctype>
1313#include < ctime>
14- #include < glib.h>
1514#include " PNG.h"
1615#include < sstream>
1716#include < stdlib.h>
1817#include < string>
1918#include < unordered_set>
2019#include " Util.h"
2120#include < vector>
22- #include " node_buffer.h"
2321#include " FontParser.h"
24-
25- #ifdef HAVE_JPEG
2622#include " JPEGStream.h"
27- #endif
2823
2924using namespace std ;
3025
@@ -44,9 +39,7 @@ Canvas::Initialize(Napi::Env& env, Napi::Object& exports) {
4439 InstanceMethod<&Canvas::ToBuffer>(" toBuffer" , napi_default_method),
4540 InstanceMethod<&Canvas::StreamPNGSync>(" streamPNGSync" , napi_default_method),
4641 InstanceMethod<&Canvas::StreamPDFSync>(" streamPDFSync" , napi_default_method),
47- #ifdef HAVE_JPEG
4842 InstanceMethod<&Canvas::StreamJPEGSync>(" streamJPEGSync" , napi_default_method),
49- #endif
5043 InstanceAccessor<&Canvas::GetType>(" type" , napi_default_jsproperty),
5144 InstanceAccessor<&Canvas::GetStride>(" stride" , napi_default_jsproperty),
5245 InstanceAccessor<&Canvas::GetWidth, &Canvas::SetWidth>(" width" , napi_default_jsproperty),
@@ -79,8 +72,8 @@ Canvas::Canvas(const Napi::CallbackInfo& info) : Napi::ObjectWrap<Canvas>(info),
7972 this ->width = 0 ;
8073 this ->height = 0 ;
8174
82- int32_t width;
83- int32_t height;
75+ uint32_t width;
76+ uint32_t height;
8477
8578 if (info[0 ].IsNumber ()) {
8679 width = info[0 ].As <Napi::Number>().Int32Value ();
@@ -219,13 +212,11 @@ Canvas::ToPngBufferAsync(Closure* base) {
219212 closure);
220213}
221214
222- #ifdef HAVE_JPEG
223215void
224216Canvas::ToJpegBufferAsync (Closure* base) {
225217 JpegClosure* closure = static_cast <JpegClosure*>(base);
226218 write_to_jpeg_buffer (closure->canvas ->ensureSurface (), closure);
227219}
228- #endif
229220
230221static void
231222parsePNGArgs (Napi::Value arg, PngClosure& pngargs) {
@@ -270,7 +261,6 @@ parsePNGArgs(Napi::Value arg, PngClosure& pngargs) {
270261 }
271262}
272263
273- #ifdef HAVE_JPEG
274264static void parseJPEGArgs (Napi::Value arg, JpegClosure& jpegargs) {
275265 // "If Type(quality) is not Number, or if quality is outside that range, the
276266 // user agent must use its default quality value, as if the quality argument
@@ -302,9 +292,6 @@ static void parseJPEGArgs(Napi::Value arg, JpegClosure& jpegargs) {
302292 }
303293 }
304294}
305- #endif
306-
307- #if CAIRO_VERSION >= CAIRO_VERSION_ENCODE(1, 16, 0)
308295
309296static inline void setPdfMetaStr (cairo_surface_t * surf, Napi::Object opts,
310297 cairo_pdf_metadata_t t, const char * propName) {
@@ -338,8 +325,6 @@ static void setPdfMetadata(Canvas* canvas, Napi::Object opts) {
338325 setPdfMetaDate (surf, opts, CAIRO_PDF_METADATA_MOD_DATE , " modDate" );
339326}
340327
341- #endif // CAIRO 16+
342-
343328/*
344329 * Converts/encodes data to a Buffer. Async when a callback function is passed.
345330
@@ -375,11 +360,9 @@ Canvas::ToBuffer(const Napi::CallbackInfo& info) {
375360 // mime type may be present, but it's not checked
376361 PdfSvgClosure* closure = static_cast <PdfSvgClosure*>(_closure);
377362 if (isPDF ()) {
378- #if CAIRO_VERSION >= CAIRO_VERSION_ENCODE(1, 16, 0)
379363 if (info[1 ].IsObject ()) { // toBuffer("application/pdf", config)
380364 setPdfMetadata (this , info[1 ].As <Napi::Object>());
381365 }
382- #endif // CAIRO 16+
383366 }
384367
385368 cairo_surface_t *surf = ensureSurface ();
@@ -398,10 +381,6 @@ Canvas::ToBuffer(const Napi::CallbackInfo& info) {
398381 if (info[0 ].StrictEquals (Napi::String::New (env, " raw" ))) {
399382 cairo_surface_t *surface = ensureSurface ();
400383 cairo_surface_flush (surface);
401- if (nBytes () > node::Buffer::kMaxLength ) {
402- Napi::Error::New (env, " Data exceeds maximum buffer length." ).ThrowAsJavaScriptException ();
403- return env.Undefined ();
404- }
405384 return Napi::Buffer<uint8_t >::Copy (env, cairo_image_surface_get_data (surface), nBytes ());
406385 }
407386
@@ -462,7 +441,6 @@ Canvas::ToBuffer(const Napi::CallbackInfo& info) {
462441 return env.Undefined ();
463442 }
464443
465- #ifdef HAVE_JPEG
466444 // Sync JPEG
467445 Napi::Value jpegStr = Napi::String::New (env, " image/jpeg" );
468446 if (info[0 ].StrictEquals (jpegStr)) {
@@ -498,7 +476,6 @@ Canvas::ToBuffer(const Napi::CallbackInfo& info) {
498476 worker->Queue ();
499477 return env.Undefined ();
500478 }
501- #endif
502479
503480 return env.Undefined ();
504481}
@@ -603,11 +580,9 @@ Canvas::StreamPDFSync(const Napi::CallbackInfo& info) {
603580 return ;
604581 }
605582
606- #if CAIRO_VERSION >= CAIRO_VERSION_ENCODE(1, 16, 0)
607583 if (info[1 ].IsObject ()) {
608584 setPdfMetadata (this , info[1 ].As <Napi::Object>());
609585 }
610- #endif
611586
612587 cairo_surface_finish (ensureSurface ());
613588
@@ -633,7 +608,6 @@ Canvas::StreamPDFSync(const Napi::CallbackInfo& info) {
633608 * Stream JPEG data synchronously.
634609 */
635610
636- #ifdef HAVE_JPEG
637611static uint32_t getSafeBufSize (Canvas* canvas) {
638612 // Don't allow the buffer size to exceed the size of the canvas (#674)
639613 // TODO not sure if this is really correct, but it fixed #674
@@ -654,7 +628,6 @@ Canvas::StreamJPEGSync(const Napi::CallbackInfo& info) {
654628 uint32_t bufsize = getSafeBufSize (this );
655629 write_to_jpeg_stream (ensureSurface (), bufsize, &closure);
656630}
657- #endif
658631
659632char *
660633str_value (Napi::Maybe<Napi::Value> maybe, const char *fallback, bool can_be_number) {
@@ -709,16 +682,14 @@ Canvas::ParseFont(const Napi::CallbackInfo& info) {
709682// This returns an approximate value only, suitable for
710683// Napi::MemoryManagement:: AdjustExternalMemory.
711684// The formats that don't map to intrinsic types (RGB30, A1) round up.
712- int32_t
685+ uint32_t
713686Canvas::approxBytesPerPixel () {
714687 switch (format) {
715688 case CAIRO_FORMAT_ARGB32 :
716689 case CAIRO_FORMAT_RGB24 :
717690 return 4 ;
718- #ifdef CAIRO_FORMAT_RGB30
719691 case CAIRO_FORMAT_RGB30 :
720692 return 3 ;
721- #endif
722693 case CAIRO_FORMAT_RGB16_565 :
723694 return 2 ;
724695 case CAIRO_FORMAT_A8 :
0 commit comments