OSDN Git Service

MVVM対応に備えてシンボル系列のプロパティもDocumentに移行した
[fooeditengine/FooEditEngine.git] / Windows / FooEditEngine / PrintableTextRender.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Drawing;
4
5 namespace FooEditEngine.Windows
6 {
7     class PrintableTextLayout : ITextLayout
8     {
9         public PrintableTextLayout(Font font)
10         {
11             this.Disposed = false;
12             this.Height = font.Height;
13         }
14         public double Width
15         {
16             get { return 0; }
17         }
18
19         public double Height
20         {
21             get;
22             private set;
23         }
24
25         public bool Disposed
26         {
27             get;
28             private set;
29         }
30
31         public bool Invaild
32         {
33             get { return false; }
34         }
35
36         public int GetIndexFromColPostion(double x)
37         {
38             return 0;
39         }
40
41         public double GetWidthFromIndex(int index)
42         {
43             return 0;
44         }
45
46         public double GetColPostionFromIndex(int index)
47         {
48             return 0;
49         }
50
51         public int AlignIndexToNearestCluster(int index, AlignDirection flow)
52         {
53             return 0;
54         }
55
56         public void Dispose()
57         {
58             this.Disposed = true;
59         }
60     }
61     class PrintableTextRender : IPrintableTextRender
62     {
63         StringFormat sf;
64         Font font;
65         Graphics g;
66
67         public PrintableTextRender(Font font, Graphics g)
68         {
69             this.font = font;
70             this.sf = StringFormat.GenericTypographic;
71             this.sf.FormatFlags |= StringFormatFlags.MeasureTrailingSpaces;
72             this.g = g;
73         }
74
75         public event ChangedRenderResourceEventHandler ChangedRenderResource;
76         public event EventHandler ChangedRightToLeft;
77
78         public void BeginDraw(Graphics g)
79         {
80             this.g = g;
81             if(this.RightToLeft)
82                 this.sf.FormatFlags |= StringFormatFlags.DirectionRightToLeft;
83         }
84
85         public void EndDraw()
86         {
87         }
88
89         public ITextLayout CreateLaytout(string str)
90         {
91             return new PrintableTextLayout(this.font);
92         }
93
94         public float HeaderHeight { get { return this.font.Height; } }
95
96         public float FooterHeight { get { return this.font.Height; } }
97
98         const int LineNumberFiledLength = 6;
99
100         public double LineNemberWidth
101         {
102             get
103             {
104                 int length = LineNumberFiledLength;
105                 length++;   //余白を確保する
106                 SizeF metrics = g.MeasureString("0", this.font, Int16.MaxValue, this.sf);
107                 return metrics.Width * length;
108             }
109         }
110
111         public double FoldingWidth
112         {
113             get
114             {
115                 return 0;
116             }
117         }
118
119         public bool RightToLeft
120         {
121             get;
122             set;
123         }
124
125         public bool InsertMode
126         {
127             get;
128             set;
129         }
130
131         public Rectangle TextArea
132         {
133             get;
134             set;
135         }
136
137         public Size emSize
138         {
139             get
140             {
141                 return new Size(0, 0);
142             }
143         }
144
145         public void DrawCachedBitmap(Rectangle rect)
146         {
147         }
148
149         public void CacheContent()
150         {
151         }
152
153         public bool IsVaildCache()
154         {
155             return false;
156         }
157
158         public void DrawLine(Point from, Point to)
159         {
160         }
161
162         public void DrawString(string str, double x, double y)
163         {
164             g.DrawString(str, this.font, new SolidBrush(this.Foreground), new PointF((float)x, (float)y), this.sf);
165         }
166
167         public void DrawCaret(Rectangle rect, bool transparent)
168         {
169         }
170
171         public void DrawFoldingMark(bool expand, double x, double y)
172         {
173         }
174
175         public void DrawString(string str, double x, double y, StringAlignment align, Size layoutRect,StringColorType colorType = StringColorType.Forground)
176         {
177             System.Drawing.StringAlignment old = this.sf.Alignment;
178             
179             this.sf.Alignment = System.Drawing.StringAlignment.Center;
180
181             g.DrawString(str, this.font, new SolidBrush(this.Foreground), new RectangleF((float)x, (float)y, (float)layoutRect.Width, (float)layoutRect.Height), this.sf);
182             
183             this.sf.Alignment = old;
184         }
185
186         public void DrawOneLine(LineToIndexTable lti, int row, double x, double y, IEnumerable<Selection> SelectRanges)
187         {           
188             g.DrawString(lti[row], this.font, new SolidBrush(this.Foreground), new RectangleF((float)x, (float)y, (float)this.TextArea.Width, (float)this.TextArea.Height), this.sf);
189         }
190
191         public void FillRectangle(Rectangle rect, FillRectType type)
192         {
193         }
194
195         public void FillBackground(Rectangle rect)
196         {
197         }
198
199         public ITextLayout CreateLaytout(string str, SyntaxInfo[] syntaxCollection, IEnumerable<Marker> MarkerRanges)
200         {
201             return new PrintableTextLayout(this.font);
202         }
203
204         public List<LineToIndexTableData> BreakLine(Document doc, LineToIndexTable layoutLineCollection, int startIndex, int endIndex, double maxwidth)
205         {
206             List<LineToIndexTableData> output = new List<LineToIndexTableData>();
207
208             foreach (string str in doc.GetLines(startIndex, endIndex))
209             {
210                 LineToIndexTableData info;
211
212                 if (str.Length == 0)
213                 {
214                     info = layoutLineCollection.CreateLineToIndexTableData(startIndex, 0, true, null);
215                     output.Add(info);
216                     return output;
217                 }
218
219                 int fitlen, index = 0, x = 0, width;
220                 do
221                 {
222                     int linesFilled;
223                     SizeF metrics = g.MeasureString(str.Substring(index), this.font, new SizeF((float)maxwidth, this.font.Height + 1), this.sf, out fitlen, out linesFilled);
224
225                     x += width = (int)metrics.Width;
226
227                     info = layoutLineCollection.CreateLineToIndexTableData(index + startIndex, fitlen, false, null);
228                     output.Add(info);
229
230                     index += fitlen;
231                 } while (index < str.Length);
232
233                 output[output.Count - 1].LineEnd = true;
234
235                 startIndex += str.Length;
236             }
237
238             return output;
239         }
240
241         public void DrawGripper(Point p, double radius)
242         {
243             //タッチには対応していないので実装する必要はない
244             throw new NotImplementedException();
245         }
246
247         public System.Drawing.Color Foreground
248         {
249             get;
250             set;
251         }
252
253         public int TabWidthChar
254         {
255             get { float taboffset; return (int)this.sf.GetTabStops(out taboffset)[0]; }
256             set { this.sf.SetTabStops(0,new float[]{value});}
257         }
258
259         public bool ShowFullSpace
260         {
261             get;
262             set;
263         }
264
265         public bool ShowHalfSpace
266         {
267             get;
268             set;
269         }
270
271         public bool ShowTab
272         {
273             get;
274             set;
275         }
276
277         public bool ShowLineBreak
278         {
279             get;
280             set;
281         }
282     }
283 }