class D2DRenderCommon : IDisposable
{
- ColorBrushCollection Brushes = new ColorBrushCollection();
- StrokeCollection Strokes = new StrokeCollection();
InlineManager HiddenChars;
TextAntialiasMode _TextAntialiasMode;
Color4 _ControlChar,_Forground,_URL,_Hilight;
DW.Factory DWFactory;
-#if METRO || WINDOWS_UWP
- D2D.Factory1 D2DFactory;
-#else
- D2D.Factory D2DFactory;
-#endif
DW.TextFormat format;
+ protected CustomTextRenderer textRender;
protected D2D.Bitmap cachedBitMap;
- CustomTextRenderer textRender;
int tabLength = 8;
bool _ShowLineBreak;
Color4 _Comment, _Literal, _Keyword1, _Keyword2;
protected bool hasCache = false;
+ protected Size renderSize;
+
+#if METRO || WINDOWS_UWP
+ protected D2D.Factory1 D2DFactory
+ {
+ get;
+ private set;
+ }
+#else
+ protected D2D.Factory D2DFactory
+ {
+ get;
+ private set;
+ }
+#endif
- protected Size renderSize
+ protected ColorBrushCollection Brushes
{
get;
private set;
}
- protected D2D.RenderTarget render
+ protected StrokeCollection Strokes
{
get;
private set;
}
+ D2D.RenderTarget _render;
+ protected D2D.RenderTarget render
+ {
+ get { return _render; }
+ set
+ {
+ _render = value;
+ this.TextAntialiasMode = this._TextAntialiasMode;
+ this.Brushes.Render = this.render;
+ }
+ }
+
public D2DRenderCommon()
{
this.DWFactory = new DW.Factory(DW.FactoryType.Shared);
#else
this.D2DFactory = new D2D.Factory(D2D.FactoryType.MultiThreaded);
#endif
+ this.Brushes = new ColorBrushCollection();
+ this.Strokes = new StrokeCollection();
this.Strokes.Factory = this.D2DFactory;
this.ChangedRenderResource += (s, e) => { };
this.ChangedRightToLeft += (s, e) => { };
this.render.DrawEllipse(ellipse, this.Brushes.Get(this.Foreground));
}
- public void ReConstructDeviceResource()
- {
- this.ReConstructDeviceResource(this.renderSize.Width, this.renderSize.Height);
- }
-
- public void ReConstructDeviceResource(double width, double height)
- {
- this.DestructDeviceResource();
- this.ConstructDeviceResource(width, height);
- }
public virtual void DrawCachedBitmap(Rectangle rect)
{
return output;
}
- public virtual void Dispose()
- {
- this.DestructDeviceResource();
- this.HiddenChars.Clear();
- if (this.format != null)
- this.format.Dispose();
- if(this.DWFactory != null)
- this.DWFactory.Dispose();
- if(this.D2DFactory != null)
- this.D2DFactory.Dispose();
- }
-
- public void ConstructDeviceResource(double width, double height)
- {
- float dpiX, dpiY;
- this.GetDpi(out dpiX, out dpiY);
- D2D.RenderTargetProperties prop = new D2D.RenderTargetProperties(
- D2D.RenderTargetType.Default,
- new D2D.PixelFormat(DXGI.Format.B8G8R8A8_UNorm, D2D.AlphaMode.Premultiplied),
- dpiX,
- dpiY,
- D2D.RenderTargetUsage.None,
- D2D.FeatureLevel.Level_DEFAULT);
-
- this.render = this.ConstructRender(this.D2DFactory,prop,width,height);
-
- this.Brushes.Render = this.render;
-
- D2D.BitmapProperties bmpProp = new D2D.BitmapProperties();
- bmpProp.DpiX = dpiX;
- bmpProp.DpiY = dpiY;
- bmpProp.PixelFormat = new D2D.PixelFormat(DXGI.Format.B8G8R8A8_UNorm, D2D.AlphaMode.Premultiplied);
- this.cachedBitMap = new D2D.Bitmap(this.render, new SharpDX.Size2((int)width, (int)height), bmpProp);
- this.hasCache = false;
-
- this.ConstrctedResource();
-
- this.textRender = new CustomTextRenderer(this.render, this.Brushes,this.Strokes, this.Foreground);
-
- this.renderSize = new Size(width,height);
-
- this.TextAntialiasMode = this._TextAntialiasMode;
- }
-
-#if METRO || WINDOWS_UWP
- protected virtual D2D.RenderTarget ConstructRender(D2D.Factory1 factory, D2D.RenderTargetProperties prop, double width, double height)
-#else
- protected virtual D2D.RenderTarget ConstructRender(D2D.Factory factory, D2D.RenderTargetProperties prop, double width, double height)
-#endif
+ bool _Disposed = false;
+ public void Dispose()
{
- throw new NotImplementedException();
+ if (!_Disposed)
+ {
+ this.Dispose(true);
+ this.HiddenChars.Clear();
+ if (this.format != null)
+ this.format.Dispose();
+ if (this.DWFactory != null)
+ this.DWFactory.Dispose();
+ if (this.D2DFactory != null)
+ this.D2DFactory.Dispose();
+ }
+ this._Disposed = true;
}
- protected virtual void ConstrctedResource()
+ protected virtual void Dispose(bool dispose)
{
- throw new NotImplementedException();
}
public virtual void GetDpi(out float dpix,out float dpiy)
return dpi / 96.0;
}
- protected virtual void DestructRender()
- {
- throw new NotImplementedException();
-
- }
-
- protected virtual void ReCreateTarget()
- {
- throw new NotImplementedException();
- }
-
- public void DestructDeviceResource()
- {
- this.hasCache = false;
- if (this.cachedBitMap != null)
- this.cachedBitMap.Dispose();
- this.Brushes.Clear();
- this.Strokes.Clear();
- if (this.textRender != null)
- this.textRender.Dispose();
- this.DestructRender();
- }
-
void ParseLayout(MyTextLayout layout, string str)
{
for (int i = 0; i < str.Length; i++)
{
SurfaceImageSource SurfaceImage;
DXGI.ISurfaceImageSourceNative SurfaceImageNative;
- D2D.Device D2DDevice;
- D2D.DeviceContext D2DContext;
D2D.Bitmap1 Bitmap;
Size Size = new Size();
DXGI.Surface Surface;
public D2DRender(FooTextBox textbox, Windows.UI.Xaml.Shapes.Rectangle rect)
: base()
{
- this.ConstructDeviceResource(rect.ActualWidth, rect.ActualHeight);
+ this.ConstructRenderAndResoruce(rect.ActualWidth, rect.ActualHeight);
this.InitTextFormat(textbox.FontFamily, textbox.FontSize);
this.Size.Width = rect.ActualWidth;
{
if (this.Size.Width == width && this.Size.Height == height)
return false;
- this.ReConstructDeviceResource(width, height);
+ this.ReConstructRenderAndResource(width, height);
this.CreateSurface(rect, width, height);
return true;
}
//デバイス依存の座標を渡さないといけない
this.Surface = this.SurfaceImageNative.BeginDraw(
new SharpDX.Rectangle(0, 0, (int)(this.Size.Width * this.GetScale()), (int)(this.Size.Height * this.GetScale())), out offset);
- var prop = new D2D.BitmapProperties1();
- prop.BitmapOptions = D2D.BitmapOptions.Target | D2D.BitmapOptions.CannotDraw | D2D.BitmapOptions.GdiCompatible;
- prop.PixelFormat = this._RenderTargetProperties.PixelFormat;
- //prop.DpiX = this._RenderTargetProperties.DpiX;
- //prop.DpiY = this._RenderTargetProperties.DpiY;
this.Bitmap = new D2D.Bitmap1(this.D2DContext, this.Surface, null);
this.D2DContext.Target = this.Bitmap;
//こっちはDirect2Dなのでデバイス非依存の座標を渡す
rect.Fill = brush;
}
- D2D.RenderTargetProperties _RenderTargetProperties;
- protected override D2D.RenderTarget ConstructRender(D2D.Factory1 factory, D2D.RenderTargetProperties prop, double width, double height)
- {
- this.D2DDevice = new D2D.Device(factory,this.DXGIDevice);
- this.D2DContext = new D2D.DeviceContext(this.D2DDevice, D2D.DeviceContextOptions.None);
- this.D2DContext.DotsPerInch = new Size2F(prop.DpiX, prop.DpiY);
- this._RenderTargetProperties = prop;
- return this.D2DContext;
- }
-
- protected override void ConstrctedResource()
- {
- }
-
- protected override void DestructRender()
- {
- if (this.D2DDevice != null)
- this.D2DDevice.Dispose();
- if (this.D2DContext != null)
- this.D2DContext.Dispose();
- }
-
- protected override void ReCreateTarget()
+ public void ReConstructRenderAndResource(double width, double height)
{
+ this.DestructRenderAndResource();
+ this.ConstructRenderAndResoruce(width, height);
}
}
}
protected DXGI.Device DXGIDevice;
protected D3D11.Device1 D3DDevice;
+ protected D2D.Device D2DDevice;
+ protected D2D.DeviceContext D2DContext;
FontFamily fontFamily;
FontStyle fontStyle = FontStyle.Normal;
FontWeight fontWeigth;
this.fontSize = size;
}
- public override void Dispose()
+ protected override void Dispose(bool dispose)
{
+ base.Dispose(dispose);
+ this.DestructRenderAndResource();
if (this.DXGIDevice != null)
this.DXGIDevice.Dispose();
if (this.D3DDevice != null)
this.D3DDevice.Dispose();
- base.Dispose();
+ }
+
+ public virtual void ConstructRenderAndResoruce(double width, double height)
+ {
+ this.D2DDevice = new D2D.Device(this.D2DFactory, this.DXGIDevice);
+ this.D2DContext = new D2D.DeviceContext(this.D2DDevice, D2D.DeviceContextOptions.None);
+ float dpiX, dpiY;
+ this.GetDpi(out dpiX, out dpiY);
+ this.D2DContext.DotsPerInch = new Size2F(dpiX, dpiY);
+ this.render = this.D2DContext;
+ this.renderSize = new Size(width, height);
+ this.textRender = new CustomTextRenderer(this.render, this.Brushes, this.Strokes, this.Foreground);
+ }
+
+ public virtual void DestructRenderAndResource()
+ {
+ this.Brushes.Clear();
+ this.Strokes.Clear();
+ if (this.textRender != null)
+ this.textRender.Dispose();
+ if (this.D2DDevice != null)
+ this.D2DDevice.Dispose();
+ if (this.D2DContext != null)
+ this.D2DContext.Dispose();
}
public FontFamily FontFamily
{
DXGI.Surface Surface;
D3D11.Texture2D Texture;
- D2D.Device D2DDevice;
- D2D.DeviceContext D2DContext;
D2D.Bitmap1 Bitmap;
Size Size;
float dpi;
this.Size = size;
this.dpi = dpi;
- base.ConstructDeviceResource(size.Width, size.Height);
+ base.ConstructRenderAndResoruce(size.Width, size.Height);
base.InitTextFormat(fontName, (float)fontSize);
}
public void Resize(float width,float height)
{
- this.ReConstructDeviceResource(width, height);
+ this.DestructRenderAndResource();
+ this.ConstructRenderAndResoruce(width, height);
}
public void SetScale(float scale)
this.D2DContext.Transform = Matrix3x2.Scaling(1/scale);
}
- protected override D2D.RenderTarget ConstructRender(D2D.Factory1 factory, D2D.RenderTargetProperties prop, double width, double height)
+ public override void ConstructRenderAndResoruce(double width, double height)
{
D3D11.Texture2DDescription desc = new D3D11.Texture2DDescription();
desc.Width = (int)(width * this.dpi / 96);
this.Surface = this.Texture.QueryInterface<DXGI.Surface>();
- this.D2DDevice = new D2D.Device(factory, this.DXGIDevice);
-
- this.D2DContext = new D2D.DeviceContext(this.D2DDevice, D2D.DeviceContextOptions.None);
-
- this.D2DContext.DotsPerInch = new Size2F(this.dpi, this.dpi);
+ base.ConstructRenderAndResoruce(width, height);
D2D.BitmapProperties1 bmpProp = new D2D.BitmapProperties1();
bmpProp.BitmapOptions = D2D.BitmapOptions.Target | D2D.BitmapOptions.CannotDraw;
this.Bitmap = new D2D.Bitmap1(this.D2DContext, this.Surface, bmpProp);
this.D2DContext.Target = this.Bitmap;
-
- return this.D2DContext;
- }
-
- protected override void ConstrctedResource()
- {
+ this.D2DContext.DotsPerInch = new Size2F(this.dpi, this.dpi);
}
- protected override void DestructRender()
+ protected override void Dispose(bool dispose)
{
if (this.Bitmap != null)
this.Bitmap.Dispose();
- if (this.D2DContext != null)
- this.D2DContext.Dispose();
- if (this.D2DDevice != null)
- this.D2DDevice.Dispose();
+ base.Dispose(dispose);
if (this.Surface != null)
this.Surface.Dispose();
if (this.Texture != null)
this.Texture.Dispose();
}
- protected override void ReCreateTarget()
- {
- }
-
public float HeaderHeight
{
get { return (float)this.emSize.Height; }
{
sealed class D2DPrintRender : D2DRenderBase, ITextRender, IPrintableTextRender
{
- D2D.Device D2DDevice;
- D2D.DeviceContext D2DContext;
D2D.PrintControl Control;
D2D.CommandList CommandList;
Size2F Size;
: base()
{
this.WICFactory = new ImagingFactory();
- base.ConstructDeviceResource(size.Width, size.Height);
+ base.ConstructRenderAndResoruce(size.Width, size.Height);
base.InitTextFormat(fontName, (float)fontSize);
this.docPackageTarget = new ComObject(docPackageTarget); //あとで廃棄する必要がある
this.dpi = (int)dpi;
}
- protected override D2D.RenderTarget ConstructRender(D2D.Factory1 factory, D2D.RenderTargetProperties prop, double width, double height)
- {
- this.D2DDevice = new D2D.Device(factory,this.DXGIDevice);
- this.D2DContext = new D2D.DeviceContext(this.D2DDevice, D2D.DeviceContextOptions.None);
- return this.D2DContext;
- }
-
- protected override void ConstrctedResource()
- {
- }
-
- protected override void DestructRender()
+ public override void DestructRenderAndResource()
{
if (this.Control != null)
{
this.Control.Close();
this.Control.Dispose();
}
- if (this.D2DDevice != null)
- this.D2DDevice.Dispose();
- if (this.D2DContext != null)
- this.D2DContext.Dispose();
+ base.DestructRenderAndResource();
if (this.docPackageTarget != null)
this.docPackageTarget.Dispose();
if (this.WICFactory != null)
this.WICFactory.Dispose();
}
- protected override void ReCreateTarget()
- {
- }
-
public float HeaderHeight
{
get { return (float)this.emSize.Height; }
D3D11.Device device;
D3D9.Device device9;
D3D9.Surface surface9;
- new D2D.RenderTarget render;
double fontSize;
FontFamily fontFamily;
FontWeight fontWeigth;
this.CreateDevice();
- this.ConstructDeviceResource(width, height);
+ this.ConstructRenderAndResource(width, height);
this.InitTextFormat(this.fontFamily.Source, (float)this.fontSize, this.GetDWFontWeigth(this.fontWeigth), this.GetDWFontStyle(this.fontStyle));
this.imageSource = new D3DImage();
{
if (Math.Floor(width) != Math.Floor(this.imageSource.Width) || Math.Floor(height) != Math.Floor(this.imageSource.Height))
{
- this.ReConstructDeviceResource(width, height);
+ this.ReConstructRenderAndResource(width, height);
this.imageSource.Lock();
this.imageSource.SetBackBuffer(D3DResourceType.IDirect3DSurface9, this.surface9.NativePointer);
this.imageSource.Unlock();
return false;
}
+ public void ReConstructRenderAndResource(double width, double height)
+ {
+ this.DestructRenderAndResource();
+ this.ConstructRenderAndResource(width, height);
+ }
+
public void DrawContent(EditView view,bool IsEnabled,Rectangle updateRect)
{
if (this.imageSource.IsFrontBufferAvailable)
return new Color4(colorref.R / 255.0f, colorref.G / 255.0f, colorref.B / 255.0f, 1);
}
- public override void Dispose()
- {
- base.Dispose();
- this.DestructDevice();
- }
-
DW.FontStyle GetDWFontStyle(FontStyle style)
{
return (DW.FontStyle)Enum.Parse(typeof(DW.FontStyle), style.ToString());
}
}
- protected override void ReCreateTarget()
+ public void ConstructRenderAndResource(double width, double height)
{
- System.Diagnostics.Debug.WriteLine("ReCreatedDevice");
- this.DestructDevice();
- this.CreateDevice();
- this.ReConstructDeviceResource();
- }
+ float dpiX, dpiY;
+ this.GetDpi(out dpiX, out dpiY);
+ D2D.RenderTargetProperties prop = new D2D.RenderTargetProperties(
+ D2D.RenderTargetType.Default,
+ new D2D.PixelFormat(DXGI.Format.B8G8R8A8_UNorm, D2D.AlphaMode.Premultiplied),
+ dpiX,
+ dpiY,
+ D2D.RenderTargetUsage.None,
+ D2D.FeatureLevel.Level_DEFAULT);
- protected override D2D.RenderTarget ConstructRender(D2D.Factory factory, D2D.RenderTargetProperties prop, double width, double height)
- {
D3D11.Texture2DDescription desc = new D3D11.Texture2DDescription();
desc.Width = (int)width;
desc.Height = (int)height;
resource.Dispose();
texture.Dispose();
- this.render = new D2D.RenderTarget(factory, this.surface, prop);
- return this.render;
+ this.render = new D2D.RenderTarget(this.D2DFactory, this.surface, prop);
+
+ D2D.BitmapProperties bmpProp = new D2D.BitmapProperties();
+ bmpProp.DpiX = dpiX;
+ bmpProp.DpiY = dpiY;
+ bmpProp.PixelFormat = new D2D.PixelFormat(DXGI.Format.B8G8R8A8_UNorm, D2D.AlphaMode.Premultiplied);
+ this.cachedBitMap = new D2D.Bitmap(this.render, new SharpDX.Size2((int)width, (int)height), bmpProp);
+ this.hasCache = false;
+
+ this.textRender = new CustomTextRenderer(this.render, this.Brushes, this.Strokes, this.Foreground);
+
+ this.renderSize = new Size(width, height);
}
- protected override void ConstrctedResource()
+ public void DestructRenderAndResource()
{
+ this.hasCache = false;
+ if (this.cachedBitMap != null)
+ this.cachedBitMap.Dispose();
+ this.Brushes.Clear();
+ this.Strokes.Clear();
+ if (this.textRender != null)
+ this.textRender.Dispose();
+ if (this.texture != null)
+ this.texture.Dispose();
+ if (this.surface != null)
+ this.surface.Dispose();
+ if (this.surface9 != null)
+ this.surface9.Dispose();
+ if (this.render != null)
+ this.render.Dispose();
}
public override void GetDpi(out float dpix, out float dpiy)
dpiy = (int)dpiYProperty.GetValue(null, null);
}
- protected override void DestructRender()
+ protected override void Dispose(bool dispose)
{
- if (this.texture != null)
- this.texture.Dispose();
- if (this.surface != null)
- this.surface.Dispose();
- if (this.surface9 != null)
- this.surface9.Dispose();
- if (this.render != null)
- this.render.Dispose();
+ base.Dispose(dispose);
+ this.DestructRenderAndResource();
+ this.DestructDevice();
}
+
}
}
this.fontSize = textbox.Font.Size;
this.InitTextFormat(textbox.Font.Name, (float)textbox.Font.Size);
//初期化ができないので適当なサイズで作る
- this.ReConstructDeviceResource(100, 100);
+ this.ReConstructRenderAndResource(100, 100);
}
public override void GetDpi(out float dpix, out float dpiy)
void textbox_SizeChanged(object sender, EventArgs e)
{
FooTextBox textbox = (FooTextBox)sender;
- this.ReConstructDeviceResource(this.TextBox.Width, this.TextBox.Height);
+ this.ReConstructRenderAndResource(this.TextBox.Width, this.TextBox.Height);
}
public static Color4 ToColor4(System.Drawing.Color color)
base.EndDraw();
}
- D2D.WindowRenderTarget WindowRender;
+ public void ReConstructRenderAndResource(double width, double height)
+ {
+ this.DestructRenderAndResource();
+ this.ConstructRenderAndResource(width, height);
+ }
- protected override D2D.RenderTarget ConstructRender(D2D.Factory factory, D2D.RenderTargetProperties prop, double width, double height)
+ public void ConstructRenderAndResource(double width, double height)
{
+ float dpiX, dpiY;
+ this.GetDpi(out dpiX, out dpiY);
+ D2D.RenderTargetProperties prop = new D2D.RenderTargetProperties(
+ D2D.RenderTargetType.Default,
+ new D2D.PixelFormat(DXGI.Format.B8G8R8A8_UNorm, D2D.AlphaMode.Premultiplied),
+ dpiX,
+ dpiY,
+ D2D.RenderTargetUsage.None,
+ D2D.FeatureLevel.Level_DEFAULT);
+
D2D.HwndRenderTargetProperties hwndProp = new D2D.HwndRenderTargetProperties();
hwndProp.Hwnd = this.TextBox.Handle;
- hwndProp.PixelSize = new SharpDX.Size2(this.TextBox.Size.Width,this.TextBox.Size.Height);
+ hwndProp.PixelSize = new SharpDX.Size2(this.TextBox.Size.Width, this.TextBox.Size.Height);
hwndProp.PresentOptions = D2D.PresentOptions.Immediately;
- this.WindowRender = new D2D.WindowRenderTarget(factory,prop,hwndProp);
- return this.WindowRender;
- }
+ this.render = new D2D.WindowRenderTarget(this.D2DFactory, prop, hwndProp);
+
+ D2D.BitmapProperties bmpProp = new D2D.BitmapProperties();
+ bmpProp.DpiX = dpiX;
+ bmpProp.DpiY = dpiY;
+ bmpProp.PixelFormat = new D2D.PixelFormat(DXGI.Format.B8G8R8A8_UNorm, D2D.AlphaMode.Premultiplied);
+ this.cachedBitMap = new D2D.Bitmap(this.render, new SharpDX.Size2((int)width, (int)height), bmpProp);
+ this.hasCache = false;
+
+ this.textRender = new CustomTextRenderer(this.render, this.Brushes, this.Strokes, this.Foreground);
+
+ this.renderSize = new Size(width, height);
- protected override void ConstrctedResource()
- {
//デフォルト値を反映させる
this.Foreground = ToColor4(this.TextBox.Foreground);
this.Background = ToColor4(this.TextBox.Background);
this.HilightForeground = ToColor4(this.TextBox.HilightForeground);
}
- protected override void DestructRender()
+ public void DestructRenderAndResource()
{
- if (this.WindowRender != null)
- this.WindowRender.Dispose();
+ this.hasCache = false;
+ if (this.cachedBitMap != null)
+ this.cachedBitMap.Dispose();
+ this.Brushes.Clear();
+ this.Strokes.Clear();
+ if (this.textRender != null)
+ this.textRender.Dispose();
+ if (this.render != null)
+ this.render.Dispose();
}
}