System.Drawing.FontFamily.GenericSansSerif - 什么会影响它选择的底层字体?

发布于 2024-12-19 18:32:30 字数 207 浏览 3 评论 0原文

我正在审查使用 system.drawing 命名空间中的 FontFamily.GenericSansSerif 的图像生成代码。如果没有发现更理想的(特定)字体,我通常会在 xhtml/css 字体选择语句的末尾看到类似的代码作为后备/最后的手段。在.net框架中,哪些环境参数会影响实际选择的字体?是否有某种图表说明了当您指定 GenericSansSerif 时 .net 框架如何选择字体?

I am reviewing image generation code that uses FontFamily.GenericSansSerif from the system.drawing namespace. I typically see code like that at the tail end of xhtml/css font selection statements as a fall back / last resort if a more desireable (specific) font is not discovered. In the .net framework, what are the environmental parameters that will affect which font this actually selects? Is there some sort of chart out there that states how the .net framework selects the font when you specify GenericSansSerif?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清风不识月 2024-12-26 18:32:30

GenericSansSerif 而言,它将尝试按名称“Microsoft San Serif”、“Arial”、“Tahoma”的顺序从以下字体返回系列。如果没有安装这些字体,它似乎会选择第一个安装的字体系列,按名称排序。

GenericSerif 而言,它尝试从名为“Times New Roman”的字体中返回系列。如果未安装,则使用与 GenericSanSerif 相同的规则。即,如果删除“Times New Roman”字体,则与调用 GenericSanSerif 效果相同。

In terms of GenericSansSerif, it will try to return the family from the following fonts by name "Microsoft San Serif", "Arial", "Tahoma" in that order. If none of those fonts are installed, it appears it picks the family of the first installed font, ordered by name.

In terms of GenericSerif, it tries to return the family from the font named "Times New Roman". If that isn't installed the same rules as GenericSanSerif are used. i.e. if you remove the "Times New Roman" font, it's the same as calling GenericSanSerif.

卸妝后依然美 2024-12-26 18:32:30

经过一番反思(反思?),我想我可以解释这是如何工作的。

FontFamily.GenericSansSerifFontFamily.GenericSerif 都使用内部构造函数,通过其 IntPtr 值查找系统上的默认字体。在这两种情况下,它都会传递 IntPtr.Zero ,这实际上让 GDI+ 进行选择(我决定不去那个特定的兔子洞)。

基本上,FontFamily 类是密封的并使用指针,因此我不会费心尝试覆盖这些属性。相反,您可以编写自己的方法来模仿您在 CSS 中看到的后备行为:

public FontFamily DefaultFont(params string[] fonts)
{
    // Try to return the first matching font
    foreach (var font in fonts)
    {
        try { return new FontFamily(font); }
        catch (ArgumentException) { }
    }

    // Resort to system default
    return new FontFamily(new GenericFontFamilies());
}

After a bit of reflecting (reflection?), I think I can explain how this works.

Both FontFamily.GenericSansSerif and FontFamily.GenericSerif use an internal constructor that looks up the default font on the system by it's IntPtr value. In both cases, it passes IntPtr.Zero which effectively lets GDI+ do the selection (I decided not to go down that particular rabbit hole).

Basically, the FontFamily class is sealed and using pointers, so I wouldn't bother with trying to override those properties. Instead, you could write your own method that mimics the fallback behavior you see in CSS:

public FontFamily DefaultFont(params string[] fonts)
{
    // Try to return the first matching font
    foreach (var font in fonts)
    {
        try { return new FontFamily(font); }
        catch (ArgumentException) { }
    }

    // Resort to system default
    return new FontFamily(new GenericFontFamilies());
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文