OSDN Git Service

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