Skip to content

Commit

Permalink
descent is positive, introduce descentSigned() for negative descent
Browse files Browse the repository at this point in the history
  • Loading branch information
xzel23 committed Dec 4, 2024
1 parent 6e9f1f6 commit e591e45
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ private FontData getFontData(Font fxFont) {
Bounds bounds = text.getBoundsInLocal();
float ascent = (float) text.getBaselineOffset();
float height = (float) bounds.getHeight();
float descent = ascent - height; // descent is negative!
float descent = height - ascent;

text.setText(" ");
bounds = text.getBoundsInLocal();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ public Font getDefaultFont() {

private FontData getFontData(java.awt.Font awtFont) {
FontDef fontDef = new FontDef();
fontDef.setFamily(awtFont.getFamily());
fontDef.setFamily(awtFont.getName());
fontDef.setSize(awtFont.getSize2D());
fontDef.setBold(awtFont.isBold());
fontDef.setItalic(awtFont.isItalic());
Expand Down
11 changes: 10 additions & 1 deletion utility/src/main/java/com/dua3/utility/text/FontData.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,20 @@ public record FontData(
assert !family.isEmpty() : "family is the empty string";
assert size >= 0 : "size is negative";
assert ascent >= 0 : "ascent is negative";
assert descent <= 0 : "descent is positive";
assert descent >= 0 : "descent is negative";
assert height >= ascent : "inconsistent height";
assert spaceWidth > 0 : "space width must be positive";
}

/**
* Returns the negative value of the descent attribute for the font.
*
* @return the signed descent, which is the negative value of the descent.
*/
public double descentSigned() {
return -descent;
}

/**
* Test if two fonts are similar. This method is provided to be used when inheriting fonts because equals
* also tests both instances to be of the exact same class.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public void testFontCss() {
builder.append(" too many fonts!");
builder.pop(style1);
RichText rt = builder.toRichText();
String expected = "<span class=\"arial-bold-16.0-#000000\">Don&apos;t <span class=\"courier-12.0-#000000\">mix</span> too many fonts!</span>";
String expected = "<span class=\"arial-bold-normal-none-no_line-16.0-#000000\">Don&apos;t <span class=\"courier-regular-normal-none-no_line-12.0-#000000\">mix</span> too many fonts!</span>";
String actual = HtmlConverter.create(HtmlConverter.useCss(true)).convert(rt);

assertEquals(expected, actual);
Expand Down

0 comments on commit e591e45

Please sign in to comment.