Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

FontManager.java 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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.net.URI;
  20. import java.util.List;
  21. import org.apache.fop.apps.FOPException;
  22. import org.apache.fop.apps.io.InternalResourceResolver;
  23. import org.apache.fop.fonts.FontTriplet.Matcher;
  24. import org.apache.fop.fonts.substitute.FontSubstitutions;
  25. // TODO: Refactor fonts package so major font activities (autodetection etc)
  26. // are all centrally managed and delegated from this class
  27. /**
  28. * The manager of fonts. The class holds a reference to the font cache and information about
  29. * font substitution, referenced fonts and similar.
  30. */
  31. public class FontManager {
  32. /** The resource resolver */
  33. private InternalResourceResolver resourceResolver;
  34. private final FontDetector fontDetector;
  35. private FontCacheManager fontCacheManager;
  36. /** Font substitutions */
  37. private FontSubstitutions fontSubstitutions = null;
  38. /** Allows enabling kerning on the base 14 fonts, default is false */
  39. private boolean enableBase14Kerning = false;
  40. /** FontTriplet matcher for fonts that shall be referenced rather than embedded. */
  41. private FontTriplet.Matcher referencedFontsMatcher;
  42. /**
  43. * Main constructor
  44. *
  45. * @param resourceResolver the URI resolver
  46. * @param fontDetector the font detector
  47. * @param fontCacheManager the font cache manager
  48. */
  49. public FontManager(InternalResourceResolver resourceResolver, FontDetector fontDetector,
  50. FontCacheManager fontCacheManager) {
  51. this.resourceResolver = resourceResolver;
  52. this.fontDetector = fontDetector;
  53. this.fontCacheManager = fontCacheManager;
  54. }
  55. /**
  56. * Sets the font resource resolver
  57. * @param resourceResolver resource resolver
  58. */
  59. public void setResourceResolver(InternalResourceResolver resourceResolver) {
  60. this.resourceResolver = resourceResolver;
  61. }
  62. public InternalResourceResolver getResourceResolver() {
  63. return this.resourceResolver;
  64. }
  65. /** @return true if kerning on base 14 fonts is enabled */
  66. public boolean isBase14KerningEnabled() {
  67. return this.enableBase14Kerning;
  68. }
  69. /**
  70. * Controls whether kerning is activated on base 14 fonts.
  71. * @param value true if kerning should be activated
  72. */
  73. public void setBase14KerningEnabled(boolean value) {
  74. this.enableBase14Kerning = value;
  75. }
  76. /**
  77. * Sets the font substitutions
  78. * @param substitutions font substitutions
  79. */
  80. public void setFontSubstitutions(FontSubstitutions substitutions) {
  81. this.fontSubstitutions = substitutions;
  82. }
  83. /**
  84. * Returns the font substitution catalog
  85. * @return the font substitution catalog
  86. */
  87. protected FontSubstitutions getFontSubstitutions() {
  88. if (fontSubstitutions == null) {
  89. this.fontSubstitutions = new FontSubstitutions();
  90. }
  91. return fontSubstitutions;
  92. }
  93. /**
  94. * Sets the font cache file
  95. * @param cacheFileURI the URI of the font cache file
  96. */
  97. public void setCacheFile(URI cacheFileURI) {
  98. fontCacheManager.setCacheFile(resourceResolver.resolveFromBase(cacheFileURI));
  99. }
  100. /**
  101. * Whether or not to cache results of font triplet detection/auto-config
  102. */
  103. public void disableFontCache() {
  104. fontCacheManager = FontCacheManagerFactory.createDisabled();
  105. }
  106. /**
  107. * Returns the font cache instance used by this font manager.
  108. * @return the font cache
  109. */
  110. public FontCache getFontCache() {
  111. return fontCacheManager.load();
  112. }
  113. /**
  114. * Saves the FontCache as necessary
  115. *
  116. * @throws FOPException fop exception
  117. */
  118. public void saveCache() throws FOPException {
  119. fontCacheManager.save();
  120. }
  121. /**
  122. * Deletes the current FontCache file
  123. * @throws FOPException if an error was thrown while deleting the cache
  124. */
  125. public void deleteCache() throws FOPException {
  126. fontCacheManager.delete();
  127. }
  128. /**
  129. * Sets up the fonts on a given FontInfo object. The fonts to setup are defined by an
  130. * array of {@link FontCollection} objects.
  131. * @param fontInfo the FontInfo object to set up
  132. * @param fontCollections the array of font collections/sources
  133. */
  134. public void setup(FontInfo fontInfo, FontCollection[] fontCollections) {
  135. int startNum = 1;
  136. for (int i = 0, c = fontCollections.length; i < c; i++) {
  137. startNum = fontCollections[i].setup(startNum, fontInfo);
  138. }
  139. // Make any defined substitutions in the font info
  140. getFontSubstitutions().adjustFontInfo(fontInfo);
  141. }
  142. /**
  143. * Sets the {@link FontTriplet.Matcher} that can be used to identify the fonts that shall
  144. * be referenced rather than embedded.
  145. * @param matcher the font triplet matcher
  146. */
  147. public void setReferencedFontsMatcher(FontTriplet.Matcher matcher) {
  148. this.referencedFontsMatcher = matcher;
  149. }
  150. /**
  151. * Gets the {@link FontTriplet.Matcher} that can be used to identify the fonts that shall
  152. * be referenced rather than embedded.
  153. * @return the font triplet matcher (or null if none is set)
  154. */
  155. public Matcher getReferencedFontsMatcher() {
  156. return this.referencedFontsMatcher;
  157. }
  158. /**
  159. * Updates the referenced font list using the FontManager's referenced fonts matcher
  160. * ({@link #getReferencedFontsMatcher()}).
  161. * @param fontInfoList a font info list
  162. */
  163. public void updateReferencedFonts(List<EmbedFontInfo> fontInfoList) {
  164. Matcher matcher = getReferencedFontsMatcher();
  165. updateReferencedFonts(fontInfoList, matcher);
  166. }
  167. /**
  168. * Updates the referenced font list.
  169. * @param fontInfoList a font info list
  170. * @param matcher the font triplet matcher to use
  171. */
  172. public void updateReferencedFonts(List<EmbedFontInfo> fontInfoList, Matcher matcher) {
  173. if (matcher == null) {
  174. return; //No referenced fonts
  175. }
  176. for (EmbedFontInfo fontInfo : fontInfoList) {
  177. for (FontTriplet triplet : fontInfo.getFontTriplets()) {
  178. if (matcher.matches(triplet)) {
  179. fontInfo.setEmbedded(false);
  180. break;
  181. }
  182. }
  183. }
  184. }
  185. /**
  186. * Detect fonts from the operating system via FOPs autodetect mechanism.
  187. *
  188. * @param autoDetectFonts if autodetect has been enabled
  189. * @param fontAdder the font adding mechanism
  190. * @param strict whether to enforce strict validation
  191. * @param listener the listener for font related events
  192. * @param fontInfoList a list of font info objects
  193. * @throws FOPException if an exception was thrown auto-detecting fonts
  194. */
  195. public void autoDetectFonts(boolean autoDetectFonts, FontAdder fontAdder, boolean strict,
  196. FontEventListener listener, List<EmbedFontInfo> fontInfoList) throws FOPException {
  197. if (autoDetectFonts) {
  198. fontDetector.detect(this, fontAdder, strict, listener, fontInfoList);
  199. }
  200. }
  201. }