2

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?

Grant Zhu
  • 2,988
  • 2
  • 23
  • 28

1 Answers1

9

Load the font:

Font.loadFont(
  CustomFontWebView.class.getResource("TRON.TTF").toExternalForm(), 
  10
);

before you use it in the WebView:

style='font-family:"TRON"';

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.

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.text.Font;
import javafx.scene.web.WebView;
import javafx.stage.Stage;

// demonstrates the use of a custom font in a WebView.
public class CustomFontWebView extends Application {
  public static void main(String[] args) { launch(args); }
  @Override public void start(Stage stage) {
    stage.setTitle("TRON Synopsis");

    // load the tron font.
    Font.loadFont(
      CustomFontWebView.class.getResource("TRON.TTF").toExternalForm(), 
      10
    );

    // use the tron font in a WebView.
    WebView webView = new WebView();
    webView.getEngine().loadContent(
      "<body bgcolor='silver'>" +   
        "<div align='center'>" +   
          "<p style='font-family:'TRON'; font-size:20;'>" +                 "TRON" + 
          "</p>" +
          "<img src='http://ia.media-imdb.com/images/M/MV5BMTY5NjM2MjAwOV5BMl5BanBnXkFtZTYwMTgyMzA5.V1.SY317.jpg' width='259' height='457'/>" + 
          "<p style='font-family:'TRON'; font-size:10;'>" + 
            "A sci-fi flick set in an alternate reality." + 
          "</p>" +
        "</div>" +   
      "</body>"
    );

    // layout the scene.
    final Scene scene = new Scene(webView, 400, 575);
    stage.setScene(scene);
    stage.show();
  }
}

tron

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.

Marcel
  • 1,509
  • 1
  • 17
  • 39
jewelsea
  • 150,031
  • 14
  • 366
  • 406
  • Thanks for the answer. I have tried this before with my font msyh.ttf(Microsoft YaHei) but it didn't work on the PC which haven't this font installed. But your font works perfectly... What's wrong with Microsoft YaHei? – Grant Zhu Jul 06 '12 at 03:38
  • Hi John, thanks for your patience. It doesn't work on the system which hasn't Microsoft YaHei installed such as Windows XP. I'm testing on a virtual machine with Windows XP. Maybe there's a specific version of this font to work under XP.. I don't have much knowledge about this.. Anyway, Font.loadFont is the correct method so I will accept your answer. Thanks again.. – Grant Zhu Jul 06 '12 at 09:40
  • My guess is correct. There's another version of the font for XP... Just copy the font file from win7 to XP won't work.. – Grant Zhu Jul 06 '12 at 11:53