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.

BitmapRendererConfigurator.java 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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.bitmap;
  19. import java.awt.Graphics2D;
  20. import java.awt.image.BufferedImage;
  21. import java.util.List;
  22. import org.apache.avalon.framework.configuration.Configuration;
  23. import org.apache.fop.apps.FOPException;
  24. import org.apache.fop.apps.FOUserAgent;
  25. import org.apache.fop.fonts.FontCollection;
  26. import org.apache.fop.fonts.FontEventAdapter;
  27. import org.apache.fop.fonts.FontEventListener;
  28. import org.apache.fop.fonts.FontInfo;
  29. import org.apache.fop.fonts.FontManager;
  30. import org.apache.fop.fonts.FontResolver;
  31. import org.apache.fop.render.DefaultFontResolver;
  32. import org.apache.fop.render.intermediate.IFDocumentHandler;
  33. import org.apache.fop.render.intermediate.IFDocumentHandlerConfigurator;
  34. import org.apache.fop.render.java2d.Base14FontCollection;
  35. import org.apache.fop.render.java2d.ConfiguredFontCollection;
  36. import org.apache.fop.render.java2d.InstalledFontCollection;
  37. import org.apache.fop.render.java2d.Java2DFontMetrics;
  38. import org.apache.fop.render.java2d.Java2DRenderer;
  39. import org.apache.fop.render.java2d.Java2DRendererConfigurator;
  40. import org.apache.fop.util.ColorUtil;
  41. /**
  42. * Configurator for bitmap output.
  43. */
  44. public class BitmapRendererConfigurator extends Java2DRendererConfigurator
  45. implements IFDocumentHandlerConfigurator {
  46. /**
  47. * Default constructor
  48. * @param userAgent user agent
  49. */
  50. public BitmapRendererConfigurator(FOUserAgent userAgent) {
  51. super(userAgent);
  52. }
  53. // ---=== IFDocumentHandler configuration ===---
  54. /** {@inheritDoc} */
  55. public void configure(IFDocumentHandler documentHandler) throws FOPException {
  56. super.configure(documentHandler);
  57. Configuration cfg = super.getRendererConfig(documentHandler.getMimeType());
  58. if (cfg != null) {
  59. AbstractBitmapDocumentHandler bitmapHandler
  60. = (AbstractBitmapDocumentHandler)documentHandler;
  61. BitmapRenderingSettings settings = bitmapHandler.getSettings();
  62. boolean transparent = cfg.getChild(
  63. Java2DRenderer.JAVA2D_TRANSPARENT_PAGE_BACKGROUND).getValueAsBoolean(
  64. settings.hasTransparentPageBackground());
  65. if (transparent) {
  66. settings.setPageBackgroundColor(null);
  67. } else {
  68. String background = cfg.getChild("background-color").getValue(null);
  69. if (background != null) {
  70. settings.setPageBackgroundColor(
  71. ColorUtil.parseColorString(this.userAgent, background));
  72. }
  73. }
  74. boolean antiAliasing = cfg.getChild("anti-aliasing").getValueAsBoolean(
  75. settings.isAntiAliasingEnabled());
  76. settings.setAntiAliasing(antiAliasing);
  77. String optimization = cfg.getChild("rendering").getValue(null);
  78. if ("quality".equalsIgnoreCase(optimization)) {
  79. settings.setQualityRendering(true);
  80. } else if ("speed".equalsIgnoreCase(optimization)) {
  81. settings.setQualityRendering(false);
  82. }
  83. String color = cfg.getChild("color-mode").getValue(null);
  84. if (color != null) {
  85. if ("rgba".equalsIgnoreCase(color)) {
  86. settings.setBufferedImageType(BufferedImage.TYPE_INT_ARGB);
  87. } else if ("rgb".equalsIgnoreCase(color)) {
  88. settings.setBufferedImageType(BufferedImage.TYPE_INT_RGB);
  89. } else if ("gray".equalsIgnoreCase(color)) {
  90. settings.setBufferedImageType(BufferedImage.TYPE_BYTE_GRAY);
  91. } else if ("binary".equalsIgnoreCase(color)) {
  92. settings.setBufferedImageType(BufferedImage.TYPE_BYTE_BINARY);
  93. } else if ("bi-level".equalsIgnoreCase(color)) {
  94. settings.setBufferedImageType(BufferedImage.TYPE_BYTE_BINARY);
  95. } else {
  96. throw new FOPException("Invalid value for color-mode: " + color);
  97. }
  98. }
  99. }
  100. }
  101. /** {@inheritDoc} */
  102. public void setupFontInfo(IFDocumentHandler documentHandler, FontInfo fontInfo)
  103. throws FOPException {
  104. final FontManager fontManager = userAgent.getFactory().getFontManager();
  105. final Java2DFontMetrics java2DFontMetrics = new Java2DFontMetrics();
  106. final List fontCollections = new java.util.ArrayList();
  107. fontCollections.add(new Base14FontCollection(java2DFontMetrics));
  108. fontCollections.add(new InstalledFontCollection(java2DFontMetrics));
  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 fontList = buildFontList(cfg, fontResolver, listener);
  115. fontCollections.add(new ConfiguredFontCollection(fontResolver, fontList));
  116. }
  117. fontManager.setup(fontInfo,
  118. (FontCollection[])fontCollections.toArray(
  119. new FontCollection[fontCollections.size()]));
  120. documentHandler.setFontInfo(fontInfo);
  121. }
  122. }