org.scilab.forge.jlatexmath.ParseException Java Examples

The following examples show how to use org.scilab.forge.jlatexmath.ParseException. 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: FormulaParser.java    From pretty-formula with MIT License 6 votes vote down vote up
/**
 * Parses a mathematical formula String like "(a+b)/c" to a pretty image.
 * 
 * @param formula A raw formula input String.
 * @return An image object containing the rendered formula.
 * @throws ParseException When the formula rendering fails.
 * @throws DetailedParseCancellationException when the formula parsing fails.
 */
public static BufferedImage parseToImage(String formula)
        throws ParseException, DetailedParseCancellationException {
   String latexFormula = FormulaParser.parseToLatex(formula);
   TeXIcon icon = FormulaParser.getTeXIcon(latexFormula);

   // now create an actual image of the rendered equation
   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());
   icon.paintIcon(null, g2, 0, 0);

   return image;
}
 
Example #2
Source File: FormulaParser.java    From pretty-formula with MIT License 6 votes vote down vote up
/**
 * Parses a mathematical formula like "(a+b)/c" to a pretty image and saves
 * it as an SVG file.
 * 
 * @param formula A raw formula input String.
 * @param file The SVG file to save to.
 * @throws ParseException When parsing the LaTeX formula failed.
 * @throws IOException When writing the file failed.
 * @throws DetailedParseCancellationException When parsing the raw formula to
 * LaTeX failed.
 */
public static void saveToSVG(String formula, File file)
        throws ParseException, IOException, DetailedParseCancellationException {
      String latexFormula = FormulaParser.parseToLatex(formula);
      TeXIcon icon = FormulaParser.getTeXIcon(latexFormula);
      
      DOMImplementation domImpl = SVGDOMImplementation.getDOMImplementation();
      Document document =domImpl.createDocument(
              SVGDOMImplementation.SVG_NAMESPACE_URI, "svg", null);
      SVGGeneratorContext ctx = SVGGeneratorContext.createDefault(document);
      
      SVGGraphics2D g2 = new SVGGraphics2D(ctx, true);
      g2.setSVGCanvasSize(new Dimension(icon.getIconWidth(),icon.getIconHeight()));
      
      icon.paintIcon(null, g2, 0, 0);

      try (FileOutputStream svgs = new FileOutputStream(file)) {
         Writer out = new OutputStreamWriter(svgs, "UTF-8");
         g2.stream(out, false);
         svgs.flush();
      }
 }
 
Example #3
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;
}
 
Example #4
Source File: JLaTeXMathCache.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @param o an Object to identify the image in the cache
 * @return an array of length 3 containing width, height and depth
 */
public static int[] getCachedTeXFormulaDimensions(Object o) throws ParseException  {
    if (o == null || !(o instanceof CachedTeXFormula)) {
        return new int[] {0, 0, 0};
    }
    CachedTeXFormula cached = (CachedTeXFormula) o;
    SoftReference<CachedImage> img = cache.get(cached);
    if (img == null || img.get() == null) {
        img = makeImage(cached);
    }

    return new int[] {cached.width, cached.height, cached.depth};
}
 
Example #5
Source File: JLaTeXMathCache.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Get a cached formula
 * @param f a formula
 * @param style a style like TeXConstants.STYLE_DISPLAY
 * @param size the size of font
 * @param inset the inset to add on the top, bottom, left and right
 * @return the key in the map
 */
public static Object getCachedTeXFormula(String f, int style, int type, int size, int inset, Color fgcolor) throws ParseException  {
    CachedTeXFormula cached = new CachedTeXFormula(f, style, type, size, inset, fgcolor);
    SoftReference<CachedImage> img = cache.get(cached);
    if (img == null || img.get() == null) {
        img = makeImage(cached);
    }

    return cached;
}
 
Example #6
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 #7
Source File: JLaTeXMathCache.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Paint a cached formula
 * @param o an Object to identify the image in the cache
 * @param g the graphics where to paint the image
 * @return the key in the map
 */
public static Object paintCachedTeXFormula(Object o, Graphics2D g) throws ParseException  {
    if (o == null || !(o instanceof CachedTeXFormula)) {
        return null;
    }
    CachedTeXFormula cached = (CachedTeXFormula) o;
    SoftReference<CachedImage> img = cache.get(cached);
    if (img == null || img.get() == null) {
        img = makeImage(cached);
    }
    g.drawImage(img.get().image, identity, null);

    return cached;
}
 
