/*
* Title: japlot.jaxodraw
* Description: Graphical user interface for drawing Feynman diagrams
* @author Daniele Binosi
* @author Lukas Theussl
Copyright (C) 2003-2006, Daniele Binosi and Lukas Theussl
See the file LICENSE in the source distribution home directory
for a full copy of the GPL (GNU General Public License).
This file is part of japlot.jaxodraw.
japlot.jaxodraw is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
japlot.jaxodraw is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package japlot.jaxodraw;
import java.awt.Dimension;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.ResourceBundle;
import javax.swing.JOptionPane;
import jhplot.jadraw.JaLatexText;
import jhplot.jadraw.JaObject;
/** The class that is responsible for creating LaTex source files that may
* be used to produce a postscript file that is (should be) equivalent to
* the one produced by japlot.jaxodraw. Not that you need J. Vermaseren's axodraw
* package to process the LaTex source.
*/
public class JaxoExportLatex extends JaxoExport {
private ResourceBundle language =
ResourceBundle.getBundle(JaxoPrefs.getPref(JaxoPrefs.PREF_LANGUAGE));
/** Exports the JaxoGraph newGraph into a LaTex source file:
* asks for an export file name.
* @param newGraph The graph to be exported.
*/
public final void export(JaxoGraph newGraph) {
setGraph(newGraph);
checkforPSText();
String exportFileName = getExportFileName();
if (exportFileName.length() != 0) {
exportLatex(newGraph, exportFileName);
}
}
/** Exports the JaxoGraph graph into a LaTex source file.
* @param graph The graph to be exported.
* @param fileName The export file name.
*/
public final void exportLatex(JaxoGraph graph, String fileName) {
setGraph(graph);
PrintWriter texFile; // Output file for writing LaTex commands
String latexCommand;
String latexColor;
String latexWidth;
String defaultColor = "\\SetColor{Black}";
String defaultWidth = "\\SetWidth{0.5}";
if (!fileName.trim().endsWith(".tex")) {
fileName = fileName.trim() + ".tex";
}
try {
// Create the output file
texFile = new PrintWriter(new FileWriter(fileName));
} catch (IOException e) {
JOptionPane.showMessageDialog(null,
language.getString("IOException:_Can't_open_file_") + fileName
+ "!");
if (JaxoPrefs.verbose()) {
System.err.println(e);
}
return;
}
double[] bBox = graph.getBoundingBox();
int bBoxWidth = (int) (bBox[JaObject.BB_MAX_X]
- bBox[JaObject.BB_MIN_X]);
int bBoxHeight = (int) (bBox[JaObject.BB_MAX_Y]
- bBox[JaObject.BB_MIN_Y]);
Dimension bBoxDim = new Dimension(bBoxWidth, bBoxHeight);
// assume: 1 java point = 1/3 mm
float boxWidth = (float) (bBox[JaObject.BB_MAX_X]
- bBox[JaObject.BB_MIN_X]) / 3.f;
float boxHeight = (float) (bBox[JaObject.BB_MAX_Y]
- bBox[JaObject.BB_MIN_Y]) / 3.f;
float scaleX = boxWidth / (210.f - (2.f * 2.54f));
float scaleY = boxHeight / (297.f - (2.f * 2.54f));
float scaleFactor = scaleX;
if (scaleY > scaleX) {
scaleFactor = scaleY;
}
if (scaleFactor < 1.f) {
scaleFactor = 1.f;
}
writeHeader(texFile);
String comment = graph.getDescription();
if (comment.length() > 0) {
comment = " " + comment;
}
texFile.println("\\begin{document}\n\n");
texFile.println("%%JaxoComment:" + comment);
texFile.println("%%JaxoScale{" + scaleFactor + "}\n\n");
texFile.println("\\begin{center}");
texFile.println("\\fcolorbox{white}{white}{");
texFile.println(" \\begin{picture}("
+ (int) (bBoxWidth / scaleFactor) + ","
+ (int) (bBoxHeight / scaleFactor) + ") ("
+ (int) ((float) bBox[JaObject.BB_MIN_X] / scaleFactor) + ","
+ (int) (((float) -bBox[JaObject.BB_MIN_Y]) / scaleFactor) + ")");
// Code to take into account the possible
// presence of groups in the graph.
// 1) Make a copy of the list to be exported into a temporary list,
// since we are going to ungroup all the groups.
ArrayList templist = graph.copyFrom(graph.getObjectList());
// 2) Ungroup all the groups, preserving the position in the list.
while (areGroupObjects(templist)) {
for (int j = 0; j < templist.size(); j++) {
JaObject ob = (JaObject) templist.get(j);
if (ob instanceof JaxoGroup) {
JaxoGroup tempg = (JaxoGroup) ob;
for (int k = 0; k < tempg.groupObListSize(); k++) {
JaObject tempob =
(JaObject) tempg.getObList().get(k);
templist.add(j + k + 1, tempob);
}
templist.remove(j);
}
}
}
// 3) The list is ready to be converted into a LaTeX file
for (int i = 0; i < templist.size(); i++) {
JaObject ob = (JaObject) templist.get(i);
latexCommand = ob.latexCommand(scaleFactor, bBoxDim);
latexColor = JaxoColor.getLatexColorCommand(ob.getColor());
latexWidth = ob.latexWidth();
// for the first element, we always set color and width
if (i == 0) {
// check that first object is not a Latex text
// because those don't have a width
if (!(ob instanceof JaLatexText)) {
texFile.println(" " + latexWidth);
} else {
texFile.println(" " + defaultWidth);
}
texFile.println(" " + latexColor);
texFile.println(" " + latexCommand);
defaultColor = latexColor;
defaultWidth = latexWidth;
// for the remaining elements, we only set color and width
// if they differ from the previous ones
} else {
if ((latexWidth.compareTo(defaultWidth) != 0)
&& !(ob instanceof JaLatexText)) {
texFile.println(" " + latexWidth);
defaultWidth = latexWidth;
}
if ((latexColor.compareTo(defaultColor) != 0)
&& !(ob instanceof JaLatexText)) {
texFile.println(" " + latexColor);
defaultColor = latexColor;
}
texFile.println(" " + latexCommand);
}
}
texFile.println(" \\end{picture}");
texFile.println("}");
texFile.println("\\end{center}\n\n");
texFile.println("\\end{document}");
texFile.close();
}
/** Determines whether there are group objects present
* in the list of objects.
* @return A boolean parameter
* @param list A list of objects
*/
private boolean areGroupObjects(ArrayList list) {
boolean thereare = false;
for (int i = 0; i < list.size(); i++) {
JaObject ob = (JaObject) list.get(i);
if (ob instanceof JaxoGroup) {
thereare = true;
break;
}
}
return thereare;
}
private void writeHeader(PrintWriter texFile) {
Calendar rightNow = Calendar.getInstance();
String creationDate =
"CreationDate: " + rightNow.get(Calendar.DAY_OF_MONTH) + "/"
+ (rightNow.get(Calendar.MONTH) + 1) + "/"
+ rightNow.get(Calendar.YEAR);
JaxoGraph graph = getGraph();
texFile.println(
"%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%");
texFile.println("%%%\t\tLaTex file generated by " + JaxoDraw.VERSION);
texFile.println("%%%\t\t\t" + creationDate);
texFile.println(
"%%%\tMake sure you have the axodraw package installed in order to proceed!");
texFile.println(
"%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%");
texFile.println("\\documentclass[a4paper]{article}\n");
ArrayList packageList = graph.getPackageList();
for (int j = 0; j < packageList.size(); j++) {
texFile.println("\\usepackage{" + packageList.get(j) + "}");
}
texFile.println("\\setlength{\\oddsidemargin}{0pt}");
texFile.println("\\setlength{\\evensidemargin}{0pt}");
texFile.println("\\setlength{\\topmargin}{0pt}");
texFile.println("\\setlength{\\headheight}{0pt}");
texFile.println("\\setlength{\\headsep}{0pt}");
texFile.println("\\setlength{\\topskip}{0pt}");
texFile.println("\\setlength{\\footskip}{0pt}");
texFile.println("\\setlength{\\textwidth}{\\paperwidth}");
texFile.println("\\addtolength{\\textwidth}{-2in}");
texFile.println("\\setlength{\\textheight}{\\paperheight}");
texFile.println("\\addtolength{\\textheight}{-2in}");
texFile.println("\\pagestyle{empty}\n\n");
}
}
Ads help maintain this website.