Skip to content

Commit 14252d5

Browse files
authored
Merge pull request #1130 from Bykiev/FixAutoSizeColumns
Fix auto size columns
2 parents 0064849 + 5845e63 commit 14252d5

File tree

6 files changed

+38
-39
lines changed

6 files changed

+38
-39
lines changed

main/HSSF/UserModel/EscherGraphics.cs

+5-1
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ public class EscherGraphics : IDisposable
7070
private Font font;
7171
private static POILogger Logger = POILogFactory.GetLogger(typeof(EscherGraphics));
7272

73+
// Default dpi
74+
private static int dpi = 96;
75+
7376
/**
7477
* Construct an escher graphics object.
7578
*
@@ -289,7 +292,8 @@ public void DrawString(String str, int x, int y)
289292
Font excelFont = new Font(SystemFonts.Get(font.Name.Equals("SansSerif") ? "Arial" : font.Name),
290293
(int)(font.Size / verticalPixelsPerPoint), font.FontMetrics.Description.Style);
291294
{
292-
int width = (int)((TextMeasurer.Measure(str, new TextOptions(excelFont)).Width * 8) + 12);
295+
var textOptions = new TextOptions(excelFont) { Dpi = dpi };
296+
int width = (int)((TextMeasurer.MeasureSize(str, textOptions).Width * 8) + 12);
293297
int height = (int)((font.Size / verticalPixelsPerPoint) + 6) * 2;
294298
y -= Convert.ToInt32((font.Size / verticalPixelsPerPoint) + 2 * verticalPixelsPerPoint); // we want to Draw the shape from the top-left
295299
HSSFTextbox textbox = escherGroup.CreateTextbox(new HSSFChildAnchor(x, y, x + width, y + height));

main/NPOI.Core.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<PackageReference Include="Microsoft.IO.RecyclableMemoryStream" Version="2.3.2" />
2020
<PackageReference Include="BouncyCastle.Cryptography" Version="2.2.1" />
2121
<PackageReference Include="SharpZipLib" Version="1.3.3" />
22-
<PackageReference Include="SixLabors.Fonts" Version="1.0.0-beta19" />
22+
<PackageReference Include="SixLabors.Fonts" Version="1.0.0" />
2323
<PackageReference Include="SixLabors.ImageSharp" Version="2.1.4" />
2424
</ItemGroup>
2525

main/SS/Util/SheetUtil.cs

+20-27
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ public class SheetUtil
4040
// */
4141
private static char defaultChar = '0';
4242

43+
// Default dpi
44+
private static int dpi = 96;
45+
4346
// /**
4447
// * This is the multiple that the font height is scaled by when determining the
4548
// * boundary of rotated text.
@@ -269,19 +272,19 @@ public static IRow CopyRow(ISheet sheet, int sourceRowIndex, int targetRowIndex)
269272

270273
public static double GetRowHeight(IRow row, bool useMergedCells, int firstColumnIdx, int lastColumnIdx)
271274
{
272-
double width = -1;
275+
double height = -1;
273276

274277
for (int cellIdx = firstColumnIdx; cellIdx <= lastColumnIdx; ++cellIdx)
275278
{
276279
ICell cell = row.GetCell(cellIdx);
277280
if (row != null && cell != null)
278281
{
279-
double cellWidth = GetCellHeight(cell, useMergedCells);
280-
width = Math.Max(width, cellWidth);
282+
double cellHeight = GetCellHeight(cell, useMergedCells);
283+
height = Math.Max(height, cellHeight);
281284
}
282285
}
283286

284-
return width;
287+
return height;
285288
}
286289

287290
public static double GetRowHeight(ISheet sheet, int rowIdx, bool useMergedCells, int firstColumnIdx, int lastColumnIdx)
@@ -366,8 +369,7 @@ private static int GetNumberOfRowsInMergedRegion(ICell cell)
366369

367370
private static double GetCellConetntHeight(double actualHeight, int numberOfRowsInMergedRegion)
368371
{
369-
var correction = 1.1;
370-
return Math.Max(-1, actualHeight / numberOfRowsInMergedRegion * correction);
372+
return Math.Max(-1, actualHeight / numberOfRowsInMergedRegion);
371373
}
372374

