OSDN Git Service

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