ides.api.latex
Class Renderer

java.lang.Object
  extended by ides.api.latex.Renderer

public class Renderer
extends java.lang.Object

This class allows for rendering of LaTeX documents from within Java. It can be used also for the coversion of LaTeX documents into EPS files (only the first page is output).

The external software that has to be available is:

To use the renderer:

 Renderer r = Renderer.getRenderer("path to latex", "path to ghostscript");
 
 Image i = r.renderString("$\\alpha$");
 

Version:
2.1
Author:
Lenko Grigorov, Michael Wood

Field Summary
protected  java.io.File gsPath
          The path to ghostscript
protected  java.io.File latexPath
          The path to latex+dvips
static int SCREEN_RESOLUTION
          The resolution to be used when rendering LaTeX files
static int TIMEOUT
          The time-out when calling external programs.
 
Method Summary
protected  void fixBoundingBox(java.awt.Rectangle boundary, java.io.File source, java.io.File target)
          Fixes the BoundingBox comment in EPS files so it reflects the true boundary of the graphic.
static java.awt.image.BufferedImage getEmptyImage()
          Returns an empty image (1x1 pixel, white).
static Renderer getRenderer(java.io.File latexPath, java.io.File gsPath)
          Create a new Renderer which will use the provided paths when calling LaTeX, dvips and GhostScript.
 void latex2EPS(java.io.File doc, java.io.File target)
          Convert the first page of a LaTeX document into an EPS file.
 void latex2EPS(java.io.InputStream doc, java.io.File target)
          Convert the first page of a LaTeX document into an EPS file.
 void latex2EPS(java.lang.String doc, java.io.File target)
          Convert the first page of a LaTeX document into an EPS file.
protected  java.awt.image.BufferedImage latex2PNG(java.io.File latexFile, int dpi)
          Renders the first page of the LaTeX document contained in a file into an uncropped image.
 java.awt.image.BufferedImage renderDocument(java.io.File doc)
          Render the first page of a LaTeX document.
 java.awt.image.BufferedImage renderDocument(java.io.File doc, java.io.File f)
          Render the first page of a LaTeX document into a PNG image file.
 java.awt.image.BufferedImage renderDocument(java.io.File doc, java.io.OutputStream out)
          Render the first page of a LaTeX document into a PNG image and output to a stream.
 java.awt.image.BufferedImage renderDocument(java.io.InputStream doc)
          Render the first page of a LaTeX document.
 java.awt.image.BufferedImage renderDocument(java.io.InputStream doc, java.io.File f)
          Render the first page of a LaTeX document into a PNG image file.
 java.awt.image.BufferedImage renderDocument(java.io.InputStream doc, java.io.OutputStream out)
          Render the first page of a LaTeX document into a PNG image and output to a stream.
 java.awt.image.BufferedImage renderDocument(java.lang.String doc)
          Render the first page of a LaTeX document.
 java.awt.image.BufferedImage renderDocument(java.lang.String doc, java.io.File f)
          Render the first page of a LaTeX document into a PNG image file.
 java.awt.image.BufferedImage renderDocument(java.lang.String doc, java.io.OutputStream out)
          Render the first page of a LaTeX document into a PNG image and output to a stream.
 java.awt.image.BufferedImage renderString(java.lang.String s)
          Render a piece of LaTeX code.
 java.awt.image.BufferedImage renderString(java.lang.String s, java.io.File f)
          Render a piece of LaTeX code into a PNG image file.
 java.awt.image.BufferedImage renderString(java.lang.String s, java.io.OutputStream out)
          Render a piece of LaTeX code into a PNG image and output to a stream.
 java.awt.image.BufferedImage renderString(java.lang.String s, java.lang.String header)
          Render a piece of LaTeX code.
 java.awt.image.BufferedImage renderString(java.lang.String s, java.lang.String header, java.io.File f)
          Render a piece of LaTeX code into a PNG image file.
 java.awt.image.BufferedImage renderString(java.lang.String s, java.lang.String header, java.io.OutputStream out)
          Render a piece of LaTeX code into a PNG image and output to a stream.
