OSDN Git Service

UWP版テキストボックスを作った
[fooeditengine/FooEditEngine.git] / Core / Direct2D / D2DRenderCommon.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 using System.Linq;
14 using System.Text;
15 using FooEditEngine;
16 using SharpDX;
17 using D2D = SharpDX.Direct2D1;
18 using DW = SharpDX.DirectWrite;
19 using DXGI = SharpDX.DXGI;
20 using System.Runtime.InteropServices;
21
22 namespace FooEditEngine
23 {
24     delegate void PreDrawOneLineHandler(MyTextLayout layout,LineToIndexTable lti,int row,double x,double y);
25
26     delegate void GetDpiHandler(out float dpix,out float dpiy);
27
28     /// <summary>
29     /// 文字列のアンチエイリアシングモードを指定する
30     /// </summary>
31     public enum TextAntialiasMode
32     {
33         /// <summary>
34         /// 最適なものが選択されます
35         /// </summary>
36         Default = D2D.TextAntialiasMode.Default,
37         /// <summary>
38         /// ClearTypeでアンチエイリアシングを行います
39         /// </summary>
40         ClearType = D2D.TextAntialiasMode.Cleartype,
41         /// <summary>
42         /// グレイスケールモードでアンチエイリアシングを行います
43         /// </summary>
44         GrayScale = D2D.TextAntialiasMode.Grayscale,
45         /// <summary>
46         /// アンチエイリアシングを行いません
47         /// </summary>
48         Aliased = D2D.TextAntialiasMode.Aliased,
49     }
50
51     sealed class ColorBrushCollection
52     {
53         ResourceManager<Color4, D2D.SolidColorBrush> cache = new ResourceManager<Color4, D2D.SolidColorBrush>();
54         D2D.RenderTarget _render;
55
56         public event EventHandler RenderChanged;
57
58         public D2D.RenderTarget Render
59         {
60             get
61             {
62                 return this._render;
63             }
64             set
65             {
66                 this._render = value;
67                 if (this.RenderChanged != null)
68                     this.RenderChanged(this, null);
69             }
70         }
71
72         public D2D.SolidColorBrush Get(Color4 key)
73         {
74             if (this.Render == null || this.Render.IsDisposed)
75                 throw new InvalidOperationException();
76             D2D.SolidColorBrush brush;
77             bool result = cache.TryGetValue(key, out brush);
78             if (!result)
79             {
80                 brush = new D2D.SolidColorBrush(this.Render, key);
81                 cache.Add(key, brush);
82             }
83             
84             return brush;
85         }
86
87         public void Clear()
88         {
89             cache.Clear();
90         }
91     }
92
93     sealed class StrokeCollection
94     {
95         ResourceManager<HilightType, D2D.StrokeStyle> cache = new ResourceManager<HilightType, D2D.StrokeStyle>();
96
97         public D2D.Factory Factory;
98
99         public D2D.StrokeStyle Get(HilightType type)
100         {
101             if(this.Factory == null || this.Factory.IsDisposed)
102                 throw new InvalidOperationException();
103             D2D.StrokeStyle stroke;
104             if (this.cache.TryGetValue(type, out stroke))
105                 return stroke;
106
107             D2D.StrokeStyleProperties prop = new D2D.StrokeStyleProperties();
108             prop.DashCap = D2D.CapStyle.Flat;
109             prop.DashOffset = 0;
110             prop.DashStyle = D2D.DashStyle.Solid;
111             prop.EndCap = D2D.CapStyle.Flat;
112             prop.LineJoin = D2D.LineJoin.Miter;
113             prop.MiterLimit = 0;
114             prop.StartCap = D2D.CapStyle.Flat;
115             switch (type)
116             {
117                 case HilightType.Sold:
118                 case HilightType.Url:
119                 case HilightType.Squiggle:
120                     prop.DashStyle = D2D.DashStyle.Solid;
121                     break;
122                 case HilightType.Dash:
123                     prop.DashStyle = D2D.DashStyle.Dash;
124                     break;
125                 case HilightType.DashDot:
126                     prop.DashStyle = D2D.DashStyle.DashDot;
127                     break;
128                 case HilightType.DashDotDot:
129                     prop.DashStyle = D2D.DashStyle.DashDotDot;
130                     break;
131                 case HilightType.Dot:
132                     prop.DashStyle = D2D.DashStyle.Dot;
133                     break;
134             }
135             stroke = new D2D.StrokeStyle(this.Factory, prop);
136             this.cache.Add(type, stroke);
137             return stroke;
138         }
139         public void Clear()
140         {
141             cache.Clear();
142         }
143     }
144
145     class D2DRenderCommon : IDisposable
146     {
147         ColorBrushCollection Brushes = new ColorBrushCollection();
148         StrokeCollection Strokes = new StrokeCollection();
149         InlineManager HiddenChars;
150         TextAntialiasMode _TextAntialiasMode;
151         Color4 _ControlChar,_Forground,_URL,_Hilight;
152         DW.Factory DWFactory;
153 #if METRO || WINDOWS_UWP
154         D2D.Factory1 D2DFactory;
155 #else
156         D2D.Factory D2DFactory;
157 #endif
158         DW.TextFormat format;
159         protected D2D.Bitmap cachedBitMap;
160         CustomTextRenderer textRender;
161         int tabLength = 8;
162         bool _ShowLineBreak;
163         Color4 _Comment, _Literal, _Keyword1, _Keyword2;
164         protected bool hasCache;
165
166         protected Size renderSize
167         {
168             get;
169             private set;
170         }
171
172         protected D2D.RenderTarget render
173         {
174             get;
175             private set;
176         }
177
178         public D2DRenderCommon()
179         {
180             this.DWFactory = new DW.Factory(DW.FactoryType.Shared);
181 #if METRO || WINDOWS_UWP
182             this.D2DFactory = new D2D.Factory1(D2D.FactoryType.MultiThreaded);
183 #else
184             this.D2DFactory = new D2D.Factory(D2D.FactoryType.MultiThreaded);
185 #endif
186             this.Strokes.Factory = this.D2DFactory;
187             this.ChangedRenderResource += (s, e) => { };
188             this.ChangedRightToLeft += (s, e) => { };
189             this.renderSize = new Size();
190         }
191
192         public event ChangedRenderResourceEventHandler ChangedRenderResource;
193
194         public event EventHandler ChangedRightToLeft;
195
196         public const int MiniumeWidth = 40;    //これ以上ないと誤操作が起こる
197
198         public void InitTextFormat(string fontName, float fontSize, DW.FontWeight fontWeigth = DW.FontWeight.Normal,DW.FontStyle fontStyle = DW.FontStyle.Normal)
199         {
200             if(this.format != null)
201                 this.format.Dispose();
202
203             float dpix, dpiy;
204             this.GetDpi(out dpix, out dpiy);
205
206             this.format = new DW.TextFormat(this.DWFactory, fontName, fontWeigth, fontStyle, fontSize);
207             this.format.WordWrapping = DW.WordWrapping.NoWrap;
208
209             if (this.HiddenChars == null)
210                 this.HiddenChars = new InlineManager(this.DWFactory, this.format, this.ControlChar, this.Brushes);
211             else
212                 this.HiddenChars.Format = this.format;
213
214             this.TabWidthChar = this.TabWidthChar;
215
216             this.hasCache = false;
217
218             MyTextLayout layout = new MyTextLayout(this.DWFactory, "0", this.format, float.MaxValue, float.MaxValue, dpix, false);
219             layout.RightToLeft = false;
220             this.emSize = new Size(layout.Width, layout.Height);
221             layout.Dispose();
222
223             layout = new MyTextLayout(this.DWFactory, "+", this.format, float.MaxValue, float.MaxValue, dpix, false);
224             layout.RightToLeft = false;
225 #if METRO
226             this.FoldingWidth = Math.Max(D2DRenderCommon.MiniumeWidth, layout.Width);
227 #else
228             this.FoldingWidth = layout.Width;
229 #endif
230             layout.Dispose();
231
232             this.ChangedRenderResource(this,new ChangedRenderRsourceEventArgs(ResourceType.Font));
233         }
234
235         public bool RightToLeft
236         {
237             get
238             {
239                 return this.format.ReadingDirection == DW.ReadingDirection.RightToLeft;
240             }
241             set
242             {
243                 this.format.ReadingDirection = value ? DW.ReadingDirection.RightToLeft : DW.ReadingDirection.LeftToRight;
244                 this.ChangedRightToLeft(this, null);                
245             }
246         }
247
248         public TextAntialiasMode TextAntialiasMode
249         {
250             get
251             {
252                 return this._TextAntialiasMode;
253             }
254             set
255             {
256                 if (this.render == null)
257                     throw new InvalidOperationException();
258                 this._TextAntialiasMode = value;
259                 this.render.TextAntialiasMode = (D2D.TextAntialiasMode)value;
260                 this.ChangedRenderResource(this, new ChangedRenderRsourceEventArgs(ResourceType.Antialias));
261             }
262         }
263
264         public bool ShowFullSpace
265         {
266             get
267             {
268                 if (this.HiddenChars == null)
269                     return false;
270                 else
271                     return this.HiddenChars.ContainsSymbol(' ');
272             }
273             set
274             {
275                 if (this.HiddenChars == null)
276                     throw new InvalidOperationException();
277                 if (value)
278                     this.HiddenChars.AddSymbol(' ', '□');
279                 else
280                     this.HiddenChars.RemoveSymbol(' ');
281             }
282         }
283
284         public bool ShowHalfSpace
285         {
286             get
287             {
288                 if (this.HiddenChars == null)
289                     return false;
290                 else
291                     return this.HiddenChars.ContainsSymbol(' ');
292             }
293             set
294             {
295                 if (this.HiddenChars == null)
296                     throw new InvalidOperationException();
297                 if (value)
298                     this.HiddenChars.AddSymbol(' ', 'ロ');
299                 else
300                     this.HiddenChars.RemoveSymbol(' ');
301             }
302         }
303
304         public bool ShowTab
305         {
306             get
307             {
308                 if (this.HiddenChars == null)
309                     return false;
310                 else
311                     return this.HiddenChars.ContainsSymbol('\t');
312             }
313             set
314             {
315                 if (this.HiddenChars == null)
316                     throw new InvalidOperationException();
317                 if (value)
318                     this.HiddenChars.AddSymbol('\t', '>');
319                 else
320                     this.HiddenChars.RemoveSymbol('\t');
321             }
322         }
323
324         public bool ShowLineBreak
325         {
326             get
327             {
328                 return this._ShowLineBreak;
329             }
330             set
331             {
332                 this._ShowLineBreak = value;
333             }
334         }
335
336         public Color4 Foreground
337         {
338             get
339             {
340                 return this._Forground;
341             }
342             set
343             {
344                 if (this.render == null)
345                     return;
346                 this._Forground = value;
347                 if (this.textRender != null)
348                     this.textRender.DefaultFore = value;
349                 this.ChangedRenderResource(this, new ChangedRenderRsourceEventArgs(ResourceType.Brush));
350             }
351         }
352
353         public Color4 Background
354         {
355             get;
356             set;
357         }
358
359         public Color4 InsertCaret
360         {
361             get;
362             set;
363         }
364
365         public Color4 OverwriteCaret
366         {
367             get;
368             set;
369         }
370
371         public Color4 LineMarker
372         {
373             get;
374             set;
375         }
376
377         public Color4 UpdateArea
378         {
379             get;
380             set;
381         }
382
383         public Color4 LineNumber
384         {
385             get;
386             set;
387         }
388
389         public Color4 ControlChar
390         {
391             get
392             {
393                 return this._ControlChar;
394             }
395             set
396             {
397                 this._ControlChar = value;
398                 if (this.HiddenChars != null)
399                     this.HiddenChars.Fore = value;
400                 this.ChangedRenderResource(this, new ChangedRenderRsourceEventArgs(ResourceType.Brush));
401             }
402         }
403
404         public Color4 Url
405         {
406             get
407             {
408                 return this._URL;
409             }
410             set
411             {
412                 this._URL = value;
413                 this.ChangedRenderResource(this, new ChangedRenderRsourceEventArgs(ResourceType.Brush));
414             }
415         }
416
417         public Color4 Hilight
418         {
419             get
420             {
421                 return this._Hilight;
422             }
423             set
424             {
425                 this._Hilight = value;
426             }
427         }
428
429         public Color4 Comment
430         {
431             get
432             {
433                 return this._Comment;
434             }
435             set
436             {
437                 this._Comment = value;
438                 this.ChangedRenderResource(this, new ChangedRenderRsourceEventArgs(ResourceType.Brush));
439             }
440         }
441
442         public Color4 Literal
443         {
444             get
445             {
446                 return this._Literal;
447             }
448             set
449             {
450                 this._Literal = value;
451                 this.ChangedRenderResource(this, new ChangedRenderRsourceEventArgs(ResourceType.Brush));
452             }
453         }
454
455         public Color4 Keyword1
456         {
457             get
458             {
459                 return this._Keyword1;
460             }
461             set
462             {
463                 this._Keyword1 = value;
464                 this.ChangedRenderResource(this, new ChangedRenderRsourceEventArgs(ResourceType.Brush));
465             }
466         }
467
468         public Color4 Keyword2
469         {
470             get
471             {
472                 return this._Keyword2;
473             }
474             set
475             {
476                 this._Keyword2 = value;
477                 this.ChangedRenderResource(this, new ChangedRenderRsourceEventArgs(ResourceType.Brush));
478             }
479         }
480
481         public Rectangle TextArea
482         {
483             get;
484             set;
485         }
486
487         public double LineNemberWidth
488         {
489             get
490             {
491                 return this.emSize.Width * EditView.LineNumberLength;
492             }
493         }
494
495         public double FoldingWidth
496         {
497             get;
498             private set;
499         }
500
501         public int TabWidthChar
502         {
503             get { return this.tabLength; }
504             set
505             {
506                 if (value == 0)
507                     return;
508                 this.tabLength = value;
509                 DW.TextLayout layout = new DW.TextLayout(this.DWFactory, "0", this.format, float.MaxValue, float.MaxValue);
510                 float width = (float)(layout.Metrics.Width * value);
511                 this.HiddenChars.TabWidth = width;
512                 this.format.IncrementalTabStop = width;
513             }
514         }
515
516         public Size emSize
517         {
518             get;
519             private set;
520         }
521
522         public void DrawGripper(Point p, double radius)
523         {
524             D2D.Ellipse ellipse = new D2D.Ellipse();
525             ellipse.Point = p;
526             ellipse.RadiusX = (float)radius;
527             ellipse.RadiusY = (float)radius;
528             this.render.FillEllipse(ellipse, this.Brushes.Get(this.Background));
529             this.render.DrawEllipse(ellipse, this.Brushes.Get(this.Foreground));
530         }
531
532         public void ReConstructDeviceResource()
533         {
534             this.ReConstructDeviceResource(this.renderSize.Width, this.renderSize.Height);
535         }
536
537         public void ReConstructDeviceResource(double width, double height)
538         {
539             this.DestructDeviceResource();
540             this.ConstructDeviceResource(width, height);
541         }
542
543         public virtual void DrawCachedBitmap(Rectangle rect)
544         {
545         }
546
547         public virtual void CacheContent()
548         {
549         }
550
551         public virtual bool IsVaildCache()
552         {
553             return this.hasCache;
554         }
555
556         protected void BegineDraw()
557         {
558             if (this.render == null || this.render.IsDisposed)
559                 return;
560             this.render.BeginDraw();
561             this.render.AntialiasMode = D2D.AntialiasMode.Aliased;
562         }
563
564         protected void EndDraw()
565         {
566             if (this.render == null || this.render.IsDisposed)
567                 return;
568             this.render.AntialiasMode = D2D.AntialiasMode.PerPrimitive;
569             this.render.EndDraw();
570         }
571
572         public void DrawString(string str, double x, double y, StringAlignment align, Size layoutRect, StringColorType colorType = StringColorType.Forground)
573         {
574             if (this.render == null || this.render.IsDisposed)
575                 return;
576             float dpix, dpiy;
577             D2D.SolidColorBrush brush;
578             switch (colorType)
579             {
580                 case StringColorType.Forground:
581                     brush = this.Brushes.Get(this.Foreground);
582                     break;
583                 case StringColorType.LineNumber:
584                     brush = this.Brushes.Get(this.LineNumber);
585                     break;
586                 default:
587                     throw new ArgumentOutOfRangeException();
588             }
589             this.GetDpi(out dpix, out dpiy);
590             MyTextLayout layout = new MyTextLayout(this.DWFactory, str, this.format, (float)layoutRect.Width, (float)layoutRect.Height,dpix,false);
591             layout.StringAlignment = align;
592             layout.Draw(this.render, (float)x, (float)y, brush);
593             layout.Dispose();
594         }
595
596         public void DrawFoldingMark(bool expand, double x, double y)
597         {
598             string mark = expand ? "-" : "+";
599             this.DrawString(mark, x, y,StringAlignment.Left, new Size(this.FoldingWidth, this.emSize.Height));
600         }
601
602         public void FillRectangle(Rectangle rect,FillRectType type)
603         {
604             if (this.render == null || this.render.IsDisposed)
605                 return;
606             D2D.SolidColorBrush brush = null;
607             switch(type)
608             {
609                 case FillRectType.OverwriteCaret:
610                     brush = this.Brushes.Get(this.OverwriteCaret);
611                     this.render.FillRectangle(rect, brush);
612                     break;
613                 case FillRectType.InsertCaret:
614                     brush = this.Brushes.Get(this.InsertCaret);
615                     this.render.FillRectangle(rect, brush);
616                     break;
617                 case FillRectType.InsertPoint:
618                     brush = this.Brushes.Get(this.Hilight);
619                     this.render.FillRectangle(rect, brush);
620                     break;
621                 case FillRectType.LineMarker:
622                     brush = this.Brushes.Get(this.LineMarker);
623                     this.render.DrawRectangle(rect, brush, EditView.LineMarkerThickness);
624                     break;
625                 case FillRectType.UpdateArea:
626                     brush = this.Brushes.Get(this.UpdateArea);
627                     this.render.FillRectangle(rect, brush);
628                     break;
629             }
630         }
631
632         public void FillBackground(Rectangle rect)
633         {
634             if (this.render == null || this.render.IsDisposed)
635                 return;
636             this.render.Clear(this.Background);
637         }
638
639         public void DrawOneLine(LineToIndexTable lti, int row, double x, double y, IEnumerable<Selection> SelectRanges,PreDrawOneLineHandler PreDrawOneLine)
640         {
641             int lineLength = lti.GetLengthFromLineNumber(row);
642
643             if (lineLength == 0 || this.render == null || this.render.IsDisposed)
644                 return;
645
646             MyTextLayout layout = (MyTextLayout)lti.GetLayout(row);
647
648             if(PreDrawOneLine != null)
649                 PreDrawOneLine(layout,lti,row,x,y);
650
651             if (SelectRanges != null)
652             {
653                 foreach (Selection sel in SelectRanges)
654                 {
655                     if (sel.length == 0 || sel.start == -1)
656                         continue;
657
658                     this.DrawMarkerEffect(layout, HilightType.Select, sel.start, sel.length, x, y, false);
659                 }
660             }
661
662             layout.Draw(textRender, (float)x, (float)y);
663
664         }
665
666         public void BeginClipRect(Rectangle rect)
667         {
668             this.render.PushAxisAlignedClip(rect, D2D.AntialiasMode.Aliased);
669         }
670
671         public void EndClipRect()
672         {
673             this.render.PopAxisAlignedClip();
674         }
675
676         public void SetTextColor(MyTextLayout layout,int start, int length, Color4? color)
677         {
678             if (color == null)
679                 return;
680             layout.SetDrawingEffect(this.Brushes.Get((Color4)color), new DW.TextRange(start, length));
681         }
682
683         public void DrawLine(Point from, Point to)
684         {
685             D2D.Brush brush = this.Brushes.Get(this.Foreground);
686             D2D.StrokeStyle stroke = this.Strokes.Get(HilightType.Sold);
687             this.render.DrawLine(from, to, brush, 1.0f, stroke);
688         }
689
690         public const int BoldThickness = 2;
691         public const int NormalThickness = 1;
692
693         public void DrawMarkerEffect(MyTextLayout layout, HilightType type, int start, int length, double x, double y, bool isBold, Color4? effectColor = null)
694         {
695             if (type == HilightType.None)
696                 return;
697
698             float thickness = isBold ? BoldThickness : NormalThickness;
699
700             Color4 color;
701             if (effectColor != null)
702                 color = (Color4)effectColor;
703             else if (type == HilightType.Select)
704                 color = this.Hilight;
705             else
706                 color = this.Foreground;
707
708             IMarkerEffecter effecter = null;
709             D2D.SolidColorBrush brush = this.Brushes.Get(color);
710
711             if (type == HilightType.Squiggle)
712                 effecter = new D2DSquilleLineMarker(this.render, brush, this.Strokes.Get(HilightType.Squiggle), thickness);
713             else if (type == HilightType.Select)
714                 effecter = new HilightMarker(this.render, brush);
715             else if (type == HilightType.None)
716                 effecter = null;
717             else
718                 effecter = new LineMarker(this.render, brush, this.Strokes.Get(type), thickness);
719
720             if (effecter != null)
721             {
722                 bool isUnderline = type != HilightType.Select;
723
724                 DW.HitTestMetrics[] metrics = layout.HitTestTextRange(start, length, (float)x, (float)y);
725                 foreach (DW.HitTestMetrics metric in metrics)
726                 {
727                     float offset = isUnderline ? metric.Height : 0;
728                     effecter.Draw(metric.Left, metric.Top + offset, metric.Width, metric.Height);
729                 }
730             }
731         }
732
733         public ITextLayout CreateLaytout(string str, SyntaxInfo[] syntaxCollection, IEnumerable<Marker> MarkerRanges)
734         {
735             float dpix,dpiy;
736             this.GetDpi(out dpix,out dpiy);
737
738             bool hasNewLine = str.Length > 0 && str[str.Length - 1] == Document.NewLine;
739             MyTextLayout newLayout = new MyTextLayout(this.DWFactory,
740                 str,
741                 this.format,
742                 this.TextArea.Width,
743                 this.TextArea.Height,
744                 dpiy,
745                 hasNewLine && this._ShowLineBreak);
746             ParseLayout(newLayout, str);
747             if (syntaxCollection != null)
748             {
749                 foreach (SyntaxInfo s in syntaxCollection)
750                 {
751                     D2D.SolidColorBrush brush = this.Brushes.Get(this.Foreground);
752                     switch (s.type)
753                     {
754                         case TokenType.Comment:
755                             brush = this.Brushes.Get(this.Comment);
756                             break;
757                         case TokenType.Keyword1:
758                             brush = this.Brushes.Get(this.Keyword1);
759                             break;
760                         case TokenType.Keyword2:
761                             brush = this.Brushes.Get(this.Keyword2);
762                             break;
763                         case TokenType.Literal:
764                             brush = this.Brushes.Get(this.Literal);
765                             break;
766                     }
767                     newLayout.SetDrawingEffect(brush, new DW.TextRange(s.index, s.length));
768                 }
769             }
770
771             if (MarkerRanges != null)
772             {
773                 foreach (Marker m in MarkerRanges)
774                 {
775                     if (m.start == -1 || m.length == 0)
776                         continue;
777                     Color4 color = new Color4(m.color.R / 255.0f, m.color.G / 255.0f, m.color.B / 255.0f, m.color.A / 255.0f);
778                     if (m.hilight == HilightType.Url)
779                         color = this.Url;
780                     newLayout.SetDrawingEffect(new DrawingEffect(m.hilight, color,m.isBoldLine), new DW.TextRange(m.start, m.length));
781                     if (m.hilight != HilightType.None && m.hilight != HilightType.Select)
782                         newLayout.SetUnderline(true, new DW.TextRange(m.start, m.length));
783                 }
784             }
785
786             return newLayout;
787        }
788
789         public List<LineToIndexTableData> BreakLine(Document doc, LineToIndexTable layoutLineCollection, int startIndex, int endIndex, double wrapwidth)
790         {
791             List<LineToIndexTableData> output = new List<LineToIndexTableData>();
792
793             this.format.WordWrapping = DW.WordWrapping.Wrap;
794
795             foreach (string str in doc.GetLines(startIndex, endIndex))
796             {
797                 DW.TextLayout layout = new DW.TextLayout(this.DWFactory, str, this.format, (float)wrapwidth, float.MaxValue);
798
799                 int i = startIndex;
800                 foreach (DW.LineMetrics metrics in layout.GetLineMetrics())
801                 {
802                     if (metrics.Length == 0 && metrics.NewlineLength == 0)
803                         continue;
804
805                     bool lineend = false;
806                     if (metrics.NewlineLength > 0)
807                         lineend = true;
808
809                     output.Add(layoutLineCollection.CreateLineToIndexTableData(i, (int)metrics.Length, lineend, null));
810                     i += Util.RoundUp(metrics.Length);
811                 }
812
813                 layout.Dispose();
814
815                 startIndex += str.Length;
816             }
817
818             this.format.WordWrapping = DW.WordWrapping.NoWrap;
819
820             if (output.Count > 0)
821                 output.Last().LineEnd = true;
822
823             return output;
824         }
825
826         public virtual void Dispose()
827         {
828             this.DestructDeviceResource();
829             this.HiddenChars.Clear();
830             if (this.format != null)
831                 this.format.Dispose();
832             if(this.DWFactory != null)
833                 this.DWFactory.Dispose();
834             if(this.D2DFactory != null)
835                 this.D2DFactory.Dispose();
836         }
837
838         public void ConstructDeviceResource(double width, double height)
839         {
840             float dpiX, dpiY;
841             this.GetDpi(out dpiX, out dpiY);
842             D2D.RenderTargetProperties prop = new D2D.RenderTargetProperties(
843                 D2D.RenderTargetType.Default,
844                 new D2D.PixelFormat(DXGI.Format.B8G8R8A8_UNorm, D2D.AlphaMode.Ignore),
845                 dpiX,
846                 dpiY,
847                 D2D.RenderTargetUsage.None,
848                 D2D.FeatureLevel.Level_DEFAULT);
849
850             this.render = this.ConstructRender(this.D2DFactory,prop,width,height);
851
852             this.Brushes.Render = this.render;
853
854             D2D.BitmapProperties bmpProp = new D2D.BitmapProperties();
855             bmpProp.DpiX = dpiX;
856             bmpProp.DpiY = dpiY;
857             bmpProp.PixelFormat = new D2D.PixelFormat(DXGI.Format.B8G8R8A8_UNorm, D2D.AlphaMode.Ignore);
858             this.cachedBitMap = new D2D.Bitmap(this.render, new SharpDX.Size2((int)width, (int)height), bmpProp);
859             this.hasCache = false;
860
861             this.ConstrctedResource();
862
863             this.textRender = new CustomTextRenderer(this.render, this.Brushes,this.Strokes, this.Foreground);
864
865             this.renderSize = new Size(width,height);
866
867             this.TextAntialiasMode = this._TextAntialiasMode;
868         }
869
870 #if METRO || WINDOWS_UWP
871         protected virtual D2D.RenderTarget ConstructRender(D2D.Factory1 factory, D2D.RenderTargetProperties prop, double width, double height)
872 #else
873         protected virtual D2D.RenderTarget ConstructRender(D2D.Factory factory, D2D.RenderTargetProperties prop, double width, double height)
874 #endif
875         {
876             throw new NotImplementedException();
877         }
878
879         protected virtual void ConstrctedResource()
880         {
881             throw new NotImplementedException();
882         }
883
884         public virtual void GetDpi(out float dpix,out float dpiy)
885         {
886             throw new NotImplementedException();
887         }
888
889         protected virtual void DestructRender()
890         {
891             throw new NotImplementedException();
892
893         }
894
895         protected virtual void ReCreateTarget()
896         {
897             throw new NotImplementedException();
898         }
899
900         public void DestructDeviceResource()
901         {
902             this.hasCache = false;
903             if (this.cachedBitMap != null)
904                 this.cachedBitMap.Dispose();
905             this.Brushes.Clear();
906             this.Strokes.Clear();
907             if (this.textRender != null)
908                 this.textRender.Dispose();
909             this.DestructRender();
910         }
911
912         void ParseLayout(MyTextLayout layout, string str)
913         {
914             for (int i = 0; i < str.Length; i++)
915             {
916                 DW.InlineObject inlineObject = this.HiddenChars.Get(layout,i, str);
917                 if (inlineObject != null)
918                 {
919                     layout.SetInlineObject(inlineObject, new DW.TextRange(i, 1));
920                     layout.SetDrawingEffect(this.Brushes.Get(this.ControlChar), new DW.TextRange(i, 1));
921                 }
922             }
923             layout.SetLineBreakBrush(this.Brushes.Get(this.ControlChar));
924         }
925     }
926 }