Java源码示例:org.scilab.forge.jlatexmath.TeXFormula

示例1
/**
 * 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);
}
 
示例2
/**
 * 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);
}
 
示例3
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);
		}
	}
}
 
示例4
private static SoftReference<CachedImage> makeImage(CachedTeXFormula cached) throws ParseException {
    TeXFormula formula = new TeXFormula(cached.f);
    TeXIcon icon = formula.createTeXIcon(cached.style, cached.size, cached.type, cached.fgcolor);
    icon.setInsets(new Insets(cached.inset, cached.inset, cached.inset, cached.inset));
    BufferedImage image = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2 = image.createGraphics();
    icon.paintIcon(null, g2, 0, 0);
    g2.dispose();
    cached.setDimensions(icon.getIconWidth(), icon.getIconHeight(), icon.getIconDepth());
    SoftReference<CachedImage> img = new SoftReference<CachedImage>(new CachedImage(image, cached), queue);

    if (cache.size() >= max) {
        Reference<? extends CachedImage> soft;
        while ((soft = queue.poll()) != null) {
            CachedImage ci = (CachedImage) soft.get();
            if (ci != null) {
                cache.remove(ci.cachedTf);
            }
        }
        Iterator<CachedTeXFormula> iter = cache.keySet().iterator();
        if (iter.hasNext()) {
            CachedTeXFormula c = iter.next();
            SoftReference<CachedImage> cachedImage = cache.get(c);
            if (cachedImage != null) {
                cachedImage.clear();
            }
            cache.remove(c);
        }
    }
    cache.put(cached, img);

    return img;
}
 
示例5
/**
 * 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;
}