import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;
public class App extends Application{
	 /*
	   * The orange text has its font size set to 30 *points* via CSS. The green
	   * text has its font size set to 30 *pixels* via CSS. And the red text has
	   * its font size set to 30 *points* via code. This means the orange text
	   * and the red text should be the same size while the green text is smaller.
	   *
	   * What actually occurs is that the orange text is larger than both the green
	   * and the red text, where the latter two are the same size.
	   */

	  @Override
	  public void start(Stage primaryStage) {
	    // Set font size to 30pt via CSS
	    var ptText = new Text("Test");
	    ptText.setFill(Color.ORANGE);
	    ptText.setStyle("-fx-font: 30pt 'System';");

	    // Set font size to 30px via CSS
	    var pxText = new Text("Test");
	    pxText.setFill(Color.GREEN);
	    pxText.setStyle("-fx-font: 30px 'System';");

	    // Set font size to 30pt via code
	    var codeText = new Text("Test");
	    codeText.setFill(Color.RED);
	    codeText.setFont(Font.font("System", 30.0));

	    var root = new HBox(5.0, ptText, pxText, codeText);
	    root.setAlignment(Pos.BASELINE_CENTER);
	    root.setPadding(new Insets(10.0));

	    primaryStage.setScene(new Scene(root, 300, -1));
	    primaryStage.show();

	    /*
	     * Print the (LOGICAL) text heights only after CSS has been applied. The
	     * output is:
	     *
	     * Text height with font size '30pt' set via ' CSS' = 53.20
	     * Text height with font size '30px' set via ' CSS' = 39.90
	     * Text height with font size '30pt' set via 'CODE' = 39.90
	     *
	     * Which suggests points are converted to pixels using a DPI of 96. For
	     * instance, 30pt * (96dpi / 72ppi) is approximately 40 pixels, which is
	     * what the height for 30pt font size via CODE is. But the same conversion
	     * occurs for a font size of 30px via CSS even though the size is already
	     * in pixels. And when the font size is set to 30pt via CSS it looks like
	     * the conversion is applied *twice*--once by FontConverter and then again
	     * by whatever code is responsible for rendering the text.
	     */
	    printTextHeight("30pt", "CSS", ptText);
	    printTextHeight("30px", "CSS", pxText);
	    printTextHeight("30pt", "CODE", codeText);
	  }

	  private void printTextHeight(String size, String source, Text text) {
	    var fmt = "Text height with font size '%s' set via '%4s' = %.2f%n";
	    System.out.printf(fmt, size, source, text.getLayoutBounds().getHeight());
	  }
}
