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.

BitmapRendererConfig.java 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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.Color;
  20. import java.awt.image.BufferedImage;
  21. import java.util.EnumMap;
  22. import org.apache.fop.apps.FOPException;
  23. import org.apache.fop.apps.FOUserAgent;
  24. import org.apache.fop.configuration.Configuration;
  25. import org.apache.fop.fonts.DefaultFontConfig;
  26. import org.apache.fop.fonts.DefaultFontConfig.DefaultFontConfigParser;
  27. import org.apache.fop.fonts.FontEventAdapter;
  28. import org.apache.fop.render.RendererConfig;
  29. import org.apache.fop.render.java2d.Java2DRendererConfig;
  30. import org.apache.fop.render.java2d.Java2DRendererConfig.Java2DRendererConfigParser;
  31. import org.apache.fop.util.ColorUtil;
  32. import static org.apache.fop.render.bitmap.BitmapRendererOption.ANTI_ALIASING;
  33. import static org.apache.fop.render.bitmap.BitmapRendererOption.BACKGROUND_COLOR;
  34. import static org.apache.fop.render.bitmap.BitmapRendererOption.COLOR_MODE;
  35. import static org.apache.fop.render.bitmap.BitmapRendererOption.JAVA2D_TRANSPARENT_PAGE_BACKGROUND;
  36. import static org.apache.fop.render.bitmap.BitmapRendererOption.RENDERING_QUALITY;
  37. import static org.apache.fop.render.bitmap.BitmapRendererOption.RENDERING_QUALITY_ELEMENT;
  38. import static org.apache.fop.render.bitmap.BitmapRendererOption.RENDERING_SPEED;
  39. /**
  40. * The Bitmap renderer config data object.
  41. */
  42. public class BitmapRendererConfig implements RendererConfig {
  43. private final EnumMap<BitmapRendererOption, Object> params
  44. = new EnumMap<BitmapRendererOption, Object>(BitmapRendererOption.class);
  45. private final DefaultFontConfig fontConfig;
  46. BitmapRendererConfig(DefaultFontConfig fontConfig) {
  47. this.fontConfig = fontConfig;
  48. }
  49. public DefaultFontConfig getFontInfoConfig() {
  50. return fontConfig;
  51. }
  52. public Color getBackgroundColor() {
  53. return (Color) get(BACKGROUND_COLOR);
  54. }
  55. public Boolean hasAntiAliasing() {
  56. return (Boolean) get(ANTI_ALIASING);
  57. }
  58. public Boolean isRenderHighQuality() {
  59. return (Boolean) get(RENDERING_QUALITY);
  60. }
  61. public Integer getColorMode() {
  62. return (Integer) get(COLOR_MODE);
  63. }
  64. public boolean hasTransparentBackround() {
  65. Object result = get(JAVA2D_TRANSPARENT_PAGE_BACKGROUND);
  66. return (Boolean) (result != null ? result
  67. : JAVA2D_TRANSPARENT_PAGE_BACKGROUND.getDefaultValue());
  68. }
  69. private Object get(BitmapRendererOption option) {
  70. return params.get(option);
  71. }
  72. /**
  73. * The parser for the Bitmap renderer configuration data.
  74. */
  75. public static class BitmapRendererConfigParser implements RendererConfigParser {
  76. private final String mimeType;
  77. public BitmapRendererConfigParser(String mimeType) {
  78. this.mimeType = mimeType;
  79. }
  80. private void setParam(BitmapRendererConfig config, BitmapRendererOption option,
  81. Object value) {
  82. config.params.put(option, value != null ? value : option.getDefaultValue());
  83. }
  84. void build(BitmapRendererConfig config, FOUserAgent userAgent,
  85. Configuration cfg) throws FOPException {
  86. if (cfg != null) {
  87. Java2DRendererConfig j2dConfig = new Java2DRendererConfigParser(null).build(
  88. userAgent, cfg);
  89. Boolean isTransparent = j2dConfig.isPageBackgroundTransparent();
  90. isTransparent = isTransparent == null
  91. ? (Boolean) JAVA2D_TRANSPARENT_PAGE_BACKGROUND.getDefaultValue()
  92. : isTransparent;
  93. setParam(config, JAVA2D_TRANSPARENT_PAGE_BACKGROUND, isTransparent);
  94. String background = getValue(cfg, BACKGROUND_COLOR);
  95. if (isTransparent) {
  96. // We don't use setParam here because we want to force a null value
  97. config.params.put(BACKGROUND_COLOR, null);
  98. } else {
  99. setParam(config, BACKGROUND_COLOR,
  100. ColorUtil.parseColorString(userAgent, background));
  101. }
  102. setParam(config, BitmapRendererOption.ANTI_ALIASING,
  103. getChild(cfg, ANTI_ALIASING).getValueAsBoolean(
  104. (Boolean) ANTI_ALIASING.getDefaultValue()));
  105. String optimization = getValue(cfg, RENDERING_QUALITY_ELEMENT);
  106. setParam(config, RENDERING_QUALITY,
  107. !(BitmapRendererOption.getValue(optimization) == RENDERING_SPEED));
  108. String color = getValue(cfg, COLOR_MODE);
  109. setParam(config, COLOR_MODE,
  110. getBufferedImageIntegerFromColor(BitmapRendererOption.getValue(color)));
  111. }
  112. }
  113. public BitmapRendererConfig build(FOUserAgent userAgent, Configuration cfg)
  114. throws FOPException {
  115. BitmapRendererConfig config = new BitmapRendererConfig(new DefaultFontConfigParser()
  116. .parse(cfg, userAgent.validateStrictly(),
  117. new FontEventAdapter(userAgent.getEventBroadcaster())));
  118. build(config, userAgent, cfg);
  119. return config;
  120. }
  121. private Integer getBufferedImageIntegerFromColor(BitmapRendererOption option) {
  122. if (option == null) {
  123. return null;
  124. }
  125. switch (option) {
  126. case COLOR_MODE_RGBA:
  127. return BufferedImage.TYPE_INT_ARGB;
  128. case COLOR_MODE_RGB:
  129. return BufferedImage.TYPE_INT_RGB;
  130. case COLOR_MODE_GRAY:
  131. return BufferedImage.TYPE_BYTE_GRAY;
  132. case COLOR_MODE_BINARY:
  133. case COLOR_MODE_BILEVEL:
  134. return BufferedImage.TYPE_BYTE_BINARY;
  135. default:
  136. return null;
  137. }
  138. }
  139. private Configuration getChild(Configuration cfg, BitmapRendererOption option) {
  140. return cfg.getChild(option.getName());
  141. }
  142. private String getValue(Configuration cfg, BitmapRendererOption option) {
  143. Object defaultValue = option.getDefaultValue();
  144. Object result = cfg.getChild(option.getName()).getValue(null);
  145. if (result == null || "".equals(result)) {
  146. result = defaultValue;
  147. }
  148. if (result == null) {
  149. return null;
  150. }
  151. if (result instanceof Color) {
  152. return ColorUtil.colorToString((Color) result);
  153. }
  154. return result.toString();
  155. }
  156. public String getMimeType() {
  157. return mimeType;
  158. }
  159. }
  160. }