From 5c8a6c2f417cb2cbc0fcfeebcbae7afc95d6c92b Mon Sep 17 00:00:00 2001 From: Simon Steiner Date: Mon, 6 Feb 2017 09:50:05 +0000 Subject: [PATCH] FOP-2686: Error for Type1 font without extension for TIFF output git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@1781856 13f79535-47bb-0310-9956-ffa450edef68 --- .../java/org/apache/fop/fonts/FontLoader.java | 9 ++- .../java2d/ConfiguredFontCollection.java | 4 +- .../ConfiguredFontCollectionTestCase.java | 73 +++++++++++++++++++ 3 files changed, 80 insertions(+), 6 deletions(-) create mode 100644 fop-core/src/test/java/org/apache/fop/render/java2d/ConfiguredFontCollectionTestCase.java diff --git a/fop-core/src/main/java/org/apache/fop/fonts/FontLoader.java b/fop-core/src/main/java/org/apache/fop/fonts/FontLoader.java index 9c11db2e0..06907f403 100644 --- a/fop-core/src/main/java/org/apache/fop/fonts/FontLoader.java +++ b/fop-core/src/main/java/org/apache/fop/fonts/FontLoader.java @@ -71,13 +71,14 @@ public abstract class FontLoader { this.resourceResolver = resourceResolver; } - private static boolean isType1(URI fontURI) { - return fontURI.toASCIIString().toLowerCase().endsWith(".pfb"); + private static boolean isType1(FontUris fontUris) { + return fontUris.getEmbed().toASCIIString().toLowerCase().endsWith(".pfb") || fontUris.getAfm() != null + || fontUris.getPfm() != null; } /** * Loads a custom font from a URI. In the case of Type 1 fonts, the PFB file must be specified. - * @param fontFileURI the URI to the font + * @param fontUris the URI to the font * @param subFontName the sub-fontname of a font (for TrueType Collections, null otherwise) * @param embedded indicates whether the font is embedded or referenced * @param embeddingMode the embedding mode of the font @@ -93,7 +94,7 @@ public abstract class FontLoader { boolean embedded, EmbeddingMode embeddingMode, EncodingMode encodingMode, boolean useKerning, boolean useAdvanced, InternalResourceResolver resourceResolver, boolean simulateStyle, boolean embedAsType1) throws IOException { - boolean type1 = isType1(fontUris.getEmbed()); + boolean type1 = isType1(fontUris); FontLoader loader; if (type1) { if (encodingMode == EncodingMode.CID) { diff --git a/fop-core/src/main/java/org/apache/fop/render/java2d/ConfiguredFontCollection.java b/fop-core/src/main/java/org/apache/fop/render/java2d/ConfiguredFontCollection.java index 4ad2044cb..e494ed65c 100644 --- a/fop-core/src/main/java/org/apache/fop/render/java2d/ConfiguredFontCollection.java +++ b/fop-core/src/main/java/org/apache/fop/render/java2d/ConfiguredFontCollection.java @@ -73,7 +73,7 @@ public class ConfiguredFontCollection implements FontCollection { internalName = "F" + num++; try { URI fontURI = configFontInfo.getEmbedURI(); - FontMetricsMapper font = null; + FontMetricsMapper font; URI metricsURI = configFontInfo.getMetricsURI(); // If the user specified an XML-based metrics file, we'll use it // Otherwise, calculate metrics directly from the font file. @@ -82,7 +82,7 @@ public class ConfiguredFontCollection implements FontCollection { InputStream fontSource = resourceResolver.getResource(fontURI); font = new CustomFontMetricsMapper(fontMetrics, fontSource); } else { - FontUris fontUris = new FontUris(fontURI, null); + FontUris fontUris = configFontInfo.getFontUris(); CustomFont fontMetrics = FontLoader.loadFont(fontUris, configFontInfo.getSubFontName(), true, configFontInfo.getEmbeddingMode(), configFontInfo.getEncodingMode(), diff --git a/fop-core/src/test/java/org/apache/fop/render/java2d/ConfiguredFontCollectionTestCase.java b/fop-core/src/test/java/org/apache/fop/render/java2d/ConfiguredFontCollectionTestCase.java new file mode 100644 index 000000000..7e2f8d4f7 --- /dev/null +++ b/fop-core/src/test/java/org/apache/fop/render/java2d/ConfiguredFontCollectionTestCase.java @@ -0,0 +1,73 @@ +/* + * 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.render.java2d; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +import org.junit.Assert; +import org.junit.Test; + +import org.apache.commons.io.IOUtils; + +import org.apache.fop.apps.io.InternalResourceResolver; +import org.apache.fop.apps.io.ResourceResolverFactory; +import org.apache.fop.fonts.EmbedFontInfo; +import org.apache.fop.fonts.FontInfo; +import org.apache.fop.fonts.FontTriplet; +import org.apache.fop.fonts.FontUris; + +public class ConfiguredFontCollectionTestCase { + @Test + public void testConfiguredFontCollection() throws IOException { + File pfb = getFontFileNoExension("test/resources/fonts/type1/c0419bt_.pfb"); + File afm = getFontFileNoExension("test/resources/fonts/type1/c0419bt_.afm"); + Assert.assertFalse(pfb.getName().endsWith(".pfb")); + try { + FontUris fontUris = new FontUris(pfb.toURI(), null, afm.toURI(), null); + EmbedFontInfo e = new EmbedFontInfo(fontUris, true, true, new ArrayList(), null); + List x = Collections.singletonList(e); + InternalResourceResolver rr = + ResourceResolverFactory.createDefaultInternalResourceResolver(new File(".").toURI()); + ConfiguredFontCollection c = new ConfiguredFontCollection(rr, x, true); + FontInfo fi = new FontInfo(); + int num = c.setup(0, fi); + Assert.assertEquals(num, 1); + Assert.assertEquals(fi.getFonts().values().iterator().next().getFontName(), "Courier10PitchBT-Roman"); + } finally { + pfb.delete(); + afm.delete(); + } + } + + private File getFontFileNoExension(String s) throws IOException { + FileInputStream pfb = new FileInputStream(s); + File tmp = File.createTempFile("fop", "font"); + FileOutputStream os = new FileOutputStream(tmp); + IOUtils.copy(pfb, os); + os.close(); + pfb.close(); + return tmp; + } +} -- 2.39.5