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.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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.awt.Graphics2D;
  20. import java.net.MalformedURLException;
  21. import javax.xml.transform.Source;
  22. import javax.xml.transform.stream.StreamSource;
  23. import org.apache.fop.apps.FopFactory;
  24. import org.apache.fop.fonts.substitute.FontSubstitutions;
  25. import org.apache.fop.render.PrintRenderer;
  26. // TODO: Refactor fonts package so major font activities (autodetection etc)
  27. // are all centrally managed and delegated from this class, also remove dependency on FopFactory
  28. // and start using POJO config/properties type classes
  29. /**
  30. * The manager of fonts
  31. */
  32. public class FontManager {
  33. /** Use cache (record previously detected font triplet info) */
  34. public static final boolean DEFAULT_USE_CACHE = true;
  35. /** The base URL for all font URL resolutions. */
  36. private String fontBase = null;
  37. /** Font cache to speed up auto-font configuration (null if disabled) */
  38. private FontCache fontCache = null;
  39. /** Font substitutions */
  40. private FontSubstitutions fontSubstitutions = null;
  41. private FopFactory fopFactory = null;
  42. /** Allows enabling kerning on the base 14 fonts, default is false */
  43. private boolean enableBase14Kerning = false;
  44. /**
  45. * Main constructor
  46. *
  47. * @param fopFactory the fo URI resolver
  48. */
  49. public FontManager(FopFactory fopFactory) {
  50. this(fopFactory, DEFAULT_USE_CACHE);
  51. }
  52. /**
  53. * Constructor
  54. *
  55. * @param fopFactory the fo URI resolver
  56. * @param useCache true if the FontCache should be used
  57. */
  58. public FontManager(FopFactory fopFactory, boolean useCache) {
  59. this.fopFactory = fopFactory;
  60. setUseCache(useCache);
  61. }
  62. /**
  63. * Sets the font base URL.
  64. * @param fontBase font base URL
  65. * @throws MalformedURLException if there's a problem with a file URL
  66. */
  67. public void setFontBaseURL(String fontBase) throws MalformedURLException {
  68. this.fontBase = fopFactory.getFOURIResolver().checkBaseURL(fontBase);
  69. }
  70. /**
  71. * @return the font base URL
  72. */
  73. public String getFontBaseURL() {
  74. return this.fontBase;
  75. }
  76. /** @return true if kerning on base 14 fonts is enabled */
  77. public boolean isBase14KerningEnabled() {
  78. return this.enableBase14Kerning;
  79. }
  80. /**
  81. * Controls whether kerning is activated on base 14 fonts.
  82. * @param value true if kerning should be activated
  83. */
  84. public void setBase14KerningEnabled(boolean value) {
  85. this.enableBase14Kerning = value;
  86. }
  87. /**
  88. * Sets the font substitutions
  89. * @param substitutions font substitutions
  90. */
  91. public void setFontSubstitutions(FontSubstitutions substitutions) {
  92. this.fontSubstitutions = substitutions;
  93. }
  94. /**
  95. * Returns the font substitution catalog
  96. * @return the font substitution catalog
  97. */
  98. protected FontSubstitutions getFontSubstitutions() {
  99. if (fontSubstitutions == null) {
  100. this.fontSubstitutions = new FontSubstitutions();
  101. }
  102. return fontSubstitutions;
  103. }
  104. /**
  105. * Whether or not to cache results of font triplet detection/auto-config
  106. * @param useCache use cache or not
  107. */
  108. public void setUseCache(boolean useCache) {
  109. if (useCache) {
  110. this.fontCache = FontCache.load();
  111. if (this.fontCache == null) {
  112. this.fontCache = new FontCache();
  113. }
  114. } else {
  115. this.fontCache = null;
  116. }
  117. }
  118. /**
  119. * Cache results of font triplet detection/auto-config?
  120. * @return true if this font manager uses the cache
  121. */
  122. public boolean useCache() {
  123. return (this.fontCache != null);
  124. }
  125. /**
  126. * Returns the font cache instance used by this font manager.
  127. * @return the font cache
  128. */
  129. public FontCache getFontCache() {
  130. return this.fontCache;
  131. }
  132. /**
  133. * Sets up the fonts on a given PrintRenderer
  134. * @param renderer a print renderer
  135. */
  136. public void setupRenderer(PrintRenderer renderer) {
  137. FontInfo fontInfo = renderer.getFontInfo();
  138. int startNum = 1;
  139. // Configure base 14 fonts
  140. org.apache.fop.fonts.base14.Base14FontCollection base14FontCollection
  141. = new org.apache.fop.fonts.base14.Base14FontCollection(this.enableBase14Kerning);
  142. startNum = base14FontCollection.setup(startNum, fontInfo);
  143. // Configure any custom font collection
  144. org.apache.fop.fonts.CustomFontCollection customFontCollection
  145. = new org.apache.fop.fonts.CustomFontCollection(renderer);
  146. startNum = customFontCollection.setup(startNum, fontInfo);
  147. // Make any defined substitutions in the font info
  148. getFontSubstitutions().adjustFontInfo(fontInfo);
  149. }
  150. /**
  151. * Sets up the fonts on a given PrintRenderer with Graphics2D
  152. * @param renderer a print renderer
  153. * @param graphics2D a graphics 2D
  154. */
  155. public void setupRenderer(PrintRenderer renderer, Graphics2D graphics2D) {
  156. FontInfo fontInfo = renderer.getFontInfo();
  157. int startNum = 1;
  158. // setup base 14 fonts
  159. org.apache.fop.render.java2d.Base14FontCollection base14FontCollection
  160. = new org.apache.fop.render.java2d.Base14FontCollection(graphics2D);
  161. // setup any custom font collection
  162. startNum = base14FontCollection.setup(startNum, fontInfo);
  163. // setup any installed fonts
  164. org.apache.fop.render.java2d.InstalledFontCollection installedFontCollection
  165. = new org.apache.fop.render.java2d.InstalledFontCollection(graphics2D);
  166. startNum = installedFontCollection.setup(startNum, fontInfo);
  167. // setup any configured fonts
  168. org.apache.fop.render.java2d.ConfiguredFontCollection configuredFontCollection
  169. = new org.apache.fop.render.java2d.ConfiguredFontCollection(renderer);
  170. startNum = configuredFontCollection.setup(startNum, fontInfo);
  171. // Make any defined substitutions in the font info
  172. getFontSubstitutions().adjustFontInfo(fontInfo);
  173. }
  174. /** @return a new FontResolver to be used by the font subsystem */
  175. public static FontResolver createMinimalFontResolver() {
  176. return new FontResolver() {
  177. /** {@inheritDoc} */
  178. public Source resolve(String href) {
  179. //Minimal functionality here
  180. return new StreamSource(href);
  181. }
  182. };
  183. }
  184. }