OSDN Git Service

バージョンナンバーを増やした
[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 #if DEBUG
44             creationFlags |= SharpDX.Direct3D11.DeviceCreationFlags.Debug;
45 #endif
46             D3D.FeatureLevel[] featureLevels ={D3D.FeatureLevel.Level_11_1,
47                                                  D3D.FeatureLevel.Level_11_0,
48                                                  D3D.FeatureLevel.Level_10_1,
49                                                  D3D.FeatureLevel.Level_10_0,
50                                                  D3D.FeatureLevel.Level_9_3,
51                                                  D3D.FeatureLevel.Level_9_2,
52                                                  D3D.FeatureLevel.Level_9_1};
53             using (var device = new D3D11.Device(D3D.DriverType.Hardware, creationFlags, featureLevels))
54             {
55                 this.D3DDevice = device.QueryInterface<D3D11.Device1>();
56             }
57             this.DXGIDevice = this.D3DDevice.QueryInterface<DXGI.Device>();
58         }
59
60         public void InitTextFormat(string fontName, double size)
61         {
62             base.InitTextFormat(fontName, (float)size);
63             this.fontSize = size;
64         }
65
66         public void InitTextFormat(FontFamily font, double size)
67         {
68             base.InitTextFormat(font.Source, (float)size);
69             this.fontFamily = font;
70             this.fontSize = size;
71         }
72
73         protected override void Dispose(bool dispose)
74         {
75             base.Dispose(dispose);
76             this.DestructRenderAndResource();
77             if (this.DXGIDevice != null)
78                 this.DXGIDevice.Dispose();
79             if (this.D3DDevice != null)
80                 this.D3DDevice.Dispose();
81         }
82
83         public virtual void ConstructRenderAndResoruce(double width, double height)
84         {
85             this.D2DDevice = new D2D.Device(D2DRenderShared.D2DFactory, this.DXGIDevice);
86             this.D2DContext = new D2D.DeviceContext(this.D2DDevice, D2D.DeviceContextOptions.None);
87             float dpiX, dpiY;
88             this.GetDpi(out dpiX, out dpiY);
89             this.D2DContext.DotsPerInch = new Size2F(dpiX, dpiY);
90             this.render = this.D2DContext;
91             this.renderSize = new Size(width, height);
92             this.textRender = new CustomTextRenderer(this.Brushes, this.Strokes, this.Foreground);
93         }
94
95         public void ReinitTextFormat()
96         {
97             this.InitTextFormat(this.fontFamily.Source, (float)this.fontSize, this.GetDWFontWeigth(this.fontWeigth), this.GetDWFontStyle(this.fontStyle));
98         }
99
100         public virtual void DestructRenderAndResource()
101         {
102             this.Brushes.Clear();
103             this.Strokes.Clear();
104             if (this.textRender != null)
105                 this.textRender.Dispose();
106             if (this.D2DDevice != null)
107                 this.D2DDevice.Dispose();
108             if (this.D2DContext != null)
109                 this.D2DContext.Dispose();
110         }
111
112         public FontFamily FontFamily
113         {
114             get { return this.fontFamily; }
115             set
116             {
117                 this.fontFamily = value;
118                 this.InitTextFormat(this.fontFamily.Source, (float)this.fontSize, this.GetDWFontWeigth(this.fontWeigth), this.GetDWFontStyle(this.fontStyle));
119                 this.TabWidthChar = this.TabWidthChar;
120             }
121         }
122
123         public double FontSize
124         {
125             get { return this.fontSize; }
126             set
127             {
128                 this.fontSize = value;
129                 this.InitTextFormat(this.fontFamily.Source, (float)this.fontSize, this.GetDWFontWeigth(this.fontWeigth), this.GetDWFontStyle(this.fontStyle));
130                 this.TabWidthChar = this.TabWidthChar;
131             }
132         }
133
134         public FontWeight FontWeigth
135         {
136             get
137             {
138                 return this.fontWeigth;
139             }
140             set
141             {
142                 this.fontWeigth = value;
143                 this.InitTextFormat(this.fontFamily.Source, (float)this.fontSize, this.GetDWFontWeigth(value), this.GetDWFontStyle(this.fontStyle));
144             }
145         }
146
147         public FontStyle FontStyle
148         {
149             get
150             {
151                 return this.fontStyle;
152             }
153             set
154             {
155                 this.fontStyle = value;
156                 this.InitTextFormat(this.fontFamily.Source, (float)this.fontSize, this.GetDWFontWeigth(this.fontWeigth), this.GetDWFontStyle(this.fontStyle));
157             }
158         }
159
160         DW.FontStyle GetDWFontStyle(FontStyle style)
161         {
162             return (DW.FontStyle)Enum.Parse(typeof(DW.FontStyle), style.ToString());
163         }
164
165         DW.FontWeight GetDWFontWeigth(FontWeight weigth)
166         {
167             if (weigth.Weight == 0)
168                 return (DW.FontWeight)400;
169             else
170                 return (DW.FontWeight)weigth.Weight;
171         }
172
173         public static Color4 ToColor4(Windows.UI.Color color)
174         {
175             return new Color4(color.R / 255.0f, color.G / 255.0f, color.B / 255.0f, color.A / 255.0f);
176         }
177
178         public override void CacheContent()
179         {
180         }
181
182         public override void DrawCachedBitmap(Rectangle rect)
183         {
184         }
185
186         public override bool IsVaildCache()
187         {
188             return false;
189         }
190
191         public void DrawOneLine(Document doc, LineToIndexTable lti, int row, double x, double y)
192         {
193             this.DrawOneLine(doc,
194                 lti,
195                 row,
196                 x,
197                 y,
198                 null);
199         }
200     }
201 }