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.

AFPRendererConfigurator.java 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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.afp;
  19. import java.io.IOException;
  20. import java.util.ArrayList;
  21. import java.util.List;
  22. import org.apache.commons.logging.Log;
  23. import org.apache.commons.logging.LogFactory;
  24. import org.apache.fop.afp.AFPEventProducer;
  25. import org.apache.fop.afp.AFPResourceLevelDefaults;
  26. import org.apache.fop.afp.fonts.AFPFontCollection;
  27. import org.apache.fop.afp.fonts.AFPFontInfo;
  28. import org.apache.fop.apps.FOPException;
  29. import org.apache.fop.apps.FOUserAgent;
  30. import org.apache.fop.apps.io.InternalResourceResolver;
  31. import org.apache.fop.fonts.FontCollection;
  32. import org.apache.fop.render.PrintRendererConfigurator;
  33. import org.apache.fop.render.RendererConfig.RendererConfigParser;
  34. import org.apache.fop.render.afp.AFPFontConfig.AFPFontConfigData;
  35. import org.apache.fop.render.intermediate.IFDocumentHandler;
  36. import org.apache.fop.render.intermediate.IFDocumentHandlerConfigurator;
  37. import org.apache.fop.util.LogUtil;
  38. /**
  39. * AFP Renderer configurator
  40. */
  41. public class AFPRendererConfigurator extends PrintRendererConfigurator implements
  42. IFDocumentHandlerConfigurator {
  43. private static Log log = LogFactory.getLog(AFPRendererConfigurator.class);
  44. private final AFPEventProducer eventProducer;
  45. /**
  46. * Default constructor
  47. *
  48. * @param userAgent user agent
  49. */
  50. public AFPRendererConfigurator(FOUserAgent userAgent, RendererConfigParser rendererConfigParser) {
  51. super(userAgent, rendererConfigParser);
  52. eventProducer = AFPEventProducer.Provider.get(userAgent.getEventBroadcaster());
  53. }
  54. @Override
  55. public void configure(IFDocumentHandler documentHandler) throws FOPException {
  56. AFPRendererConfig config = (AFPRendererConfig) getRendererConfig(documentHandler);
  57. if (config != null) {
  58. AFPDocumentHandler afpDocumentHandler = (AFPDocumentHandler) documentHandler;
  59. configure(afpDocumentHandler, config);
  60. }
  61. }
  62. private void configure(AFPDocumentHandler documentHandler, AFPRendererConfig config) {
  63. Boolean colorImages = config.isColorImages();
  64. if (colorImages != null) {
  65. documentHandler.setColorImages(colorImages);
  66. if (colorImages) {
  67. documentHandler.setCMYKImagesSupported(config.isCmykImagesSupported());
  68. } else {
  69. documentHandler.setBitsPerPixel(config.getBitsPerPixel());
  70. }
  71. }
  72. if (config.getDitheringQuality() != null) {
  73. documentHandler.setDitheringQuality(config.getDitheringQuality());
  74. }
  75. if (config.isNativeImagesSupported() != null) {
  76. documentHandler.setNativeImagesSupported(config.isNativeImagesSupported());
  77. }
  78. if (config.getShadingMode() != null) {
  79. documentHandler.setShadingMode(config.getShadingMode());
  80. }
  81. if (config.getResolution() != null) {
  82. documentHandler.setResolution(config.getResolution());
  83. }
  84. if (config.isWrapPseg() != null) {
  85. documentHandler.setWrapPSeg(config.isWrapPseg());
  86. }
  87. if (config.isFs45() != null) {
  88. documentHandler.setFS45(config.isFs45());
  89. }
  90. if (config.allowJpegEmbedding() != null) {
  91. documentHandler.canEmbedJpeg(config.allowJpegEmbedding());
  92. }
  93. if (config.getBitmapEncodingQuality() != null) {
  94. documentHandler.setBitmapEncodingQuality(config.getBitmapEncodingQuality());
  95. }
  96. if (config.getLineWidthCorrection() != null) {
  97. documentHandler.setLineWidthCorrection(config.getLineWidthCorrection());
  98. }
  99. if (config.isGocaEnabled() != null) {
  100. documentHandler.setGOCAEnabled(config.isGocaEnabled());
  101. }
  102. if (config.isStrokeGocaText() != null) {
  103. documentHandler.setStrokeGOCAText(config.isStrokeGocaText());
  104. }
  105. if (config.getDefaultResourceGroupUri() != null) {
  106. documentHandler.setDefaultResourceGroupUri(config.getDefaultResourceGroupUri());
  107. }
  108. AFPResourceLevelDefaults resourceLevelDefaults = config.getResourceLevelDefaults();
  109. if (resourceLevelDefaults != null) {
  110. documentHandler.setResourceLevelDefaults(resourceLevelDefaults);
  111. }
  112. }
  113. @Override
  114. protected List<FontCollection> getDefaultFontCollection() {
  115. return new ArrayList<FontCollection>();
  116. }
  117. @Override
  118. protected FontCollection getCustomFontCollection(InternalResourceResolver uriResolverWrapper,
  119. String mimeType) throws FOPException {
  120. AFPRendererConfig config = (AFPRendererConfig) getRendererConfig(mimeType);
  121. if (config != null) {
  122. try {
  123. return new AFPFontCollection(userAgent.getEventBroadcaster(), createFontsList(
  124. config.getFontInfoConfig(), mimeType));
  125. } catch (IOException e) {
  126. eventProducer.invalidConfiguration(this, e);
  127. LogUtil.handleException(log, e, userAgent.validateUserConfigStrictly());
  128. } catch (IllegalArgumentException iae) {
  129. eventProducer.invalidConfiguration(this, iae);
  130. LogUtil.handleException(log, iae, userAgent.validateUserConfigStrictly());
  131. }
  132. }
  133. return new AFPFontCollection(userAgent.getEventBroadcaster(), null);
  134. }
  135. private List<AFPFontInfo> createFontsList(AFPFontConfig fontConfig, String mimeType)
  136. throws FOPException, IOException {
  137. List<AFPFontInfo> afpFonts = new ArrayList<AFPFontInfo>();
  138. for (AFPFontConfigData config : fontConfig.getFontConfig()) {
  139. afpFonts.add(config.getFontInfo(userAgent.getFontManager().getResourceResolver(),
  140. eventProducer));
  141. }
  142. return afpFonts;
  143. }
  144. }