Search in sources :

Example 1 with Tuple

use of net.sf.latexdraw.util.Tuple 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)

Example 2 with Tuple

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

the class PSTLatexdrawListener method getRectangularPoints.

private Tuple<IPoint, IPoint> getRectangularPoints(final net.sf.latexdraw.parsers.pst.PSTParser.CoordContext c1, final net.sf.latexdraw.parsers.pst.PSTParser.CoordContext c2, final PSTContext ctx) {
    final IPoint pt1;
    final IPoint pt2;
    if (c2 == null) {
        pt1 = ShapeFactory.INST.createPoint(ctx.originToPoint());
        pt2 = ShapeFactory.INST.createPoint(ctx.coordToAdjustedPoint(c1));
    } else {
        pt1 = ShapeFactory.INST.createPoint(ctx.coordToAdjustedPoint(c1));
        pt2 = ShapeFactory.INST.createPoint(ctx.coordToAdjustedPoint(c2));
    }
    return new Tuple<>(pt1, pt2);
}
Also used : IPoint(net.sf.latexdraw.models.interfaces.shape.IPoint) Tuple(net.sf.latexdraw.util.Tuple)

Example 3 with Tuple

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

the class ViewText method execute.

/**
 * Executes a given command and returns the log.
 * @param cmd The command to execute.
 * @return True if the command exits normally plus the log.
 */
private Tuple<Boolean, String> execute(final String[] cmd) {
    String log = "";
    try {
        final Process process = Runtime.getRuntime().exec(cmd);
        final StreamExecReader errReader = new StreamExecReader(process.getErrorStream());
        final StreamExecReader outReader = new StreamExecReader(process.getInputStream());
        errReader.start();
        outReader.start();
        if (process.waitFor() == 0) {
            return new Tuple<>(true, log);
        }
        log = outReader.getLog() + LSystem.EOL + errReader.getLog();
    } catch (final IOException | InterruptedException | IllegalThreadStateException ex) {
        log += ex.getMessage();
    }
    return new Tuple<>(false, log);
}
Also used : StreamExecReader(net.sf.latexdraw.util.StreamExecReader) IOException(java.io.IOException) Tuple(net.sf.latexdraw.util.Tuple)

Aggregations

Tuple (net.sf.latexdraw.util.Tuple)3 PDFFile (com.sun.pdfview.PDFFile)1 BufferedImage (java.awt.image.BufferedImage)1 File (java.io.File)1 IOException (java.io.IOException)1 RandomAccessFile (java.io.RandomAccessFile)1 Image (javafx.scene.image.Image)1 IPoint (net.sf.latexdraw.models.interfaces.shape.IPoint)1 OperatingSystem (net.sf.latexdraw.util.OperatingSystem)1 StreamExecReader (net.sf.latexdraw.util.StreamExecReader)1 Triple (net.sf.latexdraw.util.Triple)1