负载家族(而不是字体)在文件系统中的字体名称 [英] Load family (and not typeface) name from font in file system

查看:296
本文介绍了负载家族(而不是字体)在文件系统中的字体名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我加载字体从文件系统。



通常我的代码工作得非常好,但我通过PrivateFontCollection加载字体加载的FontFamily的名称是在字体(我可以下字样名称请参阅Windows的字体预览)字样的名称,而不是的FontFamily名称...



由于我有一些具有相同字样的名称,但另一种字体的名称我想能够区分它们的字体。任何人有一个想法如何得到真正的FontFamily名称



 公众解释<字符串的FontFamily> LoadFontsFromDirectory(字符串路径)
{
&字典LT;字符串的FontFamily> foundFonts =新词典<字符串的FontFamily>();

如果抛出新的异常(目录犯规存在)(Directory.Exists(路径)!);

的foreach(FileInfo的网络新DirectoryInfo的(路径).GetFiles(* TTF))
{
PrivateFontCollection fileFonts =新PrivateFontCollection();
fileFonts.AddFontFile(fi.FullName);
如果(!foundFonts.ContainsKey(fileFonts.Families [0] .Name点))
{
//添加仅当此fontFamily中犯规还存在
的FontFamily家庭=新的字体的FontFamily(的String.Format(文件:/// {0}#{1},fi.FullName,fileFonts.Families [0] .Name点));
foundFonts.Add(family.Name,家庭);
}
}

返回foundFonts;
}


解决方案

如果你想获得的的FontFamily而不是在fileFonts.Families [0] .Name点的价值,你会受到文化family.FamilyNames找到它:

 的FontFamily家庭=新的FontFamily(的String.Format(文件:/// {0}#{1},fi.FullName,fileFonts.Families [0] .Name点)); 
Console.WriteLine(\tFamilySource:{0},family.Source);
的foreach(在family.FamilyNames变种X)
{
Console.WriteLine(\tFamilyName:{0},X); //< ------这是
}
的foreach(在family.FamilyTypefaces变种Y)
{
的foreach(在y.AdjustedFaceNames变种Z)
{
Console.WriteLine(\tTypeface:{0},Z);
}
}



示例输出:

 宋体
FamilySource:文件:/// C:\Users\tim\Desktop\arial.ttf#宋体
FamilyName:EN-US,宋体]
字体:[EN-US,常规]
字体:[EN-US,粗体]
字体:[EN-US,粗体斜]
字体:[EN-US,斜]
宋体
FamilySource:文件:/// C:\Users\tim\Desktop\arialbd.ttf#宋体
FamilyName:EN-US,宋体]
字体:[EN-US,粗体]
字体:[EN-US,粗体斜]
宋体
FamilySource:文件:/ //C:\Users\tim\Desktop\arialbi.ttf#Arial
FamilyName:EN-US,宋体]
字体:[EN-US,粗斜体]
宋体
FamilySource:文件:/// C:\Users\tim\Desktop\ariali.ttf#宋体
FamilyName:EN-US,宋体]
字体: [EN-US,斜体]
字体:[EN-US,粗斜体]
窄Arial
FamilySource:文件:/// C:\Users\tim\Desktop\\ \\ARIALN.TTF#窄Arial
FamilyName:EN-US,宋体]
字体:[EN-US,凝聚]
字体:[EN-US,简明粗体]
字体:[EN-US,简明粗体斜]
字体:[EN-US,凝聚斜]
窄Arial
FamilySource:文件:/// C:\Users\ tim\Desktop\ARIALNB.TTF#窄Arial
FamilyName:EN-US,宋体]
字体:[EN-US,简明粗体]
字体:[EN-US,简明粗体斜]
窄Arial
FamilySource:文件:/// C:\Users\tim\Desktop\ARIALNBI.TTF#窄Arial
FamilyName:EN-US ,宋体]
字体:[EN-US,简明粗体斜体]
窄Arial
FamilySource:文件:/// C:\Users\tim\Desktop\ARIALNI。 TTF#窄Arial
FamilyName:EN-US,宋体]
字体:[EN-US,简明斜体]
字体:[EN-US,简明粗体斜体]
宋体黑色
FamilySource:文件:/// C:\Users\tim\Desktop\ariblk.ttf#宋体黑
FamilyName:EN-US,宋体]
字样:EN-US,黑色]
字体:[EN-US,黑色斜]

这就是说,你的注意,我有一些的字体有相同字样的名称,但另一种字体的名字,我想是能够区分它们暗示的FontFamily是不是你实际上是在寻找什么


I'm loading fonts from the filesystem .

generally my code works very well, but as i load the fonts through a PrivateFontCollection the name of the loaded FontFamily is the name of the TypeFace in the font (what i can see under Typeface name in the Font previewer of windows) and not the FontFamily name...

As i have some fonts which have the same Typeface name but another font name i would like to be able to distinguish between them. Anyone has a idea how to get the real FontFamily name?

    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;
    }

解决方案

If you want to get the FontFamily as opposed to the value in fileFonts.Families[0].Name, you'll find it in family.FamilyNames by culture:

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);
    }
}

Sample output:

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]

That said, your note that "i have some fonts which have the same Typeface name but another font name and i would like to be able to distinguish between them" suggests that FontFamily is not what you are actually looking for.

这篇关于负载家族(而不是字体)在文件系统中的字体名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