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

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