From 97b21bfa8befd43956ef39b453bc0ee7c384d76e Mon Sep 17 00:00:00 2001 From: Karl Tauber Date: Sun, 4 Aug 2024 14:45:17 +0200 Subject: [PATCH] HTML: fixed occasional cutoff wrapped text when using multi-line text in HTML tags `

`...`

`, ``, ``, ``, `` or `` (issue #873; regression in 3.5) --- CHANGELOG.md | 3 + .../java/com/formdev/flatlaf/ui/FlatHTML.java | 123 +++++++++++++++--- 2 files changed, 110 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a9ecc6bd..80d21b45 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,9 @@ FlatLaf Change Log #### Fixed bugs +- HTML: Fixed occasional cutoff wrapped text when using multi-line text in HTML + tags `

`...`

`, ``, ``, ``, `` or ``. + (issue #873; regression in 3.5) - Popup: Fixed `UnsupportedOperationException: PERPIXEL_TRANSLUCENT translucency is not supported` exception on Haiku OS when showing popup (partly) outside of window. (issue #869) diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatHTML.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatHTML.java index ce942a99..c8e23107 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatHTML.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatHTML.java @@ -19,13 +19,22 @@ package com.formdev.flatlaf.ui; import java.awt.Color; import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; +import java.util.Arrays; +import java.util.HashSet; +import java.util.Locale; +import java.util.Set; +import javax.swing.AbstractButton; import javax.swing.JComponent; +import javax.swing.JLabel; +import javax.swing.JToolTip; import javax.swing.plaf.basic.BasicHTML; +import javax.swing.text.AttributeSet; import javax.swing.text.Document; import javax.swing.text.LabelView; import javax.swing.text.Style; import javax.swing.text.StyleConstants; import javax.swing.text.View; +import javax.swing.text.html.CSS; import javax.swing.text.html.HTMLDocument; import javax.swing.text.html.StyleSheet; @@ -42,7 +51,7 @@ public class FlatHTML * which re-calculates font sizes based on current component font size. * This is necessary for "absolute-size" keywords (e.g. "x-large") * for "font-size" attributes in default style sheet (see javax/swing/text/html/default.css). - * See also CSS font-size. + * See also CSS font-size. *

* This method should be invoked after {@link BasicHTML#updateRenderer(JComponent, String)}. */ @@ -61,16 +70,86 @@ public class FlatHTML // - if point size at index 7 is not 36, then probably HTML text contains BASE_SIZE rule // - if point size at index 4 is equal to given font size, then it is not necessary to add BASE_SIZE rule StyleSheet styleSheet = ((HTMLDocument)doc).getStyleSheet(); +/*debug + for( int i = 1; i <= 7; i++ ) + System.out.println( i+": "+ styleSheet.getPointSize( i ) ); +debug*/ int fontBaseSize = c.getFont().getSize(); if( styleSheet.getPointSize( 7 ) != 36f || styleSheet.getPointSize( 4 ) == fontBaseSize ) return; - // BASE_SIZE rule is parsed in javax.swing.text.html.StyleSheet.addRule() - styleSheet.addRule( "BASE_SIZE " + fontBaseSize ); - clearViewCaches( view ); + // check whether view uses "absolute-size" keywords (e.g. "x-large") for font-size + if( !usesAbsoluteSizeKeywordForFontSize( view ) ) + return; -// dumpViews( view, 0 ); + // get HTML text from component + String text; + if( c instanceof JLabel ) + text = ((JLabel)c).getText(); + else if( c instanceof AbstractButton ) + text = ((AbstractButton)c).getText(); + else if( c instanceof JToolTip ) + text = ((JToolTip)c).getTipText(); + else + return; + if( text == null ) + return; + + // BASE_SIZE rule is parsed in javax.swing.text.html.StyleSheet.addRule() + String style = ""; + String openTag = ""; + String closeTag = ""; + + String lowerText = text.toLowerCase( Locale.ENGLISH ); + int headIndex; + int styleIndex; + + int insertIndex; + if( (headIndex = lowerText.indexOf( "" )) >= 0 ) { + // there is a tag --> insert after tag + insertIndex = headIndex + "".length(); + } else if( (styleIndex = lowerText.indexOf( "