OSDN Git Service

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