protected  java.awt.image.BufferedImage renderTempFile(java.io.File latexFile)
          Render the first page of the LaTeX document contained in a temporary file.
protected  void tempLatex2EPS(java.io.File latexFile, java.io.File target)
          Convert the first page of a LaTeX document contained in a temporary file into an EPS file.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

TIMEOUT

public static final int TIMEOUT
The time-out when calling external programs.

See Also:
Constant Field Values

SCREEN_RESOLUTION

public static final int SCREEN_RESOLUTION
The resolution to be used when rendering LaTeX files

See Also:
Constant Field Values

latexPath

protected java.io.File latexPath
The path to latex+dvips


gsPath

protected java.io.File gsPath
The path to ghostscript

Method Detail

getRenderer

public static Renderer getRenderer(java.io.File latexPath,
                                   java.io.File gsPath)
Create a new Renderer which will use the provided paths when calling LaTeX, dvips and GhostScript.

Parameters:
latexPath - the path to latex and dvips (e.g., "c:\texmf\miktex\bin")
gsPath - the path to gswin32c (e.g., "c:\gs\gs8.14\bin")
Returns:
a new Renderer which will use the provided paths when calling LaTeX, dvips and GhostScript.

renderString

public java.awt.image.BufferedImage renderString(java.lang.String s,
                                                 java.io.File f)
                                          throws java.io.IOException,
                                                 LatexRenderException
Render a piece of LaTeX code into a PNG image file.

Parameters:
s - LaTeX code (the code goes in the body of a LaTeX document)
f - file where to save image. If the file exists, its content will be overwritten.
Returns:
the image which was saved in the file
Throws:
java.io.IOException - if there was a problem with the IO
LatexRenderException - if there was a problem while rendering the LaTeX code
See Also:
renderString(String, OutputStream), renderString(String, String, File), renderString(String, String, OutputStream), renderDocument(File, File), renderDocument(File, OutputStream), renderDocument(InputStream, File), renderDocument(InputStream, OutputStream), renderDocument(String, File), renderDocument(String, OutputStream), renderString(String), renderString(String, String), renderDocument(File), renderDocument(InputStream), renderDocument(String)

renderString

public java.awt.image.BufferedImage renderString(java.lang.String s,
                                                 java.io.OutputStream out)
                                          throws java.io.IOException,
                                                 LatexRenderException
Render a piece of LaTeX code into a PNG image and output to a stream.

Parameters:
s - LaTeX code (the code goes in the body of a LaTeX document)
out - stream where to output image. The stream will not be closed.
Returns:
the image which was saved in the file
Throws:
java.io.IOException - if there was a problem with the IO
LatexRenderException - if there was a problem while rendering the LaTeX code
See Also:
renderString(String, File), renderString(String, String, File), renderString(String, String, OutputStream), renderDocument(File, File), renderDocument(File, OutputStream), renderDocument(InputStream, File), renderDocument(InputStream, OutputStream), renderDocument(String, File), renderDocument(String, OutputStream), renderString(String), renderString(String, String), renderDocument(File), renderDocument(InputStream), renderDocument(String)

renderString

public java.awt.image.BufferedImage renderString(java.lang.String s,
                                                 java.lang.String header,
                                                 java.io.File f)
                                          throws java.io.IOException,
                                                 LatexRenderException
Render a piece of LaTeX code into a PNG image file.

Parameters:
s - LaTeX code (the code goes in the body of a LaTeX document)
header - LaTeX environment setup, such as usepackage (the code goes before the body of a LaTeX document)
f - file where to save image. If the file exists, its content will be overwritten.
Returns:
the image which was saved in the file
Throws:
java.io.IOException - if there was a problem with the IO
LatexRenderException - if there was a problem while rendering the LaTeX code
See Also:
renderString(String, File), renderString(String, OutputStream), renderString(String, String, OutputStream), renderDocument(File, File), renderDocument(File, OutputStream), renderDocument(InputStream, File), renderDocument(InputStream, OutputStream), renderDocument(String, File), renderDocument(String, OutputStream), renderString(String), renderString(String, String), renderDocument(File), renderDocument(InputStream), renderDocument(String)

