Skip to content

Commit b0c4829

Browse files
authored
Merge pull request #18 from dotnet-campus/t/lindexi/PixelSize
加上像素尺寸支持
2 parents 5662a4a + d5ce18a commit b0c4829

3 files changed

Lines changed: 105 additions & 0 deletions

File tree

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+
/// 像素尺寸
7+
/// </summary>
8+
public readonly struct PixelSize : IEquatable<PixelSize>
9+
{
10+
/// <summary>
11+
/// 像素尺寸
12+
/// </summary>
13+
/// <param name="width"></param>
14+
/// <param name="height"></param>
15+
public PixelSize(Pixel width, Pixel height)
16+
{
17+
Width = width;
18+
Height = height;
19+
}
20+
21+
/// <summary>
22+
/// 宽度
23+
/// </summary>
24+
public Pixel Width { get; }
25+
26+
/// <summary>
27+
/// 高度
28+
/// </summary>
29+
public Pixel Height { get; }
30+
31+
/// <inheritdoc />
32+
public bool Equals(PixelSize 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 PixelSize 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 ==(PixelSize left, PixelSize right)
56+
{
57+
return left.Equals(right);
58+
}
59+
60+
/// <summary>
61+
/// 判断不相等
62+
/// </summary>
63+
public static bool operator !=(PixelSize left, PixelSize 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/UnitConverter.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,5 +206,15 @@ public static EmuPoint ToEmuPoint(this PixelPoint pixelPoint) =>
206206
new EmuPoint(pixelPoint.X.ToEmu(), pixelPoint.Y.ToEmu());
207207

208208
#endregion
209+
210+
#region PixelSize
211+
212+
public static PixelSize ToPixelSize(this EmuSize emuPoint) =>
213+
new PixelSize(emuPoint.Width.ToPixel(), emuPoint.Height.ToPixel());
214+
215+
public static EmuSize ToEmuSize(this PixelSize pixelPoint) =>
216+
new EmuSize(pixelPoint.Width.ToEmu(), pixelPoint.Height.ToEmu());
217+
218+
#endregion
209219
}
210220
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using Microsoft.VisualStudio.TestTools.UnitTesting;
2+
using MSTest.Extensions.Contracts;
3+
4+
namespace dotnetCampus.OpenXmlUnitConverter.Tests
5+
{
6+
[TestClass]
7+
public class PixelSizeTest
8+
{
9+
[ContractTestCase]
10+
public void Convert()
11+
{
12+
"传入 EMU 表示的尺寸,可以转换为像素表示的尺寸,反向互转值相同".Test(() =>
13+
{
14+
var emuSize = new EmuSize(new Emu(952500), new Emu(95250));
15+
var pixelSize = emuSize.ToPixelSize();
16+
17+
Assert.AreEqual(100, pixelSize.Width.Value);
18+
Assert.AreEqual(10, pixelSize.Height.Value);
19+
20+
Assert.AreEqual(emuSize, pixelSize.ToEmuSize());
21+
});
22+
}
23+
}
24+
}

0 commit comments

Comments
 (0)