Skip to content

Commit

Permalink
#472 Use a custom font for custom object drawer test.
Browse files Browse the repository at this point in the history
Otherwise, an environment font may be embedded, breaking the test on different platforms.
  • Loading branch information
danfickle committed May 9, 2020
1 parent ad91bf8 commit 504e6b2
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 67 deletions.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,14 +1,29 @@
package com.openhtmltopdf.visualregressiontests;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontFormatException;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.geom.Rectangle2D;
import java.io.File;
import java.io.IOException;
import java.util.Map;

import static org.junit.Assert.assertTrue;

import org.junit.Before;
import org.junit.BeforeClass;
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.outputdevice.helper.BaseRendererBuilder.FontStyle;
import com.openhtmltopdf.render.RenderingContext;
import com.openhtmltopdf.visualtest.TestSupport;
import com.openhtmltopdf.visualtest.VisualTester;

Expand Down Expand Up @@ -558,5 +573,80 @@ public void testColumnsNestedUnbalanced() throws IOException {
public void testColumnsFloatsUnbalanced() throws IOException {
assertTrue(run("columns-floats-unbalanced"));
}


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;
try {
Font parent = Font.createFont(Font.TRUETYPE_FONT, new File("target/test/visual-tests/Karla-Bold.ttf"));
font = parent.deriveFont(20f);
} catch (FontFormatException | IOException e1) {
e1.printStackTrace();
throw new RuntimeException(e1);
}

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 static position object on multiple pages.
*/
@Test
public void testIssue472CustomObjectDrawerMultiplePages() throws IOException {
assertTrue(vtester.runTest("issue-472-custom-object-drawer-multiple-pages", builder -> {
builder.useFont(new File("target/test/visual-tests/Karla-Bold.ttf"), "Karla", 700, FontStyle.NORMAL, true);
builder.useObjectDrawerFactory(new WatermarkDrawerFactory());
}));
}

/**
* 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(vtester.runTest("issue-472-add-semi-transparent-watermark", builder -> {
builder.useFont(new File("target/test/visual-tests/Karla-Bold.ttf"), "Karla", 700, FontStyle.NORMAL, true);
builder.useObjectDrawerFactory(new WatermarkDrawerFactory());
}));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1053,59 +1053,6 @@ 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 static position object on multiple pages.
*/
@Test
public void testIssue472CustomObjectDrawerMultiplePages() throws IOException {
assertTrue(vt.runTest("issue-472-custom-object-drawer-multiple-pages", builder -> {
builder.useObjectDrawerFactory(new WatermarkDrawerFactory());
}));
}

/**
* Tests that position: fixed on multiple pages is correctly positoned
* on pages after page one.
Expand All @@ -1115,19 +1062,6 @@ public void testIssue472FixedPositionMultiplePages() throws IOException {
assertTrue(vt.runTest("issue-472-fixed-position-multiple-pages"));
}

/**
* 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 504e6b2

Please sign in to comment.