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 5.0KB

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