OSDN Git Service

SharpDX3.xに移行した
[fooeditengine/FooEditEngine.git] / Windows / FooEditEngine / D2DTextRender.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 System.Linq;
14 using System.Text;
15 using System.Windows;
16 using System.Drawing;
17 using FooEditEngine;
18 using SharpDX;
19 using D2D = SharpDX.Direct2D1;
20 using DW = SharpDX.DirectWrite;
21 using DXGI = SharpDX.DXGI;
22 using System.Runtime.InteropServices;
23
24 namespace FooEditEngine.Windows
25 {
26
27     sealed class D2DTextRender : D2DRenderCommon, IEditorRender, IDisposable
28     {
29         FooTextBox TextBox;
30         string fontName;
31         float fontSize;
32
33         public D2DTextRender(FooTextBox textbox)
34         {
35             this.TextBox = textbox;
36
37             textbox.SizeChanged += new EventHandler(textbox_SizeChanged);
38             textbox.FontChanged += new EventHandler(textbox_FontChanged);
39
40             Size size = textbox.Size;
41             this.fontName = textbox.Font.Name;
42             this.fontSize = textbox.Font.Size;
43             this.InitTextFormat(textbox.Font.Name, (float)textbox.Font.Size);
44             //初期化ができないので適当なサイズで作る
45             this.ReConstructDeviceResource(100, 100);
46         }
47
48         public override void GetDpi(out float dpix, out float dpiy)
49         {
50             IntPtr hDc = NativeMethods.GetDC(IntPtr.Zero);
51             dpix = NativeMethods.GetDeviceCaps(hDc, NativeMethods.LOGPIXELSX);
52             dpiy = NativeMethods.GetDeviceCaps(hDc, NativeMethods.LOGPIXELSY);
53             NativeMethods.ReleaseDC(IntPtr.Zero, hDc);
54         }
55
56         void textbox_FontChanged(object sender, EventArgs e)
57         {
58             FooTextBox textbox = (FooTextBox)sender;
59             Font font = textbox.Font;
60             this.fontName = font.Name;
61             this.fontSize = font.Size;
62             DW.FontWeight weigth = font.Bold ? DW.FontWeight.Bold : DW.FontWeight.Normal;
63             DW.FontStyle style = font.Italic ? DW.FontStyle.Italic : DW.FontStyle.Normal;
64             this.InitTextFormat(font.Name, font.Size,weigth,style);
65         }
66
67         void textbox_SizeChanged(object sender, EventArgs e)
68         {
69             FooTextBox textbox = (FooTextBox)sender;
70             this.ReConstructDeviceResource(this.TextBox.Width, this.TextBox.Height);
71         }
72
73         public static Color4 ToColor4(System.Drawing.Color color)
74         {
75             return new Color4(color.R / 255.0f, color.G / 255.0f, color.B / 255.0f, color.A / 255.0f);
76         }
77
78
79         public void DrawOneLine(LineToIndexTable lti, int row, double x, double y, IEnumerable<Selection> SelectRanges)
80         {
81             this.DrawOneLine(lti,
82                 row,
83                 x,
84                 y,
85                 SelectRanges,
86                 null);
87         }
88
89         public override void DrawCachedBitmap(Rectangle rect)
90         {
91             if (this.render == null || this.render.IsDisposed)
92                 return;
93             render.DrawBitmap(this.cachedBitMap, rect, 1.0f, D2D.BitmapInterpolationMode.Linear, rect);
94         }
95
96         public override void CacheContent()
97         {
98             if (this.render == null || this.cachedBitMap == null || this.cachedBitMap.IsDisposed || this.render.IsDisposed)
99                 return;
100             //render.Flush();
101             this.cachedBitMap.CopyFromRenderTarget(this.render, new SharpDX.Point(), new SharpDX.Rectangle(0, 0, (int)this.renderSize.Width, (int)this.renderSize.Height));
102             this.hasCache = true;
103         }
104
105         public void DrawContent(EditView view,Rectangle updateRect)
106         {
107             base.BegineDraw();
108             view.Draw(updateRect);
109             base.EndDraw();
110         }
111
112         D2D.WindowRenderTarget WindowRender;
113
114         protected override D2D.RenderTarget ConstructRender(D2D.Factory factory, D2D.RenderTargetProperties prop, double width, double height)
115         {
116             D2D.HwndRenderTargetProperties hwndProp = new D2D.HwndRenderTargetProperties();
117             hwndProp.Hwnd = this.TextBox.Handle;
118             hwndProp.PixelSize = new SharpDX.Size2(this.TextBox.Size.Width,this.TextBox.Size.Height);
119             hwndProp.PresentOptions = D2D.PresentOptions.Immediately;
120             this.WindowRender = new D2D.WindowRenderTarget(factory,prop,hwndProp);
121             return this.WindowRender;
122         }
123
124         protected override void ConstrctedResource()
125         {
126             this.Foreground = ToColor4(this.TextBox.Foreground);
127             this.Background = ToColor4(this.TextBox.Background);
128             this.ControlChar = ToColor4(this.TextBox.ControlChar);
129             this.Url = ToColor4(this.TextBox.Url);
130             this.Keyword1 = ToColor4(this.TextBox.Keyword1);
131             this.Keyword2 = ToColor4(this.TextBox.Keyword2);
132             this.Literal = ToColor4(this.TextBox.Literal);
133             this.Comment = ToColor4(this.TextBox.Comment);
134             this.Hilight = ToColor4(this.TextBox.Hilight);
135             this.LineMarker = ToColor4(this.TextBox.LineMarker);
136             this.InsertCaret = ToColor4(this.TextBox.InsertCaret);
137             this.OverwriteCaret = ToColor4(this.TextBox.OverwriteCaret);
138             this.UpdateArea = ToColor4(this.TextBox.UpdateArea);
139         }
140
141         protected override void DestructRender()
142         {
143             if (this.WindowRender != null)
144                 this.WindowRender.Dispose();
145         }
146     }
147
148     internal static class NativeMethods
149     {
150         public const int LOGPIXELSX = 88;
151         public const int LOGPIXELSY = 90;
152
153         [DllImport("gdi32.dll")]
154         public static extern int GetDeviceCaps(IntPtr hDc, int nIndex);
155
156         [DllImport("user32.dll")]
157         public static extern IntPtr GetDC(IntPtr hWnd);
158
159         [DllImport("user32.dll")]
160         public static extern int ReleaseDC(IntPtr hWnd, IntPtr hDc);
161     }
162 }