Skip to content

Commit

Permalink
Fixes #752 - Use non-beveled borders for collapsed table cells...
Browse files Browse the repository at this point in the history
with solid style borders only.

This should stop them being anti-aliased and problematic.

Unfortunately some tests had to be re-proofed for this change.

Additional test kindly provided by @gandboy91
  • Loading branch information
danfickle committed Sep 7, 2021
1 parent b87ef1f commit 661233a
Show file tree
Hide file tree
Showing 10 changed files with 100 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.awt.geom.Arc2D;
import java.awt.geom.Area;
import java.awt.geom.Path2D;
import java.awt.geom.Rectangle2D;

import com.openhtmltopdf.css.constants.IdentValue;
import com.openhtmltopdf.css.parser.FSRGBColor;
Expand Down Expand Up @@ -81,7 +82,37 @@ public static Path2D generateBorderShape(Rectangle bounds, int side, BorderPrope
public static Path2D generateBorderShape(Rectangle bounds, int side, BorderPropertySet border, boolean drawInterior, float scaledOffset) {
return generateBorderShape(bounds, side, border, drawInterior, scaledOffset, 1);
}


/**
* Generate a simple rectangle without beveling for a solid border side.
* Turning off beveling should disable anti-aliasing and work better with
* table cell borders.
* See https://github.com/danfickle/openhtmltopdf/issues/752
*/
private static Shape generateSimpleBorderShape(Rectangle bounds, int currentSide, BorderPropertySet border) {
if (currentSide == TOP || currentSide == BOTTOM) {
double x = bounds.getX();
double y = currentSide == TOP ?
bounds.getY() :
bounds.getY() + bounds.getHeight() - border.bottom();
double w = bounds.getWidth();
double h = currentSide == TOP ?
border.top() : border.bottom();

return new Rectangle2D.Double(x, y, w, h);
} else {
double x = currentSide == LEFT ?
bounds.getX() :
bounds.getX() + bounds.getWidth() - border.right();
double y = bounds.getY();
double w = currentSide == LEFT ?
border.left() : border.right();
double h = bounds.getHeight();

return new Rectangle2D.Double(x, y, w, h);
}
}

/**
* Generates one side of a border
* @param bounds bounds of the container
Expand Down Expand Up @@ -328,23 +359,30 @@ private static void paintBorderSide(OutputDevice outputDevice,
0, 1, sides, currentSide, bevel);
} else if (borderSideStyle == IdentValue.SOLID) {
outputDevice.setStroke(new BasicStroke(1f));
if(currentSide == TOP) {

switch (currentSide) {
case TOP:
outputDevice.setColor(border.topColor());
outputDevice.fill(generateBorderShape(bounds, TOP, border, true, 0, 1));
}
if(currentSide == RIGHT) {
break;
case RIGHT:
outputDevice.setColor(border.rightColor());
outputDevice.fill(generateBorderShape(bounds, RIGHT, border, true, 0, 1));
}
if(currentSide == BOTTOM) {
break;
case BOTTOM:
outputDevice.setColor(border.bottomColor());
outputDevice.fill(generateBorderShape(bounds, BOTTOM, border, true, 0, 1));
}
if(currentSide == LEFT) {
break;
case LEFT:
outputDevice.setColor(border.leftColor());
outputDevice.fill(generateBorderShape(bounds, LEFT, border, true, 0, 1));
break;
default:
return;
}


Shape s = bevel || border.hasBorderRadius() ?
generateBorderShape(bounds, currentSide, border, true, 0, 1) :
generateSimpleBorderShape(bounds, currentSide, border);

outputDevice.fill(s);

} else if (borderSideStyle == IdentValue.DOUBLE) {
paintDoubleBorder(outputDevice, border, bounds, sides, currentSide, bevel);
} else {
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<html>
<head>
<style>
table {
border-collapse: collapse;
border: 1px solid black;
}
table td {
border: 1px solid black;
width: 100px;
}
table td.no-border-right {
border-right: none;
}
table td.no-border-left {
border-left: none;
}
</style>
</head>
<body>

<table>
<tr>
<td>1</td>
<td class="no-border-right">2</td>
<td class="no-border-left no-border-right">3</td>
<td class="no-border-left">4</td>
</tr>
<tr>
<td>5</td>
<td>6</td>
<td>7</td>
<td>8</td>
</tr>
</table>

</body>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -1450,6 +1450,17 @@ public void testIssue551PageBreakInsideAvoidDeep() throws IOException {
assertTrue(vt.runTest("issue-551-page-break-inside-avoid-deep"));
}

/**
* Test weirdness in table borders when PDF is zoomed.
* Apparently was caused by anti-aliasing selectively applied
* to beveled borders.
*/
@Test
public void testIssue752TableBorderInconcistency() throws IOException {
assertTrue(vt.runTest("issue-752-table-border-strange"));
}


// TODO:
// + Elements that appear just on generated overflow pages.
// + content property (page counters, etc)
Expand Down

0 comments on commit 661233a

Please sign in to comment.