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