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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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.awt.geom.AffineTransform;
  21. import java.io.IOException;
  22. import org.apache.xmlgraphics.image.loader.Image;
  23. import org.apache.xmlgraphics.image.loader.ImageFlavor;
  24. import org.apache.xmlgraphics.image.loader.impl.ImageGraphics2D;
  25. import org.apache.fop.afp.AFPDataObjectInfo;
  26. import org.apache.fop.afp.AFPGraphics2D;
  27. import org.apache.fop.afp.AFPGraphicsObjectInfo;
  28. import org.apache.fop.afp.AFPPaintingState;
  29. import org.apache.fop.afp.AFPResourceInfo;
  30. import org.apache.fop.afp.AFPResourceManager;
  31. import org.apache.fop.afp.modca.ResourceObject;
  32. import org.apache.fop.render.ImageHandler;
  33. import org.apache.fop.render.ImageHandlerUtil;
  34. import org.apache.fop.render.RenderingContext;
  35. /**
  36. * PDFImageHandler implementation which handles Graphics2D images.
  37. */
  38. public class AFPImageHandlerGraphics2D extends AFPImageHandler implements ImageHandler {
  39. private static final ImageFlavor[] FLAVORS = new ImageFlavor[] {
  40. ImageFlavor.GRAPHICS2D
  41. };
  42. private void setDefaultResourceLevel(AFPGraphicsObjectInfo graphicsObjectInfo,
  43. AFPResourceManager resourceManager) {
  44. AFPResourceInfo resourceInfo = graphicsObjectInfo.getResourceInfo();
  45. if (!resourceInfo.levelChanged()) {
  46. resourceInfo.setLevel(resourceManager.getResourceLevelDefaults()
  47. .getDefaultResourceLevel(ResourceObject.TYPE_GRAPHIC));
  48. }
  49. }
  50. /** {@inheritDoc} */
  51. public int getPriority() {
  52. return 200;
  53. }
  54. /** {@inheritDoc} */
  55. public Class getSupportedImageClass() {
  56. return ImageGraphics2D.class;
  57. }
  58. /** {@inheritDoc} */
  59. public ImageFlavor[] getSupportedImageFlavors() {
  60. return FLAVORS;
  61. }
  62. /** {@inheritDoc} */
  63. @Override
  64. protected AFPDataObjectInfo createDataObjectInfo() {
  65. return new AFPGraphicsObjectInfo();
  66. }
  67. /** {@inheritDoc} */
  68. public void handleImage(RenderingContext context, Image image, Rectangle pos)
  69. throws IOException {
  70. AFPRenderingContext afpContext = (AFPRenderingContext)context;
  71. AFPDataObjectInfo info = createDataObjectInfo();
  72. assert (info instanceof AFPGraphicsObjectInfo);
  73. AFPGraphicsObjectInfo graphicsObjectInfo = (AFPGraphicsObjectInfo) info;
  74. // set resource information
  75. graphicsObjectInfo.setResourceInfo(createResourceInformation(
  76. image.getInfo().getOriginalURI(),
  77. afpContext.getForeignAttributes()));
  78. // Positioning
  79. graphicsObjectInfo.setObjectAreaInfo(
  80. createObjectAreaInfo(afpContext.getPaintingState(), pos));
  81. setDefaultResourceLevel(graphicsObjectInfo, afpContext.getResourceManager());
  82. AFPPaintingState paintingState = afpContext.getPaintingState();
  83. paintingState.save(); // save
  84. AffineTransform placement = new AffineTransform();
  85. placement.translate(pos.x, pos.y);
  86. paintingState.concatenate(placement);
  87. // Image content
  88. ImageGraphics2D imageG2D = (ImageGraphics2D)image;
  89. final boolean textAsShapes = paintingState.isStrokeGOCAText();
  90. AFPGraphics2D g2d = new AFPGraphics2D(
  91. textAsShapes,
  92. afpContext.getPaintingState(),
  93. afpContext.getResourceManager(),
  94. graphicsObjectInfo.getResourceInfo(),
  95. (textAsShapes ? null : afpContext.getFontInfo()));
  96. g2d.setGraphicContext(new org.apache.xmlgraphics.java2d.GraphicContext());
  97. graphicsObjectInfo.setGraphics2D(g2d);
  98. graphicsObjectInfo.setPainter(imageG2D.getGraphics2DImagePainter());
  99. // Create image
  100. afpContext.getResourceManager().createObject(graphicsObjectInfo);
  101. paintingState.restore(); // resume
  102. }
  103. /** {@inheritDoc} */
  104. public boolean isCompatible(RenderingContext targetContext, Image image) {
  105. boolean supported = (image == null || image instanceof ImageGraphics2D)
  106. && targetContext instanceof AFPRenderingContext;
  107. if (supported) {
  108. AFPRenderingContext afpContext = (AFPRenderingContext)targetContext;
  109. if (!afpContext.getPaintingState().isGOCAEnabled()) {
  110. return false;
  111. }
  112. String mode = (String)targetContext.getHint(ImageHandlerUtil.CONVERSION_MODE);
  113. if (ImageHandlerUtil.isConversionModeBitmap(mode)) {
  114. //Disabling this image handler automatically causes a bitmap to be generated
  115. return false;
  116. }
  117. }
  118. return supported;
  119. }
  120. }