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 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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.io.ByteArrayOutputStream;
  20. import java.io.File;
  21. import java.io.IOException;
  22. import java.net.URI;
  23. import org.junit.Test;
  24. import static org.junit.Assert.assertEquals;
  25. import static org.junit.Assert.assertTrue;
  26. import org.apache.xmlgraphics.io.ResourceResolver;
  27. import org.apache.fop.apps.io.InternalResourceResolver;
  28. import org.apache.fop.apps.io.ResourceResolverFactory;
  29. import org.apache.fop.fonts.CIDSet;
  30. import org.apache.fop.fonts.CIDSubset;
  31. import org.apache.fop.fonts.CustomFont;
  32. import org.apache.fop.fonts.EmbeddingMode;
  33. import org.apache.fop.fonts.FontUris;
  34. import org.apache.fop.fonts.MultiByteFont;
  35. import org.apache.fop.fonts.truetype.OFFontLoader;
  36. /**
  37. * Test case for {@link PDFFactory}.
  38. */
  39. public class PDFFactoryTestCase {
  40. /**
  41. * This tests that when a font is subset embedded in a PDF, the font name is prefixed with a
  42. * pseudo-random tag as per the PDF spec.
  43. */
  44. @Test
  45. public void testSubsetFontNamePrefix() {
  46. class MockedFont extends MultiByteFont {
  47. public MockedFont(InternalResourceResolver resolver) {
  48. super(resolver, EmbeddingMode.AUTO);
  49. }
  50. @Override
  51. public int[] getWidths() {
  52. return new int[] {0};
  53. }
  54. @Override
  55. public CIDSet getCIDSet() {
  56. return new CIDSubset(this);
  57. }
  58. }
  59. PDFDocument doc = new PDFDocument("Test");
  60. PDFFactory pdfFactory = new PDFFactory(doc);
  61. URI thisURI = new File(".").toURI();
  62. ResourceResolver resolver = ResourceResolverFactory.createDefaultResourceResolver();
  63. InternalResourceResolver resourceResolver = ResourceResolverFactory.createInternalResourceResolver(
  64. thisURI, resolver);
  65. MockedFont font = new MockedFont(resourceResolver);
  66. PDFFont pdfDejaVu = pdfFactory.makeFont("DejaVu", "DejaVu", "TTF", font, font);
  67. assertEquals("/EAAAAA+DejaVu", pdfDejaVu.getBaseFont().toString());
  68. PDFFont pdfArial = pdfFactory.makeFont("Arial", "Arial", "TTF", font, font);
  69. assertEquals("/EAAAAB+Arial", pdfArial.getBaseFont().toString());
  70. }
  71. @Test
  72. public void testMakeOTFFont() throws IOException {
  73. InternalResourceResolver rr =
  74. ResourceResolverFactory.createDefaultInternalResourceResolver(new File(".").toURI());
  75. PDFDocument doc = new PDFDocument("");
  76. PDFFactory pdfFactory = new PDFFactory(doc);
  77. URI uri = new File("test/resources/fonts/otf/SourceSansProBold.otf").toURI();
  78. CustomFont sb = OFFontLoader.loadFont(new FontUris(uri, null),
  79. null, true, EmbeddingMode.SUBSET, null, false, false, rr, false, false);
  80. for (char c = 0; c < 512; c++) {
  81. sb.mapChar(c);
  82. }
  83. pdfFactory.makeFont("a", "a", "WinAnsiEncoding", sb, sb);
  84. ByteArrayOutputStream bos = new ByteArrayOutputStream();
  85. doc.outputTrailer(bos);
  86. assertEquals(pdfFactory.getDocument().getFontMap().size(), 2);
  87. PDFFont pdfFont = pdfFactory.getDocument().getFontMap().get("a_1");
  88. PDFEncoding enc = (PDFEncoding) pdfFont.get("Encoding");
  89. PDFArray diff = (PDFArray) enc.get("Differences");
  90. assertEquals(diff.length(), 80);
  91. assertEquals(diff.get(1).toString(), "/nacute");
  92. pdfFont = pdfFactory.getDocument().getFontMap().get("a");
  93. enc = (PDFEncoding) pdfFont.get("Encoding");
  94. diff = (PDFArray) enc.get("Differences");
  95. assertEquals(diff.length(), 257);
  96. assertEquals(diff.get(2).toString(), "/space");
  97. assertTrue(bos.toString().contains("/Subtype /Type1\n"));
  98. assertTrue(bos.toString().contains("/Subtype /Type1C"));
  99. }
  100. }