From 7d48bf06feb9d068be35ae55b15b76a4a3c50739 Mon Sep 17 00:00:00 2001 From: Karl Tauber Date: Sat, 9 Jan 2021 23:46:56 +0100 Subject: [PATCH] Button and ToggleButton: Threat Unicode surrogate character pair as single character and make button square (issue #234) --- CHANGELOG.md | 2 ++ .../com/formdev/flatlaf/ui/FlatButtonUI.java | 5 ++- .../flatlaf/testing/FlatComponentsTest.java | 36 +++++++++++++------ .../flatlaf/testing/FlatComponentsTest.jfd | 33 +++++++++++------ 4 files changed, 55 insertions(+), 21 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d12c0bb2..fd56f619 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,8 @@ FlatLaf Change Log #### Fixed bugs +- Button and ToggleButton: Threat Unicode surrogate character pair as single + character and make button square. (issue #234) - Extras: Added missing export of package `com.formdev.flatlaf.extras.components` to Java 9 module descriptor. - JIDE Common Layer: Invoke `LookAndFeelFactory.installJideExtension()` when diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatButtonUI.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatButtonUI.java index 07554acb..0c33f4f1 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatButtonUI.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatButtonUI.java @@ -251,7 +251,10 @@ public class FlatButtonUI Icon icon = ((AbstractButton)c).getIcon(); String text = ((AbstractButton)c).getText(); return (icon != null && (text == null || text.isEmpty())) || - (icon == null && text != null && ("...".equals( text ) || text.length() == 1)); + (icon == null && text != null && + ("...".equals( text ) || + text.length() == 1 || + (text.length() == 2 && Character.isSurrogatePair( text.charAt( 0 ), text.charAt( 1 ) )))); } static final int TYPE_OTHER = -1; diff --git a/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatComponentsTest.java b/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatComponentsTest.java index f677ffee..fb5aebe3 100644 --- a/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatComponentsTest.java +++ b/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatComponentsTest.java @@ -248,6 +248,7 @@ public class FlatComponentsTest JButton button14 = new JButton(); JButton button15 = new JButton(); JButton button16 = new JButton(); + JButton button24 = new JButton(); JButton button20 = new JButton(); JLabel toggleButtonLabel = new JLabel(); JToggleButton toggleButton1 = new JToggleButton(); @@ -262,6 +263,7 @@ public class FlatComponentsTest JToggleButton toggleButton12 = new JToggleButton(); JToggleButton toggleButton13 = new JToggleButton(); JToggleButton toggleButton14 = new JToggleButton(); + JToggleButton toggleButton21 = new JToggleButton(); JToggleButton toggleButton18 = new JToggleButton(); JLabel checkBoxLabel = new JLabel(); JCheckBox checkBox1 = new JCheckBox(); @@ -549,24 +551,28 @@ public class FlatComponentsTest //---- button13 ---- button13.setIcon(UIManager.getIcon("Tree.closedIcon")); - add(button13, "cell 5 1"); + add(button13, "cell 5 1 2 1"); //---- button14 ---- button14.setText("..."); - add(button14, "cell 5 1"); + add(button14, "cell 5 1 2 1"); //---- button15 ---- button15.setText("\u2026"); - add(button15, "cell 5 1"); + add(button15, "cell 5 1 2 1"); //---- button16 ---- button16.setText("#"); - add(button16, "cell 5 1"); + add(button16, "cell 5 1 2 1"); + + //---- button24 ---- + button24.setText("A"); + add(button24, "cell 5 1 2 1"); //---- button20 ---- button20.setText("Empty border"); button20.setBorder(BorderFactory.createEmptyBorder()); - add(button20, "cell 6 1"); + add(button20, "cell 5 1 2 1"); //---- toggleButtonLabel ---- toggleButtonLabel.setText("JToggleButton:"); @@ -618,27 +624,32 @@ public class FlatComponentsTest //---- toggleButton11 ---- toggleButton11.setIcon(UIManager.getIcon("Tree.closedIcon")); toggleButton11.setSelected(true); - add(toggleButton11, "cell 5 2"); + add(toggleButton11, "cell 5 2 2 1"); //---- toggleButton12 ---- toggleButton12.setText("..."); toggleButton12.setSelected(true); - add(toggleButton12, "cell 5 2"); + add(toggleButton12, "cell 5 2 2 1"); //---- toggleButton13 ---- toggleButton13.setText("\u2026"); toggleButton13.setSelected(true); - add(toggleButton13, "cell 5 2"); + add(toggleButton13, "cell 5 2 2 1"); //---- toggleButton14 ---- toggleButton14.setText("#"); toggleButton14.setSelected(true); - add(toggleButton14, "cell 5 2"); + add(toggleButton14, "cell 5 2 2 1"); + + //---- toggleButton21 ---- + toggleButton21.setText("A"); + toggleButton21.setSelected(true); + add(toggleButton21, "cell 5 2 2 1"); //---- toggleButton18 ---- toggleButton18.setText("Empty border"); toggleButton18.setBorder(BorderFactory.createEmptyBorder()); - add(toggleButton18, "cell 6 2"); + add(toggleButton18, "cell 5 2 2 1"); //---- checkBoxLabel ---- checkBoxLabel.setText("JCheckBox"); @@ -1579,6 +1590,11 @@ public class FlatComponentsTest buttonGroup1.add(magentaCyanOutlineRadioButton); // JFormDesigner - End of component initialization //GEN-END:initComponents + // Unicode surrogate character pair "script capital A" + // https://www.compart.com/en/unicode/U+1D49C + button24.setText("\uD835\uDC9C"); + toggleButton21.setText("\uD835\uDC9C"); + // BasicComboBoxRenderer customRenderer = new BasicComboBoxRenderer(); // customRenderer.setBorder( new LineBorder( Color.red ) ); // comboBox1.setRenderer( customRenderer ); diff --git a/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatComponentsTest.jfd b/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatComponentsTest.jfd index d709f036..4188313f 100644 --- a/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatComponentsTest.jfd +++ b/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatComponentsTest.jfd @@ -123,32 +123,38 @@ new FormModel { name: "button13" "icon": &SwingIcon0 new com.jformdesigner.model.SwingIcon( 2, "Tree.closedIcon" ) }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { - "value": "cell 5 1" + "value": "cell 5 1 2 1" } ) add( new FormComponent( "javax.swing.JButton" ) { name: "button14" "text": "..." }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { - "value": "cell 5 1" + "value": "cell 5 1 2 1" } ) add( new FormComponent( "javax.swing.JButton" ) { name: "button15" "text": "…" }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { - "value": "cell 5 1" + "value": "cell 5 1 2 1" } ) add( new FormComponent( "javax.swing.JButton" ) { name: "button16" "text": "#" }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { - "value": "cell 5 1" + "value": "cell 5 1 2 1" + } ) + add( new FormComponent( "javax.swing.JButton" ) { + name: "button24" + "text": "A" + }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { + "value": "cell 5 1 2 1" } ) add( new FormComponent( "javax.swing.JButton" ) { name: "button20" "text": "Empty border" "border": &EmptyBorder0 new javax.swing.border.EmptyBorder( 0, 0, 0, 0 ) }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { - "value": "cell 6 1" + "value": "cell 5 1 2 1" } ) add( new FormComponent( "javax.swing.JLabel" ) { name: "toggleButtonLabel" @@ -220,35 +226,42 @@ new FormModel { "icon": #SwingIcon0 "selected": true }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { - "value": "cell 5 2" + "value": "cell 5 2 2 1" } ) add( new FormComponent( "javax.swing.JToggleButton" ) { name: "toggleButton12" "text": "..." "selected": true }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { - "value": "cell 5 2" + "value": "cell 5 2 2 1" } ) add( new FormComponent( "javax.swing.JToggleButton" ) { name: "toggleButton13" "text": "…" "selected": true }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { - "value": "cell 5 2" + "value": "cell 5 2 2 1" } ) add( new FormComponent( "javax.swing.JToggleButton" ) { name: "toggleButton14" "text": "#" "selected": true }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { - "value": "cell 5 2" + "value": "cell 5 2 2 1" + } ) + add( new FormComponent( "javax.swing.JToggleButton" ) { + name: "toggleButton21" + "text": "A" + "selected": true + }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { + "value": "cell 5 2 2 1" } ) add( new FormComponent( "javax.swing.JToggleButton" ) { name: "toggleButton18" "text": "Empty border" "border": #EmptyBorder0 }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { - "value": "cell 6 2" + "value": "cell 5 2 2 1" } ) add( new FormComponent( "javax.swing.JLabel" ) { name: "checkBoxLabel"