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

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