检查 FontFamily 是否为 TrueType
Check if FontFamily is TrueType
是否可以使用 C#、C++/CLI 或 P/Invoking WinAPI 来确定某个字体系列是否为 TrueType 字体?
最后我想要这样的结果
bool result1 = CheckIfIsTrueType(new FontFamily("Consolas")); //returns true
bool result2 = CheckIfIsTrueType(new FontFamily("Arial")); // returns true
bool result3 = CheckIfIsTrueType(new FontFamily("PseudoSaudi")); // returns false
bool result4 = CheckIfIsTrueType(new FontFamily("Ubuntu")); // returns true
bool result5 = CheckIfIsTrueType(new FontFamily("Purista")); // returns false
当然,结果取决于目标操作系统及其字体...
它有处理异常的开销,但如果提供的字体不是 TrueType,FontFamily
constructor 会抛出 ArgumentException
:
public bool CheckIfIsTrueType(string font)
{
try
{
var ff = new FontFamily(font)
}
catch(ArgumentException ae)
{
// this is also thrown if a font is not found
if(ae.Message.Contains("TrueType"))
return false;
throw;
}
return true;
}
深入研究 FontFamily 构造函数,它调用外部 GDIPlus 函数 GdipCreateFontFamilyFromName
:
[DllImport("Gdiplus", SetLastError=true, ExactSpelling=true, CharSet=System.Runtime.InteropServices.CharSet.Unicode)] // 3 = Unicode
internal static extern int GdipCreateFontFamilyFromName(string name, HandleRef fontCollection, out IntPtr FontFamily);
如果字体不是 True Type 字体,return 代码 16
。所以你可以绕过异常的开销:
public bool CheckIfIsTrueType(string name)
{
IntPtr fontfamily = IntPtr.Zero;
IntPtr nativeFontCollection = IntPtr.Zero ;
int status = GdipCreateFontFamilyFromName(name, new HandleRef(null, nativeFontCollection), out fontfamily);
if(status != 0)
if(status == 16) // not true type font)
return false;
else
throw new ArgumentException("GDI Error occurred creating Font");
return true;
}
显然,您可能希望为代码使用常量枚举,可以找到 here,并抛出更好的异常
如果目标操作系统是 Windows 10,则执行 D Stanley 的 C# 示例代码时不会出现异常。仅在 windows 7 上抛出异常。GDI+ 在 windows 8 和 Windows 10 上支持具有 Adobe CFF 轮廓的 OTF 字体。请参阅本文底部的 table link -> https://docs.microsoft.com/en-us/windows/desktop/gdiplus/-gdiplus-creating-a-private-font-collection-use
是否可以使用 C#、C++/CLI 或 P/Invoking WinAPI 来确定某个字体系列是否为 TrueType 字体?
最后我想要这样的结果
bool result1 = CheckIfIsTrueType(new FontFamily("Consolas")); //returns true
bool result2 = CheckIfIsTrueType(new FontFamily("Arial")); // returns true
bool result3 = CheckIfIsTrueType(new FontFamily("PseudoSaudi")); // returns false
bool result4 = CheckIfIsTrueType(new FontFamily("Ubuntu")); // returns true
bool result5 = CheckIfIsTrueType(new FontFamily("Purista")); // returns false
当然,结果取决于目标操作系统及其字体...
它有处理异常的开销,但如果提供的字体不是 TrueType,FontFamily
constructor 会抛出 ArgumentException
:
public bool CheckIfIsTrueType(string font)
{
try
{
var ff = new FontFamily(font)
}
catch(ArgumentException ae)
{
// this is also thrown if a font is not found
if(ae.Message.Contains("TrueType"))
return false;
throw;
}
return true;
}
深入研究 FontFamily 构造函数,它调用外部 GDIPlus 函数 GdipCreateFontFamilyFromName
:
[DllImport("Gdiplus", SetLastError=true, ExactSpelling=true, CharSet=System.Runtime.InteropServices.CharSet.Unicode)] // 3 = Unicode
internal static extern int GdipCreateFontFamilyFromName(string name, HandleRef fontCollection, out IntPtr FontFamily);
如果字体不是 True Type 字体,return 代码 16
。所以你可以绕过异常的开销:
public bool CheckIfIsTrueType(string name)
{
IntPtr fontfamily = IntPtr.Zero;
IntPtr nativeFontCollection = IntPtr.Zero ;
int status = GdipCreateFontFamilyFromName(name, new HandleRef(null, nativeFontCollection), out fontfamily);
if(status != 0)
if(status == 16) // not true type font)
return false;
else
throw new ArgumentException("GDI Error occurred creating Font");
return true;
}
显然,您可能希望为代码使用常量枚举,可以找到 here,并抛出更好的异常
如果目标操作系统是 Windows 10,则执行 D Stanley 的 C# 示例代码时不会出现异常。仅在 windows 7 上抛出异常。GDI+ 在 windows 8 和 Windows 10 上支持具有 Adobe CFF 轮廓的 OTF 字体。请参阅本文底部的 table link -> https://docs.microsoft.com/en-us/windows/desktop/gdiplus/-gdiplus-creating-a-private-font-collection-use