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.

FontSubstitutionsConfigurator.java 3.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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.substitute;
  19. import org.apache.fop.apps.FOPException;
  20. import org.apache.fop.configuration.Configuration;
  21. /**
  22. * Configures a font substitution catalog
  23. */
  24. public class FontSubstitutionsConfigurator {
  25. private Configuration cfg;
  26. /**
  27. * Main constructor
  28. *
  29. * @param cfg a configuration
  30. */
  31. public FontSubstitutionsConfigurator(Configuration cfg) {
  32. this.cfg = cfg;
  33. }
  34. private static FontQualifier getQualfierFromConfiguration(Configuration cfg)
  35. throws FOPException {
  36. String fontFamily = cfg.getAttribute("font-family", null);
  37. if (fontFamily == null) {
  38. throw new FOPException("substitution qualifier must have a font-family");
  39. }
  40. FontQualifier qualifier = new FontQualifier();
  41. qualifier.setFontFamily(fontFamily);
  42. String fontWeight = cfg.getAttribute("font-weight", null);
  43. if (fontWeight != null) {
  44. qualifier.setFontWeight(fontWeight);
  45. }
  46. String fontStyle = cfg.getAttribute("font-style", null);
  47. if (fontStyle != null) {
  48. qualifier.setFontStyle(fontStyle);
  49. }
  50. return qualifier;
  51. }
  52. /**
  53. * Configures a font substitution catalog
  54. *
  55. * @param substitutions font substitutions
  56. * @throws FOPException if something's wrong with the config data
  57. */
  58. public void configure(FontSubstitutions substitutions) throws FOPException {
  59. Configuration[] substitutionCfgs = cfg.getChildren("substitution");
  60. for (Configuration substitutionCfg : substitutionCfgs) {
  61. Configuration fromCfg = substitutionCfg.getChild("from", false);
  62. if (fromCfg == null) {
  63. throw new FOPException("'substitution' element without child 'from' element");
  64. }
  65. Configuration toCfg = substitutionCfg.getChild("to", false);
  66. if (fromCfg == null) {
  67. throw new FOPException("'substitution' element without child 'to' element");
  68. }
  69. FontQualifier fromQualifier = getQualfierFromConfiguration(fromCfg);
  70. FontQualifier toQualifier = getQualfierFromConfiguration(toCfg);
  71. FontSubstitution substitution = new FontSubstitution(fromQualifier, toQualifier);
  72. substitutions.add(substitution);
  73. }
  74. }
  75. }