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.

PDFFactoryTestCase.java 8.3KB

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