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 support for dotted/dashed border styles #1146

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
3 changes: 3 additions & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,9 @@ module.exports = function(grunt) {
});

grunt.registerTask('webdriver', 'Browser render tests', function(browser, test) {
if (process.env.TRAVIS_PULL_REQUEST != "false") {
return;
}
var selenium = require("./tests/selenium.js");
var done = this.async();
var browsers = (browser) ? [grunt.config.get(this.name + "." + browser)] : _.values(grunt.config.get(this.name));
Expand Down
51 changes: 51 additions & 0 deletions src/nodeparser.js
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,8 @@ NodeParser.prototype.parseBorders = function(container) {
return {
width: container.cssInt('border' + side + 'Width'),
color: colorTransform ? color[colorTransform[0]](colorTransform[1]) : color,
style: style,
pathArgs: null,
args: null
};
});
Expand All @@ -511,6 +513,12 @@ NodeParser.prototype.parseBorders = function(container) {
};

function calculateBorders(borders, nodeBounds, borderPoints, radius) {
var pathBounds = {
top: nodeBounds.top + borders[0].width/2,
right: nodeBounds.right - borders[1].width/2,
bottom: nodeBounds.bottom - borders[2].width/2,
left: nodeBounds.left + borders[3].width/2
};
return borders.map(function(border, borderSide) {
if (border.width > 0) {
var bx = nodeBounds.left;
Expand All @@ -529,6 +537,11 @@ function calculateBorders(borders, nodeBounds, borderPoints, radius) {
c4: [bx + borders[3].width, by + bh]
}, radius[0], radius[1],
borderPoints.topLeftOuter, borderPoints.topLeftInner, borderPoints.topRightOuter, borderPoints.topRightInner);
border.pathArgs = drawSidePath({
c1: [pathBounds.left, pathBounds.top],
c2: [pathBounds.right, pathBounds.top]
}, radius[0], radius[1],
borderPoints.topLeft, borderPoints.topRight);
break;
case 1:
// right border
Expand All @@ -542,6 +555,11 @@ function calculateBorders(borders, nodeBounds, borderPoints, radius) {
c4: [bx, by + borders[0].width]
}, radius[1], radius[2],
borderPoints.topRightOuter, borderPoints.topRightInner, borderPoints.bottomRightOuter, borderPoints.bottomRightInner);
border.pathArgs = drawSidePath({
c1: [pathBounds.right, pathBounds.top],
c2: [pathBounds.right, pathBounds.bottom]
}, radius[1], radius[2],
borderPoints.topRight, borderPoints.bottomRight);
break;
case 2:
// bottom border
Expand All @@ -554,6 +572,11 @@ function calculateBorders(borders, nodeBounds, borderPoints, radius) {
c4: [bx + bw - borders[3].width, by]
}, radius[2], radius[3],
borderPoints.bottomRightOuter, borderPoints.bottomRightInner, borderPoints.bottomLeftOuter, borderPoints.bottomLeftInner);
border.pathArgs = drawSidePath({
c1: [pathBounds.right, pathBounds.bottom],
c2: [pathBounds.left, pathBounds.bottom]
}, radius[2], radius[3],
borderPoints.bottomRight, borderPoints.bottomLeft);
break;
case 3:
// left border
Expand All @@ -565,6 +588,11 @@ function calculateBorders(borders, nodeBounds, borderPoints, radius) {
c4: [bx + bw, by + bh]
}, radius[3], radius[0],
borderPoints.bottomLeftOuter, borderPoints.bottomLeftInner, borderPoints.topLeftOuter, borderPoints.topLeftInner);
border.pathArgs = drawSidePath({
c1: [pathBounds.left, pathBounds.bottom],
c2: [pathBounds.left, pathBounds.top]
}, radius[3], radius[0],
borderPoints.bottomLeft, borderPoints.topLeft);
break;
}
}
Expand Down Expand Up @@ -631,6 +659,11 @@ function calculateCurvePoints(bounds, borderRadius, borders) {
leftHeight = height - blv;

return {
topLeft: getCurvePoints(x + borders[3].width/2, y + borders[0].width/2, Math.max(0, tlh - borders[3].width/2), Math.max(0, tlv - borders[0].width/2)).topLeft.subdivide(0.5),
topRight: getCurvePoints(x + Math.min(topWidth, width + borders[3].width/2), y + borders[0].width/2, (topWidth > width + borders[3].width/2) ? 0 :trh - borders[3].width/2, trv - borders[0].width/2).topRight.subdivide(0.5),
bottomRight: getCurvePoints(x + Math.min(bottomWidth, width - borders[3].width/2), y + Math.min(rightHeight, height + borders[0].width/2), Math.max(0, brh - borders[1].width/2), brv - borders[2].width/2).bottomRight.subdivide(0.5),
bottomLeft: getCurvePoints(x + borders[3].width/2, y + leftHeight, Math.max(0, blh - borders[3].width/2), blv - borders[2].width/2).bottomLeft.subdivide(0.5),

topLeftOuter: getCurvePoints(x, y, tlh, tlv).topLeft.subdivide(0.5),
topLeftInner: getCurvePoints(x + borders[3].width, y + borders[0].width, Math.max(0, tlh - borders[3].width), Math.max(0, tlv - borders[0].width)).topLeft.subdivide(0.5),
topRightOuter: getCurvePoints(x + topWidth, y, trh, trv).topRight.subdivide(0.5),
Expand Down Expand Up @@ -703,6 +736,24 @@ function drawSide(borderData, radius1, radius2, outer1, inner1, outer2, inner2)
return borderArgs;
}

function drawSidePath(borderData, radius1, radius2, curve1, curve2) {
var borderArgs = [];
if (radius1[0] > 0 || radius1[1] > 0) {
borderArgs.push(["line", curve1[1].start.x, curve1[1].start.y]);
curve1[1].curveTo(borderArgs);
} else {
borderArgs.push([ "line", borderData.c1[0], borderData.c1[1]]);
}
if (radius2[0] > 0 || radius2[1] > 0) {
borderArgs.push(["line", curve2[0].start.x, curve2[0].start.y]);
curve2[0].curveTo(borderArgs);
} else {
borderArgs.push([ "line", borderData.c2[0], borderData.c2[1]]);
}

return borderArgs;
}

function parseCorner(borderArgs, radius1, radius2, corner1, corner2, x, y) {
if (radius1[0] > 0 || radius1[1] > 0) {
borderArgs.push(["line", corner1[0].start.x, corner1[0].start.y]);
Expand Down
11 changes: 10 additions & 1 deletion src/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,16 @@ Renderer.prototype.renderBorders = function(borders) {

Renderer.prototype.renderBorder = function(data) {
if (!data.color.isTransparent() && data.args !== null) {
this.drawShape(data.args, data.color);
if (data.style === 'dashed' || data.style === 'dotted') {
var dash = (data.style === 'dashed') ? 3 : data.width;
this.ctx.setLineDash([dash]);
this.path(data.pathArgs);
this.ctx.strokeStyle = data.color;
this.ctx.lineWidth = data.width;
this.ctx.stroke();
} else {
this.drawShape(data.args, data.color);
}
}
};

Expand Down
12 changes: 12 additions & 0 deletions src/renderers/canvas.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,18 @@ CanvasRenderer.prototype.shape = function(shape) {
return this.ctx;
};

CanvasRenderer.prototype.path = function(shape) {
this.ctx.beginPath();
shape.forEach(function(point, index) {
if (point[0] === "rect") {
this.ctx.rect.apply(this.ctx, point.slice(1));
} else {
this.ctx[(index === 0) ? "moveTo" : point[0] + "To" ].apply(this.ctx, point.slice(1));
}
}, this);
return this.ctx;
};

CanvasRenderer.prototype.font = function(color, style, variant, weight, size, family) {
this.setFillStyle(color).font = [style, variant, weight, size, family].join(" ").split(",")[0];
};
Expand Down