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.

ConfiguredFontCollection.java 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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.render.java2d;
  19. import java.util.List;
  20. import javax.xml.transform.Source;
  21. import org.apache.commons.logging.Log;
  22. import org.apache.commons.logging.LogFactory;
  23. import org.apache.fop.fonts.CustomFont;
  24. import org.apache.fop.fonts.EmbedFontInfo;
  25. import org.apache.fop.fonts.FontCollection;
  26. import org.apache.fop.fonts.FontInfo;
  27. import org.apache.fop.fonts.FontLoader;
  28. import org.apache.fop.fonts.FontManager;
  29. import org.apache.fop.fonts.FontResolver;
  30. import org.apache.fop.fonts.FontTriplet;
  31. import org.apache.fop.fonts.LazyFont;
  32. import org.apache.fop.render.PrintRenderer;
  33. /**
  34. * A java2d configured font collection
  35. */
  36. public class ConfiguredFontCollection implements FontCollection {
  37. private static Log log = LogFactory.getLog(ConfiguredFontCollection.class);
  38. private PrintRenderer renderer = null;
  39. /**
  40. * Main constructor
  41. *
  42. * @param renderer a print renderer
  43. */
  44. public ConfiguredFontCollection(PrintRenderer renderer) {
  45. this.renderer = renderer;
  46. }
  47. /**
  48. * {@inheritDoc}
  49. */
  50. public int setup(int start, FontInfo fontInfo) {
  51. List/*<EmbedFontInfo>*/ fontList = renderer.getFontList();
  52. FontResolver resolver = renderer.getFontResolver();
  53. int num = start;
  54. if (fontList == null || fontList.size() < 1) {
  55. log.debug("No user configured fonts found.");
  56. return num;
  57. }
  58. if (resolver == null) {
  59. // Ensure that we have minimal font resolution capabilities
  60. resolver = FontManager.createMinimalFontResolver();
  61. }
  62. String internalName = null;
  63. for (int i = 0; i < fontList.size(); i++) {
  64. EmbedFontInfo configFontInfo = (EmbedFontInfo) fontList.get(i);
  65. String fontFile = configFontInfo.getEmbedFile();
  66. internalName = "F" + num;
  67. num++;
  68. try {
  69. FontMetricsMapper font = null;
  70. String metricsUrl = configFontInfo.getMetricsFile();
  71. // If the user specified an XML-based metrics file, we'll use it
  72. // Otherwise, calculate metrics directly from the font file.
  73. if (metricsUrl != null) {
  74. LazyFont fontMetrics = new LazyFont(configFontInfo, resolver);
  75. Source fontSource = resolver.resolve(configFontInfo.getEmbedFile());
  76. font = new CustomFontMetricsMapper(fontMetrics, fontSource);
  77. } else {
  78. CustomFont fontMetrics = FontLoader.loadFont(fontFile, null, resolver);
  79. font = new CustomFontMetricsMapper(fontMetrics);
  80. }
  81. fontInfo.addMetrics(internalName, font);
  82. List triplets = configFontInfo.getFontTriplets();
  83. for (int c = 0; c < triplets.size(); c++) {
  84. FontTriplet triplet = (FontTriplet) triplets.get(c);
  85. if (log.isDebugEnabled()) {
  86. log.debug("Registering: " + triplet + " under " + internalName);
  87. }
  88. fontInfo.addFontProperties(internalName, triplet);
  89. }
  90. } catch (Exception e) {
  91. log.warn("Unable to load custom font from file '" + fontFile + "'", e);
  92. }
  93. }
  94. return num;
  95. }
  96. }