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 Glenn Thomas Hvidsten <gt...@corena.no> on 2005/03/02 12:02:26 UTC

Batik and FreeHep

Hi,

I hope you can help me out with some problems I've run into using Batik.
I've been Googling for this the last few days without coming up with a
solution. I've also tried asking on some newsgroups but to no success.

I've recently found the batik and the VectorGraphics package from the
freehep libraries
<url:http://java.freehep.org/vectorgraphics/index.html> which mostly do
exactly what I need; to load an SVG document and convert it to another
vector graphics format (.cgm or .pdf).
My problem is that freehep only use a standard Graphics2D-enabled
component (i.e. JComponent) to be able to export. Batik, however, only
outputs images to it's custom JSVGCanvas, which is not directly
compatible with freehep.
Using only a JComponent which I draw to myself using the paint() method
I've gotten freehep export to work by using this syntax:

VectorGraphics vg = new PDFGraphics2D(
      new File("temp.pdf"),
      new Dimension(x,y));
vg.startExport();
super.print(vg);
vg.endExport();

where 'super' points to the class extending JComponent.

I've also managed to draw an image based on an SVG file using the
example on this page:
<url:http://xml.apache.org/batik/svgcanvas.html>

Using that example I added another button and added this in the
actionPerformed method for that button:

VectorGraphics vg = PDFGraphics2D(
      new File("test.pdf"),
      svgCanvas.getSize());
vg.startExport();
svgCanvas.print(vg);
vg.endExport();

This syntax gives a couple of NullPointerExceptions.

I also tried to use the graphicsnode of the SVGCanvas by using this syntax:

svgCanvas.getGraphicsNode().paint(vg);

but this gives these two error messages:

PDFWriter: PDFRef 'Paint0' is used but not defined.
PDFWriter: PDFRef 'Paint1' is used but not defined.

Do you have any experiences with these libraries, or other similar
libraries that will do the same thing? I really need this converter to
work as soon as possible. I'm kinda on a deadline here  :)

---

Glenn Thomas Hvidsten

=============  CORENA Norge AS  =============
== Dyrmyrgt. 35, N-3611 Kongsberg, NORWAY  ==
== Tlf: +47 3271 7234, Fax: +47 3271 7201  ==
== CORENA Home Page: http://www.corena.com ==


---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xml.apache.org
For additional commands, e-mail: batik-users-help@xml.apache.org


Re: Batik and FreeHep

Posted by Glenn Thomas Hvidsten <gt...@corena.no>.
Andres Toussaint wrote:
> I don't think you need to override the paint() method in your class,
> 
>> public void gvtBuildCompleted(GVTTreeBuilderEvent e) {
>>     gvtTreeWalker = new GVTTreeWalker(svgCanvas.getGraphicsNode());
>>     gvtTreeWalker.getRoot().paint(this.g);
>> }
>>
> 
> instead, change the last line to:
> 
> gvtTreeWalker.getRoot().paint(this.getGraphics());
> 
> to get access to this components Graphics object.

Thanks, that worked! :D
I now have this code which draws on a Graphics2D I can export with FreeHEP:


public void gvtBuildCompleted(GVTTreeBuilderEvent e) {
     gvtTreeWalker = new GVTTreeWalker(svgCanvas.getGraphicsNode());
     svgCanvas.getSVGDocumentSize());
     super.repaint();
}

public void paint(Graphics g) {
     this.g = (Graphics2D) g;
     if (gvtTreeWalker != null) {
         gvtTreeWalker.getRoot().paint((Graphics2D) this.g);
     }
}


The reason I have to override the paint() method is to get hold of the 
Graphics variable passed to the paint() method. I use that in calls to 
FreeHEP to be able to export... unless of course you have a more elegant 
solution :)


There now seems to be only one problem remaining. Batik uses its own 
custom gradients, but FreeHEP only supports the standard GradientPaint 
and TexturePaint. Is there any way batik can be forced to use these 
kinds of gradients instead of user defined paints?


Glenn Thomas Hvidsten


---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xml.apache.org
For additional commands, e-mail: batik-users-help@xml.apache.org


Re: Batik and FreeHep

Posted by Andres Toussaint <an...@onemileup.com>.
I am not sure you are providing your rootGraphicsNode with the proper 
Graphics object.

>
> public void paint(Graphics g) {
>     this.g = (Graphics2D) g;
> }

I don't think you need to override the paint() method in your class,

> public void gvtBuildCompleted(GVTTreeBuilderEvent e) {
>     gvtTreeWalker = new GVTTreeWalker(svgCanvas.getGraphicsNode());
>     gvtTreeWalker.getRoot().paint(this.g);
> }
>

instead, change the last line to:

gvtTreeWalker.getRoot().paint(this.getGraphics());

to get access to this components Graphics object.

Andres.



---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xml.apache.org
For additional commands, e-mail: batik-users-help@xml.apache.org


Re: Batik and FreeHep