Example #8
Source File: JLaTeXMathCache.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Get a cached formula
 * @param o an Object to identify the image in the cache
 * @return the cached image
 */
public static Image getCachedTeXFormulaImage(Object o) throws ParseException {
    if (o == null || !(o instanceof CachedTeXFormula)) {
        return null;
    }
    CachedTeXFormula cached = (CachedTeXFormula) o;
    SoftReference<CachedImage> img = cache.get(cached);
    if (img == null || img.get() == null) {
        img = makeImage(cached);
    }

    return img.get().image;
}
 
Example #9
Source File: JLaTeXMathCache.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
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;
}
 
Example #10
Source File: JLaTeXMathCache.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
public static int[] getCachedTeXFormulaDimensions(String f, int style, int size, int inset) throws ParseException  {
    return getCachedTeXFormulaDimensions(f, style, 0, size, inset, null);
}
 
Example #11
Source File: JLaTeXMathCache.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
public static Object getCachedTeXFormula(String f, int style, int size, int inset) throws ParseException  {
    return getCachedTeXFormula(f, style, 0, size, inset, null);
}
 
Example #12
Source File: JLaTeXMathCache.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
public static void removeCachedTeXFormula(String f, int style, int size, int inset) throws ParseException  {
    removeCachedTeXFormula(f, style, 0, size, inset, null);
}
 
Example #13
Source File: JLaTeXMathCache.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Remove a formula from the cache. Take care, remove the Object o, invalidate it !
 * @param o an Object to identify the image in the cache
 */
public static void removeCachedTeXFormula(Object o) throws ParseException  {
    if (o != null && o instanceof CachedTeXFormula) {
        cache.remove((CachedTeXFormula) o);
    }
}
 
Example #14
Source File: JLaTeXMathCache.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
public static Object paintCachedTeXFormula(String f, int style, int size, int inset, Graphics2D g) throws ParseException  {
    return paintCachedTeXFormula(f, style, 0, size, inset, null, g);
}
 
Example #15
Source File: JLaTeXMathCache.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
public static Image getCachedTeXFormulaImage(String f, int style, int size, int inset) throws ParseException {
    return getCachedTeXFormulaImage(f, style, 0, size, inset, null);
}
 
Example #16
Source File: JLaTeXMathCache.java    From lams with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Paint a cached formula
 * @param f a formula
 * @param style a style like TeXConstants.STYLE_DISPLAY
 * @param size the size of font
 * @param inset the inset to add on the top, bottom, left and right
 * @return the key in the map
 */
public static Object paintCachedTeXFormula(String f, int style, int type, int size, int inset, Color fgcolor, Graphics2D g) throws ParseException  {
    return paintCachedTeXFormula(new CachedTeXFormula(f, style, type, size, inset, fgcolor), g);
}
 
Example #17
Source File: JLaTeXMathCache.java    From lams with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Get a cached formula
 * @param f a formula
 * @param style a style like TeXConstants.STYLE_DISPLAY
 * @param size the size of font
 * @param inset the inset to add on the top, bottom, left and right
 * @return the cached image
 */
public static Image getCachedTeXFormulaImage(String f, int style, int type, int size, int inset, Color fgcolor) throws ParseException {
    return getCachedTeXFormulaImage(new CachedTeXFormula(f, style, type, size, inset, fgcolor));
}
 
Example #18
Source File: JLaTeXMathCache.java    From lams with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Remove a formula from the cache
 * @param f a formula
 * @param style a style like TeXConstants.STYLE_DISPLAY
 * @param size the size of font
 * @param inset the inset to add on the top, bottom, left and right
 */
public static void removeCachedTeXFormula(String f, int style, int type, int size, int inset, Color fgcolor) throws ParseException  {
    cache.remove(new CachedTeXFormula(f, style, type, size, inset, fgcolor));
}
 
Example #19
Source File: JLaTeXMathCache.java    From lams with GNU General Public License v2.0 2 votes vote down vote up
/**
 * @param f a formula
 * @param style a style like TeXConstants.STYLE_DISPLAY
 * @param size the size of font
 * @param inset the inset to add on the top, bottom, left and right
 * @return an array of length 3 containing width, height and depth
 */
public static int[] getCachedTeXFormulaDimensions(String f, int style, int type, int size, int inset, Color fgcolor) throws ParseException  {
    return getCachedTeXFormulaDimensions(new CachedTeXFormula(f, style, type, size, inset, fgcolor));
}