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.

PSImageUtils.java 4.9KB

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.render.ps;
  19. import java.awt.Rectangle;
  20. import java.awt.geom.Rectangle2D;
  21. import java.io.IOException;
  22. import org.apache.xmlgraphics.image.loader.ImageFlavor;
  23. import org.apache.xmlgraphics.image.loader.ImageInfo;
  24. import org.apache.xmlgraphics.image.loader.ImageManager;
  25. import org.apache.xmlgraphics.image.loader.pipeline.ImageProviderPipeline;
  26. import org.apache.xmlgraphics.ps.DSCConstants;
  27. import org.apache.xmlgraphics.ps.PSGenerator;
  28. import org.apache.xmlgraphics.ps.PSResource;
  29. import org.apache.fop.render.ImageHandlerRegistry;
  30. import org.apache.fop.render.RenderingContext;
  31. /**
  32. * Utility code for rendering images in PostScript.
  33. */
  34. public class PSImageUtils extends org.apache.xmlgraphics.ps.PSImageUtils {
  35. /**
  36. * Indicates whether the given image (identified by an {@link ImageInfo} object) shall be
  37. * inlined rather than generated as a PostScript form.
  38. * @param info the info object for the image
  39. * @param renderingContext the rendering context
  40. * @return true if the image shall be inlined, false if forms shall be used.
  41. */
  42. public static boolean isImageInlined(ImageInfo info, PSRenderingContext renderingContext) {
  43. String uri = info.getOriginalURI();
  44. if (uri == null || "".equals(uri)) {
  45. return true;
  46. }
  47. //Investigate choice for inline mode
  48. ImageFlavor[] inlineFlavors = determineSupportedImageFlavors(renderingContext);
  49. ImageManager manager = renderingContext.getUserAgent().getFactory().getImageManager();
  50. ImageProviderPipeline[] inlineCandidates
  51. = manager.getPipelineFactory().determineCandidatePipelines(
  52. info, inlineFlavors);
  53. ImageProviderPipeline inlineChoice = manager.choosePipeline(inlineCandidates);
  54. ImageFlavor inlineFlavor = (inlineChoice != null
  55. ? inlineChoice.getTargetFlavor() : null);
  56. //Create a rendering context for form creation
  57. PSRenderingContext formContext = renderingContext.toFormContext();
  58. //Investigate choice for form mode
  59. ImageFlavor[] formFlavors = determineSupportedImageFlavors(formContext);
  60. ImageProviderPipeline[] formCandidates
  61. = manager.getPipelineFactory().determineCandidatePipelines(
  62. info, formFlavors);
  63. ImageProviderPipeline formChoice = manager.choosePipeline(formCandidates);
  64. ImageFlavor formFlavor = (formChoice != null ? formChoice.getTargetFlavor() : null);
  65. //Inline if form is not supported or if a better choice is available with inline mode
  66. return formFlavor == null || !formFlavor.equals(inlineFlavor);
  67. }
  68. private static ImageFlavor[] determineSupportedImageFlavors(RenderingContext renderingContext) {
  69. ImageFlavor[] inlineFlavors;
  70. ImageHandlerRegistry imageHandlerRegistry
  71. = renderingContext.getUserAgent().getFactory().getImageHandlerRegistry();
  72. inlineFlavors = imageHandlerRegistry.getSupportedFlavors(renderingContext);
  73. return inlineFlavors;
  74. }
  75. /**
  76. * Draws a form at a given location.
  77. * @param form the form resource
  78. * @param info the image info object representing the image in the form
  79. * @param rect the target rectangle (coordinates in millipoints)
  80. * @param generator the PostScript generator
  81. * @throws IOException if an I/O error occurs
  82. */
  83. public static void drawForm(PSResource form, ImageInfo info, Rectangle rect,
  84. PSGenerator generator) throws IOException {
  85. Rectangle2D targetRect = new Rectangle2D.Double(
  86. rect.getMinX() / 1000.0,
  87. rect.getMinY() / 1000.0,
  88. rect.getWidth() / 1000.0,
  89. rect.getHeight() / 1000.0);
  90. generator.saveGraphicsState();
  91. translateAndScale(generator,
  92. info.getSize().getDimensionPt(), targetRect);
  93. //The following %%IncludeResource marker is needed later by ResourceHandler!
  94. generator.writeDSCComment(DSCConstants.INCLUDE_RESOURCE, form);
  95. generator.getResourceTracker().notifyResourceUsageOnPage(form);
  96. generator.writeln(form.getName() + " execform");
  97. generator.restoreGraphicsState();
  98. }
  99. }