リビジョン | 15c39353240b8cde4c604304327f0bca492bcb21 (tree) |
---|---|
日時 | 2018-03-08 02:50:49 |
作者 | yyagi <yyagi.dtxmania@gmai...> |
コミッター | yyagi |
#xxxxx CPrivateFont (とCPrivateFastFont) に以下の修正を適用。
なお、従来Font()で描画していた文字列は、DPI依存のサイズ指定となっていたため、(DPI非依存の)CPrivateFont経由でサイズ指定を変えずに描画すると大き目に表示される。そのため、Font()をCPrivateFont()に移行する際には、フォントサイズに72/96を掛けて帳尻を合わせる必要があるので注意。
@@ -36,19 +36,19 @@ namespace DTXMania | ||
36 | 36 | |
37 | 37 | |
38 | 38 | #region [ コンストラクタ ] |
39 | - public CPrivateFastFont(FontFamily fontfamily, int pt, FontStyle style) | |
39 | + public CPrivateFastFont(FontFamily fontfamily, float pt, FontStyle style) | |
40 | 40 | { |
41 | 41 | Initialize(null, null, fontfamily, pt, style); |
42 | 42 | } |
43 | - public CPrivateFastFont(FontFamily fontfamily, int pt) | |
43 | + public CPrivateFastFont(FontFamily fontfamily, float pt) | |
44 | 44 | { |
45 | 45 | Initialize(null, null, fontfamily, pt, FontStyle.Regular); |
46 | 46 | } |
47 | - public CPrivateFastFont(string fontpath, int pt, FontStyle style) | |
47 | + public CPrivateFastFont(string fontpath, float pt, FontStyle style) | |
48 | 48 | { |
49 | 49 | Initialize(fontpath, null, null, pt, style); |
50 | 50 | } |
51 | - public CPrivateFastFont(string fontpath, int pt) | |
51 | + public CPrivateFastFont(string fontpath, float pt) | |
52 | 52 | { |
53 | 53 | Initialize(fontpath, null, null, pt, FontStyle.Regular); |
54 | 54 | } |
@@ -58,7 +58,7 @@ namespace DTXMania | ||
58 | 58 | } |
59 | 59 | #endregion |
60 | 60 | #region [ コンストラクタから呼ばれる初期化処理 ] |
61 | - protected new void Initialize(string fontpath, string baseFontPath, FontFamily fontfamily, int pt, FontStyle style) | |
61 | + protected new void Initialize(string fontpath, string baseFontPath, FontFamily fontfamily, float pt, FontStyle style) | |
62 | 62 | { |
63 | 63 | this.bDispose完了済み_CPrivateFastFont = false; |
64 | 64 | this.listFontCache = new List<FontCache>(); |
@@ -24,7 +24,10 @@ namespace DTXMania | ||
24 | 24 | /// 簡単な使い方 |
25 | 25 | /// CPrivateFont prvFont = new CPrivateFont( CSkin.Path( @"Graphics\fonts\mplus-1p-bold.ttf" ), 36 ); // プライベートフォント |
26 | 26 | /// とか |
27 | - /// CPrivateFont prvFont = new CPrivateFont( new FontFamily("MS UI Gothic"), 36, FontStyle.Bold ); // システムフォント | |
27 | + /// CPrivateFont prvFont = new CPrivateFont( "MS UI Gothic", 36, FontStyle.Bold ); // システムフォント | |
28 | + /// CPrivateFont prvFont = new CPrivateFont( CSkin.Path( @"Graphics\fonts\Arial" ), 36 ); // システムフォント(拡張子の有無で判断。拡張子なしの場合はパス名を指定しても無視する) | |
29 | + /// とか | |
30 | + /// CPrivateFont prvFont = new CPrivateFont( new FontFamily("MS UI Gothic"), 36, FontStyle.Bold ); // システムフォント指定方法その2 | |
28 | 31 | /// とかした上で、 |
29 | 32 | /// Bitmap bmp = prvFont.DrawPrivateFont( "ABCDE", Color.White, Color.Black ); // フォント色=白、縁の色=黒の例。縁の色は省略可能 |
30 | 33 | /// とか |
@@ -43,24 +46,30 @@ namespace DTXMania | ||
43 | 46 | /// </remarks> |
44 | 47 | public class CPrivateFont : IDisposable |
45 | 48 | { |
49 | + public Font font | |
50 | + { | |
51 | + get => _font; | |
52 | + } | |
53 | + | |
54 | + | |
46 | 55 | #region [ コンストラクタ ] |
47 | - public CPrivateFont(FontFamily fontfamily, int pt, FontStyle style) | |
56 | + public CPrivateFont(FontFamily fontfamily, float pt, FontStyle style) | |
48 | 57 | { |
49 | 58 | Initialize(null, null, fontfamily, pt, style); |
50 | 59 | } |
51 | - public CPrivateFont(FontFamily fontfamily, int pt) | |
60 | + public CPrivateFont(FontFamily fontfamily, float pt) | |
52 | 61 | { |
53 | 62 | Initialize(null, null, fontfamily, pt, FontStyle.Regular); |
54 | 63 | } |
55 | - public CPrivateFont(string fontpath, FontFamily fontfamily, int pt, FontStyle style) | |
64 | + public CPrivateFont(string fontpath, FontFamily fontfamily, float pt, FontStyle style) | |
56 | 65 | { |
57 | 66 | Initialize(fontpath, null, fontfamily, pt, style); |
58 | 67 | } |
59 | - public CPrivateFont(string fontpath, int pt, FontStyle style) | |
68 | + public CPrivateFont(string fontpath, float pt, FontStyle style) | |
60 | 69 | { |
61 | 70 | Initialize(fontpath, null, null, pt, style); |
62 | 71 | } |
63 | - public CPrivateFont(string fontpath, int pt) | |
72 | + public CPrivateFont(string fontpath, float pt) | |
64 | 73 | { |
65 | 74 | Initialize(fontpath, null, null, pt, FontStyle.Regular); |
66 | 75 | } |
@@ -70,7 +79,7 @@ namespace DTXMania | ||
70 | 79 | } |
71 | 80 | #endregion |
72 | 81 | |
73 | - protected void Initialize(string fontpath, string baseFontPath, FontFamily fontfamily, int pt, FontStyle style) | |
82 | + protected void Initialize(string fontpath, string baseFontPath, FontFamily fontfamily, float pt, FontStyle style) | |
74 | 83 | { |
75 | 84 | this._pfc = null; |
76 | 85 | this._fontfamily = null; |
@@ -80,6 +89,16 @@ namespace DTXMania | ||
80 | 89 | this._ptOrigin = new Point(0, 0); |
81 | 90 | this.bDispose完了済み = false; |
82 | 91 | this._baseFontname = baseFontPath; |
92 | + this.bIsSystemFont = false; | |
93 | + | |
94 | + float emSize = 0f; | |
95 | + using (Bitmap b = new Bitmap(1, 1)) | |
96 | + { | |
97 | + using (Graphics g = Graphics.FromImage(b)) | |
98 | + { | |
99 | + emSize = pt * 96.0f / 72.0f * g.DpiX / 96.0f; // DPIを考慮したpxサイズ。GraphicsUnit.Pixelと併用のこと | |
100 | + } | |
101 | + } | |
83 | 102 | |
84 | 103 | if (fontfamily != null) |
85 | 104 | { |
@@ -89,9 +108,21 @@ namespace DTXMania | ||
89 | 108 | { |
90 | 109 | try |
91 | 110 | { |
92 | - this._pfc = new System.Drawing.Text.PrivateFontCollection(); //PrivateFontCollectionオブジェクトを作成する | |
93 | - this._pfc.AddFontFile(fontpath); //PrivateFontCollectionにフォントを追加する | |
94 | - _fontfamily = _pfc.Families[0]; | |
111 | + //拡張子あり == 通常のPrivateFontパス指定 | |
112 | + if (Path.GetExtension(fontpath) != string.Empty) | |
113 | + { | |
114 | + this._pfc = new System.Drawing.Text.PrivateFontCollection(); //PrivateFontCollectionオブジェクトを作成する | |
115 | + this._pfc.AddFontFile(fontpath); //PrivateFontCollectionにフォントを追加する | |
116 | + _fontfamily = _pfc.Families[0]; | |
117 | + bIsSystemFont = false; | |
118 | + } | |
119 | + //拡張子なし == Ariel, MS Gothicなど、システムフォントの指定 | |
120 | + else | |
121 | + { | |
122 | + //指定されたフォント名のFontオブジェクトを作成する | |
123 | + this._font = PublicFont(Path.GetFileName(fontpath), emSize, style, GraphicsUnit.Pixel); | |
124 | + bIsSystemFont = true; | |
125 | + } | |
95 | 126 | } |
96 | 127 | catch (Exception e) when (e is System.IO.FileNotFoundException || e is System.Runtime.InteropServices.ExternalException) |
97 | 128 | { |
@@ -119,9 +150,14 @@ namespace DTXMania | ||
119 | 150 | //} |
120 | 151 | } |
121 | 152 | |
122 | - // 指定されたフォントスタイルが適用できない場合は、フォント内で定義されているスタイルから候補を選んで使用する | |
153 | + // 通常フォントの登録に成功した場合 | |
154 | + if (bIsSystemFont && _font != null) | |
155 | + { | |
156 | + // 追加処理なし。何もしない | |
157 | + } | |
158 | + // PrivateFontの登録に成功したが、指定されたフォントスタイルが適用できない場合は、フォント内で定義されているスタイルから候補を選んで使用する | |
123 | 159 | // 何もスタイルが使えないようなフォントなら、例外を出す。 |
124 | - if (_fontfamily != null) | |
160 | + else if (_fontfamily != null) | |
125 | 161 | { |
126 | 162 | if (!_fontfamily.IsStyleAvailable(style)) |
127 | 163 | { |
@@ -141,32 +177,48 @@ namespace DTXMania | ||
141 | 177 | Trace.TraceWarning("フォント{0}は適切なスタイル{1}を選択できませんでした。", Path.GetFileName(fontpath), style.ToString()); |
142 | 178 | } |
143 | 179 | } |
144 | - //this._font = new Font(this._fontfamily, pt, style); //PrivateFontCollectionの先頭のフォントのFontオブジェクトを作成する | |
145 | - float emSize = pt * 96.0f / 72.0f; | |
146 | 180 | this._font = new Font(this._fontfamily, emSize, style, GraphicsUnit.Pixel); //PrivateFontCollectionの先頭のフォントのFontオブジェクトを作成する |
147 | - //HighDPI対応のため、pxサイズで指定 | |
148 | 181 | } |
182 | + // PrivateFontと通常フォント、どちらの登録もできていない場合は、MS PGothicを代わりに設定しようと試みる | |
149 | 183 | else |
150 | - // フォントファイルが見つからなかった場合 (MS PGothicを代わりに指定する) | |
151 | 184 | { |
152 | - float emSize = pt * 96.0f / 72.0f; | |
153 | - this._font = new Font("MS PGothic", emSize, style, GraphicsUnit.Pixel); //MS PGothicのFontオブジェクトを作成する | |
154 | - FontFamily[] ffs = new System.Drawing.Text.InstalledFontCollection().Families; | |
155 | - int lcid = System.Globalization.CultureInfo.GetCultureInfo("en-us").LCID; | |
156 | - foreach (FontFamily ff in ffs) | |
157 | - { | |
158 | - // Trace.WriteLine( lcid ) ); | |
159 | - if (ff.GetName(lcid) == "MS PGothic") | |
160 | - { | |
161 | - this._fontfamily = ff; | |
162 | - Trace.TraceInformation("MS PGothicを代わりに指定しました。"); | |
163 | - return; | |
164 | - } | |
185 | + this._font = PublicFont("MS PGothic", emSize, style, GraphicsUnit.Pixel); //MS PGothicのFontオブジェクトを作成する | |
186 | + if (this._font != null ) | |
187 | + { | |
188 | + Trace.TraceInformation("{0}の代わりにMS PGothicを指定しました。", Path.GetFileName(fontpath)); | |
189 | + bIsSystemFont = true; | |
190 | + return; | |
165 | 191 | } |
166 | 192 | throw new FileNotFoundException("プライベートフォントの追加に失敗し、MS PGothicでの代替処理にも失敗しました。({0})", Path.GetFileName(fontpath)); |
167 | 193 | } |
168 | 194 | } |
169 | 195 | |
196 | + | |
197 | + /// <summary> | |
198 | + /// プライベートフォントではない、通常のフォント指定 | |
199 | + /// </summary> | |
200 | + /// <param name="fontname">フォント名</param> | |
201 | + /// <param name="emSize">フォントサイズ</param> | |
202 | + /// <param name="style">フォントスタイル</param> | |
203 | + /// <param name="unit">GraphicsUnit</param> | |
204 | + /// <returns></returns> | |
205 | + private Font PublicFont(string fontname, float emSize, FontStyle style, GraphicsUnit unit) | |
206 | + { | |
207 | + Font f = new Font(fontname, emSize, style, unit); | |
208 | + FontFamily[] ffs = new System.Drawing.Text.InstalledFontCollection().Families; | |
209 | + int lcid = System.Globalization.CultureInfo.GetCultureInfo("en-us").LCID; | |
210 | + foreach (FontFamily ff in ffs) | |
211 | + { | |
212 | + // Trace.WriteLine( lcid ) ); | |
213 | + if (ff.GetName(lcid) == fontname) | |
214 | + { | |
215 | + this._fontfamily = ff; | |
216 | + return f; | |
217 | + } | |
218 | + } | |
219 | + return null; | |
220 | + } | |
221 | + | |
170 | 222 | [Flags] |
171 | 223 | protected enum DrawMode |
172 | 224 | { |
@@ -284,7 +336,7 @@ namespace DTXMania | ||
284 | 336 | #endregion |
285 | 337 | |
286 | 338 | /// <summary> |
287 | - /// 文字列を描画したテクスチャを返す(メイン処理) | |
339 | + /// 文字列を描画したbitmapを返す(メイン処理) | |
288 | 340 | /// </summary> |
289 | 341 | /// <param name="rectDrawn">描画された領域</param> |
290 | 342 | /// <param name="ptOrigin">描画文字列</param> |
@@ -314,7 +366,7 @@ namespace DTXMania | ||
314 | 366 | bool bGradation = ((drawmode & DrawMode.Gradation) == DrawMode.Gradation); |
315 | 367 | |
316 | 368 | // 縁取りの縁のサイズは、とりあえずフォントの大きさの1/4とする |
317 | - int nEdgePt = (bEdge) ? _pt / 4 : 0; | |
369 | + float nEdgePt = (bEdge) ? _pt / 4 : 0; | |
318 | 370 | |
319 | 371 | // 描画サイズを測定する |
320 | 372 | Size stringSize = System.Windows.Forms.TextRenderer.MeasureText(drawstr, this._font, new Size(int.MaxValue, int.MaxValue), |
@@ -323,7 +375,7 @@ namespace DTXMania | ||
323 | 375 | ); |
324 | 376 | |
325 | 377 | //取得した描画サイズを基に、描画先のbitmapを作成する |
326 | - Bitmap bmp = new Bitmap(stringSize.Width + nEdgePt * 2, stringSize.Height + nEdgePt * 2); | |
378 | + Bitmap bmp = new Bitmap((int)(stringSize.Width + nEdgePt * 2), (int)(stringSize.Height + nEdgePt * 2)); | |
327 | 379 | bmp.MakeTransparent(); |
328 | 380 | |
329 | 381 | using (Graphics g = Graphics.FromImage(bmp)) |
@@ -339,7 +391,7 @@ namespace DTXMania | ||
339 | 391 | sf.FormatFlags = StringFormatFlags.NoWrap; |
340 | 392 | |
341 | 393 | // レイアウト枠 |
342 | - Rectangle r = new Rectangle(0, 0, stringSize.Width + nEdgePt * 2, stringSize.Height + nEdgePt * 2); | |
394 | + Rectangle r = new Rectangle(0, 0, (int)(stringSize.Width + nEdgePt * 2), (int)(stringSize.Height + nEdgePt * 2)); | |
343 | 395 | |
344 | 396 | // 縁取り有りの描画 |
345 | 397 | if (bEdge) |
@@ -382,7 +434,7 @@ namespace DTXMania | ||
382 | 434 | g.DrawRectangle( new Pen( Color.Green, 1 ), new Rectangle( 0, 0, bmp.Width - 1, bmp.Height - 1 ) ); |
383 | 435 | #endif |
384 | 436 | _rectStrings = new Rectangle(0, 0, stringSize.Width, stringSize.Height); |
385 | - _ptOrigin = new Point(nEdgePt * 2, nEdgePt * 2); | |
437 | + _ptOrigin = new Point((int)(nEdgePt * 2), (int)(nEdgePt * 2)); | |
386 | 438 | } |
387 | 439 | } |
388 | 440 |
@@ -450,10 +502,11 @@ namespace DTXMania | ||
450 | 502 | |
451 | 503 | private System.Drawing.Text.PrivateFontCollection _pfc; |
452 | 504 | private FontFamily _fontfamily; |
453 | - private int _pt; | |
505 | + private float _pt; | |
454 | 506 | private Rectangle _rectStrings; |
455 | 507 | private Point _ptOrigin; |
456 | 508 | private string _baseFontname = null; |
509 | + private bool bIsSystemFont; | |
457 | 510 | //----------------- |
458 | 511 | #endregion |
459 | 512 | } |