Files
QWERTYkez.OpenXmlProcessors/QWERTYkez.WordProcessor/Builders/CellProps.cs
T

124 lines
4.8 KiB
C#

namespace QWERTYkez.WordProcessor;
public readonly struct CellProps(double? width = null!)
{
public double? Width { get; init; } = width;
public VerticalAlignments? VerticalAlignment { get; init; } = default;
public MergedCell? Merge { get; init; } = default;
public TextDirection TextDirection { get; init; } = TextDirection.LeftToRight;
#pragma warning disable IDE1006 // Стили именования
public static CellProps V_H_ { get; } = new() { Merge = MergedCell.V_H_ };
public static CellProps H_ { get; } = new() { Merge = MergedCell.H_ };
public static CellProps V_ { get; } = new() { Merge = MergedCell.V_ };
public static CellProps _V_H { get; } = new() { Merge = MergedCell._V_H };
public static CellProps _H { get; } = new() { Merge = MergedCell._H };
public static CellProps _V { get; } = new() { Merge = MergedCell._V };
public static CellProps _VH_ { get; } = new() { Merge = MergedCell._VH_ };
public static CellProps V__H { get; } = new() { Merge = MergedCell.V__H };
#pragma warning restore IDE1006 // Стили именования
internal bool TryExtract(out List<OpenXmlElement> list)
{
list =
[
new TableCellVerticalAlignment()
{
Val = (VerticalAlignment ?? VerticalAlignments.Center) switch
{
VerticalAlignments.Top => TableVerticalAlignmentValues.Top,
VerticalAlignments.Center => TableVerticalAlignmentValues.Center,
VerticalAlignments.Bottom => TableVerticalAlignmentValues.Bottom,
_ => throw new NotImplementedException(),
}
},
new TableCellMargin()
{
TopMargin = new TopMargin() { Width = "0" },
BottomMargin = new BottomMargin() { Width = "0" },
LeftMargin = new LeftMargin() { Width = "0" },
RightMargin = new RightMargin() { Width = "0" }
}
];
if (Width.HasValue)
list.Add(new TableCellWidth()
{
Type = TableWidthUnitValues.Dxa,
Width = ((uint)(Width.Value * 567d)).ToString() // 1 см = 567 DXA
});
if (TextDirection is not TextDirection.LeftToRight)
{
list.Add(new DocumentFormat.OpenXml.Wordprocessing.TextDirection
{
Val = TextDirection switch
{
TextDirection.BottomToTop => TextDirectionValues.BottomToTopLeftToRight,
TextDirection.TopToBottom => TextDirectionValues.TopToBottomRightToLeft,
_ => throw new ArgumentOutOfRangeException(nameof(TextDirection), TextDirection, "Unsupported text direction"),
}
});
}
if (Merge.HasValue)
{
switch (Merge.Value)
{
case MergedCell.V_H_:
list.Add(new HorizontalMerge() { Val = MergedCellValues.Restart });
list.Add(new VerticalMerge() { Val = MergedCellValues.Restart });
break;
case MergedCell.H_:
list.Add(new HorizontalMerge() { Val = MergedCellValues.Restart }); break;
case MergedCell.V_:
list.Add(new VerticalMerge() { Val = MergedCellValues.Restart }); break;
case MergedCell._V_H:
list.Add(new HorizontalMerge() { Val = MergedCellValues.Continue });
list.Add(new VerticalMerge() { Val = MergedCellValues.Continue });
break;
case MergedCell._H:
list.Add(new HorizontalMerge() { Val = MergedCellValues.Continue }); break;
case MergedCell._V:
list.Add(new VerticalMerge() { Val = MergedCellValues.Continue }); break;
case MergedCell._VH_:
list.Add(new HorizontalMerge() { Val = MergedCellValues.Restart });
list.Add(new VerticalMerge() { Val = MergedCellValues.Continue });
break;
case MergedCell.V__H:
list.Add(new HorizontalMerge() { Val = MergedCellValues.Continue });
list.Add(new VerticalMerge() { Val = MergedCellValues.Restart });
break;
}
}
return list.Count > 0;
}
}
public enum MergedCell
{
V_H_,
H_,
V_,
_V_H,
_H,
_V,
_VH_,
V__H,
}
public enum TextDirection
{
/// <summary> ⇒ ⇒ ⇒ Стандартный свева направо </summary>
LeftToRight,
/// <summary> ⇑ ⇑ ⇑ Снизу вверх </summary>
BottomToTop,
/// <summary> ⇓ ⇓ ⇓ Сверху вниз </summary>
TopToBottom,
}
public enum VerticalAlignments { Top, Center, Bottom }