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.Text;
15 using Windows.UI.Xaml.Media;
16 using Windows.UI.Xaml.Media.Imaging;
18 using DXGI = SharpDX.DXGI;
19 using D2D = SharpDX.Direct2D1;
20 using DW = SharpDX.DirectWrite;
21 using D3D = SharpDX.Direct3D;
22 using D3D11 = SharpDX.Direct3D11;
23 using FooEditEngine.UWP;
25 namespace FooEditEngine
27 class D2DRenderBase: D2DRenderCommon,IDisposable
29 public new const int MiniumeWidth = 40; //これ以上ないと誤操作が起こる
31 protected DXGI.Device DXGIDevice;
32 protected D3D11.Device1 D3DDevice;
33 protected D2D.Device D2DDevice;
34 protected D2D.DeviceContext D2DContext;
35 FontFamily fontFamily;
36 FontStyle fontStyle = FontStyle.Normal;
37 FontWeight fontWeigth;
40 public D2DRenderBase()
42 var creationFlags = SharpDX.Direct3D11.DeviceCreationFlags.VideoSupport | SharpDX.Direct3D11.DeviceCreationFlags.BgraSupport;
43 D3D.FeatureLevel[] featureLevels ={D3D.FeatureLevel.Level_11_1,
44 D3D.FeatureLevel.Level_11_0,
45 D3D.FeatureLevel.Level_10_1,
46 D3D.FeatureLevel.Level_10_0,
47 D3D.FeatureLevel.Level_9_3,
48 D3D.FeatureLevel.Level_9_2,
49 D3D.FeatureLevel.Level_9_1};
50 using (var device = new D3D11.Device(D3D.DriverType.Hardware, creationFlags, featureLevels))
52 this.D3DDevice = device.QueryInterface<D3D11.Device1>();
54 this.DXGIDevice = this.D3DDevice.QueryInterface<DXGI.Device>();
57 public void InitTextFormat(string fontName, double size)
59 base.InitTextFormat(fontName, (float)size);
63 public void InitTextFormat(FontFamily font, double size)
65 base.InitTextFormat(font.Source, (float)size);
66 this.fontFamily = font;
70 protected override void Dispose(bool dispose)
72 base.Dispose(dispose);
73 this.DestructRenderAndResource();
74 if (this.DXGIDevice != null)
75 this.DXGIDevice.Dispose();
76 if (this.D3DDevice != null)
77 this.D3DDevice.Dispose();
80 public virtual void ConstructRenderAndResoruce(double width, double height)
82 this.D2DDevice = new D2D.Device(this.D2DFactory, this.DXGIDevice);
83 this.D2DContext = new D2D.DeviceContext(this.D2DDevice, D2D.DeviceContextOptions.None);
85 this.GetDpi(out dpiX, out dpiY);
86 this.D2DContext.DotsPerInch = new Size2F(dpiX, dpiY);
87 this.render = this.D2DContext;
88 this.renderSize = new Size(width, height);
89 this.textRender = new CustomTextRenderer(this.render, this.Brushes, this.Strokes, this.Foreground);
92 public virtual void DestructRenderAndResource()
96 //ここでDispose()する必要はない。すると落ちる
97 //if (this.textRender != null)
98 // this.textRender.Dispose();
99 if (this.D2DDevice != null)
100 this.D2DDevice.Dispose();
101 if (this.D2DContext != null)
102 this.D2DContext.Dispose();
105 public FontFamily FontFamily
107 get { return this.fontFamily; }
110 this.fontFamily = value;
111 this.InitTextFormat(this.fontFamily.Source, (float)this.fontSize, this.GetDWFontWeigth(this.fontWeigth), this.GetDWFontStyle(this.fontStyle));
112 this.TabWidthChar = this.TabWidthChar;
116 public double FontSize
118 get { return this.fontSize; }
121 this.fontSize = value;
122 this.InitTextFormat(this.fontFamily.Source, (float)this.fontSize, this.GetDWFontWeigth(this.fontWeigth), this.GetDWFontStyle(this.fontStyle));
123 this.TabWidthChar = this.TabWidthChar;
127 public FontWeight FontWeigth
131 return this.fontWeigth;
135 this.fontWeigth = value;
136 this.InitTextFormat(this.fontFamily.Source, (float)this.fontSize, this.GetDWFontWeigth(value), this.GetDWFontStyle(this.fontStyle));
140 public FontStyle FontStyle
144 return this.fontStyle;
148 this.fontStyle = value;
149 this.InitTextFormat(this.fontFamily.Source, (float)this.fontSize, this.GetDWFontWeigth(this.fontWeigth), this.GetDWFontStyle(this.fontStyle));
153 DW.FontStyle GetDWFontStyle(FontStyle style)
155 return (DW.FontStyle)Enum.Parse(typeof(DW.FontStyle), style.ToString());
158 DW.FontWeight GetDWFontWeigth(FontWeight weigth)
160 if (weigth.Weight == 0)
161 return (DW.FontWeight)400;
163 return (DW.FontWeight)weigth.Weight;
166 public static Color4 ToColor4(Windows.UI.Color color)
168 return new Color4(color.R / 255.0f, color.G / 255.0f, color.B / 255.0f, color.A / 255.0f);
171 public override void CacheContent()
175 public override void DrawCachedBitmap(Rectangle rect)
179 public override bool IsVaildCache()
184 public void DrawOneLine(Document doc,LineToIndexTable lti, int row, double x, double y)
186 this.DrawOneLine(doc,