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.

PSImageHandlerRenderedImage.java 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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.ps;
  19. import java.awt.Rectangle;
  20. import java.awt.image.RenderedImage;
  21. import java.io.IOException;
  22. import org.apache.xmlgraphics.image.loader.Image;
  23. import org.apache.xmlgraphics.image.loader.ImageFlavor;
  24. import org.apache.xmlgraphics.image.loader.ImageInfo;
  25. import org.apache.xmlgraphics.image.loader.impl.ImageRendered;
  26. import org.apache.xmlgraphics.ps.FormGenerator;
  27. import org.apache.xmlgraphics.ps.ImageFormGenerator;
  28. import org.apache.xmlgraphics.ps.PSGenerator;
  29. import org.apache.xmlgraphics.ps.PSImageUtils;
  30. import org.apache.fop.render.RenderingContext;
  31. /**
  32. * Image handler implementation which handles RenderedImage instances for PostScript output.
  33. */
  34. public class PSImageHandlerRenderedImage implements PSImageHandler {
  35. private static final ImageFlavor[] FLAVORS = new ImageFlavor[] {
  36. ImageFlavor.BUFFERED_IMAGE,
  37. ImageFlavor.RENDERED_IMAGE
  38. };
  39. /** {@inheritDoc} */
  40. public void handleImage(RenderingContext context, Image image, Rectangle pos)
  41. throws IOException {
  42. PSRenderingContext psContext = (PSRenderingContext)context;
  43. PSGenerator gen = psContext.getGenerator();
  44. ImageRendered imageRend = (ImageRendered)image;
  45. float x = (float)pos.getX() / 1000f;
  46. float y = (float)pos.getY() / 1000f;
  47. float w = (float)pos.getWidth() / 1000f;
  48. float h = (float)pos.getHeight() / 1000f;
  49. RenderedImage ri = imageRend.getRenderedImage();
  50. PSImageUtils.renderBitmapImage(ri, x, y, w, h, gen);
  51. }
  52. /** {@inheritDoc} */
  53. public void generateForm(RenderingContext context, Image image, PSImageFormResource form)
  54. throws IOException {
  55. PSRenderingContext psContext = (PSRenderingContext)context;
  56. PSGenerator gen = psContext.getGenerator();
  57. ImageRendered imageRend = (ImageRendered)image;
  58. ImageInfo info = image.getInfo();
  59. String imageDescription = info.getMimeType() + " " + info.getOriginalURI();
  60. RenderedImage ri = imageRend.getRenderedImage();
  61. FormGenerator formGen = new ImageFormGenerator(
  62. form.getName(), imageDescription,
  63. info.getSize().getDimensionPt(),
  64. ri, false);
  65. formGen.generate(gen);
  66. }
  67. /** {@inheritDoc} */
  68. public int getPriority() {
  69. return 300;
  70. }
  71. /** {@inheritDoc} */
  72. public Class getSupportedImageClass() {
  73. return ImageRendered.class;
  74. }
  75. /** {@inheritDoc} */
  76. public ImageFlavor[] getSupportedImageFlavors() {
  77. return FLAVORS;
  78. }
  79. /** {@inheritDoc} */
  80. public boolean isCompatible(RenderingContext targetContext, Image image) {
  81. return (image == null || image instanceof ImageRendered)
  82. && targetContext instanceof PSRenderingContext;
  83. }
  84. }