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
This commit is contained in:
parent
12048f528b
commit
0e23e4e9d8
@ -45,6 +45,25 @@ public class Units {
|
|||||||
*/
|
*/
|
||||||
public static final int POINT_DPI = 72;
|
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
|
* Converts points to EMUs
|
||||||
* @param points points
|
* @param points points
|
||||||
@ -127,4 +146,24 @@ public class Units {
|
|||||||
points /= PIXEL_DPI;
|
points /= PIXEL_DPI;
|
||||||
return points;
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -29,14 +29,13 @@ import org.apache.poi.ss.util.ImageUtils;
|
|||||||
import org.apache.poi.util.Internal;
|
import org.apache.poi.util.Internal;
|
||||||
import org.apache.poi.util.POILogFactory;
|
import org.apache.poi.util.POILogFactory;
|
||||||
import org.apache.poi.util.POILogger;
|
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.XSSFAnchor;
|
||||||
import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
|
import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
|
||||||
import org.apache.poi.xssf.usermodel.XSSFDrawing;
|
import org.apache.poi.xssf.usermodel.XSSFDrawing;
|
||||||
import org.apache.poi.xssf.usermodel.XSSFPicture;
|
import org.apache.poi.xssf.usermodel.XSSFPicture;
|
||||||
import org.apache.poi.xssf.usermodel.XSSFPictureData;
|
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.XSSFSheet;
|
||||||
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
|
||||||
import org.openxmlformats.schemas.drawingml.x2006.main.CTPositiveSize2D;
|
import org.openxmlformats.schemas.drawingml.x2006.main.CTPositiveSize2D;
|
||||||
import org.openxmlformats.schemas.drawingml.x2006.main.CTShapeProperties;
|
import org.openxmlformats.schemas.drawingml.x2006.main.CTShapeProperties;
|
||||||
import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTPicture;
|
import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTPicture;
|
||||||
@ -156,7 +155,7 @@ public final class SXSSFPicture implements Picture {
|
|||||||
assert (w > scaledWidth);
|
assert (w > scaledWidth);
|
||||||
double cw = getColumnWidthInPixels(col2);
|
double cw = getColumnWidthInPixels(col2);
|
||||||
double deltaW = w - scaledWidth;
|
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.setCol2(col2);
|
||||||
anchor.setDx2(dx2);
|
anchor.setDx2(dx2);
|
||||||
@ -171,13 +170,13 @@ public final class SXSSFPicture implements Picture {
|
|||||||
assert (h > scaledHeight);
|
assert (h > scaledHeight);
|
||||||
double ch = getRowHeightInPixels(row2);
|
double ch = getRowHeightInPixels(row2);
|
||||||
double deltaH = h - scaledHeight;
|
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.setRow2(row2);
|
||||||
anchor.setDy2(dy2);
|
anchor.setDy2(dy2);
|
||||||
|
|
||||||
CTPositiveSize2D size2d = getCTPicture().getSpPr().getXfrm().getExt();
|
CTPositiveSize2D size2d = getCTPicture().getSpPr().getXfrm().getExt();
|
||||||
size2d.setCx((long)(scaledWidth * XSSFShape.EMU_PER_PIXEL));
|
size2d.setCx((long)(scaledWidth * Units.EMU_PER_PIXEL));
|
||||||
size2d.setCy((long)(scaledHeight * XSSFShape.EMU_PER_PIXEL));
|
size2d.setCy((long)(scaledHeight * Units.EMU_PER_PIXEL));
|
||||||
|
|
||||||
return anchor;
|
return anchor;
|
||||||
}
|
}
|
||||||
@ -188,7 +187,7 @@ public final class SXSSFPicture implements Picture {
|
|||||||
CTCol col = sheet.getColumnHelper().getColumn(columnIndex, false);
|
CTCol col = sheet.getColumnHelper().getColumn(columnIndex, false);
|
||||||
double numChars = col == null || !col.isSetWidth() ? DEFAULT_COLUMN_WIDTH : col.getWidth();
|
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) {
|
private float getRowHeightInPixels(int rowIndex) {
|
||||||
@ -198,7 +197,7 @@ public final class SXSSFPicture implements Picture {
|
|||||||
SXSSFSheet sheet = _wb.getSXSSFSheet(xssfSheet);
|
SXSSFSheet sheet = _wb.getSXSSFSheet(xssfSheet);
|
||||||
Row row = sheet.getRow(rowIndex);
|
Row row = sheet.getRow(rowIndex);
|
||||||
float height = row != null ? row.getHeightInPoints() : sheet.getDefaultRowHeightInPoints();
|
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
|
* Return the dimension of this image
|
||||||
|
@ -21,7 +21,7 @@ import org.apache.poi.ss.usermodel.ClientAnchor;
|
|||||||
import org.apache.poi.ss.usermodel.Row;
|
import org.apache.poi.ss.usermodel.Row;
|
||||||
import org.apache.poi.util.Internal;
|
import org.apache.poi.util.Internal;
|
||||||
import org.apache.poi.util.Removal;
|
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.CTPoint2D;
|
||||||
import org.openxmlformats.schemas.drawingml.x2006.main.CTPositiveSize2D;
|
import org.openxmlformats.schemas.drawingml.x2006.main.CTPositiveSize2D;
|
||||||
import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTMarker;
|
import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTMarker;
|
||||||
@ -167,27 +167,27 @@ public class XSSFClientAnchor extends XSSFAnchor implements ClientAnchor {
|
|||||||
int r = cell.getRow();
|
int r = cell.getRow();
|
||||||
int c = cell.getCol();
|
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
|
// start with width - offset, then keep adding column widths until the next one puts us over w
|
||||||
long wPos = cw - cell.getColOff();
|
long wPos = cw - cell.getColOff();
|
||||||
|
|
||||||
while (wPos < w) {
|
while (wPos < w) {
|
||||||
c++;
|
c++;
|
||||||
cw = EMUUtils.EMUsFromColumnWidth(sheet.getColumnWidth(c));
|
cw = Units.columnWidthToEMU(sheet.getColumnWidth(c));
|
||||||
wPos += cw;
|
wPos += cw;
|
||||||
}
|
}
|
||||||
// now wPos >= w, so end column = c, now figure offset
|
// now wPos >= w, so end column = c, now figure offset
|
||||||
c2.setCol(c);
|
c2.setCol(c);
|
||||||
c2.setColOff(cw - (wPos - w));
|
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
|
// start with height - offset, then keep adding row heights until the next one puts us over h
|
||||||
long hPos = rh - cell.getRowOff();
|
long hPos = rh - cell.getRowOff();
|
||||||
|
|
||||||
while (hPos < h) {
|
while (hPos < h) {
|
||||||
r++;
|
r++;
|
||||||
rh = EMUUtils.EMUsFromPoints(getRowHeight(sheet, r));
|
rh = Units.toEMU(getRowHeight(sheet, r));
|
||||||
hPos += rh;
|
hPos += rh;
|
||||||
}
|
}
|
||||||
// now hPos >= h, so end row = r, now figure offset
|
// now hPos >= h, so end row = r, now figure offset
|
||||||
|
@ -18,6 +18,8 @@
|
|||||||
package org.apache.poi.xssf.usermodel;
|
package org.apache.poi.xssf.usermodel;
|
||||||
|
|
||||||
import org.apache.poi.ss.usermodel.Shape;
|
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.CTLineProperties;
|
||||||
import org.openxmlformats.schemas.drawingml.x2006.main.CTNoFillProperties;
|
import org.openxmlformats.schemas.drawingml.x2006.main.CTNoFillProperties;
|
||||||
import org.openxmlformats.schemas.drawingml.x2006.main.CTPresetLineDashProperties;
|
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.
|
* Represents a shape in a SpreadsheetML drawing.
|
||||||
*/
|
*/
|
||||||
public abstract class XSSFShape implements Shape {
|
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
|
* Parent drawing
|
||||||
@ -124,7 +148,7 @@ public abstract class XSSFShape implements Shape {
|
|||||||
public void setLineWidth( double lineWidth ) {
|
public void setLineWidth( double lineWidth ) {
|
||||||
CTShapeProperties props = getShapeProperties();
|
CTShapeProperties props = getShapeProperties();
|
||||||
CTLineProperties ln = props.isSetLn() ? props.getLn() : props.addNewLn();
|
CTLineProperties ln = props.isSetLn() ? props.getLn() : props.addNewLn();
|
||||||
ln.setW((int)(lineWidth*EMU_PER_POINT));
|
ln.setW((int)(lineWidth*Units.EMU_PER_POINT));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -83,6 +83,7 @@ import org.apache.poi.util.Internal;
|
|||||||
import org.apache.poi.util.POILogFactory;
|
import org.apache.poi.util.POILogFactory;
|
||||||
import org.apache.poi.util.POILogger;
|
import org.apache.poi.util.POILogger;
|
||||||
import org.apache.poi.util.Removal;
|
import org.apache.poi.util.Removal;
|
||||||
|
import org.apache.poi.util.Units;
|
||||||
import org.apache.poi.xssf.model.CommentsTable;
|
import org.apache.poi.xssf.model.CommentsTable;
|
||||||
import org.apache.poi.xssf.usermodel.XSSFPivotTable.PivotTableReferenceConfigurator;
|
import org.apache.poi.xssf.usermodel.XSSFPivotTable.PivotTableReferenceConfigurator;
|
||||||
import org.apache.poi.xssf.usermodel.helpers.ColumnHelper;
|
import org.apache.poi.xssf.usermodel.helpers.ColumnHelper;
|
||||||
@ -916,7 +917,7 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet {
|
|||||||
@Override
|
@Override
|
||||||
public float getColumnWidthInPixels(int columnIndex) {
|
public float getColumnWidthInPixels(int columnIndex) {
|
||||||
float widthIn256 = getColumnWidth(columnIndex);
|
float widthIn256 = getColumnWidth(columnIndex);
|
||||||
return (float)(widthIn256/256.0*XSSFWorkbook.DEFAULT_CHARACTER_WIDTH);
|
return (float)(widthIn256/256.0*Units.DEFAULT_CHARACTER_WIDTH);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -83,6 +83,7 @@ import org.apache.poi.util.POILogFactory;
|
|||||||
import org.apache.poi.util.POILogger;
|
import org.apache.poi.util.POILogger;
|
||||||
import org.apache.poi.util.PackageHelper;
|
import org.apache.poi.util.PackageHelper;
|
||||||
import org.apache.poi.util.Removal;
|
import org.apache.poi.util.Removal;
|
||||||
|
import org.apache.poi.util.Units;
|
||||||
import org.apache.poi.xssf.XLSBUnsupportedException;
|
import org.apache.poi.xssf.XLSBUnsupportedException;
|
||||||
import org.apache.poi.xssf.model.CalculationChain;
|
import org.apache.poi.xssf.model.CalculationChain;
|
||||||
import org.apache.poi.xssf.model.ExternalLinksTable;
|
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.
|
* 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.
|
* Excel silently truncates long sheet names to 31 chars.
|
||||||
|
@ -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);
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user