您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

Units.java 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. import java.awt.geom.Dimension2D;
  17. import java.awt.geom.Rectangle2D;
  18. public class Units {
  19. /**
  20. * In Escher absolute distances are specified in
  21. * English Metric Units (EMUs), occasionally referred to as A units;
  22. * there are 360000 EMUs per centimeter, 914400 EMUs per inch, 12700 EMUs per point.
  23. */
  24. public static final int EMU_PER_PIXEL = 9525;
  25. public static final int EMU_PER_POINT = 12700;
  26. public static final int EMU_PER_CENTIMETER = 360000;
  27. /** 72 points per inch (dpi) */
  28. public static final int EMU_PER_INCH = 12700*72;
  29. /** EMU_PER_POINT/20 */
  30. public static final int EMU_PER_DXA = 635;
  31. /**
  32. * Master DPI (576 pixels per inch).
  33. * Used by the reference coordinate system in PowerPoint (HSLF)
  34. */
  35. public static final int MASTER_DPI = 576;
  36. /**
  37. * Pixels DPI (96 pixels per inch)
  38. */
  39. public static final int PIXEL_DPI = 96;
  40. /**
  41. * Points DPI (72 pixels per inch)
  42. */
  43. public static final int POINT_DPI = 72;
  44. /**
  45. * Width of one "standard character" of the default font in pixels. Same for Calibri and Arial.
  46. * "Standard character" defined as the widest digit character in the given font.
  47. * Copied from XSSFWorkbook, since that isn't available here.
  48. * <p>
  49. * Note this is only valid for workbooks using the default Excel font.
  50. * <p>
  51. * Would be nice to eventually support arbitrary document default fonts.
  52. */
  53. public static final float DEFAULT_CHARACTER_WIDTH = 7.0017f;
  54. /**
  55. * Column widths are in fractional characters, this is the EMU equivalent.
  56. * One character is defined as the widest value for the integers 0-9 in the
  57. * default font.
  58. */
  59. public static final int EMU_PER_CHARACTER = (int) (EMU_PER_PIXEL * DEFAULT_CHARACTER_WIDTH);
  60. /**
  61. * Converts points to EMUs
  62. * @param points points
  63. * @return EMUs
  64. */
  65. public static int toEMU(double points){
  66. return (int)Math.rint(EMU_PER_POINT*points);
  67. }
  68. /**
  69. * Converts pixels to EMUs
  70. * @param pixels pixels
  71. * @return EMUs
  72. */
  73. public static int pixelToEMU(int pixels) {
  74. return pixels*EMU_PER_PIXEL;
  75. }
  76. /**
  77. * Converts EMUs to points
  78. * @param emu emu
  79. * @return points
  80. */
  81. public static double toPoints(long emu){
  82. return (emu == -1) ? -1.d : (double)emu/EMU_PER_POINT;
  83. }
  84. /**
  85. * Converts a value of type FixedPoint to a floating point
  86. *
  87. * @param fixedPoint value in fixed point notation
  88. * @return floating point (double)
  89. *
  90. * @see <a href="http://msdn.microsoft.com/en-us/library/dd910765(v=office.12).aspx">[MS-OSHARED] - 2.2.1.6 FixedPoint</a>
  91. */
  92. public static double fixedPointToDouble(int fixedPoint) {
  93. int i = (fixedPoint >> 16);
  94. int f = fixedPoint & 0xFFFF;
  95. return (i + f/65536d);
  96. }
  97. /**
  98. * Converts a value of type floating point to a FixedPoint
  99. *
  100. * @param floatPoint value in floating point notation
  101. * @return fixedPoint value in fixed points notation
  102. *
  103. * @see <a href="http://msdn.microsoft.com/en-us/library/dd910765(v=office.12).aspx">[MS-OSHARED] - 2.2.1.6 FixedPoint</a>
  104. */
  105. public static int doubleToFixedPoint(double floatPoint) {
  106. double fractionalPart = floatPoint % 1d;
  107. double integralPart = floatPoint - fractionalPart;
  108. int i = (int)Math.floor(integralPart);
  109. int f = (int)Math.rint(fractionalPart*65536d);
  110. return (i << 16) | (f & 0xFFFF);
  111. }
  112. public static double masterToPoints(int masterDPI) {
  113. double points = masterDPI;
  114. points *= POINT_DPI;
  115. points /= MASTER_DPI;
  116. return points;
  117. }
  118. public static int pointsToMaster(double points) {
  119. points *= MASTER_DPI;
  120. points /= POINT_DPI;
  121. return (int)Math.rint(points);
  122. }
  123. public static int pointsToPixel(double points) {
  124. points *= PIXEL_DPI;
  125. points /= POINT_DPI;
  126. return (int)Math.rint(points);
  127. }
  128. public static double pixelToPoints(double pixel) {
  129. double points = pixel;
  130. points *= POINT_DPI;
  131. points /= PIXEL_DPI;
  132. return points;
  133. }
  134. public static Dimension2D pointsToPixel(Dimension2D pointsDim) {
  135. double width = pointsDim.getWidth() * PIXEL_DPI / POINT_DPI;
  136. double height = pointsDim.getHeight() * PIXEL_DPI / POINT_DPI;
  137. return new Dimension2DDouble(width, height);
  138. }
  139. public static Dimension2D pixelToPoints(Dimension2D pointsDim) {
  140. double width = pointsDim.getWidth() * POINT_DPI / PIXEL_DPI;
  141. double height = pointsDim.getHeight() * POINT_DPI / PIXEL_DPI;
  142. return new Dimension2DDouble(width, height);
  143. }
  144. public static Rectangle2D pointsToPixel(Rectangle2D pointsDim) {
  145. double x = pointsDim.getX() * PIXEL_DPI / POINT_DPI;
  146. double y = pointsDim.getY() * PIXEL_DPI / POINT_DPI;
  147. double width = pointsDim.getWidth() * PIXEL_DPI / POINT_DPI;
  148. double height = pointsDim.getHeight() * PIXEL_DPI / POINT_DPI;
  149. return new Rectangle2D.Double(x, y, width, height);
  150. }
  151. public static Rectangle2D pixelToPoints(Rectangle2D pointsDim) {
  152. double x = pointsDim.getX() * POINT_DPI / PIXEL_DPI;
  153. double y = pointsDim.getY() * POINT_DPI / PIXEL_DPI;
  154. double width = pointsDim.getWidth() * POINT_DPI / PIXEL_DPI;
  155. double height = pointsDim.getHeight() * POINT_DPI / PIXEL_DPI;
  156. return new Rectangle2D.Double(x, y, width, height);
  157. }
  158. public static int charactersToEMU(double characters) {
  159. return (int) characters * EMU_PER_CHARACTER;
  160. }
  161. /**
  162. * @param columnWidth specified in 256ths of a standard character
  163. * @return equivalent EMUs
  164. */
  165. public static int columnWidthToEMU(int columnWidth) {
  166. return charactersToEMU(columnWidth / 256d);
  167. }
  168. public static double toDXA(long emu) {
  169. return (emu == -1) ? -1.d : (double)emu/EMU_PER_DXA;
  170. }
  171. }