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.

FontManagerConfigurator.java 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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.net.URISyntaxException;
  21. import java.util.List;
  22. import java.util.regex.Pattern;
  23. import org.apache.avalon.framework.configuration.Configuration;
  24. import org.apache.avalon.framework.configuration.ConfigurationException;
  25. import org.apache.commons.logging.Log;
  26. import org.apache.commons.logging.LogFactory;
  27. import org.apache.xmlgraphics.io.ResourceResolver;
  28. import org.apache.fop.apps.FOPException;
  29. import org.apache.fop.apps.io.InternalResourceResolver;
  30. import org.apache.fop.apps.io.ResourceResolverFactory;
  31. import org.apache.fop.fonts.substitute.FontSubstitutions;
  32. import org.apache.fop.fonts.substitute.FontSubstitutionsConfigurator;
  33. import org.apache.fop.util.LogUtil;
  34. /**
  35. * Configurator of the FontManager
  36. */
  37. public class FontManagerConfigurator {
  38. /** logger instance */
  39. private static Log log = LogFactory.getLog(FontManagerConfigurator.class);
  40. private final Configuration cfg;
  41. private final URI defaultBaseUri;
  42. private final ResourceResolver resourceResolver;
  43. /**
  44. * Main constructor
  45. * @param cfg the font manager configuration object
  46. * @param defaultBaseUri the default URI base to use for URI resolution
  47. * @param resourceResolver the resource resolver
  48. */
  49. public FontManagerConfigurator(Configuration cfg, URI defaultBaseUri,
  50. ResourceResolver resourceResolver) {
  51. this.cfg = cfg;
  52. this.defaultBaseUri = defaultBaseUri;
  53. this.resourceResolver = resourceResolver;
  54. }
  55. /**
  56. * Initializes font settings from the user configuration
  57. * @param fontManager a font manager
  58. * @param strict true if strict checking of the configuration is enabled
  59. * @throws FOPException if an exception occurs while processing the configuration
  60. */
  61. public void configure(FontManager fontManager, boolean strict) throws FOPException {
  62. if (cfg.getChild("font-base", false) != null) {
  63. try {
  64. URI fontBase = InternalResourceResolver.getBaseURI(cfg.getChild("font-base")
  65. .getValue(null));
  66. fontManager.setResourceResolver(ResourceResolverFactory.createInternalResourceResolver(
  67. defaultBaseUri.resolve(fontBase), resourceResolver));
  68. } catch (URISyntaxException use) {
  69. LogUtil.handleException(log, use, true);
  70. }
  71. } else {
  72. fontManager.setResourceResolver(ResourceResolverFactory.createInternalResourceResolver(
  73. defaultBaseUri, resourceResolver));
  74. }
  75. // caching (fonts)
  76. if (cfg.getChild("use-cache", false) != null) {
  77. try {
  78. if (!cfg.getChild("use-cache").getValueAsBoolean()) {
  79. fontManager.disableFontCache();
  80. } else {
  81. if (cfg.getChild("cache-file", false) != null) {
  82. fontManager.setCacheFile(URI.create(cfg.getChild("cache-file").getValue()));
  83. }
  84. }
  85. } catch (ConfigurationException mfue) {
  86. LogUtil.handleException(log, mfue, true);
  87. }
  88. }
  89. // [GA] permit configuration control over base14 kerning; without this,
  90. // there is no way for a user to enable base14 kerning other than by
  91. // programmatic API;
  92. if (cfg.getChild("base14-kerning", false) != null) {
  93. try {
  94. fontManager
  95. .setBase14KerningEnabled(cfg.getChild("base14-kerning").getValueAsBoolean());
  96. } catch (ConfigurationException e) {
  97. LogUtil.handleException(log, e, true);
  98. }
  99. }
  100. // global font configuration
  101. Configuration fontsCfg = cfg.getChild("fonts", false);
  102. if (fontsCfg != null) {
  103. // font substitution
  104. Configuration substitutionsCfg = fontsCfg.getChild("substitutions", false);
  105. if (substitutionsCfg != null) {
  106. FontSubstitutions substitutions = new FontSubstitutions();
  107. new FontSubstitutionsConfigurator(substitutionsCfg).configure(substitutions);
  108. fontManager.setFontSubstitutions(substitutions);
  109. }
  110. // referenced fonts (fonts which are not to be embedded)
  111. Configuration referencedFontsCfg = fontsCfg.getChild("referenced-fonts", false);
  112. if (referencedFontsCfg != null) {
  113. FontTriplet.Matcher matcher = createFontsMatcher(
  114. referencedFontsCfg, strict);
  115. fontManager.setReferencedFontsMatcher(matcher);
  116. }
  117. }
  118. }
  119. /**
  120. * Creates a font triplet matcher from a configuration object.
  121. * @param cfg the configuration object
  122. * @param strict true for strict configuraton error handling
  123. * @return the font matcher
  124. * @throws FOPException if an error occurs while building the matcher
  125. */
  126. public static FontTriplet.Matcher createFontsMatcher(
  127. Configuration cfg, boolean strict) throws FOPException {
  128. List<FontTriplet.Matcher> matcherList = new java.util.ArrayList<FontTriplet.Matcher>();
  129. Configuration[] matches = cfg.getChildren("match");
  130. for (int i = 0; i < matches.length; i++) {
  131. try {
  132. matcherList.add(new FontFamilyRegExFontTripletMatcher(
  133. matches[i].getAttribute("font-family")));
  134. } catch (ConfigurationException ce) {
  135. LogUtil.handleException(log, ce, strict);
  136. continue;
  137. }
  138. }
  139. FontTriplet.Matcher orMatcher = new OrFontTripletMatcher(
  140. matcherList.toArray(new FontTriplet.Matcher[matcherList.size()]));
  141. return orMatcher;
  142. }
  143. /**
  144. * Creates a font triplet matcher from a configuration object.
  145. * @param fontFamilies the list of font families
  146. * @param strict true for strict configuraton error handling
  147. * @return the font matcher
  148. * @throws FOPException if an error occurs while building the matcher
  149. */
  150. public static FontTriplet.Matcher createFontsMatcher(
  151. List<String> fontFamilies, boolean strict) throws FOPException {
  152. List<FontTriplet.Matcher> matcherList = new java.util.ArrayList<FontTriplet.Matcher>();
  153. for (String fontFamily : fontFamilies) {
  154. matcherList.add(new FontFamilyRegExFontTripletMatcher(fontFamily));
  155. }
  156. FontTriplet.Matcher orMatcher = new OrFontTripletMatcher(
  157. matcherList.toArray(new FontTriplet.Matcher[matcherList.size()]));
  158. return orMatcher;
  159. }
  160. private static class OrFontTripletMatcher implements FontTriplet.Matcher {
  161. private final FontTriplet.Matcher[] matchers;
  162. public OrFontTripletMatcher(FontTriplet.Matcher[] matchers) {
  163. this.matchers = matchers;
  164. }
  165. /** {@inheritDoc} */
  166. public boolean matches(FontTriplet triplet) {
  167. for (int i = 0, c = matchers.length; i < c; i++) {
  168. if (matchers[i].matches(triplet)) {
  169. return true;
  170. }
  171. }
  172. return false;
  173. }
  174. }
  175. private static class FontFamilyRegExFontTripletMatcher implements FontTriplet.Matcher {
  176. private final Pattern regex;
  177. public FontFamilyRegExFontTripletMatcher(String regex) {
  178. this.regex = Pattern.compile(regex);
  179. }
  180. /** {@inheritDoc} */
  181. public boolean matches(FontTriplet triplet) {
  182. return regex.matcher(triplet.getName()).matches();
  183. }
  184. }
  185. }