【问题标题】:Load family (and not typeface) name from font in file system从文件系统中的字体加载系列(而不是字体)名称
【发布时间】:2011-05-25 12:42:14
【问题描述】:

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

通常我的代码工作得很好,但是当我通过 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;
    }

【问题讨论】:

  • 这个链接对我有用吗? MSDN
  • 抱歉,我的意思是那个链接对你有用吗?
  • 其实,略过。我应该花更多时间阅读这个问题,链接不是你要找的。我的道歉
  • 您的意思是要区分 Times New Roman 和 Times New Roman Bold(例如)?在这种情况下,两个家族都是 Times New Roman。

标签: c# fonts


【解决方案1】:

如果您想获取 FontFamily 而不是 fileFonts.Family[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 不是您真正想要的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-20
    • 2023-03-27
    • 2021-05-14
    • 2011-05-07
    • 2016-07-27
    • 2022-10-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多