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.

PCLRendererConfig.java 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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.pcl;
  19. import java.util.EnumMap;
  20. import java.util.Map;
  21. import org.apache.fop.apps.FOPException;
  22. import org.apache.fop.apps.FOUserAgent;
  23. import org.apache.fop.apps.MimeConstants;
  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 static org.apache.fop.render.pcl.Java2DRendererOption.DISABLE_PJL;
  30. import static org.apache.fop.render.pcl.Java2DRendererOption.MODE_COLOR;
  31. import static org.apache.fop.render.pcl.Java2DRendererOption.OPTIMIZE_RESOURCES;
  32. import static org.apache.fop.render.pcl.Java2DRendererOption.RENDERING_MODE;
  33. import static org.apache.fop.render.pcl.Java2DRendererOption.TEXT_RENDERING;
  34. /**
  35. * The PCL renderer configuration data object.
  36. */
  37. public final class PCLRendererConfig implements RendererConfig {
  38. private final Map<Java2DRendererOption, Object> params
  39. = new EnumMap<Java2DRendererOption, Object>(Java2DRendererOption.class);
  40. private final DefaultFontConfig fontConfig;
  41. private PCLRendererConfig(DefaultFontConfig fontConfig) {
  42. this.fontConfig = fontConfig;
  43. }
  44. public DefaultFontConfig getFontInfoConfig() {
  45. return fontConfig;
  46. }
  47. public PCLRenderingMode getRenderingMode() {
  48. return getParam(RENDERING_MODE, PCLRenderingMode.class);
  49. }
  50. public Boolean isTextRendering() {
  51. return getParam(TEXT_RENDERING, Boolean.class);
  52. }
  53. public Boolean isDisablePjl() {
  54. return getParam(DISABLE_PJL, Boolean.class);
  55. }
  56. public Boolean isColorEnabled() {
  57. return getParam(MODE_COLOR, Boolean.class);
  58. }
  59. public Boolean isOptimizeResources() {
  60. return getParam(OPTIMIZE_RESOURCES, Boolean.class);
  61. }
  62. private <T> T getParam(Java2DRendererOption option, Class<T> type) {
  63. assert option.getType().equals(type);
  64. return type.cast(params.get(option));
  65. }
  66. private <T> void setParam(Java2DRendererOption option, T value) {
  67. assert option.getType().isInstance(value);
  68. params.put(option, value);
  69. }
  70. /**
  71. * The PCL renderer configuration data parser.
  72. */
  73. public static final class PCLRendererConfigParser implements RendererConfigParser {
  74. /** {@inheritDoc} */
  75. public PCLRendererConfig build(FOUserAgent userAgent, Configuration cfg) throws FOPException {
  76. PCLRendererConfig config = new PCLRendererConfig(new DefaultFontConfigParser()
  77. .parse(cfg, userAgent.validateStrictly(),
  78. new FontEventAdapter(userAgent.getEventBroadcaster())));
  79. configure(cfg, config);
  80. return config;
  81. }
  82. private void configure(Configuration cfg, PCLRendererConfig config) throws FOPException {
  83. if (cfg != null) {
  84. Configuration imagesCfg = cfg.getChild("images");
  85. String imageMode = imagesCfg.getAttribute("mode", null);
  86. if ("color".equalsIgnoreCase(imageMode)) {
  87. config.setParam(MODE_COLOR, true);
  88. }
  89. String rendering = cfg.getChild(RENDERING_MODE.getName()).getValue(null);
  90. if (rendering != null) {
  91. try {
  92. config.setParam(RENDERING_MODE, PCLRenderingMode.getValueOf(rendering));
  93. } catch (IllegalArgumentException e) {
  94. throw new FOPException("Valid values for 'rendering' are 'quality', 'speed' and 'bitmap'."
  95. + " Value found: " + rendering);
  96. }
  97. }
  98. String textRendering = cfg.getChild(TEXT_RENDERING.getName()).getValue(null);
  99. if ("bitmap".equalsIgnoreCase(textRendering)) {
  100. config.setParam(TEXT_RENDERING, true);
  101. } else if (textRendering == null || "auto".equalsIgnoreCase(textRendering)) {
  102. config.setParam(TEXT_RENDERING, false);
  103. } else {
  104. throw new FOPException(
  105. "Valid values for 'text-rendering' are 'auto' and 'bitmap'. Value found: "
  106. + textRendering);
  107. }
  108. config.setParam(DISABLE_PJL,
  109. cfg.getChild(DISABLE_PJL.getName()).getValueAsBoolean(false));
  110. config.setParam(OPTIMIZE_RESOURCES,
  111. cfg.getChild(OPTIMIZE_RESOURCES.getName()).getValueAsBoolean(false));
  112. }
  113. }
  114. /** {@inheritDoc} */
  115. public String getMimeType() {
  116. return MimeConstants.MIME_PCL;
  117. }
  118. }
  119. }