24
23
import javax.imageio.plugins.bmp.BMPImageWriteParam;
25
24
import javax.imageio.plugins.jpeg.JPEGImageWriteParam;
26
25
import javax.imageio.stream.ImageOutputStream;
27
import javax.swing.JComponent;
28
import javax.swing.JViewport;
29
import javax.swing.Scrollable;
30
import javax.swing.SwingConstants;
32
28
import latexDraw.figures.*;
33
29
import latexDraw.figures.properties.Arrowable;
30
import latexDraw.filters.PSFilter;
31
import latexDraw.filters.TeXFilter;
34
32
import latexDraw.parsers.svg.SVGAttributes;
35
33
import latexDraw.parsers.svg.SVGDocument;
36
34
import latexDraw.parsers.svg.SVGElements;
977
public String getLatexDocument()
979
String packages = getParentPanel().getCodePanel().getDocumentPackages();
980
String eol = System.getProperty("line.separator");//$NON-NLS-1$
981
StringBuffer doc = new StringBuffer();
982
parentPanel.updateCode();
983
String code = parentPanel.getCodePanel().getEditor().getCode(false, false);
984
LaTeXDrawPoint2D se = bordersComplete.getTheSEPoint();
989
doc.append("\\documentclass{article}").append(eol).append("\\pagestyle{empty}").append(eol).append(packages).append(eol).append(
990
"\\usepackage[left=0cm,top=0.1cm,right=0cm,nohead,nofoot,paperwidth=").append(
991
se.x/pixelsPerCm).append("cm,paperheight=").append(
992
se.y/pixelsPerCm+0.3).append("cm]{geometry}").append(
993
eol).append("\\usepackage[usenames,dvipsnames]{pstricks}").append(//$NON-NLS-1$//$NON-NLS-2$
994
eol).append("\\usepackage{pstricks-add}").append(eol).append("\\usepackage{epsfig}").append(//$NON-NLS-1$//$NON-NLS-2$
995
eol).append("\\usepackage{pst-grad}").append(eol).append("\\usepackage{pst-plot}").append(eol).append(//$NON-NLS-1$//$NON-NLS-2$
996
"\\begin{document}").append(eol).append(
997
"\\addtolength{\\oddsidemargin}{-0.2in}").append(eol).append("\\addtolength{\\evensidemargin}{-0.2in}").append(
998
eol).append(code).append(eol).append("\\end{document}");//$NON-NLS-1$//$NON-NLS-2$
1000
return doc.toString();
1005
public File createLatexFile(String pathExportTex)
1007
if(pathExportTex==null)
1011
String doc = getLatexDocument();
1012
FileOutputStream fos = new FileOutputStream(pathExportTex);
1013
OutputStreamWriter osw = new OutputStreamWriter(fos);
1021
return new File(pathExportTex);
1023
catch(Exception e) { return null; }
1027
private void showErrorDialogLatexCompilation(String log) {
1028
String text = log==null || log.length()==0 ? "Check the path of your latex distribution in your preferences." : log;
1029
ExceptionFrameDialog dialog = new ExceptionFrameDialog(new IllegalAccessException(text));
1031
dialog.setTitle("Document not created");
1032
dialog.setInfoLabel("The document cannot be created.");
1033
dialog.getEditor().setText(text);
1034
dialog.getEditor().setCaretPosition(0);
1035
dialog.setVisible(true);
1040
* Create a .ps file that corresponds to the compiled latex document containing
1041
* the pstricks drawing.
1042
* @param latexDistribPath The path locating latex.
1043
* @param pathExportPs The path of the .ps file to create (MUST ends with .ps).
1044
* @return The create file or null.
1045
* @throws IllegalArgumentException If a problem occurs.
1048
public File createPSFile(String latexDistribPath, String pathExportPs)
1050
if(latexDistribPath==null)
1053
String dirBin = parentPanel.getFrameParent().getPathDistribLatex();
1054
String pathTmp = System.getProperty("java.io.tmpdir"); //$NON-NLS-1$
1055
String sep = System.getProperty("file.separator"); //$NON-NLS-1$
1056
String path = pathTmp + sep + "latexdrawTmp" + System.currentTimeMillis(); //$NON-NLS-1$
1057
File texFile = createLatexFile(path + TeXFilter.TEX_EXTENSION);
1060
LaTeXDrawPoint2D nw = bordersComplete.getTheNWPoint(), se = bordersComplete.getTheSEPoint();
1063
if(texFile==null || !texFile.exists())
1066
dirBin = dirBin==null ? "" : dirBin.endsWith(sep) || dirBin.length()==0 ? dirBin : dirBin + sep;
1067
log = execute(dirBin + "latex --interaction=nonstopmode --output-directory=" + pathTmp + " " + texFile.getAbsolutePath());//$NON-NLS-1$//$NON-NLS-2$
1068
log += execute(dirBin + "dvips -T " + ((se.x-nw.x)/pixelsPerCm+dec) + "cm," + ((se.y-nw.y)/pixelsPerCm+dec) + "cm " + path + ".dvi -o " + pathExportPs); //$NON-NLS-1$//$NON-NLS-2$
1071
new File(path + ".dvi").delete(); //$NON-NLS-1$
1072
new File(path + ".log").delete(); //$NON-NLS-1$
1073
new File(path + ".aux").delete(); //$NON-NLS-1$
1075
finalPS = new File(pathExportPs);
1077
if(!finalPS.exists()) {
1078
showErrorDialogLatexCompilation(log);
1088
* Create a .pdf file that corresponds to the compiled latex document containing
1089
* the pstricks drawing.
1090
* @param latexDistribPath The path locating latex.
1091
* @param pathExportPdf The path of the .pdf file to create (MUST ends with .pdf).
1092
* @return The create file or null.
1093
* @throws IllegalArgumentException If a problem occurs.
1096
public File createPDFFile(String latexDistribPath, String pathExportPdf)
1098
if(pathExportPdf==null)
1101
String dirBin = parentPanel.getFrameParent().getPathDistribLatex();
1102
String pathTmp = System.getProperty("java.io.tmpdir"); //$NON-NLS-1$
1103
String sep = System.getProperty("file.separator"); //$NON-NLS-1$
1104
String path = pathTmp + sep + "latexdrawTmp" + System.currentTimeMillis()*2;//$NON-NLS-1$
1105
File psFile = createPSFile(latexDistribPath, path + PSFilter.PS_EXTENSION);
1112
dirBin = dirBin==null ? "" : dirBin.endsWith(sep) || dirBin.length()==0 ? dirBin : dirBin + sep;
1113
log = execute(dirBin + "ps2pdf " + psFile.getAbsolutePath() + " " + pathExportPdf); //$NON-NLS-1$//$NON-NLS-2$
1114
pdfFile = new File(pathExportPdf);
1117
if(!pdfFile.exists()) {
1118
showErrorDialogLatexCompilation(log);
978
1128
* Allows to export the drawing as a eps file
979
1129
* @param file The new eps file