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 4.6KB

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