2
0

PrinterUnitConvert.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 * 2.54;
  51. case PrinterUnit.TenthsOfAMillimeter: return value * .254;
  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 * .254;
  60. case PrinterUnit.TenthsOfAMillimeter: return value * .0254;
  61. }
  62. break;
  63. case PrinterUnit.HundredthsOfAMillimeter:
  64. switch (toUnit)
  65. {
  66. case PrinterUnit.Display: return value / 2.54;
  67. case PrinterUnit.ThousandthsOfAnInch: return value / .254;
  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 / .254;
  76. case PrinterUnit.ThousandthsOfAnInch: return value / .0254;
  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. return (int)Convert((double)value, fromUnit, toUnit);
  90. }
  91. public static Margins Convert (Margins value,
  92. PrinterUnit fromUnit,
  93. PrinterUnit toUnit)
  94. {
  95. return new Margins(
  96. Convert(value.Left, fromUnit, toUnit),
  97. Convert(value.Right, fromUnit, toUnit),
  98. Convert(value.Top, fromUnit, toUnit),
  99. Convert(value.Bottom, fromUnit, toUnit));
  100. }
  101. public static Point Convert (Point value,
  102. PrinterUnit fromUnit,
  103. PrinterUnit toUnit)
  104. {
  105. return new Point(
  106. Convert(value.X, fromUnit, toUnit),
  107. Convert(value.Y, fromUnit, toUnit));
  108. }
  109. public static Rectangle Convert (Rectangle value,
  110. PrinterUnit fromUnit,
  111. PrinterUnit toUnit)
  112. {
  113. return new Rectangle(
  114. Convert(value.X, fromUnit, toUnit),
  115. Convert(value.Y, fromUnit, toUnit),
  116. Convert(value.Width, fromUnit, toUnit),
  117. Convert(value.Height, fromUnit, toUnit));
  118. }
  119. public static Size Convert (Size value,
  120. PrinterUnit fromUnit,
  121. PrinterUnit toUnit)
  122. {
  123. return new Size(
  124. Convert(value.Width, fromUnit, toUnit),
  125. Convert(value.Height, fromUnit, toUnit));
  126. }
  127. }
  128. }