Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.ExtLibs/HeuristicLab.Netron/3.0.2672.12446/Netron.Diagramming.Core-3.0.2672.12446/Utils/Measurements.cs

Last change on this file was 4068, checked in by swagner, 14 years ago

Sorted usings and removed unused usings in entire solution (#1094)

File size: 15.3 KB
Line 
1using System;
2using System.Drawing;
3
4namespace Netron.Diagramming.Core {
5  static class Measurements {
6    [ThreadStaticAttribute()]
7    private static float screenDpiX = 96.0F;
8
9    [ThreadStaticAttribute()]
10    private static float screenDpiY = 96.0F;
11
12    private static float[,] conversionRatios = new float[,]{
13      {1.0F, 4.0F, 1.3333334e-002F, 0.33866665F, 0.95999998F},
14      {0.25F, 1.0F, 3.3333334e-003F, 8.4666662e-002F, 0.23999999F},
15      {75.0F, 300.0F, 1.0F, 25.4F, 72.0F},
16      {2.9527559F, 11.811024F, 3.9370079e-002F, 1.0F, 2.8346457F},
17      {1.0416666F, 4.1666665F, 1.3888889e-002F, 0.35277778F, 1.0F},
18      };
19
20    private static MeasurementsUnit[] englishUnits;
21
22    private static MeasurementsUnit[] metricUnits;
23
24
25    private static int GraphicsUnitIndex(GraphicsUnit graphicsUnit) {
26      int i = -1;
27      switch (graphicsUnit) {
28        case GraphicsUnit.Display:
29          i = 0;
30          break;
31
32        case GraphicsUnit.Document:
33          i = 1;
34          break;
35
36        case GraphicsUnit.Inch:
37          i = 2;
38          break;
39
40        case GraphicsUnit.Millimeter:
41          i = 3;
42          break;
43
44        case GraphicsUnit.Point:
45          i = 4;
46          break;
47      }
48      return i;
49    }
50
51    public static void InitScreenDPI() {
52      Graphics graphics = Graphics.FromHwnd((IntPtr)0);
53      if (graphics != null) {
54        screenDpiX = graphics.DpiX;
55        screenDpiY = graphics.DpiY;
56      }
57    }
58
59    public static float UnitsPerInch(GraphicsUnit unit) {
60      float f = 1.0F;
61      switch (unit) {
62        case GraphicsUnit.Inch:
63          return 1.0F;
64
65        case GraphicsUnit.Millimeter:
66          return 25.4F;
67
68        case GraphicsUnit.Point:
69          return 72.0F;
70
71        case GraphicsUnit.Document:
72          return 300.0F;
73
74        case GraphicsUnit.Display:
75          return 75.0F;
76
77        default:
78          return f;
79      }
80    }
81
82    public static float Convert(GraphicsUnit fromUnits, GraphicsUnit toUnits, float dpi, float value) {
83      float f1 = value;
84      if (fromUnits != toUnits) {
85        float f2;
86
87        GraphicsUnit graphicsUnit;
88
89        if (fromUnits == GraphicsUnit.Pixel) {
90          graphicsUnit = GraphicsUnit.Inch;
91          f2 = value / dpi;
92        } else {
93          f2 = value;
94          graphicsUnit = fromUnits;
95        }
96        if (graphicsUnit == toUnits) {
97          f1 = f2;
98        } else if (toUnits == GraphicsUnit.Pixel) {
99          if (graphicsUnit != GraphicsUnit.Inch) {
100            int i = GraphicsUnitIndex(graphicsUnit);
101            int j = GraphicsUnitIndex(GraphicsUnit.Inch);
102            f2 *= conversionRatios[i, j];
103          }
104          f1 = f2 * dpi;
105        } else {
106          int i = GraphicsUnitIndex(graphicsUnit);
107          int j = GraphicsUnitIndex(toUnits);
108          f1 = f2 * conversionRatios[i, j];
109        }
110      }
111      return f1;
112    }
113
114    public static float Convert(GraphicsUnit fromUnits, float fromScale, GraphicsUnit toUnits, float toScale, float dpi, float value) {
115      float f1 = value;
116      if (fromUnits != toUnits || fromScale != toScale) {
117        float f2;
118
119        GraphicsUnit graphicsUnit;
120
121        if (fromUnits == GraphicsUnit.Pixel) {
122          graphicsUnit = GraphicsUnit.Inch;
123          f2 = value / dpi;
124        } else {
125          f2 = value;
126          graphicsUnit = fromUnits;
127        }
128        f2 *= fromScale;
129        if (graphicsUnit == toUnits) {
130          f1 = f2;
131        } else if (toUnits == GraphicsUnit.Pixel) {
132          if (graphicsUnit != GraphicsUnit.Inch) {
133            int i = GraphicsUnitIndex(graphicsUnit);
134            int j = GraphicsUnitIndex(GraphicsUnit.Inch);
135            f2 *= conversionRatios[i, j];
136          }
137          f1 = f2 * dpi;
138        } else {
139          int i = GraphicsUnitIndex(graphicsUnit);
140          int j = GraphicsUnitIndex(toUnits);
141          f1 = f2 * conversionRatios[i, j];
142        }
143        f1 /= toScale;
144      }
145      return f1;
146    }
147
148    public static float Convert(MeasurementsUnit fromUnits, GraphicsUnit toUnits, float toScale, float dpi, float value) {
149      GraphicsUnit graphicsUnit;
150      float f;
151      MeasurementsUnitToGraphicsUnit(fromUnits, out graphicsUnit, out f);
152      return Convert(graphicsUnit, f, toUnits, toScale, dpi, value);
153    }
154
155    public static float Convert(GraphicsUnit fromUnits, float fromScale, MeasurementsUnit toUnits, float dpi, float value) {
156      GraphicsUnit graphicsUnit;
157      float f;
158      MeasurementsUnitToGraphicsUnit(toUnits, out graphicsUnit, out f);
159      return Convert(fromUnits, fromScale, graphicsUnit, f, dpi, value);
160    }
161
162    public static float Convert(MeasurementsUnit fromUnits, MeasurementsUnit toUnits, float dpi, float value) {
163      GraphicsUnit graphicsUnit1, graphicsUnit2;
164      float f1, f2;
165      MeasurementsUnitToGraphicsUnit(fromUnits, out graphicsUnit1, out f1);
166      MeasurementsUnitToGraphicsUnit(toUnits, out graphicsUnit2, out f2);
167      return Convert(graphicsUnit1, f1, graphicsUnit2, f2, dpi, value);
168    }
169
170    public static float Convert(GraphicsUnit fromUnits, GraphicsUnit toUnits, float value) {
171      return Convert(fromUnits, toUnits, screenDpiX, value);
172    }
173
174    public static float Convert(GraphicsUnit fromUnits, GraphicsUnit toUnits, Graphics grfx, float value) {
175      float f = screenDpiX;
176      if (grfx != null) {
177        f = grfx.DpiX;
178      }
179      return Convert(fromUnits, toUnits, f, value);
180    }
181
182    public static PointF Convert(GraphicsUnit fromUnits, GraphicsUnit toUnits, PointF value) {
183      float f1 = screenDpiX;
184      float f2 = screenDpiY;
185      float f3 = Convert(fromUnits, toUnits, f1, value.X);
186      float f4 = Convert(fromUnits, toUnits, f2, value.Y);
187      return new PointF(f3, f4);
188    }
189
190    public static PointF Convert(GraphicsUnit fromUnits, GraphicsUnit toUnits, Graphics grfx, PointF value) {
191      float f1 = screenDpiX;
192      float f2 = screenDpiY;
193      if (grfx != null) {
194        f1 = grfx.DpiX;
195        f2 = grfx.DpiY;
196      }
197      float f3 = Convert(fromUnits, toUnits, f1, value.X);
198      float f4 = Convert(fromUnits, toUnits, f2, value.Y);
199      return new PointF(f3, f4);
200    }
201
202    public static PointF Convert(GraphicsUnit fromUnits, float fromScale, GraphicsUnit toUnits, float toScale, Graphics grfx, PointF value) {
203      float f1 = screenDpiX;
204      float f2 = screenDpiY;
205      if (grfx != null) {
206        f1 = grfx.DpiX;
207        f2 = grfx.DpiY;
208      }
209      float f3 = Convert(fromUnits, fromScale, toUnits, toScale, f1, value.X);
210      float f4 = Convert(fromUnits, fromScale, toUnits, toScale, f2, value.Y);
211      return new PointF(f3, f4);
212    }
213
214    public static PointF Convert(MeasurementsUnit fromUnits, MeasurementsUnit toUnits, Graphics grfx, PointF value) {
215      float f1 = screenDpiX;
216      float f2 = screenDpiY;
217      if (grfx != null) {
218        f1 = grfx.DpiX;
219        f2 = grfx.DpiY;
220      }
221      float f3 = Convert(fromUnits, toUnits, f1, value.X);
222      float f4 = Convert(fromUnits, toUnits, f2, value.Y);
223      return new PointF(f3, f4);
224    }
225
226    public static PointF Convert(GraphicsUnit fromUnits, float fromScale, MeasurementsUnit toUnits, Graphics grfx, PointF value) {
227      float f1 = screenDpiX;
228      float f2 = screenDpiY;
229      if (grfx != null) {
230        f1 = grfx.DpiX;
231        f2 = grfx.DpiY;
232      }
233      float f3 = Convert(fromUnits, fromScale, toUnits, f1, value.X);
234      float f4 = Convert(fromUnits, fromScale, toUnits, f2, value.Y);
235      return new PointF(f3, f4);
236    }
237
238    public static PointF Convert(MeasurementsUnit fromUnits, GraphicsUnit toUnits, float toScale, Graphics grfx, PointF value) {
239      float f1 = screenDpiX;
240      float f2 = screenDpiY;
241      if (grfx != null) {
242        f1 = grfx.DpiX;
243        f2 = grfx.DpiY;
244      }
245      float f3 = Convert(fromUnits, toUnits, toScale, f1, value.X);
246      float f4 = Convert(fromUnits, toUnits, toScale, f2, value.Y);
247      return new PointF(f3, f4);
248    }
249
250    public static SizeF Convert(GraphicsUnit fromUnits, GraphicsUnit toUnits, SizeF value) {
251      float f1 = screenDpiX;
252      float f2 = screenDpiY;
253      float f3 = Convert(fromUnits, toUnits, f1, value.Width);
254      float f4 = Convert(fromUnits, toUnits, f2, value.Height);
255      return new SizeF(f3, f4);
256    }
257
258    public static SizeF Convert(GraphicsUnit fromUnits, GraphicsUnit toUnits, Graphics grfx, SizeF value) {
259      float f1 = screenDpiX;
260      float f2 = screenDpiY;
261      if (grfx != null) {
262        f1 = grfx.DpiX;
263        f2 = grfx.DpiY;
264      }
265      float f3 = Convert(fromUnits, toUnits, f1, value.Width);
266      float f4 = Convert(fromUnits, toUnits, f2, value.Height);
267      return new SizeF(f3, f4);
268    }
269
270    public static SizeF Convert(GraphicsUnit fromUnits, float fromScale, GraphicsUnit toUnits, float toScale, Graphics grfx, SizeF value) {
271      float f1 = screenDpiX;
272      float f2 = screenDpiY;
273      if (grfx != null) {
274        f1 = grfx.DpiX;
275        f2 = grfx.DpiY;
276      }
277      float f3 = Convert(fromUnits, fromScale, toUnits, toScale, f1, value.Width);
278      float f4 = Convert(fromUnits, fromScale, toUnits, toScale, f2, value.Height);
279      return new SizeF(f3, f4);
280    }
281
282    public static RectangleF Convert(GraphicsUnit fromUnits, GraphicsUnit toUnits, RectangleF value) {
283      float f1 = screenDpiX;
284      float f2 = screenDpiY;
285      float f3 = Convert(fromUnits, toUnits, f1, value.X);
286      float f4 = Convert(fromUnits, toUnits, f2, value.Y);
287      float f5 = Convert(fromUnits, toUnits, f1, value.Width);
288      float f6 = Convert(fromUnits, toUnits, f2, value.Height);
289      return new RectangleF(f3, f4, f5, f6);
290    }
291
292    public static RectangleF Convert(GraphicsUnit fromUnits, GraphicsUnit toUnits, Graphics grfx, RectangleF value) {
293      float f1 = screenDpiX;
294      float f2 = screenDpiY;
295      if (grfx != null) {
296        f1 = grfx.DpiX;
297        f2 = grfx.DpiY;
298      }
299      float f3 = Convert(fromUnits, toUnits, f1, value.X);
300      float f4 = Convert(fromUnits, toUnits, f2, value.Y);
301      float f5 = Convert(fromUnits, toUnits, f1, value.Width);
302      float f6 = Convert(fromUnits, toUnits, f2, value.Height);
303      return new RectangleF(f3, f4, f5, f6);
304    }
305
306    public static void MeasurementsUnitToGraphicsUnit(MeasurementsUnit unitMeasure, out GraphicsUnit grfxUnit, out float grfxScale) {
307      switch (unitMeasure) {
308        case MeasurementsUnit.SixteenthInches:
309          grfxUnit = GraphicsUnit.Inch;
310          grfxScale = 6.25e-002F;
311          return;
312
313        case MeasurementsUnit.EighthInches:
314          grfxUnit = GraphicsUnit.Inch;
315          grfxScale = 0.125F;
316          return;
317
318        case MeasurementsUnit.QuarterInches:
319          grfxUnit = GraphicsUnit.Inch;
320          grfxScale = 0.25F;
321          return;
322
323        case MeasurementsUnit.HalfInches:
324          grfxUnit = GraphicsUnit.Inch;
325          grfxScale = 0.5F;
326          return;
327
328        case MeasurementsUnit.Inches:
329          grfxUnit = GraphicsUnit.Inch;
330          grfxScale = 1.0F;
331          return;
332
333        case MeasurementsUnit.Feet:
334          grfxUnit = GraphicsUnit.Inch;
335          grfxScale = 12.0F;
336          return;
337
338        case MeasurementsUnit.Yards:
339          grfxUnit = GraphicsUnit.Inch;
340          grfxScale = 36.0F;
341          return;
342
343        case MeasurementsUnit.Miles:
344          grfxUnit = GraphicsUnit.Inch;
345          grfxScale = 63360.0F;
346          return;
347
348        case MeasurementsUnit.Millimeters:
349          grfxUnit = GraphicsUnit.Millimeter;
350          grfxScale = 1.0F;
351          return;
352
353        case MeasurementsUnit.Centimeters:
354          grfxUnit = GraphicsUnit.Millimeter;
355          grfxScale = 10.0F;
356          return;
357
358        case MeasurementsUnit.Meters:
359          grfxUnit = GraphicsUnit.Millimeter;
360          grfxScale = 1000.0F;
361          return;
362
363        case MeasurementsUnit.Kilometers:
364          grfxUnit = GraphicsUnit.Millimeter;
365          grfxScale = 1000000.0F;
366          return;
367
368        case MeasurementsUnit.Points:
369          grfxUnit = GraphicsUnit.Point;
370          grfxScale = 1.0F;
371          return;
372
373        default:
374          grfxUnit = GraphicsUnit.Pixel;
375          grfxScale = 1.0F;
376          return;
377      }
378    }
379
380    public static MeasurementSystem GetUnitsSystem(MeasurementsUnit units) {
381      MeasurementSystem measurementSystem = MeasurementSystem.English;
382      MeasurementsUnit[] measurementsUnits = metricUnits;
383      for (int i = 0; i < (int)measurementsUnits.Length; i++) {
384        if (measurementsUnits[i] == units) {
385          measurementSystem = MeasurementSystem.Metric;
386          break;
387        }
388      }
389      return measurementSystem;
390    }
391
392    public static bool GetLargerUnits(MeasurementsUnit units, out MeasurementsUnit largerUnits) {
393      bool flag = false;
394      largerUnits = units;
395      for (int i = 0; !flag && i < (int)englishUnits.Length - 1; i++) {
396        if (englishUnits[i] == units) {
397          largerUnits = englishUnits[i + 1];
398          flag = true;
399        }
400      }
401      for (int j = 0; !flag && j < (int)metricUnits.Length - 1; j++) {
402        if (metricUnits[j] == units) {
403          largerUnits = englishUnits[j + 1];
404        }
405      }
406      return flag;
407    }
408
409    public static bool GetSmallerUnits(MeasurementsUnit units, out MeasurementsUnit smallerUnits) {
410      bool flag = false;
411      smallerUnits = units;
412      for (int i = (int)englishUnits.Length - 1; !flag && i > 0; i--) {
413        if (englishUnits[i] == units) {
414          smallerUnits = englishUnits[i - 1];
415          flag = true;
416        }
417      }
418      for (int j = (int)metricUnits.Length - 1; !flag && j > 0; j--) {
419        if (metricUnits[j] == units) {
420          smallerUnits = metricUnits[j - 1];
421          flag = true;
422        }
423      }
424      return flag;
425    }
426
427    public static MeasurementsUnit GetSystemUnits(MeasurementSystem measureSys, int unitOrdinal) {
428      MeasurementsUnit measurementsUnit;
429
430      //if (measureSys == null)
431      //{
432      //  if (unitOrdinal < 0 || unitOrdinal >= (int)englishUnits.Length)
433      //  {
434      //      throw new InconsistencyException("Oops");
435      //  }
436      //  measurementsUnit = englishUnits[unitOrdinal];
437      //}
438      //else
439      {
440        if (measureSys != MeasurementSystem.Metric) {
441          throw new InconsistencyException("Oops");
442        }
443        if (unitOrdinal < 0 || unitOrdinal >= (int)metricUnits.Length) {
444          throw new InconsistencyException("Oops");
445        }
446        measurementsUnit = metricUnits[unitOrdinal];
447      }
448      return measurementsUnit;
449    }
450
451    static Measurements() {
452      MeasurementsUnit[] measurementsUnits = new MeasurementsUnit[8];
453      measurementsUnits[1] = MeasurementsUnit.EighthInches;
454      measurementsUnits[2] = MeasurementsUnit.QuarterInches;
455      measurementsUnits[3] = MeasurementsUnit.HalfInches;
456      measurementsUnits[4] = MeasurementsUnit.Inches;
457      measurementsUnits[5] = MeasurementsUnit.Feet;
458      measurementsUnits[6] = MeasurementsUnit.Yards;
459      measurementsUnits[7] = MeasurementsUnit.Miles;
460      englishUnits = measurementsUnits;
461      measurementsUnits = new MeasurementsUnit[] { MeasurementsUnit.Millimeters, MeasurementsUnit.Centimeters, MeasurementsUnit.Meters, MeasurementsUnit.Kilometers };
462      metricUnits = measurementsUnits;
463    }
464  }
465
466}
Note: See TracBrowser for help on using the repository browser.