This project has moved and is read-only. For the latest updates, please go here.

Use of SharpVectors to parse SVG

Topics: SVG# and Parsing
Jun 30, 2011 at 8:14 AM

Hi,

  I've been using the boxViewer control to display some SVG files, but I'm now interested in parsing some of the elements of the loaded SVG file.  Which are the correct interfaces to use to do this?

  Ideally I would like to do something like -

SharpVectors.Dom.Svg.SvgDocument doc = new SharpVectors.Dom.Svg.SvgDocument(window);
doc.Load("file.svg");

  However I can't seem to work out what "window" should be, or whether I can in fact load and parse the SVG this way.  Once loaded I have a couple of groups within the SVG file I would like to measure and/or get the bounding box sizes of.  

Shaw

Coordinator
Jun 30, 2011 at 8:36 AM
Edited Jun 30, 2011 at 8:37 AM

Hello Shaw,

By design the parsing requires an ISvgWindow/SvgWindow implementation to work.
Currently, two implementations are provided; WPF and GDI+.

http://sharpvectors.codeplex.com/SourceControl/changeset/view/7326#85270

and

http://sharpvectors.codeplex.com/SourceControl/changeset/view/7326#85228

The SvgWindow is required later for the rendering and not for the parsing. You can
provide a dummy implementation. Note that this is part of the SVG 1.2 requirements.  

You can find the abstract class, SvgWindow, in the SharpVectorModel\DocumentStructure or here

http://sharpvectors.codeplex.com/SourceControl/changeset/view/7326#85414

Best regards,
Paul. 

Jun 30, 2011 at 9:10 AM
Thanks for the speedy response. I implemented it using the Wpf renderer for now - but I'm wondering whether the SvgGElement GetBBox() method will return the bounding box of any contained elements since it doesn't have a size itself?

Thanks

On Thu, Jun 30, 2011 at 5:36 PM, SelormeyPaul <notifications@codeplex.com> wrote:

From: SelormeyPaul

Hello Shaw,

By design the parsing requires an ISvgWindow/SvgWindow implementation to work.
Currently, two implementations are provided; WPF and GDI+.

http://sharpvectors.codeplex.com/SourceControl/changeset/view/7326#85270

and

http://sharpvectors.codeplex.com/SourceControl/changeset/view/7326#85228

The SvgWindow is required later for the rendering and not for the parsing. You can
provide a dummy implementation. Note that this part of the SVG 1.2 requirements.

You can find the abstract class SvgWindow in the SharpVectorModel\DocumentStructure or here

http://sharpvectors.codeplex.com/SourceControl/changeset/view/7326#85414

Best regards,
Paul.

Read the full discussion online.

To add a post to this discussion, reply to this email (sharpvectors@discussions.codeplex.com)

To start a new discussion for this project, email sharpvectors@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe on CodePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at CodePlex.com


Coordinator
Jun 30, 2011 at 9:29 AM

>>Shaw: Thanks for the speedy response.

At work, have access to the Codeplex and it does not require much to answer.

>>ShawSvgGElement GetBBox() method will return the bounding box of any contained elements since it doesn't have a size itself?

It is related to this post http://sharpvectors.codeplex.com/discussions/259774

GetBBox is part of the reason the window comes in, see the implementation in SvgTransformableElement.GetBBox().
Currently, the rendering does not keep the mapping information of the SVG element to the WPF element, still trying to find the
best way to handle this should we could still support multiple rendererers. The control-based GDI+ renderer may provide that
information, but I cannot verify how updated that information is, currently. Let me know what you wish to achieve, perhaps there
might be a way out. 

Best regards,
Paul. 

Jul 1, 2011 at 12:54 AM
Hi Paul,

I have been playing around with some snippets of code from the WPF renderer. Basically what I have is a file containing a couple of nested grids of boxes. The top level boxes are four corner markers for the file - like the outer extents, marked by four rectangular markers. These are contained in a group "g" element. What I want to achieve is to open the svg file, and find the bounding box that contains these four markers...

