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.

AFPImageHandlerGraphics2D.java 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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.awt.Rectangle;
  20. import java.io.IOException;
  21. import org.apache.xmlgraphics.image.loader.Image;
  22. import org.apache.xmlgraphics.image.loader.ImageFlavor;
  23. import org.apache.xmlgraphics.image.loader.impl.ImageGraphics2D;
  24. import org.apache.xmlgraphics.java2d.Graphics2DImagePainter;
  25. import org.apache.xmlgraphics.util.MimeConstants;
  26. import org.apache.fop.afp.AFPDataObjectInfo;
  27. import org.apache.fop.afp.AFPGraphics2D;
  28. import org.apache.fop.afp.AFPGraphicsObjectInfo;
  29. import org.apache.fop.afp.AFPPaintingState;
  30. import org.apache.fop.afp.AFPResourceInfo;
  31. import org.apache.fop.afp.AFPResourceManager;
  32. import org.apache.fop.afp.modca.ResourceObject;
  33. import org.apache.fop.render.ImageHandler;
  34. import org.apache.fop.render.ImageHandlerUtil;
  35. import org.apache.fop.render.RenderingContext;
  36. /**
  37. * PDFImageHandler implementation which handles Graphics2D images.
  38. */
  39. public class AFPImageHandlerGraphics2D extends AFPImageHandler implements ImageHandler {
  40. private static final ImageFlavor[] FLAVORS = new ImageFlavor[] {
  41. ImageFlavor.GRAPHICS2D
  42. };
  43. /** {@inheritDoc} */
  44. public AFPDataObjectInfo generateDataObjectInfo(
  45. AFPRendererImageInfo rendererImageInfo) throws IOException {
  46. AFPRendererContext rendererContext
  47. = (AFPRendererContext)rendererImageInfo.getRendererContext();
  48. AFPInfo afpInfo = rendererContext.getInfo();
  49. ImageGraphics2D imageG2D = (ImageGraphics2D)rendererImageInfo.getImage();
  50. Graphics2DImagePainter painter = imageG2D.getGraphics2DImagePainter();
  51. if (afpInfo.paintAsBitmap()) {
  52. int x = afpInfo.getX();
  53. int y = afpInfo.getY();
  54. int width = afpInfo.getWidth();
  55. int height = afpInfo.getHeight();
  56. AFPPaintingState paintingState = afpInfo.getPaintingState();
  57. AFPGraphics2DAdapter g2dAdapter = new AFPGraphics2DAdapter(paintingState);
  58. g2dAdapter.paintImage(painter, rendererContext, x, y, width, height);
  59. return null;
  60. } else {
  61. AFPGraphicsObjectInfo graphicsObjectInfo
  62. = (AFPGraphicsObjectInfo)super.generateDataObjectInfo(rendererImageInfo);
  63. setDefaultResourceLevel(graphicsObjectInfo, afpInfo.getResourceManager());
  64. // set mime type (unsupported by MOD:CA registry)
  65. graphicsObjectInfo.setMimeType(MimeConstants.MIME_AFP_GOCA);
  66. // set g2d
  67. boolean textAsShapes = false;
  68. AFPGraphics2D g2d = afpInfo.createGraphics2D(textAsShapes);
  69. graphicsObjectInfo.setGraphics2D(g2d);
  70. // set painter
  71. graphicsObjectInfo.setPainter(painter);
  72. return graphicsObjectInfo;
  73. }
  74. }
  75. private void setDefaultResourceLevel(AFPGraphicsObjectInfo graphicsObjectInfo,
  76. AFPResourceManager resourceManager) {
  77. AFPResourceInfo resourceInfo = graphicsObjectInfo.getResourceInfo();
  78. if (!resourceInfo.levelChanged()) {
  79. resourceInfo.setLevel(resourceManager.getResourceLevelDefaults()
  80. .getDefaultResourceLevel(ResourceObject.TYPE_GRAPHIC));
  81. }
  82. }
  83. /** {@inheritDoc} */
  84. public int getPriority() {
  85. return 200;
  86. }
  87. /** {@inheritDoc} */
  88. public Class getSupportedImageClass() {
  89. return ImageGraphics2D.class;
  90. }
  91. /** {@inheritDoc} */
  92. public ImageFlavor[] getSupportedImageFlavors() {
  93. return FLAVORS;
  94. }
  95. /** {@inheritDoc} */
  96. protected AFPDataObjectInfo createDataObjectInfo() {
  97. return new AFPGraphicsObjectInfo();
  98. }
  99. /** {@inheritDoc} */
  100. public void handleImage(RenderingContext context, Image image, Rectangle pos)
  101. throws IOException {
  102. AFPRenderingContext afpContext = (AFPRenderingContext)context;
  103. AFPGraphicsObjectInfo graphicsObjectInfo = (AFPGraphicsObjectInfo)createDataObjectInfo();
  104. // set resource information
  105. setResourceInformation(graphicsObjectInfo,
  106. image.getInfo().getOriginalURI(),
  107. afpContext.getForeignAttributes());
  108. // Positioning
  109. graphicsObjectInfo.setObjectAreaInfo(
  110. createObjectAreaInfo(afpContext.getPaintingState(), pos));
  111. setDefaultResourceLevel(graphicsObjectInfo, afpContext.getResourceManager());
  112. // Image content
  113. ImageGraphics2D imageG2D = (ImageGraphics2D)image;
  114. boolean textAsShapes = false; //TODO Make configurable
  115. AFPGraphics2D g2d = new AFPGraphics2D(
  116. textAsShapes,
  117. afpContext.getPaintingState(),
  118. afpContext.getResourceManager(),
  119. graphicsObjectInfo.getResourceInfo(),
  120. afpContext.getFontInfo());
  121. g2d.setGraphicContext(new org.apache.xmlgraphics.java2d.GraphicContext());
  122. graphicsObjectInfo.setGraphics2D(g2d);
  123. graphicsObjectInfo.setPainter(imageG2D.getGraphics2DImagePainter());
  124. // Create image
  125. afpContext.getResourceManager().createObject(graphicsObjectInfo);
  126. }
  127. /** {@inheritDoc} */
  128. public boolean isCompatible(RenderingContext targetContext, Image image) {
  129. boolean supported = (image == null || image instanceof ImageGraphics2D)
  130. && targetContext instanceof AFPRenderingContext;
  131. if (supported) {
  132. String mode = (String)targetContext.getHint(ImageHandlerUtil.CONVERSION_MODE);
  133. if (ImageHandlerUtil.isConversionModeBitmap(mode)) {
  134. //Disabling this image handler automatically causes a bitmap to be generated
  135. return false;
  136. }
  137. }
  138. return supported;
  139. }
  140. }