From e5d2917dd1510d32b166d2b8650b1a20d19c14ff Mon Sep 17 00:00:00 2001 From: ABykiev Date: Tue, 5 Sep 2023 22:10:07 +0300 Subject: [PATCH 1/2] Small speedup on autosizing column width Small speedup on autosizing column width --- main/SS/Util/SheetUtil.cs | 85 +++++++++++++++++++-------------------- 1 file changed, 42 insertions(+), 43 deletions(-) diff --git a/main/SS/Util/SheetUtil.cs b/main/SS/Util/SheetUtil.cs index 3dc8dfb56..dae6da2e0 100644 --- a/main/SS/Util/SheetUtil.cs +++ b/main/SS/Util/SheetUtil.cs @@ -434,7 +434,7 @@ private static double GetRotatedContentHeight(ICell cell, string stringValue, Fo private static double GetContentHeight(string stringValue, Font windowsFont) { var measureResult = TextMeasurer.MeasureAdvance(stringValue, new TextOptions(windowsFont) { Dpi = dpi }); - + return Math.Round(measureResult.Height, 0, MidpointRounding.ToEven); } @@ -472,63 +472,62 @@ public static double GetCellWidth(ICell cell, int defaultCharWidth, DataFormatte ICellStyle style = cell.CellStyle; CellType cellType = cell.CellType; - IFont defaultFont = wb.GetFontAt((short)0); - Font windowsFont = IFont2Font(defaultFont); + // for formula cells we compute the cell width for the cached formula result - if (cellType == CellType.Formula) cellType = cell.CachedFormulaResultType; + if (cellType == CellType.Formula) + cellType = cell.CachedFormulaResultType; IFont font = wb.GetFontAt(style.FontIndex); + Font windowsFont = IFont2Font(font); double width = -1; + + if (cellType == CellType.String) { - if (cellType == CellType.String) + IRichTextString rt = cell.RichStringCellValue; + String[] lines = rt.String.Split("\n".ToCharArray()); + for (int i = 0; i < lines.Length; i++) { - IRichTextString rt = cell.RichStringCellValue; - String[] lines = rt.String.Split("\n".ToCharArray()); - for (int i = 0; i < lines.Length; i++) - { - String txt = lines[i]; + String txt = lines[i]; - //AttributedString str = new AttributedString(txt); - //copyAttributes(font, str, 0, txt.length()); - windowsFont = IFont2Font(font); - if (rt.NumFormattingRuns > 0) - { - // TODO: support rich text fragments - } - - width = GetCellWidth(defaultCharWidth, colspan, style, width, txt, windowsFont, cell); + //AttributedString str = new AttributedString(txt); + //copyAttributes(font, str, 0, txt.length()); + if (rt.NumFormattingRuns > 0) + { + // TODO: support rich text fragments } + + width = GetCellWidth(defaultCharWidth, colspan, style, width, txt, windowsFont, cell); } - else + } + else + { + String sval = null; + if (cellType == CellType.Numeric) { - String sval = null; - if (cellType == CellType.Numeric) - { - // Try to get it formatted to look the same as excel - try - { - sval = formatter.FormatCellValue(cell, dummyEvaluator); - } - catch - { - sval = cell.NumericCellValue.ToString(); - } - } - else if (cellType == CellType.Boolean) + // Try to get it formatted to look the same as excel + try { - sval = cell.BooleanCellValue.ToString().ToUpper(); + sval = formatter.FormatCellValue(cell, dummyEvaluator); } - if (sval != null) + catch { - String txt = sval; - //str = new AttributedString(txt); - //copyAttributes(font, str, 0, txt.length()); - windowsFont = IFont2Font(font); - width = GetCellWidth(defaultCharWidth, colspan, style, width, txt, windowsFont, cell); + sval = cell.NumericCellValue.ToString(); } } + else if (cellType == CellType.Boolean) + { + sval = cell.BooleanCellValue.ToString().ToUpper(); + } + if (sval != null) + { + String txt = sval; + //str = new AttributedString(txt); + //copyAttributes(font, str, 0, txt.length()); + width = GetCellWidth(defaultCharWidth, colspan, style, width, txt, windowsFont, cell); + } } + return width; } @@ -682,7 +681,7 @@ public static bool CanComputeColumnWidth(IFont font) // if (font.IsItalic) str.AddAttribute(TextAttribute.POSTURE, TextAttribute.POSTURE_OBLIQUE, startIdx, endIdx); // TODO-Fonts: not supported: if (font.Underline == (byte)FontUnderlineType.SINGLE) str.AddAttribute(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON, startIdx, endIdx); //} - + /// /// Convert HSSFFont to Font. /// @@ -723,7 +722,7 @@ internal static Font IFont2Font(IFont font1) fontFamily = SystemFonts.Families.First(); } } - + Font font = new Font(fontFamily, (float)font1.FontHeightInPoints, style); return font; } From 90fc386abb5235df9d2869b6621bc71498be780f Mon Sep 17 00:00:00 2001 From: ABykiev Date: Wed, 6 Sep 2023 21:50:40 +0300 Subject: [PATCH 2/2] Sync with main branch --- main/SS/Util/SheetUtil.cs | 83 +++++++++++++++++++-------------------- 1 file changed, 41 insertions(+), 42 deletions(-) diff --git a/main/SS/Util/SheetUtil.cs b/main/SS/Util/SheetUtil.cs index 630fc2b0e..b1de15261 100644 --- a/main/SS/Util/SheetUtil.cs +++ b/main/SS/Util/SheetUtil.cs @@ -435,7 +435,7 @@ private static double GetRotatedContentHeight(ICell cell, string stringValue, Fo private static double GetContentHeight(string stringValue, Font windowsFont) { var measureResult = TextMeasurer.MeasureAdvance(stringValue, new TextOptions(windowsFont) { Dpi = dpi }); - + return Math.Round(measureResult.Height, 0, MidpointRounding.ToEven); } @@ -473,63 +473,62 @@ public static double GetCellWidth(ICell cell, int defaultCharWidth, DataFormatte ICellStyle style = cell.CellStyle; CellType cellType = cell.CellType; - IFont defaultFont = wb.GetFontAt((short)0); - Font windowsFont = IFont2Font(defaultFont); + // for formula cells we compute the cell width for the cached formula result - if (cellType == CellType.Formula) cellType = cell.CachedFormulaResultType; + if (cellType == CellType.Formula) + cellType = cell.CachedFormulaResultType; IFont font = wb.GetFontAt(style.FontIndex); + Font windowsFont = IFont2Font(font); double width = -1; + + if (cellType == CellType.String) { - if (cellType == CellType.String) + IRichTextString rt = cell.RichStringCellValue; + String[] lines = rt.String.Split("\n".ToCharArray()); + for (int i = 0; i < lines.Length; i++) { - IRichTextString rt = cell.RichStringCellValue; - String[] lines = rt.String.Split("\n".ToCharArray()); - for (int i = 0; i < lines.Length; i++) - { - String txt = lines[i]; - - //AttributedString str = new AttributedString(txt); - //copyAttributes(font, str, 0, txt.length()); - windowsFont = IFont2Font(font); - if (rt.NumFormattingRuns > 0) - { - // TODO: support rich text fragments - } + String txt = lines[i]; - width = GetCellWidth(defaultCharWidth, colspan, style, width, txt, windowsFont, cell); + //AttributedString str = new AttributedString(txt); + //copyAttributes(font, str, 0, txt.length()); + if (rt.NumFormattingRuns > 0) + { + // TODO: support rich text fragments } + + width = GetCellWidth(defaultCharWidth, colspan, style, width, txt, windowsFont, cell); } - else + } + else + { + String sval = null; + if (cellType == CellType.Numeric) { - String sval = null; - if (cellType == CellType.Numeric) - { - // Try to get it formatted to look the same as excel - try - { - sval = formatter.FormatCellValue(cell, dummyEvaluator); - } - catch - { - sval = cell.NumericCellValue.ToString(); - } - } - else if (cellType == CellType.Boolean) + // Try to get it formatted to look the same as excel + try { - sval = cell.BooleanCellValue.ToString().ToUpper(); + sval = formatter.FormatCellValue(cell, dummyEvaluator); } - if (sval != null) + catch { - String txt = sval; - //str = new AttributedString(txt); - //copyAttributes(font, str, 0, txt.length()); - windowsFont = IFont2Font(font); - width = GetCellWidth(defaultCharWidth, colspan, style, width, txt, windowsFont, cell); + sval = cell.NumericCellValue.ToString(); } } + else if (cellType == CellType.Boolean) + { + sval = cell.BooleanCellValue.ToString().ToUpper(); + } + if (sval != null) + { + String txt = sval; + //str = new AttributedString(txt); + //copyAttributes(font, str, 0, txt.length()); + width = GetCellWidth(defaultCharWidth, colspan, style, width, txt, windowsFont, cell); + } } + return width; } @@ -749,7 +748,7 @@ internal static Font IFont2Font(IFont font1) var key = new FontCacheKey(font1.FontName, (float)font1.FontHeightInPoints, style); // only use cache if font size is an integer and culture is original to prevent cache size explosion - if (font1.FontHeightInPoints == (int) font1.FontHeightInPoints && CultureInfo.CurrentCulture.Equals(StartupCulture)) + if (font1.FontHeightInPoints == (int)font1.FontHeightInPoints && CultureInfo.CurrentCulture.Equals(StartupCulture)) { return FontCache.GetOrAdd(key, IFont2FontImpl); }