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

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