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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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.EncodingMode;
  26. import org.apache.fop.fonts.FontCollection;
  27. import org.apache.fop.fonts.FontInfo;
  28. import org.apache.fop.fonts.FontLoader;
  29. import org.apache.fop.fonts.FontManager;
  30. import org.apache.fop.fonts.FontResolver;
  31. import org.apache.fop.fonts.FontTriplet;
  32. import org.apache.fop.fonts.LazyFont;
  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 FontResolver fontResolver;
  39. private List/*<EmbedFontInfo>*/ embedFontInfoList;
  40. /**
  41. * Main constructor
  42. * @param fontResolver a font resolver
  43. * @param customFonts the list of custom fonts
  44. * @param useComplexScriptFeatures true if complex script features enabled
  45. */
  46. public ConfiguredFontCollection(FontResolver fontResolver,
  47. List/*<EmbedFontInfo>*/ customFonts, boolean useComplexScriptFeatures) {
  48. this.fontResolver = fontResolver;
  49. if (this.fontResolver == null) {
  50. //Ensure that we have minimal font resolution capabilities
  51. this.fontResolver = FontManager.createMinimalFontResolver(useComplexScriptFeatures);
  52. }
  53. this.embedFontInfoList = customFonts;
  54. }
  55. /** {@inheritDoc} */
  56. public int setup(int start, FontInfo fontInfo) {
  57. int num = start;
  58. if (embedFontInfoList == null || embedFontInfoList.size() < 1) {
  59. log.debug("No user configured fonts found.");
  60. return num;
  61. }
  62. String internalName = null;
  63. for (int i = 0; i < embedFontInfoList.size(); i++) {
  64. EmbedFontInfo configFontInfo = (EmbedFontInfo) embedFontInfoList.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, fontResolver);
  75. Source fontSource = fontResolver.resolve(configFontInfo.getEmbedFile());
  76. font = new CustomFontMetricsMapper(fontMetrics, fontSource);
  77. } else {
  78. CustomFont fontMetrics = FontLoader.loadFont(
  79. fontFile, null, true, configFontInfo.getEmbeddingMode(),
  80. EncodingMode.AUTO,
  81. configFontInfo.getKerning(),
  82. configFontInfo.getAdvanced(), fontResolver);
  83. font = new CustomFontMetricsMapper(fontMetrics);
  84. }
  85. fontInfo.addMetrics(internalName, font);
  86. List<FontTriplet> triplets = configFontInfo.getFontTriplets();
  87. for (int c = 0; c < triplets.size(); c++) {
  88. FontTriplet triplet = (FontTriplet) triplets.get(c);
  89. if (log.isDebugEnabled()) {
  90. log.debug("Registering: " + triplet + " under " + internalName);
  91. }
  92. fontInfo.addFontProperties(internalName, triplet);
  93. }
  94. } catch (Exception e) {
  95. log.warn("Unable to load custom font from file '" + fontFile + "'", e);
  96. }
  97. }
  98. return num;
  99. }
  100. }