/* * 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 { /** 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 configure(FontConfig fontInfoConfig) throws FOPException { List fontInfoList = new ArrayList(); 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 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 fontInfoList) throws FOPException { // directory (multiple font) configuration List directories = fontInfoConfig.getDirectories(); for (Directory directory : directories) { // add fonts found in directory FontFileFinder fontFileFinder = new FontFileFinder(directory.isRecursive() ? -1 : 1, listener); List 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 fontInfoList) throws FOPException, URISyntaxException { // font file (singular) configuration List 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 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; } }