Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

BitmapRendererConfig.java 6.8KB

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