System.Drawing.FontFamily.GenericSansSerif - 什么会影响它选择的底层字体?
我正在审查使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
就
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 asGenericSanSerif
are used. i.e. if you remove the "Times New Roman" font, it's the same as callingGenericSanSerif
.经过一番反思(反思?),我想我可以解释这是如何工作的。
FontFamily.GenericSansSerif
和FontFamily.GenericSerif
都使用内部构造函数,通过其IntPtr
值查找系统上的默认字体。在这两种情况下,它都会传递IntPtr.Zero
,这实际上让 GDI+ 进行选择(我决定不去那个特定的兔子洞)。基本上,FontFamily 类是密封的并使用指针,因此我不会费心尝试覆盖这些属性。相反,您可以编写自己的方法来模仿您在 CSS 中看到的后备行为:
After a bit of reflecting (reflection?), I think I can explain how this works.
Both
FontFamily.GenericSansSerif
andFontFamily.GenericSerif
use an internal constructor that looks up the default font on the system by it'sIntPtr
value. In both cases, it passesIntPtr.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: