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.

AFPDitheredRectanglePainter.java 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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.afp;
  19. import java.awt.Color;
  20. import java.awt.Dimension;
  21. import java.awt.geom.AffineTransform;
  22. import java.awt.geom.Point2D;
  23. import java.io.IOException;
  24. import org.apache.xmlgraphics.image.loader.ImageSize;
  25. import org.apache.xmlgraphics.util.MimeConstants;
  26. import org.apache.fop.afp.AFPResourceLevel.ResourceType;
  27. import org.apache.fop.afp.modca.triplets.MappingOptionTriplet;
  28. import org.apache.fop.util.bitmap.DitherUtil;
  29. /**
  30. * A painter of rectangles in AFP
  31. */
  32. public class AFPDitheredRectanglePainter extends AbstractAFPPainter {
  33. private AFPResourceManager resourceManager;
  34. /**
  35. * Main constructor
  36. *
  37. * @param paintingState the AFP painting state
  38. * @param dataStream the AFP datastream
  39. * @param resourceManager the resource manager
  40. */
  41. public AFPDitheredRectanglePainter(AFPPaintingState paintingState, DataStream dataStream,
  42. AFPResourceManager resourceManager) {
  43. super(paintingState, dataStream);
  44. this.resourceManager = resourceManager;
  45. }
  46. /** {@inheritDoc} */
  47. public void paint(PaintingInfo paintInfo) throws IOException {
  48. RectanglePaintingInfo rectanglePaintInfo = (RectanglePaintingInfo)paintInfo;
  49. if (rectanglePaintInfo.getWidth() <= 0 || rectanglePaintInfo.getHeight() <= 0) {
  50. return;
  51. }
  52. int ditherMatrix = DitherUtil.DITHER_MATRIX_8X8;
  53. Dimension ditherSize = new Dimension(ditherMatrix, ditherMatrix);
  54. //Prepare an FS10 bi-level image
  55. AFPImageObjectInfo imageObjectInfo = new AFPImageObjectInfo();
  56. imageObjectInfo.setMimeType(MimeConstants.MIME_AFP_IOCA_FS10);
  57. //imageObjectInfo.setCreatePageSegment(true);
  58. imageObjectInfo.getResourceInfo().setLevel(new AFPResourceLevel(ResourceType.INLINE));
  59. imageObjectInfo.getResourceInfo().setImageDimension(ditherSize);
  60. imageObjectInfo.setBitsPerPixel(1);
  61. imageObjectInfo.setColor(false);
  62. //Note: the following may not be supported by older implementations
  63. imageObjectInfo.setMappingOption(MappingOptionTriplet.REPLICATE_AND_TRIM);
  64. //Dither image size
  65. int resolution = paintingState.getResolution();
  66. ImageSize ditherBitmapSize = new ImageSize(
  67. ditherSize.width, ditherSize.height, resolution);
  68. imageObjectInfo.setDataHeightRes((int)Math.round(
  69. ditherBitmapSize.getDpiHorizontal() * 10));
  70. imageObjectInfo.setDataWidthRes((int)Math.round(
  71. ditherBitmapSize.getDpiVertical() * 10));
  72. imageObjectInfo.setDataWidth(ditherSize.width);
  73. imageObjectInfo.setDataHeight(ditherSize.height);
  74. //Create dither image
  75. Color col = paintingState.getColor();
  76. byte[] dither = DitherUtil.getBayerDither(ditherMatrix, col, false);
  77. imageObjectInfo.setData(dither);
  78. //Positioning
  79. int rotation = paintingState.getRotation();
  80. AffineTransform at = paintingState.getData().getTransform();
  81. Point2D origin = at.transform(new Point2D.Float(
  82. rectanglePaintInfo.getX() * 1000,
  83. rectanglePaintInfo.getY() * 1000), null);
  84. AFPUnitConverter unitConv = paintingState.getUnitConverter();
  85. float width = unitConv.pt2units(rectanglePaintInfo.getWidth());
  86. float height = unitConv.pt2units(rectanglePaintInfo.getHeight());
  87. AFPObjectAreaInfo objectAreaInfo = new AFPObjectAreaInfo(
  88. (int) Math.round(origin.getX()),
  89. (int) Math.round(origin.getY()),
  90. Math.round(width), Math.round(height), resolution, rotation);
  91. imageObjectInfo.setObjectAreaInfo(objectAreaInfo);
  92. //Create rectangle
  93. resourceManager.createObject(imageObjectInfo);
  94. }
  95. }