renderString

public java.awt.image.BufferedImage renderString(java.lang.String s,
                                                 java.lang.String header,
                                                 java.io.OutputStream out)
                                          throws java.io.IOException,
                                                 LatexRenderException
Render a piece of LaTeX code into a PNG image and output to a stream.

Parameters:
s - LaTeX code (the code goes in the body of a LaTeX document)
header - LaTeX environment setup, such as usepackage (the code goes before the body of a LaTeX document)
out - stream where to output image. The stream will not be closed.
Returns:
the image which was saved in the file
Throws:
java.io.IOException - if there was a problem with the IO
LatexRenderException - if there was a problem while rendering the LaTeX code
See Also:
renderString(String, File), renderString(String, OutputStream), renderString(String, String, File), renderDocument(File, File), renderDocument(File, OutputStream), renderDocument(InputStream, File), renderDocument(InputStream, OutputStream), renderDocument(String, File), renderDocument(String, OutputStream), renderString(String), renderString(String, String), renderDocument(File), renderDocument(InputStream), renderDocument(String)

renderDocument

public java.awt.image.BufferedImage renderDocument(java.lang.String doc,
                                                   java.io.File f)
                                            throws java.io.IOException,
                                                   LatexRenderException
Render the first page of a LaTeX document into a PNG image file.

Parameters:
doc - LaTeX document
f - file where to save image. If the file exists, its content will be overwritten.
Returns:
the image which was saved in the file
Throws:
java.io.IOException - if there was a problem with the IO
LatexRenderException - if there was a problem while rendering the LaTeX code
See Also:
renderString(String, File), renderString(String, OutputStream), renderString(String, String, File), renderString(String, String, OutputStream), renderDocument(File, File), renderDocument(File, OutputStream), renderDocument(InputStream, File), renderDocument(InputStream, OutputStream), renderDocument(String, OutputStream), renderString(String), renderString(String, String), renderDocument(File), renderDocument(InputStream), renderDocument(String)

renderDocument

public java.awt.image.BufferedImage renderDocument(java.lang.String doc,
                                                   java.io.OutputStream out)
                                            throws java.io.IOException,
                                                   LatexRenderException
Render the first page of a LaTeX document into a PNG image and output to a stream.

Parameters:
doc - LaTeX document
out - stream where to output image. The stream will not be closed.
Returns:
the image which was saved in the file
Throws:
java.io.IOException - if there was a problem with the IO
LatexRenderException - if there was a problem while rendering the LaTeX code
See Also:
renderString(String, File), renderString(String, OutputStream), renderString(String, String, File), renderString(String, String, OutputStream), renderDocument(File, File), renderDocument(File, OutputStream), renderDocument(InputStream, File), renderDocument(InputStream, OutputStream), renderDocument(String, File), renderString(String), renderString(String, String), renderDocument(File), renderDocument(InputStream), renderDocument(String)

renderDocument

public java.awt.image.BufferedImage renderDocument(java.io.File doc,
                                                   java.io.File f)
                                            throws java.io.IOException,
                                                   LatexRenderException
Render the first page of a LaTeX document into a PNG image file.

Parameters:
doc - file containing the LaTeX document
f - file where to save image. If the file exists, its content will be overwritten.
Returns:
the image which was saved in the file
Throws:
java.io.IOException - if there was a problem with the IO
LatexRenderException - if there was a problem while rendering the LaTeX code
See Also:
renderString(String, File), renderString(String, OutputStream), renderString(String, String, File), renderString(String, String, OutputStream), renderDocument(File, OutputStream), renderDocument(InputStream, File), renderDocument(InputStream, OutputStream), renderDocument(String, File), renderDocument(String, OutputStream), renderString(String), renderString(String, String), renderDocument(File), renderDocument(InputStream), renderDocument(String)

