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);
}
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);
}
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);
}
Aggregations