Search in sources :

Example 1 with OperatingSystem

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

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

the class LaTeXGenerator method createEPSFile.

/**
 * Create an .eps file that corresponds to the compiled latex document containing the pstricks drawing.
 * @param pathExportEPS The path of the .eps file to create (MUST ends with .eps).
 * @return The create file or nothing.
 * @throws SecurityException In case of problem while accessing files.
 */
public Optional<File> createEPSFile(final String pathExportEPS) {
    final Optional<File> optDir = LFileUtils.INSTANCE.createTempDir();
    if (!optDir.isPresent()) {
        BadaboomCollector.INSTANCE.add(new FileNotFoundException("Cannot create a tmp dir"));
        return Optional.empty();
    }
    final File tmpDir = optDir.get();
    // $NON-NLS-1$
    final Optional<File> optFile = createPSFile(tmpDir.getAbsolutePath() + LSystem.FILE_SEP + "tmpPSFile.ps", tmpDir);
    File psFile;
    if (optFile.isPresent()) {
        psFile = optFile.get();
    } else {
        return Optional.empty();
    }
    final OperatingSystem os = LSystem.INSTANCE.getSystem().orElse(OperatingSystem.LINUX);
    final File finalFile = new File(pathExportEPS);
    // $NON-NLS-1$
    final File fileEPS = new File(psFile.getAbsolutePath().replace(".ps", ExportFormat.EPS_LATEX.getFileExtension()));
    final String[] paramsLatex = { os.getPS2EPSBinPath(), psFile.getAbsolutePath(), fileEPS.getAbsolutePath() };
    final String log = LSystem.INSTANCE.execute(paramsLatex, tmpDir);
    if (!fileEPS.exists()) {
        BadaboomCollector.INSTANCE.add(new IllegalAccessException(getDocumentCode() + LSystem.EOL + log));
        return Optional.empty();
    }
    try {
        Files.copy(fileEPS.toPath(), finalFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
    } catch (final IOException ex) {
        BadaboomCollector.INSTANCE.add(ex);
        return Optional.empty();
    }
    LFileUtils.INSTANCE.removeDirWithContent(tmpDir.getPath());
    return Optional.of(finalFile);
}
Also used : OperatingSystem(net.sf.latexdraw.util.OperatingSystem) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) File(java.io.File)

Example 3 with OperatingSystem

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

the class LaTeXGenerator method createPDFFile.

/**
 * Create a .pdf file that corresponds to the compiled latex document containing
 * the pstricks drawing.
 * @param pathExportPdf The path of the .pdf file to create (MUST ends with .pdf).
 * @param crop if true, the output document will be cropped.
 * @return The create file or null.
 * @throws SecurityException In case of problem while accessing files.
 */
public Optional<File> createPDFFile(final String pathExportPdf, final boolean crop) {
    if (pathExportPdf == null)
        return Optional.empty();
    final Optional<File> optDir = LFileUtils.INSTANCE.createTempDir();
    if (!optDir.isPresent()) {
        // $NON-NLS-1$
        BadaboomCollector.INSTANCE.add(new FileNotFoundException("Cannot create a temporary folder."));
        return Optional.empty();
    }
    final File tmpDir = optDir.get();
    final String name = pathExportPdf.substring(pathExportPdf.lastIndexOf(LSystem.FILE_SEP) + 1, pathExportPdf.lastIndexOf(ExportFormat.PDF.getFileExtension()));
    final File psFile;
    // $NON-NLS-1$
    Optional<File> optFile = createPSFile(tmpDir.getAbsolutePath() + LSystem.FILE_SEP + name + ".ps");
    if (optFile.isPresent()) {
        psFile = optFile.get();
    } else {
        return Optional.empty();
    }
    String log;
    File pdfFile;
    final OperatingSystem os = LSystem.INSTANCE.getSystem().orElse(OperatingSystem.LINUX);
    // On windows, an option must be defined using this format:
    // -optionName#valueOption Thus, the classical = character must be replaced by a # when latexdraw runs on Windows.
    // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
    final String optionEmbed = "-dEmbedAllFonts" + (LSystem.INSTANCE.isWindows() ? "#" : "=") + "true";
    log = LSystem.INSTANCE.execute(new String[] { os.getPs2pdfBinPath(), optionEmbed, psFile.getAbsolutePath(), crop ? name + ExportFormat.PDF.getFileExtension() : pathExportPdf }, tmpDir);
    if (crop) {
        pdfFile = new File(tmpDir.getAbsolutePath() + LSystem.FILE_SEP + name + ExportFormat.PDF.getFileExtension());
        log = LSystem.INSTANCE.execute(new String[] { os.getPdfcropBinPath(), pdfFile.getAbsolutePath(), pdfFile.getAbsolutePath() }, tmpDir);
        try {
            Files.move(pdfFile.toPath(), Paths.get(pathExportPdf), StandardCopyOption.REPLACE_EXISTING);
        } catch (final IOException ex) {
            BadaboomCollector.INSTANCE.add(ex);
            // $NON-NLS-1$
            log += " The final pdf document cannot be moved to its final destination. If you use Windows, you must have a Perl interpretor installed, such as strawberryPerl (http://strawberryperl.com/)";
        }
    }
    pdfFile = new File(pathExportPdf);
    if (!pdfFile.exists()) {
        BadaboomCollector.INSTANCE.add(new IllegalAccessException(getDocumentCode() + LSystem.EOL + log));
        pdfFile = null;
    }
    LFileUtils.INSTANCE.removeDirWithContent(tmpDir.getPath());
    return Optional.ofNullable(pdfFile);
}
Also used : OperatingSystem(net.sf.latexdraw.util.OperatingSystem) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) File(java.io.File)

