diff options
author | Mehdi Houshmand <mehdi@apache.org> | 2012-05-31 08:33:36 +0000 |
---|---|---|
committer | Mehdi Houshmand <mehdi@apache.org> | 2012-05-31 08:33:36 +0000 |
commit | eccd73c523bdda6a0634e9849141492f7b14ad63 (patch) | |
tree | 72f06ec1481249bdd639083ee646b3c3fd4be7a4 /src/java/org/apache/fop/fonts/DefaultFontConfigurator.java | |
parent | 05761b1df54ada8a762bfa879dc0e3455d33d828 (diff) | |
download | xmlgraphics-fop-eccd73c523bdda6a0634e9849141492f7b14ad63.tar.gz xmlgraphics-fop-eccd73c523bdda6a0634e9849141492f7b14ad63.zip |
Started unifying URI resolution mechanism, redesigned configuration system and created flexible config testing
git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/branches/Temp_URI_Unification@1344594 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/java/org/apache/fop/fonts/DefaultFontConfigurator.java')
-rw-r--r-- | src/java/org/apache/fop/fonts/DefaultFontConfigurator.java | 190 |
1 files changed, 190 insertions, 0 deletions
diff --git a/src/java/org/apache/fop/fonts/DefaultFontConfigurator.java b/src/java/org/apache/fop/fonts/DefaultFontConfigurator.java new file mode 100644 index 000000000..88c056d31 --- /dev/null +++ b/src/java/org/apache/fop/fonts/DefaultFontConfigurator.java @@ -0,0 +1,190 @@ +/* + * 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.IOException; +import java.net.URI; +import java.net.URISyntaxException; +import java.net.URL; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import org.apache.fop.apps.FOPException; +import org.apache.fop.apps.io.URIResolverWrapper; +import org.apache.fop.fonts.DefaultFontConfig.Directory; +import org.apache.fop.fonts.autodetect.FontFileFinder; +import org.apache.fop.fonts.autodetect.FontInfoFinder; +import org.apache.fop.util.LogUtil; + +/** + * The default configurator for fonts. This configurator can configure the more generic fonts used + * by the renderers i.e. TTF, Type1 etc... + */ +public class DefaultFontConfigurator implements FontConfigurator<EmbedFontInfo> { + /** logger instance */ + protected static Log log = LogFactory.getLog(DefaultFontConfigurator.class); + + private final FontManager fontManager; + private final URIResolverWrapper uriResolver; + private final FontEventListener listener; + private final boolean strict; + + /** + * Main constructor + * @param fontInfoConfig the configuration object + * @param fontManager the font manager + * @param listener the font event listener + * @param strict true if an Exception should be thrown if an error is found. + */ + public DefaultFontConfigurator(FontManager fontManager, FontEventListener listener, boolean strict) { + this.fontManager = fontManager; + this.uriResolver = fontManager.getURIResolver(); + this.listener = listener; + this.strict = strict; + } + + /** + * Initializes font info settings from the user configuration + * @throws FOPException if an exception occurs while processing the configuration + */ + public List<EmbedFontInfo> configure(FontConfig fontInfoConfig) + throws FOPException { + List<EmbedFontInfo> fontInfoList = new ArrayList<EmbedFontInfo>(); + DefaultFontConfig adobeFontInfoConfig = (DefaultFontConfig) fontInfoConfig; + if (adobeFontInfoConfig != null) { + long start = 0; + if (log.isDebugEnabled()) { + log.debug("Starting font configuration..."); + start = System.currentTimeMillis(); + } + FontAdder fontAdder = new FontAdder(fontManager, uriResolver, listener); + // native o/s search (autodetect) configuration + fontManager.autoDetectFonts(adobeFontInfoConfig.isAutoDetectFonts(), fontAdder, strict, + listener, fontInfoList); + // Add configured directories to FontInfo list + addDirectories(adobeFontInfoConfig, fontAdder, fontInfoList); + // Add configured fonts to FontInfo + FontCache fontCache = fontManager.getFontCache(); + try { + addFonts(adobeFontInfoConfig, fontCache, fontInfoList); + } catch (URISyntaxException use) { + LogUtil.handleException(log, use, strict); + } + // Update referenced fonts (fonts which are not to be embedded) + fontManager.updateReferencedFonts(fontInfoList); + // Renderer-specific referenced fonts + List<String> referencedFonts = adobeFontInfoConfig.getReferencedFontFamily(); + if (referencedFonts.size() > 0) { + FontTriplet.Matcher matcher = FontManagerConfigurator.createFontsMatcher( + referencedFonts, strict); + fontManager.updateReferencedFonts(fontInfoList, matcher); + } + // Update font cache if it has changed + fontManager.saveCache(); + if (log.isDebugEnabled()) { + log.debug("Finished font configuration in " + + (System.currentTimeMillis() - start) + "ms"); + } + } + return Collections.unmodifiableList(fontInfoList); + } + + private void addDirectories(DefaultFontConfig fontInfoConfig, FontAdder fontAdder, + List<EmbedFontInfo> fontInfoList) throws FOPException { + // directory (multiple font) configuration + List<Directory> directories = fontInfoConfig.getDirectories(); + for (Directory directory : directories) { + // add fonts found in directory + FontFileFinder fontFileFinder = new FontFileFinder(directory.isRecursive() ? -1 : 1, listener); + List<URL> fontURLList; + try { + fontURLList = fontFileFinder.find(directory.getDirectory()); + fontAdder.add(fontURLList, fontInfoList); + } catch (IOException e) { + LogUtil.handleException(log, e, strict); + } catch (URISyntaxException use) { + LogUtil.handleException(log, use, strict); + } + } + } + + private void addFonts(DefaultFontConfig fontInfoConfig, FontCache fontCache, + List<EmbedFontInfo> fontInfoList) throws FOPException, URISyntaxException { + // font file (singular) configuration + List<DefaultFontConfig.Font> fonts = fontInfoConfig.getFonts(); + for (DefaultFontConfig.Font font : fonts) { + EmbedFontInfo embedFontInfo = getFontInfo(font, fontCache); + if (embedFontInfo != null) { + fontInfoList.add(embedFontInfo); + } + } + } + + private EmbedFontInfo getFontInfo(DefaultFontConfig.Font font, FontCache fontCache) + throws FOPException, URISyntaxException { + String embed = font.getEmbedURI(); + String metrics = font.getMetrics(); + String subFont = font.getSubFont(); + URI metricsUri = metrics == null ? null : URIResolverWrapper.cleanURI(metrics); + URI embedUri = URIResolverWrapper.cleanURI(embed); + + List<FontTriplet> tripletList = font.getTripletList(); + + // no font triplet info + if (tripletList.size() == 0) { + //TODO: could be problematic!! + URI fontUri = uriResolver.getBaseURI().resolve(embedUri); + if (fontUri != null) { + FontInfoFinder finder = new FontInfoFinder(); + finder.setEventListener(listener); + EmbedFontInfo[] infos = finder.find(fontUri, uriResolver, fontCache); + return infos[0]; //When subFont is set, only one font is returned + } else { + return null; + } + } + EncodingMode encodingMode = EncodingMode.getEncodingMode(font.getEncodingMode()); + EmbedFontInfo embedFontInfo = new EmbedFontInfo(metricsUri, font.isKerning(), + font.isAdvanced(), tripletList, embedUri, subFont, encodingMode); + if (fontCache != null) { + if (!fontCache.containsFont(embedFontInfo)) { + fontCache.addFont(embedFontInfo, uriResolver); + } + } + + if (log.isDebugEnabled()) { + URI embedFile = embedFontInfo.getEmbedURI(); + log.debug("Adding font " + (embedFile != null ? embedFile + ", " : "") + + "metrics URI " + embedFontInfo.getMetricsURI()); + for (int j = 0; j < tripletList.size(); ++j) { + FontTriplet triplet = tripletList.get(j); + log.debug(" Font triplet " + + triplet.getName() + ", " + + triplet.getStyle() + ", " + + triplet.getWeight()); + } + } + return embedFontInfo; + } +} |