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

Small speedup on autosizing column width #1178

Merged
merged 3 commits into from
Oct 11, 2023
Merged
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
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
Loading