Подключил к WebView JavaFX 17 шрифт, которого нет в системе. WebView некорректно его отображает. Причем, шрифты, установленные в систему он отображает нормально. После Font.loadFont, шрифт появляется в списке шрифтов, но отображается неверно. Подскажите, что делаю не так?
Код sample.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.ListView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.web.WebView?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="500.0" prefWidth="750.0" xmlns="http://javafx.com/javafx/17" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<children>
<Label fx:id="myLabel" layoutX="10.0" layoutY="10.0" prefHeight="50.0" prefWidth="730.0" text="Select Font">
<font>
<Font size="18.0" />
</font></Label>
<AnchorPane fx:id="myWebPane" layoutX="220.0" layoutY="70.0" prefHeight="420.0" prefWidth="520.0">
<children>
<WebView fx:id="myWebView" layoutX="5.0" layoutY="5.0" prefHeight="410.0" prefWidth="510.0" />
</children>
</AnchorPane>
<ListView fx:id="myListView" layoutX="10.0" layoutY="70.0" onMouseClicked="#handleMouseClickEvent" prefHeight="420.0" prefWidth="200.0" />
</children>
</AnchorPane>
код Controller.java:
package sample;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.geometry.Pos;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.text.Font;
import javafx.scene.web.WebView;
import java.net.URL;
import java.util.ResourceBundle;
public class Controller implements Initializable {
@FXML
private ListView<String> myListView;
@FXML
private Label myLabel;
@FXML
private WebView myWebView;
@FXML
private AnchorPane myWebPane;
private String fontText = "";
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
myLabel.setAlignment(Pos.CENTER);
myLabel.setStyle("-fx-border-color: black;");
myListView.setStyle("-fx-border-color: black;");
myWebPane.setStyle("-fx-border-color: black;");
Font myFont = Font.loadFont(getClass().getResource("TRON.TTF").toExternalForm(), 10);
String myFamily = myFont.getFamily();
System.out.println("Font family = " + myFamily);
ObservableList<String> fontsList = FXCollections.observableArrayList(Font.getFontNames());
myListView.setItems(fontsList);
}
@FXML
public void handleMouseClickEvent() {
changeFont();
}
private void changeFont() {
Font useFont = Font.font(myListView.getSelectionModel().getSelectedItem(), 24);
String fontFamily = useFont.getFamily();
myLabel.setText("Now use font " + useFont.getName() +
" and his font-family is " + fontFamily);
StringBuilder sb = new StringBuilder(fontText);
sb.setLength(0);
sb.append("<p style='font-family:")
.append(myListView.getSelectionModel().getSelectedItem())
.append(";font-size:24'>The quick brown fox jump 1234567890</p>");
System.out.println("Now Use Font " + myListView.getSelectionModel().getSelectedItem());
fontText = sb.toString();
myWebView.getEngine().loadContent(fontText);
}
}
код Main.java:
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("Fonts in WebView");
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
Шрифт лежит рядом с файлами:
Вот, к примеру, отображение установленного в систему шрифта Georgia:
OS Debian, Intellij Idea 2020.3, Java 17, OpenJavaFX javafx-sdk-17.0.2