renderDocument

public java.awt.image.BufferedImage renderDocument(java.io.File doc,
                                                   java.io.OutputStream out)
                                            throws java.io.IOException,
                                                   LatexRenderException
Render the first page of a LaTeX document into a PNG image and output to a stream.

Parameters:
doc - file containing the LaTeX document
out - stream where to output image. The stream will not be closed.
Returns:
the image which was saved in the file
Throws:
java.io.IOException - if there was a problem with the IO
LatexRenderException - if there was a problem while rendering the LaTeX code
See Also:
renderString(String, File), renderString(String, OutputStream), renderString(String, String, File), renderString(String, String, OutputStream), renderDocument(File, File), renderDocument(InputStream, File), renderDocument(InputStream, OutputStream), renderDocument(String, File), renderDocument(String, OutputStream), renderString(String), renderString(String, String), renderDocument(File), renderDocument(InputStream), renderDocument(String)

renderDocument

public java.awt.image.BufferedImage renderDocument(java.io.InputStream doc,
                                                   java.io.File f)
                                            throws java.io.IOException,
                                                   LatexRenderException
Render the first page of a LaTeX document into a PNG image file.

Parameters:
doc - InputStream from which the LaTeX document will be read. The stream is closed after reading the document.
f - file where to save image. If the file exists, its content will be overwritten.
Returns:
the image which was saved in the file
Throws:
java.io.IOException - if there was a problem with the IO
LatexRenderException - if there was a problem while rendering the LaTeX code
See Also:
renderString(String, File), renderString(String, OutputStream), renderString(String, String, File), renderString(String, String, OutputStream), renderDocument(File, File), renderDocument(File, OutputStream), renderDocument(InputStream, OutputStream), renderDocument(String, File), renderDocument(String, OutputStream), renderString(String), renderString(String, String), renderDocument(File), renderDocument(InputStream), renderDocument(String)

renderDocument

public java.awt.image.BufferedImage renderDocument(java.io.InputStream doc,
                                                   java.io.OutputStream out)
                                            throws java.io.IOException,
                                                   LatexRenderException
Render the first page of a LaTeX document into a PNG image and output to a stream.

Parameters:
doc - InputStream from which the LaTeX document will be read. The stream is closed after reading the document.
out - stream where to output image. The stream will not be closed.
Returns:
the image which was saved in the file
Throws:
java.io.IOException - if there was a problem with the IO
LatexRenderException - if there was a problem while rendering the LaTeX code
See Also:
renderString(String, File), renderString(String, OutputStream), renderString(String, String, File), renderString(String, String, OutputStream), renderDocument(File, File), renderDocument(File, OutputStream), renderDocument(InputStream, File), renderDocument(String, File), renderDocument(String, OutputStream), renderString(String), renderString(String, String), renderDocument(File), renderDocument(InputStream), renderDocument(String)

renderString

public java.awt.image.BufferedImage renderString(java.lang.String s)
                                          throws java.io.IOException,
                                                 LatexRenderException
Render a piece of LaTeX code.

Parameters:
s - LaTeX code (the code goes in the body of a LaTeX document)
Returns:
the image with the rendition
Throws:
java.io.IOException - if there was a problem with the IO
LatexRenderException - if there was a problem while rendering the LaTeX code
See Also:
renderString(String, File), renderString(String, OutputStream), renderString(String, String, File), renderString(String, String, OutputStream), renderDocument(File, File), renderDocument(File, OutputStream), renderDocument(InputStream, File), renderDocument(InputStream, OutputStream), renderDocument(String, File), renderDocument(String, OutputStream), renderString(String, String), renderDocument(File), renderDocument(InputStream), renderDocument(String)

renderString

public java.awt.image.BufferedImage renderString(java.lang.String s,
                                                 java.lang.String header)
                                          throws java.io.IOException,
                                                 LatexRenderException
