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 Martin Morawetz <ma...@kreativezone.at> on 2005/12/22 20:54:39 UTC

How to render the content of a SVG-File into some Graphics2D Object?

I'd like to paint the content of an existing
Svg-File into some other Graphics2D Object.

I experimented whith BridgeContext and
GVTBuilder without success:

UserAgent userAgent = new UserAgentAdapter();
DocumentLoader loader = new DocumentLoader(userAgent);
BridgeContext ctx = new BridgeContext(userAgent, loader);
ctx.setDynamic(true);
SVGDocument svgDoc = (SVGDocument) loader.loadDocument(strInputFile);
GVTBuilder builder = new GVTBuilder();
GraphicsNode graphicsNode = builder.build(ctx, svgDoc);
graphicsNode.paint(newSvgGraphics2D);


The result is just:

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE svg PUBLIC '-//W3C//DTD SVG 1.0//EN' 
'http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd'>
<svg style="stroke-dasharray:none; shape-rendering:auto; 
font-family:&apos;sansserif&apos;; text-rendering:auto; fill-opacity:1; 
color-rendering:auto; color-interpolation:auto; font-size:12; 
fill:black; stroke:black; image-rendering:auto; stroke-miterlimit:10; 
stroke-linecap:square; stroke-linejoin:miter; font-style:normal; 
stroke-width:1; stroke-dashoffset:0; font-weight:normal; 
stroke-opacity:1;" xmlns="http://www.w3.org/2000/svg" 
contentScriptType="text/ecmascript" preserveAspectRatio="xMidYMid meet" 
xmlns:xlink="http://www.w3.org/1999/xlink" zoomAndPan="magnify" 
version="1.0" contentStyleType="text/css">
   <!--Generated by the Batik Graphics2D SVG Generator-->
   <defs id="genericDefs" />
   <g>
     <defs id="defs1">
       <clipPath clipPathUnits="userSpaceOnUse" id="clipPath1">
         <path d="M0 0 L1 0 L1 1 L0 1 L0 0 Z" />
       </clipPath>
     </defs>
     <g style="fill:white; stroke:white;">
       <rect x="0" y="0" width="640" style="clip-path:url(#clipPath1); 
stroke:none;" height="480" />
     </g>
   </g>
</svg>


The output is not even a subset of the original svg-file.

Has anybody an idea how to solve my problem?

Is there an other way than using the GVTBuilder?

-- 
Regards
Martin

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


Re: How to render the content of a SVG-File into some Graphics2D Object?

Posted by Martin Morawetz <ma...@kreativezone.at>.
thomas.deweese@kodak.com wrote:
> Hi Martin,
> 
> Martin Morawetz <ma...@kreativezone.at> wrote on 01/02/2006 
> 02:27:01 PM:
> 
> 
>>But now I've got a new problem. I would need the size in px.
>>I was able to specify the size by using the setSVGCanvasSize()
>>Method, but as it just takes a Dimension object without
>>identifier (like px...):
> 
> 
>    Well 'px' are easy in SVG 'unitless' values are ==
> to the same value with 'px'.
> 
I know, a colleague of mine has to include the svg in a pdf File,
and she insists on 'px'. So I've to provide 'px' ;-)

> 
>>svgGraphics2D.setSVGCanvasSize(new Dimension(width, height));
> 
> 
> 
>>The (more direct, I guess) approach:
>>
>>//Element svgRoot = svgGenerator.getRoot();
>>Element svgRoot = svgGraphics2D.getDOMFactory().getDocumentElement();
>>svgRoot.setAttributeNS(null, "height", height + "px");
>>svgRoot.setAttributeNS(null, "width", width + "px");
>>
>>didn't work. The result again is an almost empty SVG-File.
> 
> 
>     For a differnent reason though, you need to ensure you only
> get the DocumentElement once.

How? The chart-library we use paints into the SVGGraphics Object.
I've not found a way to get the svg-size set by modifying properties
of the chart-library.

