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 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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.MalformedURLException;
  21. import java.net.URI;
  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.fonts.substitute.FontSubstitutions;
  30. import org.apache.fop.fonts.substitute.FontSubstitutionsConfigurator;
  31. import org.apache.fop.util.LogUtil;
  32. /**
  33. * Configurator of the FontManager
  34. */
  35. public class FontManagerConfigurator {
  36. /** logger instance */
  37. private static Log log = LogFactory.getLog(FontManagerConfigurator.class);
  38. private final Configuration cfg;
  39. private URI baseURI = null;
  40. /**
  41. * Main constructor
  42. * @param cfg the font manager configuration object
  43. */
  44. public FontManagerConfigurator(Configuration cfg) {
  45. this.cfg = cfg;
  46. }
  47. /**
  48. * Main constructor
  49. * @param cfg the font manager configuration object
  50. * @param baseURI the base URI of the configuration
  51. */
  52. public FontManagerConfigurator(Configuration cfg, URI baseURI) {
  53. this.cfg = cfg;
  54. this.baseURI = baseURI;
  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. fontManager.setUseCache(cfg.getChild("use-cache").getValueAsBoolean());
  67. } catch (ConfigurationException e) {
  68. LogUtil.handleException(log, e, true);
  69. }
  70. }
  71. if (cfg.getChild("cache-file", false) != null) {
  72. try {
  73. fontManager.setCacheFile(new File(cfg.getChild("cache-file").getValue()));
  74. } catch (ConfigurationException e) {
  75. LogUtil.handleException(log, e, true);
  76. }
  77. }
  78. if (cfg.getChild("font-base", false) != null) {
  79. String path = cfg.getChild("font-base").getValue(null);
  80. if (baseURI != null) {
  81. path = baseURI.resolve(path).normalize().toString();
  82. }
  83. try {
  84. fontManager.setFontBaseURL(path);
  85. } catch (MalformedURLException 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. private static class OrFontTripletMatcher implements FontTriplet.Matcher {
  144. private final FontTriplet.Matcher[] matchers;
  145. public OrFontTripletMatcher(FontTriplet.Matcher[] matchers) {
  146. this.matchers = matchers;
  147. }
  148. /** {@inheritDoc} */
  149. public boolean matches(FontTriplet triplet) {
  150. for (int i = 0, c = matchers.length; i < c; i++) {
  151. if (matchers[i].matches(triplet)) {
  152. return true;
  153. }
  154. }
  155. return false;
  156. }
  157. }
  158. private static class FontFamilyRegExFontTripletMatcher implements FontTriplet.Matcher {
  159. private final Pattern regex;
  160. public FontFamilyRegExFontTripletMatcher(String regex) {
  161. this.regex = Pattern.compile(regex);
  162. }
  163. /** {@inheritDoc} */
  164. public boolean matches(FontTriplet triplet) {
  165. return regex.matcher(triplet.getName()).matches();
  166. }
  167. }
  168. }