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.

AFPImageGraphics2DFactory.java 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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.batik.bridge.BridgeContext;
  23. import org.apache.fop.afp.AFPDataObjectInfo;
  24. import org.apache.fop.afp.AFPGraphics2D;
  25. import org.apache.fop.afp.AFPGraphicsObjectInfo;
  26. import org.apache.fop.afp.AFPObjectAreaInfo;
  27. import org.apache.fop.afp.AFPPaintingState;
  28. import org.apache.fop.afp.AFPResourceInfo;
  29. import org.apache.fop.afp.AFPResourceLevel;
  30. import org.apache.fop.afp.AFPTextElementBridge;
  31. import org.apache.fop.afp.AFPTextHandler;
  32. import org.apache.fop.afp.AFPTextPainter;
  33. import org.apache.fop.render.RendererContext;
  34. import org.apache.fop.svg.SVGUserAgent;
  35. import org.apache.xmlgraphics.image.loader.impl.ImageGraphics2D;
  36. import org.apache.xmlgraphics.java2d.Graphics2DImagePainter;
  37. import org.apache.xmlgraphics.util.MimeConstants;
  38. /**
  39. * An AFP image graphics 2d factory
  40. */
  41. public class AFPImageGraphics2DFactory extends AFPDataObjectInfoFactory {
  42. /**
  43. * Main constructor
  44. *
  45. * @param state the AFP painting state
  46. */
  47. public AFPImageGraphics2DFactory(AFPPaintingState state) {
  48. super(state);
  49. }
  50. /** {@inheritDoc} */
  51. protected AFPDataObjectInfo createDataObjectInfo() {
  52. return new AFPGraphicsObjectInfo();
  53. }
  54. /** {@inheritDoc} */
  55. public AFPDataObjectInfo create(AFPRendererImageInfo rendererImageInfo) throws IOException {
  56. AFPGraphicsObjectInfo graphicsObjectInfo
  57. = (AFPGraphicsObjectInfo)super.create(rendererImageInfo);
  58. AFPResourceInfo resourceInfo = graphicsObjectInfo.getResourceInfo();
  59. // level not explicitly set/changed so default to inline for GOCA graphic objects
  60. // (due to a bug in the IBM AFP Workbench Viewer (2.04.01.07) - hard copy works just fine)
  61. if (!resourceInfo.levelChanged()) {
  62. resourceInfo.setLevel(new AFPResourceLevel(AFPResourceLevel.INLINE));
  63. }
  64. // set mime type (unsupported by MOD:CA registry)
  65. graphicsObjectInfo.setMimeType(MimeConstants.MIME_AFP_GOCA);
  66. // set graphics 2d
  67. AFPGraphics2DAdapter g2dAdapter = rendererImageInfo.getGraphics2DAdapter();
  68. AFPGraphics2D g2d = g2dAdapter.getGraphics2D();
  69. graphicsObjectInfo.setGraphics2D(g2d);
  70. // set resource, state and font info
  71. RendererContext rendererContext = rendererImageInfo.getRendererContext();
  72. AFPInfo afpInfo = AFPSVGHandler.getAFPInfo(rendererContext);
  73. g2d.setResourceManager(afpInfo.getResourceManager());
  74. g2d.setResourceInfo(afpInfo.getResourceInfo());
  75. g2d.setPaintingState(afpInfo.getPaintingState());
  76. g2d.setFontInfo(afpInfo.getFontInfo());
  77. // set to default graphic context
  78. g2d.setGraphicContext(new org.apache.xmlgraphics.java2d.GraphicContext());
  79. // translate to current location
  80. AffineTransform at = state.getData().getTransform();
  81. g2d.translate(at.getTranslateX(), at.getTranslateY());
  82. // set afp state
  83. g2d.setPaintingState(state);
  84. // controls whether text painted by Batik is generated using text or path operations
  85. SVGUserAgent svgUserAgent
  86. = new SVGUserAgent(rendererContext.getUserAgent(), new AffineTransform());
  87. BridgeContext ctx = new BridgeContext(svgUserAgent);
  88. if (!afpInfo.strokeText()) {
  89. AFPTextHandler textHandler = new AFPTextHandler(g2d);
  90. g2d.setCustomTextHandler(textHandler);
  91. AFPTextPainter textPainter = new AFPTextPainter(textHandler);
  92. ctx.setTextPainter(textPainter);
  93. AFPTextElementBridge textElementBridge = new AFPTextElementBridge(textPainter);
  94. ctx.putBridge(textElementBridge);
  95. }
  96. // set painter
  97. ImageGraphics2D imageG2D = (ImageGraphics2D)rendererImageInfo.getImage();
  98. Graphics2DImagePainter painter = imageG2D.getGraphics2DImagePainter();
  99. graphicsObjectInfo.setPainter(painter);
  100. // set object area
  101. AFPObjectAreaInfo objectAreaInfo = graphicsObjectInfo.getObjectAreaInfo();
  102. int width = objectAreaInfo.getWidth();
  103. int height = objectAreaInfo.getHeight();
  104. Rectangle area = new Rectangle(width, height);
  105. graphicsObjectInfo.setArea(area);
  106. // invert y-axis for GOCA
  107. final int sx = 1;
  108. final int sy = -1;
  109. g2d.translate(0, height);
  110. g2d.scale(sx, sy);
  111. return graphicsObjectInfo;
  112. }
  113. }