Render a piece of LaTeX code.

Parameters:
s - LaTeX code (the code goes in the body of a LaTeX document)
header - LaTeX environment setup, such as usepackage (the code goes before the body of a LaTeX document)
Returns:
the image with the rendition
Throws:
java.io.IOException - if there was a problem with the IO
LatexRenderException - if there was a problem while rendering the LaTeX code
See Also:
renderString(String, File), renderString(String, OutputStream), renderString(String, String, File), renderString(String, String, OutputStream), renderDocument(File, File), renderDocument(File, OutputStream), renderDocument(InputStream, File), renderDocument(InputStream, OutputStream), renderDocument(String, File), renderDocument(String, OutputStream), renderString(String), renderDocument(File), renderDocument(InputStream), renderDocument(String)

renderDocument

public java.awt.image.BufferedImage renderDocument(java.lang.String doc)
                                            throws java.io.IOException,
                                                   LatexRenderException
Render the first page of a LaTeX document.

Parameters:
doc - LaTeX document
Returns:
the image with the rendition
Throws:
java.io.IOException - if there was a problem with the IO
LatexRenderException - if there was a problem while rendering the LaTeX code
See Also:
renderString(String, File), renderString(String, OutputStream), renderString(String, String, File), renderString(String, String, OutputStream), renderDocument(File, File), renderDocument(File, OutputStream), renderDocument(InputStream, File), renderDocument(InputStream, OutputStream), renderDocument(String, File), renderDocument(String, OutputStream), renderString(String), renderString(String, String), renderDocument(File), renderDocument(InputStream)

renderDocument

public java.awt.image.BufferedImage renderDocument(java.io.File doc)
                                            throws java.io.IOException,
                                                   LatexRenderException
Render the first page of a LaTeX document.

Parameters:
doc - file containing the LaTeX document
Returns:
the image with the rendition
Throws:
java.io.IOException - if there was a problem with the IO
LatexRenderException - if there was a problem while rendering the LaTeX code
See Also:
renderString(String, File), renderString(String, OutputStream), renderString(String, String, File), renderString(String, String, OutputStream), renderDocument(File, File), renderDocument(File, OutputStream), renderDocument(InputStream, File), renderDocument(InputStream, OutputStream), renderDocument(String, File), renderDocument(String, OutputStream), renderString(String), renderString(String, String), renderDocument(InputStream), renderDocument(String)

renderDocument

public java.awt.image.BufferedImage renderDocument(java.io.InputStream doc)
                                            throws java.io.IOException,
                                                   LatexRenderException
Render the first page of a LaTeX document.

Parameters:
doc - InputStream from which the LaTeX document will be read. The stream is closed after reading the document.
Returns:
the image with the rendition
Throws:
java.io.IOException - if there was a problem with the IO
LatexRenderException - if there was a problem while rendering the LaTeX code
See Also:
renderString(String, File), renderString(String, OutputStream), renderString(String, String, File), renderString(String, String, OutputStream), renderDocument(File, File), renderDocument(File, OutputStream), renderDocument(InputStream, File), renderDocument(InputStream, OutputStream), renderDocument(String, File), renderDocument(String, OutputStream), renderString(String), renderString(String, String), renderDocument(File), renderDocument(String)

getEmptyImage

public static java.awt.image.BufferedImage getEmptyImage()
Returns an empty image (1x1 pixel, white).

Returns:
a 1x1 pixel white image

renderTempFile

protected java.awt.image.BufferedImage renderTempFile(java.io.File latexFile)
                                               throws java.io.IOException,
                                                      LatexRenderException
Render the first page of the LaTeX document contained in a temporary file. This method is called by all other rendering methods. The method removes all temporary files (including the file in the parameter) after finishing the rendering.

