c# - 从文件系统中的字体加载家族(而不是字体)名称

标签 c# fonts

我正在从文件系统加载字体。

通常我的代码工作得很好,但是当我通过 PrivateFontCollection 加载字体时,加载的 FontFamily 的名称是字体中字体的名称(我可以在 Windows 的字体预览器中的字体名称下看到)和不是 FontFamily 名称...

因为我有一些字体具有相同的字体名称,但字体名称不同,我希望能够区分它们。有人知道如何获得真正的 FontFamily 名称吗?

    public Dictionary<string, FontFamily> LoadFontsFromDirectory(string path)
    {
        Dictionary<string, FontFamily> foundFonts = new Dictionary<string, FontFamily>();

        if (!Directory.Exists(path)) throw new Exception("directory doesnt exist");

        foreach(FileInfo fi in new DirectoryInfo(path).GetFiles("*.ttf"))
        {
            PrivateFontCollection fileFonts = new PrivateFontCollection();
            fileFonts.AddFontFile(fi.FullName);
            if (!foundFonts.ContainsKey(fileFonts.Families[0].Name))
            {
                //add the font only if this fontfamily doesnt exist yet
                FontFamily family = new FontFamily(String.Format("file:///{0}#{1}", fi.FullName, fileFonts.Families[0].Name));
                foundFonts.Add(family.Name, family);
            }
        }

        return foundFonts;
    }

最佳答案

如果您想获取 FontFamily 而不是 fileFonts.Families[0].Name 中的值,您可以在 family.FamilyNames 中按文化找到它:

FontFamily family = new FontFamily(String.Format("file:///{0}#{1}", fi.FullName, fileFonts.Families[0].Name));
Console.WriteLine("\tFamilySource: {0}", family.Source);
foreach (var x in family.FamilyNames)
{
    Console.WriteLine("\tFamilyName: {0}", x);  // <------ HERE
}
foreach (var y in family.FamilyTypefaces)
{
    foreach (var z in y.AdjustedFaceNames)
    {                            
        Console.WriteLine("\tTypeface: {0}",z);
    }
}

示例输出:

Arial
    FamilySource: file:///C:\Users\tim\Desktop\arial.ttf#Arial
    FamilyName: [en-us, Arial]
    Typeface: [en-us, Regular]
    Typeface: [en-us, Bold]
    Typeface: [en-us, Bold Oblique]
    Typeface: [en-us, Oblique]
Arial
    FamilySource: file:///C:\Users\tim\Desktop\arialbd.ttf#Arial
    FamilyName: [en-us, Arial]
    Typeface: [en-us, Bold]
    Typeface: [en-us, Bold Oblique]
Arial
    FamilySource: file:///C:\Users\tim\Desktop\arialbi.ttf#Arial
    FamilyName: [en-us, Arial]
    Typeface: [en-us, Bold Italic]
Arial
    FamilySource: file:///C:\Users\tim\Desktop\ariali.ttf#Arial
    FamilyName: [en-us, Arial]
    Typeface: [en-us, Italic]
    Typeface: [en-us, Bold Italic]
Arial Narrow
    FamilySource: file:///C:\Users\tim\Desktop\ARIALN.TTF#Arial Narrow
    FamilyName: [en-us, Arial]
    Typeface: [en-us, Condensed]
    Typeface: [en-us, Condensed Bold]
    Typeface: [en-us, Condensed Bold Oblique]
    Typeface: [en-us, Condensed Oblique]
Arial Narrow
    FamilySource: file:///C:\Users\tim\Desktop\ARIALNB.TTF#Arial Narrow
    FamilyName: [en-us, Arial]
    Typeface: [en-us, Condensed Bold]
    Typeface: [en-us, Condensed Bold Oblique]
Arial Narrow
    FamilySource: file:///C:\Users\tim\Desktop\ARIALNBI.TTF#Arial Narrow
    FamilyName: [en-us, Arial]
    Typeface: [en-us, Condensed Bold Italic]
Arial Narrow
    FamilySource: file:///C:\Users\tim\Desktop\ARIALNI.TTF#Arial Narrow
    FamilyName: [en-us, Arial]
    Typeface: [en-us, Condensed Italic]
    Typeface: [en-us, Condensed Bold Italic]
Arial Black
    FamilySource: file:///C:\Users\tim\Desktop\ariblk.ttf#Arial Black
    FamilyName: [en-us, Arial]
    Typeface: [en-us, Black]
    Typeface: [en-us, Black Oblique]

就是说,您的笔记“我有一些字体具有相同的字体名称但另一个字体名称,我希望能够区分它们”表明 FontFamily 不是您真正要找的。

关于c# - 从文件系统中的字体加载家族(而不是字体)名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6124612/

相关文章:

c# - 在 if 语句中求值?

C#密码生成器,生成多个密码

android - 为 ListView 设置字体

ios - 在 UserControl 中使用自定义字体使我的字体变为粗体

c# - 在不打开文件的情况下删除文件的最后 4 个字节?

c# - ASP Core HttpClientFactory 模式使用客户端证书

c# - 如何将 List<CustomObject> 绑定(bind)到 WPF DataGrid?

html - 浏览器未显示正确的字体

android - Xamarin.Android-安卓 :fontFamily doesn't work

css - CSS 可以用于替代字体吗?