OSDN Git Service

※ まだ正常な表示にはなっておりません。あしからずご承知おきください。
[dtxmania/dtxmania.git] / DTXManiaプロジェクト / コード / 全体 / CPrivateFastFont.cs
1 using System;\r
2 using System.Collections.Generic;\r
3 using System.Runtime.InteropServices;\r
4 using System.Text;\r
5 using System.Drawing;\r
6 using System.Drawing.Drawing2D;\r
7 using System.Diagnostics;\r
8 \r
9 namespace DTXMania\r
10 {\r
11         /// <summary>\r
12         /// 高速描画版のCPrivateFontクラス。\r
13         /// といっても、一度レンダリングした結果をキャッシュして使いまわしているだけ。\r
14         /// </summary>\r
15         public class CPrivateFastFont : CPrivateFont\r
16         {\r
17                 /// <summary>\r
18                 /// キャッシュ容量\r
19                 /// </summary>\r
20                 private const int MAXCACHESIZE = 128;\r
21 \r
22                 private struct FontCache\r
23                 {\r
24                         // public Font font;\r
25                         public string drawstr;\r
26                         public DrawMode drawmode;\r
27                         public Color fontColor;\r
28                         public Color edgeColor;\r
29                         public Color gradationTopColor;\r
30                         public Color gradationBottomColor;\r
31                         public Bitmap bmp;\r
32                         public Rectangle rectStrings;\r
33                         public Point ptOrigin;\r
34                 }\r
35                 private List<FontCache> listFontCache;\r
36 \r
37 \r
38                 #region [ コンストラクタ ]\r
39                 public CPrivateFastFont( FontFamily fontfamily, int pt, FontStyle style )\r
40                 {\r
41                         Initialize( null, fontfamily, pt, style );\r
42                 }\r
43                 public CPrivateFastFont( FontFamily fontfamily, int pt )\r
44                 {\r
45                         Initialize( null, fontfamily, pt, FontStyle.Regular );\r
46                 }\r
47                 public CPrivateFastFont( string fontpath, int pt, FontStyle style )\r
48                 {\r
49                         Initialize( fontpath, null, pt, style );\r
50                 }\r
51                 public CPrivateFastFont( string fontpath, int pt )\r
52                 {\r
53                         Initialize( fontpath, null, pt, FontStyle.Regular );\r
54                 }\r
55                 public CPrivateFastFont()\r
56                 {\r
57                         throw new ArgumentException("CPrivateFastFont: 引数があるコンストラクタを使用してください。");\r
58                 }\r
59                 #endregion\r
60                 #region [ コンストラクタから呼ばれる初期化処理 ]\r
61                 protected new void Initialize( string fontpath, FontFamily fontfamily, int pt, FontStyle style )\r
62                 {\r
63                         this.bDispose完了済み_CPrivateFastFont = false;\r
64                         this.listFontCache = new List<FontCache>();\r
65                         base.Initialize( fontpath, fontfamily, pt, style );\r
66                 }\r
67                 #endregion\r
68 \r
69 \r
70                 #region [ DrawPrivateFontのオーバーロード群 ]\r
71                 /// <summary>\r
72                 /// 文字列を描画したテクスチャを返す\r
73                 /// </summary>\r
74                 /// <param name="drawstr">描画文字列</param>\r
75                 /// <param name="fontColor">描画色</param>\r
76                 /// <returns>描画済テクスチャ</returns>\r
77                 public new Bitmap DrawPrivateFont( string drawstr, Color fontColor )\r
78                 {\r
79                         return DrawPrivateFont( drawstr, DrawMode.Normal, fontColor, Color.White, Color.White, Color.White );\r
80                 }\r
81 \r
82                 /// <summary>\r
83                 /// 文字列を描画したテクスチャを返す\r
84                 /// </summary>\r
85                 /// <param name="drawstr">描画文字列</param>\r
86                 /// <param name="fontColor">描画色</param>\r
87                 /// <param name="edgeColor">縁取色</param>\r
88                 /// <returns>描画済テクスチャ</returns>\r
89                 public new Bitmap DrawPrivateFont( string drawstr, Color fontColor, Color edgeColor )\r
90                 {\r
91                         return DrawPrivateFont( drawstr, DrawMode.Edge, fontColor, edgeColor, Color.White, Color.White );\r
92                 }\r
93 \r
94                 /// <summary>\r
95                 /// 文字列を描画したテクスチャを返す\r
96                 /// </summary>\r
97                 /// <param name="drawstr">描画文字列</param>\r
98                 /// <param name="fontColor">描画色</param>\r
99                 /// <param name="gradationTopColor">グラデーション 上側の色</param>\r
100                 /// <param name="gradationBottomColor">グラデーション 下側の色</param>\r
101                 /// <returns>描画済テクスチャ</returns>\r
102                 //public new Bitmap DrawPrivateFont( string drawstr, Color fontColor, Color gradationTopColor, Color gradataionBottomColor )\r
103                 //{\r
104                 //    return DrawPrivateFont( drawstr, DrawMode.Gradation, fontColor, Color.White, gradationTopColor, gradataionBottomColor );\r
105                 //}\r
106 \r
107                 /// <summary>\r
108                 /// 文字列を描画したテクスチャを返す\r
109                 /// </summary>\r
110                 /// <param name="drawstr">描画文字列</param>\r
111                 /// <param name="fontColor">描画色</param>\r
112                 /// <param name="edgeColor">縁取色</param>\r
113                 /// <param name="gradationTopColor">グラデーション 上側の色</param>\r
114                 /// <param name="gradationBottomColor">グラデーション 下側の色</param>\r
115                 /// <returns>描画済テクスチャ</returns>\r
116                 public new Bitmap DrawPrivateFont( string drawstr, Color fontColor, Color edgeColor, Color gradationTopColor, Color gradataionBottomColor )\r
117                 {\r
118                         return DrawPrivateFont( drawstr, DrawMode.Edge | DrawMode.Gradation, fontColor, edgeColor, gradationTopColor, gradataionBottomColor );\r
119                 }\r
120 \r
121 #if こちらは使わない // (Bitmapではなく、CTextureを返す版)\r
122                 /// <summary>\r
123                 /// 文字列を描画したテクスチャを返す\r
124                 /// </summary>\r
125                 /// <param name="drawstr">描画文字列</param>\r
126                 /// <param name="fontColor">描画色</param>\r
127                 /// <returns>描画済テクスチャ</returns>\r
128                 public CTexture DrawPrivateFont( string drawstr, Color fontColor )\r
129                 {\r
130                         Bitmap bmp = DrawPrivateFont( drawstr, DrawMode.Normal, fontColor, Color.White, Color.White, Color.White );\r
131                         return CDTXMania.tテクスチャの生成( bmp, false );\r
132                 }\r
133 \r
134                 /// <summary>\r
135                 /// 文字列を描画したテクスチャを返す\r
136                 /// </summary>\r
137                 /// <param name="drawstr">描画文字列</param>\r
138                 /// <param name="fontColor">描画色</param>\r
139                 /// <param name="edgeColor">縁取色</param>\r
140                 /// <returns>描画済テクスチャ</returns>\r
141                 public CTexture DrawPrivateFont( string drawstr, Color fontColor, Color edgeColor )\r
142                 {\r
143                         Bitmap bmp = DrawPrivateFont( drawstr, DrawMode.Edge, fontColor, edgeColor, Color.White, Color.White );\r
144                         return CDTXMania.tテクスチャの生成( bmp, false );\r
145                 }\r
146 \r
147                 /// <summary>\r
148                 /// 文字列を描画したテクスチャを返す\r
149                 /// </summary>\r
150                 /// <param name="drawstr">描画文字列</param>\r
151                 /// <param name="fontColor">描画色</param>\r
152                 /// <param name="gradationTopColor">グラデーション 上側の色</param>\r
153                 /// <param name="gradationBottomColor">グラデーション 下側の色</param>\r
154                 /// <returns>描画済テクスチャ</returns>\r
155                 //public CTexture DrawPrivateFont( string drawstr, Color fontColor, Color gradationTopColor, Color gradataionBottomColor )\r
156                 //{\r
157                 //    Bitmap bmp = DrawPrivateFont( drawstr, DrawMode.Gradation, fontColor, Color.White, gradationTopColor, gradataionBottomColor );\r
158                 //        return CDTXMania.tテクスチャの生成( bmp, false );\r
159                 //}\r
160 \r
161                 /// <summary>\r
162                 /// 文字列を描画したテクスチャを返す\r
163                 /// </summary>\r
164                 /// <param name="drawstr">描画文字列</param>\r
165                 /// <param name="fontColor">描画色</param>\r
166                 /// <param name="edgeColor">縁取色</param>\r
167                 /// <param name="gradationTopColor">グラデーション 上側の色</param>\r
168                 /// <param name="gradationBottomColor">グラデーション 下側の色</param>\r
169                 /// <returns>描画済テクスチャ</returns>\r
170                 public CTexture DrawPrivateFont( string drawstr, Color fontColor, Color edgeColor,  Color gradationTopColor, Color gradataionBottomColor )\r
171                 {\r
172                         Bitmap bmp = DrawPrivateFont( drawstr, DrawMode.Edge | DrawMode.Gradation, fontColor, edgeColor, gradationTopColor, gradataionBottomColor );\r
173                         return CDTXMania.tテクスチャの生成( bmp, false );\r
174                 }\r
175 #endif\r
176                 #endregion\r
177 \r
178                 protected new Bitmap DrawPrivateFont( string drawstr, DrawMode drawmode, Color fontColor, Color edgeColor, Color gradationTopColor, Color gradationBottomColor )\r
179                 {\r
180                         #region [ 以前レンダリングしたことのある文字列/フォントか? (キャッシュにヒットするか?) ]\r
181                         int index = listFontCache.FindIndex(\r
182                                 delegate( FontCache fontcache )\r
183                                 {\r
184                                         return (\r
185                                                 drawstr == fontcache.drawstr &&\r
186                                                 drawmode == fontcache.drawmode &&\r
187                                                 fontColor == fontcache.fontColor &&\r
188                                                 edgeColor == fontcache.edgeColor &&\r
189                                                 gradationTopColor == fontcache.gradationTopColor &&\r
190                                                 gradationBottomColor == fontcache.gradationBottomColor\r
191                                                 // _font == fontcache.font\r
192                                         );\r
193                                 }\r
194                         );\r
195                         #endregion\r
196                         if ( index < 0 )\r
197                         {\r
198                                 // キャッシュにヒットせず。\r
199                                 #region [ レンダリングして、キャッシュに登録 ]\r
200                                 FontCache fc = new FontCache();\r
201                                 fc.bmp = base.DrawPrivateFont( drawstr, drawmode, fontColor, edgeColor, gradationTopColor, gradationBottomColor );\r
202                                 fc.drawstr = drawstr;\r
203                                 fc.drawmode = drawmode;\r
204                                 fc.fontColor = fontColor;\r
205                                 fc.edgeColor = edgeColor;\r
206                                 fc.gradationTopColor = gradationTopColor;\r
207                                 fc.gradationBottomColor = gradationBottomColor;\r
208                                 fc.rectStrings = RectStrings;\r
209                                 fc.ptOrigin = PtOrigin;\r
210                                 listFontCache.Add( fc );\r
211                                 // Debug.WriteLine( drawstr + ": Cacheにヒットせず。(cachesize=" + listFontCache.Count + ")" );\r
212                                 #endregion\r
213                                 #region [ もしキャッシュがあふれたら、最も古いキャッシュを破棄する ]\r
214                                 if ( listFontCache.Count > MAXCACHESIZE )\r
215                                 {\r
216                                         // Debug.WriteLine( "Cache溢れ。" + listFontCache[ 0 ].drawstr + " を解放します。" );\r
217                                         if ( listFontCache[ 0 ].bmp != null )\r
218                                         {\r
219                                                 listFontCache[ 0 ].bmp.Dispose();\r
220                                         }\r
221                                         listFontCache.RemoveAt( 0 );\r
222                                 }\r
223                                 #endregion\r
224 \r
225                                 // 呼び出し元のDispose()でキャッシュもDispose()されないように、Clone()で返す。\r
226                                 return (Bitmap)listFontCache[ listFontCache.Count - 1 ].bmp.Clone();\r
227                         }\r
228                         else\r
229                         {\r
230                                 // Debug.WriteLine( drawstr + ": Cacheにヒット!! index=" + index );\r
231                                 #region [ キャッシュにヒット。レンダリングは行わず、キャッシュ内のデータを返して終了。]\r
232                                 RectStrings = listFontCache[ index ].rectStrings;\r
233                                 PtOrigin = listFontCache[ index ].ptOrigin;\r
234                                 // 呼び出し元のDispose()でキャッシュもDispose()されないように、Clone()で返す。\r
235                                 return (Bitmap) listFontCache[ index ].bmp.Clone();\r
236                                 #endregion\r
237                         }\r
238                 }\r
239 \r
240                 #region [ IDisposable 実装 ]\r
241                 //-----------------\r
242                 public new void Dispose()\r
243                 {\r
244                         if ( !this.bDispose完了済み_CPrivateFastFont )\r
245                         {\r
246                                 if ( listFontCache != null )\r
247                                 {\r
248                                         //Debug.WriteLine( "Disposing CPrivateFastFont()" );\r
249                                         #region [ キャッシュしている画像を破棄する ]\r
250                                         foreach ( FontCache bc in listFontCache )\r
251 \r
252                                         {\r
253                                                 if ( bc.bmp != null )\r
254                                                 {\r
255                                                     bc.bmp.Dispose();\r
256                                                 }\r
257                                         }\r
258                                         #endregion\r
259                                         listFontCache.Clear();\r
260                                         listFontCache = null;\r
261                                 }\r
262                                 this.bDispose完了済み_CPrivateFastFont = true;\r
263                         }\r
264                         base.Dispose();\r
265                 }\r
266                 //-----------------\r
267                 #endregion\r
268 \r
269                 #region [ private ]\r
270                 //-----------------\r
271                 protected bool bDispose完了済み_CPrivateFastFont;\r
272                 //-----------------\r
273                 #endregion\r
274         }\r
275 }\r