OSDN Git Service

f1410262a335027f64d04f3ef42b12c791ed8b3b
[fooeditengine/FooEditEngine.git] / Core / ITextRender.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 using System.Collections.Generic;
13
14 namespace FooEditEngine
15 {
16     struct Point
17     {
18         public double X;
19         public double Y;
20         public Point(double x, double y)
21         {
22             this.X = x;
23             this.Y = y;
24         }
25         /// <summary>
26         /// 比較演算子を実装します
27         /// </summary>
28         /// <param name="a">比較される方</param>
29         /// <param name="b">比較対象</param>
30         /// <returns>条件を満たすなら真</returns>
31         public static bool operator ==(Point a, Point b)
32         {
33             return a.Equals(b);
34         }
35
36         /// <summary>
37         /// 比較演算子を実装します
38         /// </summary>
39         /// <param name="a">比較される方</param>
40         /// <param name="b">比較対象</param>
41         /// <returns>条件を満たすなら真</returns>
42         public static bool operator !=(Point a, Point b)
43         {
44             return !a.Equals(b);
45         }
46
47         /// <summary>
48         /// 一致するかどうか
49         /// </summary>
50         /// <param name="o">比較対象</param>
51         /// <returns>一致するなら真</returns>
52         public override bool Equals(object o)
53         {
54             Point b = (Point)o;
55             return this.X == b.X && this.Y == b.Y;
56         }
57
58         /// <summary>
59         /// ハッシュを返す
60         /// </summary>
61         /// <returns>ハッシュを返す</returns>
62         public override int GetHashCode()
63         {
64             int result = this.X.GetHashCode();
65             result ^= this.Y.GetHashCode();
66             return result;
67         }
68
69         public Point Scale(double scale)
70         {
71             this.X *= scale;
72             this.Y *= scale;
73             return this;
74         }
75
76         public Point Offset(double x_offset, double y_offset)
77         {
78             this.X += x_offset;
79             this.Y += y_offset;
80             return this;
81         }
82 #if WINFORM
83         public static implicit operator Point(System.Drawing.Point p)
84         {
85             return new Point(p.X, p.Y);
86         }
87         public static implicit operator System.Drawing.Point(Point p)
88         {
89             return new System.Drawing.Point((int)p.X, (int)p.Y);
90         }
91         public static implicit operator SharpDX.Mathematics.Interop.RawVector2(Point p)
92         {
93             return new SharpDX.Mathematics.Interop.RawVector2((float)p.X, (float)p.Y);
94         }
95 #endif
96 #if WPF
97         public static implicit operator Point(System.Windows.Point p)
98         {
99             return new Point(p.X, p.Y);
100         }
101         public static implicit operator System.Windows.Point(Point p)
102         {
103             return new System.Windows.Point(p.X, p.Y);
104         }
105         public static implicit operator SharpDX.Mathematics.Interop.RawVector2(Point p)
106         {
107             return new SharpDX.Mathematics.Interop.RawVector2((float)p.X, (float)p.Y);
108         }
109 #endif
110 #if METRO || WINDOWS_UWP
111         public static implicit operator Point(Windows.Foundation.Point p)
112         {
113             return new Point(p.X, p.Y);
114         }
115         public static implicit operator Windows.Foundation.Point(Point p)
116         {
117             return new Windows.Foundation.Point(p.X, p.Y);
118         }
119         public static implicit operator SharpDX.Mathematics.Interop.RawVector2(Point p)
120         {
121             return new SharpDX.Mathematics.Interop.RawVector2((float)p.X, (float)p.Y);
122         }
123 #endif
124     }
125     struct Size
126     {
127         public double Width;
128         public double Height;
129         public Size(double width, double height)
130         {
131             this.Width = width;
132             this.Height = height;
133         }
134
135         /// <summary>
136         /// 比較演算子を実装します
137         /// </summary>
138         /// <param name="a">比較される方</param>
139         /// <param name="b">比較対象</param>
140         /// <returns>条件を満たすなら真</returns>
141         public static bool operator ==(Size a, Size b)
142         {
143             return a.Equals(b);
144         }
145
146         /// <summary>
147         /// 比較演算子を実装します
148         /// </summary>
149         /// <param name="a">比較される方</param>
150         /// <param name="b">比較対象</param>
151         /// <returns>条件を満たすなら真</returns>
152         public static bool operator !=(Size a, Size b)
153         {
154             return !a.Equals(b);
155         }
156
157         /// <summary>
158         /// 一致するかどうか
159         /// </summary>
160         /// <param name="o">比較対象</param>
161         /// <returns>一致するなら真</returns>
162         public override bool Equals(object o)
163         {
164             Size b = (Size)o;
165             return this.Width == b.Width && this.Height == b.Height;
166         }
167
168         /// <summary>
169         /// ハッシュを返す
170         /// </summary>
171         /// <returns>ハッシュを返す</returns>
172         public override int GetHashCode()
173         {
174             int result = this.Height.GetHashCode();
175             result ^= this.Width.GetHashCode();
176             return result;
177         }
178 #if WINFORM
179         public static implicit operator Size(System.Drawing.Size p)
180         {
181             return new Size(p.Width, p.Height);
182         }
183         public static implicit operator System.Drawing.Size(Size p)
184         {
185             return new System.Drawing.Size((int)p.Width, (int)p.Height);
186         }
187 #endif
188 #if WPF
189         public static implicit operator Size(System.Windows.Size p)
190         {
191             return new Size(p.Width, p.Height);
192         }
193         public static implicit operator System.Windows.Size(Size p)
194         {
195             return new System.Windows.Size(p.Width, p.Height);
196         }
197 #endif
198 #if METRO || WINDOWS_UWP
199         public static implicit operator Size(Windows.Foundation.Size p)
200         {
201             return new Size(p.Width, p.Height);
202         }
203         public static implicit operator Windows.Foundation.Size(Size p)
204         {
205             return new Windows.Foundation.Size(p.Width, p.Height);
206         }
207 #endif
208     }
209     struct Rectangle
210     {
211         public Point Location;
212         public Size Size;
213         public Point TopLeft
214         {
215             get { return this.Location; }
216         }
217         public Point TopRight
218         {
219             get { return new Point(this.Right, this.Location.Y); }
220         }
221         public Point BottomLeft
222         {
223             get { return new Point(this.Location.X, this.Bottom); }
224         }
225         public Point BottomRight
226         {
227             get { return new Point(this.Right, this.Bottom); }
228         }
229         public double Right
230         {
231             get { return this.X + this.Width; }
232         }
233         public double Bottom
234         {
235             get { return this.Y + this.Height; }
236         }
237         public double Height
238         {
239             get { return this.Size.Height; }
240             set { this.Size.Height = value; }
241         }
242         public double Width
243         {
244             get { return this.Size.Width; }
245             set { this.Size.Width = value; }
246         }
247         public double X
248         {
249             get { return this.Location.X; }
250         }
251         public double Y
252         {
253             get { return this.Location.Y; }
254         }
255         public Rectangle(double x, double y, double width, double height)
256         {
257             this.Location = new Point(x, y);
258             this.Size = new Size(width, height);
259         }
260         public Rectangle(Point leftTop, Point bottomRight)
261         {
262             this.Location = leftTop;
263             this.Size = new Size(bottomRight.X - leftTop.X, bottomRight.Y - leftTop.Y);
264         }
265
266         /// <summary>
267         /// どの領域も指さないことを表す
268         /// </summary>
269         public static Rectangle Empty = new Rectangle(0, 0, 0, 0);
270
271         /// <summary>
272         /// 任意の点が領域内にあるなら真を返す
273         /// </summary>
274         /// <param name="p"></param>
275         /// <returns></returns>
276         public bool IsHit(Point p)
277         {
278             if (p.X >= this.TopLeft.X &&
279                 p.Y >= this.TopLeft.Y &&
280                 p.X <= this.BottomRight.X &&
281                 p.Y <= this.BottomRight.Y)
282                 return true;
283             return false;
284         }
285
286         /// <summary>
287         /// 比較演算子を実装します
288         /// </summary>
289         /// <param name="a">比較される方</param>
290         /// <param name="b">比較対象</param>
291         /// <returns>条件を満たすなら真</returns>
292         public static bool operator ==(Rectangle a, Rectangle b)
293         {
294             return a.Equals(b);
295         }
296
297         /// <summary>
298         /// 比較演算子を実装します
299         /// </summary>
300         /// <param name="a">比較される方</param>
301         /// <param name="b">比較対象</param>
302         /// <returns>条件を満たすなら真</returns>
303         public static bool operator !=(Rectangle a, Rectangle b)
304         {
305             return !a.Equals(b);
306         }
307
308         /// <summary>
309         /// 一致するかどうか
310         /// </summary>
311         /// <param name="o">比較対象</param>
312         /// <returns>一致するなら真</returns>
313         public override bool Equals(object o)
314         {
315             Rectangle b = (Rectangle)o;
316             return this.Location.Equals(b.Location) && this.Size.Equals(b.Size);
317         }
318
319         /// <summary>
320         /// ハッシュを返す
321         /// </summary>
322         /// <returns>ハッシュを返す</returns>
323         public override int GetHashCode()
324         {
325             int result = this.Location.GetHashCode();
326             result ^= this.Size.GetHashCode();
327             return result;
328         }
329 #if WINFORM
330         public static implicit operator Rectangle(System.Drawing.Rectangle p)
331         {
332             return new Rectangle(p.X,p.Y,p.Width,p.Height);
333         }
334         public static implicit operator System.Drawing.Rectangle(Rectangle p)
335         {
336             return new System.Drawing.Rectangle((int)p.X, (int)p.Y, (int)p.Width, (int)p.Height);
337         }
338         public static implicit operator SharpDX.Mathematics.Interop.RawRectangleF(Rectangle p)
339         {
340             return new SharpDX.Mathematics.Interop.RawRectangleF((float)p.X, (float)p.Y, (float)p.BottomRight.X, (float)p.BottomRight.Y);
341         }
342 #endif
343 #if WPF
344         public static implicit operator Rectangle(System.Windows.Rect p)
345         {
346             return new Rectangle(p.X,p.Y,p.Width,p.Height);
347         }
348         public static implicit operator System.Windows.Rect(Rectangle p)
349         {
350             return new System.Windows.Rect(p.X, p.Y, p.Width, p.Height);
351         }
352         public static implicit operator SharpDX.Mathematics.Interop.RawRectangleF(Rectangle p)
353         {
354             return new SharpDX.Mathematics.Interop.RawRectangleF((float)p.X, (float)p.Y, (float)p.BottomRight.X, (float)p.BottomRight.Y);
355         }
356 #endif
357 #if METRO || WINDOWS_UWP
358         public static implicit operator Rectangle(Windows.Foundation.Rect p)
359         {
360             return new Rectangle(p.X, p.Y, p.Width, p.Height);
361         }
362         public static implicit operator Windows.Foundation.Rect(Rectangle p)
363         {
364             return new Windows.Foundation.Rect(p.X, p.Y, p.Width, p.Height);
365         }
366
367         public static implicit operator SharpDX.Mathematics.Interop.RawRectangleF(Rectangle p)
368         {
369             return new SharpDX.Mathematics.Interop.RawRectangleF((float)p.X, (float)p.Y, (float)p.BottomRight.X, (float)p.BottomRight.Y);
370         }
371 #endif
372     }
373     /// <summary>
374     /// 色構造体
375     /// </summary>
376     public struct Color: IEqualityComparer<Color>
377     {
378         /// <summary>
379         /// アルファ成分
380         /// </summary>
381         public byte A;
382         /// <summary>
383         /// 赤成分
384         /// </summary>
385         public byte R;
386         /// <summary>
387         /// 緑成分
388         /// </summary>
389         public byte G;
390         /// <summary>
391         /// 青成分
392         /// </summary>
393         public byte B;
394
395         /// <summary>
396         /// コンストラクター
397         /// </summary>
398         /// <param name="a">A成分</param>
399         /// <param name="r">R成分</param>
400         /// <param name="g">G成分</param>
401         /// <param name="b">B成分</param>
402         public Color(byte a = 255, byte r = 0, byte g = 0, byte b = 0)
403         {
404             this.A = a;
405             this.R = r;
406             this.B = b;
407             this.G = g;
408         }
409
410         /// <summary>
411         /// 等しいかどうかを調べます
412         /// </summary>
413         /// <param name="x">比較される方</param>
414         /// <param name="y">比較する方</param>
415         /// <returns>等しいなら真。そうでなければ偽</returns>
416         public bool Equals(Color x, Color y)
417         {
418             return x.A == y.A && x.R == y.R && x.G == y.G && x.B == y.B;
419         }
420
421         /// <summary>
422         /// ハッシュを得ます
423         /// </summary>
424         /// <param name="obj">Colorオブジェクト</param>
425         /// <returns>ハッシュ</returns>
426         public int GetHashCode(Color obj)
427         {
428             return this.A ^ this.R ^ this.B ^ this.G;
429         }
430
431         /// <summary>
432         /// 一致するかどうか
433         /// </summary>
434         /// <param name="o">比較対象</param>
435         /// <returns>一致するなら真</returns>
436         public override bool Equals(object o)
437         {
438             Color b = (Color)o;
439             return this.Equals(this,b);
440         }
441
442         /// <summary>
443         /// ハッシュを返す
444         /// </summary>
445         /// <returns>ハッシュを返す</returns>
446         public override int GetHashCode()
447         {
448             return this.GetHashCode(this);
449         }
450     }
451     enum AlignDirection
452     {
453         Forward,
454         Back,
455     }
456     enum ResourceType
457     {
458         Font,
459         Brush,
460         Antialias,
461         InlineChar,
462     }
463     enum FillRectType
464     {
465         OverwriteCaret,
466         InsertCaret,
467         InsertPoint,
468         LineMarker,
469         UpdateArea,
470     }
471     enum StringColorType
472     {
473         Forground,
474         LineNumber,
475     }
476     class ChangedRenderRsourceEventArgs : EventArgs
477     {
478         public ResourceType type;
479         public ChangedRenderRsourceEventArgs(ResourceType type)
480         {
481             this.type = type;
482         }
483     }
484     delegate void ChangedRenderResourceEventHandler(object sender, ChangedRenderRsourceEventArgs e);
485     interface ITextRender
486     {
487         /// <summary>
488         /// 右から左に表示するなら真
489         /// </summary>
490         bool RightToLeft { get; set; }
491
492         /// <summary>
493         /// ドキュメントを表示する領域
494         /// </summary>
495         Rectangle TextArea { get; set; }
496
497         /// <summary>
498         /// 行番号の幅
499         /// </summary>
500         double LineNemberWidth { get; }
501
502         /// <summary>
503         /// タブの文字数
504         /// </summary>
505         int TabWidthChar { get; set; }
506
507         /// <summary>
508         /// 全角スペースを表示するかどうか
509         /// </summary>
510         bool ShowFullSpace { get; set; }
511
512         /// <summary>
513         /// 半角スペースを表示するかどうか
514         /// </summary>
515         bool ShowHalfSpace { get; set; }
516
517         /// <summary>
518         /// TABを表示するかどうか
519         /// </summary>
520         bool ShowTab { get; set; }
521
522         /// <summary>
523         /// 改行を表示するかどうか
524         /// </summary>
525         bool ShowLineBreak { get; set; }
526
527         /// <summary>
528         /// 1文字当たりの高さと幅
529         /// </summary>
530         Size emSize { get; }
531
532         /// <summary>
533         /// 保持しているリソースに変化があったことを通知する
534         /// </summary>
535         event ChangedRenderResourceEventHandler ChangedRenderResource;
536
537         /// <summary>
538         /// RightToLeftの値が変わったことを通知する
539         /// </summary>
540         event EventHandler ChangedRightToLeft;
541
542         /// <summary>
543         /// 文字列を表示する
544         /// </summary>
545         /// <param name="str">文字列</param>
546         /// <param name="x">x座標</param>
547         /// <param name="y">y座標</param>
548         /// <param name="align">書式方向</param>
549         /// <param name="layoutRect">レイアウト領域</param>
550         /// <param name="colorType">色</param>
551         void DrawString(string str, double x, double y, StringAlignment align, Size layoutRect,StringColorType colorType = StringColorType.Forground);
552
553         /// <summary>
554         /// 行を表示する
555         /// </summary>
556         /// <param name="lti">LineToIndexオブジェクト</param>
557         /// <param name="row">行</param>
558         /// <param name="x">行の左上を表すX座標</param>
559         /// <param name="y">行の左上を表すY座標</param>
560         /// <param name="SelectRanges">選択領域を保持しているコレクション。選択領域の開始位置は行の先頭を0とする相対位置としてください(位置が-1の場合表示されません)</param>
561         void DrawOneLine(LineToIndexTable lti, int row, double x, double y, IEnumerable<Selection> SelectRanges);
562
563         /// <summary>
564         /// 行を折り返す
565         /// </summary>
566         /// <param name="doc">ドキュメント</param>
567         /// <param name="layoutLineCollection">レイアウトライン</param>
568         /// <param name="startIndex">開始インデックス</param>
569         /// <param name="endIndex">終了インデックス</param>
570         /// <param name="wrapwidth">折り返しの幅</param>
571         /// <returns>行のリスト</returns>
572         List<LineToIndexTableData> BreakLine(Document doc,LineToIndexTable layoutLineCollection, int startIndex, int endIndex, double wrapwidth);
573
574         /// <summary>
575         /// レイアウトを生成する
576         /// </summary>
577         /// <param name="str">文字列</param>
578         /// <returns>ITextLayoutオブジェクト</returns>
579         /// <param name="syntaxCollection">ハイライト関連の情報を保持しているコレクション</param>
580         /// <param name="MarkerRanges">マーカーを保持しているコレクション。マーカーの開始位置は行の先頭を0とする相対位置としてください(位置が-1の場合表示しないこと)</param>
581         ITextLayout CreateLaytout(string str, SyntaxInfo[] syntaxCollection, IEnumerable<Marker> MarkerRanges);
582
583         /// <summary>
584         /// グリッパーを描く
585         /// </summary>
586         /// <param name="p">中心点</param>
587         /// <param name="radius">半径</param>
588         void DrawGripper(Point p, double radius);
589
590         /// <summary>
591         /// クリッピングを開始します
592         /// </summary>
593         /// <param name="rect">クリッピングする範囲</param>
594         void BeginClipRect(Rectangle rect);
595
596         /// <summary>
597         /// クリッピングを終了します
598         /// </summary>
599         void EndClipRect();
600     }
601     interface IEditorRender : ITextRender
602     {
603         /// <summary>
604         /// フォールティングエリアの幅
605         /// </summary>
606         double FoldingWidth { get; }
607
608         /// <summary>
609         /// キャッシュされたビットマップを描写する
610         /// </summary>
611         /// <param name="rect">描く領域</param>
612         void DrawCachedBitmap(Rectangle rect);
613
614         /// <summary>
615         /// 線を描く
616         /// </summary>
617         /// <param name="from">開始座標</param>
618         /// <param name="to">修了座標</param>
619         void DrawLine(Point from, Point to);
620
621         /// <summary>
622         /// 描写したものをキャッシュする
623         /// </summary>
624         void CacheContent();
625
626         /// <summary>
627         /// キャッシュが存在するなら真を返し、そうでないなら偽を返す
628         /// </summary>
629         bool IsVaildCache();
630
631         /// <summary>
632         /// 四角形を描く
633         /// </summary>
634         /// <param name="rect"></param>
635         /// <param name="type"></param>
636         void FillRectangle(Rectangle rect,FillRectType type);
637
638         /// <summary>
639         /// ツリーに使用するマークを描く
640         /// </summary>
641         /// <param name="expand">展開済みなら真</param>
642         /// <param name="x">x座標</param>
643         /// <param name="y">y座標</param>
644         void DrawFoldingMark(bool expand, double x, double y);
645
646         /// <summary>
647         /// 背景を塗りつぶす
648         /// </summary>
649         /// <param name="rect">塗りつぶすべき領域</param>
650         void FillBackground(Rectangle rect);
651     }
652
653     enum StringAlignment
654     {
655         Left,
656         Center,
657         Right,
658     }
659     interface IPrintableTextRender : ITextRender
660     {
661         /// <summary>
662         /// ヘッダーの高さ
663         /// </summary>
664         float HeaderHeight { get; }
665
666         /// <summary>
667         /// フッターの高さ
668         /// </summary>
669         float FooterHeight { get; }
670     }
671 }