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.

Units.java 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.util;
  16. /**
  17. * @author Yegor Kozlov
  18. */
  19. public class Units {
  20. /**
  21. * In Escher absolute distances are specified in
  22. * English Metric Units (EMUs), occasionally referred to as A units;
  23. * there are 360000 EMUs per centimeter, 914400 EMUs per inch, 12700 EMUs per point.
  24. */
  25. public static final int EMU_PER_PIXEL = 9525;
  26. public static final int EMU_PER_POINT = 12700;
  27. public static final int EMU_PER_CENTIMETER = 360000;
  28. /**
  29. * Master DPI (576 pixels per inch).
  30. * Used by the reference coordinate system in PowerPoint (HSLF)
  31. */
  32. public static final int MASTER_DPI = 576;
  33. /**
  34. * Pixels DPI (96 pixels per inch)
  35. */
  36. public static final int PIXEL_DPI = 96;
  37. /**
  38. * Points DPI (72 pixels per inch)
  39. */
  40. public static final int POINT_DPI = 72;
  41. /**
  42. * Width of one "standard character" of the default font in pixels. Same for Calibri and Arial.
  43. * "Standard character" defined as the widest digit character in the given font.
  44. * Copied from XSSFWorkbook, since that isn't available here.
  45. * <p>
  46. * Note this is only valid for workbooks using the default Excel font.
  47. * <p>
  48. * Would be nice to eventually support arbitrary document default fonts.
  49. */
  50. public static final float DEFAULT_CHARACTER_WIDTH = 7.0017f;
  51. /**
  52. * Column widths are in fractional characters, this is the EMU equivalent.
  53. * One character is defined as the widest value for the integers 0-9 in the
  54. * default font.
  55. */
  56. public static final int EMU_PER_CHARACTER = (int) (EMU_PER_PIXEL * DEFAULT_CHARACTER_WIDTH);
  57. /**
  58. * Converts points to EMUs
  59. * @param points points
  60. * @return EMUs
  61. */
  62. public static int toEMU(double points){
  63. return (int)Math.rint(EMU_PER_POINT*points);
  64. }
  65. /**
  66. * Converts pixels to EMUs
  67. * @param pixels pixels
  68. * @return EMUs
  69. */
  70. public static int pixelToEMU(int pixels) {
  71. return pixels*EMU_PER_PIXEL;
  72. }
  73. /**
  74. * Converts EMUs to points
  75. * @param emu emu
  76. * @return points
  77. */
  78. public static double toPoints(long emu){
  79. return (double)emu/EMU_PER_POINT;
  80. }
  81. /**
  82. * Converts a value of type FixedPoint to a floating point
  83. *
  84. * @param fixedPoint value in fixed point notation
  85. * @return floating point (double)
  86. *
  87. * @see <a href="http://msdn.microsoft.com/en-us/library/dd910765(v=office.12).aspx">[MS-OSHARED] - 2.2.1.6 FixedPoint</a>
  88. */
  89. public static double fixedPointToDouble(int fixedPoint) {
  90. int i = (fixedPoint >> 16);
  91. int f = fixedPoint & 0xFFFF;
  92. return (i + f/65536d);
  93. }
  94. /**
  95. * Converts a value of type floating point to a FixedPoint
  96. *
  97. * @param floatPoint value in floating point notation
  98. * @return fixedPoint value in fixed points notation
  99. *
  100. * @see <a href="http://msdn.microsoft.com/en-us/library/dd910765(v=office.12).aspx">[MS-OSHARED] - 2.2.1.6 FixedPoint</a>
  101. */
  102. public static int doubleToFixedPoint(double floatPoint) {
  103. double fractionalPart = floatPoint % 1d;
  104. double integralPart = floatPoint - fractionalPart;
  105. int i = (int)Math.floor(integralPart);
  106. int f = (int)Math.rint(fractionalPart*65536d);
  107. return (i << 16) | (f & 0xFFFF);
  108. }
  109. public static double masterToPoints(int masterDPI) {
  110. double points = masterDPI;
  111. points *= POINT_DPI;
  112. points /= MASTER_DPI;
  113. return points;
  114. }
  115. public static int pointsToMaster(double points) {
  116. points *= MASTER_DPI;
  117. points /= POINT_DPI;
  118. return (int)Math.rint(points);
  119. }
  120. public static int pointsToPixel(double points) {
  121. points *= PIXEL_DPI;
  122. points /= POINT_DPI;
  123. return (int)Math.rint(points);
  124. }
  125. public static double pixelToPoints(int pixel) {
  126. double points = pixel;
  127. points *= POINT_DPI;
  128. points /= PIXEL_DPI;
  129. return points;
  130. }
  131. public static int charactersToEMU(double characters) {
  132. return (int) characters * EMU_PER_CHARACTER;
  133. }
  134. /**
  135. * @param columnWidth specified in 256ths of a standard character
  136. * @return equivalent EMUs
  137. */
  138. public static int columnWidthToEMU(int columnWidth) {
  139. return charactersToEMU(columnWidth / 256d);
  140. }
  141. /**
  142. * @param twips (1/20th of a point) typically used for row heights
  143. * @return equivalent EMUs
  144. */
  145. public static int TwipsToEMU(short twips) {
  146. return (int) (twips / 20d * EMU_PER_POINT);
  147. }
  148. }