Skip to content

Commit

Permalink
fix: ScaledSlider font size is not applied
Browse files Browse the repository at this point in the history
  • Loading branch information
cjenkscybercom committed Sep 12, 2022
1 parent 4701d8f commit 821cbea
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,35 @@ public static Font convert(final WidgetFont font)
});
}

/** Convert font to Java FX "-fx-font" shorthand form; e.g.
* [[ <font-style> || <font-weight> ]? <font-size> <font-family> ]
* 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}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);

}

}

0 comments on commit 821cbea

Please sign in to comment.