OSDN Git Service

0ee7b5557c0ce2d26e4f40526e0ea32efdff34d8
[fooeditengine/FooEditEngine.git] / UWP / FooEditEngine.UWP / Direct2D / D2DRenderBase.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.Text;
15 using Windows.UI.Xaml.Media;
16 using Windows.UI.Xaml.Media.Imaging;
17 using SharpDX;
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;
24
25 namespace FooEditEngine
26 {
27     class D2DRenderBase: D2DRenderCommon,IDisposable
28     {
29         public new const int MiniumeWidth = 40;    //これ以上ないと誤操作が起こる
30
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;
38         double fontSize;
39
40         public D2DRenderBase()
41         {
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))
51             {
52                 this.D3DDevice = device.QueryInterface<D3D11.Device1>();
53             }
54             this.DXGIDevice = this.D3DDevice.QueryInterface<DXGI.Device>();
55         }
56
57         public void InitTextFormat(string fontName, double size)
58         {
59             base.InitTextFormat(fontName, (float)size);
60             this.fontSize = size;
61         }
62
63         public void InitTextFormat(FontFamily font, double size)
64         {
65             base.InitTextFormat(font.Source, (float)size);
66             this.fontFamily = font;
67             this.fontSize = size;
68         }
69
70         protected override void Dispose(bool dispose)
71         {
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();
78         }
79
80         public virtual void ConstructRenderAndResoruce(double width, double height)
81         {
82             this.D2DDevice = new D2D.Device(this.D2DFactory, this.DXGIDevice);
83             this.D2DContext = new D2D.DeviceContext(this.D2DDevice, D2D.DeviceContextOptions.None);
84             float dpiX, dpiY;
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);
90         }
91
92         public virtual void DestructRenderAndResource()
93         {
94             this.Brushes.Clear();
95             this.Strokes.Clear();
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();
103         }
104
105         public FontFamily FontFamily
106         {
107             get { return this.fontFamily; }
108             set
109             {
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;
113             }
114         }
115
116         public double FontSize
117         {
118             get { return this.fontSize; }
119             set
120             {
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;
124             }
125         }
126
127         public FontWeight FontWeigth
128         {
129             get
130             {
131                 return this.fontWeigth;
132             }
133             set
134             {
135                 this.fontWeigth = value;
136                 this.InitTextFormat(this.fontFamily.Source, (float)this.fontSize, this.GetDWFontWeigth(value), this.GetDWFontStyle(this.fontStyle));
137             }
138         }
139
140         public FontStyle FontStyle
141         {
142             get
143             {
144                 return this.fontStyle;
145             }
146             set
147             {
148                 this.fontStyle = value;
149                 this.InitTextFormat(this.fontFamily.Source, (float)this.fontSize, this.GetDWFontWeigth(this.fontWeigth), this.GetDWFontStyle(this.fontStyle));
150             }
151         }
152
153         DW.FontStyle GetDWFontStyle(FontStyle style)
154         {
155             return (DW.FontStyle)Enum.Parse(typeof(DW.FontStyle), style.ToString());
156         }
157
158         DW.FontWeight GetDWFontWeigth(FontWeight weigth)
159         {
160             if (weigth.Weight == 0)
161                 return (DW.FontWeight)400;
162             else
163                 return (DW.FontWeight)weigth.Weight;
164         }
165
166         public static Color4 ToColor4(Windows.UI.Color color)
167         {
168             return new Color4(color.R / 255.0f, color.G / 255.0f, color.B / 255.0f, color.A / 255.0f);
169         }
170
171         public override void CacheContent()
172         {
173         }
174
175         public override void DrawCachedBitmap(Rectangle rect)
176         {
177         }
178
179         public override bool IsVaildCache()
180         {
181             return false;
182         }
183
184         public void DrawOneLine(Document doc,LineToIndexTable lti, int row, double x, double y)
185         {
186             this.DrawOneLine(doc,
187                 lti,
188                 row,
189                 x,
190                 y,
191                 null);
192         }
193     }
194 }