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.
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.
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/>.
12 using System.Collections.Generic;
13 using Windows.Graphics.Display;
14 using Windows.UI.Xaml.Media;
15 using Windows.UI.Xaml.Media.Imaging;
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;
26 namespace FooEditEngine
28 sealed class D2DRender : D2DRenderBase,IEditorRender
30 SurfaceImageSource SurfaceImage;
31 DXGI.ISurfaceImageSourceNative SurfaceImageNative;
33 D2D.DeviceContext D2DContext;
35 Size Size = new Size();
39 public D2DRender(FooTextBox textbox,Windows.UI.Xaml.Shapes.Rectangle rect,TextStore2 store)
42 this.ConstructDeviceResource(rect.ActualWidth,rect.ActualHeight);
43 this.InitTextFormat(textbox.FontFamily, textbox.FontSize);
47 this.Size.Width = rect.ActualWidth;
48 this.Size.Height = rect.ActualHeight;
50 this.CreateSurface(rect, rect.ActualWidth, rect.ActualHeight);
52 base.PreDrawOneLine = D2DRender_PreDrawOneLine;
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);
70 void D2DRender_PreDrawOneLine(MyTextLayout layout,LineToIndexTable lti, int row, double x, double y)
72 using (Unlocker locker = this.store.LockDocument(false))
74 int lineIndex = lti.GetIndexFromLineNumber(row);
75 int lineLength = lti.GetLengthFromLineNumber(row);
76 foreach (TextDisplayAttribute attr in this.store.EnumAttributes(lineIndex, lineIndex + lineLength))
78 if (attr.startIndex == attr.endIndex)
80 int length = attr.endIndex - attr.startIndex;
81 int start = attr.startIndex - lineIndex;
83 HilightType type = HilightType.None;
85 switch (attr.attribute.lsStyle)
87 case TF_DA_LINESTYLE.TF_LS_DOT:
88 type = HilightType.Dot;
89 color = this.Foreground;
91 case TF_DA_LINESTYLE.TF_LS_SOLID:
92 type = HilightType.Sold;
93 color = this.Foreground;
95 case TF_DA_LINESTYLE.TF_LS_DASH:
96 type = HilightType.Dash;
97 color = this.Foreground;
99 case TF_DA_LINESTYLE.TF_LS_SQUIGGLE:
100 type = HilightType.Squiggle;
101 color = this.Foreground;
105 if (attr.attribute.crBk.type != TF_DA_COLORTYPE.TF_CT_NONE)
107 type = HilightType.Select;
108 color = this.Hilight;
111 this.DrawMarkerEffect(layout, type, start, length, x, y, attr.attribute.fBoldLine, color);
116 public override void GetDpi(out float dpix, out float dpiy)
118 dpix = DisplayInformation.GetForCurrentView().LogicalDpi;
119 dpiy = DisplayInformation.GetForCurrentView().LogicalDpi;
122 public bool Resize(Windows.UI.Xaml.Shapes.Rectangle rect, double width, double height)
124 if (this.Size.Width == width && this.Size.Height == height)
126 this.ReConstructDeviceResource(width, height);
127 this.CreateSurface(rect, width, height);
131 public bool IsCanDraw()
133 return this.Size.Height != 0 && this.Size.Width != 0;
136 public override void BegineDraw()
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);
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);
151 public override void EndDraw()
154 this.Surface.Dispose();
155 this.Bitmap.Dispose();
156 this.SurfaceImageNative.EndDraw();
159 void CreateSurface(Windows.UI.Xaml.Shapes.Rectangle rect, double width, double height)
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;
173 protected override D2D.RenderTarget ConstructRender(D2D.Factory1 factory, D2D.RenderTargetProperties prop, double width, double height)
175 this.D2DDevice = new D2D.Device(factory,this.DXGIDevice);
176 this.D2DContext = new D2D.DeviceContext(this.D2DDevice, D2D.DeviceContextOptions.None);
177 return this.D2DContext;
180 protected override void ConstrctedResource()
184 protected override void DestructRender()
186 if (this.D2DDevice != null)
187 this.D2DDevice.Dispose();
188 if (this.D2DContext != null)
189 this.D2DContext.Dispose();
192 protected override void ReCreateTarget()