From 5e31dc3edea23adea68b0876b616fd7b0131ca78 Mon Sep 17 00:00:00 2001 From: Jeremias Maerki Date: Sat, 12 Apr 2008 09:02:01 +0000 Subject: [PATCH] Fixed NullPointerException when loading a TrueType font using XML font metric files. git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/branches/fop-0_95@647403 13f79535-47bb-0310-9956-ffa450edef68 --- build.xml | 5 + src/java/org/apache/fop/fonts/FontReader.java | 9 +- status.xml | 5 + .../org/apache/fop/StandardTestSuite.java | 2 + .../fop/fonts/TrueTypeAnsiTestCase.java | 107 ++++++++++++++++++ test/java/org/apache/fop/fonts/fonttest.xsl | 24 ++++ 6 files changed, 148 insertions(+), 4 deletions(-) create mode 100644 test/java/org/apache/fop/fonts/TrueTypeAnsiTestCase.java create mode 100644 test/java/org/apache/fop/fonts/fonttest.xsl diff --git a/build.xml b/build.xml index c3c1a8a2f..17059be34 100644 --- a/build.xml +++ b/build.xml @@ -707,6 +707,11 @@ list of possible build targets. + + + + + diff --git a/src/java/org/apache/fop/fonts/FontReader.java b/src/java/org/apache/fop/fonts/FontReader.java index 51d90c7e5..6f148f503 100644 --- a/src/java/org/apache/fop/fonts/FontReader.java +++ b/src/java/org/apache/fop/fonts/FontReader.java @@ -27,8 +27,6 @@ import java.util.Set; import javax.xml.parsers.SAXParserFactory; -import org.apache.fop.apps.FOPException; -import org.apache.fop.fonts.apps.TTFReader; import org.xml.sax.Attributes; import org.xml.sax.InputSource; import org.xml.sax.Locator; @@ -36,6 +34,9 @@ import org.xml.sax.SAXException; import org.xml.sax.XMLReader; import org.xml.sax.helpers.DefaultHandler; +import org.apache.fop.apps.FOPException; +import org.apache.fop.fonts.apps.TTFReader; + /** * Class for reading a metric.xml file and creating a font object. * Typical usage: @@ -228,11 +229,11 @@ public class FontReader extends DefaultHandler { if ("font-name".equals(localName)) { returnFont.setFontName(content); } else if ("full-name".equals(localName)) { - multiFont.setFullName(content); + returnFont.setFullName(content); } else if ("family-name".equals(localName)) { Set s = new java.util.HashSet(); s.add(content); - multiFont.setFamilyNames(s); + returnFont.setFamilyNames(s); } else if ("ttc-name".equals(localName) && isCID) { multiFont.setTTCName(content); } else if ("encoding".equals(localName)) { diff --git a/status.xml b/status.xml index aefab0a72..48ef3b2d3 100644 --- a/status.xml +++ b/status.xml @@ -59,6 +59,11 @@ --> + + + Fixed NullPointerException when loading a TrueType font using XML font metric files. + +
diff --git a/test/java/org/apache/fop/StandardTestSuite.java b/test/java/org/apache/fop/StandardTestSuite.java index 33e034de0..6399d3b09 100644 --- a/test/java/org/apache/fop/StandardTestSuite.java +++ b/test/java/org/apache/fop/StandardTestSuite.java @@ -22,6 +22,7 @@ package org.apache.fop; import junit.framework.Test; import junit.framework.TestSuite; +import org.apache.fop.fonts.TrueTypeAnsiTestCase; import org.apache.fop.render.pdf.PDFAConformanceTestCase; import org.apache.fop.render.pdf.PDFCMapTestCase; import org.apache.fop.render.pdf.PDFEncodingTestCase; @@ -47,6 +48,7 @@ public class StandardTestSuite { suite.addTest(new TestSuite(PDFEncodingTestCase.class)); suite.addTest(new TestSuite(PDFCMapTestCase.class)); suite.addTest(new TestSuite(PDFsRGBSettingsTestCase.class)); + suite.addTest(new TestSuite(TrueTypeAnsiTestCase.class)); suite.addTest(RichTextFormatTestSuite.suite()); //$JUnit-END$ return suite; diff --git a/test/java/org/apache/fop/fonts/TrueTypeAnsiTestCase.java b/test/java/org/apache/fop/fonts/TrueTypeAnsiTestCase.java new file mode 100644 index 000000000..87dd6abcf --- /dev/null +++ b/test/java/org/apache/fop/fonts/TrueTypeAnsiTestCase.java @@ -0,0 +1,107 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* $Id$ */ + +package org.apache.fop.fonts; + +import java.io.File; +import java.io.OutputStream; +import java.io.StringReader; +import java.net.URL; +import java.util.List; + +import javax.xml.transform.Result; +import javax.xml.transform.Source; +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.sax.SAXResult; +import javax.xml.transform.stream.StreamSource; + +import junit.framework.TestCase; + +import org.apache.commons.io.output.NullOutputStream; + +import org.apache.fop.apps.FOUserAgent; +import org.apache.fop.apps.Fop; +import org.apache.fop.apps.FopFactory; +import org.apache.fop.fonts.apps.TTFReader; +import org.apache.fop.render.pdf.PDFRenderer; + +/** + * Tests XML font metrics file generation and usage for WinAnsi mode. + */ +public class TrueTypeAnsiTestCase extends TestCase { + + /** + * Tests a TrueType font in WinAnsi mode. + * @throws Exception if an error occurs + */ + public void testTrueTypeAnsi() throws Exception { + String fontFamily = "Gladiator Bold"; + File ttfFile = new File("./test/resources/fonts/glb12.ttf"); + File workDir = new File("./build/test-results"); + if (!workDir.isDirectory()) { + assertTrue(workDir.mkdirs()); + } + File metricsFile = new File(workDir, ttfFile.getName() + ".xml"); + if (metricsFile.isFile()) { + assertTrue(metricsFile.delete()); + } + + String[] args = new String[] {"-enc", "ansi", + ttfFile.getCanonicalPath(), metricsFile.getCanonicalPath()}; + TTFReader.main(args); + assertTrue(metricsFile.isFile()); + + FopFactory fopFactory = FopFactory.newInstance(); + FOUserAgent ua = fopFactory.newFOUserAgent(); + PDFRenderer renderer = new PDFRenderer(); + renderer.setUserAgent(ua); + List fontList = new java.util.ArrayList(); + List triplets = new java.util.ArrayList(); + triplets.add(new FontTriplet(fontFamily, "normal", Font.WEIGHT_NORMAL)); + EmbedFontInfo font = new EmbedFontInfo( + metricsFile.toURI().toASCIIString(), + true, triplets, + ttfFile.toURI().toASCIIString()); + fontList.add(font); + renderer.addFontList(fontList); + + ua.setRendererOverride(renderer); + OutputStream out = new NullOutputStream(); + + Fop fop = fopFactory.newFop(null, ua, out); + + TransformerFactory tFactory = TransformerFactory.newInstance(); + Source src = new StreamSource(new StringReader( + "Test!")); + Result res = new SAXResult(fop.getDefaultHandler()); + Transformer transformer = tFactory.newTransformer( + getSourceForResource(this, "fonttest.xsl")); + transformer.transform(src, res); + } + + private static Source getSourceForResource(Object reference, String name) { + URL url = reference.getClass().getResource(name); + if (url == null) { + throw new NullPointerException("Resource not found: " + name); + } + return new StreamSource(url.toExternalForm()); + } + +} diff --git a/test/java/org/apache/fop/fonts/fonttest.xsl b/test/java/org/apache/fop/fonts/fonttest.xsl new file mode 100644 index 000000000..26c7d72a5 --- /dev/null +++ b/test/java/org/apache/fop/fonts/fonttest.xsl @@ -0,0 +1,24 @@ + + + + + + + + + + + + + + + + + + + + + + -- 2.39.5