From 8bf6143e2e806b56b207cd19a48fd48b35a451b6 Mon Sep 17 00:00:00 2001 From: Andrey Bykiev <bykiev.andrei@mail.ru> Date: Wed, 26 Jul 2023 13:01:06 +0300 Subject: [PATCH 01/16] Fix AutoSize column This PR fixes #1022, but still not finished because SixLabors.Fonts returns incorrect default chart width. This bug was already fixed in master branch of SixLabors.Fonts, PR will be updated after release --- main/SS/Util/SheetUtil.cs | 35 ++++++++++++++--------------------- 1 file changed, 14 insertions(+), 21 deletions(-) diff --git a/main/SS/Util/SheetUtil.cs b/main/SS/Util/SheetUtil.cs index 8e00e6a4b..4c65bc6d4 100644 --- a/main/SS/Util/SheetUtil.cs +++ b/main/SS/Util/SheetUtil.cs @@ -40,6 +40,9 @@ public class SheetUtil // */ private static char defaultChar = '0'; + // Default dpi + private static int dpi = 96; + // /** // * This is the multiple that the font height is scaled by when determining the // * boundary of rotated text. @@ -416,7 +419,7 @@ private static Font GetWindowsFont(ICell cell) private static double GetRotatedContentHeight(ICell cell, string stringValue, Font windowsFont) { var angle = cell.CellStyle.Rotation * 2.0 * Math.PI / 360.0; - var measureResult = TextMeasurer.Measure(stringValue, new TextOptions(windowsFont)); + var measureResult = TextMeasurer.Measure(stringValue, new TextOptions(windowsFont) { Dpi = dpi }); var x1 = Math.Abs(measureResult.Height * Math.Cos(angle)); var x2 = Math.Abs(measureResult.Width * Math.Sin(angle)); @@ -426,7 +429,7 @@ private static double GetRotatedContentHeight(ICell cell, string stringValue, Fo private static double GetContentHeight(string stringValue, Font windowsFont) { - var measureResult = TextMeasurer.Measure(stringValue, new TextOptions(windowsFont)); + var measureResult = TextMeasurer.Measure(stringValue, new TextOptions(windowsFont) { Dpi = dpi }); return Math.Round(measureResult.Height, 0, MidpointRounding.ToEven); } @@ -480,7 +483,7 @@ public static double GetCellWidth(ICell cell, int defaultCharWidth, DataFormatte String[] lines = rt.String.Split("\n".ToCharArray()); for (int i = 0; i < lines.Length; i++) { - String txt = lines[i] + defaultChar; + String txt = lines[i]; //AttributedString str = new AttributedString(txt); //copyAttributes(font, str, 0, txt.length()); @@ -514,7 +517,7 @@ public static double GetCellWidth(ICell cell, int defaultCharWidth, DataFormatte } if (sval != null) { - String txt = sval + defaultChar; + String txt = sval; //str = new AttributedString(txt); //copyAttributes(font, str, 0, txt.length()); windowsFont = IFont2Font(font); @@ -530,25 +533,19 @@ private static double GetCellWidth(int defaultCharWidth, int colspan, { //Rectangle bounds; double actualWidth; - FontRectangle sf = TextMeasurer.Measure(str, new TextOptions(windowsFont)); + FontRectangle sf = TextMeasurer.Measure(str, new TextOptions(windowsFont) { Dpi = dpi }); if (style.Rotation != 0) { double angle = style.Rotation * 2.0 * Math.PI / 360.0; double x1 = Math.Abs(sf.Height * Math.Sin(angle)); double x2 = Math.Abs(sf.Width * Math.Cos(angle)); actualWidth = Math.Round(x1 + x2, 0, MidpointRounding.ToEven); - //bounds = layout.getOutline(trans).getBounds(); } else - { - //bounds = layout.getBounds(); - actualWidth = Math.Round(sf.Width, 0, MidpointRounding.ToEven); - } - // entireWidth accounts for leading spaces which is excluded from bounds.getWidth() - //double frameWidth = bounds.getX() + bounds.getWidth(); - //width = Math.max(width, ((frameWidth / colspan) / defaultCharWidth) + style.getIndention()); - var correction = 1.1; - width = Math.Max(width, (actualWidth / colspan / defaultCharWidth * correction) + cell.CellStyle.Indention); + actualWidth = Math.Round(sf.Width, 0, MidpointRounding.ToEven); + + int padding = 5; + width = Math.Max(width, ((actualWidth + padding + cell.CellStyle.Indention) / colspan / defaultCharWidth)); return width; } @@ -610,13 +607,9 @@ public static double GetColumnWidth(ISheet sheet, int column, bool useMergedCell public static int GetDefaultCharWidth(IWorkbook wb) { IFont defaultFont = wb.GetFontAt((short)0); - - //AttributedString str = new AttributedString(String.valueOf(defaultChar)); - //copyAttributes(defaultFont, str, 0, 1); - //TextLayout layout = new TextLayout(str.getIterator(), fontRenderContext); - //int defaultCharWidth = (int)layout.getAdvance(); Font font = IFont2Font(defaultFont); - return (int)Math.Ceiling(TextMeasurer.Measure(new string(defaultChar, 1), new TextOptions(font)).Width); + + return (int)Math.Ceiling(TextMeasurer.Measure(new string(defaultChar, 1), new TextOptions(font) { Dpi = dpi }).Width); } /** From 18bb8f70f11ca610136ca703a79d85d31aaf2121 Mon Sep 17 00:00:00 2001 From: Andrey Bykiev <bykiev.andrei@mail.ru> Date: Wed, 26 Jul 2023 13:53:23 +0300 Subject: [PATCH 02/16] Fix --- main/SS/Util/SheetUtil.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main/SS/Util/SheetUtil.cs b/main/SS/Util/SheetUtil.cs index 4c65bc6d4..9319d3af5 100644 --- a/main/SS/Util/SheetUtil.cs +++ b/main/SS/Util/SheetUtil.cs @@ -545,7 +545,7 @@ private static double GetCellWidth(int defaultCharWidth, int colspan, actualWidth = Math.Round(sf.Width, 0, MidpointRounding.ToEven); int padding = 5; - width = Math.Max(width, ((actualWidth + padding + cell.CellStyle.Indention) / colspan / defaultCharWidth)); + width = Math.Max(width, ((actualWidth + padding) / colspan / defaultCharWidth) + cell.CellStyle.Indention); return width; } From b60be03146d5e9326a691048a51a676686c50620 Mon Sep 17 00:00:00 2001 From: Andrey Bykiev <bykiev.andrei@mail.ru> Date: Fri, 11 Aug 2023 16:25:16 +0300 Subject: [PATCH 03/16] Update SixLabors.Fonts --- main/HSSF/UserModel/EscherGraphics.cs | 6 +++++- main/NPOI.Core.csproj | 2 +- main/SS/Util/SheetUtil.cs | 8 ++++---- testcases/main/SS/UserModel/BaseTestBugzillaIssues.cs | 7 +++++-- 4 files changed, 15 insertions(+), 8 deletions(-) diff --git a/main/HSSF/UserModel/EscherGraphics.cs b/main/HSSF/UserModel/EscherGraphics.cs index d54c682c1..c4ae8e9d8 100644 --- a/main/HSSF/UserModel/EscherGraphics.cs +++ b/main/HSSF/UserModel/EscherGraphics.cs @@ -70,6 +70,9 @@ public class EscherGraphics : IDisposable private Font font; private static POILogger Logger = POILogFactory.GetLogger(typeof(EscherGraphics)); + // Default dpi + private static int dpi = 96; + /** * Construct an escher graphics object. * @@ -289,7 +292,8 @@ public void DrawString(String str, int x, int y) Font excelFont = new Font(SystemFonts.Get(font.Name.Equals("SansSerif") ? "Arial" : font.Name), (int)(font.Size / verticalPixelsPerPoint), font.FontMetrics.Description.Style); { - int width = (int)((TextMeasurer.Measure(str, new TextOptions(excelFont)).Width * 8) + 12); + var textOptions = new TextOptions(excelFont) { Dpi = dpi }; + int width = (int)((TextMeasurer.MeasureSize(str, textOptions).Width * 8) + 12); int height = (int)((font.Size / verticalPixelsPerPoint) + 6) * 2; y -= Convert.ToInt32((font.Size / verticalPixelsPerPoint) + 2 * verticalPixelsPerPoint); // we want to Draw the shape from the top-left HSSFTextbox textbox = escherGroup.CreateTextbox(new HSSFChildAnchor(x, y, x + width, y + height)); diff --git a/main/NPOI.Core.csproj b/main/NPOI.Core.csproj index a7def2ca6..089ca2971 100644 --- a/main/NPOI.Core.csproj +++ b/main/NPOI.Core.csproj @@ -19,7 +19,7 @@ <PackageReference Include="Microsoft.IO.RecyclableMemoryStream" Version="2.3.2" /> <PackageReference Include="BouncyCastle.Cryptography" Version="2.2.1" /> <PackageReference Include="SharpZipLib" Version="1.3.3" /> - <PackageReference Include="SixLabors.Fonts" Version="1.0.0-beta19" /> + <PackageReference Include="SixLabors.Fonts" Version="1.0.0" /> <PackageReference Include="SixLabors.ImageSharp" Version="2.1.4" /> </ItemGroup> diff --git a/main/SS/Util/SheetUtil.cs b/main/SS/Util/SheetUtil.cs index 9319d3af5..80e0e1f53 100644 --- a/main/SS/Util/SheetUtil.cs +++ b/main/SS/Util/SheetUtil.cs @@ -419,7 +419,7 @@ private static Font GetWindowsFont(ICell cell) private static double GetRotatedContentHeight(ICell cell, string stringValue, Font windowsFont) { var angle = cell.CellStyle.Rotation * 2.0 * Math.PI / 360.0; - var measureResult = TextMeasurer.Measure(stringValue, new TextOptions(windowsFont) { Dpi = dpi }); + var measureResult = TextMeasurer.MeasureSize(stringValue, new TextOptions(windowsFont) { Dpi = dpi }); var x1 = Math.Abs(measureResult.Height * Math.Cos(angle)); var x2 = Math.Abs(measureResult.Width * Math.Sin(angle)); @@ -429,7 +429,7 @@ private static double GetRotatedContentHeight(ICell cell, string stringValue, Fo private static double GetContentHeight(string stringValue, Font windowsFont) { - var measureResult = TextMeasurer.Measure(stringValue, new TextOptions(windowsFont) { Dpi = dpi }); + var measureResult = TextMeasurer.MeasureSize(stringValue, new TextOptions(windowsFont) { Dpi = dpi }); return Math.Round(measureResult.Height, 0, MidpointRounding.ToEven); } @@ -533,7 +533,7 @@ private static double GetCellWidth(int defaultCharWidth, int colspan, { //Rectangle bounds; double actualWidth; - FontRectangle sf = TextMeasurer.Measure(str, new TextOptions(windowsFont) { Dpi = dpi }); + FontRectangle sf = TextMeasurer.MeasureSize(str, new TextOptions(windowsFont) { Dpi = dpi }); if (style.Rotation != 0) { double angle = style.Rotation * 2.0 * Math.PI / 360.0; @@ -609,7 +609,7 @@ public static int GetDefaultCharWidth(IWorkbook wb) IFont defaultFont = wb.GetFontAt((short)0); Font font = IFont2Font(defaultFont); - return (int)Math.Ceiling(TextMeasurer.Measure(new string(defaultChar, 1), new TextOptions(font) { Dpi = dpi }).Width); + return (int)Math.Ceiling(TextMeasurer.MeasureSize(new string(defaultChar, 1), new TextOptions(font) { Dpi = dpi }).Width); } /** diff --git a/testcases/main/SS/UserModel/BaseTestBugzillaIssues.cs b/testcases/main/SS/UserModel/BaseTestBugzillaIssues.cs index f6b6f5e5c..e8c880b7f 100644 --- a/testcases/main/SS/UserModel/BaseTestBugzillaIssues.cs +++ b/testcases/main/SS/UserModel/BaseTestBugzillaIssues.cs @@ -38,6 +38,7 @@ public abstract class BaseTestBugzillaIssues { private ITestDataProvider _testDataProvider; + private static int dpi = 96; protected BaseTestBugzillaIssues(ITestDataProvider TestDataProvider) { @@ -487,7 +488,8 @@ private double ComputeCellWidthManually(ICell cell0, IFont font) //TextLayout layout = new TextLayout(str.getIterator(), fontRenderContext); //width = ((layout.getBounds().getWidth() / 1) / 8); Font wfont = SheetUtil.IFont2Font(font); - width = (double)TextMeasurer.Measure(txt, new TextOptions(wfont)).Width; + var textOptions = new TextOptions(wfont) { Dpi = dpi }; + width = (double)TextMeasurer.MeasureSize(txt, textOptions).Width; return width; } @@ -495,7 +497,8 @@ private double ComputeCellWidthFixed(IFont font, String txt) { double width; Font wfont = SheetUtil.IFont2Font(font); - width = (double)TextMeasurer.Measure(txt, new TextOptions(wfont)).Width; + var textOptions = new TextOptions(wfont) { Dpi = dpi }; + width = (double)TextMeasurer.MeasureSize(txt, textOptions).Width; return width; } From be2041679423c34f457e352121255a140e6940e2 Mon Sep 17 00:00:00 2001 From: Andrey Bykiev <bykiev.andrei@mail.ru> Date: Fri, 11 Aug 2023 17:15:50 +0300 Subject: [PATCH 04/16] Fix test --- testcases/main/HSSF/UserModel/TestHSSFSheet.cs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/testcases/main/HSSF/UserModel/TestHSSFSheet.cs b/testcases/main/HSSF/UserModel/TestHSSFSheet.cs index d0de58a34..a0c2c6e2d 100644 --- a/testcases/main/HSSF/UserModel/TestHSSFSheet.cs +++ b/testcases/main/HSSF/UserModel/TestHSSFSheet.cs @@ -854,10 +854,7 @@ public void TestAutoSizeRow() sheet.AutoSizeRow(row.RowNum); Assert.AreNotEqual(100, row.Height); - - // there's slight difference due to fonts - var expectedRowHeight = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? 506 : 528; - Assert.AreEqual(expectedRowHeight, row.Height); + Assert.AreEqual(506, row.Height); workbook.Close(); } From 70e997aa2f64bccd16a9518da9df2c18f07e116a Mon Sep 17 00:00:00 2001 From: ABykiev <bykiev.andrei@mail.ru> Date: Fri, 11 Aug 2023 21:56:24 +0300 Subject: [PATCH 05/16] Fix calculating row height --- main/SS/Util/SheetUtil.cs | 13 ++++++++----- testcases/main/HSSF/UserModel/TestHSSFSheet.cs | 2 +- testcases/ooxml/XSSF/UserModel/TestXSSFSheet.cs | 2 +- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/main/SS/Util/SheetUtil.cs b/main/SS/Util/SheetUtil.cs index 80e0e1f53..1c12651b4 100644 --- a/main/SS/Util/SheetUtil.cs +++ b/main/SS/Util/SheetUtil.cs @@ -272,19 +272,19 @@ public static IRow CopyRow(ISheet sheet, int sourceRowIndex, int targetRowIndex) public static double GetRowHeight(IRow row, bool useMergedCells, int firstColumnIdx, int lastColumnIdx) { - double width = -1; + double height = -1; for (int cellIdx = firstColumnIdx; cellIdx <= lastColumnIdx; ++cellIdx) { ICell cell = row.GetCell(cellIdx); if (row != null && cell != null) { - double cellWidth = GetCellHeight(cell, useMergedCells); - width = Math.Max(width, cellWidth); + double cellHeight = GetCellHeight(cell, useMergedCells); + height = Math.Max(height, cellHeight); } } - return width; + return height; } public static double GetRowHeight(ISheet sheet, int rowIdx, bool useMergedCells, int firstColumnIdx, int lastColumnIdx) @@ -370,7 +370,10 @@ private static int GetNumberOfRowsInMergedRegion(ICell cell) private static double GetCellConetntHeight(double actualHeight, int numberOfRowsInMergedRegion) { var correction = 1.1; - return Math.Max(-1, actualHeight / numberOfRowsInMergedRegion * correction); + int width = (int)(actualHeight / numberOfRowsInMergedRegion * correction); + var height = Math.Max(-1, width); + + return height * 72 / dpi; } private static string GetCellStringValue(ICell cell) diff --git a/testcases/main/HSSF/UserModel/TestHSSFSheet.cs b/testcases/main/HSSF/UserModel/TestHSSFSheet.cs index a0c2c6e2d..f3bd11370 100644 --- a/testcases/main/HSSF/UserModel/TestHSSFSheet.cs +++ b/testcases/main/HSSF/UserModel/TestHSSFSheet.cs @@ -854,7 +854,7 @@ public void TestAutoSizeRow() sheet.AutoSizeRow(row.RowNum); Assert.AreNotEqual(100, row.Height); - Assert.AreEqual(506, row.Height); + Assert.AreEqual(510, row.Height); workbook.Close(); } diff --git a/testcases/ooxml/XSSF/UserModel/TestXSSFSheet.cs b/testcases/ooxml/XSSF/UserModel/TestXSSFSheet.cs index 93a929566..065de81f1 100644 --- a/testcases/ooxml/XSSF/UserModel/TestXSSFSheet.cs +++ b/testcases/ooxml/XSSF/UserModel/TestXSSFSheet.cs @@ -196,7 +196,7 @@ public void TestAutoSizeRow() sheet.AutoSizeRow(row.RowNum); Assert.AreNotEqual(100, row.Height); - Assert.AreEqual(550, row.Height); + Assert.AreEqual(540, row.Height); workbook.Close(); } From de12b387e2c99731475db3fba47f20e3079e393f Mon Sep 17 00:00:00 2001 From: ABykiev <bykiev.andrei@mail.ru> Date: Fri, 11 Aug 2023 22:09:49 +0300 Subject: [PATCH 06/16] Update SheetUtil.cs --- main/SS/Util/SheetUtil.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/main/SS/Util/SheetUtil.cs b/main/SS/Util/SheetUtil.cs index 1c12651b4..acfec1b15 100644 --- a/main/SS/Util/SheetUtil.cs +++ b/main/SS/Util/SheetUtil.cs @@ -370,8 +370,8 @@ private static int GetNumberOfRowsInMergedRegion(ICell cell) private static double GetCellConetntHeight(double actualHeight, int numberOfRowsInMergedRegion) { var correction = 1.1; - int width = (int)(actualHeight / numberOfRowsInMergedRegion * correction); - var height = Math.Max(-1, width); + int correctedHeight = (int)(actualHeight / numberOfRowsInMergedRegion * correction); + var height = Math.Max(-1, correctedHeight); return height * 72 / dpi; } From 4e6e9bcfb4780a2e599073e546d92b82900d643b Mon Sep 17 00:00:00 2001 From: Andrey Bykiev <bykiev.andrei@mail.ru> Date: Sat, 12 Aug 2023 18:48:01 +0300 Subject: [PATCH 07/16] Use MeasureAdvance while calulating height --- main/SS/Util/SheetUtil.cs | 10 +++------- testcases/main/HSSF/UserModel/TestHSSFSheet.cs | 2 +- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/main/SS/Util/SheetUtil.cs b/main/SS/Util/SheetUtil.cs index acfec1b15..02921aadd 100644 --- a/main/SS/Util/SheetUtil.cs +++ b/main/SS/Util/SheetUtil.cs @@ -369,11 +369,7 @@ private static int GetNumberOfRowsInMergedRegion(ICell cell) private static double GetCellConetntHeight(double actualHeight, int numberOfRowsInMergedRegion) { - var correction = 1.1; - int correctedHeight = (int)(actualHeight / numberOfRowsInMergedRegion * correction); - var height = Math.Max(-1, correctedHeight); - - return height * 72 / dpi; + return Math.Max(-1, actualHeight / numberOfRowsInMergedRegion); } private static string GetCellStringValue(ICell cell) @@ -422,7 +418,7 @@ private static Font GetWindowsFont(ICell cell) private static double GetRotatedContentHeight(ICell cell, string stringValue, Font windowsFont) { var angle = cell.CellStyle.Rotation * 2.0 * Math.PI / 360.0; - var measureResult = TextMeasurer.MeasureSize(stringValue, new TextOptions(windowsFont) { Dpi = dpi }); + var measureResult = TextMeasurer.MeasureAdvance(stringValue, new TextOptions(windowsFont) { Dpi = dpi }); var x1 = Math.Abs(measureResult.Height * Math.Cos(angle)); var x2 = Math.Abs(measureResult.Width * Math.Sin(angle)); @@ -432,7 +428,7 @@ private static double GetRotatedContentHeight(ICell cell, string stringValue, Fo private static double GetContentHeight(string stringValue, Font windowsFont) { - var measureResult = TextMeasurer.MeasureSize(stringValue, new TextOptions(windowsFont) { Dpi = dpi }); + var measureResult = TextMeasurer.MeasureAdvance(stringValue, new TextOptions(windowsFont) { Dpi = dpi }); return Math.Round(measureResult.Height, 0, MidpointRounding.ToEven); } diff --git a/testcases/main/HSSF/UserModel/TestHSSFSheet.cs b/testcases/main/HSSF/UserModel/TestHSSFSheet.cs index f3bd11370..e19ee0971 100644 --- a/testcases/main/HSSF/UserModel/TestHSSFSheet.cs +++ b/testcases/main/HSSF/UserModel/TestHSSFSheet.cs @@ -854,7 +854,7 @@ public void TestAutoSizeRow() sheet.AutoSizeRow(row.RowNum); Assert.AreNotEqual(100, row.Height); - Assert.AreEqual(510, row.Height); + Assert.AreEqual(540, row.Height); workbook.Close(); } From 22129639e502d771af14fd7aa224afb8e66573bf Mon Sep 17 00:00:00 2001 From: Andrey Bykiev <bykiev.andrei@mail.ru> Date: Sat, 12 Aug 2023 19:20:18 +0300 Subject: [PATCH 08/16] Measure string without paddings --- main/SS/Util/SheetUtil.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/main/SS/Util/SheetUtil.cs b/main/SS/Util/SheetUtil.cs index 02921aadd..47f75fe98 100644 --- a/main/SS/Util/SheetUtil.cs +++ b/main/SS/Util/SheetUtil.cs @@ -532,7 +532,7 @@ private static double GetCellWidth(int defaultCharWidth, int colspan, { //Rectangle bounds; double actualWidth; - FontRectangle sf = TextMeasurer.MeasureSize(str, new TextOptions(windowsFont) { Dpi = dpi }); + FontRectangle sf = TextMeasurer.MeasureBounds(str, new TextOptions(windowsFont) { Dpi = dpi }); if (style.Rotation != 0) { double angle = style.Rotation * 2.0 * Math.PI / 360.0; @@ -608,7 +608,7 @@ public static int GetDefaultCharWidth(IWorkbook wb) IFont defaultFont = wb.GetFontAt((short)0); Font font = IFont2Font(defaultFont); - return (int)Math.Ceiling(TextMeasurer.MeasureSize(new string(defaultChar, 1), new TextOptions(font) { Dpi = dpi }).Width); + return (int)Math.Ceiling(TextMeasurer.MeasureBounds(new string(defaultChar, 1), new TextOptions(font) { Dpi = dpi }).Width); } /** From 3b214ca5ced20017f8463286e8f5ec48e485a2c5 Mon Sep 17 00:00:00 2001 From: Andrey Bykiev <bykiev.andrei@mail.ru> Date: Sat, 12 Aug 2023 19:38:24 +0300 Subject: [PATCH 09/16] Revert "Measure string without paddings" This reverts commit 22129639e502d771af14fd7aa224afb8e66573bf. --- main/SS/Util/SheetUtil.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/main/SS/Util/SheetUtil.cs b/main/SS/Util/SheetUtil.cs index 47f75fe98..02921aadd 100644 --- a/main/SS/Util/SheetUtil.cs +++ b/main/SS/Util/SheetUtil.cs @@ -532,7 +532,7 @@ private static double GetCellWidth(int defaultCharWidth, int colspan, { //Rectangle bounds; double actualWidth; - FontRectangle sf = TextMeasurer.MeasureBounds(str, new TextOptions(windowsFont) { Dpi = dpi }); + FontRectangle sf = TextMeasurer.MeasureSize(str, new TextOptions(windowsFont) { Dpi = dpi }); if (style.Rotation != 0) { double angle = style.Rotation * 2.0 * Math.PI / 360.0; @@ -608,7 +608,7 @@ public static int GetDefaultCharWidth(IWorkbook wb) IFont defaultFont = wb.GetFontAt((short)0); Font font = IFont2Font(defaultFont); - return (int)Math.Ceiling(TextMeasurer.MeasureBounds(new string(defaultChar, 1), new TextOptions(font) { Dpi = dpi }).Width); + return (int)Math.Ceiling(TextMeasurer.MeasureSize(new string(defaultChar, 1), new TextOptions(font) { Dpi = dpi }).Width); } /** From b4dce169580acfad30e809252fefe5ea3d59f18e Mon Sep 17 00:00:00 2001 From: Andrey Bykiev <bykiev.andrei@mail.ru> Date: Sat, 12 Aug 2023 19:43:09 +0300 Subject: [PATCH 10/16] Use border instead of padding --- main/SS/Util/SheetUtil.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/main/SS/Util/SheetUtil.cs b/main/SS/Util/SheetUtil.cs index 02921aadd..2f8fb2ea6 100644 --- a/main/SS/Util/SheetUtil.cs +++ b/main/SS/Util/SheetUtil.cs @@ -543,8 +543,8 @@ private static double GetCellWidth(int defaultCharWidth, int colspan, else actualWidth = Math.Round(sf.Width, 0, MidpointRounding.ToEven); - int padding = 5; - width = Math.Max(width, ((actualWidth + padding) / colspan / defaultCharWidth) + cell.CellStyle.Indention); + int border = 1; + width = Math.Max(width, ((actualWidth + border) / colspan / defaultCharWidth) + cell.CellStyle.Indention); return width; } From 1085b4be3ec91e9a1a1910bd00cc7af8ceb74f4b Mon Sep 17 00:00:00 2001 From: Andrey Bykiev <bykiev.andrei@mail.ru> Date: Sat, 12 Aug 2023 20:00:16 +0300 Subject: [PATCH 11/16] Revert "Use border instead of padding" This reverts commit b4dce169580acfad30e809252fefe5ea3d59f18e. --- main/SS/Util/SheetUtil.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/main/SS/Util/SheetUtil.cs b/main/SS/Util/SheetUtil.cs index 2f8fb2ea6..02921aadd 100644 --- a/main/SS/Util/SheetUtil.cs +++ b/main/SS/Util/SheetUtil.cs @@ -543,8 +543,8 @@ private static double GetCellWidth(int defaultCharWidth, int colspan, else actualWidth = Math.Round(sf.Width, 0, MidpointRounding.ToEven); - int border = 1; - width = Math.Max(width, ((actualWidth + border) / colspan / defaultCharWidth) + cell.CellStyle.Indention); + int padding = 5; + width = Math.Max(width, ((actualWidth + padding) / colspan / defaultCharWidth) + cell.CellStyle.Indention); return width; } From 2f58934d17dd975b4eab2a52b91d8e5f296bd4bb Mon Sep 17 00:00:00 2001 From: ABykiev <bykiev.andrei@mail.ru> Date: Mon, 14 Aug 2023 19:54:12 +0300 Subject: [PATCH 12/16] Update SheetUtil.cs --- main/SS/Util/SheetUtil.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main/SS/Util/SheetUtil.cs b/main/SS/Util/SheetUtil.cs index 02921aadd..867efb04c 100644 --- a/main/SS/Util/SheetUtil.cs +++ b/main/SS/Util/SheetUtil.cs @@ -532,7 +532,7 @@ private static double GetCellWidth(int defaultCharWidth, int colspan, { //Rectangle bounds; double actualWidth; - FontRectangle sf = TextMeasurer.MeasureSize(str, new TextOptions(windowsFont) { Dpi = dpi }); + FontRectangle sf = TextMeasurer.MeasureAdvance(str, new TextOptions(windowsFont) { Dpi = dpi }); if (style.Rotation != 0) { double angle = style.Rotation * 2.0 * Math.PI / 360.0; From 094ee02e273609d156563f076f78daafeb1edec0 Mon Sep 17 00:00:00 2001 From: ABykiev <bykiev.andrei@mail.ru> Date: Wed, 16 Aug 2023 20:40:57 +0300 Subject: [PATCH 13/16] Revert "Update SheetUtil.cs" This reverts commit 2f58934d17dd975b4eab2a52b91d8e5f296bd4bb. --- main/SS/Util/SheetUtil.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main/SS/Util/SheetUtil.cs b/main/SS/Util/SheetUtil.cs index 867efb04c..02921aadd 100644 --- a/main/SS/Util/SheetUtil.cs +++ b/main/SS/Util/SheetUtil.cs @@ -532,7 +532,7 @@ private static double GetCellWidth(int defaultCharWidth, int colspan, { //Rectangle bounds; double actualWidth; - FontRectangle sf = TextMeasurer.MeasureAdvance(str, new TextOptions(windowsFont) { Dpi = dpi }); + FontRectangle sf = TextMeasurer.MeasureSize(str, new TextOptions(windowsFont) { Dpi = dpi }); if (style.Rotation != 0) { double angle = style.Rotation * 2.0 * Math.PI / 360.0; From 64def17e54fec00b7b1241e7832a26cecc386b80 Mon Sep 17 00:00:00 2001 From: ABykiev <bykiev.andrei@mail.ru> Date: Wed, 16 Aug 2023 20:49:00 +0300 Subject: [PATCH 14/16] Fix test --- testcases/main/SS/UserModel/BaseTestBugzillaIssues.cs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/testcases/main/SS/UserModel/BaseTestBugzillaIssues.cs b/testcases/main/SS/UserModel/BaseTestBugzillaIssues.cs index e8c880b7f..f87c5a89e 100644 --- a/testcases/main/SS/UserModel/BaseTestBugzillaIssues.cs +++ b/testcases/main/SS/UserModel/BaseTestBugzillaIssues.cs @@ -446,12 +446,14 @@ public void Bug51622_testAutoSizeShouldRecognizeLeadingSpaces() // if the default font or margins change. double expectedRatioThreshold = 1.2f; double leadingWhitespaceRatio = ((double)leadingWhitespaceColWidth) / noWhitespaceColWidth; - double trailingWhitespaceRatio = ((double)leadingWhitespaceColWidth) / noWhitespaceColWidth; + double trailingWhitespaceRatio = ((double)trailingWhitespaceColWidth) / noWhitespaceColWidth; assertGreaterThan("leading whitespace is longer than no whitespace", leadingWhitespaceRatio, expectedRatioThreshold); assertGreaterThan("trailing whitespace is longer than no whitespace", trailingWhitespaceRatio, expectedRatioThreshold); - Assert.AreEqual(leadingWhitespaceColWidth, trailingWhitespaceColWidth, - "cells with equal leading and trailing whitespace have equal width"); + + //This is not correct https://github.com/SixLabors/Fonts/discussions/349 + //Assert.AreEqual(leadingWhitespaceColWidth, trailingWhitespaceColWidth, + //"cells with equal leading and trailing whitespace have equal width"); wb.Close(); } From e45882d17feec5c60e392d30f932b0ba23ad6845 Mon Sep 17 00:00:00 2001 From: ABykiev <bykiev.andrei@mail.ru> Date: Wed, 16 Aug 2023 21:51:57 +0300 Subject: [PATCH 15/16] Added correction --- main/SS/Util/SheetUtil.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/main/SS/Util/SheetUtil.cs b/main/SS/Util/SheetUtil.cs index 02921aadd..0d86e4015 100644 --- a/main/SS/Util/SheetUtil.cs +++ b/main/SS/Util/SheetUtil.cs @@ -544,7 +544,8 @@ private static double GetCellWidth(int defaultCharWidth, int colspan, actualWidth = Math.Round(sf.Width, 0, MidpointRounding.ToEven); int padding = 5; - width = Math.Max(width, ((actualWidth + padding) / colspan / defaultCharWidth) + cell.CellStyle.Indention); + double correction = 1.1; + width = Math.Max(width, ((actualWidth + padding) / colspan / defaultCharWidth * correction) + cell.CellStyle.Indention); return width; } From eb84507a3cabee3e9ab1056df5db6635e2132961 Mon Sep 17 00:00:00 2001 From: ABykiev <bykiev.andrei@mail.ru> Date: Wed, 16 Aug 2023 22:19:48 +0300 Subject: [PATCH 16/16] Update SheetUtil.cs --- main/SS/Util/SheetUtil.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main/SS/Util/SheetUtil.cs b/main/SS/Util/SheetUtil.cs index 0d86e4015..659999dfa 100644 --- a/main/SS/Util/SheetUtil.cs +++ b/main/SS/Util/SheetUtil.cs @@ -544,7 +544,7 @@ private static double GetCellWidth(int defaultCharWidth, int colspan, actualWidth = Math.Round(sf.Width, 0, MidpointRounding.ToEven); int padding = 5; - double correction = 1.1; + double correction = 1.05; width = Math.Max(width, ((actualWidth + padding) / colspan / defaultCharWidth * correction) + cell.CellStyle.Indention); return width; }