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.

PrintRendererConfigurator.java 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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;
  19. import java.util.List;
  20. import org.apache.avalon.framework.configuration.Configuration;
  21. import org.apache.commons.logging.Log;
  22. import org.apache.commons.logging.LogFactory;
  23. import org.apache.fop.apps.FOPException;
  24. import org.apache.fop.apps.FOUserAgent;
  25. import org.apache.fop.apps.FopFactory;
  26. import org.apache.fop.fonts.CustomFontCollection;
  27. import org.apache.fop.fonts.EmbedFontInfo;
  28. import org.apache.fop.fonts.FontCollection;
  29. import org.apache.fop.fonts.FontEventAdapter;
  30. import org.apache.fop.fonts.FontEventListener;
  31. import org.apache.fop.fonts.FontInfo;
  32. import org.apache.fop.fonts.FontInfoConfigurator;
  33. import org.apache.fop.fonts.FontManager;
  34. import org.apache.fop.fonts.FontResolver;
  35. import org.apache.fop.fonts.base14.Base14FontCollection;
  36. import org.apache.fop.render.intermediate.IFDocumentHandler;
  37. import org.apache.fop.render.intermediate.IFDocumentHandlerConfigurator;
  38. /**
  39. * Base Print renderer configurator (mostly handles font configuration)
  40. */
  41. public class PrintRendererConfigurator extends AbstractRendererConfigurator
  42. implements RendererConfigurator, IFDocumentHandlerConfigurator {
  43. /** logger instance */
  44. protected static final Log log = LogFactory.getLog(PrintRendererConfigurator.class);
  45. /**
  46. * Default constructor
  47. * @param userAgent user agent
  48. */
  49. public PrintRendererConfigurator(FOUserAgent userAgent) {
  50. super(userAgent);
  51. }
  52. /**
  53. * Builds a list of EmbedFontInfo objects for use with the setup() method.
  54. *
  55. * @param renderer print renderer
  56. * @throws FOPException if something's wrong with the config data
  57. */
  58. public void configure(Renderer renderer) throws FOPException {
  59. Configuration cfg = getRendererConfig(renderer);
  60. if (cfg == null) {
  61. log.trace("no configuration found for " + renderer);
  62. return;
  63. }
  64. PrintRenderer printRenderer = (PrintRenderer)renderer;
  65. FontResolver fontResolver = printRenderer.getFontResolver();
  66. FontEventListener listener = new FontEventAdapter(
  67. renderer.getUserAgent().getEventBroadcaster());
  68. List/*<EmbedFontInfo>*/ embedFontInfoList = buildFontList(cfg, fontResolver, listener);
  69. printRenderer.addFontList(embedFontInfoList);
  70. }
  71. /**
  72. * Builds the font list from configuration.
  73. * @param cfg the configuration object
  74. * @param fontResolver a font resolver
  75. * @param listener the font event listener
  76. * @return the list of {@link EmbedFontInfo} objects
  77. * @throws FOPException if an error occurs while processing the configuration
  78. */
  79. protected List/*<EmbedFontInfo>*/ buildFontList(Configuration cfg, FontResolver fontResolver,
  80. FontEventListener listener) throws FOPException {
  81. FopFactory factory = userAgent.getFactory();
  82. FontManager fontManager = factory.getFontManager();
  83. if (fontResolver == null) {
  84. //Ensure that we have minimal font resolution capabilities
  85. fontResolver = FontManager.createMinimalFontResolver();
  86. }
  87. boolean strict = factory.validateUserConfigStrictly();
  88. //Read font configuration
  89. FontInfoConfigurator fontInfoConfigurator
  90. = new FontInfoConfigurator(cfg, fontManager, fontResolver, listener, strict);
  91. List/*<EmbedFontInfo>*/ fontInfoList = new java.util.ArrayList/*<EmbedFontInfo>*/();
  92. fontInfoConfigurator.configure(fontInfoList);
  93. return fontInfoList;
  94. }
  95. // ---=== IFDocumentHandler configuration ===---
  96. /** {@inheritDoc} */
  97. public void configure(IFDocumentHandler documentHandler) throws FOPException {
  98. //nop
  99. }
  100. /** {@inheritDoc} */
  101. public void setupFontInfo(IFDocumentHandler documentHandler, FontInfo fontInfo)
  102. throws FOPException {
  103. FontManager fontManager = userAgent.getFactory().getFontManager();
  104. List fontCollections = new java.util.ArrayList();
  105. fontCollections.add(new Base14FontCollection(fontManager.isBase14KerningEnabled()));
  106. Configuration cfg = super.getRendererConfig(documentHandler.getMimeType());
  107. if (cfg != null) {
  108. FontResolver fontResolver = new DefaultFontResolver(userAgent);
  109. FontEventListener listener = new FontEventAdapter(
  110. userAgent.getEventBroadcaster());
  111. List fontList = buildFontList(cfg, fontResolver, listener);
  112. fontCollections.add(new CustomFontCollection(fontResolver, fontList));
  113. }
  114. fontManager.setup(fontInfo,
  115. (FontCollection[])fontCollections.toArray(
  116. new FontCollection[fontCollections.size()]));
  117. documentHandler.setFontInfo(fontInfo);
  118. }
  119. }