簡體   English   中英

從資源創建PrivateFontCollection的問題

[英]Issue Creating PrivateFontCollection from Resources

我已經運行了相同的代碼幾個月,它的工作就像一個魅力。 最近,我已經將此代碼添加到TFS的源代碼管理中,並且不再正常工作。 唯一的更改是修改了一些名稱空間,以滿足我們的產品准則。 目的是從作為資源嵌入文件中的字體創建pfc ,原因是可執行文件具有可移植性,除了安裝適當的.NET版本外,不依賴任何內容。

    public static Dictionary<string, object> FontDict = new Dictionary<string, object>();

    public static PrivateFontCollection s_FontCollection = new PrivateFontCollection();
    public static Font staticFont;

    public static FontFamily[] FontFamilies
    {
        get
        {
            if (s_FontCollection.Families.Length == 0)
                LoadFonts();

            return s_FontCollection.Families;
        }
    }

    public static Font GetFont(string family, float size)
    {
        foreach (FontFamily font in FontFamilies)
        {
            if (font.Name.ToLower().Equals(family.ToLower()))
            {
                Font ret = new Font(font, size);
                //return (Font)ret.Clone();
                return ret;
            }
        }

        return null;
    }

    public static void LoadFonts()
    {
        if (Assembly.GetEntryAssembly() == null || Assembly.GetEntryAssembly().GetManifestResourceNames() == null)
            return;

        foreach (string resource in Assembly.GetEntryAssembly().GetManifestResourceNames())
        {
            // Load TTF files from your Fonts resource folder.
            if (resource.Contains(".Fonts.") && resource.ToLower().EndsWith(".ttf"))
            {
                using (Stream stream = Assembly.GetEntryAssembly().GetManifestResourceStream(resource))
                {
                    try
                    {
                        // create an unsafe memory block for the font data
                        System.IntPtr data = Marshal.AllocCoTaskMem((int)stream.Length);

                        // create a buffer to read in to
                        byte[] fontdata = new byte[stream.Length];

                        // read the font data from the resource
                        stream.Read(fontdata, 0, (int)stream.Length);

                        // copy the bytes to the unsafe memory block
                        Marshal.Copy(fontdata, 0, data, (int)stream.Length);

                        // pass the font to the font collection
                        s_FontCollection.AddMemoryFont(data, (int)stream.Length);

                        // close the resource stream
                        stream.Close();

                        // free up the unsafe memory
                        Marshal.FreeCoTaskMem(data);
                    }
                    catch
                    {

                    }
                }
            }
        }

        FontDict.Clear();
        FontDict.Add("SYS",    new Font("Arial", 20));
        FontDict.Add("SYSs",   new Font("Arial", 14));
        FontDict.Add("MICR",   GetFont("MICR", 18));                    // Preferred MICR font
        FontDict.Add("SIG",    GetFont("PWSignaturetwo", 30));
        FontDict.Add("HAND1l", GetFont("Daniel", 22));
        FontDict.Add("HAND2l", GetFont("Jenna Sue", 32));
        FontDict.Add("HAND3l", GetFont("Honey Script", 30));
        FontDict.Add("HAND4l", GetFont("Confessions", 42));
        FontDict.Add("HAND5l", GetFont("Soljik-Dambaek", 26));
        FontDict.Add("HAND6l", GetFont("Billy's Hand Thin", 38));
        FontDict.Add("HAND7l", GetFont("Daisy Script", 34));
        FontDict.Add("HAND8l", GetFont("Fineliner SCript", 34));
        FontDict.Add("HAND9l", GetFont("Graphe", 20));
        FontDict.Add("HAND1s", GetFont("Daniel", 16));
        FontDict.Add("HAND2s", GetFont("Jenna Sue", 26));
        FontDict.Add("HAND3s", GetFont("Honey Script", 24));
        FontDict.Add("HAND4s", GetFont("Confessions", 30));
        FontDict.Add("HAND5s", GetFont("Soljik-Dambaek", 20));
        FontDict.Add("HAND6s", GetFont("Billy's Hand Thin", 26));
        FontDict.Add("HAND7s", GetFont("Daisy Script", 26));
        FontDict.Add("HAND8s", GetFont("Fineliner Script", 26));
        FontDict.Add("HAND9s", GetFont("Graphe", 14));
        FontDict.Add("HACKs",  GetFont("Hack", 10));                    //******************************************************//
        FontDict.Add("HACKm",  GetFont("Hack", 12));                    //    Preferred fixed-width font for non-check data     //
        FontDict.Add("HACKl",  GetFont("Hack", 16));                    //******************************************************//

突然開始發生的事情是所有字體都沒有加載到字典中。 據我在調試時所知道的,資源很好。 它將正確循環所有字體資源。 但是,當到達s_FontCollection.AddMemoryFont它將開始跳過一些內容。 我試圖弄清楚如何從方法中讀取Status返回值( 如此處所述 ),但似乎無法實現。 老實說,我沒有更改與此相關的其他代碼。

真正奇怪的是,這是執行時的問題。 我可以多次運行相同的版本,最后在可用字體方面得到不同的結果。

原來問題是ttf文件在添加到源代碼管理后損壞了。 對於上面的討論,通過FreeCoTaskMem()釋放內存工作正常(就像以前一樣),因為只需要臨時將字節獲取到內存中,以便可以利用AddFontFromMem()-這是最簡單的嵌入式方法數據存入pfc。 刪除錯誤的字體可以完全解決此問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM