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         FontFamily fontFamily;
34         FontStyle fontStyle = FontStyle.Normal;
35         FontWeight fontWeigth;
36         double fontSize;
37
38         public D2DRenderBase()
39         {
40             var creationFlags = SharpDX.Direct3D11.DeviceCreationFlags.VideoSupport | SharpDX.Direct3D11.DeviceCreationFlags.BgraSupport;
41             D3D.FeatureLevel[] featureLevels ={D3D.FeatureLevel.Level_11_1,
42                                                  D3D.FeatureLevel.Level_11_0,
43                                                  D3D.FeatureLevel.Level_10_1,
44                                                  D3D.FeatureLevel.Level_10_0,
45                                                  D3D.FeatureLevel.Level_9_3,
46                                                  D3D.FeatureLevel.Level_9_2,
47                                                  D3D.FeatureLevel.Level_9_1};
48             using (var device = new D3D11.Device(D3D.DriverType.Hardware, creationFlags, featureLevels))
49             {
50                 this.D3DDevice = device.QueryInterface<D3D11.Device1>();
51             }
52             this.DXGIDevice = this.D3DDevice.QueryInterface<DXGI.Device>();
53         }
54
55         public void InitTextFormat(string fontName, double size)
56         {
57             base.InitTextFormat(fontName, (float)size);
58             this.fontSize = size;
59         }
60
61         public void InitTextFormat(FontFamily font, double size)
62         {
63             base.InitTextFormat(font.Source, (float)size);
64             this.fontFamily = font;
65             this.fontSize = size;
66         }
67
68         public override void Dispose()
69         {
70             if (this.DXGIDevice != null)
71                 this.DXGIDevice.Dispose();
72             if (this.D3DDevice != null)
73                 this.D3DDevice.Dispose();
74             base.Dispose();
75         }
76
77         public FontFamily FontFamily
78         {
79             get { return this.fontFamily; }
80             set
81             {
82                 this.fontFamily = value;
83                 this.InitTextFormat(this.fontFamily.Source, (float)this.fontSize, this.GetDWFontWeigth(this.fontWeigth), this.GetDWFontStyle(this.fontStyle));
84                 this.TabWidthChar = this.TabWidthChar;
85             }
86         }
87
88         public double FontSize
89         {
90             get { return this.fontSize; }
91             set
92             {
93                 this.fontSize = value;
94                 this.InitTextFormat(this.fontFamily.Source, (float)this.fontSize, this.GetDWFontWeigth(this.fontWeigth), this.GetDWFontStyle(this.fontStyle));
95                 this.TabWidthChar = this.TabWidthChar;
96             }
97         }
98
99         public FontWeight FontWeigth
100         {
101             get
102             {
103                 return this.fontWeigth;
104             }
105             set
106             {
107                 this.fontWeigth = value;
108                 this.InitTextFormat(this.fontFamily.Source, (float)this.fontSize, this.GetDWFontWeigth(value), this.GetDWFontStyle(this.fontStyle));
109             }
110         }
111
112         public FontStyle FontStyle
113         {
114             get
115             {
116                 return this.fontStyle;
117             }
118             set
119             {
120                 this.fontStyle = value;
121                 this.InitTextFormat(this.fontFamily.Source, (float)this.fontSize, this.GetDWFontWeigth(this.fontWeigth), this.GetDWFontStyle(this.fontStyle));
122             }
123         }
124
125         DW.FontStyle GetDWFontStyle(FontStyle style)
126         {
127             return (DW.FontStyle)Enum.Parse(typeof(DW.FontStyle), style.ToString());
128         }
129
130         DW.FontWeight GetDWFontWeigth(FontWeight weigth)
131         {
132             if (weigth.Weight == 0)
133                 return (DW.FontWeight)400;
134             else
135                 return (DW.FontWeight)weigth.Weight;
136         }
137
138         public static Color4 ToColor4(Windows.UI.Color color)
139         {
140             return new Color4(color.R / 255.0f, color.G / 255.0f, color.B / 255.0f, color.A / 255.0f);
141         }
142
143         public override void CacheContent()
144         {
145         }
146
147         public override void DrawCachedBitmap(Rectangle rect)
148         {
149         }
150
151         public override bool IsVaildCache()
152         {
153             return false;
154         }
155
156         public void DrawOneLine(Document doc,LineToIndexTable lti, int row, double x, double y)
157         {
158             this.DrawOneLine(doc,
159                 lti,
160                 row,
161                 x,
162                 y,
163                 null);
164         }
165     }
166 }