CPF/CPF.ReoGrid/Common/ResourcePoolManager.cs

138 lines
3.0 KiB
C#
Raw Normal View History

2024-06-24 10:15:59 +08:00
using System;
using System.Collections.Generic;
using System.Linq;
using CPF.Drawing;
using CPF.ReoGrid.Drawing;
namespace CPF.ReoGrid.Common
{
internal sealed class ResourcePoolManager : IDisposable
{
internal ResourcePoolManager()
{
}
public SolidColorBrush GetBrush(Color color)
{
bool flag = color.A == 0;
SolidColorBrush result;
if (flag)
{
result = null;
}
else
{
Dictionary<Color, SolidColorBrush> obj = this.cachedBrushes;
lock (obj)
{
SolidColorBrush solidColorBrush;
bool flag3 = this.cachedBrushes.TryGetValue(color, out solidColorBrush);
if (flag3)
{
result = solidColorBrush;
}
else
{
solidColorBrush = new SolidColorBrush(color);
this.cachedBrushes.Add(color, solidColorBrush);
bool flag4 = this.cachedBrushes.Count % 10 == 0;
if (flag4)
{
Logger.Log("resource pool", "solid brush count: " + this.cachedBrushes.Count.ToString());
}
result = solidColorBrush;
}
}
}
return result;
}
public CPFPen GetPen(Color color)
{
return this.GetPen(color, 1f, DashStyles.Solid);
}
public CPFPen GetPen(Color color, float weight, DashStyles style)
{
bool flag = color.A == 0;
CPFPen result;
if (flag)
{
result = null;
}
else
{
Dictionary<Color, List<CPFPen>> obj = this.cachedPens;
CPFPen cpfpen;
lock (obj)
{
List<CPFPen> list;
bool flag3 = !this.cachedPens.TryGetValue(color, out list);
if (flag3)
{
list = (this.cachedPens[color] = new List<CPFPen>());
list.Add(cpfpen = new CPFPen(color, weight));
cpfpen.DashStyle = style;
}
else
{
List<CPFPen> obj2 = list;
lock (obj2)
{
cpfpen = list.FirstOrDefault((CPFPen p) => p.Stroke.Width == weight && p.DashStyle == style);
}
bool flag5 = cpfpen == null;
if (flag5)
{
list.Add(cpfpen = new CPFPen(color, weight));
cpfpen.DashStyle = style;
}
}
}
result = cpfpen;
}
return result;
}
public static Font CreateFont(string familyName, double emSizeD, FontStyles wfs)
{
float num = (float)emSizeD;
return new Font(familyName, num * 96f / 72f, wfs);
}
public Font GetFont(string familyName, double emSizeD, FontStyles wfs)
{
bool flag = string.IsNullOrEmpty(familyName);
if (flag)
{
familyName = "宋体";
}
return ResourcePoolManager.CreateFont(familyName, emSizeD, wfs);
}
internal void ReleaseAllResources()
{
int num = this.cachedPens.Count + this.cachedBrushes.Count;
foreach (List<CPFPen> list in this.cachedPens.Values)
{
list.Clear();
}
this.cachedPens.Clear();
this.cachedBrushes.Clear();
}
public void Dispose()
{
this.ReleaseAllResources();
}
private Dictionary<Color, SolidColorBrush> cachedBrushes = new Dictionary<Color, SolidColorBrush>();
private Dictionary<Color, List<CPFPen>> cachedPens = new Dictionary<Color, List<CPFPen>>();
internal class NoAvailableFontStyleException : Exception
{
}
}
}