From: Greg Woolsey Date: Fri, 23 Jun 2017 15:43:48 +0000 (+0000) Subject: Move my new unit conversions to the Units class, move and deprecate duplicate and... X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=0e23e4e9d8ce9c51b081959aea7f1478aff69810;p=poi.git Move my new unit conversions to the Units class, move and deprecate duplicate and scattered unit constants, update constant references to standardize on the ones in the Units class. git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1799683 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/src/java/org/apache/poi/util/Units.java b/src/java/org/apache/poi/util/Units.java index 1d376e936d..e969271da2 100644 --- a/src/java/org/apache/poi/util/Units.java +++ b/src/java/org/apache/poi/util/Units.java @@ -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. + *

+ * Note this is only valid for workbooks using the default Excel font. + *

+ * 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); + } } diff --git a/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFPicture.java b/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFPicture.java index 9635882973..04475e0bc8 100644 --- a/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFPicture.java +++ b/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFPicture.java @@ -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 diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFClientAnchor.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFClientAnchor.java index 0fa3f96ff0..bc4743b9c6 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFClientAnchor.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFClientAnchor.java @@ -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 diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFShape.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFShape.java index 886d2a923b..9ecff2670d 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFShape.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFShape.java @@ -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)); } /** diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java index a67a16ad19..283a27137e 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java @@ -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); } /** diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java index e989cdf866..d13f71acda 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java @@ -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 index a7d2562b37..0000000000 --- a/src/ooxml/java/org/apache/poi/xssf/util/EMUUtils.java +++ /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". - *

- * These are defined briefly in Wikipedia - * 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); - } -}