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.

FontManager.java 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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.File;
  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. /** Provides a font cache file path **/
  43. private File cacheFile;
  44. /**
  45. * Main constructor
  46. *
  47. * @param resourceResolver the URI resolver
  48. * @param fontDetector the font detector
  49. * @param fontCacheManager the font cache manager
  50. */
  51. public FontManager(InternalResourceResolver resourceResolver, FontDetector fontDetector,
  52. FontCacheManager fontCacheManager) {
  53. this.resourceResolver = resourceResolver;
  54. this.fontDetector = fontDetector;
  55. this.fontCacheManager = fontCacheManager;
  56. }
  57. /**
  58. * Sets the font resource resolver
  59. * @param resourceResolver resource resolver
  60. */
  61. public void setResourceResolver(InternalResourceResolver resourceResolver) {
  62. this.resourceResolver = resourceResolver;
  63. }
  64. public InternalResourceResolver getResourceResolver() {
  65. return this.resourceResolver;
  66. }
  67. /** @return true if kerning on base 14 fonts is enabled */
  68. public boolean isBase14KerningEnabled() {
  69. return this.enableBase14Kerning;
  70. }
  71. /**
  72. * Controls whether kerning is activated on base 14 fonts.
  73. * @param value true if kerning should be activated
  74. */
  75. public void setBase14KerningEnabled(boolean value) {
  76. this.enableBase14Kerning = value;
  77. }
  78. /**
  79. * Sets the font substitutions
  80. * @param substitutions font substitutions
  81. */
  82. public void setFontSubstitutions(FontSubstitutions substitutions) {
  83. this.fontSubstitutions = substitutions;
  84. }
  85. /**
  86. * Returns the font substitution catalog
  87. * @return the font substitution catalog
  88. */
  89. protected FontSubstitutions getFontSubstitutions() {
  90. if (fontSubstitutions == null) {
  91. this.fontSubstitutions = new FontSubstitutions();
  92. }
  93. return fontSubstitutions;
  94. }
  95. /**
  96. * Sets the font cache file
  97. * @param cacheFile the font cache file
  98. */
  99. public void setCacheFile(File cacheFile) {
  100. this.cacheFile = cacheFile;
  101. }
  102. /**
  103. * Returns the font cache file
  104. * @return the font cache file
  105. */
  106. public File getCacheFile() {
  107. return getCacheFile(false);
  108. }
  109. private File getCacheFile(boolean writable) {
  110. if (cacheFile != null) {
  111. return cacheFile;
  112. }
  113. return FontCache.getDefaultCacheFile(writable);
  114. }
  115. /**
  116. * Whether or not to cache results of font triplet detection/auto-config
  117. * @param useCache use cache or not
  118. */
  119. public void disableFontCache() {
  120. fontCacheManager = FontCacheManagerFactory.createDisabled();
  121. }
  122. /**
  123. * Returns the font cache instance used by this font manager.
  124. * @return the font cache
  125. */
  126. public FontCache getFontCache() {
  127. return fontCacheManager.load(getCacheFile());
  128. }
  129. /**
  130. * Saves the FontCache as necessary
  131. *
  132. * @throws FOPException fop exception
  133. */
  134. public void saveCache() throws FOPException {
  135. fontCacheManager.save(getCacheFile());
  136. }
  137. /**
  138. * Deletes the current FontCache file
  139. * @return Returns true if the font cache file was successfully deleted.
  140. * @throws FOPException -
  141. */
  142. public void deleteCache() throws FOPException {
  143. fontCacheManager.delete(getCacheFile(true));
  144. }
  145. /**
  146. * Sets up the fonts on a given FontInfo object. The fonts to setup are defined by an
  147. * array of {@link FontCollection} objects.
  148. * @param fontInfo the FontInfo object to set up
  149. * @param fontCollections the array of font collections/sources
  150. */
  151. public void setup(FontInfo fontInfo, FontCollection[] fontCollections) {
  152. int startNum = 1;
  153. for (int i = 0, c = fontCollections.length; i < c; i++) {
  154. startNum = fontCollections[i].setup(startNum, fontInfo);
  155. }
  156. // Make any defined substitutions in the font info
  157. getFontSubstitutions().adjustFontInfo(fontInfo);
  158. }
  159. /**
  160. * Sets the {@link FontTriplet.Matcher} that can be used to identify the fonts that shall
  161. * be referenced rather than embedded.
  162. * @param matcher the font triplet matcher
  163. */
  164. public void setReferencedFontsMatcher(FontTriplet.Matcher matcher) {
  165. this.referencedFontsMatcher = matcher;
  166. }
  167. /**
  168. * Gets the {@link FontTriplet.Matcher} that can be used to identify the fonts that shall
  169. * be referenced rather than embedded.
  170. * @return the font triplet matcher (or null if none is set)
  171. */
  172. public Matcher getReferencedFontsMatcher() {
  173. return this.referencedFontsMatcher;
  174. }
  175. /**
  176. * Updates the referenced font list using the FontManager's referenced fonts matcher
  177. * ({@link #getReferencedFontsMatcher()}).
  178. * @param fontInfoList a font info list
  179. */
  180. public void updateReferencedFonts(List<EmbedFontInfo> fontInfoList) {
  181. Matcher matcher = getReferencedFontsMatcher();
  182. updateReferencedFonts(fontInfoList, matcher);
  183. }
  184. /**
  185. * Updates the referenced font list.
  186. * @param fontInfoList a font info list
  187. * @param matcher the font triplet matcher to use
  188. */
  189. public void updateReferencedFonts(List<EmbedFontInfo> fontInfoList, Matcher matcher) {
  190. if (matcher == null) {
  191. return; //No referenced fonts
  192. }
  193. for (EmbedFontInfo fontInfo : fontInfoList) {
  194. for (FontTriplet triplet : fontInfo.getFontTriplets()) {
  195. if (matcher.matches(triplet)) {
  196. fontInfo.setEmbedded(false);
  197. break;
  198. }
  199. }
  200. }
  201. }
  202. public void autoDetectFonts(boolean autoDetectFonts, FontAdder fontAdder, boolean strict,
  203. FontEventListener listener, List<EmbedFontInfo> fontInfoList) throws FOPException {
  204. if (autoDetectFonts) {
  205. fontDetector.detect(this, fontAdder, strict, listener, fontInfoList);
  206. }
  207. }
  208. }