OSDN Git Service

7e32fbe345481796175cd4744ff2097b3a1e58f5
[fooeditengine/FooEditEngine.git] / Core / TextPoint.cs
1 /*
2  * Copyright (C) 2013 FooProject
3  * * This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
5
6  * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of 
7  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
8
9 You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.
10  */
11 using System;
12
13 namespace FooEditEngine
14 {
15     struct SrcPoint
16     {
17         public double X;
18         public int Row;
19         public double Y;
20         public double GetOffsetY(double line_height)
21         {
22             return this.Row * line_height - this.Y;
23         }
24         public SrcPoint(double x, int row, double y)
25         {
26             if (row < 0)
27                 throw new ArgumentOutOfRangeException("マイナスを値を指定することはできません");
28             this.X = x;
29             this.Row = row;
30             this.Y = y;
31         }
32     }
33
34     /// <summary>
35     /// テキストの範囲を表す
36     /// </summary>
37     public struct TextRange
38     {
39         /// <summary>
40         /// 開始インデックス
41         /// </summary>
42         public int Index;
43         /// <summary>
44         /// 長さ
45         /// </summary>
46         public int Length;
47
48         /// <summary>
49         /// 空の範囲を表す
50         /// </summary>
51         public static TextRange Null = new TextRange(0, 0);
52
53         /// <summary>
54         /// コンストラクター
55         /// </summary>
56         /// <param name="index">開始インデックス</param>
57         /// <param name="length">長さ</param>
58         public TextRange(int index, int length)
59         {
60             this.Index = index;
61             this.Length = length;
62         }
63     }
64
65     /// <summary>
66     /// 文章の位置を示すクラス
67     /// </summary>
68     public struct TextPoint : IComparable<TextPoint>
69     {
70         /// <summary>
71         /// 行番号
72         /// </summary>
73         public int row;
74         /// <summary>
75         /// 桁
76         /// </summary>
77         public int col;
78
79         /// <summary>
80         /// TextPointがドキュメント上のどこも指していないことを表す
81         /// </summary>
82         public static readonly TextPoint Null = new TextPoint(-1,-1);
83
84         /// <summary>
85         /// コンストラクター
86         /// </summary>
87         /// <param name="row">行番号</param>
88         /// <param name="col">桁</param>
89         public TextPoint(int row, int col)
90         {
91             this.row = row;
92             this.col = col;
93         }
94
95         /// <summary>
96         /// 比較演算子を実装します
97         /// </summary>
98         /// <param name="a">比較される方</param>
99         /// <param name="b">比較対象</param>
100         /// <returns>条件を満たすなら真</returns>
101         public static bool operator <(TextPoint a, TextPoint b)
102         {
103             return a.CompareTo(b) == -1;
104         }
105
106         /// <summary>
107         /// 比較演算子を実装します
108         /// </summary>
109         /// <param name="a">比較される方</param>
110         /// <param name="b">比較対象</param>
111         /// <returns>条件を満たすなら真</returns>
112         public static bool operator <=(TextPoint a, TextPoint b)
113         {
114             return a.CompareTo(b) != 1;
115         }
116
117         /// <summary>
118         /// 比較演算子を実装します
119         /// </summary>
120         /// <param name="a">比較される方</param>
121         /// <param name="b">比較対象</param>
122         /// <returns>条件を満たすなら真</returns>
123         public static bool operator >(TextPoint a, TextPoint b)
124         {
125             return a.CompareTo(b) == 1;
126         }
127
128         /// <summary>
129         /// 比較演算子を実装します
130         /// </summary>
131         /// <param name="a">比較される方</param>
132         /// <param name="b">比較対象</param>
133         /// <returns>条件を満たすなら真</returns>
134         public static bool operator >=(TextPoint a, TextPoint b)
135         {
136             return a.CompareTo(b) != -1;
137         }
138
139         /// <summary>
140         /// 比較演算子を実装します
141         /// </summary>
142         /// <param name="a">比較される方</param>
143         /// <param name="b">比較対象</param>
144         /// <returns>条件を満たすなら真</returns>
145         public static bool operator ==(TextPoint a, TextPoint b)
146         {
147             return a.Equals(b);
148         }
149
150         /// <summary>
151         /// 比較演算子を実装します
152         /// </summary>
153         /// <param name="a">比較される方</param>
154         /// <param name="b">比較対象</param>
155         /// <returns>条件を満たすなら真</returns>
156         public static bool operator !=(TextPoint a, TextPoint b)
157         {
158             return !a.Equals(b);
159         }
160
161         /// <summary>
162         /// 一致するかどうか
163         /// </summary>
164         /// <param name="o">比較対象</param>
165         /// <returns>一致するなら真</returns>
166         public override bool Equals(object o)
167         {
168             TextPoint b = (TextPoint)o;
169             return this.col == b.col && this.row == b.row;
170         }
171
172         /// <summary>
173         /// ハッシュを返す
174         /// </summary>
175         /// <returns>ハッシュを返す</returns>
176         public override int GetHashCode()
177         {
178             int result = this.row.GetHashCode();
179             result ^= this.col.GetHashCode();
180             return result;
181         }
182
183         #region IComparable<TextPoint> メンバー
184
185         /// <summary>
186         /// 比較する
187         /// </summary>
188         /// <param name="other">比較対象となるTextPointオブジェクト</param>
189         /// <returns>相対値を返す</returns>
190         public int CompareTo(TextPoint other)
191         {
192             if (this.row == other.row && this.col == other.col)
193                 return 0;
194             if (this.row < other.row)
195                 return -1;
196             else if (this.row == other.row && this.col < other.col)
197                 return -1;
198             else
199                 return 1;
200         }
201
202         #endregion
203     }
204
205     /// <summary>
206     /// 文章上での矩形エリアを表す
207     /// </summary>
208     public struct TextRectangle
209     {
210         TextPoint _TopLeft;
211         
212         TextPoint _BottomRight;
213
214         /// <summary>
215         /// 矩形エリアがドキュメントのどこも指していないことを表す
216         /// </summary>
217         public static readonly TextRectangle Null = new TextRectangle(new TextPoint(-1,-1),new TextPoint(-1,-1));
218
219         /// <summary>
220         /// 左上を表す
221         /// </summary>
222         public TextPoint TopLeft
223         {
224             get
225             {
226                 return this._TopLeft;
227             }
228         }
229
230         /// <summary>
231         /// 右上を表す
232         /// </summary>
233         public TextPoint TopRight
234         {
235             get
236             {
237                 return new TextPoint(this._TopLeft.row, this._BottomRight.col);
238             }
239         }
240
241         /// <summary>
242         /// 左下を表す
243         /// </summary>
244         public TextPoint BottomLeft
245         {
246             get
247             {
248                 return new TextPoint(this._BottomRight.row, this._TopLeft.col);
249             }
250         }
251
252         /// <summary>
253         /// 右下を表す
254         /// </summary>
255         public TextPoint BottomRight
256         {
257             get
258             {
259                 return this._BottomRight;
260             }
261         }
262
263         /// <summary>
264         /// コンストラクター
265         /// </summary>
266         /// <param name="row">行</param>
267         /// <param name="col">桁</param>
268         /// <param name="height">高さ</param>
269         /// <param name="width">幅</param>
270         public TextRectangle(int row, int col, int height,int width)
271         {
272             this._TopLeft = new TextPoint(row, col);
273             this._BottomRight = new TextPoint(row + height - 1, col + width - 1);
274         }
275         
276         /// <summary>
277         /// コンストラクター
278         /// </summary>
279         /// <param name="topLeft">矩形の左上</param>
280         /// <param name="bottomRight">矩形の右下</param>
281         public TextRectangle(TextPoint topLeft, TextPoint bottomRight)
282         {
283             this._TopLeft = topLeft;
284             this._BottomRight = bottomRight;
285         }
286
287         /// <summary>
288         /// 比較演算子を実装します
289         /// </summary>
290         /// <param name="a">比較される方</param>
291         /// <param name="b">比較対象</param>
292         /// <returns>条件を満たすなら真</returns>
293         public static bool operator ==(TextRectangle a, TextRectangle b)
294         {
295             return a.Equals(b);
296         }
297
298         /// <summary>
299         /// 比較演算子を実装します
300         /// </summary>
301         /// <param name="a">比較される方</param>
302         /// <param name="b">比較対象</param>
303         /// <returns>条件を満たすなら真</returns>
304         public static bool operator !=(TextRectangle a, TextRectangle b)
305         {
306             return !a.Equals(b);
307         }
308
309         /// <summary>
310         /// 一致するかどうか
311         /// </summary>
312         /// <param name="o">比較対象</param>
313         /// <returns>一致するなら真</returns>
314         public override bool Equals(object o)
315         {
316             TextRectangle b = (TextRectangle)o;
317             return this._TopLeft == b._TopLeft && this._BottomRight == b._BottomRight;
318         }
319
320         /// <summary>
321         /// ハッシュを返す
322         /// </summary>
323         /// <returns>ハッシュを返す</returns>
324         public override int GetHashCode()
325         {
326             int result = this._TopLeft.GetHashCode();
327             result ^= this._BottomRight.GetHashCode();
328             return result;
329         }
330     }
331 }