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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. protected RendererConfig getRendererConfig(IFDocumentHandler documentHandler) throws FOPException {
  66. return getRendererConfig(documentHandler.getMimeType());
  67. }
  68. protected RendererConfig getRendererConfig(String mimeType) throws FOPException {
  69. return userAgent.getRendererConfig(mimeType, rendererConfigParser);
  70. }
  71. protected RendererConfig getRendererConfig(Renderer renderer) throws FOPException {
  72. return getRendererConfig(renderer.getMimeType());
  73. }
  74. /**
  75. * Builds a list of EmbedFontInfo objects for use with the setup() method.
  76. *
  77. * @param renderer print renderer
  78. * @throws FOPException if something's wrong with the config data
  79. */
  80. public void configure(Renderer renderer) throws FOPException {
  81. PrintRenderer printRenderer = (PrintRenderer) renderer;
  82. List<EmbedFontInfo> embedFontInfoList = buildFontList(renderer.getMimeType());
  83. printRenderer.addFontList(embedFontInfoList);
  84. }
  85. /** {@inheritDoc} */
  86. public void configure(IFDocumentHandler documentHandler) throws FOPException {
  87. //nop
  88. }
  89. /** {@inheritDoc} */
  90. public void setupFontInfo(String mimeType, FontInfo fontInfo) throws FOPException {
  91. FontManager fontManager = userAgent.getFontManager();
  92. List<FontCollection> fontCollections = getDefaultFontCollection();
  93. fontCollections.add(getCustomFontCollection(fontManager.getResourceResolver(), mimeType));
  94. fontManager.setup(fontInfo, fontCollections.toArray(new FontCollection[fontCollections.size()]));
  95. }
  96. protected abstract List<FontCollection> getDefaultFontCollection();
  97. protected FontCollection getCustomFontCollection(InternalResourceResolver resolver, String mimeType)
  98. throws FOPException {
  99. List<EmbedFontInfo> fontList;
  100. if (rendererConfigParser == null) {
  101. fontList = Collections.<EmbedFontInfo>emptyList();
  102. } else {
  103. fontList = fontInfoConfigurator.configure(getRendererConfig(mimeType).getFontInfoConfig());
  104. }
  105. return createCollectionFromFontList(resolver, fontList);
  106. }
  107. protected FontCollection createCollectionFromFontList(InternalResourceResolver resolver,
  108. List<EmbedFontInfo> fontList) {
  109. return new CustomFontCollection(resolver, fontList,
  110. userAgent.isComplexScriptFeaturesEnabled());
  111. }
  112. private List<EmbedFontInfo> buildFontList(String mimeType) throws FOPException {
  113. return fontInfoConfigurator.configure(getRendererConfig(mimeType).getFontInfoConfig());
  114. }
  115. }