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

add custom repeat-x/y processing #2066

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion src/CanvasRenderingContext2d.cc
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,7 @@ Context2d::setFillRule(v8::Local<v8::Value> value) {
void
Context2d::fill(bool preserve) {
cairo_pattern_t *new_pattern;
bool needsRestore = false;
if (state->fillPattern) {
if (state->globalAlpha < 1) {
new_pattern = create_transparent_pattern(state->fillPattern, state->globalAlpha);
Expand All @@ -381,10 +382,36 @@ Context2d::fill(bool preserve) {
cairo_set_source(_context, state->fillPattern);
}
repeat_type_t repeat = Pattern::get_repeat_type_for_cairo_pattern(state->fillPattern);
if (NO_REPEAT == repeat) {
if (repeat == NO_REPEAT) {
cairo_pattern_set_extend(cairo_get_source(_context), CAIRO_EXTEND_NONE);
} else if (repeat == REPEAT) {
cairo_pattern_set_extend(cairo_get_source(_context), CAIRO_EXTEND_REPEAT);
} else {
cairo_save(_context);
cairo_path_t *savedPath = cairo_copy_path(_context);
zbjornson marked this conversation as resolved.
Show resolved Hide resolved
cairo_surface_t *patternSurface = nullptr;
cairo_pattern_get_surface(cairo_get_source(_context), &patternSurface);

double width, height;
if (repeat == REPEAT_X) {
double x1, x2;
cairo_path_extents(_context, &x1, nullptr, &x2, nullptr);
width = x2 - x1;
height = cairo_image_surface_get_height(patternSurface);
} else {
double y1, y2;
cairo_path_extents(_context, nullptr, &y1, nullptr, &y2);
width = cairo_image_surface_get_width(patternSurface);
height = y2 - y1;
}

cairo_new_path(_context);
cairo_rectangle(_context, 0, 0, width, height);
cairo_clip(_context);
cairo_append_path(_context, savedPath);
cairo_path_destroy(savedPath);
cairo_pattern_set_extend(cairo_get_source(_context), CAIRO_EXTEND_REPEAT);
needsRestore = true;
}
} else if (state->fillGradient) {
if (state->globalAlpha < 1) {
Expand Down Expand Up @@ -412,6 +439,9 @@ Context2d::fill(bool preserve) {
? shadow(cairo_fill)
: cairo_fill(_context);
}
if (needsRestore) {
cairo_restore(_context);
}
}

/*
Expand Down
18 changes: 18 additions & 0 deletions test/public/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,24 @@ tests['createPattern() with globalAlpha'] = function (ctx, done) {
img.src = imageSrc('face.jpeg')
}

tests["createPattern() repeat-x and repeat-y"] = function (ctx, done) {
const img = new Image()
img.onload = function () {
ctx.scale(0.1, 0.1)
ctx.lineStyle = "black"
ctx.lineWidth = 10
ctx.fillStyle = ctx.createPattern(img, "repeat-x")
ctx.fillRect(0, 0, 900, 900)
ctx.strokeRect(0, 0, 900, 900)
ctx.translate(1000, 1000)
ctx.fillStyle = ctx.createPattern(img, "repeat-y")
ctx.fillRect(0, 0, 900, 900)
ctx.strokeRect(0, 0, 900, 900)
done()
};
img.src = imageSrc("face.jpeg")
}

tests['createPattern() no-repeat'] = function (ctx, done) {
const img = new Image()
img.onload = function () {
Expand Down