git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@1825861 13f79535-47bb-0310-9956-ffa450edef68pull/6/head
import org.apache.fop.afp.goca.GraphicsSetLineType; | import org.apache.fop.afp.goca.GraphicsSetLineType; | ||||
import org.apache.fop.afp.modca.GraphicsObject; | import org.apache.fop.afp.modca.GraphicsObject; | ||||
import org.apache.fop.afp.modca.ResourceObject; | |||||
import org.apache.fop.afp.util.CubicBezierApproximator; | import org.apache.fop.afp.util.CubicBezierApproximator; | ||||
import org.apache.fop.fonts.FontInfo; | import org.apache.fop.fonts.FontInfo; | ||||
import org.apache.fop.render.afp.AFPImageHandlerRenderedImage; | import org.apache.fop.render.afp.AFPImageHandlerRenderedImage; | ||||
double h = toMillipointFactor * imgHeight * -gat.getScaleY(); | double h = toMillipointFactor * imgHeight * -gat.getScaleY(); | ||||
AFPImageHandlerRenderedImage handler = new AFPImageHandlerRenderedImage(); | AFPImageHandlerRenderedImage handler = new AFPImageHandlerRenderedImage(); | ||||
ImageInfo imageInfo = new ImageInfo(null, null); | |||||
String uri = null; | |||||
if (resourceManager.getResourceLevelDefaults() | |||||
.getDefaultResourceLevel(ResourceObject.TYPE_GRAPHIC).isPrintFile()) { | |||||
uri = resourceInfo.getUri(); | |||||
} | |||||
ImageInfo imageInfo = new ImageInfo(uri, null); | |||||
imageInfo.setSize(new ImageSize( | imageInfo.setSize(new ImageSize( | ||||
img.getWidth(), img.getHeight(), paintingState.getResolution())); | img.getWidth(), img.getHeight(), paintingState.getResolution())); | ||||
imageInfo.getSize().calcSizeFromPixels(); | imageInfo.getSize().calcSizeFromPixels(); | ||||
(int)Math.round(h)); | (int)Math.round(h)); | ||||
AFPRenderingContext context = new AFPRenderingContext(null, | AFPRenderingContext context = new AFPRenderingContext(null, | ||||
resourceManager, paintingState, fontInfo, null); | resourceManager, paintingState, fontInfo, null); | ||||
resourceManager.includeCached = false; | |||||
try { | try { | ||||
handler.handleImage(context, red, targetPos); | handler.handleImage(context, red, targetPos); | ||||
} catch (IOException ioe) { | } catch (IOException ioe) { | ||||
handleIOException(ioe); | handleIOException(ioe); | ||||
} | } | ||||
resourceManager.includeCached = true; | |||||
} | } | ||||
/** | /** |
import java.io.OutputStream; | import java.io.OutputStream; | ||||
import java.net.URI; | import java.net.URI; | ||||
import java.net.URISyntaxException; | import java.net.URISyntaxException; | ||||
import java.util.ArrayList; | |||||
import java.util.HashMap; | |||||
import java.util.List; | |||||
import java.util.Map; | import java.util.Map; | ||||
import org.apache.commons.io.IOUtils; | import org.apache.commons.io.IOUtils; | ||||
private int instreamObjectCount; | private int instreamObjectCount; | ||||
/** Mapping of resourceInfo to AbstractCachedObject */ | /** Mapping of resourceInfo to AbstractCachedObject */ | ||||
private final Map<AFPResourceInfo, AbstractCachedObject> includeObjectCache | |||||
= new java.util.HashMap<AFPResourceInfo, AbstractCachedObject>(); | |||||
private final Map<AFPResourceInfo, List<AbstractCachedObject>> includeObjectCache = | |||||
new HashMap<AFPResourceInfo, List<AbstractCachedObject>>(); | |||||
private AFPResourceLevelDefaults resourceLevelDefaults = new AFPResourceLevelDefaults(); | private AFPResourceLevelDefaults resourceLevelDefaults = new AFPResourceLevelDefaults(); | ||||
protected boolean includeCached = true; | |||||
/** | /** | ||||
* Main constructor | * Main constructor | ||||
* | * | ||||
cachedObject.includeObject(); | cachedObject.includeObject(); | ||||
includeObjectCache.put(dataObjectInfo.getResourceInfo(), cachedObject); | |||||
addToCache(dataObjectInfo.getResourceInfo(), cachedObject); | |||||
//The data field of dataObjectInfo is not further required | //The data field of dataObjectInfo is not further required | ||||
// therefore we are safe to null the reference, saving memory | // therefore we are safe to null the reference, saving memory | ||||
} | } | ||||
private void addToCache(AFPResourceInfo resourceInfo, AbstractCachedObject cachedObject) { | |||||
List<AbstractCachedObject> objs = includeObjectCache.get(resourceInfo); | |||||
if (objs == null) { | |||||
objs = new ArrayList<AbstractCachedObject>(); | |||||
includeObjectCache.put(resourceInfo, objs); | |||||
} | |||||
objs.add(cachedObject); | |||||
} | |||||
/** | /** | ||||
* Returns {@code true} if the passed {@link AFPResourceInfo} instance is already cached. | * Returns {@code true} if the passed {@link AFPResourceInfo} instance is already cached. | ||||
* | * | ||||
* @return {@code true} if ... | * @return {@code true} if ... | ||||
*/ | */ | ||||
public boolean includeCachedObject(AFPResourceInfo resourceInfo, AFPObjectAreaInfo areaInfo) { | public boolean includeCachedObject(AFPResourceInfo resourceInfo, AFPObjectAreaInfo areaInfo) { | ||||
String objectName; | |||||
AbstractCachedObject cachedObject = includeObjectCache.get(resourceInfo); | |||||
if (cachedObject != null) { | |||||
if (areaInfo != null) { | |||||
cachedObject.dataObjectInfo.setObjectAreaInfo(areaInfo); | |||||
List<AbstractCachedObject> cachedObjectList = includeObjectCache.get(resourceInfo); | |||||
if (cachedObjectList != null && includeCached) { | |||||
for (AbstractCachedObject cachedObject : cachedObjectList) { | |||||
if (areaInfo != null && cachedObjectList.size() == 1) { | |||||
cachedObject.dataObjectInfo.setObjectAreaInfo(areaInfo); | |||||
} | |||||
cachedObject.includeObject(); | |||||
} | } | ||||
cachedObject.includeObject(); | |||||
return true; | return true; | ||||
} else { | } else { | ||||
return false; | return false; | ||||
resourceInfo.setName(resourceName); | resourceInfo.setName(resourceName); | ||||
resourceInfo.setUri(uri.toASCIIString()); | resourceInfo.setUri(uri.toASCIIString()); | ||||
AbstractCachedObject cachedObject = includeObjectCache.get(resourceInfo); | |||||
List<AbstractCachedObject> cachedObject = includeObjectCache.get(resourceInfo); | |||||
if (cachedObject == null) { | if (cachedObject == null) { | ||||
if (log.isDebugEnabled()) { | if (log.isDebugEnabled()) { | ||||
log.debug("Adding included resource: " + resourceName); | log.debug("Adding included resource: " + resourceName); | ||||
} | } | ||||
//TODO what is the data object? | //TODO what is the data object? | ||||
cachedObject = new CachedObject(resourceName, null); | |||||
CachedObject newcachedObject = new CachedObject(resourceName, null); | |||||
// record mapping of resource info to data object resource name | // record mapping of resource info to data object resource name | ||||
includeObjectCache.put(resourceInfo, cachedObject); | |||||
addToCache(resourceInfo, newcachedObject); | |||||
} else { | } else { | ||||
//skip, already created | //skip, already created | ||||
} | } | ||||
resourceInfo.setName(resourceName); | resourceInfo.setName(resourceName); | ||||
resourceInfo.setUri(uri.toASCIIString()); | resourceInfo.setUri(uri.toASCIIString()); | ||||
AbstractCachedObject cachedObject = includeObjectCache.get(resourceInfo); | |||||
if (cachedObject == null) { | |||||
List<AbstractCachedObject> resource = includeObjectCache.get(resourceInfo); | |||||
if (resource == null) { | |||||
ResourceGroup resourceGroup = streamer.getResourceGroup(resourceLevel); | ResourceGroup resourceGroup = streamer.getResourceGroup(resourceLevel); | ||||
//resourceObject delegates write commands to copyNamedResource() | //resourceObject delegates write commands to copyNamedResource() | ||||
protected void writeEnd(OutputStream os) throws IOException { } | protected void writeEnd(OutputStream os) throws IOException { } | ||||
}; | }; | ||||
resourceGroup.addObject(resourceObject); | resourceGroup.addObject(resourceObject); | ||||
cachedObject = new CachedObject(resourceName, null); | |||||
includeObjectCache.put(resourceInfo, cachedObject); | |||||
CachedObject newresource = new CachedObject(resourceName, null); | |||||
addToCache(resourceInfo, newresource); | |||||
} | } | ||||
} | } | ||||
package org.apache.fop.afp; | package org.apache.fop.afp; | ||||
import java.awt.Dimension; | |||||
import java.awt.Graphics2D; | |||||
import java.awt.geom.Rectangle2D; | |||||
import java.io.ByteArrayInputStream; | |||||
import java.io.ByteArrayOutputStream; | import java.io.ByteArrayOutputStream; | ||||
import java.io.File; | import java.io.File; | ||||
import java.io.IOException; | import java.io.IOException; | ||||
import org.junit.Before; | import org.junit.Before; | ||||
import org.junit.Test; | import org.junit.Test; | ||||
import static org.junit.Assert.assertEquals; | |||||
import static org.junit.Assert.assertFalse; | import static org.junit.Assert.assertFalse; | ||||
import static org.junit.Assert.assertTrue; | import static org.junit.Assert.assertTrue; | ||||
import org.apache.xmlgraphics.java2d.GraphicContext; | |||||
import org.apache.xmlgraphics.java2d.Graphics2DImagePainter; | |||||
import org.apache.xmlgraphics.util.MimeConstants; | import org.apache.xmlgraphics.util.MimeConstants; | ||||
import org.apache.fop.apps.io.ResourceResolverFactory; | import org.apache.fop.apps.io.ResourceResolverFactory; | ||||
import org.apache.fop.render.afp.AFPParser; | |||||
/** | /** | ||||
* Test case for {@link AFPResourceManager}. | * Test case for {@link AFPResourceManager}. | ||||
dataInfo.setObjectAreaInfo(objectAreaInfo); | dataInfo.setObjectAreaInfo(objectAreaInfo); | ||||
return dataInfo; | return dataInfo; | ||||
} | } | ||||
@Test | |||||
public void testIncludeObject() throws IOException { | |||||
sut.createObject(createAFPGraphicsObjectInfo()); | |||||
sut.createObject(createAFPGraphicsObjectInfo()); | |||||
StringBuilder sb = new StringBuilder(); | |||||
ByteArrayOutputStream bos = new ByteArrayOutputStream(); | |||||
sut.getDataStream().getCurrentPage().writeToStream(bos); | |||||
new AFPParser(true).read(new ByteArrayInputStream(bos.toByteArray()), sb); | |||||
assertEquals(sb.toString(), "BEGIN PAGE PGN00001\n" | |||||
+ "BEGIN ACTIVE_ENVIRONMENT_GROUP AEG00001\n" | |||||
+ "DESCRIPTOR PAGE\n" | |||||
+ "MIGRATION PRESENTATION_TEXT\n" | |||||
+ "END ACTIVE_ENVIRONMENT_GROUP AEG00001\n" | |||||
+ "INCLUDE DATA_RESOURCE\n" | |||||
+ "INCLUDE DATA_RESOURCE\n" | |||||
+ "INCLUDE DATA_RESOURCE\n" | |||||
+ "INCLUDE DATA_RESOURCE\n"); | |||||
} | |||||
private AFPGraphicsObjectInfo createAFPGraphicsObjectInfo() { | |||||
final AFPGraphicsObjectInfo dataInfo = new AFPGraphicsObjectInfo(); | |||||
final String uri = "test"; | |||||
dataInfo.setUri(uri); | |||||
AFPGraphics2D graphics2D = new AFPGraphics2D(false, new AFPPaintingState(), null, null, null); | |||||
graphics2D.setGraphicContext(new GraphicContext()); | |||||
dataInfo.setGraphics2D(graphics2D); | |||||
dataInfo.setPainter(new Graphics2DImagePainter() { | |||||
public void paint(Graphics2D g2d, Rectangle2D area) { | |||||
try { | |||||
AFPDataObjectInfo dataObjectInfo = createAFPDataObjectInfo(); | |||||
dataObjectInfo.setUri(uri); | |||||
sut.createObject(dataObjectInfo); | |||||
} catch (IOException e) { | |||||
throw new RuntimeException(e); | |||||
} | |||||
} | |||||
public Dimension getImageSize() { | |||||
return null; | |||||
} | |||||
}); | |||||
dataInfo.setObjectAreaInfo(new AFPObjectAreaInfo(0, 0, 0, 0, 0, 0)); | |||||
return dataInfo; | |||||
} | |||||
} | } |