You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DefaultFontConfigurator.java 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /* $Id$ */
  18. package org.apache.fop.fonts;
  19. import java.io.IOException;
  20. import java.net.URI;
  21. import java.net.URISyntaxException;
  22. import java.net.URL;
  23. import java.util.ArrayList;
  24. import java.util.Collections;
  25. import java.util.List;
  26. import org.apache.commons.logging.Log;
  27. import org.apache.commons.logging.LogFactory;
  28. import org.apache.fop.apps.FOPException;
  29. import org.apache.fop.apps.io.InternalResourceResolver;
  30. import org.apache.fop.fonts.DefaultFontConfig.Directory;
  31. import org.apache.fop.fonts.autodetect.FontFileFinder;
  32. import org.apache.fop.fonts.autodetect.FontInfoFinder;
  33. import org.apache.fop.util.LogUtil;
  34. /**
  35. * The default configurator for fonts. This configurator can configure the more generic fonts used
  36. * by the renderers i.e. TTF, Type1 etc...
  37. */
  38. public class DefaultFontConfigurator implements FontConfigurator<EmbedFontInfo> {
  39. /** logger instance */
  40. protected static final Log log = LogFactory.getLog(DefaultFontConfigurator.class);
  41. private final FontManager fontManager;
  42. private final InternalResourceResolver resourceResolver;
  43. private final FontEventListener listener;
  44. private final boolean strict;
  45. /**
  46. * Main constructor
  47. * @param fontInfoConfig the configuration object
  48. * @param fontManager the font manager
  49. * @param listener the font event listener
  50. * @param strict true if an Exception should be thrown if an error is found.
  51. */
  52. public DefaultFontConfigurator(FontManager fontManager, FontEventListener listener, boolean strict) {
  53. this.fontManager = fontManager;
  54. this.resourceResolver = fontManager.getResourceResolver();
  55. this.listener = listener;
  56. this.strict = strict;
  57. }
  58. /**
  59. * Initializes font info settings from the user configuration
  60. * @throws FOPException if an exception occurs while processing the configuration
  61. */
  62. public List<EmbedFontInfo> configure(FontConfig fontInfoConfig) throws FOPException {
  63. List<EmbedFontInfo> fontInfoList = new ArrayList<EmbedFontInfo>();
  64. if (fontInfoConfig != null) {
  65. assert fontInfoConfig instanceof DefaultFontConfig;
  66. DefaultFontConfig adobeFontInfoConfig = (DefaultFontConfig) fontInfoConfig;
  67. long start = 0;
  68. if (log.isDebugEnabled()) {
  69. log.debug("Starting font configuration...");
  70. start = System.currentTimeMillis();
  71. }
  72. FontAdder fontAdder = new FontAdder(fontManager, resourceResolver, listener);
  73. // native o/s search (autodetect) configuration
  74. fontManager.autoDetectFonts(adobeFontInfoConfig.isAutoDetectFonts(), fontAdder, strict,
  75. listener, fontInfoList);
  76. // Add configured directories to FontInfo list
  77. addDirectories(adobeFontInfoConfig, fontAdder, fontInfoList);
  78. // Add configured fonts to FontInfo
  79. FontCache fontCache = fontManager.getFontCache();
  80. try {
  81. addFonts(adobeFontInfoConfig, fontCache, fontInfoList);
  82. } catch (URISyntaxException use) {
  83. LogUtil.handleException(log, use, strict);
  84. }
  85. // Update referenced fonts (fonts which are not to be embedded)
  86. fontManager.updateReferencedFonts(fontInfoList);
  87. // Renderer-specific referenced fonts
  88. List<String> referencedFonts = adobeFontInfoConfig.getReferencedFontFamily();
  89. if (referencedFonts.size() > 0) {
  90. FontTriplet.Matcher matcher = FontManagerConfigurator.createFontsMatcher(
  91. referencedFonts, strict);
  92. fontManager.updateReferencedFonts(fontInfoList, matcher);
  93. }
  94. // Update font cache if it has changed
  95. fontManager.saveCache();
  96. if (log.isDebugEnabled()) {
  97. log.debug("Finished font configuration in "
  98. + (System.currentTimeMillis() - start) + "ms");
  99. }
  100. }
  101. return Collections.unmodifiableList(fontInfoList);
  102. }
  103. private void addDirectories(DefaultFontConfig fontInfoConfig, FontAdder fontAdder,
  104. List<EmbedFontInfo> fontInfoList) throws FOPException {
  105. // directory (multiple font) configuration
  106. List<Directory> directories = fontInfoConfig.getDirectories();
  107. for (Directory directory : directories) {
  108. // add fonts found in directory
  109. FontFileFinder fontFileFinder = new FontFileFinder(directory.isRecursive() ? -1 : 1, listener);
  110. List<URL> fontURLList;
  111. try {
  112. fontURLList = fontFileFinder.find(directory.getDirectory());
  113. fontAdder.add(fontURLList, fontInfoList);
  114. } catch (IOException e) {
  115. LogUtil.handleException(log, e, strict);
  116. } catch (URISyntaxException use) {
  117. LogUtil.handleException(log, use, strict);
  118. }
  119. }
  120. }
  121. private void addFonts(DefaultFontConfig fontInfoConfig, FontCache fontCache,
  122. List<EmbedFontInfo> fontInfoList) throws FOPException, URISyntaxException {
  123. // font file (singular) configuration
  124. List<DefaultFontConfig.Font> fonts = fontInfoConfig.getFonts();
  125. for (DefaultFontConfig.Font font : fonts) {
  126. EmbedFontInfo embedFontInfo = getFontInfo(font, fontCache);
  127. if (embedFontInfo != null) {
  128. fontInfoList.add(embedFontInfo);
  129. }
  130. }
  131. }
  132. private EmbedFontInfo getFontInfo(DefaultFontConfig.Font font, FontCache fontCache)
  133. throws FOPException, URISyntaxException {
  134. String embed = font.getEmbedURI();
  135. String metrics = font.getMetrics();
  136. String subFont = font.getSubFont();
  137. URI metricsUri = metrics == null ? null : InternalResourceResolver.cleanURI(metrics);
  138. URI embedUri = InternalResourceResolver.cleanURI(embed);
  139. List<FontTriplet> tripletList = font.getTripletList();
  140. // no font triplet info
  141. if (tripletList.size() == 0) {
  142. URI fontUri = resourceResolver.resolveFromBase(embedUri);
  143. FontInfoFinder finder = new FontInfoFinder();
  144. finder.setEventListener(listener);
  145. EmbedFontInfo[] infos = finder.find(fontUri, resourceResolver, fontCache);
  146. return infos[0]; //When subFont is set, only one font is returned
  147. }
  148. EncodingMode encodingMode = EncodingMode.getValue(font.getEncodingMode());
  149. EmbeddingMode embeddingMode = EmbeddingMode.getValue(font.getEmbeddingMode());
  150. EmbedFontInfo embedFontInfo = new EmbedFontInfo(metricsUri, font.isKerning(),
  151. font.isAdvanced(), tripletList, embedUri, subFont, encodingMode, embeddingMode);
  152. if (fontCache != null) {
  153. if (!fontCache.containsFont(embedFontInfo)) {
  154. fontCache.addFont(embedFontInfo, resourceResolver);
  155. }
  156. }
  157. if (log.isDebugEnabled()) {
  158. URI embedFile = embedFontInfo.getEmbedURI();
  159. log.debug("Adding font " + (embedFile != null ? embedFile + ", " : "")
  160. + "metrics URI " + embedFontInfo.getMetricsURI());
  161. for (int j = 0; j < tripletList.size(); ++j) {
  162. FontTriplet triplet = tripletList.get(j);
  163. log.debug(" Font triplet "
  164. + triplet.getName() + ", "
  165. + triplet.getStyle() + ", "
  166. + triplet.getWeight());
  167. }
  168. }
  169. return embedFontInfo;
  170. }
  171. }