Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GrammaticalOptimization/SharpVectorRenderingGdi/Gdi/GdiMarkerRendering.cs @ 13777

Last change on this file since 13777 was 12762, checked in by aballeit, 9 years ago

#2283 GUI updates, Tree-chart, MCTS Version 2 (prune leaves)

File size: 3.5 KB
Line 
1using System;
2using System.Xml;
3using System.Drawing;
4using System.Drawing.Drawing2D;
5
6using SharpVectors.Dom.Svg;
7
8namespace SharpVectors.Renderers.Gdi
9{
10  public enum SvgMarkerPosition{Start, Mid, End}
11
12    public sealed class GdiMarkerRendering : GdiRendering
13  {
14        #region Constructors and Destructor
15
16    public GdiMarkerRendering(SvgElement element) : base(element)
17    {
18    }
19
20        #endregion
21
22        #region Public Methods
23
24        // disable default rendering
25        public override void BeforeRender(GdiGraphicsRenderer renderer)
26    {
27    }
28        public override void Render(GdiGraphicsRenderer renderer)
29        {
30        }
31        public override void AfterRender(GdiGraphicsRenderer renderer)
32        {
33        }
34
35    public void PaintMarker(GdiGraphicsRenderer renderer, GdiGraphicsWrapper gr,
36            SvgMarkerPosition markerPos, SvgStyleableElement refElement)
37    {
38      ISharpMarkerHost markerHostElm = (ISharpMarkerHost)refElement;
39      SvgMarkerElement markerElm     = (SvgMarkerElement) element;
40
41            SvgPointF[] vertexPositions = markerHostElm.MarkerPositions;
42      int start;
43      int len;
44
45      // Choose which part of the position array to use
46      switch (markerPos)
47      {
48        case SvgMarkerPosition.Start:
49          start = 0;
50          len = 1;
51          break;
52        case SvgMarkerPosition.Mid:
53          start = 1;
54          len = vertexPositions.Length - 2;
55          break;
56        default:
57          // == MarkerPosition.End
58          start = vertexPositions.Length-1;
59          len = 1;
60          break;
61      }
62
63      for (int i = start; i < start+len; i++)
64      {
65                SvgPointF point = vertexPositions[i];
66
67        GdiGraphicsContainer gc = gr.BeginContainer();
68
69        gr.TranslateTransform(point.X, point.Y);
70
71        if (markerElm.OrientType.AnimVal.Equals(SvgMarkerOrient.Angle))
72        {
73          gr.RotateTransform((float)markerElm.OrientAngle.AnimVal.Value);
74        }
75        else
76        {
77                    double angle;
78
79          switch(markerPos)
80          {
81            case SvgMarkerPosition.Start:
82              angle = markerHostElm.GetStartAngle(i + 1);
83              break;
84            case SvgMarkerPosition.Mid:
85              //angle = (markerHostElm.GetEndAngle(i) + markerHostElm.GetStartAngle(i + 1)) / 2;
86              angle = SvgNumber.CalcAngleBisection(markerHostElm.GetEndAngle(i), markerHostElm.GetStartAngle(i + 1));
87              break;
88            default:
89              angle = markerHostElm.GetEndAngle(i);
90              break;
91          }
92          gr.RotateTransform((float)angle);
93        }
94
95        if (markerElm.MarkerUnits.AnimVal.Equals(SvgMarkerUnit.StrokeWidth))
96        {
97          SvgLength strokeWidthLength = new SvgLength(refElement,
98                        "stroke-width", SvgLengthSource.Css, SvgLengthDirection.Viewport, "1");
99          float strokeWidth = (float)strokeWidthLength.Value;
100          gr.ScaleTransform(strokeWidth, strokeWidth);
101        }
102
103        SvgPreserveAspectRatio spar =
104                    (SvgPreserveAspectRatio)markerElm.PreserveAspectRatio.AnimVal;
105                double[] translateAndScale = spar.FitToViewBox((SvgRect)markerElm.ViewBox.AnimVal,
106          new SvgRect(0, 0, markerElm.MarkerWidth.AnimVal.Value,
107            markerElm.MarkerHeight.AnimVal.Value));
108
109
110        gr.TranslateTransform(-(float)(markerElm.RefX.AnimVal.Value * translateAndScale[2]),
111          -(float)(markerElm.RefY.AnimVal.Value * translateAndScale[3]));
112
113        gr.ScaleTransform((float)translateAndScale[2], (float)translateAndScale[3]);
114
115        Clip(gr);
116                             
117                renderer.RenderChildren(markerElm);
118
119        gr.EndContainer(gc);
120      }
121    }
122
123        #endregion
124  }
125}
Note: See TracBrowser for help on using the repository browser.