Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8310565

[macos]When setting the font family for text, the Regular font is not being used

XMLWordPrintable

    • aarch64
    • os_x

      ADDITIONAL SYSTEM INFORMATION :
      Java version:
      openjdk version "17.0.7" 2023-04-18 LTS
      Os version:
      macOS 13.4

      A DESCRIPTION OF THE PROBLEM :
      In the case of TTC fonts, which can include multiple font variants of different weights, when setting the font family in JavaFX, the selected font may not be the desired Regular font. For example, the "PingFang SC" font family includes five fonts:

      "PingFang SC Light"
      "PingFang SC Medium"
      "PingFang SC Ultralight"
      "PingFang SC Semibold"
       "PingFang SC Thin"
       "PingFang SC Regular"

      Therefore, when using the font family "PingFang SC" in JavaFX, it may select "PingFang SC Light" instead of "PingFang SC Regular" due to the font selection logic described above.

      When setting the font family to "PingFang SC" in JavaFX, the font selection logic, specifically in the com.sun.javafx.font.PrismFontFactory#getFontResource code, iterates through the fonts in the family. It returns the first font that is not bold or italic. If a font like "PingFang SC Light" appears before "PingFang SC Regular" in the font family, it will be chosen instead.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Running the provided code on macOS will display different ways to set the font, indicating the use of Light font instead of Regular font.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The first line set the font family to "PingFang SC",The display should be the same as the second line(using the "PingFang SC Regular" font).
      ACTUAL -
      The first line displays same as the third line(using the "PingFang SC Light" font).

      ---------- BEGIN SOURCE ----------
      import javafx.application.Application;
      import javafx.scene.Scene;
      import javafx.scene.control.Label;
      import javafx.scene.image.Image;
      import javafx.scene.layout.VBox;
      import javafx.scene.text.Font;
      import javafx.stage.Stage;

      public class TestMain extends Application {
          @Override
          public void start(Stage stage) {
              var vbox = new VBox();

              var label1 = new Label("测试字体 - Family PingFang SC"); // use font family "PingFang SC"
              label1.setStyle("-fx-font-family: \"PingFang SC\"");
              double fontSize = label1.getFont().getSize();
              var label2 = new Label("测试字体 - Font PingFang SC Regular"); // use font "PingFang SC Regular"
              label2.setFont(new Font("PingFang SC Regular", fontSize));
              var label3 = new Label("测试字体 - Font PingFang SC Light"); // use font "PingFang SC Light"
              label3.setFont(new Font("PingFang SC Light", fontSize));

              vbox.getChildren().addAll(label1, label2, label3);

              Scene scene = new Scene(vbox);

              stage.setTitle("Test Font");
              stage.setWidth(800);
              stage.setHeight(600);
              stage.setScene(scene);

              stage.show();
          }

          public static void main(String[] args) {
              launch(args);
          }
      }

      ---------- END SOURCE ----------

      FREQUENCY : always


            prr Philip Race
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated: