1

Issue

Hi guys, I have setup the following JavaFX CSS to work on a Label control to display the sum-total of my calculator app.

.label-sumtotal {
  -fx-font-family: "Noto Sans CJK SC Bold", Arial, sans-serif;
  -fx-text-fill: color-crimson;
  -fx-font-size: 32pt;
  -fx-font-weight: bold;
}

This is how it looks correctly on Windows and Xubuntu 14.04

Sample on Windows and Xubuntu 14.04

And this is how it looks incorrectly on Xubuntu 16.04

Sample on Xubuntu 16.04


What I have tested so far on Xubuntu 16.04

  • I change to another font-family that is included by Xubuntu 16.04, yet the Label still doesn't change its font-family nor its font-weight.

  • But when I change the control from Label to Text without touching any of the CSS code, then somehow the font-family gets recognized and applied.


My Question

How can I make the Label control display the font-family on Xubuntu 16.04? I don't want to use Text control since it doesn't have certain formatting features as the Label.

Cheng Fei
  • 37
  • 8
  • [JavaFX CSS reference guide](https://docs.oracle.com/javase/8/javafx/api/javafx/scene/doc-files/cssref.html#introlimitations) states: "JavaFX CSS does not support comma-separated series of font family names in the -fx-font-family property. The optional line height parameter when specifying fonts is not supported.". So perhaps trying to do that has confused the system. – jewelsea Sep 04 '19 at 22:11
  • Also `color-crimson` is not a valid color, it should just be `crimson`. – jewelsea Sep 04 '19 at 22:36
  • As to color-crimson, I have declared it in CSS like this `color-crimson: #dc143c;`. Therefore I can reuse `color-crimson` in other parts of CSS. – Cheng Fei Sep 05 '19 at 20:16
  • As to "comma-separated series of font family names in the -fx-font-family property", I removed the other fonts and kept only a singled one. Yet still no improvements. I wonder how and where JavaFX gets its default font-families, maybe the default values have overwritten mine constantly. – Cheng Fei Sep 05 '19 at 20:22
  • Run this: `System.out.println(String.join("\n", Font.getFamilies()));` to find out what font families are available, read the JavaFX source if you wish to know definitively how it determines these (as far as I know it is not documented anywhere and implementation would be platform dependent). – jewelsea Sep 05 '19 at 20:28
  • In terms of "maybe the default values have overwritten mine constantly", I think it is unlikely, note that if you have a web font (as in my answer below), that the webfont definition will be defined to try to use local fonts if possible rather than load the font remotely (I guess JavaFX will honor that), but as long as the fonts are well defined both locally and remotely then I don't think it should cause an issue. See https://stackoverflow.com/questions/3837249/font-face-src-local-how-to-use-the-local-font-if-the-user-already-has-it for more info. – jewelsea Sep 05 '19 at 20:54

1 Answers1

1

I don't have an Ubuntu system, but you could try the following use of a Noto Sans SC Bold font loaded from the web, which worked for me on a Mac, to style a label. Output for me on a Mac was the same as the output which you say is correct on Windows and Xubunto 14.04.

Google web font reference for Noto Sans SC Bold:

The solution is loading a web font from Google rather than relying on a pre-packaged font with the app or OS.

The version of the font being loaded from the web is specified as Noto+Sans+SC:700, so the typeface is already bolded, so I don't specify a bold weight in the css, instead I just specify a "Noto Sans SC Bold" value for -fx-font-family. You could still also have -fx-font-weight: bold;, then you would likely end up with a double-bolded font, once from the font itself, then again from the weight setting. It was necessary, in this instance for me to have the word "Bold" in the family otherwise it would not be found (which is contrary to Google's instructions for use of the web font in standard HTML CSS, which did not require "Bold" in the name).

You could also download the font as .ttc file from Google and load the font from a local resource using the solution specified at:

But, I was running a version of JavaFX earlier than the minimum required version of 9 for that feature to work, so I didn't try this with an embedded font.

I know that, likely, your Xubuntu system which is not correctly displaying the font must have it somewhere already (otherwise it wouldn't be able to display the font correctly when using Text rather than Label). But, I don't know how to get that to pick up the local OS font correctly for a Label in the case you described, which is why I suggest trying a webfont or embedded font instead.

font

Sample CSS

.label-sumtotal {
    -fx-font-family: "Noto Sans SC Bold";
    -fx-text-fill: crimson;
    -fx-font-size: 32pt;
}

Web Font Sample App

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class NotoFontApp extends Application {

    @Override
    public void start(Stage stage) throws Exception {
        Label label = new Label("5,40\u20AC");
        label.getStyleClass().add("label-sumtotal");

        Pane layout = new StackPane(label);
        layout.setPadding(new Insets(10));
        Scene scene = new Scene(layout);
        scene.getStylesheets().add(
                NotoFontApp.class.getResource("font.css").toURI().toURL().toExternalForm()
        );
        scene.getStylesheets().add(
                "https://fonts.googleapis.com/css?family=Noto+Sans+SC:700&display=swap"
        );

        stage.setScene(scene);
        stage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}
jewelsea
  • 150,031
  • 14
  • 366
  • 406
  • I am really impressed about your considerate and comprehensive answer. I will try to use this as webfont as you suggested and post back about my results. Embedding the font is out of question as it will increase the size of my already big app (150MB) by another 10%. – Cheng Fei Sep 05 '19 at 17:15