You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PDFResourcesTestCase.java 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /* $Id: PDFReferenceTestCase.java 1551536 2013-12-17 13:15:06Z vhennebert $ */
  18. package org.apache.fop.pdf;
  19. import java.io.ByteArrayOutputStream;
  20. import java.io.IOException;
  21. import java.util.ArrayList;
  22. import java.util.Arrays;
  23. import java.util.List;
  24. import java.util.concurrent.atomic.AtomicInteger;
  25. import org.junit.Test;
  26. import static org.junit.Assert.assertTrue;
  27. /**
  28. * Test case for {@link PDFResources}.
  29. */
  30. public class PDFResourcesTestCase {
  31. private AtomicInteger patternCount = new AtomicInteger(0);
  32. private AtomicInteger objectNummerCount = new AtomicInteger(0);
  33. /**
  34. * Test PDF resources output with color space, pattern and shading.
  35. * @throws IOException
  36. */
  37. @Test
  38. public void testOutput() throws IOException {
  39. PDFDocument pdfDoc = new PDFDocument(null);
  40. PDFResources res = new PDFResources(pdfDoc);
  41. res.addColorSpace(this.createColorSpace());
  42. PDFResourceContext context = new PDFResourceContext(res);
  43. context.addPattern(this.createPDFPattern(res, pdfDoc));
  44. context.addShading(this.createPDFShading(res, pdfDoc));
  45. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  46. res.output(baos);
  47. String expectedShading = "/Shading << /Sh2 4 0 R >>";
  48. String expectedPattern = "/Pattern << /Pa1 2 0 R >>\n";
  49. String expectedColorspace = "/ColorSpace << /cs1 [/Separation /cs1 /DeviceRGB 1 0 R] >>\n";
  50. String outputString = baos.toString();
  51. assertTrue(outputString.contains(expectedShading));
  52. assertTrue(outputString.contains(expectedPattern));
  53. assertTrue(outputString.contains(expectedColorspace));
  54. }
  55. /**
  56. * Test PDF resources output with color space, pattern and shading,
  57. * if the PDF resource object has a parent resource object.
  58. * @throws IOException
  59. */
  60. @Test
  61. public void testOutputWithParent() throws IOException {
  62. PDFDocument pdfDoc = new PDFDocument(null);
  63. PDFResources res = new PDFResources(pdfDoc);
  64. PDFResources resParent = new PDFResources(pdfDoc);
  65. res.setParentResources(resParent);
  66. resParent.addColorSpace(this.createColorSpace());
  67. PDFResourceContext context = new PDFResourceContext(resParent);
  68. context.addPattern(this.createPDFPattern(resParent, pdfDoc));
  69. context.addShading(this.createPDFShading(resParent, pdfDoc));
  70. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  71. res.output(baos);
  72. String expectedShading = "/Shading << /Sh2 4 0 R >>";
  73. String expectedPattern = "/Pattern << /Pa1 2 0 R >>\n";
  74. String expectedColorspace = "/ColorSpace << /cs1 [/Separation /cs1 /DeviceRGB 1 0 R] >>\n";
  75. String outputString = baos.toString();
  76. assertTrue(outputString.contains(expectedShading));
  77. assertTrue(outputString.contains(expectedPattern));
  78. assertTrue(outputString.contains(expectedColorspace));
  79. }
  80. /**
  81. * Test PDF resources output with color space, pattern and shading,
  82. * if the PDF resource object has a parent resource object, that also has
  83. * color spaces, patterns and shadings.
  84. * @throws IOException
  85. */
  86. @Test
  87. public void testOutputWithParent2() throws IOException {
  88. PDFDocument pdfDoc = new PDFDocument(null);
  89. PDFResources res = new PDFResources(pdfDoc);
  90. PDFDictionary shadingDict = new PDFDictionary();
  91. shadingDict.put("Sh1-1718006973", new PDFReference("9 0 R"));
  92. res.put("Shading", shadingDict);
  93. PDFDictionary patternDict = new PDFDictionary();
  94. patternDict.put("Pa1-1718006973", new PDFReference("10 0 R"));
  95. res.put("Pattern", patternDict);
  96. PDFDictionary colorSpaceDict = new PDFDictionary();
  97. colorSpaceDict.put("DefaultRGB", new PDFReference("11 0 R"));
  98. res.put("ColorSpace", colorSpaceDict);
  99. PDFResources resParent = new PDFResources(pdfDoc);
  100. res.setParentResources(resParent);
  101. resParent.addColorSpace(this.createColorSpace());
  102. PDFResourceContext context = new PDFResourceContext(resParent);
  103. context.addPattern(this.createPDFPattern(resParent, pdfDoc));
  104. context.addShading(this.createPDFShading(resParent, pdfDoc));
  105. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  106. res.output(baos);
  107. String outputString = baos.toString();
  108. String expectedShading = "/Shading << /Sh1-1718006973 9 0 R /Sh2 4 0 R >>";
  109. String expectedPattern = "/Shading << /Sh1-1718006973 9 0 R /Sh2 4 0 R >>";
  110. String expectedColorspace = "/ColorSpace << /DefaultRGB 11 0 R"
  111. + " /cs1 [/Separation /cs1 /DeviceRGB 1 0 R] >>";
  112. assertTrue(outputString.contains(expectedShading));
  113. assertTrue(outputString.contains(expectedPattern));
  114. assertTrue(outputString.contains(expectedColorspace));
  115. }
  116. private PDFShading createPDFShading(PDFResources res, PDFDocument pdfDoc) {
  117. List<Double> coords = new ArrayList<Double>(4);
  118. coords.add(1d);
  119. coords.add(1d);
  120. coords.add(1d);
  121. coords.add(1d);
  122. PDFFunction pdfFunction = createPDFFunction();
  123. PDFDeviceColorSpace deviceColorspace = new PDFDeviceColorSpace(PDFDeviceColorSpace.DEVICE_RGB);
  124. PDFShading shading = new PDFShading(2, deviceColorspace, coords, pdfFunction);
  125. shading.setObjectNumber(objectNummerCount.incrementAndGet());
  126. shading.setDocument(pdfDoc);
  127. shading.setName("Sh" + patternCount.incrementAndGet());
  128. return shading;
  129. }
  130. private PDFColorSpace createColorSpace() {
  131. PDFFunction tintFunction = createPDFFunction();
  132. return new PDFSeparationColorSpace("cs1", tintFunction);
  133. }
  134. private PDFFunction createPDFFunction() {
  135. final Double zero = 0d;
  136. final Double one = 1d;
  137. List<Double> domain = Arrays.asList(new Double[] {zero, one});
  138. List<Double> range = Arrays.asList(new Double[] {zero, one, zero, one, zero, one});
  139. float[] cZero = new float[] {1f, 1f, 1f};
  140. float[] cOne = {0f, 0f, 0f};
  141. PDFFunction tintFunction = new PDFFunction(domain, range, cZero, cOne, 1.0d);
  142. tintFunction.setObjectNumber(objectNummerCount.incrementAndGet());
  143. return tintFunction;
  144. }
  145. private PDFPattern createPDFPattern(PDFResources res, PDFDocument pdfDoc) {
  146. List<Double> bbox = new ArrayList<Double>();
  147. bbox.add(1d);
  148. bbox.add(1d);
  149. bbox.add(1d);
  150. bbox.add(1d);
  151. List<Double> theMatrix = new ArrayList<Double>();
  152. for (int i = 0; i < 6; i++) {
  153. theMatrix.add(1d);
  154. }
  155. PDFPattern pattern = new PDFPattern(res, 1, 1, 1, bbox, 1, 1, theMatrix, null,
  156. new StringBuffer());
  157. pattern.setObjectNumber(objectNummerCount.incrementAndGet());
  158. pattern.setDocument(pdfDoc);
  159. pattern.setName("Pa" + patternCount.incrementAndGet());
  160. return pattern;
  161. }
  162. }