Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GrammaticalOptimization/SharpVectorModel/BasicTypes/SvgPreserveAspectRatio.cs @ 12762

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

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

File size: 7.0 KB
Line 
1using System;
2using System.Text.RegularExpressions;
3
4namespace SharpVectors.Dom.Svg
5{
6    public sealed class SvgPreserveAspectRatio : ISvgPreserveAspectRatio
7    {
8        #region Static members
9        private static Regex parCheck = new Regex("^(?<align>[A-Za-z]+)\\s*(?<meet>[A-Za-z]*)$");
10        #endregion
11
12        #region Private Fields
13
14        private SvgElement ownerElement;
15
16        private SvgMeetOrSlice _meetOrSlice;
17        private SvgPreserveAspectRatioType _alignment;
18
19        #endregion
20
21        #region Constructors
22        public SvgPreserveAspectRatio(string attr, SvgElement ownerElement)
23        {
24            this.ownerElement = ownerElement;
25
26            Match match = parCheck.Match(attr.Trim());
27            if (match.Groups["align"].Success)
28            {
29                switch (match.Groups["align"].Value)
30                {
31                    case "none":
32                        _alignment = SvgPreserveAspectRatioType.None;
33                        break;
34                    case "xMinYMin":
35                        _alignment = SvgPreserveAspectRatioType.XMinYMin;
36                        break;
37                    case "xMidYMin":
38                        _alignment = SvgPreserveAspectRatioType.XMidYMin;
39                        break;
40                    case "xMaxYMin":
41                        _alignment = SvgPreserveAspectRatioType.XMaxYMin;
42                        break;
43                    case "xMinYMid":
44                        _alignment = SvgPreserveAspectRatioType.XMinYMid;
45                        break;
46                    case "xMaxYMid":
47                        _alignment = SvgPreserveAspectRatioType.XMaxYMid;
48                        break;
49                    case "xMinYMax":
50                        _alignment = SvgPreserveAspectRatioType.XMinYMax;
51                        break;
52                    case "xMidYMax":
53                        _alignment = SvgPreserveAspectRatioType.XMidYMax;
54                        break;
55                    case "xMaxYMax":
56                        _alignment = SvgPreserveAspectRatioType.XMaxYMax;
57                        break;
58                    default:
59                        _alignment = SvgPreserveAspectRatioType.XMidYMid;
60                        break;
61                }
62            }
63            else
64            {
65                //TODO--PAUL: align = SvgPreserveAspectRatioType.XMidYMid;
66                _alignment = SvgPreserveAspectRatioType.XMidYMid;
67            }
68
69            if (match.Groups["meet"].Success)
70            {
71                switch (match.Groups["meet"].Value)
72                {
73                    case "slice":
74                        _meetOrSlice = SvgMeetOrSlice.Slice;
75                        break;
76                    case "meet":
77                        _meetOrSlice = SvgMeetOrSlice.Meet;
78                        break;
79                    case "":
80                        _meetOrSlice = SvgMeetOrSlice.Meet;
81                        break;
82                    default:
83                        _meetOrSlice = SvgMeetOrSlice.Unknown;
84                        break;
85                }
86            }
87            else
88            {
89                _meetOrSlice = SvgMeetOrSlice.Meet;
90            }
91        }
92        #endregion
93
94        #region Public Methods
95
96        public double[] FitToViewBox(SvgRect viewBox, SvgRect rectToFit)
97        {
98            if (ownerElement is SvgSvgElement)
99            {
100                ISvgMatrix mat = ((SvgSvgElement)ownerElement).ViewBoxTransform;
101                return new double[] { mat.E, mat.F, mat.A, mat.D };
102            }                                           
103
104            double translateX = 0;
105            double translateY = 0;
106            double scaleX = 1;
107            double scaleY = 1;
108
109            if (!viewBox.IsEmpty)
110            {
111                // calculate scale values for non-uniform scaling
112                scaleX = rectToFit.Width / viewBox.Width;
113                scaleY = rectToFit.Height / viewBox.Height;
114
115                if (_alignment != SvgPreserveAspectRatioType.None)
116                {
117                    // uniform scaling
118                    if (_meetOrSlice == SvgMeetOrSlice.Meet)
119                        scaleX = Math.Min(scaleX, scaleY);
120                    else
121                        scaleX = Math.Max(scaleX, scaleY);
122
123                    scaleY = scaleX;
124
125                    if (_alignment == SvgPreserveAspectRatioType.XMidYMax ||
126                      _alignment == SvgPreserveAspectRatioType.XMidYMid   ||
127                      _alignment == SvgPreserveAspectRatioType.XMidYMin)
128                    {
129                        // align to the Middle X
130                        translateX = (rectToFit.X + rectToFit.Width / 2) - scaleX * (viewBox.X + viewBox.Width / 2);
131                    }
132                    else if (_alignment == SvgPreserveAspectRatioType.XMaxYMax ||
133                      _alignment == SvgPreserveAspectRatioType.XMaxYMid        ||
134                      _alignment == SvgPreserveAspectRatioType.XMaxYMin)
135                    {
136                        // align to the right X
137                        translateX = (rectToFit.Width - viewBox.Width * scaleX);
138                    }
139
140                    if (_alignment == SvgPreserveAspectRatioType.XMaxYMid ||
141                      _alignment == SvgPreserveAspectRatioType.XMidYMid   ||
142                      _alignment == SvgPreserveAspectRatioType.XMinYMid)
143                    {
144                        // align to the Middle Y
145                        translateY = (rectToFit.Y + rectToFit.Height / 2) - scaleY * (viewBox.Y + viewBox.Height / 2);
146                    }
147                    else if (_alignment == SvgPreserveAspectRatioType.XMaxYMax ||
148                      _alignment == SvgPreserveAspectRatioType.XMidYMax        ||
149                      _alignment == SvgPreserveAspectRatioType.XMinYMax)
150                    {
151                        // align to the bottom Y
152                        translateY = (rectToFit.Height - viewBox.Height * scaleY);
153                    }
154                }
155                else
156                {
157                    translateX = -viewBox.X * scaleX;
158                    translateY = -viewBox.Y * scaleY;
159                }
160            }
161
162            return new double[]{
163                translateX,
164                translateY,
165                scaleX,
166                scaleY };
167        }
168
169        #endregion
170
171        #region ISvgPreserveAspectRatio Members
172
173        public SvgPreserveAspectRatioType Align
174        {
175            get
176            {
177                return _alignment;
178            }
179            set
180            {
181                throw new NotImplementedException();
182            }
183        }
184
185        public SvgMeetOrSlice MeetOrSlice
186        {
187            get
188            {
189                return _meetOrSlice;
190            }
191            set
192            {
193                throw new NotImplementedException();
194            }
195        }
196
197        #endregion
198    }
199}
Note: See TracBrowser for help on using the repository browser.