Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

DefaultFontConfigurator.java 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 fontManager the font manager
  48. * @param listener the font event listener
  49. * @param strict true if an Exception should be thrown if an error is found.
  50. */
  51. public DefaultFontConfigurator(FontManager fontManager, FontEventListener listener, boolean strict) {
  52. this.fontManager = fontManager;
  53. this.resourceResolver = fontManager.getResourceResolver();
  54. this.listener = listener;
  55. this.strict = strict;
  56. }
  57. /**
  58. * Initializes font info settings from the user configuration
  59. * @throws FOPException if an exception occurs while processing the configuration
  60. */
  61. public List<EmbedFontInfo> configure(FontConfig fontInfoConfig) throws FOPException {
  62. List<EmbedFontInfo> fontInfoList = new ArrayList<EmbedFontInfo>();
  63. if (fontInfoConfig != null) {
  64. assert fontInfoConfig instanceof DefaultFontConfig;
  65. DefaultFontConfig adobeFontInfoConfig = (DefaultFontConfig) fontInfoConfig;
  66. long start = 0;
  67. if (log.isDebugEnabled()) {
  68. log.debug("Starting font configuration...");
  69. start = System.currentTimeMillis();
  70. }
  71. FontAdder fontAdder = new FontAdder(fontManager, resourceResolver, listener);
  72. // native o/s search (autodetect) configuration
  73. fontManager.autoDetectFonts(adobeFontInfoConfig.isAutoDetectFonts(), fontAdder, strict,
  74. listener, fontInfoList);
  75. // Add configured directories to FontInfo list
  76. addDirectories(adobeFontInfoConfig, fontAdder, fontInfoList);
  77. // Add configured fonts to FontInfo
  78. FontCache fontCache = fontManager.getFontCache();
  79. try {
  80. addFonts(adobeFontInfoConfig, fontCache, fontInfoList);
  81. } catch (URISyntaxException use) {
  82. LogUtil.handleException(log, use, strict);
  83. }
  84. // Update referenced fonts (fonts which are not to be embedded)
  85. fontManager.updateReferencedFonts(fontInfoList);
  86. // Renderer-specific referenced fonts
  87. List<String> referencedFonts = adobeFontInfoConfig.getReferencedFontFamily();
  88. if (referencedFonts.size() > 0) {
  89. FontTriplet.Matcher matcher = FontManagerConfigurator.createFontsMatcher(
  90. referencedFonts, strict);
  91. fontManager.updateReferencedFonts(fontInfoList, matcher);
  92. }
  93. // Update font cache if it has changed
  94. fontManager.saveCache();
  95. if (log.isDebugEnabled()) {
  96. log.debug("Finished font configuration in "
  97. + (System.currentTimeMillis() - start) + "ms");
  98. }
  99. }
  100. return Collections.unmodifiableList(fontInfoList);
  101. }
  102. private void addDirectories(DefaultFontConfig fontInfoConfig, FontAdder fontAdder,
  103. List<EmbedFontInfo> fontInfoList) throws FOPException {
  104. // directory (multiple font) configuration
  105. List<Directory> directories = fontInfoConfig.getDirectories();
  106. for (Directory directory : directories) {
  107. // add fonts found in directory
  108. FontFileFinder fontFileFinder = new FontFileFinder(directory.isRecursive() ? -1 : 1, listener);
  109. List<URL> fontURLList;
  110. try {
  111. fontURLList = fontFileFinder.find(directory.getDirectory());
  112. fontAdder.add(fontURLList, fontInfoList);
  113. } catch (IOException e) {
  114. LogUtil.handleException(log, e, strict);
  115. } catch (URISyntaxException use) {
  116. LogUtil.handleException(log, use, strict);
  117. }
  118. }
  119. }
  120. private void addFonts(DefaultFontConfig fontInfoConfig, FontCache fontCache,
  121. List<EmbedFontInfo> fontInfoList) throws FOPException, URISyntaxException {
  122. // font file (singular) configuration
  123. List<DefaultFontConfig.Font> fonts = fontInfoConfig.getFonts();
  124. for (DefaultFontConfig.Font font : fonts) {
  125. EmbedFontInfo embedFontInfo = getFontInfo(font, fontCache);
  126. if (embedFontInfo != null) {
  127. fontInfoList.add(embedFontInfo);
  128. }
  129. }
  130. }
  131. private EmbedFontInfo getFontInfo(DefaultFontConfig.Font font, FontCache fontCache)
  132. throws FOPException, URISyntaxException {
  133. String embed = font.getEmbedURI();
  134. String metrics = font.getMetrics();
  135. String subFont = font.getSubFont();
  136. URI metricsUri = metrics == null ? null : InternalResourceResolver.cleanURI(metrics);
  137. URI embedUri = InternalResourceResolver.cleanURI(embed);
  138. List<FontTriplet> tripletList = font.getTripletList();
  139. // no font triplet info
  140. if (tripletList.size() == 0) {
  141. URI fontUri = resourceResolver.resolveFromBase(embedUri);
  142. FontInfoFinder finder = new FontInfoFinder();
  143. finder.setEventListener(listener);
  144. EmbedFontInfo[] infos = finder.find(fontUri, resourceResolver, fontCache);
  145. return infos[0]; //When subFont is set, only one font is returned
  146. }
  147. EncodingMode encodingMode = EncodingMode.getValue(font.getEncodingMode());
  148. EmbeddingMode embeddingMode = EmbeddingMode.getValue(font.getEmbeddingMode());
  149. EmbedFontInfo embedFontInfo = new EmbedFontInfo(metricsUri, font.isKerning(),
  150. font.isAdvanced(), tripletList, embedUri, subFont, encodingMode, embeddingMode);
  151. if (fontCache != null) {
  152. if (!fontCache.containsFont(embedFontInfo)) {
  153. fontCache.addFont(embedFontInfo, resourceResolver);
  154. }
  155. }
  156. if (log.isDebugEnabled()) {
  157. URI embedFile = embedFontInfo.getEmbedURI();
  158. log.debug("Adding font " + (embedFile != null ? embedFile + ", " : "")
  159. + "metrics URI " + embedFontInfo.getMetricsURI());
  160. for (int j = 0; j < tripletList.size(); ++j) {
  161. FontTriplet triplet = tripletList.get(j);
  162. log.debug(" Font triplet "
  163. + triplet.getName() + ", "
  164. + triplet.getStyle() + ", "
  165. + triplet.getWeight());
  166. }
  167. }
  168. return embedFontInfo;
  169. }
  170. }