Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: Auto sizing not working properly due to rounding error when calculating defaultCharWidth #541

Open
wants to merge 1 commit into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more
*/
@Internal
/*package*/ class AutoSizeColumnTracker {
private final int defaultCharWidth;
private final double defaultCharWidth;
private final DataFormatter dataFormatter = new DataFormatter();

// map of tracked columns, with values containing the best-fit width for the column
Expand Down
14 changes: 7 additions & 7 deletions poi/src/main/java/org/apache/poi/ss/util/SheetUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public void evaluateAll() {}
* @param useMergedCells whether to use merged cells
* @return the width in pixels or -1 if cell is empty
*/
public static double getCellWidth(Cell cell, int defaultCharWidth, DataFormatter formatter, boolean useMergedCells) {
public static double getCellWidth(Cell cell, double defaultCharWidth, DataFormatter formatter, boolean useMergedCells) {
List<CellRangeAddress> mergedRegions = cell.getSheet().getMergedRegions();
return getCellWidth(cell, defaultCharWidth, formatter, useMergedCells, mergedRegions);
}
Expand All @@ -137,7 +137,7 @@ public static double getCellWidth(Cell cell, int defaultCharWidth, DataFormatter
* @param mergedRegions The list of merged regions as received via cell.getSheet().getMergedRegions()
* @return the width in pixels or -1 if cell is empty
*/
public static double getCellWidth(Cell cell, int defaultCharWidth, DataFormatter formatter, boolean useMergedCells,
public static double getCellWidth(Cell cell, double defaultCharWidth, DataFormatter formatter, boolean useMergedCells,
List<CellRangeAddress> mergedRegions) {
Sheet sheet = cell.getSheet();
Workbook wb = sheet.getWorkbook();
Expand Down Expand Up @@ -219,7 +219,7 @@ public static double getCellWidth(Cell cell, int defaultCharWidth, DataFormatter
* @param str the text contained in the cell
* @return the best fit cell width
*/
private static double getCellWidth(int defaultCharWidth, int colspan,
private static double getCellWidth(double defaultCharWidth, int colspan,
CellStyle style, double minWidth, AttributedString str) {
TextLayout layout = new TextLayout(str.getIterator(), fontRenderContext);
final Rectangle2D bounds;
Expand Down Expand Up @@ -270,7 +270,7 @@ public static double getColumnWidth(Sheet sheet, int column, boolean useMergedCe
*/
public static double getColumnWidth(Sheet sheet, int column, boolean useMergedCells, int firstRow, int lastRow){
DataFormatter formatter = new DataFormatter();
int defaultCharWidth = getDefaultCharWidth(sheet.getWorkbook());
double defaultCharWidth = getDefaultCharWidth(sheet.getWorkbook());

List<CellRangeAddress> mergedRegions = sheet.getMergedRegions();
double width = -1;
Expand All @@ -292,14 +292,14 @@ public static double getColumnWidth(Sheet sheet, int column, boolean useMergedCe
* @return default character width in pixels
*/
@Internal
public static int getDefaultCharWidth(final Workbook wb) {
public static double getDefaultCharWidth(final Workbook wb) {
Font defaultFont = wb.getFontAt( 0);

AttributedString str = new AttributedString(String.valueOf(defaultChar));
copyAttributes(defaultFont, str, 0, 1);
try {
TextLayout layout = new TextLayout(str.getIterator(), fontRenderContext);
return Math.round(layout.getAdvance());
return layout.getAdvance();
} catch (UnsatisfiedLinkError | NoClassDefFoundError | InternalError e) {
if (ignoreMissingFontSystem) {
return DEFAULT_CHAR_WIDTH;
Expand All @@ -321,7 +321,7 @@ public static int getDefaultCharWidth(final Workbook wb) {
* @return the width in pixels or -1 if cell is empty
*/
private static double getColumnWidthForRow(
Row row, int column, int defaultCharWidth, DataFormatter formatter, boolean useMergedCells,
Row row, int column, double defaultCharWidth, DataFormatter formatter, boolean useMergedCells,
List<CellRangeAddress> mergedRegions) {
if( row == null ) {
return -1;
Expand Down