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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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.Collections;
  20. import java.util.List;
  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.io.InternalResourceResolver;
  26. import org.apache.fop.fonts.CustomFontCollection;
  27. import org.apache.fop.fonts.DefaultFontConfigurator;
  28. import org.apache.fop.fonts.EmbedFontInfo;
  29. import org.apache.fop.fonts.FontCollection;
  30. import org.apache.fop.fonts.FontConfigurator;
  31. import org.apache.fop.fonts.FontEventAdapter;
  32. import org.apache.fop.fonts.FontInfo;
  33. import org.apache.fop.fonts.FontManager;
  34. import org.apache.fop.render.RendererConfig.RendererConfigParser;
  35. import org.apache.fop.render.intermediate.IFDocumentHandler;
  36. import org.apache.fop.render.intermediate.IFDocumentHandlerConfigurator;
  37. /**
  38. * Base Print renderer configurator (mostly handles font configuration)
  39. */
  40. public abstract class PrintRendererConfigurator extends AbstractRendererConfigurator
  41. implements IFDocumentHandlerConfigurator {
  42. /** logger instance */
  43. private static Log LOG = LogFactory.getLog(PrintRendererConfigurator.class);
  44. private final RendererConfigParser rendererConfigParser;
  45. private final FontConfigurator<EmbedFontInfo> fontInfoConfigurator;
  46. /**
  47. * Default constructor
  48. * @param userAgent user agent
  49. */
  50. public PrintRendererConfigurator(FOUserAgent userAgent, RendererConfigParser rendererConfigParser) {
  51. this(userAgent, rendererConfigParser,
  52. new DefaultFontConfigurator(userAgent.getFontManager(), new FontEventAdapter(
  53. userAgent.getEventBroadcaster()), userAgent.validateUserConfigStrictly()));
  54. }
  55. /**
  56. * Default constructor
  57. * @param userAgent user agent
  58. */
  59. public PrintRendererConfigurator(FOUserAgent userAgent, RendererConfigParser rendererConfigParser,
  60. FontConfigurator<EmbedFontInfo> fontInfoConfigurator) {
  61. super(userAgent);
  62. this.rendererConfigParser = rendererConfigParser;
  63. this.fontInfoConfigurator = fontInfoConfigurator;
  64. }
  65. /**
  66. * Returns the renderer configuration data for a specific renderer.
  67. *
  68. * @param documentHandler the document handler
  69. * @return the renderer configuration data
  70. * @throws FOPException if an error occurs
  71. */
  72. protected RendererConfig getRendererConfig(IFDocumentHandler documentHandler) throws FOPException {
  73. return getRendererConfig(documentHandler.getMimeType());
  74. }
  75. /**
  76. * gets the renderer configuration data for a specific renderer.
  77. *
  78. * @param mimeType the MIME type
  79. * @return the renderer configuration data
  80. * @throws FOPException if an error occurs
  81. */
  82. protected RendererConfig getRendererConfig(String mimeType) throws FOPException {
  83. return userAgent.getRendererConfig(mimeType, rendererConfigParser);
  84. }
  85. /**
  86. * gets the renderer configuration data for a specific renderer.
  87. *
  88. * @param renderer the renderer
  89. * @return the renderer configuration data
  90. * @throws FOPException if an error occurs
  91. */
  92. protected RendererConfig getRendererConfig(Renderer renderer) throws FOPException {
  93. return getRendererConfig(renderer.getMimeType());
  94. }
  95. /**
  96. * Builds a list of EmbedFontInfo objects for use with the setup() method.
  97. *
  98. * @param renderer print renderer
  99. * @throws FOPException if something's wrong with the config data
  100. */
  101. public void configure(Renderer renderer) throws FOPException {
  102. PrintRenderer printRenderer = (PrintRenderer) renderer;
  103. List<EmbedFontInfo> embedFontInfoList = buildFontList(renderer.getMimeType());
  104. printRenderer.addFontList(embedFontInfoList);
  105. }
  106. /** {@inheritDoc} */
  107. public void configure(IFDocumentHandler documentHandler) throws FOPException {
  108. //nop
  109. }
  110. /** {@inheritDoc} */
  111. public void setupFontInfo(String mimeType, FontInfo fontInfo) throws FOPException {
  112. FontManager fontManager = userAgent.getFontManager();
  113. List<FontCollection> fontCollections = getDefaultFontCollection();
  114. fontCollections.add(getCustomFontCollection(fontManager.getResourceResolver(), mimeType));
  115. fontManager.setup(fontInfo, fontCollections.toArray(new FontCollection[fontCollections.size()]));
  116. }
  117. protected abstract List<FontCollection> getDefaultFontCollection();
  118. /**
  119. * Returns the font collection for custom configured fonts.
  120. *
  121. * @param resolver the resource resolver
  122. * @param mimeType the renderer MIME type
  123. * @return the font collection
  124. * @throws FOPException if an error occurs
  125. */
  126. protected FontCollection getCustomFontCollection(InternalResourceResolver resolver, String mimeType)
  127. throws FOPException {
  128. List<EmbedFontInfo> fontList;
  129. if (rendererConfigParser == null) {
  130. fontList = Collections.<EmbedFontInfo>emptyList();
  131. } else {
  132. fontList = fontInfoConfigurator.configure(getRendererConfig(mimeType).getFontInfoConfig());
  133. }
  134. return createCollectionFromFontList(resolver, fontList);
  135. }
  136. /***
  137. * Creates the font collection given a list of embedded font infomation.
  138. *
  139. * @param resolver the resource resolver
  140. * @param fontList the embedded font infomation
  141. * @return the font collection
  142. */
  143. protected FontCollection createCollectionFromFontList(InternalResourceResolver resolver,
  144. List<EmbedFontInfo> fontList) {
  145. return new CustomFontCollection(resolver, fontList,
  146. userAgent.isComplexScriptFeaturesEnabled());
  147. }
  148. private List<EmbedFontInfo> buildFontList(String mimeType) throws FOPException {
  149. return fontInfoConfigurator.configure(getRendererConfig(mimeType).getFontInfoConfig());
  150. }
  151. }