GraphicsPathGraphicsNode.cs :  » GUI » SharpVectorGraphics » SharpVectors » Renderer » Gdi » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » GUI » SharpVectorGraphics 
SharpVectorGraphics » SharpVectors » Renderer » Gdi » GraphicsPathGraphicsNode.cs
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Text.RegularExpressions;
using System.Diagnostics;

using SharpVectors.Dom.Css;
using SharpVectors.Dom.Svg;
using SharpVectors.Dom.Svg.Rendering;


namespace SharpVectors.Renderer.Gdi{
  public class GDIPathGraphicsNode : GraphicsNode
  {
        #region Constructor
    public GDIPathGraphicsNode(SvgElement element) : base(element)
    {
    }
        #endregion
    
    #region Marker code
    private string extractMarkerUrl(string propValue)
    {
      Regex reUrl = new Regex(@"^url\((?<uri>.+)\)$");

      Match match = reUrl.Match(propValue);
      if(match.Success)
      {
        return match.Groups["uri"].Value;
      }
      else
      {
        return String.Empty;
      }
    }

    private void PaintMarkers(GdiRenderer renderer, SvgStyleableElement styleElm, GraphicsWrapper gr)
    {    
      // OPTIMIZE

      if ( styleElm is ISharpMarkerHost )
      {
        string markerStartUrl = extractMarkerUrl(styleElm.GetPropertyValue("marker-start", "marker"));
        string markerMiddleUrl = extractMarkerUrl(styleElm.GetPropertyValue("marker-mid", "marker"));
        string markerEndUrl = extractMarkerUrl(styleElm.GetPropertyValue("marker-end", "marker"));

        RenderingNode grNode;
        if ( markerStartUrl.Length > 0 )
        {
          grNode = renderer.GetGraphicsNodeByUri(styleElm.BaseURI, markerStartUrl);
          if (grNode is SvgMarkerGraphicsNode)
          {
            ((SvgMarkerGraphicsNode) grNode).PaintMarker(renderer, gr, SvgMarkerPosition.Start, styleElm);
          }
        }

        if ( markerMiddleUrl.Length > 0 )
        {
          // TODO markerMiddleUrl != markerStartUrl
          grNode = renderer.GetGraphicsNodeByUri(styleElm.BaseURI, markerMiddleUrl);
          if ( grNode is SvgMarkerGraphicsNode )
          {
            ((SvgMarkerGraphicsNode) grNode).PaintMarker(renderer, gr, SvgMarkerPosition.Mid, styleElm);
          }
        }

        if ( markerEndUrl.Length > 0 )
        {
          // TODO: markerEndUrl != markerMiddleUrl
          grNode = renderer.GetGraphicsNodeByUri(styleElm.BaseURI, markerEndUrl);

          if(grNode is SvgMarkerGraphicsNode)
          {
            ((SvgMarkerGraphicsNode) grNode).PaintMarker(renderer, gr, SvgMarkerPosition.End, styleElm);
          }
        }
      }
    }
    #endregion

    #region Private methods
    private Brush GetBrush(GraphicsPath gp)
    {
      GdiSvgPaint paint = new GdiSvgPaint(element as SvgStyleableElement, "fill");
      return paint.GetBrush(gp);
    }

    private Pen GetPen(GraphicsPath gp)
    {
      GdiSvgPaint paint = new GdiSvgPaint(element as SvgStyleableElement, "stroke");
      return paint.GetPen(gp);
    }
    #endregion

    #region Public methods

        public override void BeforeRender(ISvgRenderer renderer)
        {
            if (_uniqueColor.IsEmpty)
              _uniqueColor = ((GdiRenderer)renderer)._getNextColor(this);

            GraphicsWrapper graphics = ((GdiRenderer) renderer).GraphicsWrapper;

            graphicsContainer = graphics.BeginContainer();
            SetQuality(graphics);
            Transform(graphics);
        }

        public override void Render(ISvgRenderer renderer)
        {
      GdiRenderer gdiRenderer = renderer as GdiRenderer;
            GraphicsWrapper graphics = gdiRenderer.GraphicsWrapper;

      if ( !(element is SvgClipPathElement) && !(element.ParentNode is SvgClipPathElement) )
      {
        SvgStyleableElement styleElm = element as SvgStyleableElement;
        if ( styleElm != null )
        {
          string sVisibility = styleElm.GetPropertyValue("visibility");
          string sDisplay = styleElm.GetPropertyValue("display");

          if (element is ISharpGDIPath && sVisibility != "hidden" && sDisplay != "none")
          {
            GraphicsPath gp = ((ISharpGDIPath)element).GetGraphicsPath();

            if ( gp != null )
            {
              Clip(graphics);

              GdiSvgPaint fillPaint = new GdiSvgPaint(styleElm, "fill");
              Brush brush = fillPaint.GetBrush(gp);

              GdiSvgPaint strokePaint = new GdiSvgPaint(styleElm, "stroke");
              Pen pen = strokePaint.GetPen(gp);

              if ( brush != null ) 
              {
                if ( brush is PathGradientBrush ) 
                {
                  GradientPaintServer gps = fillPaint.PaintServer as GradientPaintServer;
                  //GraphicsContainer container = graphics.BeginContainer();
                  
                  graphics.SetClip(gps.GetRadialGradientRegion(gp.GetBounds()), CombineMode.Exclude);

                  SolidBrush tempBrush = new SolidBrush(((PathGradientBrush)brush).InterpolationColors.Colors[0]);
                  graphics.FillPath(this, tempBrush,gp);
                  tempBrush.Dispose();
                  graphics.ResetClip();

                  //graphics.EndContainer(container);
                }
 
                graphics.FillPath(this, brush, gp);
                brush.Dispose();
              }

              if ( pen != null ) 
              {
                if ( pen.Brush is PathGradientBrush ) 
                {
                  GradientPaintServer gps = strokePaint.PaintServer as GradientPaintServer;
                  GraphicsContainerWrapper container = graphics.BeginContainer();

                  graphics.SetClip(gps.GetRadialGradientRegion(gp.GetBounds()), CombineMode.Exclude);

                  SolidBrush tempBrush = new SolidBrush(((PathGradientBrush)pen.Brush).InterpolationColors.Colors[0]);
                  Pen tempPen = new Pen(tempBrush, pen.Width);
                  graphics.DrawPath(this, tempPen,gp);
                  tempPen.Dispose();
                  tempBrush.Dispose();

                  graphics.EndContainer(container);
                }
 
                graphics.DrawPath(this, pen, gp);
                pen.Dispose();
              }
            }
          }
          PaintMarkers(gdiRenderer, styleElm, graphics);
        }
      }
    }
    #endregion

  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.