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.

PSImageHandlerEPS.java 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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.geom.Point2D;
  21. import java.awt.geom.Rectangle2D;
  22. import java.io.IOException;
  23. import java.io.InputStream;
  24. import org.apache.commons.io.IOUtils;
  25. import org.apache.xmlgraphics.image.loader.Image;
  26. import org.apache.xmlgraphics.image.loader.ImageFlavor;
  27. import org.apache.xmlgraphics.image.loader.ImageInfo;
  28. import org.apache.xmlgraphics.image.loader.impl.ImageRawEPS;
  29. import org.apache.xmlgraphics.ps.PSGenerator;
  30. import org.apache.xmlgraphics.ps.PSImageUtils;
  31. import org.apache.fop.render.ImageHandler;
  32. import org.apache.fop.render.RenderingContext;
  33. /**
  34. * Image handler implementation which handles EPS images for PostScript output.
  35. */
  36. public class PSImageHandlerEPS implements ImageHandler {
  37. private static final ImageFlavor[] FLAVORS = new ImageFlavor[] {
  38. ImageFlavor.RAW_EPS
  39. };
  40. /** {@inheritDoc} */
  41. public void handleImage(RenderingContext context, Image image, Rectangle pos)
  42. throws IOException {
  43. PSRenderingContext psContext = (PSRenderingContext)context;
  44. PSGenerator gen = psContext.getGenerator();
  45. ImageRawEPS eps = (ImageRawEPS)image;
  46. float x = (float)pos.getX() / 1000f;
  47. float y = (float)pos.getY() / 1000f;
  48. float w = (float)pos.getWidth() / 1000f;
  49. float h = (float)pos.getHeight() / 1000f;
  50. ImageInfo info = image.getInfo();
  51. Rectangle2D bbox = eps.getBoundingBox();
  52. if (bbox == null) {
  53. bbox = new Rectangle2D.Double();
  54. bbox.setFrame(new Point2D.Double(), info.getSize().getDimensionPt());
  55. }
  56. InputStream in = eps.createInputStream();
  57. try {
  58. String resourceName = info.getOriginalURI();
  59. if (resourceName == null) {
  60. resourceName = "inline image";
  61. }
  62. PSImageUtils.renderEPS(in, resourceName,
  63. new Rectangle2D.Float(x, y, w, h),
  64. bbox,
  65. gen);
  66. } finally {
  67. IOUtils.closeQuietly(in);
  68. }
  69. }
  70. /** {@inheritDoc} */
  71. public int getPriority() {
  72. return 200;
  73. }
  74. /** {@inheritDoc} */
  75. public Class getSupportedImageClass() {
  76. return ImageRawEPS.class;
  77. }
  78. /** {@inheritDoc} */
  79. public ImageFlavor[] getSupportedImageFlavors() {
  80. return FLAVORS;
  81. }
  82. /** {@inheritDoc} */
  83. public boolean isCompatible(RenderingContext targetContext, Image image) {
  84. if (targetContext instanceof PSRenderingContext) {
  85. PSRenderingContext psContext = (PSRenderingContext)targetContext;
  86. return !psContext.isCreateForms()
  87. && (image == null || image instanceof ImageRawEPS);
  88. }
  89. return false;
  90. }
  91. }