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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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.io.InputStream;
  20. import java.net.URI;
  21. import java.util.List;
  22. import org.apache.commons.logging.Log;
  23. import org.apache.commons.logging.LogFactory;
  24. import org.apache.fop.apps.io.InternalResourceResolver;
  25. import org.apache.fop.fonts.CustomFont;
  26. import org.apache.fop.fonts.EmbedFontInfo;
  27. import org.apache.fop.fonts.FontCollection;
  28. import org.apache.fop.fonts.FontInfo;
  29. import org.apache.fop.fonts.FontLoader;
  30. import org.apache.fop.fonts.FontTriplet;
  31. import org.apache.fop.fonts.LazyFont;
  32. /**
  33. * A java2d configured font collection
  34. */
  35. public class ConfiguredFontCollection implements FontCollection {
  36. private static Log log = LogFactory.getLog(ConfiguredFontCollection.class);
  37. private final InternalResourceResolver resourceResolver;
  38. private final List<EmbedFontInfo> embedFontInfoList;
  39. private final boolean useComplexScripts;
  40. /**
  41. * Main constructor
  42. * @param resourceResolver a font resolver
  43. * @param customFonts the list of custom fonts
  44. * @param useComplexScriptFeatures true if complex script features enabled
  45. */
  46. public ConfiguredFontCollection(InternalResourceResolver resourceResolver,
  47. List<EmbedFontInfo> customFonts, boolean useComplexScriptFeatures) {
  48. this.resourceResolver = resourceResolver;
  49. this.embedFontInfoList = customFonts;
  50. this.useComplexScripts = useComplexScriptFeatures;
  51. }
  52. /** {@inheritDoc} */
  53. public int setup(int start, FontInfo fontInfo) {
  54. int num = start;
  55. if (embedFontInfoList == null || embedFontInfoList.size() < 1) {
  56. log.debug("No user configured fonts found.");
  57. return num;
  58. }
  59. String internalName = null;
  60. for (EmbedFontInfo configFontInfo : embedFontInfoList) {
  61. internalName = "F" + num++;
  62. try {
  63. URI fontURI = configFontInfo.getEmbedURI();
  64. FontMetricsMapper font = null;
  65. URI metricsURI = configFontInfo.getMetricsURI();
  66. // If the user specified an XML-based metrics file, we'll use it
  67. // Otherwise, calculate metrics directly from the font file.
  68. if (metricsURI != null) {
  69. LazyFont fontMetrics = new LazyFont(configFontInfo, resourceResolver, useComplexScripts);
  70. InputStream fontSource = resourceResolver.getResource(fontURI);
  71. font = new CustomFontMetricsMapper(fontMetrics, fontSource);
  72. } else {
  73. CustomFont fontMetrics = FontLoader.loadFont(fontURI, null, true,
  74. configFontInfo.getEmbeddingMode(), configFontInfo.getEncodingMode(),
  75. configFontInfo.getKerning(), configFontInfo.getAdvanced(),
  76. resourceResolver);
  77. font = new CustomFontMetricsMapper(fontMetrics);
  78. }
  79. fontInfo.addMetrics(internalName, font);
  80. for (FontTriplet triplet : configFontInfo.getFontTriplets()) {
  81. if (log.isDebugEnabled()) {
  82. log.debug("Registering: " + triplet + " under " + internalName);
  83. }
  84. fontInfo.addFontProperties(internalName, triplet);
  85. }
  86. } catch (Exception e) {
  87. log.warn("Unable to load custom font from file '" + configFontInfo.getEmbedURI()
  88. + "'", e);
  89. }
  90. }
  91. return num;
  92. }
  93. }