You are viewing a plain text version of this content. The canonical link for it is here.
Posted to fop-commits@xmlgraphics.apache.org by je...@apache.org on 2008/08/05 17:31:02 UTC
svn commit: r682757 [2/2] - in
/xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src:
java/META-INF/services/ java/org/apache/fop/render/
java/org/apache/fop/render/intermediate/ java/org/apache/fop/render/pdf/
java/org/apache/fop/svg/ sandbox/META-INF/...
Modified: xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/java/org/apache/fop/render/pdf/PDFPainter.java
URL: http://svn.apache.org/viewvc/xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/java/org/apache/fop/render/pdf/PDFPainter.java?rev=682757&r1=682756&r2=682757&view=diff
==============================================================================
--- xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/java/org/apache/fop/render/pdf/PDFPainter.java (original)
+++ xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/java/org/apache/fop/render/pdf/PDFPainter.java Tue Aug 5 08:31:00 2008
@@ -24,16 +24,23 @@
import java.awt.Paint;
import java.awt.Rectangle;
import java.awt.geom.AffineTransform;
+import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
+import org.apache.xmlgraphics.image.loader.ImageException;
+import org.apache.xmlgraphics.image.loader.ImageInfo;
+import org.apache.xmlgraphics.image.loader.ImageManager;
+import org.apache.xmlgraphics.image.loader.ImageSessionContext;
+import org.apache.xmlgraphics.image.loader.util.ImageUtil;
import org.apache.xmlgraphics.xmp.Metadata;
import org.apache.fop.apps.FOUserAgent;
import org.apache.fop.apps.MimeConstants;
+import org.apache.fop.events.ResourceEventProducer;
import org.apache.fop.fo.extensions.xmp.XMPMetadata;
import org.apache.fop.fonts.Font;
import org.apache.fop.fonts.FontTriplet;
@@ -42,19 +49,17 @@
import org.apache.fop.fonts.Typeface;
import org.apache.fop.pdf.PDFAnnotList;
import org.apache.fop.pdf.PDFDocument;
-import org.apache.fop.pdf.PDFFilterList;
import org.apache.fop.pdf.PDFNumber;
import org.apache.fop.pdf.PDFPage;
import org.apache.fop.pdf.PDFResourceContext;
import org.apache.fop.pdf.PDFResources;
-import org.apache.fop.pdf.PDFState;
-import org.apache.fop.pdf.PDFStream;
import org.apache.fop.pdf.PDFTextUtil;
+import org.apache.fop.pdf.PDFXObject;
+import org.apache.fop.render.ImageHandler;
import org.apache.fop.render.intermediate.AbstractBinaryWritingIFPainter;
import org.apache.fop.render.intermediate.IFException;
import org.apache.fop.render.intermediate.IFState;
import org.apache.fop.util.CharUtilities;
-import org.apache.fop.util.ColorUtil;
/**
* IFPainter implementation that produces PDF.
@@ -79,8 +84,8 @@
/** the /Resources object of the PDF document being created */
protected PDFResources pdfResources;
- /** the current stream to add PDF commands to */
- protected PDFStream currentStream;
+ /** The current content generator */
+ protected PDFContentGenerator generator;
/** the current annotation list to add annotations to */
protected PDFResourceContext currentContext;
@@ -98,16 +103,6 @@
/** the current page's PDF reference string (to avoid numerous function calls) */
protected String currentPageRef;
- /** drawing state */
- protected PDFState currentState;
-
- /** Text generation utility holding the current font status */
- protected PDFTextUtil textutil;
-
-
- /** Image handler registry */
- private PDFImageHandlerRegistry imageHandlerRegistry = new PDFImageHandlerRegistry();
-
/**
* Default constructor.
*/
@@ -173,11 +168,9 @@
//pageReferences.clear();
pdfResources = null;
- currentStream = null;
+ this.generator = null;
currentContext = null;
currentPage = null;
- currentState = null;
- this.textutil = null;
//idPositions.clear();
//idGoTos.clear();
@@ -213,20 +206,11 @@
currentPageRef = currentPage.referencePDF();
- currentStream = this.pdfDoc.getFactory()
- .makeStream(PDFFilterList.CONTENT_FILTER, false);
- this.textutil = new PDFTextUtil() {
- protected void write(String code) {
- currentStream.add(code);
- }
- };
-
- currentState = new PDFState();
+ this.generator = new PDFContentGenerator(this.pdfDoc, this.outputStream, this.currentPage);
// Transform the PDF's default coordinate system (0,0 at lower left) to the PDFPainter's
AffineTransform basicPageTransform = new AffineTransform(1, 0, 0, -1, 0,
size.height);
- currentState.concatenate(basicPageTransform);
- currentStream.add(CTMHelper.toPDFString(basicPageTransform, true) + " cm\n");
+ generator.concatenate(basicPageTransform, true);
}
/** {@inheritDoc} */
@@ -258,80 +242,136 @@
/** {@inheritDoc} */
public void endPage() throws IFException {
try {
- this.pdfDoc.registerObject(currentStream);
- currentPage.setContents(currentStream);
+ this.pdfDoc.registerObject(generator.getStream());
+ currentPage.setContents(generator.getStream());
PDFAnnotList annots = currentPage.getAnnotations();
if (annots != null) {
this.pdfDoc.addObject(annots);
}
this.pdfDoc.addObject(currentPage);
- this.pdfDoc.output(this.outputStream);
- this.textutil = null;
+ this.generator.flushPDFDoc();
+ this.generator = null;
} catch (IOException ioe) {
throw new IFException("I/O error in endPage()", ioe);
}
}
/** {@inheritDoc} */
- private void saveGraphicsState() {
- //endTextObject();
- currentState.push();
- this.state = this.state.push();
- currentStream.add("q\n");
- }
-
- private void restoreGraphicsState(boolean popState) {
- endTextObject();
- currentStream.add("Q\n");
- if (popState) {
- currentState.pop();
- this.state = this.state.pop();
- }
- }
-
- private void restoreGraphicsState() {
- restoreGraphicsState(true);
- }
-
- /** {@inheritDoc} */
public void startViewport(AffineTransform transform, Dimension size, Rectangle clipRect)
throws IFException {
- saveGraphicsState();
- currentStream.add(CTMHelper.toPDFString(transform, true) + " cm\n");
+ generator.saveGraphicsState();
+ generator.add(CTMHelper.toPDFString(transform, true) + " cm\n");
if (clipRect != null) {
StringBuffer sb = new StringBuffer();
sb.append(format(clipRect.x)).append(' ');
sb.append(format(clipRect.y)).append(' ');
sb.append(format(clipRect.width)).append(' ');
sb.append(format(clipRect.height)).append(" re W n\n");
- currentStream.add(sb.toString());
+ generator.add(sb.toString());
}
}
/** {@inheritDoc} */
- public void startGroup(AffineTransform transform) throws IFException {
- saveGraphicsState();
- currentStream.add(CTMHelper.toPDFString(transform, true) + " cm\n");
+ public void endViewport() throws IFException {
+ generator.restoreGraphicsState();
}
/** {@inheritDoc} */
- public void endGroup() throws IFException {
- restoreGraphicsState();
+ public void startGroup(AffineTransform transform) throws IFException {
+ generator.saveGraphicsState();
+ generator.add(CTMHelper.toPDFString(transform, true) + " cm\n");
}
/** {@inheritDoc} */
- public void endViewport() throws IFException {
- restoreGraphicsState();
+ public void endGroup() throws IFException {
+ generator.restoreGraphicsState();
}
/** {@inheritDoc} */
- public void startImage(Rectangle rect) throws IFException {
- // TODO Auto-generated method stub
+ public void drawImage(String uri, Rectangle rect, Map foreignAttributes) throws IFException {
+ PDFXObject xobject = pdfDoc.getXObject(uri);
+ if (xobject != null) {
+ placeImage(rect, xobject);
+ return;
+ }
+
+ ImageManager manager = getUserAgent().getFactory().getImageManager();
+ ImageInfo info = null;
+ try {
+ ImageSessionContext sessionContext = getUserAgent().getImageSessionContext();
+ info = manager.getImageInfo(uri, sessionContext);
+
+ PDFRenderingContext pdfContext = new PDFRenderingContext(
+ getUserAgent(), generator, currentPage, getFontInfo());
+
+ Map hints = ImageUtil.getDefaultHints(sessionContext);
+ org.apache.xmlgraphics.image.loader.Image img = manager.getImage(
+ info, imageHandlerRegistry.getSupportedFlavors(pdfContext),
+ hints, sessionContext);
+
+ //First check for a dynamically registered handler
+ ImageHandler handler = imageHandlerRegistry.getHandler(pdfContext, img);
+ if (handler == null) {
+ throw new UnsupportedOperationException(
+ "No ImageHandler available for image: "
+ + info + " (" + img.getClass().getName() + ")");
+ }
+
+ if (log.isDebugEnabled()) {
+ log.debug("Using ImageHandler: " + handler.getClass().getName());
+ }
+ try {
+ //TODO foreign attributes
+ handler.handleImage(pdfContext, img, rect);
+ } catch (IOException ioe) {
+ ResourceEventProducer eventProducer = ResourceEventProducer.Provider.get(
+ getUserAgent().getEventBroadcaster());
+ eventProducer.imageWritingError(this, ioe);
+ return;
+ }
+ } catch (ImageException ie) {
+ ResourceEventProducer eventProducer = ResourceEventProducer.Provider.get(
+ getUserAgent().getEventBroadcaster());
+ eventProducer.imageError(this, (info != null ? info.toString() : uri), ie, null);
+ } catch (FileNotFoundException fe) {
+ ResourceEventProducer eventProducer = ResourceEventProducer.Provider.get(
+ getUserAgent().getEventBroadcaster());
+ eventProducer.imageNotFound(this, (info != null ? info.toString() : uri), fe, null);
+ } catch (IOException ioe) {
+ ResourceEventProducer eventProducer = ResourceEventProducer.Provider.get(
+ getUserAgent().getEventBroadcaster());
+ eventProducer.imageIOError(this, (info != null ? info.toString() : uri), ioe, null);
+ }
+ // output new data
+ try {
+ generator.flushPDFDoc();
+ } catch (IOException ioe) {
+ throw new IFException("I/O error flushing the PDF document", ioe);
+ }
+ }
+
+ /**
+ * Places a previously registered image at a certain place on the page.
+ * @param x X coordinate
+ * @param y Y coordinate
+ * @param w width for image
+ * @param h height for image
+ * @param xobj the image XObject
+ */
+ private void placeImage(Rectangle rect, PDFXObject xobj) {
+ generator.saveGraphicsState();
+ generator.add(format(rect.width) + " 0 0 "
+ + format(-rect.height) + " "
+ + format(rect.x) + " "
+ + format(rect.y + rect.height )
+ + " cm " + xobj.getName() + " Do\n");
+ generator.restoreGraphicsState();
}
+
/** {@inheritDoc} */
- public void drawImage(String uri, Rectangle rect) throws IFException {
+ public void startImage(Rectangle rect) throws IFException {
// TODO Auto-generated method stub
}
@@ -348,14 +388,6 @@
}
- private static String toString(Paint paint) {
- if (paint instanceof Color) {
- return ColorUtil.colorToString((Color)paint);
- } else {
- throw new UnsupportedOperationException("Paint not supported: " + paint);
- }
- }
-
/**
* Formats a integer value (normally coordinates in millipoints) to a String.
* @param value the value (in millipoints)
@@ -365,37 +397,16 @@
return PDFNumber.doubleOut(value / 1000f);
}
- /**
- * Establishes a new foreground or fill color.
- * @param col the color to apply (null skips this operation)
- * @param fill true to set the fill color, false for the foreground color
- */
- private void updateColor(Color col, boolean fill) {
- if (col == null) {
- return;
- }
- boolean update = false;
- if (fill) {
- update = currentState.setBackColor(col);
- } else {
- update = currentState.setColor(col);
- }
-
- if (update) {
- pdfUtil.setColor(col, fill, this.currentStream);
- }
- }
-
/** {@inheritDoc} */
public void drawRect(Rectangle rect, Paint fill, Color stroke) throws IFException {
if (fill == null && stroke == null) {
return;
}
- endTextObject();
+ generator.endTextObject();
if (rect.width != 0 && rect.height != 0) {
if (fill != null) {
if (fill instanceof Color) {
- updateColor((Color)fill, true);
+ generator.updateColor((Color)fill, true, null);
} else {
throw new UnsupportedOperationException("Non-Color paints NYI");
}
@@ -415,21 +426,7 @@
sb.append(" S");
}
sb.append('\n');
- currentStream.add(sb.toString());
- }
- }
-
- /** Indicates the beginning of a text object. */
- private void beginTextObject() {
- if (!textutil.isInTextObject()) {
- textutil.beginTextObject();
- }
- }
-
- /** Indicates the end of a text object. */
- private void endTextObject() {
- if (textutil.isInTextObject()) {
- textutil.endTextObject();
+ generator.add(sb.toString());
}
}
@@ -447,14 +444,14 @@
/** {@inheritDoc} */
public void drawText(int x, int y, int[] dx, int[] dy, String text) throws IFException {
//Note: dy is currently ignored
- beginTextObject();
+ generator.beginTextObject();
FontTriplet triplet = new FontTriplet(
state.getFontFamily(), state.getFontStyle(), state.getFontWeight());
//TODO Ignored: state.getFontVariant()
String fontKey = fontInfo.getInternalFontKey(triplet);
int sizeMillipoints = state.getFontSize();
float fontSize = sizeMillipoints / 1000f;
- updateColor(state.getTextColor(), true);
+ generator.updateColor(state.getTextColor(), true, null);
// This assumes that *all* CIDFonts use a /ToUnicode mapping
Typeface tf = getTypeface(fontKey);
@@ -465,6 +462,7 @@
Font font = fontInfo.getFontInstance(triplet, sizeMillipoints);
String fontName = font.getFontName();
+ PDFTextUtil textutil = generator.getTextUtil();
textutil.updateTf(fontKey, fontSize, tf.isMultiByte());
textutil.writeTextMatrix(new AffineTransform(1, 0, 0, -1, x / 1000f, y / 1000f));
Modified: xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/java/org/apache/fop/render/pdf/PDFPainterMaker.java
URL: http://svn.apache.org/viewvc/xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/java/org/apache/fop/render/pdf/PDFPainterMaker.java?rev=682757&r1=682756&r2=682757&view=diff
==============================================================================
--- xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/java/org/apache/fop/render/pdf/PDFPainterMaker.java (original)
+++ xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/java/org/apache/fop/render/pdf/PDFPainterMaker.java Tue Aug 5 08:31:00 2008
@@ -30,7 +30,8 @@
*/
public class PDFPainterMaker extends AbstractIFPainterMaker {
- private static final String[] MIMES = new String[] {MimeConstants.MIME_PDF};
+ //TODO Revert to normal MIME after stabilization!
+ private static final String[] MIMES = new String[] {MimeConstants.MIME_PDF + ";mode=painter"};
/** {@inheritDoc} */
public IFPainter makePainter(FOUserAgent ua) {
Modified: xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/java/org/apache/fop/render/pdf/PDFRenderer.java
URL: http://svn.apache.org/viewvc/xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/java/org/apache/fop/render/pdf/PDFRenderer.java?rev=682757&r1=682756&r2=682757&view=diff
==============================================================================
--- xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/java/org/apache/fop/render/pdf/PDFRenderer.java (original)
+++ xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/java/org/apache/fop/render/pdf/PDFRenderer.java Tue Aug 5 08:31:00 2008
@@ -76,7 +76,6 @@
import org.apache.fop.pdf.PDFDocument;
import org.apache.fop.pdf.PDFEncryptionParams;
import org.apache.fop.pdf.PDFFactory;
-import org.apache.fop.pdf.PDFFilterList;
import org.apache.fop.pdf.PDFGoTo;
import org.apache.fop.pdf.PDFInfo;
import org.apache.fop.pdf.PDFLink;
@@ -86,7 +85,6 @@
import org.apache.fop.pdf.PDFResourceContext;
import org.apache.fop.pdf.PDFResources;
import org.apache.fop.pdf.PDFState;
-import org.apache.fop.pdf.PDFStream;
import org.apache.fop.pdf.PDFTextUtil;
import org.apache.fop.pdf.PDFXMode;
import org.apache.fop.pdf.PDFXObject;
@@ -167,10 +165,8 @@
*/
protected PDFResources pdfResources;
- /**
- * the current stream to add PDF commands to
- */
- protected PDFStream currentStream;
+ /** The current content generator to produce PDF commands with */
+ protected PDFContentGenerator generator;
/**
* the current annotation list to add annotations to
@@ -187,11 +183,6 @@
*/
protected String currentPageRef;
- /** drawing state */
- protected PDFState currentState = null;
-
- /** Text generation utility holding the current font status */
- protected PDFTextUtil textutil;
/** page height */
protected int pageHeight;
@@ -214,6 +205,14 @@
return this.pdfUtil;
}
+ PDFContentGenerator getGenerator() {
+ return this.generator;
+ }
+
+ PDFState getState() {
+ return getGenerator().getState();
+ }
+
/** {@inheritDoc} */
public void startRenderer(OutputStream stream) throws IOException {
if (userAgent == null) {
@@ -258,11 +257,12 @@
pageReferences.clear();
//pvReferences.clear();
pdfResources = null;
- currentStream = null;
+ this.generator = null;
+ //currentStream = null;
currentContext = null;
currentPage = null;
- currentState = null;
- this.textutil = null;
+ //currentState = null;
+ //this.textutil = null;
idPositions.clear();
idGoTos.clear();
@@ -354,48 +354,24 @@
return new PDFGraphics2DAdapter(this);
}
- /**
- * writes out a comment.
- * @param text text for the comment
- */
- protected void comment(String text) {
- if (WRITE_COMMENTS) {
- currentStream.add("% " + text + "\n");
- }
- }
-
/** {@inheritDoc} */
protected void saveGraphicsState() {
- endTextObject();
- currentState.push();
- currentStream.add("q\n");
- }
-
- private void restoreGraphicsState(boolean popState) {
- endTextObject();
- currentStream.add("Q\n");
- if (popState) {
- currentState.pop();
- }
+ generator.saveGraphicsState();
}
/** {@inheritDoc} */
protected void restoreGraphicsState() {
- restoreGraphicsState(true);
+ generator.restoreGraphicsState();
}
/** Indicates the beginning of a text object. */
protected void beginTextObject() {
- if (!textutil.isInTextObject()) {
- textutil.beginTextObject();
- }
+ generator.beginTextObject();
}
/** Indicates the end of a text object. */
protected void endTextObject() {
- if (textutil.isInTextObject()) {
- textutil.endTextObject();
- }
+ generator.endTextObject();
}
/**
@@ -483,6 +459,8 @@
double h = bounds.getHeight();
pageHeight = (int) h;
+ this.generator = new PDFContentGenerator(this.pdfDoc, this.ostream, this.currentPage);
+ /*
currentStream = this.pdfDoc.getFactory()
.makeStream(PDFFilterList.CONTENT_FILTER, false);
this.textutil = new PDFTextUtil() {
@@ -492,31 +470,37 @@
};
currentState = new PDFState();
+ */
// Transform the PDF's default coordinate system (0,0 at lower left) to the PDFRenderer's
AffineTransform basicPageTransform = new AffineTransform(1, 0, 0, -1, 0,
pageHeight / 1000f);
+ generator.concatenate(basicPageTransform);
+ /*
currentState.concatenate(basicPageTransform);
currentStream.add(CTMHelper.toPDFString(basicPageTransform, false) + " cm\n");
+ */
super.renderPage(page);
- this.pdfDoc.registerObject(currentStream);
- currentPage.setContents(currentStream);
+ this.pdfDoc.registerObject(generator.getStream());
+ currentPage.setContents(generator.getStream());
PDFAnnotList annots = currentPage.getAnnotations();
if (annots != null) {
this.pdfDoc.addObject(annots);
}
this.pdfDoc.addObject(currentPage);
- this.pdfDoc.output(ostream);
- this.textutil = null;
+ this.generator.flushPDFDoc();
+ this.generator = null;
}
/** {@inheritDoc} */
protected void startVParea(CTM ctm, Rectangle2D clippingRect) {
saveGraphicsState();
// Set the given CTM in the graphics state
+ /*
currentState.concatenate(
new AffineTransform(CTMHelper.toPDFArray(ctm)));
+ */
if (clippingRect != null) {
clipRect((float)clippingRect.getX() / 1000f,
@@ -525,7 +509,8 @@
(float)clippingRect.getHeight() / 1000f);
}
// multiply with current CTM
- currentStream.add(CTMHelper.toPDFString(ctm) + " cm\n");
+ generator.concatenate(new AffineTransform(CTMHelper.toPDFArray(ctm)));
+ //currentStream.add(CTMHelper.toPDFString(ctm) + " cm\n");
}
/** {@inheritDoc} */
@@ -535,10 +520,12 @@
/** {@inheritDoc} */
protected void concatenateTransformationMatrix(AffineTransform at) {
+ generator.concatenate(at);
+ /*
if (!at.isIdentity()) {
currentState.concatenate(at);
currentStream.add(CTMHelper.toPDFString(at, false) + " cm\n");
- }
+ }*/
}
/**
@@ -562,7 +549,7 @@
}
switch (style) {
case Constants.EN_DASHED:
- setColor(col, false, null);
+ generator.setColor(col, false);
if (horz) {
float unit = Math.abs(2 * h);
int rep = (int)(w / unit);
@@ -570,10 +557,10 @@
rep++;
}
unit = w / rep;
- currentStream.add("[" + format(unit) + "] 0 d ");
- currentStream.add(format(h) + " w\n");
+ generator.add("[" + format(unit) + "] 0 d ");
+ generator.add(format(h) + " w\n");
float ym = y1 + (h / 2);
- currentStream.add(format(x1) + " " + format(ym) + " m "
+ generator.add(format(x1) + " " + format(ym) + " m "
+ format(x2) + " " + format(ym) + " l S\n");
} else {
float unit = Math.abs(2 * w);
@@ -582,16 +569,16 @@
rep++;
}
unit = h / rep;
- currentStream.add("[" + format(unit) + "] 0 d ");
- currentStream.add(format(w) + " w\n");
+ generator.add("[" + format(unit) + "] 0 d ");
+ generator.add(format(w) + " w\n");
float xm = x1 + (w / 2);
- currentStream.add(format(xm) + " " + format(y1) + " m "
+ generator.add(format(xm) + " " + format(y1) + " m "
+ format(xm) + " " + format(y2) + " l S\n");
}
break;
case Constants.EN_DOTTED:
- setColor(col, false, null);
- currentStream.add("1 J ");
+ generator.setColor(col, false);
+ generator.add("1 J ");
if (horz) {
float unit = Math.abs(2 * h);
int rep = (int)(w / unit);
@@ -599,10 +586,10 @@
rep++;
}
unit = w / rep;
- currentStream.add("[0 " + format(unit) + "] 0 d ");
- currentStream.add(format(h) + " w\n");
+ generator.add("[0 " + format(unit) + "] 0 d ");
+ generator.add(format(h) + " w\n");
float ym = y1 + (h / 2);
- currentStream.add(format(x1) + " " + format(ym) + " m "
+ generator.add(format(x1) + " " + format(ym) + " m "
+ format(x2) + " " + format(ym) + " l S\n");
} else {
float unit = Math.abs(2 * w);
@@ -611,33 +598,33 @@
rep++;
}
unit = h / rep;
- currentStream.add("[0 " + format(unit) + " ] 0 d ");
- currentStream.add(format(w) + " w\n");
+ generator.add("[0 " + format(unit) + " ] 0 d ");
+ generator.add(format(w) + " w\n");
float xm = x1 + (w / 2);
- currentStream.add(format(xm) + " " + format(y1) + " m "
+ generator.add(format(xm) + " " + format(y1) + " m "
+ format(xm) + " " + format(y2) + " l S\n");
}
break;
case Constants.EN_DOUBLE:
- setColor(col, false, null);
- currentStream.add("[] 0 d ");
+ generator.setColor(col, false);
+ generator.add("[] 0 d ");
if (horz) {
float h3 = h / 3;
- currentStream.add(format(h3) + " w\n");
+ generator.add(format(h3) + " w\n");
float ym1 = y1 + (h3 / 2);
float ym2 = ym1 + h3 + h3;
- currentStream.add(format(x1) + " " + format(ym1) + " m "
+ generator.add(format(x1) + " " + format(ym1) + " m "
+ format(x2) + " " + format(ym1) + " l S\n");
- currentStream.add(format(x1) + " " + format(ym2) + " m "
+ generator.add(format(x1) + " " + format(ym2) + " m "
+ format(x2) + " " + format(ym2) + " l S\n");
} else {
float w3 = w / 3;
- currentStream.add(format(w3) + " w\n");
+ generator.add(format(w3) + " w\n");
float xm1 = x1 + (w3 / 2);
float xm2 = xm1 + w3 + w3;
- currentStream.add(format(xm1) + " " + format(y1) + " m "
+ generator.add(format(xm1) + " " + format(y1) + " m "
+ format(xm1) + " " + format(y2) + " l S\n");
- currentStream.add(format(xm2) + " " + format(y1) + " m "
+ generator.add(format(xm2) + " " + format(y1) + " m "
+ format(xm2) + " " + format(y2) + " l S\n");
}
break;
@@ -645,36 +632,36 @@
case Constants.EN_RIDGE:
{
float colFactor = (style == EN_GROOVE ? 0.4f : -0.4f);
- currentStream.add("[] 0 d ");
+ generator.add("[] 0 d ");
if (horz) {
Color uppercol = lightenColor(col, -colFactor);
Color lowercol = lightenColor(col, colFactor);
float h3 = h / 3;
- currentStream.add(format(h3) + " w\n");
+ generator.add(format(h3) + " w\n");
float ym1 = y1 + (h3 / 2);
- setColor(uppercol, false, null);
- currentStream.add(format(x1) + " " + format(ym1) + " m "
+ generator.setColor(uppercol, false);
+ generator.add(format(x1) + " " + format(ym1) + " m "
+ format(x2) + " " + format(ym1) + " l S\n");
- setColor(col, false, null);
- currentStream.add(format(x1) + " " + format(ym1 + h3) + " m "
+ generator.setColor(col, false);
+ generator.add(format(x1) + " " + format(ym1 + h3) + " m "
+ format(x2) + " " + format(ym1 + h3) + " l S\n");
- setColor(lowercol, false, null);
- currentStream.add(format(x1) + " " + format(ym1 + h3 + h3) + " m "
+ generator.setColor(lowercol, false);
+ generator.add(format(x1) + " " + format(ym1 + h3 + h3) + " m "
+ format(x2) + " " + format(ym1 + h3 + h3) + " l S\n");
} else {
Color leftcol = lightenColor(col, -colFactor);
Color rightcol = lightenColor(col, colFactor);
float w3 = w / 3;
- currentStream.add(format(w3) + " w\n");
+ generator.add(format(w3) + " w\n");
float xm1 = x1 + (w3 / 2);
- setColor(leftcol, false, null);
- currentStream.add(format(xm1) + " " + format(y1) + " m "
+ generator.setColor(leftcol, false);
+ generator.add(format(xm1) + " " + format(y1) + " m "
+ format(xm1) + " " + format(y2) + " l S\n");
- setColor(col, false, null);
- currentStream.add(format(xm1 + w3) + " " + format(y1) + " m "
+ generator.setColor(col, false);
+ generator.add(format(xm1 + w3) + " " + format(y1) + " m "
+ format(xm1 + w3) + " " + format(y2) + " l S\n");
- setColor(rightcol, false, null);
- currentStream.add(format(xm1 + w3 + w3) + " " + format(y1) + " m "
+ generator.setColor(rightcol, false);
+ generator.add(format(xm1 + w3 + w3) + " " + format(y1) + " m "
+ format(xm1 + w3 + w3) + " " + format(y2) + " l S\n");
}
break;
@@ -683,21 +670,21 @@
case Constants.EN_OUTSET:
{
float colFactor = (style == EN_OUTSET ? 0.4f : -0.4f);
- currentStream.add("[] 0 d ");
+ generator.add("[] 0 d ");
Color c = col;
if (horz) {
c = lightenColor(c, (startOrBefore ? 1 : -1) * colFactor);
- currentStream.add(format(h) + " w\n");
+ generator.add(format(h) + " w\n");
float ym1 = y1 + (h / 2);
- setColor(c, false, null);
- currentStream.add(format(x1) + " " + format(ym1) + " m "
+ generator.setColor(c, false);
+ generator.add(format(x1) + " " + format(ym1) + " m "
+ format(x2) + " " + format(ym1) + " l S\n");
} else {
c = lightenColor(c, (startOrBefore ? 1 : -1) * colFactor);
- currentStream.add(format(w) + " w\n");
+ generator.add(format(w) + " w\n");
float xm1 = x1 + (w / 2);
- setColor(c, false, null);
- currentStream.add(format(xm1) + " " + format(y1) + " m "
+ generator.setColor(c, false);
+ generator.add(format(xm1) + " " + format(y1) + " m "
+ format(xm1) + " " + format(y2) + " l S\n");
}
break;
@@ -705,36 +692,25 @@
case Constants.EN_HIDDEN:
break;
default:
- setColor(col, false, null);
- currentStream.add("[] 0 d ");
+ generator.setColor(col, false);
+ generator.add("[] 0 d ");
if (horz) {
- currentStream.add(format(h) + " w\n");
+ generator.add(format(h) + " w\n");
float ym = y1 + (h / 2);
- currentStream.add(format(x1) + " " + format(ym) + " m "
+ generator.add(format(x1) + " " + format(ym) + " m "
+ format(x2) + " " + format(ym) + " l S\n");
} else {
- currentStream.add(format(w) + " w\n");
+ generator.add(format(w) + " w\n");
float xm = x1 + (w / 2);
- currentStream.add(format(xm) + " " + format(y1) + " m "
+ generator.add(format(xm) + " " + format(y1) + " m "
+ format(xm) + " " + format(y2) + " l S\n");
}
}
}
- /**
- * Sets the current line width in points.
- * @param width line width in points
- */
- private void updateLineWidth(float width) {
- if (currentState.setLineWidth(width)) {
- //Only write if value has changed WRT the current line width
- currentStream.add(format(width) + " w\n");
- }
- }
-
/** {@inheritDoc} */
protected void clipRect(float x, float y, float width, float height) {
- currentStream.add(format(x) + " " + format(y) + " "
+ generator.add(format(x) + " " + format(y) + " "
+ format(width) + " " + format(height) + " re ");
clip();
}
@@ -743,8 +719,7 @@
* Clip an area.
*/
protected void clip() {
- currentStream.add("W\n");
- currentStream.add("n\n");
+ generator.add("W\n" + "n\n");
}
/**
@@ -753,7 +728,7 @@
* @param y y coordinate
*/
protected void moveTo(float x, float y) {
- currentStream.add(format(x) + " " + format(y) + " m ");
+ generator.add(format(x) + " " + format(y) + " m ");
}
/**
@@ -763,7 +738,7 @@
* @param y y coordinate
*/
protected void lineTo(float x, float y) {
- currentStream.add(format(x) + " " + format(y) + " l ");
+ generator.add(format(x) + " " + format(y) + " l ");
}
/**
@@ -771,7 +746,7 @@
* the current point to the starting point of the subpath.
*/
protected void closePath() {
- currentStream.add("h ");
+ generator.add("h ");
}
/**
@@ -779,7 +754,7 @@
*/
protected void fillRect(float x, float y, float w, float h) {
if (w != 0 && h != 0) {
- currentStream.add(format(x) + " " + format(y) + " "
+ generator.add(format(x) + " " + format(y) + " "
+ format(w) + " " + format(h) + " re f\n");
}
}
@@ -793,8 +768,8 @@
* @param endy the y end position
*/
private void drawLine(float startx, float starty, float endx, float endy) {
- currentStream.add(format(startx) + " " + format(starty) + " m ");
- currentStream.add(format(endx) + " " + format(endy) + " l S\n");
+ generator.add(format(startx) + " " + format(starty) + " m ");
+ generator.add(format(endx) + " " + format(endy) + " l S\n");
}
/**
@@ -805,15 +780,15 @@
List breakOutList = new java.util.ArrayList();
PDFState.Data data;
while (true) {
- data = currentState.getData();
- if (currentState.pop() == null) {
+ data = getState().getData();
+ if (getState().pop() == null) {
break;
}
if (breakOutList.size() == 0) {
- comment("------ break out!");
+ generator.comment("------ break out!");
}
breakOutList.add(0, data); //Insert because of stack-popping
- restoreGraphicsState(false);
+ generator.restoreGraphicsState();
}
return breakOutList;
}
@@ -823,7 +798,7 @@
* @param breakOutList the state stack to restore.
*/
protected void restoreStateStackAfterBreakOut(List breakOutList) {
- comment("------ restoring context after break-out...");
+ generator.comment("------ restoring context after break-out...");
PDFState.Data data;
Iterator i = breakOutList.iterator();
while (i.hasNext()) {
@@ -835,7 +810,7 @@
//Left out for now because all this painting stuff is very
//inconsistent. Some values go over PDFState, some don't.
}
- comment("------ done.");
+ generator.comment("------ done.");
}
/**
@@ -967,7 +942,7 @@
*/
protected void saveAbsolutePosition(String id, int relativeIPP, int relativeBPP) {
saveAbsolutePosition(id, currentPageRef,
- relativeIPP, relativeBPP, currentState.getTransform());
+ relativeIPP, relativeBPP, getState().getTransform());
}
/**
@@ -991,8 +966,8 @@
bpp += currentBPPosition;
}
AffineTransform tf = positioning == Block.FIXED
- ? currentState.getBaseTransform()
- : currentState.getTransform();
+ ? getState().getBaseTransform()
+ : getState().getTransform();
saveAbsolutePosition(id, currentPageRef, ipp, bpp, tf);
}
}
@@ -1055,7 +1030,7 @@
int bpp = currentBPPosition + ip.getOffset();
ipRect = new Rectangle2D.Float(ipp / 1000f, bpp / 1000f,
ip.getIPD() / 1000f, ip.getBPD() / 1000f);
- AffineTransform transform = currentState.getTransform();
+ AffineTransform transform = getState().getTransform();
ipRect = transform.createTransformedShape(ipRect).getBounds2D();
factory = pdfDoc.getFactory();
@@ -1131,6 +1106,7 @@
// This assumes that *all* CIDFonts use a /ToUnicode mapping
Typeface tf = getTypeface(fontName);
+ PDFTextUtil textutil = generator.getTextUtil();
textutil.updateTf(fontName, size / 1000f, tf.isMultiByte());
@@ -1174,7 +1150,7 @@
if (tws != 0) {
float adjust = tws / (font.getFontSize() / 1000f);
- textutil.adjustGlyphTJ(adjust);
+ generator.getTextUtil().adjustGlyphTJ(adjust);
}
}
@@ -1213,6 +1189,7 @@
if (tf instanceof SingleByteFont) {
singleByteFont = (SingleByteFont)tf;
}
+ PDFTextUtil textutil = generator.getTextUtil();
int l = s.length();
@@ -1258,48 +1235,9 @@
}
}
- /**
- * Establishes a new foreground or fill color. In contrast to updateColor
- * this method does not check the PDFState for optimization possibilities.
- * @param col the color to apply
- * @param fill true to set the fill color, false for the foreground color
- * @param pdf StringBuffer to write the PDF code to, if null, the code is
- * written to the current stream.
- */
- protected void setColor(Color col, boolean fill, StringBuffer pdf) {
- if (pdf != null) {
- pdfUtil.setColor(col, fill, pdf);
- } else {
- pdfUtil.setColor(col, fill, this.currentStream);
- }
- }
-
- /**
- * Establishes a new foreground or fill color.
- * @param col the color to apply (null skips this operation)
- * @param fill true to set the fill color, false for the foreground color
- * @param pdf StringBuffer to write the PDF code to, if null, the code is
- * written to the current stream.
- */
- private void updateColor(Color col, boolean fill, StringBuffer pdf) {
- if (col == null) {
- return;
- }
- boolean update = false;
- if (fill) {
- update = currentState.setBackColor(col);
- } else {
- update = currentState.setColor(col);
- }
-
- if (update) {
- setColor(col, fill, pdf);
- }
- }
-
/** {@inheritDoc} */
protected void updateColor(Color col, boolean fill) {
- updateColor(col, fill, null);
+ generator.updateColor(col, fill, null);
}
/** {@inheritDoc} */
@@ -1398,7 +1336,7 @@
// output new data
try {
- this.pdfDoc.output(ostream);
+ this.generator.flushPDFDoc();
} catch (IOException ioe) {
// ioexception will be caught later
}
@@ -1414,7 +1352,7 @@
*/
public void placeImage(float x, float y, float w, float h, PDFXObject xobj) {
saveGraphicsState();
- currentStream.add(format(w) + " 0 0 "
+ generator.add(format(w) + " 0 0 "
+ format(-h) + " "
+ format(currentIPPosition / 1000f + x) + " "
+ format(currentBPPosition / 1000f + h + y)
@@ -1429,12 +1367,12 @@
x, y, width, height, foreignAttributes);
context.setProperty(PDFRendererContextConstants.PDF_DOCUMENT, pdfDoc);
context.setProperty(PDFRendererContextConstants.OUTPUT_STREAM, ostream);
- context.setProperty(PDFRendererContextConstants.PDF_STATE, currentState);
+ context.setProperty(PDFRendererContextConstants.PDF_STATE, getState());
context.setProperty(PDFRendererContextConstants.PDF_PAGE, currentPage);
context.setProperty(PDFRendererContextConstants.PDF_CONTEXT,
currentContext == null ? currentPage : currentContext);
context.setProperty(PDFRendererContextConstants.PDF_CONTEXT, currentContext);
- context.setProperty(PDFRendererContextConstants.PDF_STREAM, currentStream);
+ context.setProperty(PDFRendererContextConstants.PDF_STREAM, generator.getStream());
context.setProperty(PDFRendererContextConstants.PDF_FONT_INFO, fontInfo);
context.setProperty(PDFRendererContextConstants.PDF_FONT_NAME, "");
context.setProperty(PDFRendererContextConstants.PDF_FONT_SIZE, new Integer(0));
@@ -1449,7 +1387,7 @@
public void renderLeader(Leader area) {
renderInlineAreaBackAndBorders(area);
- currentState.push();
+ getState().push();
saveGraphicsState();
int style = area.getRuleStyle();
float startx = (currentIPPosition + area.getBorderAndPaddingWidthStart()) / 1000f;
@@ -1470,7 +1408,7 @@
clipRect(startx, starty, endx - startx, ruleThickness);
//This displaces the dots to the right by half a dot's width
//TODO There's room for improvement here
- currentStream.add("1 0 0 1 " + format(ruleThickness / 2) + " 0 cm\n");
+ generator.add("1 0 0 1 " + format(ruleThickness / 2) + " 0 cm\n");
drawBorderLine(startx, starty, endx, starty + ruleThickness,
true, true, style, col);
break;
@@ -1478,36 +1416,36 @@
case EN_RIDGE:
float half = area.getRuleThickness() / 2000f;
- setColor(lightenColor(col, 0.6f), true, null);
- currentStream.add(format(startx) + " " + format(starty) + " m\n");
- currentStream.add(format(endx) + " " + format(starty) + " l\n");
- currentStream.add(format(endx) + " " + format(starty + 2 * half) + " l\n");
- currentStream.add(format(startx) + " " + format(starty + 2 * half) + " l\n");
- currentStream.add("h\n");
- currentStream.add("f\n");
- setColor(col, true, null);
+ generator.setColor(lightenColor(col, 0.6f), true);
+ generator.add(format(startx) + " " + format(starty) + " m\n");
+ generator.add(format(endx) + " " + format(starty) + " l\n");
+ generator.add(format(endx) + " " + format(starty + 2 * half) + " l\n");
+ generator.add(format(startx) + " " + format(starty + 2 * half) + " l\n");
+ generator.add("h\n");
+ generator.add("f\n");
+ generator.setColor(col, true);
if (style == EN_GROOVE) {
- currentStream.add(format(startx) + " " + format(starty) + " m\n");
- currentStream.add(format(endx) + " " + format(starty) + " l\n");
- currentStream.add(format(endx) + " " + format(starty + half) + " l\n");
- currentStream.add(format(startx + half) + " " + format(starty + half) + " l\n");
- currentStream.add(format(startx) + " " + format(starty + 2 * half) + " l\n");
+ generator.add(format(startx) + " " + format(starty) + " m\n");
+ generator.add(format(endx) + " " + format(starty) + " l\n");
+ generator.add(format(endx) + " " + format(starty + half) + " l\n");
+ generator.add(format(startx + half) + " " + format(starty + half) + " l\n");
+ generator.add(format(startx) + " " + format(starty + 2 * half) + " l\n");
} else {
- currentStream.add(format(endx) + " " + format(starty) + " m\n");
- currentStream.add(format(endx) + " " + format(starty + 2 * half) + " l\n");
- currentStream.add(format(startx) + " " + format(starty + 2 * half) + " l\n");
- currentStream.add(format(startx) + " " + format(starty + half) + " l\n");
- currentStream.add(format(endx - half) + " " + format(starty + half) + " l\n");
+ generator.add(format(endx) + " " + format(starty) + " m\n");
+ generator.add(format(endx) + " " + format(starty + 2 * half) + " l\n");
+ generator.add(format(startx) + " " + format(starty + 2 * half) + " l\n");
+ generator.add(format(startx) + " " + format(starty + half) + " l\n");
+ generator.add(format(endx - half) + " " + format(starty + half) + " l\n");
}
- currentStream.add("h\n");
- currentStream.add("f\n");
+ generator.add("h\n");
+ generator.add("f\n");
break;
default:
throw new UnsupportedOperationException("rule style not supported");
}
restoreGraphicsState();
- currentState.pop();
+ getState().pop();
beginTextObject();
super.renderLeader(area);
}
Added: xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/java/org/apache/fop/render/pdf/PDFRenderingContext.java
URL: http://svn.apache.org/viewvc/xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/java/org/apache/fop/render/pdf/PDFRenderingContext.java?rev=682757&view=auto
==============================================================================
--- xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/java/org/apache/fop/render/pdf/PDFRenderingContext.java (added)
+++ xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/java/org/apache/fop/render/pdf/PDFRenderingContext.java Tue Aug 5 08:31:00 2008
@@ -0,0 +1,82 @@
+/*
+ * 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.render.pdf;
+
+import org.apache.xmlgraphics.util.MimeConstants;
+
+import org.apache.fop.apps.FOUserAgent;
+import org.apache.fop.fonts.FontInfo;
+import org.apache.fop.pdf.PDFPage;
+import org.apache.fop.render.AbstractRenderingContext;
+
+/**
+ * Rendering context for PDF production.
+ */
+public class PDFRenderingContext extends AbstractRenderingContext {
+
+ private PDFContentGenerator generator;
+ private FontInfo fontInfo;
+ private PDFPage page;
+
+ /**
+ * Main constructor.
+ * @param userAgent the user agent
+ * @param generator the PDF content generator
+ * @param page the current PDF page
+ * @param fontInfo the font list
+ */
+ public PDFRenderingContext(FOUserAgent userAgent,
+ PDFContentGenerator generator, PDFPage page, FontInfo fontInfo) {
+ super(userAgent);
+ this.generator = generator;
+ this.page = page;
+ this.fontInfo = fontInfo;
+ }
+
+ /** {@inheritDoc} */
+ public String getMimeType() {
+ return MimeConstants.MIME_PDF;
+ }
+
+ /**
+ * Returns the PDF content generator.
+ * @return the PDF content generator
+ */
+ public PDFContentGenerator getGenerator() {
+ return this.generator;
+ }
+
+ /**
+ * Returns the current PDF page.
+ * @return the PDF page
+ */
+ public PDFPage getPage() {
+ return this.page;
+ }
+
+ /**
+ * Returns the font list.
+ * @return the font list
+ */
+ public FontInfo getFontInfo() {
+ return this.fontInfo;
+ }
+
+}
Propchange: xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/java/org/apache/fop/render/pdf/PDFRenderingContext.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/java/org/apache/fop/render/pdf/PDFRenderingContext.java
------------------------------------------------------------------------------
svn:keywords = Id
Modified: xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/java/org/apache/fop/render/pdf/PDFRenderingUtil.java
URL: http://svn.apache.org/viewvc/xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/java/org/apache/fop/render/pdf/PDFRenderingUtil.java?rev=682757&r1=682756&r2=682757&view=diff
==============================================================================
--- xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/java/org/apache/fop/render/pdf/PDFRenderingUtil.java (original)
+++ xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/java/org/apache/fop/render/pdf/PDFRenderingUtil.java Tue Aug 5 08:31:00 2008
@@ -19,7 +19,6 @@
package org.apache.fop.render.pdf;
-import java.awt.Color;
import java.awt.color.ICC_Profile;
import java.io.IOException;
import java.io.InputStream;
@@ -41,7 +40,6 @@
import org.apache.fop.apps.FOUserAgent;
import org.apache.fop.fo.extensions.xmp.XMPMetadata;
import org.apache.fop.pdf.PDFAMode;
-import org.apache.fop.pdf.PDFColor;
import org.apache.fop.pdf.PDFConformanceException;
import org.apache.fop.pdf.PDFDictionary;
import org.apache.fop.pdf.PDFDocument;
@@ -54,7 +52,6 @@
import org.apache.fop.pdf.PDFNumsArray;
import org.apache.fop.pdf.PDFOutputIntent;
import org.apache.fop.pdf.PDFPageLabels;
-import org.apache.fop.pdf.PDFStream;
import org.apache.fop.pdf.PDFXMode;
import org.apache.fop.util.ColorProfileUtil;
@@ -410,30 +407,4 @@
nums.put(pageIndex, dict);
}
- /**
- * Establishes a new foreground or fill color. In contrast to updateColor
- * this method does not check the PDFState for optimization possibilities.
- * @param col the color to apply
- * @param fill true to set the fill color, false for the foreground color
- * @param pdf StringBuffer to write the PDF code to
- */
- public void setColor(Color col, boolean fill, StringBuffer pdf) {
- assert pdf != null;
- PDFColor color = new PDFColor(this.pdfDoc, col);
- pdf.append(color.getColorSpaceOut(fill));
- }
-
- /**
- * Establishes a new foreground or fill color.
- * @param col the color to apply
- * @param fill true to set the fill color, false for the foreground color
- * @param stream the PDFStream to write the PDF code to
- */
- public void setColor(Color col, boolean fill, PDFStream stream) {
- assert stream != null;
- PDFColor color = new PDFColor(this.pdfDoc, col);
- stream.add(color.getColorSpaceOut(fill));
- }
-
-
}
Modified: xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/java/org/apache/fop/render/pdf/PDFSVGHandler.java
URL: http://svn.apache.org/viewvc/xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/java/org/apache/fop/render/pdf/PDFSVGHandler.java?rev=682757&r1=682756&r2=682757&view=diff
==============================================================================
--- xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/java/org/apache/fop/render/pdf/PDFSVGHandler.java (original)
+++ xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/java/org/apache/fop/render/pdf/PDFSVGHandler.java Tue Aug 5 08:31:00 2008
@@ -44,8 +44,6 @@
import org.apache.fop.pdf.PDFDocument;
import org.apache.fop.pdf.PDFPage;
import org.apache.fop.pdf.PDFResourceContext;
-import org.apache.fop.pdf.PDFState;
-import org.apache.fop.pdf.PDFStream;
import org.apache.fop.render.AbstractGenericSVGHandler;
import org.apache.fop.render.Renderer;
import org.apache.fop.render.RendererContext;
@@ -78,10 +76,10 @@
PDFInfo pdfi = new PDFInfo();
pdfi.pdfDoc = (PDFDocument)context.getProperty(PDF_DOCUMENT);
pdfi.outputStream = (OutputStream)context.getProperty(OUTPUT_STREAM);
- pdfi.pdfState = (PDFState)context.getProperty(PDF_STATE);
+ //pdfi.pdfState = (PDFState)context.getProperty(PDF_STATE);
pdfi.pdfPage = (PDFPage)context.getProperty(PDF_PAGE);
pdfi.pdfContext = (PDFResourceContext)context.getProperty(PDF_CONTEXT);
- pdfi.currentStream = (PDFStream)context.getProperty(PDF_STREAM);
+ //pdfi.currentStream = (PDFStream)context.getProperty(PDF_STREAM);
pdfi.width = ((Integer)context.getProperty(WIDTH)).intValue();
pdfi.height = ((Integer)context.getProperty(HEIGHT)).intValue();
pdfi.fi = (FontInfo)context.getProperty(PDF_FONT_INFO);
@@ -108,13 +106,13 @@
/** see OUTPUT_STREAM */
public OutputStream outputStream;
/** see PDF_STATE */
- public PDFState pdfState;
+ //public PDFState pdfState;
/** see PDF_PAGE */
public PDFPage pdfPage;
/** see PDF_CONTEXT */
public PDFResourceContext pdfContext;
/** see PDF_STREAM */
- public PDFStream currentStream;
+ //public PDFStream currentStream;
/** see PDF_WIDTH */
public int width;
/** see PDF_HEIGHT */
@@ -216,14 +214,15 @@
* Note: To have the svg overlay (under) a text area then use
* an fo:block-container
*/
- pdfInfo.currentStream.add("%SVG setup\n");
- renderer.saveGraphicsState();
- renderer.setColor(Color.black, false, null);
- renderer.setColor(Color.black, true, null);
+ PDFContentGenerator generator = renderer.getGenerator();
+ generator.comment("SVG setup");
+ generator.saveGraphicsState();
+ generator.setColor(Color.black, false);
+ generator.setColor(Color.black, true);
if (!scaling.isIdentity()) {
- pdfInfo.currentStream.add("%viewbox\n");
- pdfInfo.currentStream.add(CTMHelper.toPDFString(scaling, false) + " cm\n");
+ generator.comment("viewbox");
+ generator.add(CTMHelper.toPDFString(scaling, false) + " cm\n");
}
//SVGSVGElement svg = ((SVGDocument)doc).getRootElement();
@@ -238,38 +237,38 @@
graphics.setGraphicContext(new org.apache.xmlgraphics.java2d.GraphicContext());
if (!resolutionScaling.isIdentity()) {
- pdfInfo.currentStream.add("%resolution scaling for " + uaResolution
+ generator.comment("resolution scaling for " + uaResolution
+ " -> " + deviceResolution + "\n");
- pdfInfo.currentStream.add(
+ generator.add(
CTMHelper.toPDFString(resolutionScaling, false) + " cm\n");
graphics.scale(1 / s, 1 / s);
}
- pdfInfo.currentStream.add("%SVG start\n");
+ generator.comment("SVG start");
//Save state and update coordinate system for the SVG image
- pdfInfo.pdfState.push();
- pdfInfo.pdfState.concatenate(imageTransform);
+ generator.getState().push();
+ generator.getState().concatenate(imageTransform);
//Now that we have the complete transformation matrix for the image, we can update the
//transformation matrix for the AElementBridge.
PDFAElementBridge aBridge = (PDFAElementBridge)ctx.getBridge(
SVGDOMImplementation.SVG_NAMESPACE_URI, SVGConstants.SVG_A_TAG);
- aBridge.getCurrentTransform().setTransform(pdfInfo.pdfState.getTransform());
+ aBridge.getCurrentTransform().setTransform(generator.getState().getTransform());
- graphics.setPDFState(pdfInfo.pdfState);
+ graphics.setPDFState(generator.getState());
graphics.setOutputStream(pdfInfo.outputStream);
try {
root.paint(graphics);
- pdfInfo.currentStream.add(graphics.getString());
+ generator.add(graphics.getString());
} catch (Exception e) {
SVGEventProducer eventProducer = SVGEventProducer.Provider.get(
context.getUserAgent().getEventBroadcaster());
eventProducer.svgRenderingError(this, e, getDocumentURI(doc));
}
- pdfInfo.pdfState.pop();
- renderer.restoreGraphicsState();
- pdfInfo.currentStream.add("%SVG end\n");
+ generator.getState().pop();
+ generator.restoreGraphicsState();
+ generator.comment("SVG end");
}
/** {@inheritDoc} */
Modified: xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/java/org/apache/fop/svg/PDFGraphics2D.java
URL: http://svn.apache.org/viewvc/xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/java/org/apache/fop/svg/PDFGraphics2D.java?rev=682757&r1=682756&r2=682757&view=diff
==============================================================================
--- xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/java/org/apache/fop/svg/PDFGraphics2D.java (original)
+++ xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/java/org/apache/fop/svg/PDFGraphics2D.java Tue Aug 5 08:31:00 2008
@@ -440,6 +440,14 @@
}
PDFXObject xObject = this.pdfDoc.addImage(resourceContext, pdfImage);
+ flushPDFDocument();
+
+ AffineTransform at = new AffineTransform();
+ at.translate(x, y);
+ useXObject(xObject, at, width, height);
+ }
+
+ private void flushPDFDocument() {
if (outputStream != null) {
try {
this.pdfDoc.output(outputStream);
@@ -447,10 +455,6 @@
// ignore exception, will be thrown again later
}
}
-
- AffineTransform at = new AffineTransform();
- at.translate(x, y);
- useXObject(xObject, at, width, height);
}
/**
@@ -1044,13 +1048,7 @@
this.pdfDoc.addObject(annots);
}
- if (outputStream != null) {
- try {
- this.pdfDoc.output(outputStream);
- } catch (IOException ioe) {
- // ignore exception, will be thrown again later
- }
- }
+ flushPDFDocument();
return true;
}
@@ -1147,26 +1145,14 @@
PDFImageXObject xobj = pdfDoc.addImage(resourceContext, fopimg);
maskRef = xobj.referencePDF();
- if (outputStream != null) {
- try {
- this.pdfDoc.output(outputStream);
- } catch (IOException ioe) {
- // ignore exception, will be thrown again later
- }
- }
+ flushPDFDocument();
}
BitmapImage fopimg;
fopimg = new BitmapImage("TempImage:" + pctx.toString(),
devW, devH, rgb, maskRef);
fopimg.setTransparent(new PDFColor(255, 255, 255));
imageInfo = pdfDoc.addImage(resourceContext, fopimg);
- if (outputStream != null) {
- try {
- this.pdfDoc.output(outputStream);
- } catch (IOException ioe) {
- // ignore exception, will be thrown again later
- }
- }
+ flushPDFDocument();
}
currentStream.write("q\n");
@@ -1275,13 +1261,7 @@
ImageRendered imgRend = new ImageRendered(info, img, null);
ImageRenderedAdapter adapter = new ImageRenderedAdapter(imgRend, key);
PDFXObject xObject = pdfDoc.addImage(resourceContext, adapter);
- if (outputStream != null) {
- try {
- this.pdfDoc.output(outputStream);
- } catch (IOException ioe) {
- // ignore exception, will be thrown again later
- }
- }
+ flushPDFDocument();
return xObject;
}
Added: xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/sandbox/META-INF/services/org.apache.fop.render.ImageHandler
URL: http://svn.apache.org/viewvc/xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/sandbox/META-INF/services/org.apache.fop.render.ImageHandler?rev=682757&view=auto
==============================================================================
--- xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/sandbox/META-INF/services/org.apache.fop.render.ImageHandler (added)
+++ xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/sandbox/META-INF/services/org.apache.fop.render.ImageHandler Tue Aug 5 08:31:00 2008
@@ -0,0 +1,2 @@
+org.apache.fop.render.svg.SVGDataUrlImageHandler
+org.apache.fop.render.svg.EmbeddedSVGImageHandler
Modified: xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/sandbox/org/apache/fop/render/svg/AbstractSVGPainter.java
URL: http://svn.apache.org/viewvc/xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/sandbox/org/apache/fop/render/svg/AbstractSVGPainter.java?rev=682757&r1=682756&r2=682757&view=diff
==============================================================================
--- xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/sandbox/org/apache/fop/render/svg/AbstractSVGPainter.java (original)
+++ xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/sandbox/org/apache/fop/render/svg/AbstractSVGPainter.java Tue Aug 5 08:31:00 2008
@@ -24,13 +24,30 @@
import java.awt.Paint;
import java.awt.Rectangle;
import java.awt.geom.AffineTransform;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.util.Map;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.AttributesImpl;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+import org.apache.xmlgraphics.image.loader.ImageException;
+import org.apache.xmlgraphics.image.loader.ImageInfo;
+import org.apache.xmlgraphics.image.loader.ImageManager;
+import org.apache.xmlgraphics.image.loader.ImageSessionContext;
+import org.apache.xmlgraphics.image.loader.util.ImageUtil;
+import org.apache.xmlgraphics.util.MimeConstants;
+import org.apache.xmlgraphics.util.QName;
import org.apache.xmlgraphics.xmp.Metadata;
+import org.apache.fop.events.ResourceEventProducer;
+import org.apache.fop.fo.extensions.ExtensionElementMapping;
+import org.apache.fop.render.ImageHandler;
import org.apache.fop.render.intermediate.AbstractXMLWritingIFPainter;
+import org.apache.fop.render.intermediate.IFConstants;
import org.apache.fop.render.intermediate.IFException;
import org.apache.fop.render.intermediate.IFState;
import org.apache.fop.util.ColorUtil;
@@ -41,6 +58,9 @@
public abstract class AbstractSVGPainter extends AbstractXMLWritingIFPainter
implements SVGConstants {
+ /** logging instance */
+ private static Log log = LogFactory.getLog(AbstractSVGPainter.class);
+
/** Holds the intermediate format state */
protected IFState state;
@@ -184,11 +204,81 @@
}
+ private QName CONVERSION_MODE = new QName(ExtensionElementMapping.URI, null, "conversion-mode");
+
/** {@inheritDoc} */
- public void drawImage(String uri, Rectangle rect) throws IFException {
- //establish(MODE_NORMAL);
- // TODO Auto-generated method stub
+ public void drawImage(String uri, Rectangle rect, Map foreignAttributes) throws IFException {
+ try {
+ establish(MODE_NORMAL);
+ ImageManager manager = getUserAgent().getFactory().getImageManager();
+ ImageInfo info = null;
+ try {
+ ImageSessionContext sessionContext = getUserAgent().getImageSessionContext();
+ info = manager.getImageInfo(uri, sessionContext);
+ String mime = info.getMimeType();
+ String conversionMode = (String)foreignAttributes.get(CONVERSION_MODE);
+ if ("reference".equals(conversionMode)
+ && (MimeConstants.MIME_GIF.equals(mime)
+ || MimeConstants.MIME_JPEG.equals(mime)
+ || MimeConstants.MIME_PNG.equals(mime)
+ || MimeConstants.MIME_SVG.equals(mime))) {
+ //Just reference the image
+ //TODO Some additional URI rewriting might be necessary
+ AttributesImpl atts = new AttributesImpl();
+ addAttribute(atts, IFConstants.XLINK_HREF, uri);
+ atts.addAttribute("", "x", "x", CDATA, Integer.toString(rect.x));
+ atts.addAttribute("", "y", "y", CDATA, Integer.toString(rect.y));
+ atts.addAttribute("", "width", "width", CDATA, Integer.toString(rect.width));
+ atts.addAttribute("", "height", "height", CDATA, Integer.toString(rect.height));
+ element("image", atts);
+ } else {
+ //Convert the image
+ SVGRenderingContext svgContext = new SVGRenderingContext(
+ getUserAgent(), handler);
+
+ Map hints = ImageUtil.getDefaultHints(sessionContext);
+ org.apache.xmlgraphics.image.loader.Image img = manager.getImage(
+ info, imageHandlerRegistry.getSupportedFlavors(svgContext),
+ hints, sessionContext);
+
+ //First check for a dynamically registered handler
+ ImageHandler handler = imageHandlerRegistry.getHandler(svgContext, img);
+ if (handler == null) {
+ throw new UnsupportedOperationException(
+ "No ImageHandler available for image: "
+ + info + " (" + img.getClass().getName() + ")");
+ }
+
+ if (log.isDebugEnabled()) {
+ log.debug("Using ImageHandler: " + handler.getClass().getName());
+ }
+ try {
+ //TODO foreign attributes
+ handler.handleImage(svgContext, img, rect);
+ } catch (IOException ioe) {
+ ResourceEventProducer eventProducer = ResourceEventProducer.Provider.get(
+ getUserAgent().getEventBroadcaster());
+ eventProducer.imageWritingError(this, ioe);
+ return;
+ }
+ }
+ } catch (ImageException ie) {
+ ResourceEventProducer eventProducer = ResourceEventProducer.Provider.get(
+ getUserAgent().getEventBroadcaster());
+ eventProducer.imageError(this, (info != null ? info.toString() : uri), ie, null);
+ } catch (FileNotFoundException fe) {
+ ResourceEventProducer eventProducer = ResourceEventProducer.Provider.get(
+ getUserAgent().getEventBroadcaster());
+ eventProducer.imageNotFound(this, (info != null ? info.toString() : uri), fe, null);
+ } catch (IOException ioe) {
+ ResourceEventProducer eventProducer = ResourceEventProducer.Provider.get(
+ getUserAgent().getEventBroadcaster());
+ eventProducer.imageIOError(this, (info != null ? info.toString() : uri), ioe, null);
+ }
+ } catch (SAXException e) {
+ throw new IFException("SAX error in drawImage()", e);
+ }
}
/** {@inheritDoc} */
Added: xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/sandbox/org/apache/fop/render/svg/EmbeddedSVGImageHandler.java
URL: http://svn.apache.org/viewvc/xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/sandbox/org/apache/fop/render/svg/EmbeddedSVGImageHandler.java?rev=682757&view=auto
==============================================================================
--- xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/sandbox/org/apache/fop/render/svg/EmbeddedSVGImageHandler.java (added)
+++ xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/sandbox/org/apache/fop/render/svg/EmbeddedSVGImageHandler.java Tue Aug 5 08:31:00 2008
@@ -0,0 +1,164 @@
+/*
+ * 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.render.svg;
+
+import java.awt.Rectangle;
+import java.io.IOException;
+
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.sax.SAXResult;
+
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+
+import org.xml.sax.Attributes;
+import org.xml.sax.ContentHandler;
+import org.xml.sax.SAXException;
+import org.xml.sax.helpers.AttributesImpl;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+import org.apache.xmlgraphics.image.loader.Image;
+import org.apache.xmlgraphics.image.loader.ImageFlavor;
+import org.apache.xmlgraphics.image.loader.impl.ImageRawStream;
+import org.apache.xmlgraphics.image.loader.impl.ImageXMLDOM;
+import org.apache.xmlgraphics.util.QName;
+
+import org.apache.fop.image.loader.batik.BatikImageFlavors;
+import org.apache.fop.render.ImageHandler;
+import org.apache.fop.render.RenderingContext;
+import org.apache.fop.render.intermediate.DelegatingFragmentContentHandler;
+
+/**
+ * Image handler implementation that embeds SVG images in the target SVG file.
+ */
+public class EmbeddedSVGImageHandler implements ImageHandler, SVGConstants {
+
+ /** logging instance */
+ private static Log log = LogFactory.getLog(EmbeddedSVGImageHandler.class);
+
+ /** Constant for the "CDATA" attribute type. */
+ private static final String CDATA = "CDATA";
+
+ /** {@inheritDoc} */
+ public int getPriority() {
+ return 500;
+ }
+
+ /** {@inheritDoc} */
+ public Class getSupportedImageClass() {
+ return ImageRawStream.class;
+ }
+
+ /** {@inheritDoc} */
+ public ImageFlavor[] getSupportedImageFlavors() {
+ return new ImageFlavor[] {
+ BatikImageFlavors.SVG_DOM
+ };
+ }
+
+ private void addAttribute(AttributesImpl atts, QName attribute, String value) {
+ atts.addAttribute(attribute.getNamespaceURI(),
+ attribute.getLocalName(), attribute.getQName(), CDATA, value);
+ }
+
+ /** {@inheritDoc} */
+ public void handleImage(RenderingContext context, Image image, final Rectangle pos)
+ throws IOException {
+ SVGRenderingContext svgContext = (SVGRenderingContext)context;
+ ImageXMLDOM svg = (ImageXMLDOM)image;
+ ContentHandler handler = svgContext.getContentHandler();
+ AttributesImpl atts = new AttributesImpl();
+ atts.addAttribute("", "x", "x", CDATA, Integer.toString(pos.x));
+ atts.addAttribute("", "y", "y", CDATA, Integer.toString(pos.y));
+ atts.addAttribute("", "width", "width", CDATA, Integer.toString(pos.width));
+ atts.addAttribute("", "height", "height", CDATA, Integer.toString(pos.height));
+ try {
+ //handler.startElement(NAMESPACE, "svg", "svg", atts);
+
+ Document doc = (Document)svg.getDocument();
+ Element svgEl = (Element)doc.getDocumentElement();
+ if (svgEl.getAttribute("viewBox").length() == 0) {
+ log.warn("SVG doesn't have a viewBox. The result might not be scaled correctly!");
+ }
+
+ TransformerFactory tFactory = TransformerFactory.newInstance();
+ Transformer transformer = tFactory.newTransformer();
+ DOMSource src = new DOMSource(svg.getDocument());
+ SAXResult res = new SAXResult(new DelegatingFragmentContentHandler(handler) {
+
+ private boolean topLevelSVGFound = false;
+
+ private void setAttribute(AttributesImpl atts, String localName, String value) {
+ int index;
+ index = atts.getIndex("", localName);
+ if (index < 0) {
+ atts.addAttribute("", localName, localName, CDATA, value);
+ } else {
+ atts.setAttribute(index, "", localName, localName, CDATA, value);
+ }
+ }
+
+ public void startElement(String uri, String localName, String name, Attributes atts)
+ throws SAXException {
+ if (!topLevelSVGFound
+ && SVG_ELEMENT.getNamespaceURI().equals(uri)
+ && SVG_ELEMENT.getLocalName().equals(localName)) {
+ topLevelSVGFound = true;
+ AttributesImpl modAtts = new AttributesImpl(atts);
+ setAttribute(modAtts, "x", Integer.toString(pos.x));
+ setAttribute(modAtts, "y", Integer.toString(pos.y));
+ setAttribute(modAtts, "width", Integer.toString(pos.width));
+ setAttribute(modAtts, "height", Integer.toString(pos.height));
+ super.startElement(uri, localName, name, modAtts);
+ } else {
+ super.startElement(uri, localName, name, atts);
+ }
+ }
+
+ });
+ transformer.transform(src, res);
+ //handler.endElement(NAMESPACE, "svg", "svg");
+ //} catch (SAXException e) {
+ //throw new IOException(e.getMessage());
+ } catch (TransformerException te) {
+ throw new IOException(te.getMessage());
+ }
+ }
+
+ /** {@inheritDoc} */
+ public boolean isCompatible(RenderingContext targetContext, Image image) {
+ if (targetContext instanceof SVGRenderingContext) {
+ if (image == null) {
+ return true;
+ }
+ if (image instanceof ImageXMLDOM) {
+ ImageXMLDOM svg = (ImageXMLDOM)image;
+ return NAMESPACE.equals(svg.getRootNamespace());
+ }
+ }
+ return false;
+ }
+
+}
Propchange: xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/sandbox/org/apache/fop/render/svg/EmbeddedSVGImageHandler.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/sandbox/org/apache/fop/render/svg/EmbeddedSVGImageHandler.java
------------------------------------------------------------------------------
svn:keywords = Id
Modified: xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/sandbox/org/apache/fop/render/svg/SVGConstants.java
URL: http://svn.apache.org/viewvc/xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/sandbox/org/apache/fop/render/svg/SVGConstants.java?rev=682757&r1=682756&r2=682757&view=diff
==============================================================================
--- xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/sandbox/org/apache/fop/render/svg/SVGConstants.java (original)
+++ xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/sandbox/org/apache/fop/render/svg/SVGConstants.java Tue Aug 5 08:31:00 2008
@@ -19,6 +19,8 @@
package org.apache.fop.render.svg;
+import org.apache.xmlgraphics.util.QName;
+
import org.apache.fop.apps.MimeConstants;
/**
@@ -46,4 +48,7 @@
/** XML namespace for XLink */
String XLINK_NAMESPACE = "http://www.w3.org/1999/xlink";
+ /** the SVG element */
+ QName SVG_ELEMENT = new QName(NAMESPACE, null, "svg");
+
}
Added: xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/sandbox/org/apache/fop/render/svg/SVGDataUrlImageHandler.java
URL: http://svn.apache.org/viewvc/xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/sandbox/org/apache/fop/render/svg/SVGDataUrlImageHandler.java?rev=682757&view=auto
==============================================================================
--- xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/sandbox/org/apache/fop/render/svg/SVGDataUrlImageHandler.java (added)
+++ xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/sandbox/org/apache/fop/render/svg/SVGDataUrlImageHandler.java Tue Aug 5 08:31:00 2008
@@ -0,0 +1,106 @@
+/*
+ * 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.render.svg;
+
+import java.awt.Rectangle;
+import java.io.IOException;
+import java.io.InputStream;
+
+import org.xml.sax.ContentHandler;
+import org.xml.sax.SAXException;
+import org.xml.sax.helpers.AttributesImpl;
+
+import org.apache.commons.io.IOUtils;
+
+import org.apache.xmlgraphics.image.loader.Image;
+import org.apache.xmlgraphics.image.loader.ImageFlavor;
+import org.apache.xmlgraphics.image.loader.impl.ImageRawStream;
+import org.apache.xmlgraphics.util.QName;
+
+import org.apache.fop.render.ImageHandler;
+import org.apache.fop.render.RenderingContext;
+import org.apache.fop.render.intermediate.IFConstants;
+import org.apache.fop.util.DataURLUtil;
+
+/**
+ * Image handler implementation that embeds JPEG bitmaps as RFC 2397 data URLs in the target SVG
+ * file.
+ */
+public class SVGDataUrlImageHandler implements ImageHandler, SVGConstants {
+
+ /** Constant for the "CDATA" attribute type. */
+ private static final String CDATA = "CDATA";
+
+ /** {@inheritDoc} */
+ public int getPriority() {
+ return 500;
+ }
+
+ /** {@inheritDoc} */
+ public Class getSupportedImageClass() {
+ return ImageRawStream.class;
+ }
+
+ /** {@inheritDoc} */
+ public ImageFlavor[] getSupportedImageFlavors() {
+ return new ImageFlavor[] {
+ ImageFlavor.RAW_PNG,
+ ImageFlavor.RAW_JPEG,
+ };
+ }
+
+ private void addAttribute(AttributesImpl atts, QName attribute, String value) {
+ atts.addAttribute(attribute.getNamespaceURI(),
+ attribute.getLocalName(), attribute.getQName(), CDATA, value);
+ }
+
+ /** {@inheritDoc} */
+ public void handleImage(RenderingContext context, Image image, Rectangle pos)
+ throws IOException {
+ SVGRenderingContext svgContext = (SVGRenderingContext)context;
+ ImageRawStream raw = (ImageRawStream)image;
+ InputStream in = raw.createInputStream();
+ try {
+ ContentHandler handler = svgContext.getContentHandler();
+ String url = DataURLUtil.createDataURL(in, raw.getMimeType());
+ AttributesImpl atts = new AttributesImpl();
+ addAttribute(atts, IFConstants.XLINK_HREF, url);
+ atts.addAttribute("", "x", "x", CDATA, Integer.toString(pos.x));
+ atts.addAttribute("", "y", "y", CDATA, Integer.toString(pos.y));
+ atts.addAttribute("", "width", "width", CDATA, Integer.toString(pos.width));
+ atts.addAttribute("", "height", "height", CDATA, Integer.toString(pos.height));
+ try {
+ handler.startElement(NAMESPACE, "image", "image", atts);
+ handler.endElement(NAMESPACE, "image", "image");
+ } catch (SAXException e) {
+ throw new IOException(e.getMessage());
+ }
+ } finally {
+ IOUtils.closeQuietly(in);
+ }
+ }
+
+ /** {@inheritDoc} */
+ public boolean isCompatible(RenderingContext targetContext, Image image) {
+ return (image == null || image instanceof ImageRawStream)
+ && targetContext instanceof SVGRenderingContext;
+ }
+
+}
Propchange: xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/sandbox/org/apache/fop/render/svg/SVGDataUrlImageHandler.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/sandbox/org/apache/fop/render/svg/SVGDataUrlImageHandler.java
------------------------------------------------------------------------------
svn:keywords = Id
Added: xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/sandbox/org/apache/fop/render/svg/SVGRenderingContext.java
URL: http://svn.apache.org/viewvc/xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/sandbox/org/apache/fop/render/svg/SVGRenderingContext.java?rev=682757&view=auto
==============================================================================
--- xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/sandbox/org/apache/fop/render/svg/SVGRenderingContext.java (added)
+++ xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/sandbox/org/apache/fop/render/svg/SVGRenderingContext.java Tue Aug 5 08:31:00 2008
@@ -0,0 +1,59 @@
+/*
+ * 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.render.svg;
+
+import org.xml.sax.ContentHandler;
+
+import org.apache.xmlgraphics.util.MimeConstants;
+
+import org.apache.fop.apps.FOUserAgent;
+import org.apache.fop.render.AbstractRenderingContext;
+
+/**
+ * Rendering context for SVG production.
+ */
+public class SVGRenderingContext extends AbstractRenderingContext {
+
+ private ContentHandler handler;
+
+ /**
+ * Main constructor.
+ * @param userAgent the user agent
+ * @param handler the target content handler
+ */
+ public SVGRenderingContext(FOUserAgent userAgent, ContentHandler handler) {
+ super(userAgent);
+ this.handler = handler;
+ }
+
+ /** {@inheritDoc} */
+ public String getMimeType() {
+ return MimeConstants.MIME_SVG;
+ }
+
+ /**
+ * Returns the target content handler.
+ * @return the content handler
+ */
+ public ContentHandler getContentHandler() {
+ return this.handler;
+ }
+
+}
Propchange: xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/sandbox/org/apache/fop/render/svg/SVGRenderingContext.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/sandbox/org/apache/fop/render/svg/SVGRenderingContext.java
------------------------------------------------------------------------------
svn:keywords = Id
---------------------------------------------------------------------
To unsubscribe, e-mail: fop-commits-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: fop-commits-help@xmlgraphics.apache.org