Javafx如何在webview中显示自定义字体? [英] Javafx How to display custom font in webview?

查看:444
本文介绍了Javafx如何在webview中显示自定义字体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

网页使用的是我的电脑上未安装的自定义字体。在这种情况下,WebView似乎使用操作系统的默认字体。



但我有字体文件xx.ttf。如何将字体嵌入到我的应用程序中并告诉WebView使用它来识别页面上的字体?

解决方案

加载字体:

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

在WebView中使用它之前:

 风格= '字体家族: TRON'; 

这是一个完整的例子。
该示例依赖于TRON.TTF字体,您可以从机制,这意味着您可以在网页的css中声明字体引用,而不是像上面的示例中那样在Java代码中声明。以下是相关jira问题的链接,以跟踪此实施的进展情况功能。


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:

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();
  }
}

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.

这篇关于Javafx如何在webview中显示自定义字体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