Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support evenodd fill rule #762

Closed
wants to merge 10 commits into from
Closed
47 changes: 14 additions & 33 deletions src/CanvasRenderingContext2d.cc
Expand Up @@ -297,8 +297,15 @@ Context2d::restorePath() {
*/

void
Context2d::setFillRule(cairo_fill_rule_t rule) {
cairo_set_fill_rule(_context, rule);
Context2d::setFillRule(v8::Local<v8::Value> value) {
if (value->IsString()) {
cairo_fill_rule_t rule = CAIRO_FILL_RULE_WINDING;
String::Utf8Value str(value);
if (std::strcmp(*str, "evenodd") == 0) {
rule = CAIRO_FILL_RULE_EVEN_ODD;
}
cairo_set_fill_rule(_context, rule);
}
}

void
Expand Down Expand Up @@ -1403,22 +1410,10 @@ NAN_METHOD(Context2d::IsPointInPath) {
cairo_t *ctx = context->context();
double x = info[0]->NumberValue()
, y = info[1]->NumberValue();
bool ctxSaved = false;
if (info.Length() == 3 && info[2]->IsString()) {
cairo_fill_rule_t rule = CAIRO_FILL_RULE_WINDING;
String::Utf8Value str(info[2]);
if (std::strcmp(*str, "evenodd") == 0) {
rule = CAIRO_FILL_RULE_EVEN_ODD;
}
// save cairo context for following tests
cairo_save(ctx);
ctxSaved = true;
context->setFillRule(rule);
}
cairo_save(ctx);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think you need cairo_save and cairo_restore here?

context->setFillRule(info[2]);
info.GetReturnValue().Set(Nan::New<Boolean>(cairo_in_fill(ctx, x, y) || cairo_in_stroke(ctx, x, y)));
if (ctxSaved) {
cairo_restore(ctx);
}
cairo_restore(ctx);
return;
}
info.GetReturnValue().Set(Nan::False());
Expand Down Expand Up @@ -1695,14 +1690,7 @@ NAN_METHOD(Context2d::Scale) {

NAN_METHOD(Context2d::Clip) {
Context2d *context = Nan::ObjectWrap::Unwrap<Context2d>(info.This());
if (info.Length() == 1 && info[0]->IsString()) {
cairo_fill_rule_t rule = CAIRO_FILL_RULE_WINDING;
String::Utf8Value str(info[0]);
if (std::strcmp(*str, "evenodd") == 0) {
rule = CAIRO_FILL_RULE_EVEN_ODD;
}
context->setFillRule(rule);
}
context->setFillRule(info[0]);
cairo_t *ctx = context->context();
cairo_clip_preserve(ctx);
}
Expand All @@ -1713,14 +1701,7 @@ NAN_METHOD(Context2d::Clip) {

NAN_METHOD(Context2d::Fill) {
Context2d *context = Nan::ObjectWrap::Unwrap<Context2d>(info.This());
if (info.Length() == 1 && info[0]->IsString()) {
cairo_fill_rule_t rule = CAIRO_FILL_RULE_WINDING;
String::Utf8Value str(info[0]);
if (std::strcmp(*str, "evenodd") == 0) {
rule = CAIRO_FILL_RULE_EVEN_ODD;
}
context->setFillRule(rule);
}
context->setFillRule(info[0]);
context->fill(true);
}

Expand Down
2 changes: 1 addition & 1 deletion src/CanvasRenderingContext2d.h
Expand Up @@ -161,7 +161,7 @@ class Context2d: public Nan::ObjectWrap {
void restorePath();
void saveState();
void restoreState();
void inline setFillRule(cairo_fill_rule_t rule = CAIRO_FILL_RULE_WINDING);
void inline setFillRule(v8::Local<v8::Value> value);
void fill(bool preserve = false);
void stroke(bool preserve = false);
void save();
Expand Down