You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

UnitConv.java 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * Copyright 2006 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /* $Id: FixedLength.java 279656 2005-09-08 22:06:48Z pietsch $ */
  17. package org.apache.fop.util;
  18. /**
  19. * Utility class for unit conversions.
  20. */
  21. public final class UnitConv {
  22. /** conversion factory from millimeters to inches. */
  23. public static final float IN2MM = 25.4f;
  24. /** conversion factory from centimeters to inches. */
  25. public static final float IN2CM = 2.54f;
  26. /** conversion factory from inches to points. */
  27. public static final int IN2PT = 72;
  28. /**
  29. * Converts millimeters (mm) to points (pt)
  30. * @param mm the value in mm
  31. * @return the value in pt
  32. */
  33. public static double mm2pt(double mm) {
  34. return mm * IN2PT / IN2MM;
  35. }
  36. /**
  37. * Converts millimeters (mm) to millipoints (mpt)
  38. * @param mm the value in mm
  39. * @return the value in mpt
  40. */
  41. public static double mm2mpt(double mm) {
  42. return mm * 1000 * IN2PT / IN2MM;
  43. }
  44. /**
  45. * Converts points (pt) to millimeters (mm)
  46. * @param pt the value in pt
  47. * @return the value in mm
  48. */
  49. public static double pt2mm(double pt) {
  50. return pt * IN2MM / IN2PT;
  51. }
  52. /**
  53. * Converts millimeters (mm) to inches (in)
  54. * @param mm the value in mm
  55. * @return the value in inches
  56. */
  57. public static double mm2in(double mm) {
  58. return mm / IN2MM;
  59. }
  60. /**
  61. * Converts inches (in) to millimeters (mm)
  62. * @param in the value in inches
  63. * @return the value in mm
  64. */
  65. public static double in2mm(double in) {
  66. return in * IN2MM;
  67. }
  68. /**
  69. * Converts inches (in) to millipoints (mpt)
  70. * @param in the value in inches
  71. * @return the value in mpt
  72. */
  73. public static double in2mpt(double in) {
  74. return in * IN2PT * 1000;
  75. }
  76. /**
  77. * Converts millipoints (mpt) to inches (in)
  78. * @param mpt the value in mpt
  79. * @return the value in inches
  80. */
  81. public static double mpt2in(double mpt) {
  82. return mpt / IN2PT / 1000;
  83. }
  84. /**
  85. * Converts millimeters (mm) to pixels (px)
  86. * @param mm the value in mm
  87. * @param resolution the resolution in dpi (dots per inch)
  88. * @return the value in pixels
  89. */
  90. public static int mm2px(double mm, int resolution) {
  91. return (int)Math.round(mm2in(mm) * resolution);
  92. }
  93. /**
  94. * Converts millipoints (mpt) to pixels (px)
  95. * @param mpt the value in mpt
  96. * @param resolution the resolution in dpi (dots per inch)
  97. * @return the value in pixels
  98. */
  99. public static int mpt2px(double mpt, int resolution) {
  100. return (int)Math.round(mpt2in(mpt) * resolution);
  101. }
  102. }