The file defines a document definition which I'm writing an OCR detection routine for - if I can load the SVG and measure the bbox of those four markers, then I would like to iterate through the various other regions of the svg file to match them against the captured image.

I was hoping to avoid parsing it all in XML myself - and it seems like SharpVectors should allow me to do that right?

Thanks again for your quick responses yesterday.

On Thu, Jun 30, 2011 at 6:29 PM, SelormeyPaul <notifications@codeplex.com> wrote:

From: SelormeyPaul

>>Shaw: Thanks for the speedy response.

At work, have access to the Codeplex and it does not require much to answer.

>>Shaw: SvgGElement GetBBox() method will return the bounding box of any contained elements since it doesn't have a size itself?

It is related to this post http://sharpvectors.codeplex.com/discussions/259774

GetBBox is part of the reason the window comes in, see the implementation in SvgTransformableElement.GetBBox().
Currently, the rendering does not keep the mapping information of the SVG element to the WPF element, still trying to find the
best way to handle this should we could still support multiple rendererers. The control-based GDI+ renderer may provide that
information, but I cannot verify how updated that information is, currently. Let me know what you wish to achieve, perhaps there
might be a way out.

Best regards,
Paul.

Read the full discussion online.

To add a post to this discussion, reply to this email (sharpvectors@discussions.codeplex.com)

To start a new discussion for this project, email sharpvectors@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe on CodePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at CodePlex.com




--

Coordinator
Jul 1, 2011 at 4:30 AM

>>Shawfind the bounding box that contains these four markers...

Being special, they most likely have id assigned to them. The content of the link
I posted previous should provide enough to get this done, if not please let me know...

Bounding Box computation

>>ShawI was hoping to avoid parsing it all in XML ... SharpVectors should allow me to do that right?

SVG elements are defined by view boxes and transformations, therefore the bounding box is not truly defined until
it is rendered, so convert it and use the explanation in the above link to get the bounding box.
Otherwise, you will have to apply all the transformations and others manually to get such information - if I am missing
something let me know.

Best regards,
Paul.

Jul 1, 2011 at 5:42 AM
I managed to get it to work the way I wanted through the following code:

            WpfDrawingSettings settings = new WpfDrawingSettings();
            settings.IncludeRuntime = true;
 
            FileSvgReader reader = new FileSvgReader(settings);
            DrawingGroup group = reader.Read(cmapFile);
 
            DrawingGroup drawingLayer = FindNamedDrawingGroup(group, "Layer");
            debugText.Text = drawingLayer.Children[0].GetValue(FrameworkElement.NameProperty) as string;
 
            DrawingGroup markList = FindNamedDrawingGroup(group, "boundaries");
            DrawingGroup gridList = FindNamedDrawingGroup(group, "details");
 
            DrawingGroup myGroup = new DrawingGroup();
            myGroup.Children.Add(markList);
            myGroup.Children.Add(gridList);
 
            DrawingImage drawingImage = new DrawingImage();
 
            drawingImage.Drawing = myGroup;
            svgImage.Source = drawingImage;

It seems to work nicely, and I can pull out individual groups and add them to the drawing group as needed.

Thanks for your tips, they got me going in the right direction :)

Shaw

On Fri, Jul 1, 2011 at 1:30 PM, SelormeyPaul <notifications@codeplex.com> wrote:

From: SelormeyPaul

>>Shaw: find the bounding box that contains these four markers...

Being special, they most likely have id assigned to them. The content of the link
I posted previous should provide enough to get this done, if not please let me know...

Bounding Box computation

>>Shaw: I was hoping to avoid parsing it all in XML ... SharpVectors should allow me to do that right?

SVG elements are defined by view boxes and transformations, therefore the bounding box is not truly defined until
it is rendered, so convert it and use the explanation in the above link to get the bounding box.
Otherwise, you will have to apply all the transformations and others manually to get such information - if I am missing
something let me know.

Best regards,
Paul.

Read the full discussion online.

To add a post to this discussion, reply to this email (sharpvectors@discussions.codeplex.com)

To start a new discussion for this project, email sharpvectors@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe on CodePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at CodePlex.com




--