OSDN Git Service

71d13f8053b71acba96789d88d8d59df5287812d
[fooeditengine/FooEditEngine.git] / Metro / FooEditEngine / Print / D2DPrintPreviewRender.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.Xaml.Media;
15 using Windows.UI.Xaml.Media.Imaging;
16 using SharpDX;
17 using SharpDX.WIC;
18 using DXGI = SharpDX.DXGI;
19 using D2D = SharpDX.Direct2D1;
20 using D3D = SharpDX.Direct3D;
21 using D3D11 = SharpDX.Direct3D11;
22 using FooEditEngine.Metro;
23 using DotNetTextStore;
24 using DotNetTextStore.UnmanagedAPI.TSF;
25 using DotNetTextStore.UnmanagedAPI.WinDef;
26
27 namespace FooEditEngine
28 {
29     sealed class D2DPrintPreviewRender : D2DRenderBase, ITextRender, IPrintableTextRender
30     {
31         DXGI.Surface Surface;
32         D3D11.Texture2D Texture;
33         D2D.Device D2DDevice;
34         D2D.DeviceContext D2DContext;
35         D2D.Bitmap1 Bitmap;
36         Size Size;
37         float dpi;
38
39         public D2DPrintPreviewRender(string fontName, double fontSize, Size size,float dpi)
40             : base()
41         {
42             this.Size = size;
43             this.dpi = dpi;
44
45             base.ConstructDeviceResource(size.Width, size.Height);
46             base.InitTextFormat(fontName, (float)fontSize);
47         }
48
49         public override void GetDpi(out float dpix, out float dpiy)
50         {
51             dpix = this.dpi;
52             dpiy = this.dpi;
53         }
54
55         public void DrawContent(PrintableView view, IPrintPreviewDxgiPackageTarget target, uint page)
56         {
57             base.BegineDraw();
58             view.Draw(view.PageBound);
59             base.EndDraw();
60             target.DrawPage(page, this.Surface.NativePointer, this.dpi, this.dpi);
61         }
62
63         public void Resize(float width,float height)
64         {
65             this.ReConstructDeviceResource(width, height);
66         }
67
68         public void SetScale(float scale)
69         {
70             this.D2DContext.Transform = Matrix3x2.Scaling(1/scale);
71         }
72
73         protected override D2D.RenderTarget ConstructRender(D2D.Factory1 factory, D2D.RenderTargetProperties prop, double width, double height)
74         {
75             D3D11.Texture2DDescription desc = new D3D11.Texture2DDescription();
76             desc.Width = (int)(width * this.dpi / 96);
77             desc.Height = (int)(height * this.dpi / 96);
78             desc.Format = DXGI.Format.B8G8R8A8_UNorm;
79             desc.BindFlags = D3D11.BindFlags.RenderTarget | D3D11.BindFlags.ShaderResource;
80             desc.ArraySize = 1;
81             desc.MipLevels = 1;
82             desc.Usage = D3D11.ResourceUsage.Default;
83             desc.CpuAccessFlags = 0;
84             desc.SampleDescription = new DXGI.SampleDescription(1, 0);
85             this.Texture = new D3D11.Texture2D(base.D3DDevice, desc);
86
87             this.Surface = this.Texture.QueryInterface<DXGI.Surface>();
88
89             this.D2DDevice = new D2D.Device(factory, this.DXGIDevice);
90             
91             this.D2DContext = new D2D.DeviceContext(this.D2DDevice, D2D.DeviceContextOptions.None);
92
93             this.D2DContext.DotsPerInch = new Size2F(this.dpi, this.dpi);
94
95             D2D.BitmapProperties1 bmpProp = new D2D.BitmapProperties1();
96             bmpProp.BitmapOptions = D2D.BitmapOptions.Target | D2D.BitmapOptions.CannotDraw;
97             bmpProp.PixelFormat = new D2D.PixelFormat(DXGI.Format.B8G8R8A8_UNorm, D2D.AlphaMode.Premultiplied);
98
99             this.Bitmap = new D2D.Bitmap1(this.D2DContext, this.Surface, bmpProp);
100
101             this.D2DContext.Target = this.Bitmap;
102
103             return this.D2DContext;
104         }
105
106         protected override void ConstrctedResource()
107         {
108         }
109
110         protected override void DestructRender()
111         {
112             if (this.Texture != null)
113                 this.Texture.Dispose();
114             if (this.Surface != null)
115                 this.Surface.Dispose();
116             if (this.Bitmap != null)
117                 this.Bitmap.Dispose();
118             if (this.D2DDevice != null)
119                 this.D2DDevice.Dispose();
120             if (this.D2DContext != null)
121                 this.D2DContext.Dispose();
122         }
123
124         protected override void ReCreateTarget()
125         {
126         }
127
128         public float HeaderHeight
129         {
130             get { return (float)this.emSize.Height; }
131         }
132
133         public float FooterHeight
134         {
135             get { return (float)this.emSize.Height; }
136         }
137     }
138 }