You are viewing a plain text version of this content. The canonical link for it is here.
Posted to batik-users@xmlgraphics.apache.org by John Hoar <jo...@yahoo.co.uk> on 2003/05/04 19:55:35 UTC
Generating images using flowtext tags through DOM. I'm confused
Help.
I'm trying to generate images through SVG using Batik 1.5b5 on WindowsXP.
I want to use the Batik flowtext extensions (which seems like a reasonable
thing to do, given that they appear to be in the SVG1.2 spec, which means
what I'm doing should be reasonably well supported in the future).
No I'm using the DOM API for this and and strange thing appears to be
happening. If I directly the Document object to the JPEGTranscoder, the
image is rendered without the text. However, if I first dump the document
to an SVG XML file through the SVGTranscoder and then generate the image
from the SVG file, the text appears.
Q. So I must be missing a step in my hand construction of the DOM tree
which Batik does when it constructs it from the SVG XML file. But I have
no idea what it is. Can you help
I also want to use CSS to style the documents. If I use
createProcessingInstruction, the CCSEngine class complains:
org.w3c.dom.DOMException: Null Document reference:
Invalid CSS document.
However, if I use squiggle, there's no problem.
Q. Again, what's the problem here? The error is coming from
CSSEngine.parseStyleSheet(StyleSheet ss, URL uri) not be passed an URI.
Why can squiggle do it, when I can't.
Below are the pair of classes I'm using. SlideMaker.save() works,
SlideMaker.save2() doesn't. And I'm commented out the insertion of the xml
processing instruction in Slide.createDocument(). You can use any old CSS
file named test.css. The contents do not appear to matter.
If you can help and clear my misunderstanding, I'm be very grateful.
Thanks,
John
--
package slide;
import org.apache.batik.dom.svg.ExtensibleSVGDOMImplementation;
import org.apache.batik.dom.svg.SVGDOMImplementation;
import org.apache.batik.extension.svg.BatikDomExtension;
import org.apache.batik.extension.svg.BatikExtConstants;
import org.apache.batik.util.SVGConstants;
import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.ProcessingInstruction;
import org.w3c.dom.Text;
/**
*/
public class Slide {
protected static final String svgNS =
SVGDOMImplementation.SVG_NAMESPACE_URI;
protected static final String batikNS =
BatikDomExtension.BATIK_EXT_NAMESPACE_URI;
public static Document createDocument(int width, int height, String
cssSheet) {
DOMImplementation impl =
ExtensibleSVGDOMImplementation.getDOMImplementation();
Document document = impl.createDocument(svgNS, SVGConstants.SVG_SVG_TAG,
null);
Element root = document.getDocumentElement();
root.setAttributeNS(null, SVGConstants.SVG_WIDTH_ATTRIBUTE,
String.valueOf(width));
root.setAttributeNS(null, SVGConstants.SVG_HEIGHT_ATTRIBUTE,
String.valueOf(height));
ProcessingInstruction p =
document.createProcessingInstruction("xml-stylesheet",
" href=\"" + cssSheet "\"type=\"text/css\" ");
// Uncomment this to provoke second problem
// document.insertBefore(p,root);
return document;
}
public static Element addTextBox(Document document, String text,
int x, int y, int width, int height,
String cssClass) {
/* Set up the top level flowText element */
Element flowTextElement =
document.createElementNS(batikNS,
BatikExtConstants.BATIK_EXT_FLOW_TEXT_TAG);
flowTextElement.setAttributeNS(null,"xmlns",batikNS);
flowTextElement.setAttributeNS(null,"class",cssClass);
/* Set up the flowRegion sub-element */
Element flowRegionElement =
document.createElementNS(batikNS,
BatikExtConstants.BATIK_EXT_FLOW_REGION_TAG);
/* Set up the Rect sub-element */
Element rectElement = document.createElementNS(svgNS,
SVGConstants.SVG_RECT_TAG);
rectElement.setAttributeNS(null, SVGConstants.SVG_X_ATTRIBUTE,
String.valueOf(x));
rectElement.setAttributeNS(null, SVGConstants.SVG_Y_ATTRIBUTE,
String.valueOf(y));
rectElement.setAttributeNS(null, SVGConstants.SVG_WIDTH_ATTRIBUTE,
String.valueOf(height));
rectElement.setAttributeNS(null, SVGConstants.SVG_HEIGHT_ATTRIBUTE,
String.valueOf(width));
/* Set up the flowDiv sub-element */
Element flowDivElement =
document.createElementNS(batikNS,
BatikExtConstants.BATIK_EXT_FLOW_DIV_TAG);
/* Set up the flowPara sub-element */
Element flowParaElement =
document.createElementNS(batikNS,
BatikExtConstants.BATIK_EXT_FLOW_PARA_TAG);
/* Set up the text sub-element */
Text flowText = document.createTextNode(text);
/* Now build tree */
flowTextElement.appendChild(flowRegionElement);
flowTextElement.appendChild(flowDivElement);
flowRegionElement.appendChild(rectElement);
flowDivElement.appendChild(flowParaElement);
flowParaElement.appendChild(flowText);
return flowTextElement;
}
}
--
package slide;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.batik.transcoder.TranscoderInput;
import org.apache.batik.transcoder.TranscoderOutput;
import org.apache.batik.transcoder.image.JPEGTranscoder;
import org.apache.batik.transcoder.svg2svg.SVGTranscoder;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
/**
*/
public class SlideMaker {
public static void main(String[] args) throws Exception {
// Set the test string
String testString = "This is a test.";
// Make me an instance of myself
SlideMaker maker = new SlideMaker();
// Create the document
Document mySlide = Slide.createDocument(1024,768,"test.css");
// Fetch the root element
Element root = mySlide.getDocumentElement();
// Add a text box
root.appendChild(Slide.addTextBox(mySlide,testString,100,100,300,300,"test"));
// Go and save it
maker.save(mySlide);
// Go and save it (does not work)
maker.save2(mySlide);
return;
}
private void save(Document slide) throws Exception {
SVGTranscoder svgTranscoder = new SVGTranscoder();
JPEGTranscoder jpegTranscoder = new JPEGTranscoder();
jpegTranscoder.addTranscodingHint(JPEGTranscoder.KEY_QUALITY,
new Float(1.0));
// Create the file output stream;
FileWriter svgOutputStream = new FileWriter("test1.svg");
// Set up the SVG transcoder
TranscoderInput svgInput = new TranscoderInput(slide);
TranscoderOutput svgOutput = new TranscoderOutput(svgOutputStream);
svgTranscoder.transcode(svgInput,svgOutput);
svgOutputStream.flush();
svgOutputStream.close();
// Create the file input stream;
InputStream jpgInputStream = new FileInputStream("test1.svg");
// Create the file output stream;
OutputStream jpgOutputStream = new FileOutputStream("test1.jpg");
// Set up the JPG transcoder
TranscoderInput jpgInput = new TranscoderInput(jpgInputStream);
TranscoderOutput jpgOutput = new TranscoderOutput(jpgOutputStream);
jpegTranscoder.transcode(jpgInput,jpgOutput);
jpgOutputStream.flush();
jpgOutputStream.close();
}
private void save2(Document slide) throws Exception {
JPEGTranscoder jpegTranscoder = new JPEGTranscoder();
jpegTranscoder.addTranscodingHint(JPEGTranscoder.KEY_QUALITY,
new Float(1.0));
OutputStream jpgOutputStream = new FileOutputStream("test2.jpg");
// Set up the SVG transcoder
TranscoderInput jpgInput = new TranscoderInput(slide);
TranscoderOutput jpgOutput = new TranscoderOutput(jpgOutputStream);
jpegTranscoder.transcode(jpgInput,jpgOutput);
jpgOutputStream.flush();
jpgOutputStream.close();
}
}
__________________________________________________
Yahoo! Plus
For a better Internet experience
http://www.yahoo.co.uk/btoffer
---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xml.apache.org
For additional commands, e-mail: batik-users-help@xml.apache.org
RE: Generating images using flowtext tags through DOM. I'm confused
Posted by Thomas E Deweese <th...@kodak.com>.
>>>>> "JH" == John Hoar <jo...@yahoo.co.uk> writes:
JH> I'm trying to generate images through SVG using Batik 1.5b5 on
JH> WindowsXP.
[...]
JH> No I'm using the DOM API for this and and strange thing appears to
JH> be happening. If I directly the Document object to the
JH> JPEGTranscoder, the image is rendered without the text. However,
JH> if I first dump the document to an SVG XML file through the
JH> SVGTranscoder and then generate the image from the SVG file, the
JH> text appears.
The problem is that the 'rect' elements in the flowText must be in
the Batik Namespace as well. By setting xmlns on the flowText element
_all_ the children elements are in that namespace.
JH> I also want to use CSS to style the documents. If I use
JH> createProcessingInstruction, the CCSEngine class complains:
Hmm, I suspect that the 'dynamic' code is getting invoked here
which isn't good since we really haven't setup the dynamic environment
(hence the NPE).
JH> org.w3c.dom.DOMException: Null Document reference: Invalid CSS
JH> document.
Can you provide the whole stack trace? It might be something
simple.
JH> However, if I use squiggle, there's no problem.
JH> Q. Again, what's the problem here? The error is coming from
JH> CSSEngine.parseStyleSheet(StyleSheet ss, URL uri) not be passed an
JH> URI. Why can squiggle do it, when I can't.
JH> Below are the pair of classes I'm using. SlideMaker.save() works,
JH> SlideMaker.save2() doesn't. And I'm commented out the insertion of
JH> the xml processing instruction in Slide.createDocument(). You can
JH> use any old CSS file named test.css. The contents do not appear to
JH> matter.
JH> If you can help and clear my misunderstanding, I'm be very
JH> grateful.
Well so far I'm batting 500 :)
JH> Thanks,
JH> John
---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xml.apache.org
For additional commands, e-mail: batik-users-help@xml.apache.org