bell-sw / LibericaNIK

Native Image Kit
8 stars 0 forks source link

Native Image Kit - Inconsistent Swing HTML Handling in compiled version #12

Open apocalyptech opened 1 year ago

apocalyptech commented 1 year ago

I'm working w/ Liberica NIK 22 (Java 17), and I've noticed that Liberica NIK-compiled Swing applications don't have consistent handling of HTML rendering, compared to the non-compiled versions. The two I've noticed in particular is that <ul> elements don't get bullet points (or get indented), and that <nobr> tags seem to be ignored. A simple testcase for the <ul> element could be shown with:

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class test {

    public static void main (String[] args) {
        JDialog myDialog = new JDialog();
        myDialog.setTitle("Testing");

        JPanel myPanel = new JPanel();
        myDialog.add(myPanel);

        JLabel myLabel = new JLabel(
                "<html>"
                + "<ul>"
                + "<li>List Item One</li>"
                + "<li>List Item Two</li>"
                + "</ul>"
        );
        myPanel.add(myLabel);

        JButton okButton = new JButton("OK");
        okButton.addActionListener(e -> myDialog.dispose());
        myPanel.add(okButton);

        myDialog.pack();
        myDialog.setVisible(true);
    }

}

When run with the usual JVMs (including the JVM included with the Liberica NIK), it renders like so:

run_as_jar

... but once compiled with native-image, it ends up looking like:

run_as_exe

I've verified that I'm running both with the same Liberica NIK environments:

C:\Users\cj\libericatest>where java
C:\Program Files\BellSoft\LibericaNIK-22-OpenJDK-17\bin\java.exe

C:\Users\cj\libericatest>where native-image
C:\Program Files\BellSoft\LibericaNIK-22-OpenJDK-17\bin\native-image.cmd

C:\Users\cj\libericatest>echo %JAVA_HOME%
C:\Program Files\BellSoft\LibericaNIK-22-OpenJDK-17\

Let me know if I can provide any more info!

AlexanderScherbatiy commented 1 year ago

Thank you for the report.

The problem is that the src/java.desktop/share/classes/javax/swing/text/html/default.css from jdk is not added as a resource when html is used by a Swing application. default.css contains some default values which updates component margins and insets.

For some reason native-image-agent does not dump the default.css resource:

java -agentlib:native-image-agent=config-output-dir=conf-dir test
cat conf-dir/resource-config.json

{
  "resources":{
  "includes":[]},
  "bundles":[
    {
      "name":"com.sun.swing.internal.plaf.basic.resources.basic",
      "classNames":["com.sun.swing.internal.plaf.basic.resources.basic"]
    },
    {
      "name":"com.sun.swing.internal.plaf.metal.resources.metal",
      "classNames":["com.sun.swing.internal.plaf.metal.resources.metal"]
    },
    {
      "name":"sun.awt.resources.awt",
      "classNames":["sun.awt.resources.awt"]
    }
  ]
}
AlexanderScherbatiy commented 1 year ago

The workaround is to explicitly include default.css into the native image building with -H:IncludeResources=.*/default.css$ option:

native-image.cmd  -Djava.awt.headless=false  -H:IncludeResources=.*/default.css$ test
apocalyptech commented 1 year ago

Hello! Indeed, that workaround works great. Thanks a bunch! That's definitely good enough for me -- feel free to close this out if there's nothing else you wanted to look into about it.

Cheers!