diff options
author | Jeremias Maerki <jeremias@apache.org> | 2009-02-05 16:27:08 +0000 |
---|---|---|
committer | Jeremias Maerki <jeremias@apache.org> | 2009-02-05 16:27:08 +0000 |
commit | 4e27a1c9dd7246dff9d9380755171f179e3fcb8a (patch) | |
tree | 93817887ac0a99f6a3c1b736cae9ce5688eb4bab /src/java/org/apache/fop/afp | |
parent | 7fbf442044c42fce773c36cd9aea80694abf007d (diff) | |
download | xmlgraphics-fop-4e27a1c9dd7246dff9d9380755171f179e3fcb8a.tar.gz xmlgraphics-fop-4e27a1c9dd7246dff9d9380755171f179e3fcb8a.zip |
Performance improvements and file-size reductions by introducing letter-spacing and word-spacing attributes in new IF (as mentioned on fop-dev).
Allow to control whether kerning information is loaded from fonts.
Started support for AFP font embedding (incomplete and currently disabled)
git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/branches/Temp_AreaTreeNewDesign@741165 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/java/org/apache/fop/afp')
6 files changed, 323 insertions, 33 deletions
diff --git a/src/java/org/apache/fop/afp/AFPResourceManager.java b/src/java/org/apache/fop/afp/AFPResourceManager.java index 9218ea8e4..78c06bedf 100644 --- a/src/java/org/apache/fop/afp/AFPResourceManager.java +++ b/src/java/org/apache/fop/afp/AFPResourceManager.java @@ -21,19 +21,32 @@ package org.apache.fop.afp; import java.io.IOException; import java.io.OutputStream; +import java.net.URI; +import java.net.URISyntaxException; import java.util.Map; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + import org.apache.fop.afp.modca.AbstractNamedAFPObject; import org.apache.fop.afp.modca.AbstractPageObject; import org.apache.fop.afp.modca.IncludeObject; +import org.apache.fop.afp.modca.IncludedResourceObject; import org.apache.fop.afp.modca.PageSegment; import org.apache.fop.afp.modca.Registry; import org.apache.fop.afp.modca.ResourceGroup; +import org.apache.fop.afp.modca.ResourceObject; +import org.apache.fop.afp.util.ResourceAccessor; +import org.apache.fop.afp.util.SimpleResourceAccessor; /** * Manages the creation and storage of document resources */ public class AFPResourceManager { + + /** logging instance */ + private static Log log = LogFactory.getLog(AFPResourceManager.class); + /** The AFP datastream (document tree) */ private DataStream dataStream; @@ -47,8 +60,8 @@ public class AFPResourceManager { /** Maintain a reference count of instream objects for referencing purposes */ private int instreamObjectCount = 0; - /** a mapping of resourceInfo --> names of includable objects */ - private final Map/*<AFPResourceInfo,String>*/ includableObjectsMap + /** a mapping of resourceInfo --> include name */ + private final Map/*<AFPResourceInfo,String>*/ includeNameMap = new java.util.HashMap()/*<AFPResourceInfo,String>*/; private Map pageSegmentMap = new java.util.HashMap(); @@ -120,7 +133,7 @@ public class AFPResourceManager { AFPResourceInfo resourceInfo = dataObjectInfo.getResourceInfo(); updateResourceInfoUri(resourceInfo); - String objectName = (String)includableObjectsMap.get(resourceInfo); + String objectName = (String)includeNameMap.get(resourceInfo); if (objectName != null) { // an existing data resource so reference it by adding an include to the current page includeObject(dataObjectInfo, objectName); @@ -156,35 +169,35 @@ public class AFPResourceManager { useInclude &= resourceGroup != null; if (useInclude) { - boolean usePageSegment = dataObjectInfo.isCreatePageSegment(); - - // if it is to reside within a resource group at print-file or external level - if (resourceLevel.isPrintFile() || resourceLevel.isExternal()) { - if (usePageSegment) { - String pageSegmentName = "S10" + namedObj.getName().substring(3); - namedObj.setName(pageSegmentName); - PageSegment seg = new PageSegment(pageSegmentName); - seg.addObject(namedObj); - namedObj = seg; - } + boolean usePageSegment = dataObjectInfo.isCreatePageSegment(); - // wrap newly created data object in a resource object - namedObj = dataObjectFactory.createResource(namedObj, resourceInfo, objectType); + // if it is to reside within a resource group at print-file or external level + if (resourceLevel.isPrintFile() || resourceLevel.isExternal()) { + if (usePageSegment) { + String pageSegmentName = "S10" + namedObj.getName().substring(3); + namedObj.setName(pageSegmentName); + PageSegment seg = new PageSegment(pageSegmentName); + seg.addObject(namedObj); + namedObj = seg; } - // add data object into its resource group destination - resourceGroup.addObject(namedObj); + // wrap newly created data object in a resource object + namedObj = dataObjectFactory.createResource(namedObj, resourceInfo, objectType); + } - // create the include object - objectName = namedObj.getName(); - if (usePageSegment) { - includePageSegment(dataObjectInfo, objectName); - pageSegmentMap.put(resourceInfo, objectName); - } else { - includeObject(dataObjectInfo, objectName); - // record mapping of resource info to data object resource name - includableObjectsMap.put(resourceInfo, objectName); - } + // add data object into its resource group destination + resourceGroup.addObject(namedObj); + + // create the include object + objectName = namedObj.getName(); + if (usePageSegment) { + includePageSegment(dataObjectInfo, objectName); + pageSegmentMap.put(resourceInfo, objectName); + } else { + includeObject(dataObjectInfo, objectName); + // record mapping of resource info to data object resource name + includeNameMap.put(resourceInfo, objectName); + } } else { // not to be included so inline data object directly into the current page @@ -206,10 +219,10 @@ public class AFPResourceManager { private void includeObject(AFPDataObjectInfo dataObjectInfo, String objectName) { - IncludeObject includeObject - = dataObjectFactory.createInclude(objectName, dataObjectInfo); - dataStream.getCurrentPage().addObject(includeObject); - } + IncludeObject includeObject + = dataObjectFactory.createInclude(objectName, dataObjectInfo); + dataStream.getCurrentPage().addObject(includeObject); + } private void includePageSegment(AFPDataObjectInfo dataObjectInfo, String pageSegmentName) { @@ -221,6 +234,53 @@ public class AFPResourceManager { } /** + * Creates an included resource object by loading the contained object from a file. + * @param resourceName the name of the resource + * @param basePath the base path in which to look for the resource files + * @param resourceObjectType the resource object type ({@link ResourceObject}.*) + * @throws IOException if an I/O error occurs while loading the resource + */ + public void createIncludedResource(String resourceName, String basePath, + byte resourceObjectType) throws IOException { + AFPResourceLevel resourceLevel = new AFPResourceLevel(AFPResourceLevel.PRINT_FILE); + URI uri; + try { + uri = new URI(resourceName.trim()); + } catch (URISyntaxException e) { + throw new IOException("Could not create URI from resource name: " + resourceName + + " (" + e.getMessage() + ")"); + } + + AFPResourceInfo resourceInfo = new AFPResourceInfo(); + resourceInfo.setLevel(resourceLevel); + resourceInfo.setName(resourceName); + resourceInfo.setUri(uri.toASCIIString()); + + String objectName = (String)includeNameMap.get(resourceInfo); + if (objectName == null) { + if (log.isDebugEnabled()) { + log.debug("Adding included resource: " + resourceName); + } + //TODO This works with local filenames only. In the long term, this + //should work through FOP's URI resolver. + ResourceAccessor accessor = new SimpleResourceAccessor(basePath); + IncludedResourceObject resourceContent = new IncludedResourceObject( + resourceName, accessor, uri); + + ResourceObject resourceObject = factory.createResource(resourceName); + resourceObject.setDataObject(resourceContent); + resourceObject.setType(resourceObjectType); + + ResourceGroup resourceGroup = streamer.getResourceGroup(resourceLevel); + resourceGroup.addObject(resourceObject); + // record mapping of resource info to data object resource name + includeNameMap.put(resourceInfo, resourceName); + } else { + //skip, already created + } + } + + /** * Sets resource level defaults. The existing defaults over merged with the ones passed in * as parameter. * @param defaults the new defaults @@ -236,4 +296,5 @@ public class AFPResourceManager { public AFPResourceLevelDefaults getResourceLevelDefaults() { return this.resourceLevelDefaults; } -}
\ No newline at end of file + +} diff --git a/src/java/org/apache/fop/afp/fonts/AFPFont.java b/src/java/org/apache/fop/afp/fonts/AFPFont.java index 4801737b3..f56611087 100644 --- a/src/java/org/apache/fop/afp/fonts/AFPFont.java +++ b/src/java/org/apache/fop/afp/fonts/AFPFont.java @@ -97,6 +97,14 @@ public abstract class AFPFont extends Typeface { */ public abstract CharacterSet getCharacterSet(int size); + /** + * Indicates if this font may be embedded. + * @return True, if embedding is possible/permitted + */ + public boolean isEmbeddable() { + return false; //TODO Complete AFP font embedding + } + /** {@inheritDoc} */ public String toString() { return "name=" + name; diff --git a/src/java/org/apache/fop/afp/modca/IncludedResourceObject.java b/src/java/org/apache/fop/afp/modca/IncludedResourceObject.java new file mode 100644 index 000000000..296ab2d9d --- /dev/null +++ b/src/java/org/apache/fop/afp/modca/IncludedResourceObject.java @@ -0,0 +1,63 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* $Id$ */ + +package org.apache.fop.afp.modca; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.net.URI; + +import org.apache.commons.io.IOUtils; + +import org.apache.fop.afp.util.ResourceAccessor; + + +/** + * Encapsulates an included resource object that is loaded from an external file. + */ +public class IncludedResourceObject extends AbstractNamedAFPObject { + + private ResourceAccessor resourceAccessor; + private URI uri; + + /** + * Main constructor. + * @param name the name of the included resource + * @param resourceAccessor the resource accessor to load the external file with + * @param uri the URI of the external file + */ + public IncludedResourceObject(String name, + ResourceAccessor resourceAccessor, URI uri) { + super(name); + this.resourceAccessor = resourceAccessor; + this.uri = uri; + } + + /** {@inheritDoc} */ + public void writeToStream(OutputStream os) throws IOException { + InputStream in = resourceAccessor.createInputStream(this.uri); + try { + IOUtils.copy(in, os); + } finally { + IOUtils.closeQuietly(in); + } + } + +} diff --git a/src/java/org/apache/fop/afp/util/DefaultFOPResourceAccessor.java b/src/java/org/apache/fop/afp/util/DefaultFOPResourceAccessor.java new file mode 100644 index 000000000..55c8eab7d --- /dev/null +++ b/src/java/org/apache/fop/afp/util/DefaultFOPResourceAccessor.java @@ -0,0 +1,60 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* $Id$ */ + +package org.apache.fop.afp.util; + +import java.io.IOException; +import java.io.InputStream; +import java.net.URI; + +import javax.xml.transform.Source; +import javax.xml.transform.stream.StreamSource; + +import org.apache.fop.apps.FOUserAgent; + +/** + * Default implementation of the {@link ResourceAccessor} interface for use inside FOP. + */ +public class DefaultFOPResourceAccessor implements ResourceAccessor { + + private FOUserAgent userAgent; + + /** + * Main constructor. + * @param userAgent the FO user agent + */ + public DefaultFOPResourceAccessor(FOUserAgent userAgent) { + this.userAgent = userAgent; + } + + /** {@inheritDoc} */ + public InputStream createInputStream(URI uri) throws IOException { + Source src = userAgent.resolveURI(uri.toASCIIString()); + if (src == null) { + return null; + } else if (src instanceof StreamSource) { + StreamSource ss = (StreamSource)src; + InputStream in = ss.getInputStream(); + return in; + } else { + return null; + } + } + +} diff --git a/src/java/org/apache/fop/afp/util/ResourceAccessor.java b/src/java/org/apache/fop/afp/util/ResourceAccessor.java new file mode 100644 index 000000000..6b9995c44 --- /dev/null +++ b/src/java/org/apache/fop/afp/util/ResourceAccessor.java @@ -0,0 +1,40 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* $Id$ */ + +package org.apache.fop.afp.util; + +import java.io.IOException; +import java.io.InputStream; +import java.net.URI; + +/** + * Defines an interface through which external resource objects can be accessed. + */ +public interface ResourceAccessor { + + /** + * Creates a new {@link InputStream} for the given URI that allows read access to an external + * resource. + * @param uri the URI of an external resource. + * @return the new input stream + * @throws IOException if an I/O error occurs while opening the resource + */ + InputStream createInputStream(URI uri) throws IOException; + +} diff --git a/src/java/org/apache/fop/afp/util/SimpleResourceAccessor.java b/src/java/org/apache/fop/afp/util/SimpleResourceAccessor.java new file mode 100644 index 000000000..51772c253 --- /dev/null +++ b/src/java/org/apache/fop/afp/util/SimpleResourceAccessor.java @@ -0,0 +1,58 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* $Id$ */ + +package org.apache.fop.afp.util; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.net.URI; +import java.net.URL; + +/** + * Simple implementation of the {@link ResourceAccessor} interface for access via files. + */ +public class SimpleResourceAccessor implements ResourceAccessor { + + private URI baseURI; + + /** + * Creates a new simple resource accessor. + * @param basePath the base path to resolve relative URIs to + */ + public SimpleResourceAccessor(File basePath) { + this.baseURI = basePath.toURI(); + } + + /** + * Creates a new simple resource accessor. + * @param basePath the base path to resolve relative URIs to + */ + public SimpleResourceAccessor(String basePath) { + this(new File(basePath)); + } + + /** {@inheritDoc} */ + public InputStream createInputStream(URI uri) throws IOException { + URI resolved = this.baseURI.resolve(uri); + URL url = resolved.toURL(); + return url.openStream(); + } + +} |