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: BitmapProducerJava2D.java 1877382 2020-05-05 09:30:52Z ssteiner $ */ 19 20 package org.apache.fop.visual; 21 22 import java.awt.image.BufferedImage; 23 import java.io.BufferedOutputStream; 24 import java.io.File; 25 import java.io.FileOutputStream; 26 import java.io.OutputStream; 27 import java.net.URI; 28 29 import javax.xml.transform.Transformer; 30 import javax.xml.transform.sax.SAXResult; 31 import javax.xml.transform.stream.StreamSource; 32 33 import org.apache.commons.io.IOUtils; 34 35 import org.apache.fop.apps.FOUserAgent; 36 import org.apache.fop.apps.Fop; 37 import org.apache.fop.apps.FopFactory; 38 import org.apache.fop.apps.MimeConstants; 39 import org.apache.fop.configuration.Configurable; 40 import org.apache.fop.configuration.Configuration; 41 import org.apache.fop.configuration.ConfigurationException; 42 import org.apache.fop.util.DefaultErrorListener; 43 44 /** 45 * BitmapProducer implementation that uses the Java2DRenderer to create bitmaps. 46 * <p> 47 * Here's what the configuration element looks like for the class: 48 * <p> 49 * <pre> 50 * <producer classname="org.apache.fop.visual.BitmapProducerJava2D"> 51 * <delete-temp-files>false</delete-temp-files> 52 * </producer> 53 * </pre> 54 * <p> 55 * The "delete-temp-files" element is optional and defaults to true. 56 */ 57 public class BitmapProducerJava2D extends AbstractBitmapProducer implements Configurable { 58 59 // configure fopFactory as desired 60 private final FopFactory fopFactory; 61 62 private boolean deleteTempFiles; 63 BitmapProducerJava2D(URI baseUri)64 public BitmapProducerJava2D(URI baseUri) { 65 super(baseUri); 66 fopFactory = FopFactory.newInstance(baseUri); 67 } 68 configure(Configuration cfg)69 public void configure(Configuration cfg) throws ConfigurationException { 70 this.deleteTempFiles = cfg.getChild("delete-temp-files").getValueAsBoolean(true); 71 } 72 73 /** @see org.apache.fop.visual.BitmapProducer */ produce(File src, int index, ProducerContext context)74 public BufferedImage produce(File src, int index, ProducerContext context) { 75 try { 76 FOUserAgent userAgent = fopFactory.newFOUserAgent(); 77 userAgent.setTargetResolution(context.getTargetResolution()); 78 79 File outputFile = new File(context.getTargetDir(), 80 src.getName() + "." + index + ".java2d.png"); 81 OutputStream out = new FileOutputStream(outputFile); 82 out = new BufferedOutputStream(out); 83 try { 84 Fop fop = fopFactory.newFop(MimeConstants.MIME_PNG, userAgent, out); 85 SAXResult res = new SAXResult(fop.getDefaultHandler()); 86 87 Transformer transformer = getTransformer(context); 88 transformer.setErrorListener(new DefaultErrorListener(log)); 89 transformer.transform(new StreamSource(src), res); 90 } finally { 91 IOUtils.closeQuietly(out); 92 } 93 94 BufferedImage img = BitmapComparator.getImage(outputFile); 95 if (deleteTempFiles) { 96 if (!outputFile.delete()) { 97 log.warn("Cannot delete " + outputFile); 98 outputFile.deleteOnExit(); 99 } 100 } 101 return img; 102 } catch (Exception e) { 103 e.printStackTrace(); 104 log.error(e); 105 return null; 106 } 107 } 108 109 } 110