373375
private static string GetCellStringValue(ICell cell)
@@ -416,7 +418,7 @@ private static Font GetWindowsFont(ICell cell)
416418
private static double GetRotatedContentHeight(ICell cell, string stringValue, Font windowsFont)
417419
{
418420
var angle = cell.CellStyle.Rotation * 2.0 * Math.PI / 360.0;
419-
var measureResult = TextMeasurer.Measure(stringValue, new TextOptions(windowsFont));
421+
var measureResult = TextMeasurer.MeasureAdvance(stringValue, new TextOptions(windowsFont) { Dpi = dpi });
420422

421423
var x1 = Math.Abs(measureResult.Height * Math.Cos(angle));
422424
var x2 = Math.Abs(measureResult.Width * Math.Sin(angle));
@@ -426,7 +428,7 @@ private static double GetRotatedContentHeight(ICell cell, string stringValue, Fo
426428

427429
private static double GetContentHeight(string stringValue, Font windowsFont)
428430
{
429-
var measureResult = TextMeasurer.Measure(stringValue, new TextOptions(windowsFont));
431+
var measureResult = TextMeasurer.MeasureAdvance(stringValue, new TextOptions(windowsFont) { Dpi = dpi });
430432

431433
return Math.Round(measureResult.Height, 0, MidpointRounding.ToEven);
432434
}
@@ -480,7 +482,7 @@ public static double GetCellWidth(ICell cell, int defaultCharWidth, DataFormatte
480482
String[] lines = rt.String.Split("\n".ToCharArray());
481483
for (int i = 0; i < lines.Length; i++)
482484
{
483-
String txt = lines[i] + defaultChar;
485+
String txt = lines[i];
484486

485487
//AttributedString str = new AttributedString(txt);
486488
//copyAttributes(font, str, 0, txt.length());
@@ -514,7 +516,7 @@ public static double GetCellWidth(ICell cell, int defaultCharWidth, DataFormatte
514516
}
515517
if (sval != null)
516518
{
517-
String txt = sval + defaultChar;
519+
String txt = sval;
518520
//str = new AttributedString(txt);
519521
//copyAttributes(font, str, 0, txt.length());
520522
windowsFont = IFont2Font(font);
@@ -530,25 +532,20 @@ private static double GetCellWidth(int defaultCharWidth, int colspan,
530532
{
531533
//Rectangle bounds;
532534
double actualWidth;
533-
FontRectangle sf = TextMeasurer.Measure(str, new TextOptions(windowsFont));
535+
FontRectangle sf = TextMeasurer.MeasureSize(str, new TextOptions(windowsFont) { Dpi = dpi });
534536
if (style.Rotation != 0)
535537
{
536538
double angle = style.Rotation * 2.0 * Math.PI / 360.0;
537539
double x1 = Math.Abs(sf.Height * Math.Sin(angle));
538540
double x2 = Math.Abs(sf.Width * Math.Cos(angle));
539541
actualWidth = Math.Round(x1 + x2, 0, MidpointRounding.ToEven);
540-
//bounds = layout.getOutline(trans).getBounds();
541542
}
542543
else
543-
{
544-
//bounds = layout.getBounds();
545-
actualWidth = Math.Round(sf.Width, 0, MidpointRounding.ToEven);
546-
}
547-
// entireWidth accounts for leading spaces which is excluded from bounds.getWidth()
548-
//double frameWidth = bounds.getX() + bounds.getWidth();
549-
//width = Math.max(width, ((frameWidth / colspan) / defaultCharWidth) + style.getIndention());
550-
var correction = 1.1;
551-
width = Math.Max(width, (actualWidth / colspan / defaultCharWidth * correction) + cell.CellStyle.Indention);
544+
actualWidth = Math.Round(sf.Width, 0, MidpointRounding.ToEven);
545+
546+
int padding = 5;
547+
double correction = 1.05;
548+
width = Math.Max(width, ((actualWidth + padding) / colspan / defaultCharWidth * correction) + cell.CellStyle.Indention);
552549
return width;
553550
}
554551

@@ -610,13 +607,9 @@ public static double GetColumnWidth(ISheet sheet, int column, bool useMergedCell
610607
public static int GetDefaultCharWidth(IWorkbook wb)
611608
{
612609
IFont defaultFont = wb.GetFontAt((short)0);
613-
614-
//AttributedString str = new AttributedString(String.valueOf(defaultChar));
615-
//copyAttributes(defaultFont, str, 0, 1);
616-
//TextLayout layout = new TextLayout(str.getIterator(), fontRenderContext);
617-
//int defaultCharWidth = (int)layout.getAdvance();
618610
Font font = IFont2Font(defaultFont);
619-
return (int)Math.Ceiling(TextMeasurer.Measure(new string(defaultChar, 1), new TextOptions(font)).Width);
611+
612+
return (int)Math.Ceiling(TextMeasurer.MeasureSize(new string(defaultChar, 1), new TextOptions(font) { Dpi = dpi }).Width);
620613
}
621614

622615
/**

testcases/main/HSSF/UserModel/TestHSSFSheet.cs

+1-4
Original file line numberDiff line numberDiff line change
@@ -854,10 +854,7 @@ public void TestAutoSizeRow()
854854
sheet.AutoSizeRow(row.RowNum);
855855

856856
Assert.AreNotEqual(100, row.Height);
857-
858-
// there's slight difference due to fonts
859-
var expectedRowHeight = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? 506 : 528;
860-
Assert.AreEqual(expectedRowHeight, row.Height);
857+
Assert.AreEqual(540, row.Height);
861858

862859
workbook.Close();
863860
}

testcases/main/SS/UserModel/BaseTestBugzillaIssues.cs

+10-5
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public abstract class BaseTestBugzillaIssues
3838
{
3939

4040
private ITestDataProvider _testDataProvider;
41+
private static int dpi = 96;
4142

4243
protected BaseTestBugzillaIssues(ITestDataProvider TestDataProvider)
4344
{
@@ -445,12 +446,14 @@ public void Bug51622_testAutoSizeShouldRecognizeLeadingSpaces()
445446
// if the default font or margins change.
446447
double expectedRatioThreshold = 1.2f;
447448
double leadingWhitespaceRatio = ((double)leadingWhitespaceColWidth) / noWhitespaceColWidth;
448-
double trailingWhitespaceRatio = ((double)leadingWhitespaceColWidth) / noWhitespaceColWidth;
449+
double trailingWhitespaceRatio = ((double)trailingWhitespaceColWidth) / noWhitespaceColWidth;
449450

450451
assertGreaterThan("leading whitespace is longer than no whitespace", leadingWhitespaceRatio, expectedRatioThreshold);
451452
assertGreaterThan("trailing whitespace is longer than no whitespace", trailingWhitespaceRatio, expectedRatioThreshold);
452-
Assert.AreEqual(leadingWhitespaceColWidth, trailingWhitespaceColWidth,
453-
"cells with equal leading and trailing whitespace have equal width");
453+
454+
//This is not correct https://github.com/SixLabors/Fonts/discussions/349
455+
//Assert.AreEqual(leadingWhitespaceColWidth, trailingWhitespaceColWidth,
456+
//"cells with equal leading and trailing whitespace have equal width");
454457

455458
wb.Close();
456459
}
@@ -487,15 +490,17 @@ private double ComputeCellWidthManually(ICell cell0, IFont font)
487490
//TextLayout layout = new TextLayout(str.getIterator(), fontRenderContext);
488491
//width = ((layout.getBounds().getWidth() / 1) / 8);
489492
Font wfont = SheetUtil.IFont2Font(font);
490-
width = (double)TextMeasurer.Measure(txt, new TextOptions(wfont)).Width;
493+
var textOptions = new TextOptions(wfont) { Dpi = dpi };
494+
width = (double)TextMeasurer.MeasureSize(txt, textOptions).Width;
491495
return width;
492496
}
493497

494498
private double ComputeCellWidthFixed(IFont font, String txt)
495499
{
496500
double width;
497501
Font wfont = SheetUtil.IFont2Font(font);
498-
width = (double)TextMeasurer.Measure(txt, new TextOptions(wfont)).Width;
502+
var textOptions = new TextOptions(wfont) { Dpi = dpi };
503+
width = (double)TextMeasurer.MeasureSize(txt, textOptions).Width;
499504
return width;
500505
}
501506

testcases/ooxml/XSSF/UserModel/TestXSSFSheet.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ public void TestAutoSizeRow()
196196
sheet.AutoSizeRow(row.RowNum);
197197

198198
Assert.AreNotEqual(100, row.Height);
199-
Assert.AreEqual(550, row.Height);
199+
Assert.AreEqual(540, row.Height);
200200

201201
workbook.Close();
202202
}

0 commit comments

Comments
 (0)