Skip to content

Commit

Permalink
Small speedup on autosizing column width
Browse files Browse the repository at this point in the history
Small speedup on autosizing column width
  • Loading branch information
Bykiev committed Sep 5, 2023
1 parent 66448d4 commit e5d2917
Showing 1 changed file with 42 additions and 43 deletions.
85 changes: 42 additions & 43 deletions main/SS/Util/SheetUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

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

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

/// <summary>
/// Convert HSSFFont to Font.
/// </summary>
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit e5d2917

Please sign in to comment.