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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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.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. // caching (fonts)
  63. if (cfg.getChild("use-cache", false) != null) {
  64. try {
  65. if (!cfg.getChild("use-cache").getValueAsBoolean()) {
  66. fontManager.disableFontCache();
  67. } else {
  68. if (cfg.getChild("cache-file", false) != null) {
  69. fontManager.setCacheFile(new File(cfg.getChild("cache-file").getValue()));
  70. }
  71. }
  72. } catch (ConfigurationException mfue) {
  73. LogUtil.handleException(log, mfue, true);
  74. }
  75. }
  76. if (cfg.getChild("font-base", false) != null) {
  77. try {
  78. URI fontBase = InternalResourceResolver.getBaseURI(cfg.getChild("font-base").getValue(
  79. null));
  80. fontManager.setResourceResolver(new InternalResourceResolver(
  81. defaultBaseUri.resolve(fontBase), resourceResolver));
  82. } catch (URISyntaxException use) {
  83. LogUtil.handleException(log, use, true);
  84. }
  85. } else {
  86. fontManager.setResourceResolver(new InternalResourceResolver(defaultBaseUri, resourceResolver));
  87. }
  88. // [GA] permit configuration control over base14 kerning; without this,
  89. // there is no way for a user to enable base14 kerning other than by
  90. // programmatic API;
  91. if (cfg.getChild("base14-kerning", false) != null) {
  92. try {
  93. fontManager
  94. .setBase14KerningEnabled(cfg.getChild("base14-kerning").getValueAsBoolean());
  95. } catch (ConfigurationException e) {
  96. LogUtil.handleException(log, e, true);
  97. }
  98. }
  99. // global font configuration
  100. Configuration fontsCfg = cfg.getChild("fonts", false);
  101. if (fontsCfg != null) {
  102. // font substitution
  103. Configuration substitutionsCfg = fontsCfg.getChild("substitutions", false);
  104. if (substitutionsCfg != null) {
  105. FontSubstitutions substitutions = new FontSubstitutions();
  106. new FontSubstitutionsConfigurator(substitutionsCfg).configure(substitutions);
  107. fontManager.setFontSubstitutions(substitutions);
  108. }
  109. // referenced fonts (fonts which are not to be embedded)
  110. Configuration referencedFontsCfg = fontsCfg.getChild("referenced-fonts", false);
  111. if (referencedFontsCfg != null) {
  112. FontTriplet.Matcher matcher = createFontsMatcher(
  113. referencedFontsCfg, strict);
  114. fontManager.setReferencedFontsMatcher(matcher);
  115. }
  116. }
  117. }
  118. /**
  119. * Creates a font triplet matcher from a configuration object.
  120. * @param cfg the configuration object
  121. * @param strict true for strict configuraton error handling
  122. * @return the font matcher
  123. * @throws FOPException if an error occurs while building the matcher
  124. */
  125. public static FontTriplet.Matcher createFontsMatcher(
  126. Configuration cfg, boolean strict) throws FOPException {
  127. List<FontTriplet.Matcher> matcherList = new java.util.ArrayList<FontTriplet.Matcher>();
  128. Configuration[] matches = cfg.getChildren("match");
  129. for (int i = 0; i < matches.length; i++) {
  130. try {
  131. matcherList.add(new FontFamilyRegExFontTripletMatcher(
  132. matches[i].getAttribute("font-family")));
  133. } catch (ConfigurationException ce) {
  134. LogUtil.handleException(log, ce, strict);
  135. continue;
  136. }
  137. }
  138. FontTriplet.Matcher orMatcher = new OrFontTripletMatcher(
  139. matcherList.toArray(new FontTriplet.Matcher[matcherList.size()]));
  140. return orMatcher;
  141. }
  142. /**
  143. * Creates a font triplet matcher from a configuration object.
  144. * @param fontFamilies the list of font families
  145. * @param strict true for strict configuraton error handling
  146. * @return the font matcher
  147. * @throws FOPException if an error occurs while building the matcher
  148. */
  149. public static FontTriplet.Matcher createFontsMatcher(
  150. List<String> fontFamilies, boolean strict) throws FOPException {
  151. List<FontTriplet.Matcher> matcherList = new java.util.ArrayList<FontTriplet.Matcher>();
  152. for (String fontFamily : fontFamilies) {
  153. matcherList.add(new FontFamilyRegExFontTripletMatcher(fontFamily));
  154. }
  155. FontTriplet.Matcher orMatcher = new OrFontTripletMatcher(
  156. matcherList.toArray(new FontTriplet.Matcher[matcherList.size()]));
  157. return orMatcher;
  158. }
  159. private static class OrFontTripletMatcher implements FontTriplet.Matcher {
  160. private final FontTriplet.Matcher[] matchers;
  161. public OrFontTripletMatcher(FontTriplet.Matcher[] matchers) {
  162. this.matchers = matchers;
  163. }
  164. /** {@inheritDoc} */
  165. public boolean matches(FontTriplet triplet) {
  166. for (int i = 0, c = matchers.length; i < c; i++) {
  167. if (matchers[i].matches(triplet)) {
  168. return true;
  169. }
  170. }
  171. return false;
  172. }
  173. }
  174. private static class FontFamilyRegExFontTripletMatcher implements FontTriplet.Matcher {
  175. private final Pattern regex;
  176. public FontFamilyRegExFontTripletMatcher(String regex) {
  177. this.regex = Pattern.compile(regex);
  178. }
  179. /** {@inheritDoc} */
  180. public boolean matches(FontTriplet triplet) {
  181. return regex.matcher(triplet.getName()).matches();
  182. }
  183. }
  184. }