Example 4 with OperatingSystem

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

the class LaTeXGenerator method createPSFile.

/**
 * Create a .ps file that corresponds to the compiled latex document containing
 * the pstricks drawing.
 * @param pathExportPs The path of the .ps file to create (MUST ends with .ps).
 * @param tmpDir The temporary directory used for the compilation.
 * @return The create file or nothing.
 */
private Optional<File> createPSFile(final String pathExportPs, final File tmpDir) {
    if (pathExportPs == null)
        return Optional.empty();
    final int lastSep = pathExportPs.lastIndexOf(LSystem.FILE_SEP) + 1;
    // $NON-NLS-1$
    final String name = pathExportPs.substring(lastSep == -1 ? 0 : lastSep, pathExportPs.lastIndexOf(".ps"));
    final File tmpDir2 = tmpDir == null ? LFileUtils.INSTANCE.createTempDir().orElse(null) : tmpDir;
    if (tmpDir2 == null) {
        // $NON-NLS-1$
        BadaboomCollector.INSTANCE.add(new FileNotFoundException("Cannot create a temporary folder."));
        return Optional.empty();
    }
    final String path = tmpDir2.getAbsolutePath() + LSystem.FILE_SEP;
    Optional<File> optFile = LFileUtils.INSTANCE.saveFile(path + name + ExportFormat.TEX.getFileExtension(), getDocumentCode());
    File texFile;
    if (optFile.isPresent()) {
        texFile = optFile.get();
    } else {
        return Optional.empty();
    }
    String log;
    File finalPS;
    final IPoint tr = handler.getTopRightDrawingPoint();
    final IPoint bl = handler.getBottomLeftDrawingPoint();
    final int ppc = handler.getPPCDrawing();
    final float dec = 0.2f;
    final OperatingSystem os = LSystem.INSTANCE.getSystem().orElse(OperatingSystem.LINUX);
    if (!texFile.exists())
        return Optional.empty();
    final String[] paramsLatex = { // $NON-NLS-1$//$NON-NLS-2$
    os.getLatexBinPath(), // $NON-NLS-1$//$NON-NLS-2$
    "--interaction=nonstopmode", // $NON-NLS-1$//$NON-NLS-2$
    "--output-directory=" + tmpDir2.getAbsolutePath(), // $NON-NLS-1$
    LFileUtils.INSTANCE.normalizeForLaTeX(texFile.getAbsolutePath()) };
    log = LSystem.INSTANCE.execute(paramsLatex, tmpDir2);
    final String[] paramsDvi = { // $NON-NLS-1$ //$NON-NLS-2$
    os.getDvipsBinPath(), // $NON-NLS-1$ //$NON-NLS-2$
    "-Pdownload35", // $NON-NLS-1$ //$NON-NLS-2$
    "-T", // $NON-NLS-1$ //$NON-NLS-2$
    (tr.getX() - bl.getX()) / ppc * scale + dec + "cm," + ((bl.getY() - tr.getY()) / ppc * scale + dec) + "cm", name, "-o", // $NON-NLS-1$
    pathExportPs };
    log += LSystem.INSTANCE.execute(paramsDvi, tmpDir2);
    finalPS = new File(pathExportPs);
    if (!finalPS.exists()) {
        BadaboomCollector.INSTANCE.add(new IllegalAccessException(getDocumentCode() + LSystem.EOL + log));
        finalPS = null;
    }
    if (tmpDir == null) {
        LFileUtils.INSTANCE.removeDirWithContent(tmpDir2.getPath());
    }
    return Optional.ofNullable(finalPS);
}
Also used : OperatingSystem(net.sf.latexdraw.util.OperatingSystem) FileNotFoundException(java.io.FileNotFoundException) IPoint(net.sf.latexdraw.models.interfaces.shape.IPoint) File(java.io.File) IPoint(net.sf.latexdraw.models.interfaces.shape.IPoint)

Aggregations

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