Skip to content

Commit de12741

Browse files
authored
Merge pull request #15 from dotnet-campus/t/lindexi/OpenXml
加上对接 OpenXML 的定义类型
2 parents 85b9733 + 3c4a705 commit de12741

12 files changed

Lines changed: 470 additions & 9 deletions

File tree

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
using System;
2+
3+
namespace dotnetCampus.OpenXmlUnitConverter
4+
{
5+
/// <summary>
6+
/// 采用 <see cref="Emu"/> 表示的点
7+
/// </summary>
8+
public readonly struct EmuPoint : IEquatable<EmuPoint>
9+
{
10+
/// <summary>
11+
/// 创建 <see cref="Emu"/> 表示的点
12+
/// </summary>
13+
/// <param name="x"></param>
14+
/// <param name="y"></param>
15+
public EmuPoint(Emu x, Emu y)
16+
{
17+
X = x;
18+
Y = y;
19+
}
20+
21+
/// <summary>
22+
/// 表示 X 坐标
23+
/// </summary>
24+
public Emu X { get; }
25+
26+
/// <summary>
27+
/// 表示 Y 坐标
28+
/// </summary>
29+
public Emu Y { get; }
30+
31+
/// <inheritdoc />
32+
public bool Equals(EmuPoint other)
33+
{
34+
return X.Equals(other.X) && Y.Equals(other.Y);
35+
}
36+
37+
/// <inheritdoc />
38+
public override bool Equals(object? obj)
39+
{
40+
return obj is EmuPoint other && Equals(other);
41+
}
42+
43+
/// <inheritdoc />
44+
public override int GetHashCode()
45+
{
46+
unchecked
47+
{
48+
return (X.GetHashCode() * 397) ^ Y.GetHashCode();
49+
}
50+
}
51+
52+
/// <summary>
53+
/// 判断相等
54+
/// </summary>
55+
/// <param name="left"></param>
56+
/// <param name="right"></param>
57+
/// <returns></returns>
58+
public static bool operator ==(EmuPoint left, EmuPoint right)
59+
{
60+
return left.Equals(right);
61+
}
62+
63+
/// <summary>
64+
/// 判断不相等
65+
/// </summary>
66+
/// <param name="left"></param>
67+
/// <param name="right"></param>
68+
/// <returns></returns>
69+
public static bool operator !=(EmuPoint left, EmuPoint right)
70+
{
71+
return !left.Equals(right);
72+
}
73+
74+
/// <inheritdoc />
75+
public override string ToString() => $"X={X} ;Y={Y}";
76+
}
77+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
using System;
2+
3+
#pragma warning disable CS1591 // 缺少对公共可见类型或成员的 XML 注释
4+
5+
namespace dotnetCampus.OpenXmlUnitConverter
6+
{
7+
/// <summary>
8+
/// 采用 <see cref="Emu"/> 表示的矩形
9+
/// </summary>
10+
public readonly struct EmuRectangle : IEquatable<EmuRectangle>
11+
{
12+
/// <summary>
13+
/// 创建使用 <see cref="Emu"/> 表示的矩形
14+
/// </summary>
15+
public EmuRectangle(Emu left, Emu top, Emu right, Emu bottom)
16+
{
17+
Left = left;
18+
Top = top;
19+
Right = right;
20+
Bottom = bottom;
21+
}
22+
23+
/// <summary>
24+
/// 创建使用 <see cref="Emu"/> 表示的矩形
25+
/// </summary>
26+
public EmuRectangle(EmuPoint point, EmuSize size)
27+
{
28+
Left = point.X;
29+
Top = point.Y;
30+
Right = new Emu(point.X.Value + size.Width.Value);
31+
Bottom = new Emu(point.Y.Value + size.Height.Value);
32+
}
33+
34+
public Emu Left { get; }
35+
public Emu Top { get; }
36+
37+
public Emu Right { get; }
38+
public Emu Bottom { get; }
39+
40+
public Emu Width => new Emu(Right.Value - Left.Value);
41+
public Emu Height => new Emu(Bottom.Value - Top.Value);
42+
43+
public EmuPoint LeftTop => new EmuPoint(Left, Top);
44+
public EmuPoint RightBottom => new EmuPoint(Right, Bottom);
45+
public EmuSize Size => new EmuSize(Width, Height);
46+
47+
public bool Equals(EmuRectangle other)
48+
{
49+
return Left.Equals(other.Left)
50+
&& Top.Equals(other.Top)
51+
&& Right.Equals(other.Right)
52+
&& Bottom.Equals(other.Bottom);
53+
}
54+
55+
public override bool Equals(object? obj)
56+
{
57+
return obj is EmuRectangle other && Equals(other);
58+
}
59+
60+
public override int GetHashCode()
61+
{
62+
unchecked
63+
{
64+
var hashCode = Left.GetHashCode();
65+
hashCode = (hashCode * 397) ^ Top.GetHashCode();
66+
hashCode = (hashCode * 397) ^ Right.GetHashCode();
67+
hashCode = (hashCode * 397) ^ Bottom.GetHashCode();
68+
return hashCode;
69+
}
70+
}
71+
72+
/// <summary>
73+
/// 判断相等
74+
/// </summary>
75+
public static bool operator ==(in EmuRectangle left,in EmuRectangle right)
76+
{
77+
return left.Equals(right);
78+
}
79+
80+
/// <summary>
81+
/// 判断不相等
82+
/// </summary>
83+
public static bool operator !=(in EmuRectangle left, in EmuRectangle right)
84+
{
85+
return !left.Equals(right);
86+
}
87+
88+
/// <inheritdoc />
89+
public override string ToString() =>
90+
$"{LeftTop} ;{Size}";
91+
}
92+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
using System;
2+
3+
namespace dotnetCampus.OpenXmlUnitConverter
4+
{
5+
/// <summary>
6+
/// 采用 <see cref="Emu"/> 表示的尺寸
7+
/// </summary>
8+
public readonly struct EmuSize : IEquatable<EmuSize>
9+
{
10+
/// <summary>
11+
/// 创建使用 EMU 单位表示的 Size 尺寸
12+
/// </summary>
13+
/// <param name="width"></param>
14+
/// <param name="height"></param>
15+
public EmuSize(Emu width, Emu height)
16+
{
17+
Width = width;
18+
Height = height;
19+
}
20+
21+
/// <summary>
22+
/// 宽度
23+
/// </summary>
24+
public Emu Width { get; }
25+
26+
/// <summary>
27+
/// 高度
28+
/// </summary>
29+
public Emu Height { get; }
30+
31+
/// <inheritdoc />
32+
public bool Equals(EmuSize other)
33+
{
34+
return Width.Equals(other.Width) && Height.Equals(other.Height);
35+
}
36+
37+
/// <inheritdoc />
38+
public override bool Equals(object? obj)
39+
{
40+
return obj is EmuSize other && Equals(other);
41+
}
42+
43+
/// <inheritdoc />
44+
public override int GetHashCode()
45+
{
46+
unchecked
47+
{
48+
return (Width.GetHashCode() * 397) ^ Height.GetHashCode();
49+
}
50+
}
51+
52+
/// <summary>
53+
/// 判断相等
54+
/// </summary>
55+
public static bool operator ==(in EmuSize left, in EmuSize right)
56+
{
57+
return left.Equals(right);
58+
}
59+
60+
/// <summary>
61+
/// 判断不相等
62+
/// </summary>
63+
public static bool operator !=(in EmuSize left, in EmuSize right)
64+
{
65+
return !left.Equals(right);
66+
}
67+
68+
/// <inheritdoc />
69+
public override string ToString() => $"W={Width} ;H={Height}";
70+
}
71+
}

src/dotnetCampus.OpenXMLUnitConverter/MillisecondTime.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,5 +89,8 @@ public TimeSpan ToTimeSpan() => IsIndefinite
8989
: TimeSpan.FromMilliseconds(Milliseconds);
9090

9191
private const int TicksPerMillisecond = 10000;
92+
93+
/// <inheritdoc />
94+
public override string ToString() => $"Millisecond:{Milliseconds}";
9295
}
9396
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#pragma warning disable CS1591 // 缺少对公共可见类型或成员的 XML 注释
2+
3+
using System;
4+
using DocumentFormat.OpenXml;
5+
6+
namespace dotnetCampus.OpenXmlUnitConverter
7+
{
8+
public static class OpenXmlUnitConverter
9+
{
10+
public static Int64Value ToOpenXmlInt64Value(this Emu emu)
11+
{
12+
return new Int64Value((long)Math.Round(emu.Value));
13+
}
14+
15+
public static Int32Value ToOpenXmlInt32Value(this Emu emu)
16+
{
17+
return new Int32Value((int)Math.Round(emu.Value));
18+
}
19+
}
20+
}

src/dotnetCampus.OpenXmlUnitConverter/Emu.cs

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
namespace dotnetCampus.OpenXmlUnitConverter
1+
using System;
2+
3+
namespace dotnetCampus.OpenXmlUnitConverter
24
{
35
/// <summary>
46
/// 英制公制单位(English Metric Unit)。用于对接厘米(<see cref="Cm"/>)和英寸(<see cref="Inch"/>)的虚拟单位。
57
/// <para>其特殊的数值设计,便于让你在转换百位以内的英寸和毫米、像素长度时,不会产生小数。</para>
68
/// <para>1 <see cref="Inch"/> = 914400 <see cref="Emu"/></para>
79
/// </summary>
8-
public readonly struct Emu
10+
public readonly struct Emu : IEquatable<Emu>
911
{
1012
/// <summary>
1113
/// 创建 <see cref="Emu"/> 单位。
@@ -24,6 +26,49 @@ public Emu(double value)
2426
/// <summary>
2527
/// 表示已初始化为零的 <see cref="Emu"/> 长度。
2628
/// </summary>
27-
public static readonly Emu Zero = new Emu(0);
29+
public static Emu Zero => new Emu(0);
30+
31+
/// <inheritdoc />
32+
public override string ToString() => $"EMU: {Value:0.00}";
33+
34+
/// <inheritdoc />
35+
public bool Equals(Emu other)
36+
{
37+
return Value.Equals(other.Value);
38+
}
39+
40+
/// <inheritdoc />
41+
public override bool Equals(object? obj)
42+
{
43+
return obj is Emu other && Equals(other);
44+
}
45+
46+
/// <inheritdoc />
47+
public override int GetHashCode()
48+
{
49+
return Value.GetHashCode();
50+
}
51+
52+
/// <summary>
53+
/// 判断相等
54+
/// </summary>
55+
/// <param name="left"></param>
56+
/// <param name="right"></param>
57+
/// <returns></returns>
58+
public static bool operator ==(Emu left, Emu right)
59+
{
60+
return left.Equals(right);
61+
}
62+
63+
/// <summary>
64+
/// 判断不相等
65+
/// </summary>
66+
/// <param name="left"></param>
67+
/// <param name="right"></param>
68+
/// <returns></returns>
69+
public static bool operator !=(Emu left, Emu right)
70+
{
71+
return !left.Equals(right);
72+
}
2873
}
2974
}

src/dotnetCampus.OpenXmlUnitConverter/Pixel.cs

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
namespace dotnetCampus.OpenXmlUnitConverter
1+
using System;
2+
3+
namespace dotnetCampus.OpenXmlUnitConverter
24
{
35
/// <summary>
46
/// 像素
57
/// </summary>
6-
public readonly struct Pixel
8+
public readonly struct Pixel : IEquatable<Pixel>
79
{
810
/// <summary>
911
/// 像素
@@ -22,6 +24,27 @@ public Pixel(double value)
2224
/// <summary>
2325
/// 表示值是 0 的像素
2426
/// </summary>
25-
public static readonly Pixel ZeroPixel = new Pixel(0);
27+
public static Pixel ZeroPixel => new Pixel(0);
28+
29+
/// <inheritdoc />
30+
public override string ToString() => $"Pixel:{Value:0.00}";
31+
32+
/// <inheritdoc />
33+
public bool Equals(Pixel other)
34+
{
35+
return Value.Equals(other.Value);
36+
}
37+
38+
/// <inheritdoc />
39+
public override bool Equals(object? obj)
40+
{
41+
return obj is Pixel other && Equals(other);
42+
}
43+
44+
/// <inheritdoc />
45+
public override int GetHashCode()
46+
{
47+
return Value.GetHashCode();
48+
}
2649
}
2750
}

0 commit comments

Comments
 (0)