forked from wmjordan/PDFPatcher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBound.cs
243 lines (226 loc) · 7.14 KB
/
Bound.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
using System;
using System.Diagnostics;
namespace PDFPatcher.Model
{
[DebuggerDisplay("T={Top},L={Left},B={Bottom},R={Right}; H={Height},W={Width}")]
public class Bound
{
internal float Top { get; private set; }
internal float Left { get; private set; }
internal float Bottom { get; private set; }
internal float Right { get; private set; }
/// <summary>
/// 获取此区域坐标是否属于笛卡尔坐标系。
/// </summary>
internal bool IsTopUp { get; private set; }
/// <summary>
/// 获取此坐标区域是否属于绘图坐标系。
/// </summary>
internal bool IsTopDown { get; private set; }
internal float Height { get; private set; }
internal float Width { get; private set; }
internal float Middle { get; private set; }
internal float Center { get; private set; }
private Bound() {
IsTopDown = true;
IsTopUp = true;
}
public Bound(float left, float bottom, float right, float top) {
if (right < left) {
Debug.WriteLine("右端坐标不能小于左端坐标。");
var t = right;
right = left;
left = t;
}
Left = left;
Bottom = bottom;
Right = right;
Top = top;
RecalculateSize();
}
/// <summary>
/// 创建宽度和高度均为 0 的区域(点)实例。
/// </summary>
/// <param name="x">横坐标。</param>
/// <param name="y">纵坐标。</param>
public Bound(float x, float y) : this(x, y, x, y) {
}
/// <summary>
/// 从指定区域复制副本。
/// </summary>
/// <param name="source">需要复制副本的区域。</param>
public Bound(Bound source) : this(source.Left, source.Bottom, source.Right, source.Top) {
}
private void RecalculateSize() {
IsTopUp = Top >= Bottom;
IsTopDown = Top <= Bottom;
Height = IsTopUp ? Top - Bottom : Bottom - Top;
Middle = (Top + Bottom) / 2;
Width = Right - Left;
Center = (Left + Right) / 2;
}
internal Bound Merge(Bound source) {
// 笛卡尔坐标
if (IsTopUp) {
if (Top < source.Top) {
Top = source.Top;
}
if (Bottom > source.Bottom) {
Bottom = source.Bottom;
}
}
else {
if (Top > source.Top) {
Top = source.Top;
}
if (Bottom < source.Bottom) {
Bottom = source.Bottom;
}
}
if (Left > source.Left) {
Left = source.Left;
}
if (Right < source.Right) {
Right = source.Right;
}
RecalculateSize();
return this;
}
/// <summary>
/// 获取区域 <paramref name="other"/> 到当前区域之间的距离。
/// </summary>
/// <param name="other">另一个区域。</param>
/// <param name="writingDirection">假设书写方向。</param>
/// <returns><paramref name="other"/> 相对于此区域的距离关系。</returns>
internal DistanceInfo GetDistance(Bound other, WritingDirection writingDirection) {
if (IsTopDown != other.IsTopDown && IsTopUp != other.IsTopUp) {
throw new ArgumentException("区域坐标系不同。");
}
float hd = float.MaxValue, vd = float.MaxValue;
var hp = DistanceInfo.Placement.Unknown;
var vp = DistanceInfo.Placement.Unknown;
float au, ad, bu, bd;
if (IsTopDown) {
au = Top;
ad = Bottom;
bu = other.Top;
bd = other.Bottom;
}
else {
au = -Top;
ad = -Bottom;
bu = -other.Top;
bd = -other.Bottom;
}
bool ov = false;
if (IntersectWith(other)) {
ov = true;
hd = other.Center - Center;
if (hd > 0) {
hp = DistanceInfo.Placement.Right;
}
else if (hd < 0) {
hp = DistanceInfo.Placement.Left;
hd = -hd;
}
vd = other.Middle - Middle;
if (vd > 0) {
vp = IsTopUp ? DistanceInfo.Placement.Up : DistanceInfo.Placement.Down;
}
else if (vd < 0) {
vp = IsTopUp ? DistanceInfo.Placement.Down : DistanceInfo.Placement.Up;
}
if (vd == 0 && hd == 0) {
return new DistanceInfo(DistanceInfo.Placement.Overlapping, 0, 0);
}
else if (vd == 0) {
return new DistanceInfo(DistanceInfo.Placement.Overlapping | hp, hd, vd);
}
else if (hp == 0) {
return new DistanceInfo(DistanceInfo.Placement.Overlapping | vp, hd, vd);
}
else {
return new DistanceInfo(DistanceInfo.Placement.Overlapping, hd, vd);
}
}
if (other.Left >= Right) {
hp = DistanceInfo.Placement.Right;
hd = other.Left - Right;
}
else if (other.Right <= Left) {
hp = DistanceInfo.Placement.Left;
hd = Left - other.Right;
}
if (bd <= au) {
vp = DistanceInfo.Placement.Up;
vd = au - bd;
}
else if (bu >= ad) {
vp = DistanceInfo.Placement.Down;
vd = bu - ad;
}
if (hp == DistanceInfo.Placement.Unknown && vp == DistanceInfo.Placement.Unknown) {
throw new ArgumentOutOfRangeException("位置错误。");
}
var v = new DistanceInfo(ov ? DistanceInfo.Placement.Overlapping | vp : vp, hd, vd);
var h = new DistanceInfo(ov ? DistanceInfo.Placement.Overlapping | hp : hp, hd, vd);
return writingDirection switch {
WritingDirection.Vertical => hp != DistanceInfo.Placement.Unknown ? h : v,
WritingDirection.Horizontal => vp != DistanceInfo.Placement.Unknown ? v : h,
_ => (hd < vd) ? h : v
};
}
/// <summary>
/// 返回当前区域是否和指定区域在同一行上(中心点是否落在 <paramref name="other"/> 的两个边缘之间)。
/// </summary>
/// <param name="other">需要比较的区域。</param>
/// <param name="direction">比较方向。</param>
/// <returns>在同一行上时返回 true。</returns>
internal bool IsAlignedWith(Bound other, WritingDirection direction) {
switch (direction) {
case WritingDirection.Horizontal:
return IsTopDown ? (other.Top < Middle && Middle < other.Bottom || Top < other.Middle && other.Middle < Bottom) : (other.Bottom < Middle && Middle < other.Top || Bottom < other.Middle && other.Middle < Top);
case WritingDirection.Vertical:
return other.Left < Center && Center < other.Right
|| Left < other.Center && other.Center < Right;
default:
return IntersectWith(other);
}
}
internal bool IntersectWith(Bound other) {
return other.Left < Right && Left < other.Right &&
(IsTopDown
? (other.Top < Bottom && Top < other.Bottom)
: (other.Bottom < Top && Bottom < other.Top));
}
internal bool Contains(float x, float y) {
float x1, x2, y1, y2;
x1 = Left;
x2 = Right;
if (IsTopUp) {
y1 = Bottom;
y2 = Top;
}
else {
y1 = Top;
y2 = Bottom;
}
return x1 <= x && x <= x2 && y1 <= y && y <= y2;
}
public static bool operator ==(Bound a, Bound b) {
return a.Top == b.Top && a.Bottom == b.Bottom && a.Left == b.Left && a.Right == b.Right;
}
public static bool operator !=(Bound a, Bound b) {
return a.Top != b.Top || a.Bottom != b.Bottom || a.Left != b.Left || a.Right != b.Right;
}
public override bool Equals(object obj) {
return this == (Bound)obj;
}
public override int GetHashCode() {
return Top.GetHashCode() ^ Bottom.GetHashCode() ^ Left.GetHashCode() ^ Right.GetHashCode();
}
public static implicit operator System.Drawing.RectangleF(Bound bound) {
return new System.Drawing.RectangleF(Math.Min(bound.Left, bound.Right), Math.Min(bound.Top, bound.Bottom), Math.Abs(bound.Left - bound.Right), Math.Abs(bound.Top - bound.Bottom));
}
}
}