PrinterUnitConvert.cs 4.2 KB

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