The web page uses a custom font which is not installed on my PC. In such case, WebView seems to use the default font of the operation system.
But I have the font file "xx.ttf". How can I embed the font into my application and tell WebView to use it to identify the font on the page?
Load the font:
before you use it in the WebView:
Here is a complete example. The example relies on a TRON.TTF font which you can download from dafont. Once you have downloaded the TRON.TTF font, place it in the same directory as CustomFontWebView.java and ensure that your build system copies the TRON.TTF file to the class output directory.
On use of the Microsoft YaHei font specifically
Microsoft YaHei works fine for me (Windows 7). My Win7 install comes with the font (and it can't be removed), so it doesn't need to be explicitly loaded - just reference the correct font family from your html and it will just work for such a Win7 install. I have a standard US edition of Win7 Pro, not a Chinese version. I did copy the msyh.ttf file from my windows/fonts directory into my project and load it via JavaFX just to make sure the loading via JavaFX works and that also worked fine. The font-family I used to set the html style css font-family specifier was "Microsoft YaHei" (instead of TRON used in the above answer example). The text I displayed to test it was 微软雅黑 and I compared the rendered text by the JavaFX app against the same text rendered in WordPad with Microsoft YaHei selected and the glyphs were identical.
Note that with JavaFX 3.0 is seems you will be able to use the css @font-face mechanism, which means you could declare the font reference in the web page's css rather than in Java code as I did in the above example. Here is a link to the relevant jira issue to track progress on implementation of this feature.