OSDN Git Service

f8003b6fdf95711e782b4154ed1707a86736779b
[fooeditengine/FooEditEngine.git] / Metro / FooEditEngine / Direct2D / D2DRender.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 Windows.Graphics.Display;
14 using Windows.UI.Xaml.Media;
15 using Windows.UI.Xaml.Media.Imaging;
16 using SharpDX;
17 using DXGI = SharpDX.DXGI;
18 using D2D = SharpDX.Direct2D1;
19 using D3D = SharpDX.Direct3D;
20 using D3D11 = SharpDX.Direct3D11;
21 using FooEditEngine.Metro;
22 using DotNetTextStore;
23 using DotNetTextStore.UnmanagedAPI.TSF;
24 using DotNetTextStore.UnmanagedAPI.WinDef;
25
26 namespace FooEditEngine
27 {
28     sealed class D2DRender : D2DRenderBase,IEditorRender
29     {
30         SurfaceImageSource SurfaceImage;
31         DXGI.ISurfaceImageSourceNative SurfaceImageNative;
32         D2D.Device D2DDevice;
33         D2D.DeviceContext D2DContext;
34         D2D.Bitmap1 Bitmap;
35         Size Size = new Size();
36         DXGI.Surface Surface;
37         TextStore2 store;
38
39         public D2DRender(FooTextBox textbox,Windows.UI.Xaml.Shapes.Rectangle rect,TextStore2 store)
40             : base()
41         {
42             this.ConstructDeviceResource(rect.ActualWidth,rect.ActualHeight);
43             this.InitTextFormat(textbox.FontFamily, textbox.FontSize);
44
45             this.store = store;
46
47             this.Size.Width = rect.ActualWidth;
48             this.Size.Height = rect.ActualHeight;
49
50             this.CreateSurface(rect, rect.ActualWidth, rect.ActualHeight);
51
52             base.PreDrawOneLine = D2DRender_PreDrawOneLine;
53
54             this.Foreground = D2DRenderBase.ToColor4( textbox.Foreground);
55             this.Background = D2DRenderBase.ToColor4(textbox.Background);
56             this.Hilight = D2DRenderBase.ToColor4(textbox.Hilight);
57             this.Keyword1 = D2DRenderBase.ToColor4(textbox.Keyword1);
58             this.Keyword2 = D2DRenderBase.ToColor4(textbox.Keyword2);
59             this.Literal = D2DRenderBase.ToColor4(textbox.Literal);
60             this.Url = D2DRenderBase.ToColor4(textbox.URL);
61             this.ControlChar = D2DRenderBase.ToColor4(textbox.ControlChar);
62             this.Comment = D2DRenderBase.ToColor4(textbox.Comment);
63             this.InsertCaret = D2DRenderBase.ToColor4(textbox.InsertCaret);
64             this.OverwriteCaret = D2DRenderBase.ToColor4(textbox.OverwriteCaret);
65             this.LineMarker = D2DRenderBase.ToColor4(textbox.LineMarker);
66             this.UpdateArea = D2DRenderBase.ToColor4(textbox.UpdateArea);
67             this.LineNumber = D2DRenderBase.ToColor4(textbox.LineNumber);
68         }
69
70         void D2DRender_PreDrawOneLine(MyTextLayout layout,LineToIndexTable lti, int row, double x, double y)
71         {
72             using (Unlocker locker = this.store.LockDocument(false))
73             {
74                 int lineIndex = lti.GetIndexFromLineNumber(row);
75                 int lineLength = lti.GetLengthFromLineNumber(row);
76                 foreach (TextDisplayAttribute attr in this.store.EnumAttributes(lineIndex, lineIndex + lineLength))
77                 {
78                     if (attr.startIndex == attr.endIndex)
79                         continue;
80                     int length = attr.endIndex - attr.startIndex;
81                     int start = attr.startIndex - lineIndex;
82
83                     HilightType type = HilightType.None;
84                     Color4? color = null;
85                     switch (attr.attribute.lsStyle)
86                     {
87                         case TF_DA_LINESTYLE.TF_LS_DOT:
88                             type = HilightType.Dot;
89                             color = this.Foreground;
90                             break;
91                         case TF_DA_LINESTYLE.TF_LS_SOLID:
92                             type = HilightType.Sold;
93                             color = this.Foreground;
94                             break;
95                         case TF_DA_LINESTYLE.TF_LS_DASH:
96                             type = HilightType.Dash;
97                             color = this.Foreground;
98                             break;
99                         case TF_DA_LINESTYLE.TF_LS_SQUIGGLE:
100                             type = HilightType.Squiggle;
101                             color = this.Foreground;
102                             break;
103                     }
104
105                     if (attr.attribute.crBk.type != TF_DA_COLORTYPE.TF_CT_NONE)
106                     {
107                         type = HilightType.Select;
108                         color = this.Hilight;
109                     }
110
111                     this.DrawMarkerEffect(layout, type, start, length, x, y, attr.attribute.fBoldLine, color);
112                 }
113             }
114         }
115
116         public override void GetDpi(out float dpix, out float dpiy)
117         {
118             dpix = DisplayInformation.GetForCurrentView().LogicalDpi;
119             dpiy = DisplayInformation.GetForCurrentView().LogicalDpi;
120         }
121
122         public bool Resize(Windows.UI.Xaml.Shapes.Rectangle rect, double width, double height)
123         {
124             if (this.Size.Width == width && this.Size.Height == height)
125                 return false;
126             this.ReConstructDeviceResource(width, height);
127             this.CreateSurface(rect, width, height);
128             return true;
129         }
130
131         public bool IsCanDraw()
132         {
133             return this.Size.Height != 0 && this.Size.Width != 0;
134         }
135
136         public override void BegineDraw()
137         {
138             SharpDX.Point offset;
139             this.Surface = this.SurfaceImageNative.BeginDraw(
140                 new SharpDX.Rectangle(0, 0, (int)this.Size.Width, (int)this.Size.Height), out offset);
141             float dpix, dpiy;
142             this.GetDpi(out dpix, out dpiy);
143             D2D.BitmapProperties1 prop = new D2D.BitmapProperties1(new D2D.PixelFormat(DXGI.Format.B8G8R8A8_UNorm, D2D.AlphaMode.Premultiplied),
144                     dpix, dpiy, D2D.BitmapOptions.Target | D2D.BitmapOptions.CannotDraw);
145             this.Bitmap = new D2D.Bitmap1(this.D2DContext, this.Surface, prop);
146             this.D2DContext.Target = this.Bitmap;
147             this.D2DContext.Transform = Matrix3x2.Translation(offset.X,offset.Y);
148             base.BegineDraw();
149         }
150
151         public override void EndDraw()
152         {
153             base.EndDraw();
154             this.Surface.Dispose();
155             this.Bitmap.Dispose();
156             this.SurfaceImageNative.EndDraw();
157         }
158
159         void CreateSurface(Windows.UI.Xaml.Shapes.Rectangle rect, double width, double height)
160         {
161             if (this.SurfaceImageNative != null)
162                 this.SurfaceImageNative.Dispose();
163             this.SurfaceImage = new SurfaceImageSource((int)width, (int)height);
164             this.SurfaceImageNative = ComObject.As<DXGI.ISurfaceImageSourceNative>(this.SurfaceImage);
165             this.SurfaceImageNative.Device = this.DXGIDevice;
166             this.Size.Width = width;
167             this.Size.Height = height;
168             ImageBrush brush = new ImageBrush();
169             brush.ImageSource = this.SurfaceImage;
170             rect.Fill = brush;
171         }
172
173         protected override D2D.RenderTarget ConstructRender(D2D.Factory1 factory, D2D.RenderTargetProperties prop, double width, double height)
174         {
175             this.D2DDevice = new D2D.Device(factory,this.DXGIDevice);
176             this.D2DContext = new D2D.DeviceContext(this.D2DDevice, D2D.DeviceContextOptions.None);
177             return this.D2DContext;
178         }
179
180         protected override void ConstrctedResource()
181         {
182         }
183
184         protected override void DestructRender()
185         {
186             if (this.D2DDevice != null)
187                 this.D2DDevice.Dispose();
188             if (this.D2DContext != null)
189                 this.D2DContext.Dispose();
190         }
191
192         protected override void ReCreateTarget()
193         {
194         }
195     }
196 }