using MathStyle = DocumentFormat.OpenXml.Math.Style; using MathStyleValues = DocumentFormat.OpenXml.Math.StyleValues; namespace QWERTYkez.WordProcessor; public record FontProps { public FontProps(double? size = null) => Size = size ?? 12; public string? FontFamily { get; init; } public double? Size { get => _SizeD.HasValue ? _SizeD.Value / 2d : null; init => _SizeD = value.HasValue ? (uint?)(value.Value * 2) : null; } uint? _SizeD; public bool? IsItalic { get; init; } public bool? IsBold { get; init; } public UnderlineValues? Underline { get; init; } public VerticalPositionValues? SubSup { get; init; } public string? Color { get; init; } public bool TryExtract(out List list) { list = []; if (!string.IsNullOrWhiteSpace(FontFamily)) list.Add(new RunFonts { Ascii = FontFamily, HighAnsi = FontFamily }); if (_SizeD is { } sizeD) list.Add(new FontSize { Val = sizeD.ToString() }); if (IsBold is { } isBold && isBold) list.Add(new Bold()); if (IsItalic is { } isItalic && isItalic) list.Add(new Italic()); if (Underline is { } underline) list.Add(new Underline { Val = underline }); if (!string.IsNullOrWhiteSpace(Color)) list.Add(new Color { Val = Color }); if (SubSup is { } subsup) list.Add(new VerticalTextAlignment { Val = subsup }); return list.Count > 0; } public bool TryExtractWithoutFamily(out List list) { list = []; if (_SizeD is { } sizeD) list.Add(new FontSize { Val = sizeD.ToString() }); if (IsBold is { } isBold && isBold) list.Add(new Bold()); if (IsItalic is { } isItalic && isItalic) list.Add(new Italic()); if (Underline is { } underline) list.Add(new Underline { Val = underline }); if (!string.IsNullOrWhiteSpace(Color)) list.Add(new Color { Val = Color }); if (SubSup is { } subsup) list.Add(new VerticalTextAlignment { Val = subsup }); return list.Count > 0; } public bool TrySupExtract(out List list) { list = []; if (!string.IsNullOrWhiteSpace(FontFamily)) list.Add(new RunFonts { Ascii = FontFamily, HighAnsi = FontFamily }); if (_SizeD is { } sizeD) list.Add(new FontSize { Val = sizeD.ToString() }); if (IsBold is { } isBold && isBold) list.Add(new Bold()); if (IsItalic is { } isItalic && isItalic) list.Add(new Italic()); if (Underline is { } underline) list.Add(new Underline { Val = underline }); if (!string.IsNullOrWhiteSpace(Color)) list.Add(new Color { Val = Color }); list.Add(new VerticalTextAlignment { Val = VerticalPositionValues.Superscript }); return list.Count > 0; } public bool TrySubExtract(out List list) { list = []; if (!string.IsNullOrWhiteSpace(FontFamily)) list.Add(new RunFonts { Ascii = FontFamily, HighAnsi = FontFamily }); if (_SizeD is { } sizeD) list.Add(new FontSize { Val = sizeD.ToString() }); if (IsBold is { } isBold && isBold) list.Add(new Bold()); if (IsItalic is { } isItalic && isItalic) list.Add(new Italic()); if (Underline is { } underline) list.Add(new Underline { Val = underline }); if (!string.IsNullOrWhiteSpace(Color)) list.Add(new Color { Val = Color }); list.Add(new VerticalTextAlignment { Val = VerticalPositionValues.Subscript }); return list.Count > 0; } public bool TryExtractForMath(out List list) { list = []; // Для математики используем только элемент Style if (IsBold is { } isBold && isBold) list.Add(new MathStyle { Val = isBold ? MathStyleValues.BoldItalic : MathStyleValues.Italic }); // Обычное начертание можно не добавлять, так как по умолчанию и так обычное // (но если нужно явно сбросить, можно добавить Style { Val = StyleValues.Plain }) return list.Count > 0; } // Фабричные методы для создания FontProps public static FontProps Default => new(); public static FontProps WithFont(string fontFamily, double? size = null) => new(size) { FontFamily = fontFamily }; public static FontProps WithSize(double size) => new(size); public static FontProps WithStyle(bool bold = false, bool italic = false) => new() { IsBold = bold, IsItalic = italic }; public static FontProps WithColor(string hexColor) => new() { Color = hexColor }; public FontProps Override(FontProps? second) { if (second is null) return this; return new FontProps { FontFamily = second.FontFamily ?? FontFamily, Size = second.Size ?? Size, IsItalic = second.IsItalic ?? IsItalic, IsBold = second.IsBold ?? IsBold, Underline = second.Underline ?? Underline, SubSup = second.SubSup ?? SubSup, Color = second.Color ?? Color, }; } }