Parameters:
latexFile - file containing the LaTeX document
Returns:
image with the rendition
Throws:
java.io.IOException - if there was a problem with the IO
LatexRenderException - if there was a problem while rendering the LaTeX code
See Also:
renderString(String, File), renderString(String, OutputStream), renderString(String, String, File), renderString(String, String, OutputStream), renderDocument(File, File), renderDocument(File, OutputStream), renderDocument(InputStream, File), renderDocument(InputStream, OutputStream), renderDocument(String, File), renderDocument(String, OutputStream), renderString(String), renderString(String, String), renderDocument(File), renderDocument(InputStream), renderDocument(String)

latex2PNG

protected java.awt.image.BufferedImage latex2PNG(java.io.File latexFile,
                                                 int dpi)
                                          throws java.io.IOException,
                                                 LatexRenderException
Renders the first page of the LaTeX document contained in a file into an uncropped image.

Parameters:
latexFile - file containing the LaTeX document
dpi - the resolution to be used for the redering (in dots per inch)
Returns:
uncropped image of the first page of the LaTeX document
Throws:
java.io.IOException - if there was a problem with the IO
LatexRenderException - if there was a problem while rendering the LaTeX code
See Also:
renderTempFile(File), tempLatex2EPS(File, File)

latex2EPS

public void latex2EPS(java.lang.String doc,
                      java.io.File target)
               throws java.io.IOException,
                      LatexRenderException
Convert the first page of a LaTeX document into an EPS file.

Parameters:
doc - LaTeX document
target - file where to save the EPS. If the file exists, its content will be overwritten.
Throws:
java.io.IOException - if there was a problem with the IO
LatexRenderException - if there was a problem while rendering the LaTeX code
See Also:
latex2EPS(File, File), latex2EPS(InputStream, File)

latex2EPS

public void latex2EPS(java.io.File doc,
                      java.io.File target)
               throws java.io.IOException,
                      LatexRenderException
Convert the first page of a LaTeX document into an EPS file.

Parameters:
doc - file containing the LaTeX document
target - file where to save the EPS. If the file exists, its content will be overwritten.
Throws:
java.io.IOException - if there was a problem with the IO
LatexRenderException - if there was a problem while rendering the LaTeX code
See Also:
latex2EPS(InputStream, File), latex2EPS(String, File)

latex2EPS

public void latex2EPS(java.io.InputStream doc,
                      java.io.File target)
               throws java.io.IOException,
                      LatexRenderException
Convert the first page of a LaTeX document into an EPS file.

Parameters:
doc - InputStream from which the LaTeX document will be read. The stream is closed after reading the document.
target - file where to save the EPS. If the file exists, its content will be overwritten.
Throws:
java.io.IOException - if there was a problem with the IO
LatexRenderException - if there was a problem while rendering the LaTeX code
See Also:
latex2EPS(File, File), latex2EPS(String, File)

tempLatex2EPS

protected void tempLatex2EPS(java.io.File latexFile,
                             java.io.File target)
                      throws java.io.IOException,
                             LatexRenderException
Convert the first page of a LaTeX document contained in a temporary file into an EPS file. This method is called by all other methods that convert into EPS. The method removes all temporary files (including the file in the parameter) after finishing conversion.

Parameters:
latexFile - file containing the LaTeX document
target - file where to save the EPS. If the file exists, its content will be overwritten.
Throws:
java.io.IOException - if there was a problem with the IO
LatexRenderException - if there was a problem while rendering the LaTeX code
See Also:
latex2EPS(File, File), latex2EPS(InputStream, File), latex2EPS(String, File)

fixBoundingBox

protected void fixBoundingBox(java.awt.Rectangle boundary,
                              java.io.File source,
                              java.io.File target)
                       throws java.io.IOException
Fixes the BoundingBox comment in EPS files so it reflects the true boundary of the graphic. Some tools (notably dvips) may not always generate a correct BoundingBox comment.

Parameters:
boundary - the correct boundary of the graphic
source - EPS file which needs to be fixed. Cannot be the same as the target file.
target - file where the corrected EPS will be written. Cannot be the same as the source file.
Throws:
java.io.IOException - if there was a problem with the IO
See Also:
tempLatex2EPS(File, File)