chart.paint(svgGraphics);

Trying to get the DocumentElement after that doesn't work as it
creates a new one as you outlined.

Is there an other way to get the size set? Maybe without
the need to parse the document again and modifying the node?

-- 
Regards
Martin

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


Re: How to render the content of a SVG-File into some Graphics2D Object?

Posted by th...@kodak.com.
Hi Martin,

Martin Morawetz <ma...@kreativezone.at> wrote on 01/02/2006 
02:27:01 PM:

> But now I've got a new problem. I would need the size in px.
> I was able to specify the size by using the setSVGCanvasSize()
> Method, but as it just takes a Dimension object without
> identifier (like px...):

   Well 'px' are easy in SVG 'unitless' values are ==
to the same value with 'px'.

> svgGraphics2D.setSVGCanvasSize(new Dimension(width, height));


> The (more direct, I guess) approach:
> 
> //Element svgRoot = svgGenerator.getRoot();
> Element svgRoot = svgGraphics2D.getDOMFactory().getDocumentElement();
> svgRoot.setAttributeNS(null, "height", height + "px");
> svgRoot.setAttributeNS(null, "width", width + "px");
> 
> didn't work. The result again is an almost empty SVG-File.

    For a differnent reason though, you need to ensure you only
get the DocumentElement once.  It creates a new one after you 
request the first one and all drawing goes into that root.

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


Re: How to render the content of a SVG-File into some Graphics2D Object?

Posted by Martin Morawetz <ma...@kreativezone.at>.
Hi Thomas,

> Martin Morawetz <ma...@kreativezone.at> wrote on 12/22/2005 
> 02:54:39 PM:
> 
> 
>>I'd like to paint the content of an existing
>>Svg-File into some other Graphics2D Object.
> 
> 
>    You are going down exactly the right road here.
> 
> 
>>I experimented with BridgeContext and
>>GVTBuilder without success:
> 
> 
>    This code looks good, but without being able to run it
> a small error could be lurking. Can you provide a small
> stand-alone example (full Java source + SVG file being
> read). It looks to me like there is confusion over what
> part of the SVG document should be rendered.  It
> might be as simple as the source document lacking
> width/height and/or viewBox attributes.  Also if the
> newSvgGraphics2D doesn't have a set 'size' it could
> cause issues.  All hard to track down w/o a running
> example of the problem.
> 

Thanks a lot, you just gave me the hint I needed. When
I created the the SVG-Graphics I didn't provide height
and width. Setting the size solved the problem.

But now I've got a new problem. I would need the size in px.
I was able to specify the size by using the setSVGCanvasSize()
Method, but as it just takes a Dimension object without
identifier (like px...):

svgGraphics2D.setSVGCanvasSize(new Dimension(width, height));

The (more direct, I guess) approach:

//Element svgRoot = svgGenerator.getRoot();
Element svgRoot = svgGraphics2D.getDOMFactory().getDocumentElement();
svgRoot.setAttributeNS(null, "height", height + "px");
svgRoot.setAttributeNS(null, "width", width + "px");

didn't work. The result again is an almost empty SVG-File.


Any Ideas?

--
Cheers
Martin

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


Re: How to render the content of a SVG-File into some Graphics2D Object?

Posted by th...@kodak.com.
Hi Martin,

Martin Morawetz <ma...@kreativezone.at> wrote on 12/22/2005 
02:54:39 PM:

> I'd like to paint the content of an existing
> Svg-File into some other Graphics2D Object.

   You are going down exactly the right road here.

> I experimented with BridgeContext and
> GVTBuilder without success:

   This code looks good, but without being able to run it
a small error could be lurking. Can you provide a small
stand-alone example (full Java source + SVG file being
read). It looks to me like there is confusion over what
part of the SVG document should be rendered.  It
might be as simple as the source document lacking
width/height and/or viewBox attributes.  Also if the
newSvgGraphics2D doesn't have a set 'size' it could
cause issues.  All hard to track down w/o a running
example of the problem.


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