diff --git a/app/display/representation-javafx/src/main/java/org/csstudio/display/builder/representation/javafx/JFXUtil.java b/app/display/representation-javafx/src/main/java/org/csstudio/display/builder/representation/javafx/JFXUtil.java index d0b11b540c..6658d68288 100644 --- a/app/display/representation-javafx/src/main/java/org/csstudio/display/builder/representation/javafx/JFXUtil.java +++ b/app/display/representation-javafx/src/main/java/org/csstudio/display/builder/representation/javafx/JFXUtil.java @@ -158,6 +158,35 @@ public static Font convert(final WidgetFont font) }); } + /** Convert font to Java FX "-fx-font" shorthand form; e.g. + * [[ || ]? ] + * per https://docs.oracle.com/javase/8/javafx/api/javafx/scene/doc-files/cssref.html#typefont + * @param prefix Typically "-fx-font" + * @param font {@link Font} + * @return "-fx-font: italic 64px 'Source Sans Pro';" (recall many-word fonts must be surrounded by single quotes) + */ + public static String cssFontShorthand(final String prefix, final Font font) { + final StringBuilder buf = new StringBuilder(); + buf.append(prefix).append(": "); + switch (font.getStyle()) + { + case "Bold": + buf.append("bold "); + break; + case "Italic": + buf.append("italic "); + break; + case "Bold Italic": + buf.append("bold italic "); + break; + default: + buf.append("normal "); + } + buf.append((int)font.getSize()).append("px "); + buf.append("'").append(font.getFamily()).append("';"); + return buf.toString(); + } + /** Convert font to Java FX "-fx-font-*" * @param prefix Typically "-fx-font" * @param font {@link Font} diff --git a/app/display/representation-javafx/src/main/java/org/csstudio/display/builder/representation/javafx/widgets/ScaledSliderRepresentation.java b/app/display/representation-javafx/src/main/java/org/csstudio/display/builder/representation/javafx/widgets/ScaledSliderRepresentation.java index ec0a578150..d6d3cc00a8 100644 --- a/app/display/representation-javafx/src/main/java/org/csstudio/display/builder/representation/javafx/widgets/ScaledSliderRepresentation.java +++ b/app/display/representation-javafx/src/main/java/org/csstudio/display/builder/representation/javafx/widgets/ScaledSliderRepresentation.java @@ -470,14 +470,17 @@ public void updateChanges() final Font font = JFXUtil.convert(model_widget.propFont().getValue()); markers.setFont(font); + DecimalFormat fontSizeFormat = new DecimalFormat("#"); + final String style = // Text color (and border around the 'track') "-fx-text-background-color: " + JFXUtil.webRGB(model_widget.propForegroundColor().getValue()) + // Axis tick marks "; -fx-background: " + JFXUtil.webRGB(model_widget.propForegroundColor().getValue()) + - // Font (XXX: size isn't used, would have to set it on the SliderSkin's axis?) - "; " + JFXUtil.cssFont("-fx-tick-label-font", font); - jfx_node.setStyle(style); + // Font; NOTE only the shorthand font style is supported for fx-tick-label-font; + // e.g. fx-tick-label-font-size etc are not supported! + "; " + JFXUtil.cssFontShorthand("-fx-tick-label-font", font); + jfx_node.setStyle(style); if (model_widget.propShowScale().getValue()) { String format = model_widget.propScaleFormat().getValue(); diff --git a/app/display/representation-javafx/src/test/java/org/csstudio/display/builder/representation/javafx/JFXUtilTest.java b/app/display/representation-javafx/src/test/java/org/csstudio/display/builder/representation/javafx/JFXUtilTest.java index cdb113de13..32f014f008 100644 --- a/app/display/representation-javafx/src/test/java/org/csstudio/display/builder/representation/javafx/JFXUtilTest.java +++ b/app/display/representation-javafx/src/test/java/org/csstudio/display/builder/representation/javafx/JFXUtilTest.java @@ -8,8 +8,12 @@ package org.csstudio.display.builder.representation.javafx; import static org.hamcrest.CoreMatchers.equalTo; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertThat; +import javafx.scene.text.Font; +import javafx.scene.text.FontPosture; +import javafx.scene.text.FontWeight; import org.csstudio.display.builder.model.properties.WidgetColor; import org.junit.Test; @@ -25,4 +29,54 @@ public void testRGB() assertThat(JFXUtil.webRGB(new WidgetColor(15, 255, 0)), equalTo("#0FFF00")); assertThat(JFXUtil.webRGB(new WidgetColor(0, 16, 255)), equalTo("#0010FF")); } + + @Test + public void tetCssFont() { + + // Given a font and prefix + String prefix = "foobar"; + Font font = Font.font("serif", FontWeight.BOLD, FontPosture.ITALIC,37); + + // When converted + String actual = JFXUtil.cssFont(prefix, font); + + // Then it matches as expected, note the system lookup finds Serif instead of serif + // And the individual pieces are broken out instead of on one line + String expected = "foobar-size: 37px;foobar-family: \"Serif\";foobar-weight: bold;foobar-style: italic;"; + assertEquals(expected, actual); + + } + + @Test + public void testCssFontShorthand() { + + // Given a font and prefix + String prefix = "foobar"; + Font font = Font.font("serif", FontWeight.BOLD, FontPosture.ITALIC,37); + + // When converted + String actual = JFXUtil.cssFontShorthand(prefix, font); + + // Then it matches as expected, note the system lookup finds Serif instead of serif + String expected = "foobar: bold italic 37px 'Serif';"; + assertEquals(expected, actual); + + } + + @Test + public void testCssFontShorthandNonexistentFont() { + + // Given a font and prefix + String prefix = "foobar"; + Font font = Font.font("My Fancy Font That Doesnt Exist", FontWeight.BOLD, FontPosture.ITALIC,37); + + // When converted + String actual = JFXUtil.cssFontShorthand(prefix, font); + + // Then it matches as expected + String expected = "foobar: bold italic 37px 'System';"; + assertEquals(expected, actual); + + } + } \ No newline at end of file