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

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