Why doesn't FontFactory.GetFont(“Known Font Name”, floatSize) work?

烂漫一生 提交于 2019-12-17 14:54:47

问题


If I say:

var georgia = FontFactory.GetFont("Georgia Regular", 10f);

it doesn't work. When I check the state of the variable georgia, it has its Family property set to the value UNDEFINED and its FamilyName property set to Unknown.

It only works if I actually load and register the font file and then get it like so:

FontFactory.Register("C:\\Windows\\Fonts\\georgia.ttf", "Georgia");
var georgia = FontFactory.GetFont("Georgia", 20f);

Why is that?


回答1:


iText is written in Java, which means it's platform-independent. It ships with 14 AFM files containing the metrics of the 14 Standard Type 1 fonts (4 flavors of Helvetica, 4 flavors of Times Roman, 4 flavors of Courier, Symbol and ZapfDingbats).

As soon as you need other fonts, you need to register the font files by passing the path to the font directory or the path to an actual font. The font directory on Linux is different from the font directory on Windows (there is no "C:/Windows/fonts" on Linux). There's also a method registerDirectories() that looks at the operating system you're currently using and that registers all the 'usual suspects' (iText guesses the font path based on the OS). This method is expensive: it registers all fonts it finds and this costs time and memory.

Once fonts are registered, you can ask the FontFactory for the registered names. This is shown in the FontFactoryExample. You'll notice the difference between the getRegisteredFonts() method and the getRegisteredFamilies() method.

Additional note: the original question is about iTextSharp, written in C#. iTextSharp is ported from Java and tries to stay as close as possible to the original version written in Java. Nevertheless, the same rationale applies: starting up an application would be much slower if iTextSharp would have to scan the fonts directory. In most applications, you only need a handful of fonts; registering all fonts available in the Windows fonts directory would be overkill.



来源:https://stackoverflow.com/questions/24007492/why-doesnt-fontfactory-getfontknown-font-name-floatsize-work

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!