[FOray-commit] SF.net SVN: foray: [9809] trunk/foray
Modular XSL-FO Implementation for Java.
Status: Alpha
Brought to you by:
victormote
|
From: <vic...@us...> - 2007-06-17 01:07:26
|
Revision: 9809
http://svn.sourceforge.net/foray/?rev=9809&view=rev
Author: victormote
Date: 2007-06-16 18:07:27 -0700 (Sat, 16 Jun 2007)
Log Message:
-----------
Use a true OutputStream to output the SVG-to-PDF work.
Modified Paths:
--------------
trunk/foray/foray-graphic/src/java/org/foray/graphic/SVGGraphic.java
trunk/foray/foray-graphic/src/java/org/foray/graphic/batik/PDFGraphics2D.java
trunk/foray/foray-pdf/src/java/org/foray/pdf/object/PDFContentStream.java
Modified: trunk/foray/foray-graphic/src/java/org/foray/graphic/SVGGraphic.java
===================================================================
--- trunk/foray/foray-graphic/src/java/org/foray/graphic/SVGGraphic.java 2007-06-16 23:55:21 UTC (rev 9808)
+++ trunk/foray/foray-graphic/src/java/org/foray/graphic/SVGGraphic.java 2007-06-17 01:07:27 UTC (rev 9809)
@@ -426,7 +426,8 @@
*/
public void drawPdfStreamContent(final OutputStream output,
final Rectangle2D location, final GraphicPdfContext pdfContext,
- final FontConsumer fontConsumer, final boolean strokeText) {
+ final FontConsumer fontConsumer, final boolean strokeText)
+ throws GraphicException {
SVGDocument doc = null;
try {
doc = this.getSVGDocument();
@@ -436,20 +437,14 @@
return;
}
final PDFGraphics2D graphics = getPDFContent4SVG(location,
- fontConsumer, doc, strokeText, pdfContext);
+ fontConsumer, doc, strokeText, pdfContext, output);
if (graphics == null) {
- // Error messages have already been logged.
- return;
+ throw new GraphicException("Unable to convert to PDF: "
+ + this.getUrl().toExternalForm());
}
- final String svgString = graphics.getString();
- final CharacterOutputStream stream = new CharacterOutputStream(output,
- true);
- try {
- stream.write(svgString);
- stream.close();
- } catch (final IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
+ if (graphics.ioErrorFound()) {
+ throw new GraphicException("I/O error(s) while converting to PDF: "
+ + this.getUrl().toExternalForm());
}
}
@@ -462,10 +457,13 @@
* @param strokeSVGText Indicates whether text should be stroked.
* @param pdfContext The PDF context in which this content is being written.
* @return The PDFGraphics2D instance for the SVG.
+ * @param outputStream The output stream to which this processor writes its
+ * PDF output.
*/
public PDFGraphics2D getPDFContent4SVG(final Rectangle2D contentRectangle,
final FontConsumer fontConsumer, final SVGDocument svgDocument,
- final boolean strokeSVGText, final GraphicPdfContext pdfContext) {
+ final boolean strokeSVGText, final GraphicPdfContext pdfContext,
+ final OutputStream outputStream) {
/* If not running in a graphical environment, log an error message
* and skip the SVG. */
if (! Environment.isGraphicalEnvironment()) {
@@ -497,7 +495,8 @@
builder = null;
final PDFGraphics2D graphics = new PDFGraphics2D(true, contentRectangle,
- svgDocument, fontConsumer, this.getLogger(), pdfContext);
+ svgDocument, fontConsumer, this.getLogger(), pdfContext,
+ outputStream);
graphics.setGraphicContext(
new org.apache.batik.ext.awt.g2d.GraphicContext());
Modified: trunk/foray/foray-graphic/src/java/org/foray/graphic/batik/PDFGraphics2D.java
===================================================================
--- trunk/foray/foray-graphic/src/java/org/foray/graphic/batik/PDFGraphics2D.java 2007-06-16 23:55:21 UTC (rev 9808)
+++ trunk/foray/foray-graphic/src/java/org/foray/graphic/batik/PDFGraphics2D.java 2007-06-17 01:07:27 UTC (rev 9809)
@@ -27,6 +27,7 @@
*/
package org.foray.graphic.batik;
+import org.foray.common.CharacterOutputStream;
import org.foray.common.WKConstants;
import org.foray.common.ps.PsColor;
import org.foray.common.ps.PsUtil;
@@ -79,7 +80,8 @@
import java.awt.image.Raster;
import java.awt.image.RenderedImage;
import java.awt.image.renderable.RenderableImage;
-import java.io.StringWriter;
+import java.io.IOException;
+import java.io.OutputStream;
import java.text.AttributedCharacterIterator;
import java.text.CharacterIterator;
import java.util.ArrayList;
@@ -116,7 +118,7 @@
private PDFDocument pdfDoc;
/** The current stream to add PDF commands to. */
- private StringWriter currentStream = new StringWriter();
+ private CharacterOutputStream currentStream;
/** The current annotation list to add annotations to. */
private PDFAnnotList currentAnnotList = null;
@@ -145,6 +147,9 @@
/** The graphics state. */
private PDFGraphicsState graphicsState;
+ /** Indicates whether any I/O Exceptions were found while processing. */
+ private boolean ioErrorFound = false;
+
/**
* Create a new PDFGraphics2D with the given pdf document info.
* This is used to create a Graphics object for use inside an already
@@ -157,12 +162,16 @@
* @param fontConsumer The font consumer for this document.
* @param logger The logger.
* @param pdfContext The PDF context in which this content is being written.
+ * @param outputStream The output stream to which this processor writes its
+ * PDF output.
*/
public PDFGraphics2D(final boolean textAsShapes,
final Rectangle2D contentRectangle, final SVGDocument svgDocument,
final FontConsumer fontConsumer, final Log logger,
- final GraphicPdfContext pdfContext) {
+ final GraphicPdfContext pdfContext,
+ final OutputStream outputStream) {
super(textAsShapes);
+ this.currentStream = new CharacterOutputStream(outputStream, true);
/* TODO: What we really need here is a new GraphicsState, not a complete
* new document. */
@@ -204,7 +213,7 @@
// transform so that the coordinates (0,0) is from the top left
// and positive is down and to the right. (0,0) is where the
// viewBox puts it.
- currentStream.write("1 0 0 -1 " + contentRectangle.getX() + " "
+ this.write("1 0 0 -1 " + contentRectangle.getX() + " "
+ contentRectangle.getY() + " cm\n");
final SVGSVGElement svg = svgDocument.getRootElement();
@@ -218,8 +227,8 @@
new double[PsUtil.MATRIX_QTY_ELEMENTS];
at.getMatrix(vals);
final String matrixString = octalMatrixToString(vals);
- currentStream.write(matrixString);
- currentStream.write("cm\n");
+ this.write(matrixString);
+ this.write("cm\n");
}
}
@@ -255,14 +264,6 @@
}
/**
- * Returns this object as the value which should be written to the PDF file.
- * @return The PDF representation of this object.
- */
- public String getString() {
- return currentStream.toString();
- }
-
- /**
* Sets the graphic context.
* @param c The new graphic context.
*/
@@ -414,14 +415,14 @@
final AffineTransform at = getTransform();
final double[] matrix = new double[PsUtil.MATRIX_QTY_ELEMENTS];
at.getMatrix(matrix);
- currentStream.write("q\n");
+ this.write("q\n");
final Shape imclip = getClip();
writeClip(imclip);
final String matrixString = matrixToString(matrix, QTY_ELEMENTS_CUBIC);
- currentStream.write(matrixString);
- currentStream.write("cm\n");
+ this.write(matrixString);
+ this.write("cm\n");
// this.xObject must be null, so this code is not used ??!!
- currentStream.write("" + width + " 0 0 " + (-height) + " " + x
+ this.write("" + width + " 0 0 " + (-height) + " " + x
+ " " + (y + height) + " cm\n" + "/"
+ this.xObject.getXObjectName() + " Do\nQ\n");
return true;
@@ -454,7 +455,7 @@
return;
}
- currentStream.write("q\n");
+ this.write("q\n");
final Shape imclip = getClip();
writeClip(imclip);
applyColor(c, false);
@@ -467,8 +468,8 @@
new double[PsUtil.MATRIX_QTY_ELEMENTS];
trans.getMatrix(tranvals);
String matrixString = octalMatrixToString(tranvals);
- currentStream.write(matrixString);
- currentStream.write("cm\n");
+ this.write(matrixString);
+ this.write("cm\n");
final PathIterator iter = s.getPathIterator(new AffineTransform());
while (!iter.isDone()) {
@@ -478,26 +479,26 @@
switch (type) {
case PathIterator.SEG_CUBICTO:
matrixString = matrixToString(vals, QTY_ELEMENTS_CUBIC);
- currentStream.write(matrixString);
- currentStream.write("c\n");
+ this.write(matrixString);
+ this.write("c\n");
break;
case PathIterator.SEG_LINETO:
matrixString = matrixToString(vals, QTY_ELEMENTS_LINEAR);
- currentStream.write(matrixString);
- currentStream.write("l\n");
+ this.write(matrixString);
+ this.write("l\n");
break;
case PathIterator.SEG_MOVETO:
matrixString = matrixToString(vals, QTY_ELEMENTS_LINEAR);
- currentStream.write(matrixString);
- currentStream.write("m\n");
+ this.write(matrixString);
+ this.write("m\n");
break;
case PathIterator.SEG_QUADTO:
matrixString = matrixToString(vals, QTY_ELEMENTS_QUAD);
- currentStream.write(matrixString);
- currentStream.write("y\n");
+ this.write(matrixString);
+ this.write("y\n");
break;
case PathIterator.SEG_CLOSE:
- currentStream.write("h\n");
+ this.write("h\n");
break;
default:
break;
@@ -505,7 +506,7 @@
iter.next();
}
doDrawing(false, true, false);
- currentStream.write("Q\n");
+ this.write("Q\n");
}
/**
@@ -525,26 +526,26 @@
switch (type) {
case PathIterator.SEG_CUBICTO:
matrixString = matrixToString(vals, QTY_ELEMENTS_CUBIC);
- currentStream.write(matrixString);
- currentStream.write("c\n");
+ this.write(matrixString);
+ this.write("c\n");
break;
case PathIterator.SEG_LINETO:
matrixString = matrixToString(vals, QTY_ELEMENTS_LINEAR);
- currentStream.write(matrixString);
- currentStream.write("l\n");
+ this.write(matrixString);
+ this.write("l\n");
break;
case PathIterator.SEG_MOVETO:
matrixString = matrixToString(vals, QTY_ELEMENTS_LINEAR);
- currentStream.write(matrixString);
- currentStream.write("m\n");
+ this.write(matrixString);
+ this.write("m\n");
break;
case PathIterator.SEG_QUADTO:
matrixString = matrixToString(vals, QTY_ELEMENTS_QUAD);
- currentStream.write(matrixString);
- currentStream.write("y\n");
+ this.write(matrixString);
+ this.write("y\n");
break;
case PathIterator.SEG_CLOSE:
- currentStream.write("h\n");
+ this.write("h\n");
break;
default:
break;
@@ -552,8 +553,8 @@
iter.next();
}
// clip area
- currentStream.write("W\n");
- currentStream.write("n\n");
+ this.write("W\n");
+ this.write("n\n");
}
/**
@@ -569,7 +570,7 @@
anyChange = this.graphicsState.setStrokeColor(color);
}
if (anyChange) {
- currentStream.write(PsColor.toPS(color, fill) + "\n");
+ this.write(PsColor.toPS(color, fill) + "\n");
}
}
@@ -620,7 +621,7 @@
final PDFPattern myPat = PDFPattern.createGradient(false,
aColorSpace,
someColors, null, theCoords, pdfDoc);
- currentStream.write(myPat.getSetterString(fill));
+ this.write(myPat.getSetterString(fill));
} else if (paint instanceof TexturePaint) { }
}
@@ -634,53 +635,53 @@
final float[] da = bs.getDashArray();
if (da != null) {
- currentStream.write("[");
+ this.write("[");
for (int count = 0; count < da.length; count++) {
if (((int) da[count]) == 0) {
// the dasharray units in pdf are (whole) numbers
// in user space units, cannot be 0
- currentStream.write("1");
+ this.write("1");
} else {
- currentStream.write("" + ((int) da[count]));
+ this.write("" + ((int) da[count]));
}
if (count < da.length - 1) {
- currentStream.write(" ");
+ this.write(" ");
}
}
- currentStream.write("] ");
+ this.write("] ");
final float offset = bs.getDashPhase();
- currentStream.write(((int) offset) + " d\n");
+ this.write(((int) offset) + " d\n");
}
final int ec = bs.getEndCap();
switch (ec) {
case BasicStroke.CAP_BUTT:
- currentStream.write(0 + " J\n");
+ this.write(0 + " J\n");
break;
case BasicStroke.CAP_ROUND:
- currentStream.write(1 + " J\n");
+ this.write(1 + " J\n");
break;
case BasicStroke.CAP_SQUARE:
- currentStream.write(2 + " J\n");
+ this.write(2 + " J\n");
break;
}
final int lj = bs.getLineJoin();
switch (lj) {
case BasicStroke.JOIN_MITER:
- currentStream.write(0 + " j\n");
+ this.write(0 + " j\n");
break;
case BasicStroke.JOIN_ROUND:
- currentStream.write(1 + " j\n");
+ this.write(1 + " j\n");
break;
case BasicStroke.JOIN_BEVEL:
- currentStream.write(2 + " j\n");
+ this.write(2 + " j\n");
break;
}
final float lw = bs.getLineWidth();
- currentStream.write(PsUtil.doubleOut(lw) + " w\n");
+ this.write(PsUtil.doubleOut(lw) + " w\n");
final float ml = bs.getMiterLimit();
- currentStream.write(PsUtil.doubleOut(ml) + " M\n");
+ this.write(PsUtil.doubleOut(ml) + " M\n");
}
}
@@ -720,10 +721,10 @@
this.graphicsState.setFont(font);
this.graphicsState.setFontSize(size);
final String fontName = this.pdfContext.getFontName(font);
- currentStream.write("/" + fontName + " " + size + " Tf\n");
+ this.write("/" + fontName + " " + size + " Tf\n");
}
- currentStream.write("q\n");
+ this.write("q\n");
final Shape imclip = getClip();
writeClip(imclip);
@@ -732,7 +733,7 @@
c = getBackground();
applyColor(c, false);
- currentStream.write("BT\n");
+ this.write("BT\n");
final AffineTransform trans = getTransform();
trans.translate(x, y);
@@ -740,17 +741,17 @@
trans.getMatrix(vals);
final String matrixString = octalMatrixToString(vals);
- currentStream.write(matrixString);
- currentStream.write("cm\n");
- currentStream.write("1 0 0 -1 0 0 Tm ");
+ this.write(matrixString);
+ this.write("cm\n");
+ this.write("1 0 0 -1 0 0 Tm ");
final PDFString pdfString = new PDFString(this.pdfDoc, s, font,
true);
final String outputString = pdfString.toPDF();
- currentStream.write(outputString);
+ this.write(outputString);
- currentStream.write("ET\n");
- currentStream.write("Q\n");
+ this.write("ET\n");
+ this.write("Q\n");
}
/**
@@ -804,7 +805,7 @@
c = getBackground();
applyColor(c, false);
- currentStream.write("BT\n");
+ this.write("BT\n");
final AffineTransform trans = getTransform();
trans.translate(x, y);
@@ -837,15 +838,15 @@
this.graphicsState.setFont(font);
this.graphicsState.setFontSize(size);
final String fontName = this.pdfContext.getFontName(font);
- currentStream.write("/" + fontName + " " + size + " Tf\n");
+ this.write("/" + fontName + " " + size + " Tf\n");
}
final String matrixString = octalMatrixToString(vals);
- currentStream.write(matrixString);
- currentStream.write(" Tm (" + ch + ") Tj\n");
+ this.write(matrixString);
+ this.write(" Tm (" + ch + ") Tj\n");
}
- currentStream.write("ET\n");
+ this.write("ET\n");
}
/**
@@ -861,7 +862,7 @@
return;
}
}
- currentStream.write("q\n");
+ this.write("q\n");
final Shape imclip = getClip();
writeClip(imclip);
c = getColor();
@@ -880,26 +881,26 @@
switch (type) {
case PathIterator.SEG_CUBICTO:
matrixString = matrixToString(vals, QTY_ELEMENTS_CUBIC);
- currentStream.write(matrixString);
- currentStream.write("c\n");
+ this.write(matrixString);
+ this.write("c\n");
break;
case PathIterator.SEG_LINETO:
matrixString = matrixToString(vals, QTY_ELEMENTS_LINEAR);
- currentStream.write(matrixString);
- currentStream.write("l\n");
+ this.write(matrixString);
+ this.write("l\n");
break;
case PathIterator.SEG_MOVETO:
matrixString = matrixToString(vals, QTY_ELEMENTS_LINEAR);
- currentStream.write(matrixString);
- currentStream.write("m\n");
+ this.write(matrixString);
+ this.write("m\n");
break;
case PathIterator.SEG_QUADTO:
matrixString = matrixToString(vals, QTY_ELEMENTS_QUAD);
- currentStream.write(matrixString);
- currentStream.write("y\n");
+ this.write(matrixString);
+ this.write("y\n");
break;
case PathIterator.SEG_CLOSE:
- currentStream.write("h\n");
+ this.write("h\n");
break;
default:
break;
@@ -908,7 +909,7 @@
}
doDrawing(true, false,
iter.getWindingRule() == PathIterator.WIND_EVEN_ODD);
- currentStream.write("Q\n");
+ this.write("Q\n");
}
/**
@@ -922,20 +923,20 @@
if (fill) {
if (stroke) {
if (nonzero) {
- currentStream.write("B*\n");
+ this.write("B*\n");
} else {
- currentStream.write("B\n");
+ this.write("B\n");
}
} else {
if (nonzero) {
- currentStream.write("f*\n");
+ this.write("f*\n");
} else {
- currentStream.write("f\n");
+ this.write("f\n");
}
}
} else {
// if(stroke)
- currentStream.write("S\n");
+ this.write("S\n");
}
}
@@ -986,4 +987,26 @@
this.logger.info("copyArea");
}
+ /**
+ * Writes some content.
+ * @param text The content to be written.
+ */
+ private void write(final String text) {
+ try {
+ this.currentStream.write(text);
+ } catch (final IOException e) {
+ this.ioErrorFound = true;
+ }
+ }
+
+ /**
+ * Indicates whether any I/O errors were found while writing to the output
+ * stream.
+ * @return True iff there were I/O errors while writing to the output
+ * stream.
+ */
+ public boolean ioErrorFound() {
+ return this.ioErrorFound;
+ }
+
}
Modified: trunk/foray/foray-pdf/src/java/org/foray/pdf/object/PDFContentStream.java
===================================================================
--- trunk/foray/foray-pdf/src/java/org/foray/pdf/object/PDFContentStream.java 2007-06-16 23:55:21 UTC (rev 9808)
+++ trunk/foray/foray-pdf/src/java/org/foray/pdf/object/PDFContentStream.java 2007-06-17 01:07:27 UTC (rev 9809)
@@ -331,8 +331,13 @@
add("n" + EOL);
}
- svg.drawPdfStreamContent(this.getOutputStream(), contentRectangle,
- this.getPDFDocument(), fontConsumer, strokeSVGText);
+ try {
+ svg.drawPdfStreamContent(this.getOutputStream(), contentRectangle,
+ this.getPDFDocument(), fontConsumer, strokeSVGText);
+ } catch (final GraphicException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
/* Add any links found in the SVG to the current page. */
final GraphicLink[] graphicLinks = svg.getLinks();
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|