Skip to content

Commit

Permalink
#472 Failing test for using a custom object drawer inside a fixed ele…
Browse files Browse the repository at this point in the history
…ment.
  • Loading branch information
danfickle committed May 4, 2020
1 parent e0baa44 commit e5e6b8d
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<html>
<head>
<style>
@page {
size: 300px 300px;
margin: 10px;
}
object[type="watermark"] {
position: fixed;
display: block;
width: 100%;
height: 100%;
transform: rotate(45deg);
z-index: 1000;
}
</style>
</head>
<body>
<object type="watermark"></object>

Lorem ipsum dolor sit amet, consectetur adipiscing elit,
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Volutpat sed cras ornare arcu dui. Tempor id eu nisl nunc mi.
Urna nec tincidunt praesent semper. Elementum tempus egestas sed sed.
Consequat interdum varius sit amet mattis vulputate. Pulvinar etiam non quam
lacus suspendisse. Ultrices vitae auctor eu augue ut lectus. Adipiscing at in
tellus integer. Porta nibh venenatis cras sed felis eget. Fermentum iaculis eu
non diam phasellus. Urna condimentum mattis pellentesque id. Eu scelerisque felis
imperdiet proin. Arcu non odio euismod lacinia at quis risus.
Porta nibh venenatis cras sed felis eget. Fermentum iaculis eu
non diam phasellus. Urna condimentum mattis pellentesque id. Eu scelerisque felis
imperdiet proin. Arcu non odio euismod lacinia at quis risus.

Lorem ipsum dolor sit amet, consectetur adipiscing elit,
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Volutpat sed cras ornare arcu dui. Tempor id eu nisl nunc mi.
Urna nec tincidunt praesent semper. Elementum tempus egestas sed sed.
Consequat interdum varius sit amet mattis vulputate. Pulvinar etiam non quam
lacus suspendisse. Ultrices vitae auctor eu augue ut lectus. Adipiscing at in
tellus integer. Porta nibh venenatis cras sed felis eget. Fermentum iaculis eu
non diam phasellus. Urna condimentum mattis pellentesque id. Eu scelerisque felis
imperdiet proin. Arcu non odio euismod lacinia at quis risus.
Porta nibh venenatis cras sed felis eget. Fermentum iaculis eu
non diam phasellus. Urna condimentum mattis pellentesque id. Eu scelerisque felis
imperdiet proin. Arcu non odio euismod lacinia at quis risus.
</body>
</html>
Original file line number Diff line number Diff line change
@@ -1,18 +1,31 @@
package com.openhtmltopdf.visualregressiontests;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.geom.Rectangle2D;
import java.io.File;
import static org.junit.Assert.assertTrue;
import java.io.IOException;
import java.util.Map;

import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.w3c.dom.Element;

import com.openhtmltopdf.extend.FSObjectDrawer;
import com.openhtmltopdf.extend.FSObjectDrawerFactory;
import com.openhtmltopdf.extend.OutputDevice;
import com.openhtmltopdf.extend.OutputDeviceGraphicsDrawer;
import com.openhtmltopdf.latexsupport.LaTeXDOMMutator;
import com.openhtmltopdf.mathmlsupport.MathMLDrawer;
import com.openhtmltopdf.objects.jfreechart.JFreeChartBarDiagramObjectDrawer;
import com.openhtmltopdf.objects.jfreechart.JFreeChartPieDiagramObjectDrawer;
import com.openhtmltopdf.render.DefaultObjectDrawerFactory;
import com.openhtmltopdf.render.RenderingContext;
import com.openhtmltopdf.svgsupport.BatikSVGDrawer;
import com.openhtmltopdf.svgsupport.BatikSVGDrawer.SvgExternalResourceMode;
import com.openhtmltopdf.svgsupport.BatikSVGDrawer.SvgScriptMode;
Expand Down Expand Up @@ -1040,6 +1053,61 @@ public void testIssue473BulletsInPageMargin() throws IOException {
assertTrue(vt.runTest("issue-473-bullets-in-page-margin"));
}

private static class WatermarkDrawer implements FSObjectDrawer {
@Override
public Map<Shape, String> drawObject(Element e, double x, double y, double width, double height,
OutputDevice outputDevice, RenderingContext ctx, int dotsPerPixel) {
outputDevice.drawWithGraphics((float) x, (float) y, (float) width / dotsPerPixel,
(float) height / dotsPerPixel, (Graphics2D g2d) -> {

double realWidth = width / dotsPerPixel;
double realHeight = height / dotsPerPixel;

Font font = new Font("Arial", Font.BOLD, 20);
Rectangle2D bounds = font.getStringBounds("OpenHTMLToPDF", g2d.getFontRenderContext());

g2d.setFont(font);
g2d.setPaint(Color.RED);
g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.3f));

g2d.drawString("OpenHTMLToPDF",
(float) ((realWidth - bounds.getWidth()) / 2),
(float) ((realHeight - bounds.getHeight()) / 2));

});

return null;
}
}

private static class WatermarkDrawerFactory implements FSObjectDrawerFactory {
@Override
public FSObjectDrawer createDrawer(Element e) {
if (isReplacedObject(e)) {
return new WatermarkDrawer();
}
return null;
}

@Override
public boolean isReplacedObject(Element e) {
return e.getAttribute("type").equals("watermark");
}
}

/**
* Tests adding a transparent watermark using a custom object drawer
* inside a fixed position object.
*/
@Test
@Ignore // Works well on the first page, but not positioned correctly on
// subsequent pages.
public void testIssue472AddSemiTransparentWatermark() throws IOException {
assertTrue(vt.runTest("issue-472-add-semi-transparent-watermark", builder -> {
builder.useObjectDrawerFactory(new WatermarkDrawerFactory());
}));
}

/**
* Tests what the CSS content property is capable of.
*/
Expand Down

0 comments on commit e5e6b8d

Please sign in to comment.