flush
[mcs.git] / class / System.Drawing / System.Drawing.Printing / PrinterUnitConvert.cs
blob00452060da083b39b27e81ff36e100e8f0e8eaaf
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 //
12 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
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.
34 namespace System.Drawing.Printing
36 public sealed class PrinterUnitConvert
38 private PrinterUnitConvert ()
42 public static double Convert (double value,
43 PrinterUnit fromUnit,
44 PrinterUnit toUnit)
46 switch (fromUnit)
48 case PrinterUnit.Display:
49 switch (toUnit)
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;
56 break;
57 case PrinterUnit.ThousandthsOfAnInch:
58 switch (toUnit)
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;
65 break;
66 case PrinterUnit.HundredthsOfAMillimeter:
67 switch (toUnit)
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;
74 break;
75 case PrinterUnit.TenthsOfAMillimeter:
76 switch (toUnit)
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;
83 break;
85 // should never happen
86 throw new NotImplementedException();
89 public static int Convert (int value,
90 PrinterUnit fromUnit,
91 PrinterUnit toUnit)
93 double rslt;
94 rslt = Convert ((double) value, fromUnit, toUnit);
95 return (int) Math.Round (rslt);
99 public static Margins Convert (Margins value,
100 PrinterUnit fromUnit,
101 PrinterUnit toUnit)
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));
110 public static Point Convert (Point value,
111 PrinterUnit fromUnit,
112 PrinterUnit toUnit)
114 return new Point(
115 Convert(value.X, fromUnit, toUnit),
116 Convert(value.Y, fromUnit, toUnit));
119 public static Rectangle Convert (Rectangle value,
120 PrinterUnit fromUnit,
121 PrinterUnit toUnit)
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));
130 public static Size Convert (Size value,
131 PrinterUnit fromUnit,
132 PrinterUnit toUnit)
134 return new Size(
135 Convert(value.Width, fromUnit, toUnit),
136 Convert(value.Height, fromUnit, toUnit));