OSDN Git Service

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