|
8 | 8 | import java.io.ByteArrayOutputStream; |
9 | 9 | import java.io.File; |
10 | 10 | import java.io.IOException; |
| 11 | +import java.util.ArrayList; |
| 12 | +import java.util.List; |
11 | 13 |
|
12 | 14 | import org.apache.commons.io.FileUtils; |
13 | 15 | import org.apache.pdfbox.io.IOUtils; |
|
26 | 28 | import org.apache.pdfbox.pdmodel.interactive.form.PDTextField; |
27 | 29 | import org.apache.pdfbox.util.Charsets; |
28 | 30 | import org.hamcrest.CustomTypeSafeMatcher; |
| 31 | +import org.junit.Assert; |
29 | 32 | import org.junit.Ignore; |
30 | 33 | import org.junit.Test; |
31 | 34 |
|
32 | 35 | import com.openhtmltopdf.pdfboxout.PdfRendererBuilder; |
33 | 36 | import com.openhtmltopdf.testcases.TestcaseRunner; |
| 37 | +import com.openhtmltopdf.visualtest.TestSupport; |
34 | 38 | import com.openhtmltopdf.visualtest.VisualTester.BuilderConfig; |
35 | 39 |
|
36 | 40 | public class NonVisualRegressionTest { |
@@ -717,7 +721,106 @@ public void testInputWithoutNameAttribute() throws IOException { |
717 | 721 | assertEquals(0, form.getFields().size()); |
718 | 722 | remove("input-without-name-attribute", doc); |
719 | 723 | } |
720 | | - |
| 724 | + |
| 725 | + private static float[] getQuadPoints(PDDocument doc, int pg, int linkIndex) throws IOException { |
| 726 | + return ((PDAnnotationLink) doc.getPage(pg).getAnnotations().get(linkIndex)).getQuadPoints(); |
| 727 | + } |
| 728 | + |
| 729 | + private static String print(float[] floats) { |
| 730 | + StringBuilder sb = new StringBuilder(); |
| 731 | + |
| 732 | + sb.append("new float[] { "); |
| 733 | + for (float floater : floats) { |
| 734 | + sb.append(floater); |
| 735 | + sb.append("f, "); |
| 736 | + } |
| 737 | + |
| 738 | + sb.deleteCharAt(sb.length() - 2); |
| 739 | + sb.append("}"); |
| 740 | + |
| 741 | + return sb.toString(); |
| 742 | + } |
| 743 | + |
| 744 | + private static final float QUAD_DELTA = 0.5f; |
| 745 | + private static boolean qAssert(List<float[]> expectedList, float[] actual, StringBuilder sb, int pg, int linkIndex) { |
| 746 | + sb.append("PAGE: " + pg + ", LINK: " + linkIndex + "\n"); |
| 747 | + sb.append(" ACT(" + actual.length + "): " + print(actual) + "\n"); |
| 748 | + |
| 749 | + // NOTE: The shapes are returned as a map and are therefore placed on |
| 750 | + // the page in a non-determined order. So we just searh the expected |
| 751 | + // list for a match. |
| 752 | + ALL_EXPECTED: |
| 753 | + for (float[] expected : expectedList) { |
| 754 | + if (expected.length != actual.length) { |
| 755 | + continue; |
| 756 | + } |
| 757 | + |
| 758 | + for (int i = 0; i < expected.length; i++) { |
| 759 | + float diff = Math.abs(expected[i] - actual[i]); |
| 760 | + |
| 761 | + if (diff > QUAD_DELTA) { |
| 762 | + continue ALL_EXPECTED; |
| 763 | + } |
| 764 | + } |
| 765 | + |
| 766 | + return false; |
| 767 | + } |
| 768 | + |
| 769 | + sb.append(" !FAILED!"); |
| 770 | + sb.append("\n\n"); |
| 771 | + return true; |
| 772 | + } |
| 773 | + |
| 774 | + /** |
| 775 | + * Tests the shaped links support for custom object drawers |
| 776 | + * in the main document area and in the page margin on multiple |
| 777 | + * pages. |
| 778 | + */ |
| 779 | + @Test |
| 780 | + public void testPR480LinkShapes() throws IOException { |
| 781 | + try (PDDocument doc = run("pr-480-link-shapes", TestSupport.WITH_SHAPES_DRAWER)) { |
| 782 | + StringBuilder sb = new StringBuilder(); |
| 783 | + List<float[]> page0 = new ArrayList<>(); |
| 784 | + List<float[]> page1 = new ArrayList<>(); |
| 785 | + boolean failure = false; |
| 786 | + |
| 787 | + page0.add(new float[] { 486.75f, 251.25f, 468.0f, 213.75f, 486.75f, 213.75f, 505.5f, 213.75f, 486.75f, 251.25f, 505.5f, 213.75f, 496.125f, 232.5f, 486.75f, 251.25f }); |
| 788 | + page0.add(new float[] { 449.25f, 270.0f, 449.25f, 251.25f, 458.625f, 251.25f, 468.0f, 251.25f, 449.25f, 270.0f, 468.0f, 251.25f, 468.0f, 260.625f, 468.0f, 270.0f }); |
| 789 | + page0.add(new float[] { 505.5f, 213.75f, 505.5f, 195.0f, 514.875f, 195.0f, 524.25f, 195.0f, 505.5f, 213.75f, 524.25f, 195.0f, 524.25f, 204.375f, 524.25f, 213.75f }); |
| 790 | + page0.add(new float[] { 243.0f, 203.25f, 243.0f, 128.25f, 280.5f, 128.25f, 318.0f, 128.25f, 243.0f, 203.25f, 318.0f, 128.25f, 318.0f, 165.75f, 318.0f, 203.25f }); |
| 791 | + page0.add(new float[] { 168.0f, 353.25f, 93.0f, 203.25f, 168.0f, 203.25f, 243.0f, 203.25f, 168.0f, 353.25f, 243.0f, 203.25f, 205.5f, 278.25f, 168.0f, 353.25f }); |
| 792 | + page0.add(new float[] { 18.0f, 428.25f, 18.0f, 353.25f, 55.5f, 353.25f, 93.0f, 353.25f, 18.0f, 428.25f, 93.0f, 353.25f, 93.0f, 390.75f, 93.0f, 428.25f }); |
| 793 | + |
| 794 | + failure |= qAssert(page0, getQuadPoints(doc, 0, 0), sb, 0, 0); |
| 795 | + failure |= qAssert(page0, getQuadPoints(doc, 0, 1), sb, 0, 1); |
| 796 | + failure |= qAssert(page0, getQuadPoints(doc, 0, 2), sb, 0, 2); |
| 797 | + failure |= qAssert(page0, getQuadPoints(doc, 0, 3), sb, 0, 3); |
| 798 | + failure |= qAssert(page0, getQuadPoints(doc, 0, 4), sb, 0, 4); |
| 799 | + failure |= qAssert(page0, getQuadPoints(doc, 0, 5), sb, 0, 5); |
| 800 | + |
| 801 | + page1.add(new float[] { 486.75f, 251.25f, 468.0f, 213.75f, 486.75f, 213.75f, 505.5f, 213.75f, 486.75f, 251.25f, 505.5f, 213.75f, 496.125f, 232.5f, 486.75f, 251.25f }); |
| 802 | + page1.add(new float[] { 449.25f, 270.0f, 449.25f, 251.25f, 458.625f, 251.25f, 468.0f, 251.25f, 449.25f, 270.0f, 468.0f, 251.25f, 468.0f, 260.625f, 468.0f, 270.0f }); |
| 803 | + page1.add(new float[] { 505.5f, 213.75f, 505.5f, 195.0f, 514.875f, 195.0f, 524.25f, 195.0f, 505.5f, 213.75f, 524.25f, 195.0f, 524.25f, 204.375f, 524.25f, 213.75f }); |
| 804 | + page1.add(new float[] { 243.0f, 209.25f, 243.0f, 134.25f, 280.5f, 134.25f, 318.0f, 134.25f, 243.0f, 209.25f, 318.0f, 134.25f, 318.0f, 171.75f, 318.0f, 209.25f }); |
| 805 | + page1.add(new float[] { 168.0f, 359.25f, 93.0f, 209.25f, 168.0f, 209.25f, 243.0f, 209.25f, 168.0f, 359.25f, 243.0f, 209.25f, 205.5f, 284.25f, 168.0f, 359.25f }); |
| 806 | + page1.add(new float[] { 18.0f, 434.25f, 18.0f, 359.25f, 55.5f, 359.25f, 93.0f, 359.25f, 18.0f, 434.25f, 93.0f, 359.25f, 93.0f, 396.75f, 93.0f, 434.25f }); |
| 807 | + |
| 808 | + failure |= qAssert(page1, getQuadPoints(doc, 1, 0), sb, 1, 0); |
| 809 | + failure |= qAssert(page1, getQuadPoints(doc, 1, 1), sb, 1, 1); |
| 810 | + failure |= qAssert(page1, getQuadPoints(doc, 1, 2), sb, 1, 2); |
| 811 | + failure |= qAssert(page1, getQuadPoints(doc, 1, 3), sb, 1, 3); |
| 812 | + failure |= qAssert(page1, getQuadPoints(doc, 1, 4), sb, 1, 4); |
| 813 | + failure |= qAssert(page1, getQuadPoints(doc, 1, 5), sb, 1, 5); |
| 814 | + |
| 815 | + if (failure) { |
| 816 | + System.out.print(sb.toString()); |
| 817 | + Assert.fail("Quad points were not correct"); |
| 818 | + } |
| 819 | + |
| 820 | + remove("pr-480-link-shapes", doc); |
| 821 | + } |
| 822 | + } |
| 823 | + |
721 | 824 | // TODO: |
722 | 825 | // + More form controls. |
723 | 826 | // + Custom meta info. |
|
0 commit comments