Skip to content

Commit

Permalink
Fix class properties should have defaults as standard js classes woul…
Browse files Browse the repository at this point in the history
…d have

Changed PNG consts to static properties
  • Loading branch information
mcfedr committed Aug 13, 2024
1 parent 7dfeb04 commit 1e058c9
Show file tree
Hide file tree
Showing 10 changed files with 117 additions and 102 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ This release notably changes to using N-API. 🎉
* Remove unused private field `backend` in the `Backend` class. (#2229)
* Add Node.js v20 to CI. (#2237)
* Replaced `dtslint` with `tsd` (#2313)
* Changed PNG consts to static properties of Canvas class
### Added
* Added string tags to support class detection
### Fixed
Expand All @@ -41,6 +42,7 @@ This release notably changes to using N-API. 🎉
* RGB functions should support real numbers now instead of just integers. (#2339)
* Allow alternate or properly escaped quotes *within* font-family names
* Fix TextMetrics type to include alphabeticBaseline, emHeightAscent, and emHeightDescent properties
* Fix class properties should have defaults as standard js classes (#2390)

2.11.2
==================
Expand Down
14 changes: 7 additions & 7 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,19 +63,19 @@ export class Canvas {
readonly stride: number;

/** Constant used in PNG encoding methods. */
readonly PNG_NO_FILTERS: number
static readonly PNG_NO_FILTERS: number
/** Constant used in PNG encoding methods. */
readonly PNG_ALL_FILTERS: number
static readonly PNG_ALL_FILTERS: number
/** Constant used in PNG encoding methods. */
readonly PNG_FILTER_NONE: number
static readonly PNG_FILTER_NONE: number
/** Constant used in PNG encoding methods. */
readonly PNG_FILTER_SUB: number
static readonly PNG_FILTER_SUB: number
/** Constant used in PNG encoding methods. */
readonly PNG_FILTER_UP: number
static readonly PNG_FILTER_UP: number
/** Constant used in PNG encoding methods. */
readonly PNG_FILTER_AVG: number
static readonly PNG_FILTER_AVG: number
/** Constant used in PNG encoding methods. */
readonly PNG_FILTER_PAETH: number
static readonly PNG_FILTER_PAETH: number

constructor(width: number, height: number, type?: 'image'|'pdf'|'svg')

Expand Down
2 changes: 1 addition & 1 deletion index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ expectType<number>(dm.a)

expectType<Buffer>(canv.toBuffer())
expectType<Buffer>(canv.toBuffer('application/pdf'))
canv.toBuffer((err, data) => {}, 'image/png')
canv.toBuffer((err, data) => {}, 'image/png', {filters: Canvas.Canvas.PNG_ALL_FILTERS})
expectAssignable<Readable>(canv.createJPEGStream({ quality: 0.5 }))
expectAssignable<Readable>(canv.createPDFStream({ author: 'octocat' }))
canv.toDataURL()
Expand Down
46 changes: 27 additions & 19 deletions src/Canvas.cc
Original file line number Diff line number Diff line change
Expand Up @@ -50,25 +50,25 @@ Canvas::Initialize(Napi::Env& env, Napi::Object& exports) {

// Constructor
Napi::Function ctor = DefineClass(env, "Canvas", {
InstanceMethod<&Canvas::ToBuffer>("toBuffer"),
InstanceMethod<&Canvas::StreamPNGSync>("streamPNGSync"),
InstanceMethod<&Canvas::StreamPDFSync>("streamPDFSync"),
InstanceMethod<&Canvas::ToBuffer>("toBuffer", napi_default_method),
InstanceMethod<&Canvas::StreamPNGSync>("streamPNGSync", napi_default_method),
InstanceMethod<&Canvas::StreamPDFSync>("streamPDFSync", napi_default_method),
#ifdef HAVE_JPEG
InstanceMethod<&Canvas::StreamJPEGSync>("streamJPEGSync"),
InstanceMethod<&Canvas::StreamJPEGSync>("streamJPEGSync", napi_default_method),
#endif
InstanceAccessor<&Canvas::GetType>("type"),
InstanceAccessor<&Canvas::GetStride>("stride"),
InstanceAccessor<&Canvas::GetWidth, &Canvas::SetWidth>("width"),
InstanceAccessor<&Canvas::GetHeight, &Canvas::SetHeight>("height"),
InstanceValue("PNG_NO_FILTERS", Napi::Number::New(env, PNG_NO_FILTERS)),
InstanceValue("PNG_FILTER_NONE", Napi::Number::New(env, PNG_FILTER_NONE)),
InstanceValue("PNG_FILTER_SUB", Napi::Number::New(env, PNG_FILTER_SUB)),
InstanceValue("PNG_FILTER_UP", Napi::Number::New(env, PNG_FILTER_UP)),
InstanceValue("PNG_FILTER_AVG", Napi::Number::New(env, PNG_FILTER_AVG)),
InstanceValue("PNG_FILTER_PAETH", Napi::Number::New(env, PNG_FILTER_PAETH)),
InstanceValue("PNG_ALL_FILTERS", Napi::Number::New(env, PNG_ALL_FILTERS)),
StaticMethod<&Canvas::RegisterFont>("_registerFont"),
StaticMethod<&Canvas::DeregisterAllFonts>("_deregisterAllFonts")
InstanceAccessor<&Canvas::GetType>("type", napi_default_jsproperty),
InstanceAccessor<&Canvas::GetStride>("stride", napi_default_jsproperty),
InstanceAccessor<&Canvas::GetWidth, &Canvas::SetWidth>("width", napi_default_jsproperty),
InstanceAccessor<&Canvas::GetHeight, &Canvas::SetHeight>("height", napi_default_jsproperty),
StaticValue("PNG_NO_FILTERS", Napi::Number::New(env, PNG_NO_FILTERS), napi_default_jsproperty),
StaticValue("PNG_FILTER_NONE", Napi::Number::New(env, PNG_FILTER_NONE), napi_default_jsproperty),
StaticValue("PNG_FILTER_SUB", Napi::Number::New(env, PNG_FILTER_SUB), napi_default_jsproperty),
StaticValue("PNG_FILTER_UP", Napi::Number::New(env, PNG_FILTER_UP), napi_default_jsproperty),
StaticValue("PNG_FILTER_AVG", Napi::Number::New(env, PNG_FILTER_AVG), napi_default_jsproperty),
StaticValue("PNG_FILTER_PAETH", Napi::Number::New(env, PNG_FILTER_PAETH), napi_default_jsproperty),
StaticValue("PNG_ALL_FILTERS", Napi::Number::New(env, PNG_ALL_FILTERS), napi_default_jsproperty),
StaticMethod<&Canvas::RegisterFont>("_registerFont", napi_default_method),
StaticMethod<&Canvas::DeregisterAllFonts>("_deregisterAllFonts", napi_default_method)
});

data->CanvasCtor = Napi::Persistent(ctor);
Expand Down Expand Up @@ -382,7 +382,15 @@ Canvas::ToBuffer(const Napi::CallbackInfo& info) {
closure = static_cast<SvgBackend*>(backend())->closure();
}

cairo_surface_finish(surface());
cairo_surface_t *surf = surface();
cairo_surface_finish(surf);

cairo_status_t status = cairo_surface_status(surf);
if (status != CAIRO_STATUS_SUCCESS) {
Napi::Error::New(env, cairo_status_to_string(status)).ThrowAsJavaScriptException();
return env.Undefined();
}

return Napi::Buffer<uint8_t>::Copy(env, &closure->vec[0], closure->vec.size());
}

Expand Down Expand Up @@ -658,7 +666,7 @@ str_value(Napi::Maybe<Napi::Value> maybe, const char *fallback, bool can_be_numb
return strdup(fallback);
}
}

return NULL;
}

Expand Down
2 changes: 1 addition & 1 deletion src/CanvasGradient.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Gradient::Initialize(Napi::Env& env, Napi::Object& exports) {
InstanceData* data = env.GetInstanceData<InstanceData>();

Napi::Function ctor = DefineClass(env, "CanvasGradient", {
InstanceMethod<&Gradient::AddColorStop>("addColorStop")
InstanceMethod<&Gradient::AddColorStop>("addColorStop", napi_default_method)
});

exports.Set("CanvasGradient", ctor);
Expand Down
2 changes: 1 addition & 1 deletion src/CanvasPattern.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Pattern::Initialize(Napi::Env& env, Napi::Object& exports) {

// Constructor
Napi::Function ctor = DefineClass(env, "CanvasPattern", {
InstanceMethod<&Pattern::setTransform>("setTransform")
InstanceMethod<&Pattern::setTransform>("setTransform", napi_default_method)
});

// Prototype
Expand Down
126 changes: 63 additions & 63 deletions src/CanvasRenderingContext2d.cc
Original file line number Diff line number Diff line change
Expand Up @@ -94,69 +94,69 @@ Context2d::Initialize(Napi::Env& env, Napi::Object& exports) {
InstanceData* data = env.GetInstanceData<InstanceData>();

Napi::Function ctor = DefineClass(env, "CanvasRenderingContext2D", {
InstanceMethod<&Context2d::DrawImage>("drawImage"),
InstanceMethod<&Context2d::PutImageData>("putImageData"),
InstanceMethod<&Context2d::GetImageData>("getImageData"),
InstanceMethod<&Context2d::CreateImageData>("createImageData"),
InstanceMethod<&Context2d::AddPage>("addPage"),
InstanceMethod<&Context2d::Save>("save"),
InstanceMethod<&Context2d::Restore>("restore"),
InstanceMethod<&Context2d::Rotate>("rotate"),
InstanceMethod<&Context2d::Translate>("translate"),
InstanceMethod<&Context2d::Transform>("transform"),
InstanceMethod<&Context2d::GetTransform>("getTransform"),
InstanceMethod<&Context2d::ResetTransform>("resetTransform"),
InstanceMethod<&Context2d::SetTransform>("setTransform"),
InstanceMethod<&Context2d::IsPointInPath>("isPointInPath"),
InstanceMethod<&Context2d::Scale>("scale"),
InstanceMethod<&Context2d::Clip>("clip"),
InstanceMethod<&Context2d::Fill>("fill"),
InstanceMethod<&Context2d::Stroke>("stroke"),
InstanceMethod<&Context2d::FillText>("fillText"),
InstanceMethod<&Context2d::StrokeText>("strokeText"),
InstanceMethod<&Context2d::FillRect>("fillRect"),
InstanceMethod<&Context2d::StrokeRect>("strokeRect"),
InstanceMethod<&Context2d::ClearRect>("clearRect"),
InstanceMethod<&Context2d::Rect>("rect"),
InstanceMethod<&Context2d::RoundRect>("roundRect"),
InstanceMethod<&Context2d::MeasureText>("measureText"),
InstanceMethod<&Context2d::MoveTo>("moveTo"),
InstanceMethod<&Context2d::LineTo>("lineTo"),
InstanceMethod<&Context2d::BezierCurveTo>("bezierCurveTo"),
InstanceMethod<&Context2d::QuadraticCurveTo>("quadraticCurveTo"),
InstanceMethod<&Context2d::BeginPath>("beginPath"),
InstanceMethod<&Context2d::ClosePath>("closePath"),
InstanceMethod<&Context2d::Arc>("arc"),
InstanceMethod<&Context2d::ArcTo>("arcTo"),
InstanceMethod<&Context2d::Ellipse>("ellipse"),
InstanceMethod<&Context2d::SetLineDash>("setLineDash"),
InstanceMethod<&Context2d::GetLineDash>("getLineDash"),
InstanceMethod<&Context2d::CreatePattern>("createPattern"),
InstanceMethod<&Context2d::CreateLinearGradient>("createLinearGradient"),
InstanceMethod<&Context2d::CreateRadialGradient>("createRadialGradient"),
InstanceAccessor<&Context2d::GetFormat>("pixelFormat"),
InstanceAccessor<&Context2d::GetPatternQuality, &Context2d::SetPatternQuality>("patternQuality"),
InstanceAccessor<&Context2d::GetImageSmoothingEnabled, &Context2d::SetImageSmoothingEnabled>("imageSmoothingEnabled"),
InstanceAccessor<&Context2d::GetGlobalCompositeOperation, &Context2d::SetGlobalCompositeOperation>("globalCompositeOperation"),
InstanceAccessor<&Context2d::GetGlobalAlpha, &Context2d::SetGlobalAlpha>("globalAlpha"),
InstanceAccessor<&Context2d::GetShadowColor, &Context2d::SetShadowColor>("shadowColor"),
InstanceAccessor<&Context2d::GetMiterLimit, &Context2d::SetMiterLimit>("miterLimit"),
InstanceAccessor<&Context2d::GetLineWidth, &Context2d::SetLineWidth>("lineWidth"),
InstanceAccessor<&Context2d::GetLineCap, &Context2d::SetLineCap>("lineCap"),
InstanceAccessor<&Context2d::GetLineJoin, &Context2d::SetLineJoin>("lineJoin"),
InstanceAccessor<&Context2d::GetLineDashOffset, &Context2d::SetLineDashOffset>("lineDashOffset"),
InstanceAccessor<&Context2d::GetShadowOffsetX, &Context2d::SetShadowOffsetX>("shadowOffsetX"),
InstanceAccessor<&Context2d::GetShadowOffsetY, &Context2d::SetShadowOffsetY>("shadowOffsetY"),
InstanceAccessor<&Context2d::GetShadowBlur, &Context2d::SetShadowBlur>("shadowBlur"),
InstanceAccessor<&Context2d::GetAntiAlias, &Context2d::SetAntiAlias>("antialias"),
InstanceAccessor<&Context2d::GetTextDrawingMode, &Context2d::SetTextDrawingMode>("textDrawingMode"),
InstanceAccessor<&Context2d::GetQuality, &Context2d::SetQuality>("quality"),
InstanceAccessor<&Context2d::GetCurrentTransform, &Context2d::SetCurrentTransform>("currentTransform"),
InstanceAccessor<&Context2d::GetFillStyle, &Context2d::SetFillStyle>("fillStyle"),
InstanceAccessor<&Context2d::GetStrokeStyle, &Context2d::SetStrokeStyle>("strokeStyle"),
InstanceAccessor<&Context2d::GetFont, &Context2d::SetFont>("font"),
InstanceAccessor<&Context2d::GetTextBaseline, &Context2d::SetTextBaseline>("textBaseline"),
InstanceAccessor<&Context2d::GetTextAlign, &Context2d::SetTextAlign>("textAlign")
InstanceMethod<&Context2d::DrawImage>("drawImage", napi_default_method),
InstanceMethod<&Context2d::PutImageData>("putImageData", napi_default_method),
InstanceMethod<&Context2d::GetImageData>("getImageData", napi_default_method),
InstanceMethod<&Context2d::CreateImageData>("createImageData", napi_default_method),
InstanceMethod<&Context2d::AddPage>("addPage", napi_default_method),
InstanceMethod<&Context2d::Save>("save", napi_default_method),
InstanceMethod<&Context2d::Restore>("restore", napi_default_method),
InstanceMethod<&Context2d::Rotate>("rotate", napi_default_method),
InstanceMethod<&Context2d::Translate>("translate", napi_default_method),
InstanceMethod<&Context2d::Transform>("transform", napi_default_method),
InstanceMethod<&Context2d::GetTransform>("getTransform", napi_default_method),
InstanceMethod<&Context2d::ResetTransform>("resetTransform", napi_default_method),
InstanceMethod<&Context2d::SetTransform>("setTransform", napi_default_method),
InstanceMethod<&Context2d::IsPointInPath>("isPointInPath", napi_default_method),
InstanceMethod<&Context2d::Scale>("scale", napi_default_method),
InstanceMethod<&Context2d::Clip>("clip", napi_default_method),
InstanceMethod<&Context2d::Fill>("fill", napi_default_method),
InstanceMethod<&Context2d::Stroke>("stroke", napi_default_method),
InstanceMethod<&Context2d::FillText>("fillText", napi_default_method),
InstanceMethod<&Context2d::StrokeText>("strokeText", napi_default_method),
InstanceMethod<&Context2d::FillRect>("fillRect", napi_default_method),
InstanceMethod<&Context2d::StrokeRect>("strokeRect", napi_default_method),
InstanceMethod<&Context2d::ClearRect>("clearRect", napi_default_method),
InstanceMethod<&Context2d::Rect>("rect", napi_default_method),
InstanceMethod<&Context2d::RoundRect>("roundRect", napi_default_method),
InstanceMethod<&Context2d::MeasureText>("measureText", napi_default_method),
InstanceMethod<&Context2d::MoveTo>("moveTo", napi_default_method),
InstanceMethod<&Context2d::LineTo>("lineTo", napi_default_method),
InstanceMethod<&Context2d::BezierCurveTo>("bezierCurveTo", napi_default_method),
InstanceMethod<&Context2d::QuadraticCurveTo>("quadraticCurveTo", napi_default_method),
InstanceMethod<&Context2d::BeginPath>("beginPath", napi_default_method),
InstanceMethod<&Context2d::ClosePath>("closePath", napi_default_method),
InstanceMethod<&Context2d::Arc>("arc", napi_default_method),
InstanceMethod<&Context2d::ArcTo>("arcTo", napi_default_method),
InstanceMethod<&Context2d::Ellipse>("ellipse", napi_default_method),
InstanceMethod<&Context2d::SetLineDash>("setLineDash", napi_default_method),
InstanceMethod<&Context2d::GetLineDash>("getLineDash", napi_default_method),
InstanceMethod<&Context2d::CreatePattern>("createPattern", napi_default_method),
InstanceMethod<&Context2d::CreateLinearGradient>("createLinearGradient", napi_default_method),
InstanceMethod<&Context2d::CreateRadialGradient>("createRadialGradient", napi_default_method),
InstanceAccessor<&Context2d::GetFormat>("pixelFormat", napi_default_jsproperty),
InstanceAccessor<&Context2d::GetPatternQuality, &Context2d::SetPatternQuality>("patternQuality", napi_default_jsproperty),
InstanceAccessor<&Context2d::GetImageSmoothingEnabled, &Context2d::SetImageSmoothingEnabled>("imageSmoothingEnabled", napi_default_jsproperty),
InstanceAccessor<&Context2d::GetGlobalCompositeOperation, &Context2d::SetGlobalCompositeOperation>("globalCompositeOperation", napi_default_jsproperty),
InstanceAccessor<&Context2d::GetGlobalAlpha, &Context2d::SetGlobalAlpha>("globalAlpha", napi_default_jsproperty),
InstanceAccessor<&Context2d::GetShadowColor, &Context2d::SetShadowColor>("shadowColor", napi_default_jsproperty),
InstanceAccessor<&Context2d::GetMiterLimit, &Context2d::SetMiterLimit>("miterLimit", napi_default_jsproperty),
InstanceAccessor<&Context2d::GetLineWidth, &Context2d::SetLineWidth>("lineWidth", napi_default_jsproperty),
InstanceAccessor<&Context2d::GetLineCap, &Context2d::SetLineCap>("lineCap", napi_default_jsproperty),
InstanceAccessor<&Context2d::GetLineJoin, &Context2d::SetLineJoin>("lineJoin", napi_default_jsproperty),
InstanceAccessor<&Context2d::GetLineDashOffset, &Context2d::SetLineDashOffset>("lineDashOffset", napi_default_jsproperty),
InstanceAccessor<&Context2d::GetShadowOffsetX, &Context2d::SetShadowOffsetX>("shadowOffsetX", napi_default_jsproperty),
InstanceAccessor<&Context2d::GetShadowOffsetY, &Context2d::SetShadowOffsetY>("shadowOffsetY", napi_default_jsproperty),
InstanceAccessor<&Context2d::GetShadowBlur, &Context2d::SetShadowBlur>("shadowBlur", napi_default_jsproperty),
InstanceAccessor<&Context2d::GetAntiAlias, &Context2d::SetAntiAlias>("antialias", napi_default_jsproperty),
InstanceAccessor<&Context2d::GetTextDrawingMode, &Context2d::SetTextDrawingMode>("textDrawingMode", napi_default_jsproperty),
InstanceAccessor<&Context2d::GetQuality, &Context2d::SetQuality>("quality", napi_default_jsproperty),
InstanceAccessor<&Context2d::GetCurrentTransform, &Context2d::SetCurrentTransform>("currentTransform", napi_default_jsproperty),
InstanceAccessor<&Context2d::GetFillStyle, &Context2d::SetFillStyle>("fillStyle", napi_default_jsproperty),
InstanceAccessor<&Context2d::GetStrokeStyle, &Context2d::SetStrokeStyle>("strokeStyle", napi_default_jsproperty),
InstanceAccessor<&Context2d::GetFont, &Context2d::SetFont>("font", napi_default_jsproperty),
InstanceAccessor<&Context2d::GetTextBaseline, &Context2d::SetTextBaseline>("textBaseline", napi_default_jsproperty),
InstanceAccessor<&Context2d::GetTextAlign, &Context2d::SetTextAlign>("textAlign", napi_default_jsproperty)
});

exports.Set("CanvasRenderingContext2d", ctor);
Expand Down
16 changes: 8 additions & 8 deletions src/Image.cc
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,14 @@ Image::Initialize(Napi::Env& env, Napi::Object& exports) {
Napi::HandleScope scope(env);

Napi::Function ctor = DefineClass(env, "Image", {
InstanceAccessor<&Image::GetComplete>("complete"),
InstanceAccessor<&Image::GetWidth, &Image::SetWidth>("width"),
InstanceAccessor<&Image::GetHeight, &Image::SetHeight>("height"),
InstanceAccessor<&Image::GetNaturalWidth>("naturalWidth"),
InstanceAccessor<&Image::GetNaturalHeight>("naturalHeight"),
InstanceAccessor<&Image::GetDataMode, &Image::SetDataMode>("dataMode"),
StaticValue("MODE_IMAGE", Napi::Number::New(env, DATA_IMAGE)),
StaticValue("MODE_MIME", Napi::Number::New(env, DATA_MIME))
InstanceAccessor<&Image::GetComplete>("complete", napi_default_jsproperty),
InstanceAccessor<&Image::GetWidth, &Image::SetWidth>("width", napi_default_jsproperty),
InstanceAccessor<&Image::GetHeight, &Image::SetHeight>("height", napi_default_jsproperty),
InstanceAccessor<&Image::GetNaturalWidth>("naturalWidth", napi_default_jsproperty),
InstanceAccessor<&Image::GetNaturalHeight>("naturalHeight", napi_default_jsproperty),
InstanceAccessor<&Image::GetDataMode, &Image::SetDataMode>("dataMode", napi_default_jsproperty),
StaticValue("MODE_IMAGE", Napi::Number::New(env, DATA_IMAGE), napi_default_jsproperty),
StaticValue("MODE_MIME", Napi::Number::New(env, DATA_MIME), napi_default_jsproperty)
});

// Used internally in lib/image.js
Expand Down
4 changes: 2 additions & 2 deletions src/ImageData.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ ImageData::Initialize(Napi::Env& env, Napi::Object& exports) {
InstanceData *data = env.GetInstanceData<InstanceData>();

Napi::Function ctor = DefineClass(env, "ImageData", {
InstanceAccessor<&ImageData::GetWidth>("width"),
InstanceAccessor<&ImageData::GetHeight>("height")
InstanceAccessor<&ImageData::GetWidth>("width", napi_default_jsproperty),
InstanceAccessor<&ImageData::GetHeight>("height", napi_default_jsproperty)
});

exports.Set("ImageData", ctor);
Expand Down
5 changes: 5 additions & 0 deletions test/canvas.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -694,6 +694,11 @@ describe('Canvas', function () {
assert.equal('PNG', buf.slice(1, 4).toString())
})

it('Canvas#toBuffer("image/png", {filters: PNG_ALL_FILTERS})', function () {
const buf = createCanvas(200, 200).toBuffer('image/png', { filters: Canvas.PNG_ALL_FILTERS })
assert.equal('PNG', buf.slice(1, 4).toString())
})

it('Canvas#toBuffer("image/jpeg")', function () {
const buf = createCanvas(200, 200).toBuffer('image/jpeg')
assert.equal(buf[0], 0xff)
Expand Down

0 comments on commit 1e058c9

Please sign in to comment.