Search code examples
javafxjava-8javafx-8mathmljavafx-webengine

MathML tags don't render properly in JavaFX WebView


MathML tags don't render properly in JavaFX WebView

JDK Version : 1.8.0_192

For example,

<math xmlns="http://www.w3.org/1998/Math/MathML"> 
   <msub> 
    <mi>
      S 
    </mi> 
    <mi>
      n 
    </mi> 
   </msub> 
   <mo>
     &lt; 
   </mo> 
   <mstyle displaystyle="true" scriptlevel="0"> 
    <mfrac> 
     <mi>
       π 
     </mi> 
     <mrow> 
      <mn>
        3 
      </mn>     
      <msqrt> 
       <mn>
         3 
       </mn> 
      </msqrt> 
     </mrow> 
    </mfrac> 
   </mstyle> 
 </math>

The above code renders as:

Chrome rendering

in Google Chrome, while it renders as:

WebView version

in JavaFX WebView.

How to fix this issue?


Solution

  • The result on my computer with the code you posted ? ?

    Screenshot CaptainIRS MathML Code

    First, be sure you're using at least Java/JavaFX 8 192 build 04 or JavaFX 11 (MathML support is broken, in Java/JavaFX 9 and 10 and won't be fixed in these versions).

    Secondly, verify your font list. May be a font config issue on your computer ?

    At least one of these fonts must be installed (in order of preference):

    • Latin Modern Math

    • STIX Two Math

    • XITS Math
    • STIX Math
    • Libertinus Math
    • TeX Gyre Termes Math

    • TeX Gyre Bonum Math

    • TeX Gyre Schola

    • DejaVu Math TeX Gyre

    • TeX Gyre Pagella Math

    • Asana Math

    • Cambria Math

    • Lucida Bright Math
    • Minion Math

    • Times New Roman

    The result on my computer too, but after Latin Modern Math font installation :

    enter image description here

    Code example : did you use a code like this ?

    import java.time.LocalDate;
    import java.time.format.DateTimeFormatter;
    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.layout.StackPane;
    import javafx.scene.web.WebView;
    import javafx.stage.Stage;
    
    public class NavigateurTest extends Application {
    
        final StackPane root = new StackPane();
        final WebView webView =  new WebView();
        String ContentStackOverFlow = ""
                + "<math xmlns=\"http://www.w3.org/1998/Math/MathML\">"
                + "   <msub>"
                + "      <mi>S</mi>"
                + "      <mi>n</mi>"
                + "   </msub>"
                + "   <mo>&lt;</mo>"
                + "   <mstyle displaystyle=\"true\" scriptlevel=\"0\">"
                + "      <mfrac>"
                + "         <mi>π</mi>"
                + "         <mrow>"
                + "            <mn>3</mn>"
                + "            <msqrt>"
                + "               <mn>3</mn>"
                + "            </msqrt>"
                + "         </mrow>"
                + "      </mfrac>"
                + "   </mstyle>"
                + "</math>";
    
        public void init() {    
            root.getChildren().add(webView);
        }
    
        @Override
        public void start(Stage primaryStage) {
    
            //webView.getEngine().load("https://www.qwant.com");
            webView.getEngine().loadContent(ContentStackOverFlow);
    
            primaryStage.setTitle("OpenJFX MathML Rendering WebBrowser Test");
            primaryStage.setScene(new Scene(root));
            primaryStage.show();
    
        }
    
    }