From 12e26814bbaf3eb21be5bebecc9f4d3aaef8f102 Mon Sep 17 00:00:00 2001 From: Tj Holowaychuk Date: Tue, 9 Nov 2010 09:57:40 -0800 Subject: [PATCH] Added Context2d#antialias= --- Makefile | 2 +- Readme.md | 9 +++++++ src/CanvasRenderingContext2d.cc | 44 +++++++++++++++++++++++++++++++++ src/CanvasRenderingContext2d.h | 2 ++ test/canvas.test.js | 17 +++++++++++++ test/public/tests.js | 2 +- 6 files changed, 74 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 27e3d91..a30b445 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ build/default/canvas.node: src/*.cc node-waf configure build -test: +test: build/default/canvas.node @./support/expresso/bin/expresso \ -I lib \ test/*.test.js diff --git a/Readme.md b/Readme.md index 4f533ad..466c7c7 100644 --- a/Readme.md +++ b/Readme.md @@ -103,6 +103,15 @@ or specify the mime type: - hsl-color - hsl-luminosity +## Anti-Aliasing + + Set anti-aliasing mode: + + - default + - none + - gray + - subpixel + ## Benchmarks Although node-canvas is extremely new, and we have not even begun optimization yet it is already quite fast. For benchmarks vs other node canvas implementations view this [gist](https://gist.github.com/664922), or update the submodules and run `$ make benchmark` yourself. diff --git a/src/CanvasRenderingContext2d.cc b/src/CanvasRenderingContext2d.cc index 941762a..94997fc 100644 --- a/src/CanvasRenderingContext2d.cc +++ b/src/CanvasRenderingContext2d.cc @@ -108,6 +108,7 @@ Context2d::Initialize(Handle target) { proto->SetAccessor(String::NewSymbol("shadowOffsetX"), GetShadowOffsetX, SetShadowOffsetX); proto->SetAccessor(String::NewSymbol("shadowOffsetY"), GetShadowOffsetY, SetShadowOffsetY); proto->SetAccessor(String::NewSymbol("shadowBlur"), GetShadowBlur, SetShadowBlur); + proto->SetAccessor(String::NewSymbol("antialias"), GetAntiAlias, SetAntiAlias); target->Set(String::NewSymbol("CanvasRenderingContext2d"), t->GetFunction()); } @@ -573,6 +574,49 @@ Context2d::SetShadowBlur(Local prop, Local val, const AccessorInf } } +/* + * Get current antialiasing setting. + */ + +Handle +Context2d::GetAntiAlias(Local prop, const AccessorInfo &info) { + Context2d *context = ObjectWrap::Unwrap(info.This()); + switch (cairo_get_antialias(context->getContext())) { + case CAIRO_ANTIALIAS_NONE: + return String::NewSymbol("none"); + case CAIRO_ANTIALIAS_GRAY: + return String::NewSymbol("gray"); + case CAIRO_ANTIALIAS_SUBPIXEL: + return String::NewSymbol("subpixel"); + default: + return String::NewSymbol("default"); + } +} + +/* + * Set antialiasing. + */ + +void +Context2d::SetAntiAlias(Local prop, Local val, const AccessorInfo &info) { + String::AsciiValue str(val->ToString()); + Context2d *context = ObjectWrap::Unwrap(info.This()); + cairo_t *ctx = context->getContext(); + cairo_antialias_t a; + if (0 == strcmp("none", *str)) { + a = CAIRO_ANTIALIAS_NONE; + } else if (0 == strcmp("default", *str)) { + a = CAIRO_ANTIALIAS_DEFAULT; + } else if (0 == strcmp("gray", *str)) { + a = CAIRO_ANTIALIAS_GRAY; + } else if (0 == strcmp("subpixel", *str)) { + a = CAIRO_ANTIALIAS_SUBPIXEL; + } else { + a = cairo_get_antialias(ctx); + } + cairo_set_antialias(ctx, a); +} + /* * Get miter limit. */ diff --git a/src/CanvasRenderingContext2d.h b/src/CanvasRenderingContext2d.h index 8fc8010..adb543a 100644 --- a/src/CanvasRenderingContext2d.h +++ b/src/CanvasRenderingContext2d.h @@ -89,6 +89,7 @@ class Context2d: public node::ObjectWrap { static Handle GetShadowOffsetX(Local prop, const AccessorInfo &info); static Handle GetShadowOffsetY(Local prop, const AccessorInfo &info); static Handle GetShadowBlur(Local prop, const AccessorInfo &info); + static Handle GetAntiAlias(Local prop, const AccessorInfo &info); static void SetGlobalCompositeOperation(Local prop, Local val, const AccessorInfo &info); static void SetGlobalAlpha(Local prop, Local val, const AccessorInfo &info); static void SetMiterLimit(Local prop, Local val, const AccessorInfo &info); @@ -98,6 +99,7 @@ class Context2d: public node::ObjectWrap { static void SetShadowOffsetX(Local prop, Local val, const AccessorInfo &info); static void SetShadowOffsetY(Local prop, Local val, const AccessorInfo &info); static void SetShadowBlur(Local prop, Local val, const AccessorInfo &info); + static void SetAntiAlias(Local prop, Local val, const AccessorInfo &info); inline cairo_t *getContext(){ return _context; } inline Canvas *getCanvas(){ return _canvas; } inline bool hasShadow(); diff --git a/test/canvas.test.js b/test/canvas.test.js index 84c5b8e..330dcc6 100644 --- a/test/canvas.test.js +++ b/test/canvas.test.js @@ -174,6 +174,23 @@ module.exports = { assert.equal(10, ctx.lineWidth); }, + 'test Context2d#antiAlias=': function(assert){ + var canvas = new Canvas(200, 200) + , ctx = canvas.getContext('2d'); + + assert.equal('default', ctx.antialias); + ctx.antialias = 'none'; + assert.equal('none', ctx.antialias); + ctx.antialias = 'gray'; + assert.equal('gray', ctx.antialias); + ctx.antialias = 'subpixel'; + assert.equal('subpixel', ctx.antialias); + ctx.antialias = 'invalid'; + assert.equal('subpixel', ctx.antialias); + ctx.antialias = 1; + assert.equal('subpixel', ctx.antialias); + }, + 'test Context2d#lineCap=': function(assert){ var canvas = new Canvas(200, 200) , ctx = canvas.getContext('2d'); diff --git a/test/public/tests.js b/test/public/tests.js index 28e42ea..e933182 100644 --- a/test/public/tests.js +++ b/test/public/tests.js @@ -1357,4 +1357,4 @@ tests['shadow integration'] = function(ctx){ ctx.arc(50, 151, 50, 0, Math.PI*2, false); ctx.fillStyle = "gold"; ctx.fill(); -}; +}; \ No newline at end of file