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 if (this.textRender != null)
97 this.textRender.Dispose();
98 if (this.D2DDevice != null)
99 this.D2DDevice.Dispose();
100 if (this.D2DContext != null)
101 this.D2DContext.Dispose();
104 public FontFamily FontFamily
106 get { return this.fontFamily; }
109 this.fontFamily = value;
110 this.InitTextFormat(this.fontFamily.Source, (float)this.fontSize, this.GetDWFontWeigth(this.fontWeigth), this.GetDWFontStyle(this.fontStyle));
111 this.TabWidthChar = this.TabWidthChar;
115 public double FontSize
117 get { return this.fontSize; }
120 this.fontSize = value;
121 this.InitTextFormat(this.fontFamily.Source, (float)this.fontSize, this.GetDWFontWeigth(this.fontWeigth), this.GetDWFontStyle(this.fontStyle));
122 this.TabWidthChar = this.TabWidthChar;
126 public FontWeight FontWeigth
130 return this.fontWeigth;
134 this.fontWeigth = value;
135 this.InitTextFormat(this.fontFamily.Source, (float)this.fontSize, this.GetDWFontWeigth(value), this.GetDWFontStyle(this.fontStyle));
139 public FontStyle FontStyle
143 return this.fontStyle;
147 this.fontStyle = value;
148 this.InitTextFormat(this.fontFamily.Source, (float)this.fontSize, this.GetDWFontWeigth(this.fontWeigth), this.GetDWFontStyle(this.fontStyle));
152 DW.FontStyle GetDWFontStyle(FontStyle style)
154 return (DW.FontStyle)Enum.Parse(typeof(DW.FontStyle), style.ToString());
157 DW.FontWeight GetDWFontWeigth(FontWeight weigth)
159 if (weigth.Weight == 0)
160 return (DW.FontWeight)400;
162 return (DW.FontWeight)weigth.Weight;
165 public static Color4 ToColor4(Windows.UI.Color color)
167 return new Color4(color.R / 255.0f, color.G / 255.0f, color.B / 255.0f, color.A / 255.0f);
170 public override void CacheContent()
174 public override void DrawCachedBitmap(Rectangle rect)
178 public override bool IsVaildCache()
183 public void DrawOneLine(Document doc,LineToIndexTable lti, int row, double x, double y)
185 this.DrawOneLine(doc,