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