123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190 |
- /*
- * 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;
- }
- }
|