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
2924#define GENERIC_FACE_ERROR \
3025 " The second argument to registerFont is required, and should be an object " \
@@ -49,9 +44,7 @@ Canvas::Initialize(Napi::Env& env, Napi::Object& exports) {
4944 InstanceMethod<&Canvas::ToBuffer>(" toBuffer" , napi_default_method),
5045 InstanceMethod<&Canvas::StreamPNGSync>(" streamPNGSync" , napi_default_method),
5146 InstanceMethod<&Canvas::StreamPDFSync>(" streamPDFSync" , napi_default_method),
52- #ifdef HAVE_JPEG
5347 InstanceMethod<&Canvas::StreamJPEGSync>(" streamJPEGSync" , napi_default_method),
54- #endif
5548 InstanceAccessor<&Canvas::GetType>(" type" , napi_default_jsproperty),
5649 InstanceAccessor<&Canvas::GetStride>(" stride" , napi_default_jsproperty),
5750 InstanceAccessor<&Canvas::GetWidth, &Canvas::SetWidth>(" width" , napi_default_jsproperty),
@@ -84,8 +77,8 @@ Canvas::Canvas(const Napi::CallbackInfo& info) : Napi::ObjectWrap<Canvas>(info),
8477 this ->width = 0 ;
8578 this ->height = 0 ;
8679
87- int32_t width;
88- int32_t height;
80+ uint32_t width;
81+ uint32_t height;
8982
9083 if (info[0 ].IsNumber ()) {
9184 width = info[0 ].As <Napi::Number>().Int32Value ();
@@ -224,13 +217,11 @@ Canvas::ToPngBufferAsync(Closure* base) {
224217 closure);
225218}
226219
227- #ifdef HAVE_JPEG
228220void
229221Canvas::ToJpegBufferAsync (Closure* base) {
230222 JpegClosure* closure = static_cast <JpegClosure*>(base);
231223 write_to_jpeg_buffer (closure->canvas ->ensureSurface (), closure);
232224}
233- #endif
234225
235226static void
236227parsePNGArgs (Napi::Value arg, PngClosure& pngargs) {
@@ -275,7 +266,6 @@ parsePNGArgs(Napi::Value arg, PngClosure& pngargs) {
275266 }
276267}
277268
278- #ifdef HAVE_JPEG
279269static void parseJPEGArgs (Napi::Value arg, JpegClosure& jpegargs) {
280270 // "If Type(quality) is not Number, or if quality is outside that range, the
281271 // 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) {
307297 }
308298 }
309299}
310- #endif
311-
312- #if CAIRO_VERSION >= CAIRO_VERSION_ENCODE(1, 16, 0)
313300
314301static inline void setPdfMetaStr (cairo_surface_t * surf, Napi::Object opts,
315302 cairo_pdf_metadata_t t, const char * propName) {
@@ -343,8 +330,6 @@ static void setPdfMetadata(Canvas* canvas, Napi::Object opts) {
343330 setPdfMetaDate (surf, opts, CAIRO_PDF_METADATA_MOD_DATE, " modDate" );
344331}
345332
346- #endif // CAIRO 16+
347-
348333/*
349334 * Converts/encodes data to a Buffer. Async when a callback function is passed.
350335
@@ -380,11 +365,9 @@ Canvas::ToBuffer(const Napi::CallbackInfo& info) {
380365 // mime type may be present, but it's not checked
381366 PdfSvgClosure* closure = static_cast <PdfSvgClosure*>(_closure);
382367 if (isPDF ()) {
383- #if CAIRO_VERSION >= CAIRO_VERSION_ENCODE(1, 16, 0)
384368 if (info[1 ].IsObject ()) { // toBuffer("application/pdf", config)
385369 setPdfMetadata (this , info[1 ].As <Napi::Object>());
386370 }
387- #endif // CAIRO 16+
388371 }
389372
390373 cairo_surface_t *surf = ensureSurface ();
@@ -403,10 +386,6 @@ Canvas::ToBuffer(const Napi::CallbackInfo& info) {
403386 if (info[0 ].StrictEquals (Napi::String::New (env, " raw" ))) {
404387 cairo_surface_t *surface = ensureSurface ();
405388 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- }
410389 return Napi::Buffer<uint8_t >::Copy (env, cairo_image_surface_get_data (surface), nBytes ());
411390 }
412391
@@ -467,7 +446,6 @@ Canvas::ToBuffer(const Napi::CallbackInfo& info) {
467446 return env.Undefined ();
468447 }
469448
470- #ifdef HAVE_JPEG
471449 // Sync JPEG
472450 Napi::Value jpegStr = Napi::String::New (env, " image/jpeg" );
473451 if (info[0 ].StrictEquals (jpegStr)) {
@@ -503,7 +481,6 @@ Canvas::ToBuffer(const Napi::CallbackInfo& info) {
503481 worker->Queue ();
504482 return env.Undefined ();
505483 }
506- #endif
507484
508485 return env.Undefined ();
509486}
@@ -608,11 +585,9 @@ Canvas::StreamPDFSync(const Napi::CallbackInfo& info) {
608585 return ;
609586 }
610587
611- #if CAIRO_VERSION >= CAIRO_VERSION_ENCODE(1, 16, 0)
612588 if (info[1 ].IsObject ()) {
613589 setPdfMetadata (this , info[1 ].As <Napi::Object>());
614590 }
615- #endif
616591
617592 cairo_surface_finish (ensureSurface ());
618593
@@ -638,7 +613,6 @@ Canvas::StreamPDFSync(const Napi::CallbackInfo& info) {
638613 * Stream JPEG data synchronously.
639614 */
640615
641- #ifdef HAVE_JPEG
642616static uint32_t getSafeBufSize (Canvas* canvas) {
643617 // Don't allow the buffer size to exceed the size of the canvas (#674)
644618 // TODO not sure if this is really correct, but it fixed #674
@@ -659,7 +633,6 @@ Canvas::StreamJPEGSync(const Napi::CallbackInfo& info) {
659633 uint32_t bufsize = getSafeBufSize (this );
660634 write_to_jpeg_stream (ensureSurface (), bufsize, &closure);
661635}
662- #endif
663636
664637char *
665638str_value (Napi::Maybe<Napi::Value> maybe, const char *fallback, bool can_be_number) {
@@ -714,16 +687,14 @@ Canvas::ParseFont(const Napi::CallbackInfo& info) {
714687// This returns an approximate value only, suitable for
715688// Napi::MemoryManagement:: AdjustExternalMemory.
716689// The formats that don't map to intrinsic types (RGB30, A1) round up.
717- int32_t
690+ uint32_t
718691Canvas::approxBytesPerPixel () {
719692 switch (format) {
720693 case CAIRO_FORMAT_ARGB32:
721694 case CAIRO_FORMAT_RGB24:
722695 return 4 ;
723- #ifdef CAIRO_FORMAT_RGB30
724696 case CAIRO_FORMAT_RGB30:
725697 return 3 ;
726- #endif
727698 case CAIRO_FORMAT_RGB16_565:
728699 return 2 ;
729700 case CAIRO_FORMAT_A8:
0 commit comments