]> source.dussan.org Git - poi.git/commitdiff
Move my new unit conversions to the Units class, move and deprecate duplicate and...
authorGreg Woolsey <gwoolsey@apache.org>
Fri, 23 Jun 2017 15:43:48 +0000 (15:43 +0000)
committerGreg Woolsey <gwoolsey@apache.org>
Fri, 23 Jun 2017 15:43:48 +0000 (15:43 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1799683 13f79535-47bb-0310-9956-ffa450edef68

src/java/org/apache/poi/util/Units.java
src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFPicture.java
src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFClientAnchor.java
src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFShape.java
src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java
src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java
src/ooxml/java/org/apache/poi/xssf/util/EMUUtils.java [deleted file]

index 1d376e936db48881f12a5beafe071074680d9166..e969271da21c7b4eb969f7332f4b17ff515cf97d 100644 (file)
@@ -45,6 +45,25 @@ public class Units {
      */
     public static final int POINT_DPI = 72;    
 
+
+    /**
+     * Width of one "standard character" of the default font in pixels. Same for Calibri and Arial.
+     * "Standard character" defined as the widest digit character in the given font.
+     * Copied from XSSFWorkbook, since that isn't available here.
+     * <p/>
+     * Note this is only valid for workbooks using the default Excel font.
+     * <p/>
+     * Would be nice to eventually support arbitrary document default fonts.
+     */
+    public static final float DEFAULT_CHARACTER_WIDTH = 7.0017f;
+
+    /**
+     * Column widths are in fractional characters, this is the EMU equivalent.
+     * One character is defined as the widest value for the integers 0-9 in the 
+     * default font.
+     */
+    public static final int EMU_PER_CHARACTER = (int) (EMU_PER_PIXEL * DEFAULT_CHARACTER_WIDTH);
+
     /**
      * Converts points to EMUs
      * @param points points
@@ -127,4 +146,24 @@ public class Units {
         points /= PIXEL_DPI;
         return points;
     }
+    
+    public static int charactersToEMU(double characters) {
+        return (int) characters * EMU_PER_CHARACTER;
+    }
+    
+    /**
+     * @param columnWidth specified in 256ths of a standard character
+     * @return equivalent EMUs
+     */
+    public static int columnWidthToEMU(int columnWidth) {
+        return charactersToEMU(columnWidth / 256d);
+    }
+    
+    /**
+     * @param twips (1/20th of a point) typically used for row heights
+     * @return equivalent EMUs
+     */
+    public static int TwipsToEMU(short twips) {
+        return (int) (twips / 20d * EMU_PER_POINT);
+    }
 }
index 9635882973e05af77183ea92c46c40364da232d3..04475e0bc8e037a2cd569b486c867423356b3295 100644 (file)
@@ -29,14 +29,13 @@ import org.apache.poi.ss.util.ImageUtils;
 import org.apache.poi.util.Internal;
 import org.apache.poi.util.POILogFactory;
 import org.apache.poi.util.POILogger;
+import org.apache.poi.util.Units;
 import org.apache.poi.xssf.usermodel.XSSFAnchor;
 import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
 import org.apache.poi.xssf.usermodel.XSSFDrawing;
 import org.apache.poi.xssf.usermodel.XSSFPicture;
 import org.apache.poi.xssf.usermodel.XSSFPictureData;
-import org.apache.poi.xssf.usermodel.XSSFShape;
 import org.apache.poi.xssf.usermodel.XSSFSheet;
-import org.apache.poi.xssf.usermodel.XSSFWorkbook;
 import org.openxmlformats.schemas.drawingml.x2006.main.CTPositiveSize2D;
 import org.openxmlformats.schemas.drawingml.x2006.main.CTShapeProperties;
 import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTPicture;
@@ -156,7 +155,7 @@ public final class SXSSFPicture implements Picture {
         assert (w > scaledWidth);
         double cw = getColumnWidthInPixels(col2);
         double deltaW = w - scaledWidth;
-        int dx2 = (int)(XSSFShape.EMU_PER_PIXEL * (cw - deltaW));
+        int dx2 = (int)(Units.EMU_PER_PIXEL * (cw - deltaW));
 
         anchor.setCol2(col2);
         anchor.setDx2(dx2);
@@ -171,13 +170,13 @@ public final class SXSSFPicture implements Picture {
         assert (h > scaledHeight);
         double ch = getRowHeightInPixels(row2);
         double deltaH = h - scaledHeight;
-        int dy2 = (int)(XSSFShape.EMU_PER_PIXEL * (ch - deltaH));
+        int dy2 = (int)(Units.EMU_PER_PIXEL * (ch - deltaH));
         anchor.setRow2(row2);
         anchor.setDy2(dy2);
 
         CTPositiveSize2D size2d =  getCTPicture().getSpPr().getXfrm().getExt();
-        size2d.setCx((long)(scaledWidth * XSSFShape.EMU_PER_PIXEL));
-        size2d.setCy((long)(scaledHeight * XSSFShape.EMU_PER_PIXEL));
+        size2d.setCx((long)(scaledWidth * Units.EMU_PER_PIXEL));
+        size2d.setCy((long)(scaledHeight * Units.EMU_PER_PIXEL));
 
         return anchor;
     }
@@ -188,7 +187,7 @@ public final class SXSSFPicture implements Picture {
         CTCol col = sheet.getColumnHelper().getColumn(columnIndex, false);
         double numChars = col == null || !col.isSetWidth() ? DEFAULT_COLUMN_WIDTH : col.getWidth();
 
-        return (float)numChars*XSSFWorkbook.DEFAULT_CHARACTER_WIDTH;
+        return (float)numChars*Units.DEFAULT_CHARACTER_WIDTH;
     }
 
     private float getRowHeightInPixels(int rowIndex) {
@@ -198,7 +197,7 @@ public final class SXSSFPicture implements Picture {
         SXSSFSheet sheet = _wb.getSXSSFSheet(xssfSheet);
         Row row = sheet.getRow(rowIndex);
         float height = row != null ?  row.getHeightInPoints() : sheet.getDefaultRowHeightInPoints();
-        return height * XSSFShape.PIXEL_DPI / XSSFShape.POINT_DPI;
+        return height * Units.PIXEL_DPI / Units.POINT_DPI;
     }
     /**
      * Return the dimension of this image
index 0fa3f96ff01402680374352ee9f4592de10171ca..bc4743b9c69453da7199bb830b0dd7871dd0a8d3 100644 (file)
@@ -21,7 +21,7 @@ import org.apache.poi.ss.usermodel.ClientAnchor;
 import org.apache.poi.ss.usermodel.Row;
 import org.apache.poi.util.Internal;
 import org.apache.poi.util.Removal;
-import org.apache.poi.xssf.util.EMUUtils;
+import org.apache.poi.util.Units;
 import org.openxmlformats.schemas.drawingml.x2006.main.CTPoint2D;
 import org.openxmlformats.schemas.drawingml.x2006.main.CTPositiveSize2D;
 import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTMarker;
@@ -167,27 +167,27 @@ public class XSSFClientAnchor extends XSSFAnchor implements ClientAnchor {
         int r = cell.getRow();
         int c = cell.getCol();
         
-        int cw = EMUUtils.EMUsFromColumnWidth(sheet.getColumnWidth(c));
+        int cw = Units.columnWidthToEMU(sheet.getColumnWidth(c));
         
         // start with width - offset, then keep adding column widths until the next one puts us over w
         long wPos = cw - cell.getColOff();
         
         while (wPos < w) {
             c++;
-            cw = EMUUtils.EMUsFromColumnWidth(sheet.getColumnWidth(c));
+            cw = Units.columnWidthToEMU(sheet.getColumnWidth(c));
             wPos += cw;
         }
         // now wPos >= w, so end column = c, now figure offset
         c2.setCol(c);
         c2.setColOff(cw - (wPos - w));
         
-        int rh = EMUUtils.EMUsFromPoints(getRowHeight(sheet, r));
+        int rh = Units.toEMU(getRowHeight(sheet, r));
         // start with height - offset, then keep adding row heights until the next one puts us over h
         long hPos = rh - cell.getRowOff();
         
         while (hPos < h) {
             r++;
-            rh = EMUUtils.EMUsFromPoints(getRowHeight(sheet, r));
+            rh = Units.toEMU(getRowHeight(sheet, r));
             hPos += rh;
         }
         // now hPos >= h, so end row = r, now figure offset
index 886d2a923bb9b5b965149084029d8ba18a480878..9ecff2670dd1d1fcbbaac76c1580b26e41a73d73 100644 (file)
@@ -18,6 +18,8 @@
 package org.apache.poi.xssf.usermodel;
 
 import org.apache.poi.ss.usermodel.Shape;
+import org.apache.poi.util.Removal;
+import org.apache.poi.util.Units;
 import org.openxmlformats.schemas.drawingml.x2006.main.CTLineProperties;
 import org.openxmlformats.schemas.drawingml.x2006.main.CTNoFillProperties;
 import org.openxmlformats.schemas.drawingml.x2006.main.CTPresetLineDashProperties;
@@ -30,11 +32,33 @@ import org.openxmlformats.schemas.drawingml.x2006.main.STPresetLineDashVal;
  * Represents a shape in a SpreadsheetML drawing.
  */
 public abstract class XSSFShape implements Shape {
-    public static final int EMU_PER_PIXEL = 9525;
-    public static final int EMU_PER_POINT = 12700;
+    /**
+     * @deprecated POI 3.17 beta 1
+     * @see Units#EMU_PER_PIXEL
+     */
+    @Removal(version="3.19")
+    public static final int EMU_PER_PIXEL = Units.EMU_PER_PIXEL;
+    
+    /**
+     * @deprecated POI 3.17 beta 1
+     * @see Units#EMU_PER_POINT
+     */
+    @Removal(version="3.19")
+    public static final int EMU_PER_POINT = Units.EMU_PER_POINT;
 
-    public static final int POINT_DPI = 72;
-    public static final int PIXEL_DPI = 96;
+    /**
+     * @deprecated POI 3.17 beta 1
+     * @see Units#POINT_DPI
+     */
+    @Removal(version="3.19")
+    public static final int POINT_DPI = Units.POINT_DPI;
+
+    /**
+     * @deprecated POI 3.17 beta 1
+     * @see Units#PIXEL_DPI
+     */
+    @Removal(version="3.19")
+    public static final int PIXEL_DPI = Units.PIXEL_DPI;
 
     /**
      * Parent drawing
@@ -124,7 +148,7 @@ public abstract class XSSFShape implements Shape {
     public void setLineWidth( double lineWidth ) {
         CTShapeProperties props = getShapeProperties();
         CTLineProperties ln = props.isSetLn() ? props.getLn() : props.addNewLn();
-        ln.setW((int)(lineWidth*EMU_PER_POINT));
+        ln.setW((int)(lineWidth*Units.EMU_PER_POINT));
     }
 
     /**
index a67a16ad19ddc2361ec8625d96ab1bc4ba1755a2..283a27137ee55337efdf408706a67e5ceed6311a 100644 (file)
@@ -83,6 +83,7 @@ import org.apache.poi.util.Internal;
 import org.apache.poi.util.POILogFactory;
 import org.apache.poi.util.POILogger;
 import org.apache.poi.util.Removal;
+import org.apache.poi.util.Units;
 import org.apache.poi.xssf.model.CommentsTable;
 import org.apache.poi.xssf.usermodel.XSSFPivotTable.PivotTableReferenceConfigurator;
 import org.apache.poi.xssf.usermodel.helpers.ColumnHelper;
@@ -916,7 +917,7 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet  {
     @Override
     public float getColumnWidthInPixels(int columnIndex) {
         float widthIn256 = getColumnWidth(columnIndex);
-        return (float)(widthIn256/256.0*XSSFWorkbook.DEFAULT_CHARACTER_WIDTH);
+        return (float)(widthIn256/256.0*Units.DEFAULT_CHARACTER_WIDTH);
     }
     
     /**
index e989cdf8666e7273a443d8e74b773238a87918cd..d13f71acdab9313418a71501bb7ecb85842b677f 100644 (file)
@@ -83,6 +83,7 @@ import org.apache.poi.util.POILogFactory;
 import org.apache.poi.util.POILogger;
 import org.apache.poi.util.PackageHelper;
 import org.apache.poi.util.Removal;
+import org.apache.poi.util.Units;
 import org.apache.poi.xssf.XLSBUnsupportedException;
 import org.apache.poi.xssf.model.CalculationChain;
 import org.apache.poi.xssf.model.ExternalLinksTable;
@@ -123,8 +124,11 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook {
 
     /**
      * Width of one character of the default font in pixels. Same for Calibry and Arial.
+     * @deprecated POI 3.17 beta 1
+     * @see Units#DEFAULT_CHARACTER_WIDTH
      */
-    public static final float DEFAULT_CHARACTER_WIDTH = 7.0017f;
+    @Removal(version="3.19")
+    public static final float DEFAULT_CHARACTER_WIDTH = Units.DEFAULT_CHARACTER_WIDTH;
 
     /**
      * Excel silently truncates long sheet names to 31 chars.
diff --git a/src/ooxml/java/org/apache/poi/xssf/util/EMUUtils.java b/src/ooxml/java/org/apache/poi/xssf/util/EMUUtils.java
deleted file mode 100644 (file)
index a7d2562..0000000
+++ /dev/null
@@ -1,66 +0,0 @@
-/* ====================================================================
-   Licensed to the Apache Software Foundation (ASF) under one or more
-   contributor license agreements.  See the NOTICE file distributed with
-   this work for additional information regarding copyright ownership.
-   The ASF licenses this file to You under the Apache License, Version 2.0
-   (the "License"); you may not use this file except in compliance with
-   the License.  You may obtain a copy of the License at
-
-       http://www.apache.org/licenses/LICENSE-2.0
-
-   Unless required by applicable law or agreed to in writing, software
-   distributed under the License is distributed on an "AS IS" BASIS,
-   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-   See the License for the specific language governing permissions and
-   limitations under the License.
-==================================================================== */
-package org.apache.poi.xssf.util;
-
-import org.apache.poi.xssf.usermodel.XSSFShape;
-import org.apache.poi.xssf.usermodel.XSSFWorkbook;
-
-/**
- * Conversion methods for OOXML EMU values - "English Metric Units" or more accurately
- * "Evil Measurement Units".
- * <p/>
- * These are defined briefly in <a href="https://en.wikipedia.org/wiki/Office_Open_XML_file_formats#DrawingML">Wikipedia</a>
- * as a "rational" way to use an integer value to represent something that could be in 
- * inches, centimeters, points, or pixels.
- * So now we get to convert between all those.
- */
-public class EMUUtils {
-    public static final int EMUS_PER_INCH = 914400;
-    public static final int EMUS_PER_POINT = 12700;
-    public static final int EMUS_PER_CENTIMETER = 360000;
-    
-    // TODO: these could move here or something to standardize definitions
-    public static final int EMU_PER_PIXEL = XSSFShape.EMU_PER_PIXEL;
-    public static final int EMU_PER_POINT = XSSFShape.EMU_PER_POINT;
-    public static final int PIXEL_DPI = XSSFShape.PIXEL_DPI;
-    public static final int POINT_DPI = XSSFShape.POINT_DPI;
-    public static final int EMU_PER_CHARACTER = (int) (EMU_PER_PIXEL * XSSFWorkbook.DEFAULT_CHARACTER_WIDTH);
-    
-    /**
-     * @param columnWidth as (fractional # of characters) * 256
-     * @return EMUs
-     */
-    public static final int EMUsFromColumnWidth(int columnWidth) {
-        return (int) (columnWidth /256d * EMUUtils.EMU_PER_CHARACTER);
-    }
-    
-    /**
-     * @param twips (1/20th of a point) typically a row height
-     * @return EMUs
-     */
-    public static final int EMUsFromTwips(short twips) {
-        return (int) (twips / 20d * EMU_PER_POINT);
-    }
-    
-    /**
-     * @param points (fractional)
-     * @return EMUs
-     */
-    public static final int EMUsFromPoints(float points) {
-        return (int) (points * EMU_PER_POINT);
-    }
-}