org.scilab.forge.jlatexmath.TeXConstants Java Examples

The following examples show how to use org.scilab.forge.jlatexmath.TeXConstants. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Draw.java    From MeteoInfo with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Get string dimension
 *
 * @param str String
 * @param angle Angle
 * @param g Graphics2D
 * @param isLaTeX Is LaTeX or not
 * @return String dimension
 */
public static Dimension getStringDimension(String str, float angle, Graphics2D g, boolean isLaTeX) {
    float width, height;
    if (isLaTeX) {
        float size = g.getFont().getSize2D();
        // create a formula
        TeXFormula formula = new TeXFormula(str);

        // render the formla to an icon of the same size as the formula.
        TeXIcon icon = formula.createTeXIcon(TeXConstants.STYLE_TEXT, size);

        // insert a border 
        //icon.setInsets(new Insets(5, 5, 5, 5));
        //return new Dimension(icon.getIconWidth(), icon.getIconHeight());
        width = (int) icon.getTrueIconWidth() + 10;
        height = (int) icon.getTrueIconHeight();
        //height = icon.getIconHeight();
    } else {
        FontMetrics metrics = g.getFontMetrics();
        width = metrics.stringWidth(str);
        height = metrics.getAscent();
    }
    float temp;
    if (angle == 90 || angle == -90) {
        temp = width;
        width = height;
        height = temp;
    } else {
        width = (float) (width * Math.cos(Math.toRadians(angle))) + (float) (height * Math.sin(Math.toRadians(angle)));
        height = (float) (width * Math.sin(Math.toRadians(angle))) + (float) (height * Math.cos(Math.toRadians(angle)));
    }
    return new Dimension((int) width, (int) height);
}
 
Example #2
Source File: Draw.java    From MeteoInfo with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Draw LaTeX string
 *
 * @param g Graphics2D
 * @param str String
 * @param size Size
 * @param x X
 * @param y Y
 * @param useExternalFont If use external font
 */
public static void drawLaTeX(Graphics2D g, String str, float size, float x, float y, boolean useExternalFont) {
    if (useExternalFont) {
        //Set font
        TeXFormula.registerExternalFont(Character.UnicodeBlock.BASIC_LATIN, g.getFont().getName());
    } else {
        TeXFormula.registerExternalFont(Character.UnicodeBlock.BASIC_LATIN, null, null);
    }

    // create a formula
    TeXFormula formula = new TeXFormula(str);

    // render the formla to an icon of the same size as the formula.
    TeXIcon icon = formula.createTeXIcon(TeXConstants.STYLE_TEXT, size);

    // insert a border 
    icon.setInsets(new Insets(5, 5, 5, 5));
    icon.setForeground(g.getColor());
    y = y - icon.getIconHeight() + (icon.getIconHeight() - icon.getTrueIconHeight()) * 0.6f;
    //y = y - icon.getIconHeight() + size * 0.7f;
    //y = y - icon.getTrueIconHeight() * 1.f;
    Font font = g.getFont();
    icon.paintIcon(null, g, (int) x, (int) y);
    g.setFont(font);
}
 
Example #3
Source File: TextElement.java    From PDF4Teachers with Apache License 2.0 5 votes vote down vote up
public static BufferedImage renderLatex(String text, java.awt.Color color, int size){
	try {

		TeXFormula formula = new TeXFormula(text);
		formula.setColor(color);

		TeXIcon icon = formula.createTeXIcon(TeXConstants.STYLE_DISPLAY, size*imageFactor);
		icon.setInsets(new Insets((int) (-size*imageFactor/7), (int) (-size*imageFactor/7), (int) (-size*imageFactor/7), (int) (-size*imageFactor/7)));

		BufferedImage image = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_INT_ARGB);
		Graphics2D g = image.createGraphics();
		g.setBackground(new java.awt.Color(0f, 0f, 0f, 1f));
		icon.paintIcon(null, g, 0, 0);

		return image;

	}catch(ParseException ex){
		if(ex.getMessage().contains("Unknown symbol or command or predefined TeXFormula: ")){
			return renderLatex("$" + TR.tr("Commande/Symbole~inconnu~:") + "\\\\" +
					ex.getMessage().replaceAll(Pattern.quote("Unknown symbol or command or predefined TeXFormula:"), ""), color, size);
		}else if(text.startsWith(TR.tr("Erreur~:") + "\\\\")){
			return renderLatex(TR.tr("Impossible~de~lire~la~formule"), color, size);
		}else{
			return renderLatex(TR.tr("Erreur~:") + "\\\\" + ex.getMessage(), color, size);
		}
	}
}
 
Example #4
Source File: FormulaParser.java    From pretty-formula with MIT License 5 votes vote down vote up
/**
 * Renders a valid LaTeX math formula to an icon.
 * 
 * @param formula Valid LaTeX formula.
 * @return Rendered Icon.
 * @throws ParseException When rendering has failed (e.g. the formula was
 * not valid).
 */
private static TeXIcon getTeXIcon(String formula) throws ParseException {
   TeXFormula latexFormula = new TeXFormula(formula);

   // render the formla to an icon of the same size as the formula.
   TeXIcon icon = latexFormula.createTeXIcon(TeXConstants.STYLE_DISPLAY, 20);

   // insert a border 
   icon.setInsets(new Insets(5, 5, 5, 5));
   
   return icon;
}