Skip to content

Commit 7f201b3

Browse files
FOP-3228: Limit embed of only native PDF in AFP
1 parent fd90461 commit 7f201b3

14 files changed

+85
-9
lines changed

fop-core/src/main/java/org/apache/fop/afp/AFPPaintingState.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ public class AFPPaintingState extends org.apache.fop.util.AbstractPaintingState
6767
* format.
6868
*/
6969
private boolean nativeImagesSupported;
70+
private boolean nativePDFImagesSupported;
7071

7172
private boolean canEmbedJpeg;
7273

@@ -254,6 +255,14 @@ public boolean isNativeImagesSupported() {
254255
return this.nativeImagesSupported;
255256
}
256257

258+
public void setNativePDFImagesSupported(boolean nativePDFImagesSupported) {
259+
this.nativePDFImagesSupported = nativePDFImagesSupported;
260+
}
261+
262+
public boolean isNativePDFImagesSupported() {
263+
return nativePDFImagesSupported;
264+
}
265+
257266
/**
258267
* Set whether or not JPEG images can be embedded within an AFP document.
259268
*

fop-core/src/main/java/org/apache/fop/render/ImageHandlerRegistry.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,11 @@ public ImageHandler getHandler(RenderingContext targetContext, Image image) {
142142
* @param context the rendering context
143143
* @return the array of image flavors
144144
*/
145-
public synchronized ImageFlavor[] getSupportedFlavors(RenderingContext context) {
145+
public synchronized ImageFlavor[] getSupportedFlavors(RenderingContext context, Image image) {
146146
//Extract all ImageFlavors into a single array
147147
List<ImageFlavor> flavors = new java.util.ArrayList<ImageFlavor>();
148148
for (ImageHandler handler : this.handlerList) {
149-
if (handler.isCompatible(context, null)) {
149+
if (handler.isCompatible(context, image)) {
150150
ImageFlavor[] f = handler.getSupportedImageFlavors();
151151
Collections.addAll(flavors, f);
152152
}

fop-core/src/main/java/org/apache/fop/render/afp/AFPDocumentHandler.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,10 @@ public void setNativeImagesSupported(boolean nativeImages) {
476476
paintingState.setNativeImagesSupported(nativeImages);
477477
}
478478

479+
public void setNativePDFImagesSupported(boolean nativeImages) {
480+
paintingState.setNativePDFImagesSupported(nativeImages);
481+
}
482+
479483
/** {@inheritDoc} */
480484
public void setCMYKImagesSupported(boolean value) {
481485
paintingState.setCMYKImagesSupported(value);

fop-core/src/main/java/org/apache/fop/render/afp/AFPImageHandlerRawStream.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ public void handleImage(RenderingContext context, Image image, Rectangle pos)
8686
public boolean isCompatible(RenderingContext targetContext, Image image) {
8787
if (targetContext instanceof AFPRenderingContext) {
8888
AFPRenderingContext afpContext = (AFPRenderingContext)targetContext;
89+
if (afpContext.getPaintingState().isNativePDFImagesSupported() && isPDFCompatible(image)) {
90+
return true;
91+
}
8992
return (afpContext.getPaintingState().isNativeImagesSupported())
9093
&& (image == null
9194
|| image instanceof ImageRawJPEG
@@ -99,4 +102,12 @@ private boolean isCompatible(ImageRawStream rawStream) {
99102
return MimeConstants.MIME_TIFF.equals(rawStream.getMimeType())
100103
|| MimeConstants.MIME_PDF.equals(rawStream.getMimeType());
101104
}
105+
106+
private boolean isPDFCompatible(Image image) {
107+
if (image instanceof ImageRawStream) {
108+
ImageRawStream rawStream = (ImageRawStream)image;
109+
return MimeConstants.MIME_PDF.equals(rawStream.getMimeType());
110+
}
111+
return false;
112+
}
102113
}

fop-core/src/main/java/org/apache/fop/render/afp/AFPPainter.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import org.apache.xmlgraphics.image.loader.ImageSessionContext;
4848
import org.apache.xmlgraphics.image.loader.ImageSize;
4949
import org.apache.xmlgraphics.image.loader.impl.ImageGraphics2D;
50+
import org.apache.xmlgraphics.image.loader.impl.ImageRawStream;
5051
import org.apache.xmlgraphics.java2d.Graphics2DImagePainter;
5152

5253
import org.apache.fop.afp.AFPBorderPainter;
@@ -68,6 +69,7 @@
6869
import org.apache.fop.afp.ptoca.PtocaBuilder;
6970
import org.apache.fop.afp.ptoca.PtocaProducer;
7071
import org.apache.fop.afp.util.AFPResourceAccessor;
72+
import org.apache.fop.apps.MimeConstants;
7173
import org.apache.fop.fonts.Font;
7274
import org.apache.fop.fonts.FontTriplet;
7375
import org.apache.fop.fonts.Typeface;
@@ -1151,4 +1153,15 @@ public void fillBackground(Rectangle rect, Paint fill, BorderProps bpsBefore,
11511153
BorderProps bpsAfter, BorderProps bpsStart, BorderProps bpsEnd) throws IFException {
11521154
// not supported in AFP
11531155
}
1156+
1157+
protected Image getImageForSupportedFlavors(ImageInfo info) {
1158+
if (getPaintingState().isNativePDFImagesSupported() && MimeConstants.MIME_PDF.equals(info.getMimeType())) {
1159+
return new ImageRawStream(info, null, (ImageRawStream.InputStreamFactory) null) {
1160+
public String getMimeType() {
1161+
return MimeConstants.MIME_PDF;
1162+
}
1163+
};
1164+
}
1165+
return null;
1166+
}
11541167
}

fop-core/src/main/java/org/apache/fop/render/afp/AFPRendererConfig.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
import static org.apache.fop.render.afp.AFPRendererOption.IMAGES_MASK_ENABLED;
5858
import static org.apache.fop.render.afp.AFPRendererOption.IMAGES_MODE;
5959
import static org.apache.fop.render.afp.AFPRendererOption.IMAGES_NATIVE;
60+
import static org.apache.fop.render.afp.AFPRendererOption.IMAGES_NATIVE_PDF;
6061
import static org.apache.fop.render.afp.AFPRendererOption.IMAGES_WRAP_PSEG;
6162
import static org.apache.fop.render.afp.AFPRendererOption.JPEG_ALLOW_JPEG_EMBEDDING;
6263
import static org.apache.fop.render.afp.AFPRendererOption.JPEG_BITMAP_ENCODING_QUALITY;
@@ -148,6 +149,10 @@ public Boolean isNativeImagesSupported() {
148149
return getParam(IMAGES_NATIVE, Boolean.class);
149150
}
150151

152+
public Boolean isNativePDFImagesSupported() {
153+
return getParam(IMAGES_NATIVE_PDF, Boolean.class);
154+
}
155+
151156
public AFPShadingMode getShadingMode() {
152157
return getParam(SHADING, AFPShadingMode.class);
153158
}
@@ -322,8 +327,8 @@ private void configureImages() throws ConfigurationException, FOPException {
322327
}
323328
setParam(IMAGES_DITHERING_QUALITY, dq);
324329
setParam(IMAGES_NATIVE, imagesCfg.getAttributeAsBoolean(IMAGES_NATIVE.getName(), false));
325-
setParam(IMAGES_WRAP_PSEG,
326-
imagesCfg.getAttributeAsBoolean(IMAGES_WRAP_PSEG.getName(), false));
330+
setParam(IMAGES_NATIVE_PDF, imagesCfg.getAttributeAsBoolean(IMAGES_NATIVE_PDF.getName(), false));
331+
setParam(IMAGES_WRAP_PSEG, imagesCfg.getAttributeAsBoolean(IMAGES_WRAP_PSEG.getName(), false));
327332
setParam(IMAGES_FS45, imagesCfg.getAttributeAsBoolean(IMAGES_FS45.getName(), false));
328333
setParam(IMAGES_MASK_ENABLED, imagesCfg.getAttributeAsBoolean(IMAGES_MASK_ENABLED.getName(), false));
329334
if ("scale-to-fit".equals(imagesCfg.getAttribute(IMAGES_MAPPING_OPTION.getName(), null))) {

fop-core/src/main/java/org/apache/fop/render/afp/AFPRendererConfigurator.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ private void configure(AFPDocumentHandler documentHandler, AFPRendererConfig con
8484
if (config.isNativeImagesSupported() != null) {
8585
documentHandler.setNativeImagesSupported(config.isNativeImagesSupported());
8686
}
87+
if (config.isNativePDFImagesSupported() != null) {
88+
documentHandler.setNativePDFImagesSupported(config.isNativePDFImagesSupported());
89+
}
8790
if (config.getShadingMode() != null) {
8891
documentHandler.setShadingMode(config.getShadingMode());
8992
}

fop-core/src/main/java/org/apache/fop/render/afp/AFPRendererOption.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public enum AFPRendererOption implements RendererConfigOption {
3535
IMAGES_MAPPING_OPTION("mapping_option", Byte.class),
3636
IMAGES_MODE("mode", Boolean.class),
3737
IMAGES_NATIVE("native", Boolean.class),
38+
IMAGES_NATIVE_PDF("native-pdf", Boolean.class),
3839
IMAGES_WRAP_PSEG("pseg", Boolean.class),
3940
JPEG_ALLOW_JPEG_EMBEDDING("allow-embedding", Boolean.class),
4041
JPEG_BITMAP_ENCODING_QUALITY("bitmap-encoding-quality", Float.class),

fop-core/src/main/java/org/apache/fop/render/intermediate/AbstractIFPainter.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ protected void drawImageUsingImageHandler(ImageInfo info, Rectangle rect)
158158
Map hints = createDefaultImageProcessingHints(sessionContext);
159159
context.putHints(hints);
160160

161-
ImageFlavor[] flavors = imageHandlerRegistry.getSupportedFlavors(context);
161+
ImageFlavor[] flavors = imageHandlerRegistry.getSupportedFlavors(context, getImageForSupportedFlavors(info));
162162
info.getCustomObjects().put("warningincustomobject", true);
163163
org.apache.xmlgraphics.image.loader.Image img = manager.getImage(
164164
info, flavors,
@@ -181,6 +181,10 @@ protected void drawImageUsingImageHandler(ImageInfo info, Rectangle rect)
181181
}
182182
}
183183

184+
protected Image getImageForSupportedFlavors(ImageInfo info) {
185+
return null;
186+
}
187+
184188
/**
185189
* Creates the default map of processing hints for the image loading framework.
186190
* @param sessionContext the session context for access to resolution information
@@ -234,8 +238,7 @@ protected void drawImage(Image image, Rectangle rect,
234238
if (additionalHints != null) {
235239
hints.putAll(additionalHints);
236240
}
237-
effImage = manager.convertImage(image,
238-
imageHandlerRegistry.getSupportedFlavors(context), hints);
241+
effImage = manager.convertImage(image, imageHandlerRegistry.getSupportedFlavors(context, null), hints);
239242
} else {
240243
effImage = image;
241244
}

fop-core/src/main/java/org/apache/fop/render/ps/PSImageUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ private static ImageFlavor[] determineSupportedImageFlavors(RenderingContext ren
8181
ImageFlavor[] inlineFlavors;
8282
ImageHandlerRegistry imageHandlerRegistry
8383
= renderingContext.getUserAgent().getImageHandlerRegistry();
84-
inlineFlavors = imageHandlerRegistry.getSupportedFlavors(renderingContext);
84+
inlineFlavors = imageHandlerRegistry.getSupportedFlavors(renderingContext, null);
8585
return inlineFlavors;
8686
}
8787

0 commit comments

Comments
 (0)