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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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.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.fop.apps.FOPException;
  28. import org.apache.fop.fonts.substitute.FontSubstitutions;
  29. import org.apache.fop.fonts.substitute.FontSubstitutionsConfigurator;
  30. import org.apache.fop.util.LogUtil;
  31. /**
  32. * Configurator of the FontManager
  33. */
  34. public class FontManagerConfigurator {
  35. /** logger instance */
  36. private static Log log = LogFactory.getLog(FontManagerConfigurator.class);
  37. private Configuration cfg;
  38. /**
  39. * Main constructor
  40. * @param cfg the font manager configuration object
  41. */
  42. public FontManagerConfigurator(Configuration cfg) {
  43. this.cfg = cfg;
  44. }
  45. /**
  46. * Initializes font settings from the user configuration
  47. * @param fontManager a font manager
  48. * @param strict true if strict checking of the configuration is enabled
  49. * @throws FOPException if an exception occurs while processing the configuration
  50. */
  51. public void configure(FontManager fontManager, boolean strict) throws FOPException {
  52. // caching (fonts)
  53. if (cfg.getChild("use-cache", false) != null) {
  54. try {
  55. fontManager.setUseCache(cfg.getChild("use-cache").getValueAsBoolean());
  56. } catch (ConfigurationException e) {
  57. LogUtil.handleException(log, e, true);
  58. }
  59. }
  60. if (cfg.getChild("cache-file", false) != null) {
  61. try {
  62. fontManager.setCacheFile(new File(cfg.getChild("cache-file").getValue()));
  63. } catch (ConfigurationException e) {
  64. LogUtil.handleException(log, e, true);
  65. }
  66. }
  67. if (cfg.getChild("font-base", false) != null) {
  68. try {
  69. fontManager.setFontBaseURL(cfg.getChild("font-base").getValue(null));
  70. } catch (MalformedURLException mfue) {
  71. LogUtil.handleException(log, mfue, true);
  72. }
  73. }
  74. // global font configuration
  75. Configuration fontsCfg = cfg.getChild("fonts", false);
  76. if (fontsCfg != null) {
  77. // font substitution
  78. Configuration substitutionsCfg = fontsCfg.getChild("substitutions", false);
  79. if (substitutionsCfg != null) {
  80. FontSubstitutions substitutions = new FontSubstitutions();
  81. new FontSubstitutionsConfigurator(substitutionsCfg).configure(substitutions);
  82. fontManager.setFontSubstitutions(substitutions);
  83. }
  84. // referenced fonts (fonts which are not to be embedded)
  85. Configuration referencedFontsCfg = fontsCfg.getChild("referenced-fonts", false);
  86. if (referencedFontsCfg != null) {
  87. FontTriplet.Matcher matcher = createFontsMatcher(
  88. referencedFontsCfg, strict);
  89. fontManager.setReferencedFontsMatcher(matcher);
  90. }
  91. }
  92. }
  93. /**
  94. * Creates a font triplet matcher from a configuration object.
  95. * @param cfg the configuration object
  96. * @param strict true for strict configuraton error handling
  97. * @return the font matcher
  98. * @throws FOPException if an error occurs while building the matcher
  99. */
  100. public static FontTriplet.Matcher createFontsMatcher(
  101. Configuration cfg, boolean strict) throws FOPException {
  102. List matcherList = new java.util.ArrayList();
  103. Configuration[] matches = cfg.getChildren("match");
  104. for (int i = 0; i < matches.length; i++) {
  105. try {
  106. matcherList.add(new FontFamilyRegExFontTripletMatcher(
  107. matches[i].getAttribute("font-family")));
  108. } catch (ConfigurationException ce) {
  109. LogUtil.handleException(log, ce, strict);
  110. continue;
  111. }
  112. }
  113. FontTriplet.Matcher orMatcher = new OrFontTripletMatcher(
  114. (FontTriplet.Matcher[])matcherList.toArray(
  115. new FontTriplet.Matcher[matcherList.size()]));
  116. return orMatcher;
  117. }
  118. private static class OrFontTripletMatcher implements FontTriplet.Matcher {
  119. private FontTriplet.Matcher[] matchers;
  120. public OrFontTripletMatcher(FontTriplet.Matcher[] matchers) {
  121. this.matchers = matchers;
  122. }
  123. /** {@inheritDoc} */
  124. public boolean matches(FontTriplet triplet) {
  125. for (int i = 0, c = matchers.length; i < c; i++) {
  126. if (matchers[i].matches(triplet)) {
  127. return true;
  128. }
  129. }
  130. return false;
  131. }
  132. }
  133. private static class FontFamilyRegExFontTripletMatcher implements FontTriplet.Matcher {
  134. private Pattern regex;
  135. public FontFamilyRegExFontTripletMatcher(String regex) {
  136. this.regex = Pattern.compile(regex);
  137. }
  138. /** {@inheritDoc} */
  139. public boolean matches(FontTriplet triplet) {
  140. return regex.matcher(triplet.getName()).matches();
  141. }
  142. }
  143. }