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.

AFPImageHandler.java 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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.Point;
  20. import java.awt.Rectangle;
  21. import java.awt.geom.Rectangle2D;
  22. import java.io.IOException;
  23. import java.util.Map;
  24. import org.apache.fop.afp.AFPDataObjectInfo;
  25. import org.apache.fop.afp.AFPObjectAreaInfo;
  26. import org.apache.fop.afp.AFPPaintingState;
  27. import org.apache.fop.afp.AFPResourceInfo;
  28. import org.apache.fop.afp.AFPUnitConverter;
  29. import org.apache.fop.render.ImageHandlerBase;
  30. import org.apache.fop.render.RendererContext;
  31. /**
  32. * A base abstract AFP image handler
  33. */
  34. public abstract class AFPImageHandler implements ImageHandlerBase {
  35. private static final int X = 0;
  36. private static final int Y = 1;
  37. /** foreign attribute reader */
  38. private static final AFPForeignAttributeReader FOREIGN_ATTRIBUTE_READER
  39. = new AFPForeignAttributeReader();
  40. /**
  41. * Generates an intermediate AFPDataObjectInfo that is later used to construct
  42. * the appropriate data object in the AFP DataStream.
  43. *
  44. * @param rendererImageInfo the renderer image info
  45. * @return a data object info object
  46. * @throws IOException thrown if an I/O exception of some sort has occurred.
  47. */
  48. public AFPDataObjectInfo generateDataObjectInfo(
  49. AFPRendererImageInfo rendererImageInfo) throws IOException {
  50. AFPDataObjectInfo dataObjectInfo = createDataObjectInfo();
  51. // set resource information
  52. dataObjectInfo.setResourceInfo(createResourceInformation(
  53. rendererImageInfo.getURI(),
  54. rendererImageInfo.getForeignAttributes()));
  55. Point origin = rendererImageInfo.getOrigin();
  56. Rectangle2D position = rendererImageInfo.getPosition();
  57. int srcX = Math.round(origin.x + (float)position.getX());
  58. int srcY = Math.round(origin.y + (float)position.getY());
  59. Rectangle targetRect = new Rectangle(
  60. srcX,
  61. srcY,
  62. (int)Math.round(position.getWidth()),
  63. (int)Math.round(position.getHeight()));
  64. RendererContext context = rendererImageInfo.getRendererContext();
  65. assert (context instanceof AFPRendererContext);
  66. AFPRendererContext rendererContext = (AFPRendererContext) context;
  67. AFPInfo afpInfo = rendererContext.getInfo();
  68. AFPPaintingState paintingState = afpInfo.getPaintingState();
  69. dataObjectInfo.setObjectAreaInfo(createObjectAreaInfo(paintingState, targetRect));
  70. return dataObjectInfo;
  71. }
  72. /**
  73. * Sets resource information on the data object info.
  74. * @param uri the image's URI (or null if no URI is available)
  75. * @param foreignAttributes a Map of foreign attributes (or null)
  76. * @return the resource information object
  77. */
  78. public static AFPResourceInfo createResourceInformation(
  79. String uri, Map foreignAttributes) {
  80. AFPResourceInfo resourceInfo
  81. = FOREIGN_ATTRIBUTE_READER.getResourceInfo(foreignAttributes);
  82. resourceInfo.setUri(uri);
  83. return resourceInfo;
  84. }
  85. /**
  86. * Creates and returns an {@link AFPObjectAreaInfo} instance for the placement of the image.
  87. * @param paintingState the painting state
  88. * @param targetRect the target rectangle in which to place the image (coordinates in mpt)
  89. * @return the newly created object area info instance
  90. */
  91. public static AFPObjectAreaInfo createObjectAreaInfo(AFPPaintingState paintingState,
  92. Rectangle targetRect) {
  93. AFPUnitConverter unitConv = paintingState.getUnitConverter();
  94. int[] coords = unitConv.mpts2units(new float[] {targetRect.x, targetRect.y});
  95. int width = (int) Math.ceil(unitConv.mpt2units(targetRect.width));
  96. int height = (int) Math.ceil(unitConv.mpt2units(targetRect.height));
  97. int resolution = paintingState.getResolution();
  98. AFPObjectAreaInfo objectAreaInfo = new AFPObjectAreaInfo(coords[X], coords[Y], width,
  99. height, resolution, paintingState.getRotation());
  100. return objectAreaInfo;
  101. }
  102. /**
  103. * Creates the data object information object
  104. *
  105. * @return the data object information object
  106. */
  107. protected abstract AFPDataObjectInfo createDataObjectInfo();
  108. }