diff options
author | Simon Pepping <spepping@apache.org> | 2010-11-26 18:37:40 +0000 |
---|---|---|
committer | Simon Pepping <spepping@apache.org> | 2010-11-26 18:37:40 +0000 |
commit | 347387b317df9ea899e96955232d5ccba3d355fd (patch) | |
tree | 2630de4552e7da01dcd89badc16f6330b52ff96b | |
parent | dbe01e0fa2bf8916be0d5d5f9a9bd83b2a726462 (diff) | |
download | xmlgraphics-fop-347387b317df9ea899e96955232d5ccba3d355fd.tar.gz xmlgraphics-fop-347387b317df9ea899e96955232d5ccba3d355fd.zip |
Second part of patch 50245 by Mehdi Houshmand
git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@1039502 13f79535-47bb-0310-9956-ffa450edef68
10 files changed, 115 insertions, 86 deletions
diff --git a/src/java/org/apache/fop/fonts/FontAdder.java b/src/java/org/apache/fop/fonts/FontAdder.java index 884a874a8..c5e2b8b9f 100644 --- a/src/java/org/apache/fop/fonts/FontAdder.java +++ b/src/java/org/apache/fop/fonts/FontAdder.java @@ -20,7 +20,6 @@ package org.apache.fop.fonts; import java.net.URL; -import java.util.Iterator; import java.util.List; import org.apache.fop.fonts.autodetect.FontInfoFinder; @@ -29,9 +28,9 @@ import org.apache.fop.fonts.autodetect.FontInfoFinder; * Adds a list of fonts to a given font info list */ public class FontAdder { - private FontEventListener listener; - private FontResolver resolver; - private FontManager manager; + private final FontEventListener listener; + private final FontResolver resolver; + private final FontManager manager; /** * Main constructor @@ -50,14 +49,13 @@ public class FontAdder { * @param fontURLList font file list * @param fontInfoList a configured font info list */ - public void add(List/*<URL>*/ fontURLList, List/*<EmbedFontInfo>*/ fontInfoList) { + public void add(List<URL> fontURLList, List<EmbedFontInfo> fontInfoList) { FontCache cache = manager.getFontCache(); FontInfoFinder finder = new FontInfoFinder(); finder.setEventListener(listener); - for (Iterator iter = fontURLList.iterator(); iter.hasNext();) { - URL fontUrl = (URL)iter.next(); - EmbedFontInfo[] embedFontInfos = finder.find(fontUrl, resolver, cache); + for (URL fontURL : fontURLList) { + EmbedFontInfo[] embedFontInfos = finder.find(fontURL, resolver, cache); if (embedFontInfos == null) { continue; } diff --git a/src/java/org/apache/fop/fonts/FontDetector.java b/src/java/org/apache/fop/fonts/FontDetector.java index d69dfc9bd..88e4c6e69 100644 --- a/src/java/org/apache/fop/fonts/FontDetector.java +++ b/src/java/org/apache/fop/fonts/FontDetector.java @@ -42,9 +42,9 @@ public class FontDetector { "application/x-font", "application/x-font-truetype" }; - private FontManager fontManager; - private FontAdder fontAdder; - private boolean strict; + private final FontManager fontManager; + private final FontAdder fontAdder; + private final boolean strict; /** * Main constructor @@ -72,8 +72,7 @@ public class FontDetector { try { File fontBase = FileUtils.toFile(new URL(fontBaseURL)); if (fontBase != null) { - List/*<URL>*/ fontURLList = fontFileFinder.find( - fontBase.getAbsolutePath()); + List<URL> fontURLList = fontFileFinder.find(fontBase.getAbsolutePath()); fontAdder.add(fontURLList, fontInfoList); //Can only use the font base URL if it's a file URL @@ -84,7 +83,7 @@ public class FontDetector { } // native o/s font directory finding - List/*<URL>*/ systemFontList; + List<URL> systemFontList; try { systemFontList = fontFileFinder.find(); fontAdder.add(systemFontList, fontInfoList); diff --git a/src/java/org/apache/fop/fonts/FontInfoConfigurator.java b/src/java/org/apache/fop/fonts/FontInfoConfigurator.java index adbcd1260..67bb2e295 100644 --- a/src/java/org/apache/fop/fonts/FontInfoConfigurator.java +++ b/src/java/org/apache/fop/fonts/FontInfoConfigurator.java @@ -140,7 +140,7 @@ public class FontInfoConfigurator { // add fonts found in directory FontFileFinder fontFileFinder = new FontFileFinder(recursive ? -1 : 1); - List/*<URL>*/ fontURLList; + List<URL> fontURLList; try { fontURLList = fontFileFinder.find(directory); fontAdder.add(fontURLList, fontInfoList); @@ -228,25 +228,23 @@ public class FontInfoConfigurator { LogUtil.handleError(log, "font without font-triplet", strict); File fontFile = FontCache.getFileFromUrls(new String[] {embedUrl, metricsUrl}); - URL fontUrl; + URL fontURL = null; try { - fontUrl = fontFile.toURI().toURL(); + fontURL = fontFile.toURI().toURL(); } catch (MalformedURLException e) { - // Should never happen - log.debug("Malformed Url: " + e.getMessage()); - return null; + LogUtil.handleException(log, e, strict); } if (fontFile != null) { FontInfoFinder finder = new FontInfoFinder(); finder.setEventListener(listener); - EmbedFontInfo[] infos = finder.find(fontUrl, fontResolver, fontCache); + EmbedFontInfo[] infos = finder.find(fontURL, fontResolver, fontCache); return infos[0]; //When subFont is set, only one font is returned } else { return null; } } - List/*<FontTriplet>*/ tripletList = new java.util.ArrayList/*<FontTriplet>*/(); + List<FontTriplet> tripletList = new java.util.ArrayList<FontTriplet>(); for (int j = 0; j < tripletCfg.length; j++) { FontTriplet fontTriplet = getFontTriplet(tripletCfg[j]); tripletList.add(fontTriplet); @@ -269,7 +267,7 @@ public class FontInfoConfigurator { log.debug("Adding font " + (embedFile != null ? embedFile + ", " : "") + "metric file " + embedFontInfo.getMetricsFile()); for (int j = 0; j < tripletList.size(); ++j) { - FontTriplet triplet = (FontTriplet) tripletList.get(j); + FontTriplet triplet = tripletList.get(j); log.debug(" Font triplet " + triplet.getName() + ", " + triplet.getStyle() + ", " diff --git a/src/java/org/apache/fop/fonts/autodetect/FontDirFinder.java b/src/java/org/apache/fop/fonts/autodetect/FontDirFinder.java new file mode 100644 index 000000000..383c5283d --- /dev/null +++ b/src/java/org/apache/fop/fonts/autodetect/FontDirFinder.java @@ -0,0 +1,41 @@ +/* + * 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.autodetect; + +import java.io.File; +import java.io.IOException; +import java.util.List; + +/** + * Implementers provide find method for searching native operating system + * for available fonts. + */ +public interface FontDirFinder { + + /** + * Finds a list of font files. + * + * @return list of font files. + * @throws IOException + * In case of an I/O problem + */ + List<File> find() throws IOException; + +} diff --git a/src/java/org/apache/fop/fonts/autodetect/FontFileFinder.java b/src/java/org/apache/fop/fonts/autodetect/FontFileFinder.java index bd0f1ee14..9a0a73e2f 100644 --- a/src/java/org/apache/fop/fonts/autodetect/FontFileFinder.java +++ b/src/java/org/apache/fop/fonts/autodetect/FontFileFinder.java @@ -22,8 +22,8 @@ package org.apache.fop.fonts.autodetect; import java.io.File; import java.io.IOException; import java.net.MalformedURLException; +import java.net.URL; import java.util.Collection; -import java.util.Iterator; import java.util.List; import org.apache.commons.io.DirectoryWalker; @@ -91,6 +91,7 @@ public class FontFileFinder extends DirectoryWalker implements FontFinder { * @return whether directory should be handled * {@inheritDoc} */ + @Override protected boolean handleDirectory(File directory, int depth, Collection results) { return true; } @@ -101,6 +102,7 @@ public class FontFileFinder extends DirectoryWalker implements FontFinder { * @param results collection * {@inheritDoc} */ + @Override protected void handleFile(File file, int depth, Collection results) { try { // Looks Strange, but is actually recommended over just .URL() @@ -116,6 +118,7 @@ public class FontFileFinder extends DirectoryWalker implements FontFinder { * @param results the collection of results objects * {@inheritDoc} */ + @Override protected void handleDirectoryEnd(File directory, int depth, Collection results) { if (log.isDebugEnabled()) { log.debug(directory + ": found " + results.size() + " font" @@ -130,8 +133,8 @@ public class FontFileFinder extends DirectoryWalker implements FontFinder { * @throws IOException io exception * {@inheritDoc} */ - public List/*<URL>*/ find() throws IOException { - final FontFinder fontDirFinder; + public List<URL> find() throws IOException { + final FontDirFinder fontDirFinder; final String osName = System.getProperty("os.name"); if (osName.startsWith("Windows")) { fontDirFinder = new WindowsFontDirFinder(); @@ -142,10 +145,9 @@ public class FontFileFinder extends DirectoryWalker implements FontFinder { fontDirFinder = new UnixFontDirFinder(); } } - List/*<URL>*/ fontDirs = fontDirFinder.find(); - List/*<URL>*/ results = new java.util.ArrayList/*<URL>*/(); - for (Iterator iter = fontDirs.iterator(); iter.hasNext();) { - final File dir = (File)iter.next(); + List<File> fontDirs = fontDirFinder.find(); + List<URL> results = new java.util.ArrayList<URL>(); + for (File dir : fontDirs) { super.walk(dir, results); } return results; @@ -158,8 +160,8 @@ public class FontFileFinder extends DirectoryWalker implements FontFinder { * @return list of font files * @throws IOException thrown if an I/O exception of some sort has occurred */ - public List find(String dir) throws IOException { - List results = new java.util.ArrayList(); + public List<URL> find(String dir) throws IOException { + List<URL> results = new java.util.ArrayList<URL>(); super.walk(new File(dir), results); return results; } diff --git a/src/java/org/apache/fop/fonts/autodetect/FontFinder.java b/src/java/org/apache/fop/fonts/autodetect/FontFinder.java index ee0d1e07f..51e79443a 100644 --- a/src/java/org/apache/fop/fonts/autodetect/FontFinder.java +++ b/src/java/org/apache/fop/fonts/autodetect/FontFinder.java @@ -20,6 +20,7 @@ package org.apache.fop.fonts.autodetect; import java.io.IOException; +import java.net.URL; import java.util.List; /** @@ -31,12 +32,10 @@ public interface FontFinder { /** * Finds a list of font files. * - * @return list of font files. List<URL> in the case of the - * FontFinder, and List<File> in the case of the - * FonrDirFinders. + * @return list of font files. * @throws IOException * In case of an I/O problem */ - List find() throws IOException; + List<URL> find() throws IOException; } diff --git a/src/java/org/apache/fop/fonts/autodetect/FontInfoFinder.java b/src/java/org/apache/fop/fonts/autodetect/FontInfoFinder.java index 9f0fa9fd6..714af0e57 100644 --- a/src/java/org/apache/fop/fonts/autodetect/FontInfoFinder.java +++ b/src/java/org/apache/fop/fonts/autodetect/FontInfoFinder.java @@ -22,7 +22,6 @@ package org.apache.fop.fonts.autodetect; import java.io.InputStream; import java.net.URL; import java.util.Collection; -import java.util.Iterator; import java.util.List; import java.util.Set; import java.util.regex.Pattern; @@ -30,7 +29,6 @@ import java.util.regex.Pattern; import org.apache.commons.io.IOUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; - import org.apache.fop.fonts.CustomFont; import org.apache.fop.fonts.EmbedFontInfo; import org.apache.fop.fonts.EncodingMode; @@ -52,7 +50,7 @@ import org.apache.fop.fonts.truetype.TTFFontLoader; public class FontInfoFinder { /** logging instance */ - private Log log = LogFactory.getLog(FontInfoFinder.class); + private final Log log = LogFactory.getLog(FontInfoFinder.class); private FontEventListener eventListener; @@ -72,7 +70,7 @@ public class FontInfoFinder { * @param customFont CustomFont * @param triplets Collection that will take the generated triplets */ - private void generateTripletsFromFont(CustomFont customFont, Collection triplets) { + private void generateTripletsFromFont(CustomFont customFont, Collection<FontTriplet> triplets) { if (log.isTraceEnabled()) { log.trace("Font: " + customFont.getFullName() + ", family: " + customFont.getFamilyNames() @@ -99,10 +97,9 @@ public class FontInfoFinder { if (!fullName.equals(strippedName)) { triplets.add(new FontTriplet(strippedName, Font.STYLE_NORMAL, Font.WEIGHT_NORMAL)); } - Set familyNames = customFont.getFamilyNames(); - Iterator iter = familyNames.iterator(); - while (iter.hasNext()) { - String familyName = stripQuotes((String)iter.next()); + Set<String> familyNames = customFont.getFamilyNames(); + for (String familyName : familyNames) { + familyName = stripQuotes(familyName); if (!fullName.equals(familyName)) { /* Heuristic: * The more similar the family name to the full font name, @@ -135,17 +132,17 @@ public class FontInfoFinder { /** * Attempts to determine FontInfo from a given custom font - * @param fontUrl the font URL + * @param fontURL the font URL * @param customFont the custom font * @param fontCache font cache (may be null) * @return FontInfo from the given custom font */ private EmbedFontInfo getFontInfoFromCustomFont( - URL fontUrl, CustomFont customFont, FontCache fontCache) { - List fontTripletList = new java.util.ArrayList(); + URL fontURL, CustomFont customFont, FontCache fontCache) { + List<FontTriplet> fontTripletList = new java.util.ArrayList<FontTriplet>(); generateTripletsFromFont(customFont, fontTripletList); String embedUrl; - embedUrl = fontUrl.toExternalForm(); + embedUrl = fontURL.toExternalForm(); String subFontName = null; if (customFont instanceof MultiByteFont) { subFontName = ((MultiByteFont)customFont).getTTCName(); @@ -162,29 +159,29 @@ public class FontInfoFinder { /** * Attempts to determine EmbedFontInfo from a given font file. * - * @param fontUrl font URL. Assumed to be local. + * @param fontURL font URL. Assumed to be local. * @param resolver font resolver used to resolve font * @param fontCache font cache (may be null) * @return an array of newly created embed font info. Generally, this array * will have only one entry, unless the fontUrl is a TrueType Collection */ - public EmbedFontInfo[] find(URL fontUrl, FontResolver resolver, FontCache fontCache) { - String embedUrl = null; - embedUrl = fontUrl.toExternalForm(); + public EmbedFontInfo[] find(URL fontURL, FontResolver resolver, FontCache fontCache) { + String embedURL = null; + embedURL = fontURL.toExternalForm(); long fileLastModified = -1; if (fontCache != null) { - fileLastModified = FontCache.getLastModified(fontUrl); + fileLastModified = FontCache.getLastModified(fontURL); // firstly try and fetch it from cache before loading/parsing the font file - if (fontCache.containsFont(embedUrl)) { - EmbedFontInfo[] fontInfos = fontCache.getFontInfos(embedUrl, fileLastModified); + if (fontCache.containsFont(embedURL)) { + EmbedFontInfo[] fontInfos = fontCache.getFontInfos(embedURL, fileLastModified); if (fontInfos != null) { return fontInfos; } // is this a previously failed parsed font? - } else if (fontCache.isFailedFont(embedUrl, fileLastModified)) { + } else if (fontCache.isFailedFont(embedURL, fileLastModified)) { if (log.isDebugEnabled()) { - log.debug("Skipping font file that failed to load previously: " + embedUrl); + log.debug("Skipping font file that failed to load previously: " + embedURL); } return null; } @@ -193,77 +190,72 @@ public class FontInfoFinder { // try to determine triplet information from font file CustomFont customFont = null; - if (fontUrl.toExternalForm().endsWith(".ttc")) { + if (fontURL.toExternalForm().endsWith(".ttc")) { // Get a list of the TTC Font names - List ttcNames = null; //List<String> - String fontFileURI = fontUrl.toExternalForm().trim(); + List<String> ttcNames = null; + String fontFileURL = fontURL.toExternalForm().trim(); InputStream in = null; try { - in = FontLoader.openFontUri(resolver, fontFileURI); + in = FontLoader.openFontUri(resolver, fontFileURL); TTFFile ttf = new TTFFile(); FontFileReader reader = new FontFileReader(in); ttcNames = ttf.getTTCnames(reader); } catch (Exception e) { if (this.eventListener != null) { - this.eventListener.fontLoadingErrorAtAutoDetection(this, fontFileURI, e); + this.eventListener.fontLoadingErrorAtAutoDetection(this, fontFileURL, e); } return null; } finally { IOUtils.closeQuietly(in); } - List/*<EmbedFontInfo>*/ embedFontInfoList - = new java.util.ArrayList/*<EmbedFontInfo>*/(); + List<EmbedFontInfo> embedFontInfoList = new java.util.ArrayList<EmbedFontInfo>(); // For each font name ... - //for (String fontName : ttcNames) { - Iterator ttcNamesIterator = ttcNames.iterator(); - while (ttcNamesIterator.hasNext()) { - String fontName = (String)ttcNamesIterator.next(); - + for (String fontName : ttcNames) { if (log.isDebugEnabled()) { log.debug("Loading " + fontName); } try { TTFFontLoader ttfLoader = new TTFFontLoader( - fontFileURI, fontName, true, EncodingMode.AUTO, true, resolver); + fontFileURL, fontName, true, EncodingMode.AUTO, true, resolver); customFont = ttfLoader.getFont(); if (this.eventListener != null) { customFont.setEventListener(this.eventListener); } } catch (Exception e) { if (fontCache != null) { - fontCache.registerFailedFont(embedUrl, fileLastModified); + fontCache.registerFailedFont(embedURL, fileLastModified); } if (this.eventListener != null) { - this.eventListener.fontLoadingErrorAtAutoDetection(this, embedUrl, e); + this.eventListener.fontLoadingErrorAtAutoDetection(this, embedURL, e); } continue; } - EmbedFontInfo fi = getFontInfoFromCustomFont(fontUrl, customFont, fontCache); + EmbedFontInfo fi = getFontInfoFromCustomFont(fontURL, customFont, fontCache); if (fi != null) { embedFontInfoList.add(fi); } } - return (EmbedFontInfo[])embedFontInfoList.toArray( + return embedFontInfoList.toArray( new EmbedFontInfo[embedFontInfoList.size()]); } else { // The normal case try { - customFont = FontLoader.loadFont(fontUrl, null, true, EncodingMode.AUTO, resolver); + customFont = FontLoader.loadFont(fontURL, null, true, EncodingMode.AUTO, resolver); if (this.eventListener != null) { customFont.setEventListener(this.eventListener); } } catch (Exception e) { if (fontCache != null) { - fontCache.registerFailedFont(embedUrl, fileLastModified); + fontCache.registerFailedFont(embedURL, fileLastModified); } if (this.eventListener != null) { - this.eventListener.fontLoadingErrorAtAutoDetection(this, embedUrl, e); + this.eventListener.fontLoadingErrorAtAutoDetection(this, embedURL, e); } return null; } - EmbedFontInfo fi = getFontInfoFromCustomFont(fontUrl, customFont, fontCache); + EmbedFontInfo fi = getFontInfoFromCustomFont(fontURL, customFont, fontCache); if (fi != null) { return new EmbedFontInfo[] {fi}; } else { diff --git a/src/java/org/apache/fop/fonts/autodetect/NativeFontDirFinder.java b/src/java/org/apache/fop/fonts/autodetect/NativeFontDirFinder.java index 4072257b4..9f723a308 100644 --- a/src/java/org/apache/fop/fonts/autodetect/NativeFontDirFinder.java +++ b/src/java/org/apache/fop/fonts/autodetect/NativeFontDirFinder.java @@ -25,15 +25,15 @@ import java.util.List; /** * Native font finder base class */ -public abstract class NativeFontDirFinder implements FontFinder { +public abstract class NativeFontDirFinder implements FontDirFinder { /** * Generic method used by Mac and Unix font finders. * @return list of natively existing font directories * {@inheritDoc} */ - public List find() { - List fontDirList = new java.util.ArrayList(); + public List<File> find() { + List<File> fontDirList = new java.util.ArrayList<File>(); String[] searchableDirectories = getSearchableDirectories(); if (searchableDirectories != null) { for (int i = 0; i < searchableDirectories.length; i++) { diff --git a/src/java/org/apache/fop/fonts/autodetect/WindowsFontDirFinder.java b/src/java/org/apache/fop/fonts/autodetect/WindowsFontDirFinder.java index 2e07229f6..d9773a026 100644 --- a/src/java/org/apache/fop/fonts/autodetect/WindowsFontDirFinder.java +++ b/src/java/org/apache/fop/fonts/autodetect/WindowsFontDirFinder.java @@ -28,7 +28,7 @@ import java.util.List; /** * FontFinder for native Windows platforms */ -public class WindowsFontDirFinder implements FontFinder { +public class WindowsFontDirFinder implements FontDirFinder { /** * Attempts to read windir environment variable on windows @@ -51,8 +51,8 @@ public class WindowsFontDirFinder implements FontFinder { * {@inheritDoc} * @return a list of detected font files */ - public List find() { - List fontDirList = new java.util.ArrayList(); + public List<File> find() { + List<File> fontDirList = new java.util.ArrayList<File>(); String windir = null; try { windir = System.getProperty("env.windir"); diff --git a/src/java/org/apache/fop/fonts/truetype/TTFFile.java b/src/java/org/apache/fop/fonts/truetype/TTFFile.java index 11386299e..f4806d95e 100644 --- a/src/java/org/apache/fop/fonts/truetype/TTFFile.java +++ b/src/java/org/apache/fop/fonts/truetype/TTFFile.java @@ -1568,8 +1568,8 @@ public class TTFFile { * @return True if not collection or font name present, false otherwise * @throws IOException In case of an I/O problem */ - public final List getTTCnames(FontFileReader in) throws IOException { - List fontNames = new java.util.ArrayList(); + public final List<String> getTTCnames(FontFileReader in) throws IOException { + List<String> fontNames = new java.util.ArrayList<String>(); String tag = in.readTTFString(4); |