2 using System.Collections.Generic;
3 using System.Runtime.InteropServices;
6 using System.Drawing.Drawing2D;
7 using System.Diagnostics;
12 /// 高速描画版のCPrivateFontクラス。
13 /// といっても、一度レンダリングした結果をキャッシュして使いまわしているだけ。
15 public class CPrivateFastFont : CPrivateFont
20 private const int MAXCACHESIZE = 128;
22 private struct FontCache
25 public string drawstr;
26 public DrawMode drawmode;
27 public Color fontColor;
28 public Color edgeColor;
29 public Color gradationTopColor;
30 public Color gradationBottomColor;
32 public Rectangle rectStrings;
33 public Point ptOrigin;
35 private List<FontCache> listFontCache;
39 public CPrivateFastFont(FontFamily fontfamily, float pt, FontStyle style)
41 Initialize(null, null, fontfamily, pt, style);
43 public CPrivateFastFont(FontFamily fontfamily, float pt)
45 Initialize(null, null, fontfamily, pt, FontStyle.Regular);
47 public CPrivateFastFont(string fontpath, float pt, FontStyle style)
49 Initialize(fontpath, null, null, pt, style);
51 public CPrivateFastFont(string fontpath, float pt)
53 Initialize(fontpath, null, null, pt, FontStyle.Regular);
55 public CPrivateFastFont()
57 throw new ArgumentException("CPrivateFastFont: 引数があるコンストラクタを使用してください。");
60 #region [ コンストラクタから呼ばれる初期化処理 ]
61 protected new void Initialize(string fontpath, string baseFontPath, FontFamily fontfamily, float pt, FontStyle style)
63 this.bDispose完了済み_CPrivateFastFont = false;
64 this.listFontCache = new List<FontCache>();
65 base.Initialize(fontpath, baseFontPath, fontfamily, pt, style);
70 #region [ DrawPrivateFontのオーバーロード群 ]
74 /// <param name="drawstr">描画文字列</param>
75 /// <param name="fontColor">描画色</param>
76 /// <returns>描画済テクスチャ</returns>
77 public new Bitmap DrawPrivateFont(string drawstr, Color fontColor)
79 return DrawPrivateFont(drawstr, DrawMode.Normal, fontColor, Color.White, Color.White, Color.White);
85 /// <param name="drawstr">描画文字列</param>
86 /// <param name="fontColor">描画色</param>
87 /// <param name="edgeColor">縁取色</param>
88 /// <returns>描画済テクスチャ</returns>
89 public new Bitmap DrawPrivateFont(string drawstr, Color fontColor, Color edgeColor)
91 return DrawPrivateFont(drawstr, DrawMode.Edge, fontColor, edgeColor, Color.White, Color.White);
97 /// <param name="drawstr">描画文字列</param>
98 /// <param name="fontColor">描画色</param>
99 /// <param name="gradationTopColor">グラデーション 上側の色</param>
100 /// <param name="gradationBottomColor">グラデーション 下側の色</param>
101 /// <returns>描画済テクスチャ</returns>
102 //public new Bitmap DrawPrivateFont( string drawstr, Color fontColor, Color gradationTopColor, Color gradataionBottomColor )
104 // return DrawPrivateFont( drawstr, DrawMode.Gradation, fontColor, Color.White, gradationTopColor, gradataionBottomColor );
110 /// <param name="drawstr">描画文字列</param>
111 /// <param name="fontColor">描画色</param>
112 /// <param name="edgeColor">縁取色</param>
113 /// <param name="gradationTopColor">グラデーション 上側の色</param>
114 /// <param name="gradationBottomColor">グラデーション 下側の色</param>
115 /// <returns>描画済テクスチャ</returns>
116 public new Bitmap DrawPrivateFont(string drawstr, Color fontColor, Color edgeColor, Color gradationTopColor, Color gradataionBottomColor)
118 return DrawPrivateFont(drawstr, DrawMode.Edge | DrawMode.Gradation, fontColor, edgeColor, gradationTopColor, gradataionBottomColor);
121 #if こちらは使わない // (Bitmapではなく、CTextureを返す版)
125 /// <param name="drawstr">描画文字列</param>
126 /// <param name="fontColor">描画色</param>
127 /// <returns>描画済テクスチャ</returns>
128 public CTexture DrawPrivateFont( string drawstr, Color fontColor )
130 Bitmap bmp = DrawPrivateFont( drawstr, DrawMode.Normal, fontColor, Color.White, Color.White, Color.White );
131 return TextureFactory.tテクスチャの生成( bmp, false );
137 /// <param name="drawstr">描画文字列</param>
138 /// <param name="fontColor">描画色</param>
139 /// <param name="edgeColor">縁取色</param>
140 /// <returns>描画済テクスチャ</returns>
141 public CTexture DrawPrivateFont( string drawstr, Color fontColor, Color edgeColor )
143 Bitmap bmp = DrawPrivateFont( drawstr, DrawMode.Edge, fontColor, edgeColor, Color.White, Color.White );
144 return TextureFactory.tテクスチャの生成( bmp, false );
150 /// <param name="drawstr">描画文字列</param>
151 /// <param name="fontColor">描画色</param>
152 /// <param name="gradationTopColor">グラデーション 上側の色</param>
153 /// <param name="gradationBottomColor">グラデーション 下側の色</param>
154 /// <returns>描画済テクスチャ</returns>
155 //public CTexture DrawPrivateFont( string drawstr, Color fontColor, Color gradationTopColor, Color gradataionBottomColor )
157 // Bitmap bmp = DrawPrivateFont( drawstr, DrawMode.Gradation, fontColor, Color.White, gradationTopColor, gradataionBottomColor );
158 // return TextureFactory.tテクスチャの生成( bmp, false );
164 /// <param name="drawstr">描画文字列</param>
165 /// <param name="fontColor">描画色</param>
166 /// <param name="edgeColor">縁取色</param>
167 /// <param name="gradationTopColor">グラデーション 上側の色</param>
168 /// <param name="gradationBottomColor">グラデーション 下側の色</param>
169 /// <returns>描画済テクスチャ</returns>
170 public CTexture DrawPrivateFont( string drawstr, Color fontColor, Color edgeColor, Color gradationTopColor, Color gradataionBottomColor )
172 Bitmap bmp = DrawPrivateFont( drawstr, DrawMode.Edge | DrawMode.Gradation, fontColor, edgeColor, gradationTopColor, gradataionBottomColor );
173 return TextureFactory.tテクスチャの生成( bmp, false );
178 protected new Bitmap DrawPrivateFont(string drawstr, DrawMode drawmode, Color fontColor, Color edgeColor, Color gradationTopColor, Color gradationBottomColor)
180 #region [ 以前レンダリングしたことのある文字列/フォントか? (キャッシュにヒットするか?) ]
181 int index = listFontCache.FindIndex(
182 delegate (FontCache fontcache)
185 drawstr == fontcache.drawstr &&
186 drawmode == fontcache.drawmode &&
187 fontColor == fontcache.fontColor &&
188 edgeColor == fontcache.edgeColor &&
189 gradationTopColor == fontcache.gradationTopColor &&
190 gradationBottomColor == fontcache.gradationBottomColor
191 // _font == fontcache.font
199 #region [ レンダリングして、キャッシュに登録 ]
200 FontCache fc = new FontCache();
201 fc.bmp = base.DrawPrivateFont(drawstr, drawmode, fontColor, edgeColor, gradationTopColor, gradationBottomColor);
202 fc.drawstr = drawstr;
203 fc.drawmode = drawmode;
204 fc.fontColor = fontColor;
205 fc.edgeColor = edgeColor;
206 fc.gradationTopColor = gradationTopColor;
207 fc.gradationBottomColor = gradationBottomColor;
208 fc.rectStrings = RectStrings;
209 fc.ptOrigin = PtOrigin;
210 listFontCache.Add(fc);
211 // Debug.WriteLine( drawstr + ": Cacheにヒットせず。(cachesize=" + listFontCache.Count + ")" );
213 #region [ もしキャッシュがあふれたら、最も古いキャッシュを破棄する ]
214 if (listFontCache.Count > MAXCACHESIZE)
216 // Debug.WriteLine( "Cache溢れ。" + listFontCache[ 0 ].drawstr + " を解放します。" );
217 if (listFontCache[0].bmp != null)
219 listFontCache[0].bmp.Dispose();
221 listFontCache.RemoveAt(0);
225 // 呼び出し元のDispose()でキャッシュもDispose()されないように、Clone()で返す。
226 return (Bitmap)listFontCache[listFontCache.Count - 1].bmp.Clone();
230 // Debug.WriteLine( drawstr + ": Cacheにヒット!! index=" + index );
231 #region [ キャッシュにヒット。レンダリングは行わず、キャッシュ内のデータを返して終了。]
232 RectStrings = listFontCache[index].rectStrings;
233 PtOrigin = listFontCache[index].ptOrigin;
234 // 呼び出し元のDispose()でキャッシュもDispose()されないように、Clone()で返す。
235 return (Bitmap)listFontCache[index].bmp.Clone();
240 #region [ IDisposable 実装 ]
242 public new void Dispose()
244 if (!this.bDispose完了済み_CPrivateFastFont)
246 if (listFontCache != null)
248 //Debug.WriteLine( "Disposing CPrivateFastFont()" );
249 #region [ キャッシュしている画像を破棄する ]
250 foreach (FontCache bc in listFontCache)
258 listFontCache.Clear();
259 listFontCache = null;
261 this.bDispose完了済み_CPrivateFastFont = true;
270 protected bool bDispose完了済み_CPrivateFastFont;