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.

TrueTypeAnsiTestCase.java 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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.fonts;
  19. import java.io.File;
  20. import java.io.OutputStream;
  21. import java.io.StringReader;
  22. import java.net.URL;
  23. import java.util.List;
  24. import javax.xml.transform.Result;
  25. import javax.xml.transform.Source;
  26. import javax.xml.transform.Transformer;
  27. import javax.xml.transform.TransformerFactory;
  28. import javax.xml.transform.sax.SAXResult;
  29. import javax.xml.transform.stream.StreamSource;
  30. import junit.framework.TestCase;
  31. import org.apache.commons.io.output.NullOutputStream;
  32. import org.apache.fop.apps.FOUserAgent;
  33. import org.apache.fop.apps.Fop;
  34. import org.apache.fop.apps.FopFactory;
  35. import org.apache.fop.fonts.apps.TTFReader;
  36. import org.apache.fop.render.pdf.PDFRenderer;
  37. /**
  38. * Tests XML font metrics file generation and usage for WinAnsi mode.
  39. */
  40. public class TrueTypeAnsiTestCase extends TestCase {
  41. /**
  42. * Tests a TrueType font in WinAnsi mode.
  43. * @throws Exception if an error occurs
  44. */
  45. public void testTrueTypeAnsi() throws Exception {
  46. String fontFamily = "Gladiator Bold";
  47. File ttfFile = new File("./test/resources/fonts/glb12.ttf");
  48. File workDir = new File("./build/test-results");
  49. if (!workDir.isDirectory()) {
  50. assertTrue(workDir.mkdirs());
  51. }
  52. File metricsFile = new File(workDir, ttfFile.getName() + ".xml");
  53. if (metricsFile.isFile()) {
  54. assertTrue(metricsFile.delete());
  55. }
  56. String[] args = new String[] {"-enc", "ansi",
  57. ttfFile.getCanonicalPath(), metricsFile.getCanonicalPath()};
  58. TTFReader.main(args);
  59. assertTrue(metricsFile.isFile());
  60. FopFactory fopFactory = FopFactory.newInstance();
  61. FOUserAgent ua = fopFactory.newFOUserAgent();
  62. PDFRenderer renderer = new PDFRenderer();
  63. renderer.setUserAgent(ua);
  64. List fontList = new java.util.ArrayList();
  65. List triplets = new java.util.ArrayList();
  66. triplets.add(new FontTriplet(fontFamily, "normal", Font.WEIGHT_NORMAL));
  67. EmbedFontInfo font = new EmbedFontInfo(
  68. metricsFile.toURI().toASCIIString(),
  69. true, triplets,
  70. ttfFile.toURI().toASCIIString(), null);
  71. fontList.add(font);
  72. renderer.addFontList(fontList);
  73. ua.setRendererOverride(renderer);
  74. OutputStream out = new NullOutputStream();
  75. Fop fop = fopFactory.newFop(null, ua, out);
  76. TransformerFactory tFactory = TransformerFactory.newInstance();
  77. Source src = new StreamSource(new StringReader(
  78. "<root font-family='" + fontFamily + "'>Test!</root>"));
  79. Result res = new SAXResult(fop.getDefaultHandler());
  80. Transformer transformer = tFactory.newTransformer(
  81. getSourceForResource(this, "fonttest.xsl"));
  82. transformer.transform(src, res);
  83. }
  84. private static Source getSourceForResource(Object reference, String name) {
  85. URL url = reference.getClass().getResource(name);
  86. if (url == null) {
  87. throw new NullPointerException("Resource not found: " + name);
  88. }
  89. return new StreamSource(url.toExternalForm());
  90. }
  91. }