예제 #1
0
  public void setLaTeX(Application app, String str) {

    if (str.indexOf('"') > -1) {

      GeoText text = app.getKernel().getAlgebraProcessor().evaluateToText(str, false, false);

      if (text != null) {
        text.setLaTeX(true, false);
        str = text.getTextString();
      } else {
        // bad syntax, remove all quotes and use raw string
        while (str.indexOf('"') > -1) str = str.replace('"', ' ');
        // latexPreview.setLaTeX(str);
      }
    } else {
      // latexPreview.setLaTeX(str);

    }

    String f = str.trim();
    if (f.length() >= 2 && f.startsWith("$") && f.endsWith("$")) {
      f = f.substring(1, f.length() - 1);
    }

    im =
        (BufferedImage)
            TeXFormula.getPartialTeXFormula(f)
                .createBufferedImage(
                    TeXConstants.STYLE_DISPLAY, defaultSize, Color.black, Color.white);

    /*
    icon = TeXFormula.getPartialTeXFormula(f).createTeXIcon(
    		TeXConstants.STYLE_DISPLAY, defaultSize);
    if (icon == null) {
    	icon = TeXFormula.getPartialTeXFormula("").createTeXIcon(
    			TeXConstants.STYLE_DISPLAY, defaultSize);
    }

    width = icon.getIconWidth();
    height = icon.getIconHeight();*/
    width = im.getWidth();
    height = im.getHeight();
    Dimension dim = new Dimension(width + 2 * INSET, height + 2 * INSET);
    setPreferredSize(dim);
    setSize(dim);
    setLocation(0, 0);
    setVisible(true);
    repaint();
  }
예제 #2
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);
  }