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.3KB

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