Browse Source

Bugzilla #42971 and #42956

AFP Renderer Improvements
Submitted by Adrian Cumiskey <dev.at.cumiskey.com>



git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@560936 13f79535-47bb-0310-9956-ffa450edef68
tags/fop-0_95beta
Chris Bowditch 17 years ago
parent
commit
1e7bbc0e02

+ 72
- 55
src/java/org/apache/fop/render/afp/AFPRenderer.java View File

@@ -256,10 +256,10 @@ public class AFPRenderer extends AbstractPathOrientedRenderer {
this.fontInfo = inFontInfo;
int num = 1;
if (this.fontList != null && this.fontList.size() > 0) {
for (Iterator it = this.fontList.iterator(); it.hasNext(); ) {
for (Iterator it = this.fontList.iterator(); it.hasNext();) {
AFPFontInfo afi = (AFPFontInfo)it.next();
AFPFont bf = (AFPFont)afi.getAFPFont();
for (Iterator it2 = afi.getFontTriplets().iterator(); it2.hasNext(); ) {
for (Iterator it2 = afi.getFontTriplets().iterator(); it2.hasNext();) {
FontTriplet ft = (FontTriplet)it2.next();
this.fontInfo.addFontProperties("F" + num, ft.getName()
, ft.getStyle(), ft.getWeight());
@@ -271,21 +271,24 @@ public class AFPRenderer extends AbstractPathOrientedRenderer {
log.warn("No AFP fonts configured - using default setup");
}
if (this.fontInfo.fontLookup("sans-serif", "normal", 400) == null) {
CharacterSet cs = new FopCharacterSet("T1V10500", "Cp500", "CZH200 ", 1, new Helvetica());
CharacterSet cs = new FopCharacterSet("T1V10500", "Cp500", "CZH200 ",
1, new Helvetica());
AFPFont bf = new OutlineFont("Helvetica", cs);
this.fontInfo.addFontProperties("F" + num, "sans-serif", "normal", 400);
this.fontInfo.addMetrics("F" + num, bf);
num++;
}
if (this.fontInfo.fontLookup("serif", "normal", 400) == null) {
CharacterSet cs = new FopCharacterSet("T1V10500", "Cp500", "CZN200 ", 1, new TimesRoman());
CharacterSet cs = new FopCharacterSet("T1V10500", "Cp500", "CZN200 ",
1, new TimesRoman());
AFPFont bf = new OutlineFont("Helvetica", cs);
this.fontInfo.addFontProperties("F" + num, "serif", "normal", 400);
this.fontInfo.addMetrics("F" + num, bf);
num++;
}
if (this.fontInfo.fontLookup("monospace", "normal", 400) == null) {
CharacterSet cs = new FopCharacterSet("T1V10500", "Cp500", "CZ4200 ", 1, new Courier());
CharacterSet cs = new FopCharacterSet("T1V10500", "Cp500", "CZ4200 ",
1, new Courier());
AFPFont bf = new OutlineFont("Helvetica", cs);
this.fontInfo.addFontProperties("F" + num, "monospace", "normal", 400);
this.fontInfo.addMetrics("F" + num, bf);
@@ -293,7 +296,8 @@ public class AFPRenderer extends AbstractPathOrientedRenderer {
}
if (this.fontInfo.fontLookup("any", "normal", 400) == null) {
FontTriplet ft = this.fontInfo.fontLookup("sans-serif", "normal", 400);
this.fontInfo.addFontProperties(this.fontInfo.getInternalFontKey(ft), "any", "normal", 400);
this.fontInfo.addFontProperties(
this.fontInfo.getInternalFontKey(ft), "any", "normal", 400);
}
}

@@ -340,7 +344,7 @@ public class AFPRenderer extends AbstractPathOrientedRenderer {
*
* {@inheritDoc}
*/
public void preparePage(PageViewport page) {
public void preparePage(PageViewport pageViewport) {
// initializeRootExtensions(page);

_currentFontFamily = "";
@@ -349,7 +353,7 @@ public class AFPRenderer extends AbstractPathOrientedRenderer {
_currentPageFonts.clear();
_lineCache = new HashSet();

Rectangle2D bounds = page.getViewArea();
Rectangle2D bounds = pageViewport.getViewArea();

_pageWidth = mpts2units(bounds.getWidth());
_pageHeight = mpts2units(bounds.getHeight());
@@ -358,12 +362,12 @@ public class AFPRenderer extends AbstractPathOrientedRenderer {

_afpDataStream.startPage(_pageWidth, _pageHeight, 0);

renderPageObjectExtensions(page);
renderPageObjectExtensions(pageViewport);

if (_pages == null) {
_pages = new HashMap();
}
_pages.put(page, _afpDataStream.savePage());
_pages.put(pageViewport, _afpDataStream.savePage());

}

@@ -380,7 +384,7 @@ public class AFPRenderer extends AbstractPathOrientedRenderer {
}

/**
* {@inheritDoc}
* {@inheritDoc}
*/
public void startVParea(CTM ctm, Rectangle2D clippingRect) {
// dummy not used
@@ -436,7 +440,7 @@ public class AFPRenderer extends AbstractPathOrientedRenderer {
}

/**
* {@inheritDoc}
* {@inheritDoc}
*/
protected void renderBlockViewport(BlockViewport bv, List children) {
// clip and position viewport if necessary
@@ -548,7 +552,7 @@ public class AFPRenderer extends AbstractPathOrientedRenderer {
/**
* {@inheritDoc}
*/
public void renderPage(PageViewport page) {
public void renderPage(PageViewport pageViewport) {

// initializeRootExtensions(page);

@@ -558,27 +562,27 @@ public class AFPRenderer extends AbstractPathOrientedRenderer {
_currentPageFonts.clear();
_lineCache = new HashSet();

Rectangle2D bounds = page.getViewArea();
Rectangle2D bounds = pageViewport.getViewArea();

_pageWidth = mpts2units(bounds.getWidth());
_pageHeight = mpts2units(bounds.getHeight());

if (_pages != null && _pages.containsKey(page)) {
if (_pages != null && _pages.containsKey(pageViewport)) {

_afpDataStream.restorePage((PageObject)_pages.remove(page));
_afpDataStream.restorePage((PageObject)_pages.remove(pageViewport));

} else {
// renderPageGroupExtensions(page);

_afpDataStream.startPage(_pageWidth, _pageHeight, 0);

renderPageObjectExtensions(page);
renderPageObjectExtensions(pageViewport);

}

pushViewPortPos(new ViewPortPos());

renderPageAreas(page.getPage());
renderPageAreas(pageViewport.getPage());

Iterator i = _currentPageFonts.values().iterator();
while (i.hasNext()) {
@@ -609,21 +613,21 @@ public class AFPRenderer extends AbstractPathOrientedRenderer {
}

/**
* {@inheritDoc}
* {@inheritDoc}
*/
public void clipRect(float x, float y, float width, float height) {
// TODO
}

/**
* {@inheritDoc}
* {@inheritDoc}
*/
public void moveTo(float x, float y) {
// TODO
}

/**
* {@inheritDoc}
* {@inheritDoc}
*/
public void lineTo(float x, float y) {
// TODO
@@ -637,7 +641,7 @@ public class AFPRenderer extends AbstractPathOrientedRenderer {
}

/**
* {@inheritDoc}
* {@inheritDoc}
*/
public void fillRect(float x, float y, float width, float height) {
/*
@@ -857,13 +861,13 @@ public class AFPRenderer extends AbstractPathOrientedRenderer {
protected RendererContext createRendererContext(int x, int y, int width, int height, Map foreignAttributes) {
RendererContext context;
context = super.createRendererContext(x, y, width, height, foreignAttributes);
context.setProperty(AFPRendererContextConstants.AFP_GRAYSCALE,
context.setProperty(AFPRendererContextConstants.AFP_GRAYSCALE,
new Boolean(!this.colorImages));
return context;
}

/**
* {@inheritDoc}
* {@inheritDoc}
*/
public void drawImage(String url, Rectangle2D pos, Map foreignAttributes) {
String name = null;
@@ -1007,7 +1011,7 @@ public class AFPRenderer extends AbstractPathOrientedRenderer {
}
}
}
/**
* Draws a BufferedImage to AFP.
* @param bi the BufferedImage
@@ -1027,7 +1031,7 @@ public class AFPRenderer extends AbstractPathOrientedRenderer {
//Serialize image
writeImage(bi, baout);
byte[] buf = baout.toByteArray();
//Generate image
ImageObject io = _afpDataStream.getImageObject(afpx, afpy, afpw, afph);
io.setImageParameters(
@@ -1047,10 +1051,10 @@ public class AFPRenderer extends AbstractPathOrientedRenderer {
log.error("Error while serializing bitmap: " + ioe.getMessage(), ioe);
}
}
/**
* Establishes a new foreground or fill color.
* {@inheritDoc}
* {@inheritDoc}
*/
public void updateColor(Color col, boolean fill) {
if (fill) {
@@ -1095,7 +1099,7 @@ public class AFPRenderer extends AbstractPathOrientedRenderer {
}

/**
* {@inheritDoc}
* {@inheritDoc}
*/
public void renderImage(Image image, Rectangle2D pos) {
String url = image.getURL();
@@ -1354,40 +1358,50 @@ public class AFPRenderer extends AbstractPathOrientedRenderer {
* Method to render the page extension.
* <p>
*
* @param page
* @param pageViewport
* the page object
*/
private void renderPageObjectExtensions(PageViewport page) {
private void renderPageObjectExtensions(PageViewport pageViewport) {

_pageSegmentsMap = null;
if (page.getExtensionAttachments() != null
&& page.getExtensionAttachments().size() > 0) {
if (pageViewport.getExtensionAttachments() != null
&& pageViewport.getExtensionAttachments().size() > 0) {
//Extract all AFPPageSetup instances from the attachment list on the s-p-m
Iterator i = page.getExtensionAttachments().iterator();
Iterator i = pageViewport.getExtensionAttachments().iterator();
while (i.hasNext()) {
ExtensionAttachment attachment = (ExtensionAttachment)i.next();
if (AFPPageSetup.CATEGORY.equals(attachment.getCategory())) {
AFPPageSetup aps = (AFPPageSetup)attachment;
String element = aps.getElementName();
if (AFPElementMapping.INCLUDE_PAGE_OVERLAY.equals(element)) {
String overlay = aps.getName();
if (overlay != null) {
_afpDataStream.createIncludePageOverlay(overlay);
if (attachment instanceof AFPPageSetup) {
AFPPageSetup aps = (AFPPageSetup)attachment;
if (log.isDebugEnabled()) {
log.debug(aps);
}
} else if (AFPElementMapping.INCLUDE_PAGE_SEGMENT.equals(element)) {
String name = aps.getName();
String source = aps.getValue();
if (_pageSegmentsMap == null) {
_pageSegmentsMap = new HashMap();
String element = aps.getElementName();
if (AFPElementMapping.INCLUDE_PAGE_OVERLAY.equals(element)) {
String overlay = aps.getName();
if (overlay != null) {
_afpDataStream.createIncludePageOverlay(overlay);
}
} else if (AFPElementMapping.INCLUDE_PAGE_SEGMENT.equals(element)) {
String name = aps.getName();
String source = aps.getValue();
if (_pageSegmentsMap == null) {
_pageSegmentsMap = new HashMap();
}
_pageSegmentsMap.put(source, name);
} else if (AFPElementMapping.TAG_LOGICAL_ELEMENT.equals(element)) {
String name = aps.getName();
String value = aps.getValue();
if (_pageSegmentsMap == null) {
_pageSegmentsMap = new HashMap();
}
_afpDataStream.createTagLogicalElement(name, value);
} else if (AFPElementMapping.NO_OPERATION.equals(element)) {
String content = aps.getContent();
if (content != null) {
_afpDataStream.createNoOperation(content);
}
}
_pageSegmentsMap.put(source, name);
} else if (AFPElementMapping.TAG_LOGICAL_ELEMENT.equals(element)) {
String name = aps.getName();
String value = aps.getValue();
if (_pageSegmentsMap == null) {
_pageSegmentsMap = new HashMap();
}
_afpDataStream.createTagLogicalElement(name, value);
}
}
}
@@ -1419,14 +1433,17 @@ public class AFPRenderer extends AbstractPathOrientedRenderer {
return (int)Math.round(mpt / DPI_CONVERSION_FACTOR_240);
}

private void convertToGrayScaleImage(ImageObject io, byte raw[]) {
private void convertToGrayScaleImage(ImageObject io, byte[] raw) {
int pixelsPerByte = 8 / bitsPerPixel;
byte bw[] = new byte[raw.length / (3 * pixelsPerByte)];
byte[] bw = new byte[raw.length / (3 * pixelsPerByte)];
int k = 0;
for (int i = 0, j = 0; i < raw.length; i += 3, j++) {
if (j == pixelsPerByte) {
j = 0;
k++;
if (k == bw.length) {
break;
}
}
// see http://www.jguru.com/faq/view.jsp?EID=221919
double greyVal = 0.212671d * ((int) raw[i] & 0xff)

+ 1
- 11
src/java/org/apache/fop/render/afp/extensions/AFPElement.java View File

@@ -18,11 +18,11 @@
/* $Id$ */

package org.apache.fop.render.afp.extensions;

import org.apache.fop.apps.FOPException;
import org.apache.fop.fo.Constants;
import org.apache.fop.fo.FONode;
import org.apache.fop.fo.ValidationException;
import org.apache.fop.fo.XMLObj;

/**
* This class extends the org.apache.fop.extensions.ExtensionObj class. The
@@ -42,16 +42,6 @@ public class AFPElement extends AbstractAFPExtensionObject {
super(parent, name);
}

/** {@inheritDoc} */
public String getNamespaceURI() {
return AFPElementMapping.NAMESPACE;
}

/** {@inheritDoc} */
public String getNormalNamespacePrefix() {
return "afp";
}

/** {@inheritDoc} */
protected void startOfNode() throws FOPException {
super.startOfNode();

+ 11
- 0
src/java/org/apache/fop/render/afp/extensions/AFPElementMapping.java View File

@@ -45,6 +45,9 @@ public class AFPElementMapping extends ElementMapping {

public static final String INCLUDE_PAGE_SEGMENT = "include-page-segment";

/** NOP */
public static final String NO_OPERATION = "no-operation";

/**
* The namespace used for AFP extensions
*/
@@ -79,6 +82,9 @@ public class AFPElementMapping extends ElementMapping {
foObjs.put(
INCLUDE_PAGE_OVERLAY,
new AFPIncludePageOverlayMaker());
foObjs.put(
NO_OPERATION,
new AFPNoOperationMaker());
}

}
@@ -107,4 +113,9 @@ public class AFPElementMapping extends ElementMapping {
}
}

static class AFPNoOperationMaker extends ElementMapping.Maker {
public FONode make(FONode parent) {
return new AFPElement(parent, NO_OPERATION);
}
}
}

+ 48
- 5
src/java/org/apache/fop/render/afp/extensions/AFPPageSetup.java View File

@@ -22,11 +22,17 @@ package org.apache.fop.render.afp.extensions;
import java.io.Serializable;

import org.apache.fop.fo.extensions.ExtensionAttachment;
import org.apache.fop.util.XMLizable;
import org.xml.sax.ContentHandler;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.AttributesImpl;

/**
* This is the pass-through value object for the PostScript extension.
*/
public class AFPPageSetup implements ExtensionAttachment, Serializable {
public class AFPPageSetup implements ExtensionAttachment, Serializable, XMLizable {

private static final long serialVersionUID = 7190606822558332901L;

/** The category URI for this extension attachment. */
public static final String CATEGORY = "apache:fop:extensions:afp";
@@ -37,12 +43,15 @@ public class AFPPageSetup implements ExtensionAttachment, Serializable {

private String value;

private String content;

/**
* Default constructor.
* @param name the name of the setup code object, may be null
*
* @param elementName the name of the setup code object, may be null
*/
public AFPPageSetup(String name) {
this.elementName = name;
public AFPPageSetup(String elementName) {
this.elementName = elementName;
}

/** @return the name */
@@ -77,15 +86,49 @@ public class AFPPageSetup implements ExtensionAttachment, Serializable {
public void setValue(String source) {
this.value = source;
}
/** {@inheritDoc} */
public String getCategory() {
return CATEGORY;
}

/**
* @return the data
*/
public String getContent() {
return content;
}

/**
* Sets the data
* @param content The byte data to set.
*/
public void setContent(String content) {
this.content = content;
}

/** {@inheritDoc} */
public String toString() {
return "AFPPageSetup(element-name=" + getElementName() + " name=" + getName() + ")";
}

private static final String ATT_NAME = "name";
private static final String ATT_VALUE = "value";

/** {@inheritDoc} */
public void toSAX(ContentHandler handler) throws SAXException {
AttributesImpl atts = new AttributesImpl();
if (name != null && name.length() > 0) {
atts.addAttribute(null, ATT_NAME, ATT_NAME, "CDATA", name);
}
if (value != null && value.length() > 0) {
atts.addAttribute(null, ATT_VALUE, ATT_VALUE, "CDATA", value);
}
handler.startElement(CATEGORY, elementName, elementName, atts);
if (content != null && content.length() > 0) {
char[] chars = content.toCharArray();
handler.characters(chars, 0, chars.length);
}
handler.endElement(CATEGORY, elementName, elementName);
}
}

+ 14
- 16
src/java/org/apache/fop/render/afp/extensions/AbstractAFPExtensionObject.java View File

@@ -33,10 +33,13 @@ import org.xml.sax.Locator;
*/
public abstract class AbstractAFPExtensionObject extends FONode {

private AFPPageSetup setupCode = null;

private String _name = null;
/**
* AFP setup code
*/
private AFPPageSetup setupCode;
private String name;
/**
* @see org.apache.fop.fo.FONode#FONode(FONode)
* @param parent the parent formatting object
@@ -44,14 +47,11 @@ public abstract class AbstractAFPExtensionObject extends FONode {
*/
public AbstractAFPExtensionObject(FONode parent, String name) {
super(parent);
_name = name;
setupCode = new AFPPageSetup(name);
this.name = name;
this.setupCode = new AFPPageSetup(name);
}

/**
* {@inheritDoc} String, String)
* here, blocks XSL FO's from having non-FO parents.
*/
/** {@inheritDoc} */
protected void validateChildNode(Locator loc, String nsURI, String localName)
throws ValidationException {
if (FO_URI.equals(nsURI)) {
@@ -60,8 +60,9 @@ public abstract class AbstractAFPExtensionObject extends FONode {
}

/** {@inheritDoc} */
protected void addCharacters(char[] data, int start, int length,
protected void addCharacters(char[] data, int start, int end,
PropertyList pList, Locator locator) {
setupCode.setContent(new String(data, start, end - start));
}

/** {@inheritDoc} */
@@ -69,7 +70,7 @@ public abstract class AbstractAFPExtensionObject extends FONode {
return AFPElementMapping.NAMESPACE;
}

/**{@inheritDoc} */
/** {@inheritDoc} */
public String getNormalNamespacePrefix() {
return AFPElementMapping.NAMESPACE_PREFIX;
}
@@ -91,8 +92,7 @@ public abstract class AbstractAFPExtensionObject extends FONode {
} else {
throw new FOPException(elementName + " must have a src attribute.");
}
}
if (AFPElementMapping.TAG_LOGICAL_ELEMENT.equals(elementName)) {
} else if (AFPElementMapping.TAG_LOGICAL_ELEMENT.equals(elementName)) {
name = attlist.getValue("value");
if (name != null && name.length() > 0) {
setupCode.setValue(name);
@@ -114,8 +114,6 @@ public abstract class AbstractAFPExtensionObject extends FONode {

/** {@inheritDoc} */
public String getLocalName() {
return _name;
return name;
}

}


+ 8
- 3
src/java/org/apache/fop/render/afp/fonts/AFPFontReader.java View File

@@ -111,7 +111,7 @@ public final class AFPFontReader {
/**
* The collection of code pages
*/
private static HashMap _codePages = new HashMap();
private HashMap _codePages = new HashMap();

/**
* Load the font details and metrics into the CharacterSetMetric object,
@@ -120,7 +120,7 @@ public final class AFPFontReader {
*
* @param characterSet the CharacterSetMetric object to populate
*/
public static void loadCharacterSetMetric(CharacterSet characterSet) {
public void loadCharacterSetMetric(CharacterSet characterSet) {

InputStream inputStream = null;

@@ -275,6 +275,11 @@ public final class AFPFontReader {
}

File directory = new File(url.getPath());
if (!directory.canRead()) {
String msg = "Failed to read directory " + url.getPath();
log.error(msg);
throw new FileNotFoundException(msg);
}

final String filterpattern = codePage.trim();
FilenameFilter filter = new FilenameFilter() {
@@ -606,4 +611,4 @@ public final class AFPFontReader {
}
}

}
}

+ 2
- 1
src/java/org/apache/fop/render/afp/fonts/CharacterSet.java View File

@@ -250,7 +250,8 @@ public class CharacterSet {

if (!_isMetricsLoaded) {

AFPFontReader.loadCharacterSetMetric(this);
AFPFontReader afpFontReader = new AFPFontReader();
afpFontReader.loadCharacterSetMetric(this);
_isMetricsLoaded = true;

}

+ 9
- 0
src/java/org/apache/fop/render/afp/modca/AFPDataStream.java View File

@@ -570,6 +570,15 @@ public class AFPDataStream {

}

/**
* Creates a NoOperation item
*
* @param content byte data
*/
public void createNoOperation(String content) {
_currentPage.createNoOperation(content);
}

/**
* Start a new page group. When processing has finished on the current page
* group the {@link #endPageGroup()}method must be invoked to mark the page

+ 13
- 1
src/java/org/apache/fop/render/afp/modca/AbstractPageObject.java View File

@@ -356,6 +356,18 @@ public abstract class AbstractPageObject extends AbstractNamedAFPObject {

}

/**
* Creates a NoOperation on the page.
*
* @param content the byte data
*/
public void createNoOperation(String content) {

NoOperation noOp = new NoOperation(content);
_objects.add(noOp);

}

/**
* Creates an IncludePageSegment on the current page.
*
@@ -410,4 +422,4 @@ public abstract class AbstractPageObject extends AbstractNamedAFPObject {
return _rotation;
}

}
}

Loading…
Cancel
Save