Posted by Glenn Thomas Hvidsten <gt...@corena.no>.
> You can Load your SVG into a JSVGComponent, and once the GVTTree is 
> build, you can get  a TreeWalker to paint your graphics2D objects in 
> whatever swing component you like.
> 
> JSVGComponent extends a JComponent, so it should work for you. I have a 
> custom SVG to DXF converter that works in the following manner:
> 
> I load my SVG into a JSVGComponent, and i register a listener to  wait 
> for a gvtTreeBuildCompleted event. Once this event is called, i create a 
> GVTTreeWalker, that holds all the SVG objects converted into Graphics2D 
> representations.
> 
> I then walk the GVTTree to parse all the Graphics 2D objects into DXF.
> 
> Another idea:
> You may want to implement the actions done by the 
> "org.apache.batik.gvt.GraphicsNode" in a custom JComponent of yours to 
> paint() your graphics2D content into your exporter. You can then simply 
> "paint()" the gvtRoot.

I'm not entirely sure what you mean here. I've made a class that extends 
JComponent:

class Foo extends JComponent implements GVTTreeBuilderListener {
private Graphics2D g = null;
JSVGCanvas svgCanvas = new JSVGCanvas();
GVTTreeWalker gvtTreeWalker = null;
...
...
public void paint(Graphics g) {
     this.g = (Graphics2D) g;
}
public void gvtBuildCompleted(GVTTreeBuilderEvent e) {
     gvtTreeWalker = new GVTTreeWalker(svgCanvas.getGraphicsNode());
     gvtTreeWalker.getRoot().paint(this.g);
}

But the call to paint in gvtBuildCompleted does not produce any visible 
results in the JFrame that has this class added.
The idea sound good and if you could provide me with some more details 
on how to do this I'd really appreciate it.

Glenn Thomas Hvidsten


---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xml.apache.org
For additional commands, e-mail: batik-users-help@xml.apache.org


Re: Batik and FreeHep

Posted by Andres Toussaint <an...@onemileup.com>.
>
> My problem is that freehep only use a standard Graphics2D-enabled
> component (i.e. JComponent) to be able to export. Batik, however, only
> outputs images to it's custom JSVGCanvas, which is not directly
> compatible with freehep.

You can Load your SVG into a JSVGComponent, and once the GVTTree is 
build, you can get  a TreeWalker to paint your graphics2D objects in 
whatever swing component you like.

JSVGComponent extends a JComponent, so it should work for you. I have a 
custom SVG to DXF converter that works in the following manner:

I load my SVG into a JSVGComponent, and i register a listener to  wait 
for a gvtTreeBuildCompleted event. Once this event is called, i create 
a GVTTreeWalker, that holds all the SVG objects converted into 
Graphics2D representations.

I then walk the GVTTree to parse all the Graphics 2D objects into DXF.

Another idea:
You may want to implement the actions done by the 
"org.apache.batik.gvt.GraphicsNode" in a custom JComponent of yours to 
paint() your graphics2D content into your exporter. You can then simply 
"paint()" the gvtRoot.

Andres.



---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xml.apache.org
For additional commands, e-mail: batik-users-help@xml.apache.org


Re: Batik and FreeHep

Posted by Glenn Thomas Hvidsten <gt...@corena.no>.
>> I've recently found the batik and the VectorGraphics package from the
>> freehep libraries
>> <url:http://java.freehep.org/vectorgraphics/index.html> which mostly do
>> exactly what I need; to load an SVG document and convert it to another
>> vector graphics format (.cgm or .pdf).
> 
> if you just want to convert from svg to pdf, you can use the 
> PDFTranscoder from the fop project together with the batik rasterizer. 
> There are some limitations however, since PDF does not support all SVG 
> features.

The primary conversion target is CGM, but I also convert to PDF so that 
I can see the results (I don't have a CGM-viewer here). And I figured 
that since the procedure for converting to a CGM is practically 
identical to PDF I can use the PDF converter for testing, but will need 
to use the CGM converter for the final conversion.


Glenn Thomas Hvidsten

=============  CORENA Norge AS  =============
== Dyrmyrgt. 35, N-3611 Kongsberg, NORWAY  ==
== Tlf: +47 3271 7234, Fax: +47 3271 7201  ==
== CORENA Home Page: http://www.corena.com ==
=============================================



---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xml.apache.org
For additional commands, e-mail: batik-users-help@xml.apache.org


Re: Batik and FreeHep

Posted by Christian Gawron <ch...@gmx.de>.
Hi,
Glenn Thomas Hvidsten wrote:
> Hi,
> 
> I hope you can help me out with some problems I've run into using Batik.
> I've been Googling for this the last few days without coming up with a
> solution. I've also tried asking on some newsgroups but to no success.
> 
> I've recently found the batik and the VectorGraphics package from the
> freehep libraries
> <url:http://java.freehep.org/vectorgraphics/index.html> which mostly do
> exactly what I need; to load an SVG document and convert it to another
> vector graphics format (.cgm or .pdf).
if you just want to convert from svg to pdf, you can use the PDFTranscoder from the fop 
project together with the batik rasterizer. There are some limitations however, since PDF 
does not support all SVG features.

Best wishes
Christian



---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xml.apache.org
For additional commands, e-mail: batik-users-help@xml.apache.org