Exemple #1
0
        public static SixLabors.Fonts.FontStyle GetStyle(int paran)
        {
            SixLabors.Fonts.FontStyle result = SixLabors.Fonts.FontStyle.Regular;
            switch (paran)
            {
            case (int)SixLabors.Fonts.FontStyle.Regular:
                break;

            case (int)SixLabors.Fonts.FontStyle.Bold:
                result = SixLabors.Fonts.FontStyle.Bold;
                break;

            case (int)SixLabors.Fonts.FontStyle.Italic:
                result = SixLabors.Fonts.FontStyle.Italic;
                break;

            case (int)SixLabors.Fonts.FontStyle.BoldItalic:
                result = SixLabors.Fonts.FontStyle.BoldItalic;
                break;
            }

            return(result);
        }
 public CPrivateFont(string fontpath, int pt, SixLabors.Fonts.FontStyle style)
 {
     Initialize(fontpath, pt, style);
 }
        /// <param name="fontpath">フォント名orフォントパス</param>
        /// <param name="pt">大きさ</param>
        /// <param name="stylel">フォントスタイル</param>
        protected void Initialize(string fontpath, int pt, SixLabors.Fonts.FontStyle stylel)
        {
            this._pfc         = null;
            this._fontfamily  = null;
            this._font        = null;
            this._pt          = pt;
            this._rectStrings = new Rectangle(0, 0, 0, 0);
            this._ptOrigin    = new Point(0, 0);
            this.bDispose完了済み = false;

            FontStyle style;

            switch (stylel)
            {
            case SixLabors.Fonts.FontStyle.Bold:
                style = FontStyle.Bold;
                break;

            case SixLabors.Fonts.FontStyle.BoldItalic:
                style = FontStyle.Bold | FontStyle.Italic;
                break;

            case SixLabors.Fonts.FontStyle.Italic:
                style = FontStyle.Italic;
                break;

            default:
                style = FontStyle.Regular;
                break;
            }

            try
            {
                this._fontfamily = new FontFamily(fontpath);
            }
            catch
            {
                Trace.TraceWarning($"{fontpath}はフォント名ではないようです。");
                this._fontfamily = null;
            }

            if (this._fontfamily == null)
            {
                try
                {
                    this._pfc = new System.Drawing.Text.PrivateFontCollection();                        //PrivateFontCollectionオブジェクトを作成する
                    this._pfc.AddFontFile(fontpath);                                                    //PrivateFontCollectionにフォントを追加する
                    _fontfamily = _pfc.Families[0];
                }
                catch (System.IO.FileNotFoundException)
                {
                    Trace.TraceWarning($"プライベートフォントの追加に失敗しました({fontpath})。代わりに内蔵フォントの使用を試みます。");
                    //throw new FileNotFoundException( "プライベートフォントの追加に失敗しました。({0})", Path.GetFileName( fontpath ) );
                    //return;
                    this._fontfamily = null;
                }
            }

            // 指定されたフォントスタイルが適用できない場合は、フォント内で定義されているスタイルから候補を選んで使用する
            // 何もスタイルが使えないようなフォントなら、例外を出す。
            if (_fontfamily != null)
            {
                if (!_fontfamily.IsStyleAvailable(style))
                {
                    FontStyle[] FS = { FontStyle.Regular, FontStyle.Bold, FontStyle.Italic, FontStyle.Underline, FontStyle.Strikeout };
                    style = FontStyle.Regular | FontStyle.Bold | FontStyle.Italic | FontStyle.Underline | FontStyle.Strikeout;                          // null非許容型なので、代わりに全盛をNGワードに設定
                    foreach (FontStyle ff in FS)
                    {
                        if (this._fontfamily.IsStyleAvailable(ff))
                        {
                            style = ff;
                            Trace.TraceWarning("フォント{0}へのスタイル指定を、{1}に変更しました。", Path.GetFileName(fontpath), style.ToString());
                            break;
                        }
                    }
                    if (style == (FontStyle.Regular | FontStyle.Bold | FontStyle.Italic | FontStyle.Underline | FontStyle.Strikeout))
                    {
                        Trace.TraceWarning("フォント{0}は適切なスタイル{1}を選択できませんでした。", Path.GetFileName(fontpath), style.ToString());
                    }
                }
                //this._font = new Font(this._fontfamily, pt, style);			//PrivateFontCollectionの先頭のフォントのFontオブジェクトを作成する
                float emSize = pt * 96.0f / 72.0f;
                this._font = new Font(this._fontfamily, emSize, style, GraphicsUnit.Pixel);                     //PrivateFontCollectionの先頭のフォントのFontオブジェクトを作成する
                //HighDPI対応のため、pxサイズで指定
            }
            else
            // フォントファイルが見つからなかった場合 (デフォルトフォントを代わりに指定する)
            {
                float emSize = pt * 96.0f / 72.0f;
                try
                {
                    this._pfc = new System.Drawing.Text.PrivateFontCollection();
                    using (MemoryStream ms = new MemoryStream())
                    {
                        Assembly.GetExecutingAssembly().GetManifestResourceStream(@"FDK.mplus-1p-medium.ttf").CopyTo(ms);
                        byte[] bytes = ms.ToArray();
                        unsafe {
                            fixed(byte *bytesp = bytes)
                            this._pfc.AddMemoryFont((IntPtr)bytesp, bytes.Length);
                        }
                    }

                    this._fontfamily = _pfc.Families[0];
                    this._font       = new Font(this._fontfamily, emSize, style, GraphicsUnit.Pixel);

                    Trace.TraceInformation($"{this._fontfamily.Name}を代わりに指定しました。");

                    return;
                }
                catch (Exception e)
                {
                    Trace.TraceError(e.ToString());
                    this._fontfamily = null;
                    this._font       = null;
                }

                throw new FileNotFoundException($"プライベートフォントの追加に失敗し、内蔵フォントでの代替処理にも失敗しました。({Path.GetFileName(fontpath)})");
            }
        }
Exemple #4
0
 protected new void Initialize(string fontpath, int pt, SixLabors.Fonts.FontStyle style)
 {
     this.bDispose完了済み_CPrivateFastFont = false;
     this.listFontCache = new List <FontCache>();
     base.Initialize(fontpath, pt, style);
 }