Use CoreFX sources for System.Drawing.Imaging (#5048)
[mono.git] / mcs / class / System.Drawing / System.Drawing.Printing / PrinterUnitConvert.cs
1 //
2 // System.Drawing.Printing.PrinterUnitConvert.cs
3 //
4 // Authors:
5 //   Martin Willemoes Hansen (mwh@sysrq.dk)
6 //   Herve Poussineau (hpoussineau@fr.st)
7 //
8 // (C) 2003 Martin Willemoes Hansen
9 //
10
11 //
12 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
13 //
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
21 // 
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
24 // 
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 //
33
34 namespace System.Drawing.Printing
35 {
36         public sealed class PrinterUnitConvert
37         {
38                 private PrinterUnitConvert ()
39                 {
40                 }
41
42                 public static double Convert (double value,
43                                               PrinterUnit fromUnit,
44                                               PrinterUnit toUnit)
45                 {
46                         switch (fromUnit)
47                         {
48                                 case PrinterUnit.Display:
49                                         switch (toUnit)
50                                         {
51                                                 case PrinterUnit.Display: return value;
52                                                 case PrinterUnit.ThousandthsOfAnInch: return value * 10;
53                                                 case PrinterUnit.HundredthsOfAMillimeter: return value * 25.4;
54                                                 case PrinterUnit.TenthsOfAMillimeter: return value * 2.54;
55                                         }
56                                         break;
57                                 case PrinterUnit.ThousandthsOfAnInch:
58                                         switch (toUnit)
59                                         {
60                                                 case PrinterUnit.Display: return value / 10;
61                                                 case PrinterUnit.ThousandthsOfAnInch: return value;
62                                                 case PrinterUnit.HundredthsOfAMillimeter: return value * 2.54;
63                                                 case PrinterUnit.TenthsOfAMillimeter: return value * 0.254;
64                                         }
65                                         break;
66                                 case PrinterUnit.HundredthsOfAMillimeter:
67                                         switch (toUnit)
68                                         {
69                                                 case PrinterUnit.Display: return value / 25.4;
70                                                 case PrinterUnit.ThousandthsOfAnInch: return value / 2.54;
71                                                 case PrinterUnit.HundredthsOfAMillimeter: return value;
72                                                 case PrinterUnit.TenthsOfAMillimeter: return value / 10;
73                                         }
74                                         break;
75                                 case PrinterUnit.TenthsOfAMillimeter:
76                                         switch (toUnit)
77                                         {
78                                                 case PrinterUnit.Display: return value / 2.54;
79                                                 case PrinterUnit.ThousandthsOfAnInch: return value / 0.254;
80                                                 case PrinterUnit.HundredthsOfAMillimeter: return value * 10;
81                                                 case PrinterUnit.TenthsOfAMillimeter: return value;
82                                         }
83                                         break;
84                         }
85                         // should never happen
86                         throw new NotImplementedException();
87                 }
88
89                 public static int Convert (int value,
90                                            PrinterUnit fromUnit,
91                                            PrinterUnit toUnit)
92                 {
93                         double rslt;
94                         rslt = Convert ((double) value, fromUnit, toUnit);
95                         return (int) Math.Round (rslt);                 
96
97                 }
98
99                 public static Margins Convert (Margins value,
100                                                PrinterUnit fromUnit,
101                                                PrinterUnit toUnit)
102                 {
103                         return new Margins(
104                                 Convert(value.Left, fromUnit, toUnit),
105                                 Convert(value.Right, fromUnit, toUnit),
106                                 Convert(value.Top, fromUnit, toUnit),
107                                 Convert(value.Bottom, fromUnit, toUnit));
108                 }
109
110                 public static Point Convert (Point value,
111                                              PrinterUnit fromUnit,
112                                              PrinterUnit toUnit)
113                 {
114                         return new Point(
115                                 Convert(value.X, fromUnit, toUnit),
116                                 Convert(value.Y, fromUnit, toUnit));
117                 }
118
119                 public static Rectangle Convert (Rectangle value,
120                                                  PrinterUnit fromUnit,
121                                                  PrinterUnit toUnit)
122                 {
123                         return new Rectangle(
124                                 Convert(value.X, fromUnit, toUnit),
125                                 Convert(value.Y, fromUnit, toUnit),
126                                 Convert(value.Width, fromUnit, toUnit),
127                                 Convert(value.Height, fromUnit, toUnit));
128                 }
129
130                 public static Size Convert (Size value,
131                                             PrinterUnit fromUnit,
132                                             PrinterUnit toUnit)
133                 {
134                         return new Size(
135                                 Convert(value.Width, fromUnit, toUnit),
136                                 Convert(value.Height, fromUnit, toUnit));
137                 }
138         }
139 }