public MyCanvas(String formulaStr) {
      this.g2 = new FXGraphics2D(getGraphicsContext2D());
      this.g2.scale(20, 20);

      // create a formula
      TeXFormula formula = new TeXFormula(formulaStr);
      TeXFormula f1 = new TeXFormula();
      f1.setLaTeX(formulaStr);
      TeXIcon icon = f1.createTeXIcon(TeXConstants.STYLE_SCRIPT, 1000);

      // the 'Box' seems to be the thing we can draw directly to Graphics2D
      this.box = icon.getBox();

      // Redraw canvas when size changes.
      widthProperty().addListener(evt -> draw());
      heightProperty().addListener(evt -> draw());
    }
  public EPLLatexTab(final EncogProgram prg) {
    super(null);

    try {
      RenderLatexExpression render = new RenderLatexExpression();
      String latex = render.render(prg);
      try {
        TeXFormula formula = new TeXFormula(latex);
        TeXIcon icon = formula.createTeXIcon(TeXConstants.STYLE_DISPLAY, 20);
        icon.setInsets(new Insets(5, 5, 5, 5));

        this.image =
            new BufferedImage(
                icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2 = image.createGraphics();
        g2.setColor(Color.white);
        g2.fillRect(0, 0, icon.getIconWidth(), icon.getIconHeight());
        JLabel jl = new JLabel();
        jl.setForeground(new Color(0, 0, 0));
        icon.paintIcon(jl, g2, 0, 0);
      } catch (Exception ex) {
        throw new UnsupportedOperationException(
            "Can't parse LATEX: " + latex + "\nFrom: " + prg.toString(), ex);
      }

    } catch (Exception ex) {
      EncogWorkBench.displayError("Error", ex);
    }
  }
Example #3
0
  public static void main(String[] args) throws FileNotFoundException {

    TeXFormula.addPredefinedCommands(new FileInputStream("Package_Foo.xml"));
    String latex = "\\begin{array}{l}";
    latex += "\\fooA{\\pi}{C}\\\\";
    latex += "\\mbox{A red circle }\\fooB{75.3}\\\\";
    latex += "\\mbox{A red disk }\\fooC[abc]{126.7}\\\\";
    latex += "\\mbox{An other red circle }\\fooD{159.81}[ab]";
    latex += "\\end{array}";

    TeXFormula formula = new TeXFormula(latex);
    TeXIcon icon = formula.createTeXIcon(TeXConstants.STYLE_DISPLAY, 20);
    icon.setInsets(new Insets(5, 5, 5, 5));

    BufferedImage image =
        new BufferedImage(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2 = image.createGraphics();
    g2.setColor(Color.white);
    g2.fillRect(0, 0, icon.getIconWidth(), icon.getIconHeight());
    JLabel jl = new JLabel();
    jl.setForeground(new Color(0, 0, 0));
    icon.paintIcon(jl, g2, 0, 0);
    File file = new File("Example.png");
    try {
      ImageIO.write(image, "png", file.getAbsoluteFile());
    } catch (IOException ex) {
    }
  }
Example #4
0
  public static void main(String[] args) {

    String latex = "\\definecolor{gris}{gray}{0.9}";
    latex += "\\definecolor{bleu}{rgb}{0,0,1}\\newcommand{\\pa}{\\left|}";
    latex += "\\begin{array}{c}";
    latex += "\\JLaTeXMath\\\\";
    latex += "\\begin{split}";
    latex += " &Тепловой\\ поток\\ \\mathrm{Тепловой\\ поток}\\ \\mathtt{Тепловой\\ поток}\\\\";
    latex += " &\\boldsymbol{\\mathrm{Тепловой\\ поток}}\\ \\mathsf{Тепловой\\ поток}\\\\";
    latex +=
        "|I_2| &= \\pa\\int_0^T\\psi(t)\\left\\{ u(a,t)-\\int_{\\gamma(t)}^a \\frac{d\\theta}{k} (\\theta,t) \\int_a^\\theta c(\\xi) u_t (\\xi,t)\\,d\\xi\\right\\}dt\\right|\\\\";
    latex +=
        "&\\le C_6 \\Bigg|\\pa f \\int_\\Omega \\pa\\widetilde{S}^{-1,0}_{a,-} W_2(\\Omega, \\Gamma_1)\\right|\\ \\right|\\left| |u|\\overset{\\circ}{\\to} W_2^{\\widetilde{A}}(\\Omega;\\Gamma_r,T)\\right|\\Bigg|\\\\";
    latex += "&\\\\";
    latex +=
        "&\\textcolor{magenta}{\\mathrm{Produit\\ avec\\ Java\\ et\\ \\LaTeX\\ par\\ }\\mathscr{C}\\mathcal{A}\\mathfrak{L}\\mathbf{I}\\mathtt{X}\\mathbb{T}\\mathsf{E}}\\\\";
    latex +=
        "&\\begin{pmatrix}\\alpha&\\beta&\\gamma&\\delta\\\\\\aleph&\\beth&\\gimel&\\daleth\\\\\\mathfrak{A}&\\mathfrak{B}&\\mathfrak{C}&\\mathfrak{D}\\\\\\boldsymbol{\\mathfrak{a}}&\\boldsymbol{\\mathfrak{b}}&\\boldsymbol{\\mathfrak{c}}&\\boldsymbol{\\mathfrak{d}}\\end{pmatrix}\\quad{(a+b)}^{\\frac{n}{2}}=\\sqrt{\\sum_{k=0}^n\\tbinom{n}{k}a^kb^{n-k}}\\quad \\Biggl(\\biggl(\\Bigl(\\bigl(()\\bigr)\\Bigr)\\biggr)\\Biggr)\\\\";
    latex +=
        "&\\forall\\varepsilon\\in\\mathbb{R}_+^*\\ \\exists\\eta>0\\ |x-x_0|\\leq\\eta\\Longrightarrow|f(x)-f(x_0)|\\leq\\varepsilon\\\\";
    latex +=
        "&\\det\\begin{bmatrix}a_{11}&a_{12}&\\cdots&a_{1n}\\\\a_{21}&\\ddots&&\\vdots\\\\\\vdots&&\\ddots&\\vdots\\\\a_{n1}&\\cdots&\\cdots&a_{nn}\\end{bmatrix}\\overset{\\mathrm{def}}{=}\\sum_{\\sigma\\in\\mathfrak{S}_n}\\varepsilon(\\sigma)\\prod_{k=1}^n a_{k\\sigma(k)}\\\\";
    latex +=
        "&\\Delta f(x,y)=\\frac{\\partial^2f}{\\partial x^2}+\\frac{\\partial^2f}{\\partial y^2}\\qquad\\qquad \\fcolorbox{noir}{gris}{n!\\underset{n\\rightarrow+\\infty}{\\sim} {\\left(\\frac{n}{e}\\right)}^n\\sqrt{2\\pi n}}\\\\";
    latex +=
        "&\\sideset{_\\alpha^\\beta}{_\\gamma^\\delta}{\\begin{pmatrix}a&b\\\\c&d\\end{pmatrix}}\\xrightarrow[T]{n\\pm i-j}\\sideset{^t}{}A\\xleftarrow{\\overrightarrow{u}\\wedge\\overrightarrow{v}}\\underleftrightarrow{\\iint_{\\mathds{R}^2}e^{-\\left(x^2+y^2\\right)}\\,\\mathrm{d}x\\mathrm{d}y}";
    latex += "\\end{split}\\\\";
    latex +=
        "\\rotatebox{30}{\\sum_{n=1}^{+\\infty}}\\quad\\mbox{Mirror rorriM}\\reflectbox{\\mbox{Mirror rorriM}}";
    latex += "\\end{array}";

    TeXFormula formula = new TeXFormula(latex);
    TeXIcon icon = formula.createTeXIcon(TeXConstants.STYLE_DISPLAY, 20);
    icon.setInsets(new Insets(5, 5, 5, 5));

    BufferedImage image =
        new BufferedImage(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2 = image.createGraphics();
    g2.setColor(Color.white);
    g2.fillRect(0, 0, icon.getIconWidth(), icon.getIconHeight());
    JLabel jl = new JLabel();
    jl.setForeground(new Color(0, 0, 0));
    icon.paintIcon(jl, g2, 0, 0);
    File file = new File("Example3.png");
    try {
      ImageIO.write(image, "png", file.getAbsoluteFile());
    } catch (IOException ex) {
    }
  }
Example #5
0
  /**
   * Renders LaTeX equation using JLaTeXMath
   *
   * @param app
   * @param g2
   * @param x
   * @param y
   * @param text
   * @param font
   * @param serif
   * @param fgColor
   * @param bgColor
   * @return dimension of rendered equation
   */
  public final FormulaDimension drawEquationJLaTeXMath(
      Application app,
      GeoElement geo,
      Graphics2D g2,
      int x,
      int y,
      String text,
      Font font,
      boolean serif,
      Color fgColor,
      Color bgColor,
      boolean useCache,
      Integer maxWidth,
      Float lineSpace) {
    // TODO uncomment when \- works
    // text=addPossibleBreaks(text);

    int width = -1;
    int height = -1;
    int depth = 0;

    if (drawEquationJLaTeXMathFirstCall) { // first call

      drawEquationJLaTeXMathFirstCall = false;

      // initialise definitions
      if (initJLaTeXMath == null)
        initJLaTeXMath =
            new TeXFormula(
                "\\DeclareMathOperator{\\sech}{sech} \\DeclareMathOperator{\\csch}{csch} \\DeclareMathOperator{\\erf}{erf}");

      // make sure cache doesn't get too big
      JLaTeXMathCache.setMaxCachedObjects(100);

      Iterator<String> it = Unicode.getCharMapIterator();

      while (it.hasNext()) {
        String lang = it.next();
        Character ch = Unicode.getTestChar(lang);
        Font testFont = app.getFontCanDisplay(ch.toString(), true, Font.PLAIN, 12);
        if (testFont != null)
          TeXFormula.registerExternalFont(Character.UnicodeBlock.of(ch), testFont.getFontName());
        // Application.debug("LaTeX font registering: "+lang+" "+testFont.getFontName());

      }

      // Arabic is in standard Java fonts, so we don't need to search for a font
      TeXFormula.registerExternalFont(Character.UnicodeBlock.of('\u0681'), "Sans Serif", "Serif");

      try {
        WebStartAlphabetRegistration.register(AlphabetRegistration.JLM_GREEK);
        WebStartAlphabetRegistration.register(AlphabetRegistration.JLM_CYRILLIC);
        // URLAlphabetRegistration.register(new URL(app.getCodeBase()+"jlm_greek.jar"),
        // "greek",URLAlphabetRegistration.JLM_GREEK);
        // URLAlphabetRegistration.register(new URL(app.getCodeBase()+"jlm_cyrillic.jar"),
        // "cyrillic",URLAlphabetRegistration.JLM_CYRILLIC);
      } catch (Exception e) {
        e.printStackTrace();
      }
      LatexConvertorFactory factory = new LatexConvertorFactory(app.getKernel());
      DynamicAtom.setExternalConverterFactory(factory);
    }

    int style = 0;
    if (font.isBold()) style = style | TeXFormula.BOLD;
    if (font.isItalic()) style = style | TeXFormula.ITALIC;
    if (!serif) style = style | TeXFormula.SANSSERIF;

    // if we're exporting, we want to draw it full resolution
    // if it's a \jlmDynamic text, we don't want to add it to the cache
    if (app.exporting || text.indexOf("\\jlmDynamic") > -1 || !useCache) {

      // Application.debug("creating new icon for: "+text);
      TeXFormula formula;
      TeXIcon icon;

      try {
        formula = new TeXFormula(text);

        if (maxWidth == null)
          icon =
              formula.createTeXIcon(TeXConstants.STYLE_DISPLAY, font.getSize() + 3, style, fgColor);
        else
          icon =
              formula.createTeXIcon(
                  TeXConstants.STYLE_DISPLAY,
                  font.getSize() + 3,
                  TeXConstants.UNIT_CM,
                  maxWidth.intValue(),
                  TeXConstants.ALIGN_LEFT,
                  TeXConstants.UNIT_CM,
                  lineSpace.floatValue());
      } catch (MyError e) {
        // e.printStackTrace();
        // Application.debug("MyError LaTeX parse exception: "+e.getMessage()+"\n"+text);
        // Write error message to Graphics View

        formula = TeXFormula.getPartialTeXFormula(text);
        icon =
            formula.createTeXIcon(TeXConstants.STYLE_DISPLAY, font.getSize() + 3, style, fgColor);

        formula.createTeXIcon(
            TeXConstants.STYLE_DISPLAY,
            15,
            TeXConstants.UNIT_CM,
            4f,
            TeXConstants.ALIGN_LEFT,
            TeXConstants.UNIT_CM,
            0.5f);

        // Rectangle rec = drawMultiLineText(e.getMessage()+"\n"+text, x, y +
        // g2.getFont().getSize(), g2);
        // return new Dimension(rec.width, rec.height);
      } catch (Exception e) {
        // e.printStackTrace();
        // Application.debug("LaTeX parse exception: "+e.getMessage()+"\n"+text);
        // Write error message to Graphics View

        formula = TeXFormula.getPartialTeXFormula(text);
        icon =
            formula.createTeXIcon(TeXConstants.STYLE_DISPLAY, font.getSize() + 3, style, fgColor);

        // Rectangle rec = drawMultiLineText(e.getMessage()+"\n"+text, x, y +
        // g2.getFont().getSize(), g2);
        // return new Dimension(rec.width, rec.height);
      }
      icon.setInsets(new Insets(1, 1, 1, 1));

      jl.setForeground(fgColor);
      icon.paintIcon(jl, g2, x, y);
      return new FormulaDimension(icon.getIconWidth(), icon.getIconHeight(), icon.getIconDepth());
    }

    Object key = null;
    Image im = null;
    try {
      // if geoText != null then keep track of which key goes with the GeoText
      // so that we can remove it from the cache if it changes
      // eg for a (regular) dynamic LaTeX text eg "\sqrt{"+a+"}"
      if (geo == null)
        key =
            JLaTeXMathCache.getCachedTeXFormula(
                text,
                TeXConstants.STYLE_DISPLAY,
                style,
                font.getSize() + 3 /*font size*/,
                1 /* inset around the label*/,
                fgColor);
      else key = geo.getLaTeXCache().getCachedLaTeXKey(text, font.getSize() + 3, style, fgColor);

      im = JLaTeXMathCache.getCachedTeXFormulaImage(key);

      int ret[] = JLaTeXMathCache.getCachedTeXFormulaDimensions(key);
      width = ret[0];
      height = ret[1];
      depth = ret[2];

    } catch (Exception e) {
      // Application.debug("LaTeX parse exception: "+e.getMessage()+"\n"+text);
      // Write error message to Graphics View

      TeXFormula formula = TeXFormula.getPartialTeXFormula(text);
      im =
          formula.createBufferedImage(
              TeXConstants.STYLE_DISPLAY, font.getSize() + 3, Color.black, Color.white);

      // Rectangle rec = drawMultiLineText(e.getMessage()+"\n"+text, x, y + g2.getFont().getSize(),
      // g2);
      // return new Dimension(rec.width, rec.height);

    }

    g2.drawImage(im, x, y, null);

    if (width == -1) {
      width = im.getWidth(null);
    }
    if (height == -1) {
      height = im.getHeight(null);
    }

    return new FormulaDimension(width, height, depth);
  }