Search in sources :

Example 1 with Triple

use of net.sf.latexdraw.util.Triple in project latexdraw by arnobl.

the class ViewText method createImage.

/**
 * @return The LaTeX compiled picture of the text with its file path and its log.
 */
private Tuple<Image, String> createImage() {
    final Optional<File> optDir = LFileUtils.INSTANCE.createTempDir();
    if (!optDir.isPresent()) {
        return new Tuple<>(null, "A temporary file cannot be created.");
    }
    BufferedImage bi = null;
    // $NON-NLS-1$
    String log = "";
    final File tmpDir = optDir.get();
    final String doc = getLaTeXDocument();
    // $NON-NLS-1$
    final String basePathPic = tmpDir.getAbsolutePath() + LSystem.FILE_SEP + "latexdrawTmpPic" + System.currentTimeMillis();
    final String pathTex = basePathPic + ExportFormat.TEX.getFileExtension();
    final OperatingSystem os = LSystem.INSTANCE.getSystem().orElse(OperatingSystem.LINUX);
    // Saving the LaTeX document into a file to be compiled.
    if (!LFileUtils.INSTANCE.saveFile(pathTex, doc).isPresent()) {
        return new Triple<>(null, basePathPic, log);
    }
    // Compiling the LaTeX document.
    Tuple<Boolean, String> res = execute(new String[] { // $NON-NLS-1$ //$NON-NLS-2$
    os.getLatexBinPath(), // $NON-NLS-1$ //$NON-NLS-2$
    "--halt-on-error", // $NON-NLS-1$ //$NON-NLS-2$
    "--interaction=nonstopmode", "--output-directory=" + tmpDir.getAbsolutePath(), // $NON-NLS-1$
    LFileUtils.INSTANCE.normalizeForLaTeX(pathTex) });
    boolean ok = res.a;
    log = res.b;
    // Compiling the DVI document.
    if (ok) {
        // $NON-NLS-1$ //$NON-NLS-2$
        res = execute(new String[] { os.getDvipsBinPath(), basePathPic + ".dvi", "-o", basePathPic + ExportFormat.EPS_LATEX.getFileExtension() });
        ok = res.a;
        log = log + res.b;
    }
    // Converting the PS document as a PDF one.
    if (ok) {
        res = execute(new String[] { os.getPs2pdfBinPath(), basePathPic + ExportFormat.EPS_LATEX.getFileExtension(), basePathPic + ExportFormat.PDF.getFileExtension() });
        ok = res.a;
        log = log + res.b;
    }
    // Getting the image of the first page of the PDF document.
    if (ok) {
        final String pdfpath = basePathPic + ExportFormat.PDF.getFileExtension();
        final File pdfFile = new File(pdfpath);
        bi = readPDFFirstPage(pdfFile);
    }
    // Converting the image as a JFX one.
    final Image fxImage;
    if (bi == null) {
        fxImage = null;
    } else {
        fxImage = SwingFXUtils.toFXImage(bi, null);
        bi.flush();
    }
    // Deleting the temporary folder and its content.
    LFileUtils.INSTANCE.removeDirWithContent(tmpDir.getPath());
    return new Tuple<>(fxImage, log);
}
Also used : OperatingSystem(net.sf.latexdraw.util.OperatingSystem) Triple(net.sf.latexdraw.util.Triple) BufferedImage(java.awt.image.BufferedImage) Image(javafx.scene.image.Image) RandomAccessFile(java.io.RandomAccessFile) PDFFile(com.sun.pdfview.PDFFile) File(java.io.File) Tuple(net.sf.latexdraw.util.Tuple) BufferedImage(java.awt.image.BufferedImage)

Aggregations

PDFFile (com.sun.pdfview.PDFFile)1 BufferedImage (java.awt.image.BufferedImage)1 File (java.io.File)1 RandomAccessFile (java.io.RandomAccessFile)1 Image (javafx.scene.image.Image)1 OperatingSystem (net.sf.latexdraw.util.OperatingSystem)1 Triple (net.sf.latexdraw.util.Triple)1 Tuple (net.sf.latexdraw.util.Tuple)1