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.6KB

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