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.Xaml.Media;
15 using Windows.UI.Xaml.Media.Imaging;
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;
27 namespace FooEditEngine
29 sealed class D2DPrintPreviewRender : D2DRenderBase, ITextRender, IPrintableTextRender
32 D3D11.Texture2D Texture;
34 D2D.DeviceContext D2DContext;
39 public D2DPrintPreviewRender(string fontName, double fontSize, Size size,float dpi)
45 base.ConstructDeviceResource(size.Width, size.Height);
46 base.InitTextFormat(fontName, (float)fontSize);
49 public override void GetDpi(out float dpix, out float dpiy)
55 public void DrawContent(PrintableView view, IPrintPreviewDxgiPackageTarget target, uint page)
58 view.Draw(view.PageBound);
60 target.DrawPage(page, this.Surface.NativePointer, this.dpi, this.dpi);
63 public void Resize(float width,float height)
65 this.ReConstructDeviceResource(width, height);
68 public void SetScale(float scale)
70 this.D2DContext.Transform = Matrix3x2.Scaling(1/scale);
73 protected override D2D.RenderTarget ConstructRender(D2D.Factory1 factory, D2D.RenderTargetProperties prop, double width, double height)
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;
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);
87 this.Surface = this.Texture.QueryInterface<DXGI.Surface>();
89 this.D2DDevice = new D2D.Device(factory, this.DXGIDevice);
91 this.D2DContext = new D2D.DeviceContext(this.D2DDevice, D2D.DeviceContextOptions.None);
93 this.D2DContext.DotsPerInch = new Size2F(this.dpi, this.dpi);
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);
99 this.Bitmap = new D2D.Bitmap1(this.D2DContext, this.Surface, bmpProp);
101 this.D2DContext.Target = this.Bitmap;
103 return this.D2DContext;
106 protected override void ConstrctedResource()
110 protected override void DestructRender()
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();
124 protected override void ReCreateTarget()
128 public float HeaderHeight
130 get { return (float)this.emSize.Height; }
133 public float FooterHeight
135 get { return (float)this.emSize.Height; }