112 lines
4.3 KiB
C#
112 lines
4.3 KiB
C#
namespace QWERTYkez.ExcelProcessor;
|
||
|
||
/// <summary>
|
||
/// Определяет шрифтовое оформление всей ячейки: размер, семейство, цвет, начертание.
|
||
/// Все свойства опциональны.
|
||
/// </summary>
|
||
public readonly struct CellFont : IEquatable<CellFont>
|
||
{
|
||
/// <summary>Размер шрифта в пунктах (например, 11).</summary>
|
||
public double? FontSize { get; init; }
|
||
|
||
/// <summary>Имя семейства шрифта (например, "Calibri", "Arial").</summary>
|
||
public string? FontFamily { get; init; }
|
||
|
||
/// <summary>Цвет текста.</summary>
|
||
public ExColor? FontColor { get; init; }
|
||
|
||
/// <summary>Жирное начертание.</summary>
|
||
public bool? IsBold { get; init; }
|
||
|
||
/// <summary>Курсив.</summary>
|
||
public bool? IsItalic { get; init; }
|
||
|
||
/// <summary>Подчёркивание (одинарное).</summary>
|
||
public bool? IsUnderline { get; init; }
|
||
|
||
/// <summary>Зачёркивание.</summary>
|
||
public bool? IsStrike { get; init; }
|
||
|
||
/// <summary>Создаёт элемент Font для Open XML.</summary>
|
||
public Font? ToFont()
|
||
{
|
||
if (!FontSize.HasValue && FontFamily is null && FontColor is null &&
|
||
!IsBold.HasValue && !IsItalic.HasValue && !IsUnderline.HasValue && !IsStrike.HasValue)
|
||
return null;
|
||
|
||
var font = new Font();
|
||
if (FontSize.HasValue)
|
||
font.FontSize = new FontSize { Val = FontSize.Value };
|
||
if (FontFamily is not null)
|
||
font.FontName = new FontName { Val = FontFamily };
|
||
if (FontColor.HasValue && FontColor.Value.Color.HasValue)
|
||
{
|
||
var c = FontColor.Value.Color.Value;
|
||
font.Color = new Color { Rgb = $"{c.R:X2}{c.G:X2}{c.B:X2}" };
|
||
}
|
||
if (IsBold == true) font.Bold = new Bold();
|
||
if (IsItalic == true) font.Italic = new Italic();
|
||
if (IsUnderline == true) font.Underline = new Underline();
|
||
if (IsStrike == true) font.Strike = new Strike();
|
||
return font;
|
||
}
|
||
|
||
/// <summary>Создаёт CellFont из элемента Font Open XML.</summary>
|
||
public static CellFont FromFont(Font? font)
|
||
{
|
||
if (font == null)
|
||
return default;
|
||
|
||
var result = new CellFont
|
||
{
|
||
IsBold = font.Bold != null,
|
||
IsItalic = font.Italic != null,
|
||
IsUnderline = font.Underline != null,
|
||
IsStrike = font.Strike != null
|
||
};
|
||
if (font.FontSize?.Val?.Value is { } size)
|
||
result = result with { FontSize = size };
|
||
if (font.FontName?.Val?.Value is { } name)
|
||
result = result with { FontFamily = name };
|
||
if (font.Color?.Rgb?.Value is { } rgb && rgb.Length >= 6)
|
||
{
|
||
var color = System.Drawing.Color.FromArgb(
|
||
Convert.ToByte(rgb.Substring(0, 2), 16),
|
||
Convert.ToByte(rgb.Substring(2, 2), 16),
|
||
Convert.ToByte(rgb.Substring(4, 2), 16)
|
||
);
|
||
result = result with { FontColor = new ExColor(color) };
|
||
}
|
||
return result;
|
||
}
|
||
|
||
public override bool Equals(object? obj) => obj is CellFont other && Equals(other);
|
||
public bool Equals(CellFont other) => this == other;
|
||
|
||
public static bool operator ==(CellFont left, CellFont right) =>
|
||
left.FontSize == right.FontSize &&
|
||
left.FontFamily == right.FontFamily &&
|
||
Equals(left.FontColor, right.FontColor) &&
|
||
left.IsBold == right.IsBold &&
|
||
left.IsItalic == right.IsItalic &&
|
||
left.IsUnderline == right.IsUnderline &&
|
||
left.IsStrike == right.IsStrike;
|
||
|
||
public static bool operator !=(CellFont left, CellFont right) => !(left == right);
|
||
|
||
public override int GetHashCode()
|
||
{
|
||
unchecked
|
||
{
|
||
int hash = 17;
|
||
hash = hash * 31 + (FontSize?.GetHashCode() ?? 0);
|
||
hash = hash * 31 + (FontFamily?.GetHashCode() ?? 0);
|
||
hash = hash * 31 + (FontColor?.GetHashCode() ?? 0);
|
||
hash = hash * 31 + (IsBold?.GetHashCode() ?? 0);
|
||
hash = hash * 31 + (IsItalic?.GetHashCode() ?? 0);
|
||
hash = hash * 31 + (IsUnderline?.GetHashCode() ?? 0);
|
||
hash = hash * 31 + (IsStrike?.GetHashCode() ?? 0);
|
||
return hash;
|
||
}
|
||
}
|
||
} |