Skip to content

Commit

Permalink
Merge pull request #1178 from Bykiev/ImproveAutoSize
Browse files Browse the repository at this point in the history
Small speedup on autosizing column width
  • Loading branch information
tonyqus authored Oct 11, 2023
2 parents dbd4020 + 90fc386 commit 18b5971
Showing 1 changed file with 41 additions and 42 deletions.
83 changes: 41 additions & 42 deletions main/SS/Util/SheetUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit 18b5971

Please sign in to comment.