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;
44 creationFlags |= SharpDX.Direct3D11.DeviceCreationFlags.Debug;
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))
55 this.D3DDevice = device.QueryInterface<D3D11.Device1>();
57 this.DXGIDevice = this.D3DDevice.QueryInterface<DXGI.Device>();
60 public void InitTextFormat(string fontName, double size)
62 base.InitTextFormat(fontName, (float)size);
66 public void InitTextFormat(FontFamily font, double size)
68 base.InitTextFormat(font.Source, (float)size);
69 this.fontFamily = font;
73 protected override void Dispose(bool dispose)
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();
83 public virtual void ConstructRenderAndResoruce(double width, double height)
85 this.D2DDevice = new D2D.Device(D2DRenderShared.D2DFactory, this.DXGIDevice);
86 this.D2DContext = new D2D.DeviceContext(this.D2DDevice, D2D.DeviceContextOptions.None);
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);
95 public void ReinitTextFormat()
97 this.InitTextFormat(this.fontFamily.Source, (float)this.fontSize, this.GetDWFontWeigth(this.fontWeigth), this.GetDWFontStyle(this.fontStyle));
100 public virtual void DestructRenderAndResource()
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();
112 public FontFamily FontFamily
114 get { return this.fontFamily; }
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;
123 public double FontSize
125 get { return this.fontSize; }
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;
134 public FontWeight FontWeigth
138 return this.fontWeigth;
142 this.fontWeigth = value;
143 this.InitTextFormat(this.fontFamily.Source, (float)this.fontSize, this.GetDWFontWeigth(value), this.GetDWFontStyle(this.fontStyle));
147 public FontStyle FontStyle
151 return this.fontStyle;
155 this.fontStyle = value;
156 this.InitTextFormat(this.fontFamily.Source, (float)this.fontSize, this.GetDWFontWeigth(this.fontWeigth), this.GetDWFontStyle(this.fontStyle));
160 DW.FontStyle GetDWFontStyle(FontStyle style)
162 return (DW.FontStyle)Enum.Parse(typeof(DW.FontStyle), style.ToString());
165 DW.FontWeight GetDWFontWeigth(FontWeight weigth)
167 if (weigth.Weight == 0)
168 return (DW.FontWeight)400;
170 return (DW.FontWeight)weigth.Weight;
173 public static Color4 ToColor4(Windows.UI.Color color)
175 return new Color4(color.R / 255.0f, color.G / 255.0f, color.B / 255.0f, color.A / 255.0f);
178 public override void CacheContent()
182 public override void DrawCachedBitmap(Rectangle rect)
186 public override bool IsVaildCache()
191 public void DrawOneLine(Document doc, LineToIndexTable lti, int row, double x, double y)
193 this.DrawOneLine(doc,