Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

PDFFactoryTestCase.java 9.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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$ */
  18. package org.apache.fop.pdf;
  19. import java.awt.Rectangle;
  20. import java.awt.geom.Rectangle2D;
  21. import java.io.ByteArrayOutputStream;
  22. import java.io.File;
  23. import java.io.FileInputStream;
  24. import java.io.IOException;
  25. import java.io.InputStream;
  26. import java.net.URI;
  27. import org.junit.Test;
  28. import static org.junit.Assert.assertEquals;
  29. import static org.junit.Assert.assertNull;
  30. import static org.junit.Assert.assertTrue;
  31. import org.apache.xmlgraphics.io.ResourceResolver;
  32. import org.apache.fop.apps.io.InternalResourceResolver;
  33. import org.apache.fop.apps.io.ResourceResolverFactory;
  34. import org.apache.fop.fonts.CIDSet;
  35. import org.apache.fop.fonts.CIDSubset;
  36. import org.apache.fop.fonts.CodePointMapping;
  37. import org.apache.fop.fonts.CustomFont;
  38. import org.apache.fop.fonts.EmbeddingMode;
  39. import org.apache.fop.fonts.FontType;
  40. import org.apache.fop.fonts.FontUris;
  41. import org.apache.fop.fonts.MultiByteFont;
  42. import org.apache.fop.fonts.NamedCharacter;
  43. import org.apache.fop.fonts.SingleByteFont;
  44. import org.apache.fop.fonts.truetype.OFFontLoader;
  45. /**
  46. * Test case for {@link PDFFactory}.
  47. */
  48. public class PDFFactoryTestCase {
  49. /**
  50. * This tests that when a font is subset embedded in a PDF, the font name is prefixed with a
  51. * pseudo-random tag as per the PDF spec.
  52. */
  53. @Test
  54. public void testSubsetFontNamePrefix() {
  55. class MockedFont extends MultiByteFont {
  56. public MockedFont(InternalResourceResolver resolver) {
  57. super(resolver, EmbeddingMode.AUTO);
  58. }
  59. @Override
  60. public int[] getWidths() {
  61. return new int[] {0};
  62. }
  63. @Override
  64. public CIDSet getCIDSet() {
  65. return new CIDSubset(this);
  66. }
  67. }
  68. PDFDocument doc = new PDFDocument("Test");
  69. PDFFactory pdfFactory = new PDFFactory(doc);
  70. URI thisURI = new File(".").toURI();
  71. ResourceResolver resolver = ResourceResolverFactory.createDefaultResourceResolver();
  72. InternalResourceResolver resourceResolver = ResourceResolverFactory.createInternalResourceResolver(
  73. thisURI, resolver);
  74. MockedFont font = new MockedFont(resourceResolver);
  75. PDFFont pdfDejaVu = pdfFactory.makeFont("DejaVu", "DejaVu", "TTF", font, font);
  76. assertEquals("/EAAAAA+DejaVu", pdfDejaVu.getBaseFont().toString());
  77. PDFFont pdfArial = pdfFactory.makeFont("Arial", "Arial", "TTF", font, font);
  78. assertEquals("/EAAAAB+Arial", pdfArial.getBaseFont().toString());
  79. }
  80. @Test
  81. public void testMakeFont() throws IOException {
  82. PDFDocument doc = new PDFDocument("");
  83. PDFFactory pdfFactory = new PDFFactory(doc);
  84. SingleByteFont sb = new TestSingleByteFont(null);
  85. sb.setFontName("test");
  86. sb.setWidth(0, 0);
  87. sb.setFlags(0);
  88. sb.setEmbedResourceName("");
  89. sb.mapChar('a');
  90. sb.addUnencodedCharacter(new NamedCharacter("xyz", String.valueOf((char) 0x2202)), 0, new Rectangle());
  91. sb.mapChar((char) 0x2202);
  92. sb.setEncoding(new CodePointMapping("FOPPDFEncoding", new int[0]));
  93. PDFFont font = pdfFactory.makeFont("a", "a", "WinAnsiEncoding", sb, sb);
  94. ByteArrayOutputStream bos = new ByteArrayOutputStream();
  95. font.output(bos);
  96. assertTrue(bos.toString().contains("/BaseFont /EAAAAA+a"));
  97. assertEquals(sb.getAdditionalEncodingCount(), 1);
  98. }
  99. @Test
  100. public void testMakeTrueTypeFont() throws IOException {
  101. PDFDocument doc = new PDFDocument("");
  102. PDFFactory pdfFactory = new PDFFactory(doc);
  103. SingleByteFont sb = new TestSingleByteFont(null);
  104. sb.setFontType(FontType.TRUETYPE);
  105. sb.setFontName("test");
  106. sb.setWidth(0, 0);
  107. sb.setFlags(0);
  108. sb.setEncoding(new CodePointMapping("FOPPDFEncoding", new int[0]));
  109. String enc = "MacRomanEncoding";
  110. PDFFont font = pdfFactory.makeFont("a", "a", enc, sb, sb);
  111. font.output(new ByteArrayOutputStream());
  112. assertEquals(((PDFName)font.entries.get("Encoding")).getName(), enc);
  113. }
  114. class TestSingleByteFont extends SingleByteFont {
  115. public TestSingleByteFont(InternalResourceResolver resourceResolver) {
  116. super(resourceResolver, EmbeddingMode.SUBSET);
  117. }
  118. public InputStream getInputStream() throws IOException {
  119. File f = new File("test/resources/fonts/type1/a010013l.pfb");
  120. return new FileInputStream(f);
  121. }
  122. }
  123. @Test
  124. public void testMakeOTFFont() throws IOException {
  125. InternalResourceResolver rr =
  126. ResourceResolverFactory.createDefaultInternalResourceResolver(new File(".").toURI());
  127. PDFDocument doc = new PDFDocument("");
  128. PDFFactory pdfFactory = new PDFFactory(doc);
  129. URI uri = new File("test/resources/fonts/otf/SourceSansProBold.otf").toURI();
  130. CustomFont sb = OFFontLoader.loadFont(new FontUris(uri, null),
  131. null, true, EmbeddingMode.SUBSET, null, false, false, rr, false, false, true);
  132. for (char c = 0; c < 512; c++) {
  133. sb.mapChar(c);
  134. }
  135. pdfFactory.makeFont("a", "a", "WinAnsiEncoding", sb, sb);
  136. ByteArrayOutputStream bos = new ByteArrayOutputStream();
  137. doc.outputTrailer(bos);
  138. assertEquals(pdfFactory.getDocument().getFontMap().size(), 2);
  139. PDFFont pdfFont = pdfFactory.getDocument().getFontMap().get("a_1");
  140. PDFEncoding enc = (PDFEncoding) pdfFont.get("Encoding");
  141. PDFArray diff = (PDFArray) enc.get("Differences");
  142. assertEquals(diff.length(), 80);
  143. assertEquals(diff.get(1).toString(), "/nacute");
  144. pdfFont = pdfFactory.getDocument().getFontMap().get("a");
  145. enc = (PDFEncoding) pdfFont.get("Encoding");
  146. diff = (PDFArray) enc.get("Differences");
  147. assertEquals(diff.length(), 257);
  148. assertEquals(diff.get(2).toString(), "/space");
  149. assertTrue(bos.toString().contains("/Subtype /Type1\n"));
  150. assertTrue(bos.toString().contains("/Subtype /Type1C"));
  151. }
  152. @Test
  153. public void testMakeOTFFontPDFA() throws IOException {
  154. InternalResourceResolver rr =
  155. ResourceResolverFactory.createDefaultInternalResourceResolver(new File(".").toURI());
  156. PDFDocument doc = new PDFDocument("");
  157. doc.getProfile().setPDFAMode(PDFAMode.PDFA_2A);
  158. PDFFactory pdfFactory = new PDFFactory(doc);
  159. URI uri = new File("test/resources/fonts/otf/SourceSansProBold.otf").toURI();
  160. CustomFont sb = OFFontLoader.loadFont(new FontUris(uri, null),
  161. null, true, EmbeddingMode.SUBSET, null, false, false, rr, false, false, true);
  162. for (char c = 0; c < 512; c++) {
  163. sb.mapChar(c);
  164. }
  165. pdfFactory.makeFont("a", "a", "WinAnsiEncoding", sb, sb);
  166. PDFFont pdfFont = pdfFactory.getDocument().getFontMap().get("a_1");
  167. PDFFontDescriptor fontDescriptor = (PDFFontDescriptor) pdfFont.get("FontDescriptor");
  168. assertNull(fontDescriptor.getCIDSet());
  169. }
  170. @Test
  171. public void testGetExternalAction() {
  172. String germanAe = "\u00E4";
  173. String filename = "test";
  174. String unicodeFilename = "t" + germanAe + "st.pdf";
  175. PDFFileSpec fileSpec = new PDFFileSpec(filename, unicodeFilename);
  176. PDFDocument doc = new PDFDocument("");
  177. doc.registerObject(fileSpec);
  178. PDFNames names = doc.getRoot().getNames();
  179. if (names == null) {
  180. //Add Names if not already present
  181. names = doc.getFactory().makeNames();
  182. doc.getRoot().setNames(names);
  183. }
  184. PDFEmbeddedFiles embeddedFiles = names.getEmbeddedFiles();
  185. if (embeddedFiles == null) {
  186. embeddedFiles = new PDFEmbeddedFiles();
  187. doc.assignObjectNumber(embeddedFiles);
  188. doc.addTrailerObject(embeddedFiles);
  189. names.setEmbeddedFiles(embeddedFiles);
  190. }
  191. PDFArray nameArray = embeddedFiles.getNames();
  192. if (nameArray == null) {
  193. nameArray = new PDFArray();
  194. embeddedFiles.setNames(nameArray);
  195. }
  196. nameArray.add(fileSpec.getFilename());
  197. nameArray.add(new PDFReference(fileSpec));
  198. PDFFactory pdfFactory = new PDFFactory(doc);
  199. String target = "embedded-file:" + unicodeFilename;
  200. PDFJavaScriptLaunchAction pdfAction = (PDFJavaScriptLaunchAction)
  201. pdfFactory.getExternalAction(target, false);
  202. String expectedString = "<<\n/S /JavaScript\n/JS (this.exportDataObject\\({cName:\""
  203. + fileSpec.getFilename() + "\", nLaunch:2}\\);)\n>>";
  204. assertEquals(expectedString, pdfAction.toPDFString());
  205. }
  206. @Test
  207. public void testMakeLink() {
  208. PDFDocument doc = new PDFDocument("");
  209. PDFFactory pdfFactory = new PDFFactory(doc);
  210. Rectangle2D rect = new Rectangle(10, 20);
  211. PDFLink link = pdfFactory.makeLink(rect, "dest", true);
  212. String expectedString = "<< /Type /Annot\n" + "/Subtype /Link\n" + "/Rect [ "
  213. + "0.0 0.0 10.0 20.0 ]\n/C [ 0 0 0 ]\n"
  214. + "/Border [ 0 0 0 ]\n" + "/A 1 0 R"
  215. + "\n/H /I\n\n>>";
  216. assertEquals(expectedString, link.toPDFString());
  217. }
  218. }