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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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.isGocaWrapPseg() != null) {
  86. documentHandler.setWrapGocaPSeg(config.isGocaWrapPseg());
  87. }
  88. if (config.isFs45() != null) {
  89. documentHandler.setFS45(config.isFs45());
  90. }
  91. if (config.allowJpegEmbedding() != null) {
  92. documentHandler.canEmbedJpeg(config.allowJpegEmbedding());
  93. }
  94. if (config.getBitmapEncodingQuality() != null) {
  95. documentHandler.setBitmapEncodingQuality(config.getBitmapEncodingQuality());
  96. }
  97. if (config.getLineWidthCorrection() != null) {
  98. documentHandler.setLineWidthCorrection(config.getLineWidthCorrection());
  99. }
  100. if (config.isGocaEnabled() != null) {
  101. documentHandler.setGOCAEnabled(config.isGocaEnabled());
  102. }
  103. if (config.isStrokeGocaText() != null) {
  104. documentHandler.setStrokeGOCAText(config.isStrokeGocaText());
  105. }
  106. if (config.getDefaultResourceGroupUri() != null) {
  107. documentHandler.setDefaultResourceGroupUri(config.getDefaultResourceGroupUri());
  108. }
  109. AFPResourceLevelDefaults resourceLevelDefaults = config.getResourceLevelDefaults();
  110. if (resourceLevelDefaults != null) {
  111. documentHandler.setResourceLevelDefaults(resourceLevelDefaults);
  112. }
  113. }
  114. @Override
  115. protected List<FontCollection> getDefaultFontCollection() {
  116. return new ArrayList<FontCollection>();
  117. }
  118. @Override
  119. protected FontCollection getCustomFontCollection(InternalResourceResolver uriResolverWrapper,
  120. String mimeType) throws FOPException {
  121. AFPRendererConfig config = (AFPRendererConfig) getRendererConfig(mimeType);
  122. if (config != null) {
  123. try {
  124. return new AFPFontCollection(userAgent.getEventBroadcaster(), createFontsList(
  125. config.getFontInfoConfig(), mimeType));
  126. } catch (IOException e) {
  127. eventProducer.invalidConfiguration(this, e);
  128. LogUtil.handleException(log, e, userAgent.validateUserConfigStrictly());
  129. } catch (IllegalArgumentException iae) {
  130. eventProducer.invalidConfiguration(this, iae);
  131. LogUtil.handleException(log, iae, userAgent.validateUserConfigStrictly());
  132. }
  133. }
  134. return new AFPFontCollection(userAgent.getEventBroadcaster(), null);
  135. }
  136. private List<AFPFontInfo> createFontsList(AFPFontConfig fontConfig, String mimeType)
  137. throws FOPException, IOException {
  138. List<AFPFontInfo> afpFonts = new ArrayList<AFPFontInfo>();
  139. for (AFPFontConfigData config : fontConfig.getFontConfig()) {
  140. afpFonts.add(config.getFontInfo(userAgent.getFontManager().getResourceResolver(),
  141. eventProducer));
  142. }
  143. return afpFonts;
  144. }
  145. }