Skip to content

Commit

Permalink
Merge PR #931: Fixing NPE when using HTML text on a component with nu…
Browse files Browse the repository at this point in the history
…ll font
  • Loading branch information
DevCharly committed Dec 8, 2024
2 parents ef61ae5 + f96baf1 commit 41332de
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ FlatLaf Change Log
- Extras: `FlatSVGIcon` color filters now can access painting component to
implement component state based color mappings. (PR #906)

#### Fixed bugs

- HTML: Fixed NPE when using HTML text on a component with `null` font. (issue
#930; PR #931; regression in 3.5)

## 3.5.3

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.formdev.flatlaf.ui;

import java.awt.Color;
import java.awt.Font;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.Arrays;
Expand Down Expand Up @@ -74,9 +75,9 @@ public static void updateRendererCSSFontBaseSize( JComponent c ) {
for( int i = 1; i <= 7; i++ )
System.out.println( i+": "+ styleSheet.getPointSize( i ) );
debug*/
int fontBaseSize = c.getFont().getSize();
Font font = c.getFont();
if( styleSheet.getPointSize( 7 ) != 36f ||
styleSheet.getPointSize( 4 ) == fontBaseSize )
font == null || styleSheet.getPointSize( 4 ) == font.getSize() )
return;

// check whether view uses "absolute-size" keywords (e.g. "x-large") for font-size
Expand All @@ -97,7 +98,7 @@ else if( c instanceof JToolTip )
return;

// BASE_SIZE rule is parsed in javax.swing.text.html.StyleSheet.addRule()
String style = "<style>BASE_SIZE " + c.getFont().getSize() + "</style>";
String style = "<style>BASE_SIZE " + font.getSize() + "</style>";
String openTag = "";
String closeTag = "";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.formdev.flatlaf.ui;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.Locale;
import javax.swing.JComponent;
Expand Down Expand Up @@ -73,6 +74,15 @@ void htmlWithStyleTag() {
testHtmlBaseSize( "<html>${BASE_SIZE}<style type='text/css'>body { color: #f00; }</style><h1>header1</h1>" + body + "</html>", "header1\n" + bodyPlain );
}

@Test
void htmlOnComponentWithNullFont() {
assertDoesNotThrow( () -> {
JLabel label = new JLabel();
label.setFont( null );
label.setText( "<html>foo<br>bar</html>" );
} );
}

private void testHtmlBaseSize( String html, String expectedPlain ) {
testHtmlBaseSizeImpl( html, expectedPlain );
testHtmlBaseSizeImpl( html.toUpperCase( Locale.ENGLISH ), expectedPlain.toUpperCase( Locale.ENGLISH ) );
Expand Down

0 comments on commit 41332de

Please sign in to comment.