]> source.dussan.org Git - xmlgraphics-fop.git/commitdiff
Fixed NullPointerException when loading a TrueType font using XML font metric files.
authorJeremias Maerki <jeremias@apache.org>
Sat, 12 Apr 2008 09:02:01 +0000 (09:02 +0000)
committerJeremias Maerki <jeremias@apache.org>
Sat, 12 Apr 2008 09:02:01 +0000 (09:02 +0000)
git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/branches/fop-0_95@647403 13f79535-47bb-0310-9956-ffa450edef68

build.xml
src/java/org/apache/fop/fonts/FontReader.java
status.xml
test/java/org/apache/fop/StandardTestSuite.java
test/java/org/apache/fop/fonts/TrueTypeAnsiTestCase.java [new file with mode: 0644]
test/java/org/apache/fop/fonts/fonttest.xsl [new file with mode: 0644]

index c3c1a8a2fdc4a4e5baf257417a634f1e4e6413b4..17059be34a5c04c54e4b6704d0c5ddeac6d17ffe 100644 (file)
--- a/build.xml
+++ b/build.xml
@@ -707,6 +707,11 @@ list of possible build targets.
         </fileset>
       </classpath>
     </javac>
+    <copy todir="${build.dir}/test-classes">
+      <fileset dir="${basedir}/test/java">
+        <include name="**/*.xsl"/>
+      </fileset>
+    </copy>
   </target>
 
   <target name="junit-transcoder" depends="junit-compile" description="Runs FOP's JUnit transcoder tests" if="junit.present">
index 51d90c7e525de782f74e812c34e19ac5d35e3ceb..6f148f503e4952bd38e97498bae60ea8c3f2266e 100644 (file)
@@ -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)) {
index aefab0a72840907f9a0b0fd78a916a0b48e68d41..48ef3b2d3562041734353a54e0c1d5558bb9f095 100644 (file)
         </action>
       -->
     <!--/release-->
+    <release version="0.95" date="TBD">
+      <action context="Fonts" dev="JM" type="fix">
+        Fixed NullPointerException when loading a TrueType font using XML font metric files.
+      </action>
+    </release>
     <release version="0.95beta" date="26 March 2008">
       <notes>
         <section>
index 33e034de062553d7baf4958b421f3305d1ee44e3..6399d3b090ca805b28dc243078bf50c494f116e3 100644 (file)
@@ -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 (file)
index 0000000..87dd6ab
--- /dev/null
@@ -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(
+                "<root font-family='" + fontFamily + "'>Test!</root>"));
+        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 (file)
index 0000000..26c7d72
--- /dev/null
@@ -0,0 +1,24 @@
+<?xml version="1.0" encoding="utf-8"?>
+<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
+       xmlns:fo="http://www.w3.org/1999/XSL/Format">
+
+       <xsl:output method="xml" indent="yes"/>
+
+       <xsl:template match="root">
+               <fo:root>
+                       <fo:layout-master-set>
+                               <fo:simple-page-master master-name="A4" page-height="29.7cm" page-width="21cm"
+                                       margin="2cm">
+                                       <fo:region-body/>
+                               </fo:simple-page-master>
+                       </fo:layout-master-set>
+                       <fo:page-sequence master-reference="A4">
+                               <fo:flow flow-name="xsl-region-body">
+                                       <fo:block font-family="{@font-family}">
+                                               <xsl:value-of select="."/>
+                                       </fo:block>
+                               </fo:flow>
+                       </fo:page-sequence>
+               </fo:root>
+       </xsl:template>
+</xsl:stylesheet>