001    // GraphLab Project: http://graphlab.sharif.edu
002    // Copyright (C) 2008 Mathematical Science Department of Sharif University of Technology
003    // Distributed under the terms of the GNU General Public License (GPL): http://www.gnu.org/licenses/
004    
005    package graphlab.extensions.io;
006    
007    import graphlab.graph.graph.EdgeModel;
008    import graphlab.graph.graph.GraphModel;
009    import graphlab.graph.graph.GraphPoint;
010    import graphlab.graph.graph.VertexModel;
011    import graphlab.platform.parameter.Parametrizable;
012    import graphlab.plugins.main.saveload.core.GraphIOException;
013    import graphlab.plugins.main.saveload.core.extension.GraphWriterExtension;
014    
015    import java.awt.geom.Rectangle2D;
016    import java.io.File;
017    import java.io.FileWriter;
018    import java.io.IOException;
019    import java.util.Iterator;
020    
021    /**
022     * @author  Mohammad Ali Rostami
023     * @email ma.rostami@yahoo.com
024     */
025    public class LatexWriter implements GraphWriterExtension, Parametrizable {
026    
027        public String getName() {
028            return "Latex";
029        }
030    
031        public String getExtension() {
032            return "tex";
033        }
034    
035    
036        public void write(File file, GraphModel graph) throws GraphIOException {
037            FileWriter output = null;
038            try {
039                output = new FileWriter(file);
040                Rectangle2D r = graph.getAbsBounds();
041                output.write(
042                        "\\documentclass[12pt,bezier]{article}\n" +
043                                "\\textwidth = 15 cm\n" +
044                                "\\textheight = 21.2 cm\n" +
045                                "\\oddsidemargin = 0 cm\n" +
046                                "\\evensidemargin = 0 cm\n" +
047                                "\\topmargin = -1 cm\n" +
048                                "\\parskip = 1.5 mm\n" +
049                                "\\parindent = 5 mm\n" +
050                                "%\n" +
051                                "\\def\\bfG{\\mbox{\\boldmath$G$}}\n" +
052                                "\n" +
053                                "\\title{" + graph.getLabel() + "}\n" +
054                                "\\input{epsf}\n" +
055                                "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n" +
056                                "\\pagestyle{plain}\n" +
057                                "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n" +
058                                "\\def\\emline#1#2#3#4#5#6{\\put(#4,#5){\\special{em:lineto}}}\n" +
059                                "\\def\\newpic#1{}\n" +
060                                "%\n" +
061                                "\n" +
062                                "\\author{GraphLab}\n" +
063                                "%\n" +
064                                "\\date{}\n" +
065                                "\n" +
066                                "\\begin{document}\n" +
067                                "\n" +
068                                "\\begin{figure}[h]\n" +
069                                "%\n" +
070                                "\\def\\emline#1#2#3#4#5#6{%\n" +
071                                "%\n" +
072                                "\\put(#1,#2){\\special{em:moveto}}%\n" +
073                                "%\n" +
074                                "\\put(#4,#5){\\special{em:lineto}}}\n" +
075                                "%\n" +
076                                "\\def\\newpic#1{}\n" +
077                                "%\n" +
078                                "%\\pagestyle{empty}\n" +
079                                "%\n" +
080                                "%\\begin{document}\n" +
081                                "%\n" +
082                                "\\unitlength 0.7mm\n" +
083                                "%\n" +
084                                "\\special{em:linewidth 0.4pt}\n" +
085                                "%\n" +
086                                "\\linethickness{0.4pt}\n" +
087                                "%\n" +
088                                "\\begin{picture}(150,150)(0,0)\n" +
089                                "%\n" +
090                                "%Vertices\n");
091    
092                String vertices = " ";
093                for (VertexModel vm : graph)
094                    vertices += "\\put("
095                            + (vm.getLocation().getX() / r.getMaxX()) * 100
096                            + ","
097                            + (vm.getLocation().getY() / r.getMaxY()) * 100
098                            + "){\\circle*{2}}\n";
099                output.write(vertices);
100    
101                String edges = "";
102                Iterator<EdgeModel> em = graph.edgeIterator();
103                while (em.hasNext()) {
104                    EdgeModel e = em.next();
105                    final GraphPoint sx = e.source.getLocation();
106                    if (!graph.isEdgesCurved()) {
107                        edges += "%Edge Label:" + e.getLabel() + "\n";
108                        edges += "\\emline{" +
109                                (sx.getX() / r.getMaxX()) * 100
110                                + "}{" +
111                                (sx.getY() / r.getMaxY()) * 100
112                                + "}{1}{" +
113    
114                                (e.target.getLocation().getX() / r.getMaxX()) * 100
115                                + "}{" +
116                                (e.target.getLocation().getY() / r.getMaxY()) * 100
117                                + "}{2}\n";
118                    } else {
119                        edges += "%Edge Label:" + e.getLabel() + "\n";
120                        double centerx, centery;
121                        centerx = (sx.getX() + e.target.getLocation().getX()) / 2;
122                        centery = (sx.getY() + e.target.getLocation().getY()) / 2;
123                        double cx = ((centerx + e.getCurveControlPoint().getX()) / r.getMaxX()) * 100;
124                        double cy = ((e.getCurveControlPoint().getY() + centery) / r.getMaxY()) * 100;
125                        edges += "\\bezier{500}(" +
126                                (sx.getX() / r.getMaxX()) * 100
127                                + "," +
128                                (sx.getY() / r.getMaxY()) * 100
129                                + ")(" +
130                                +cx
131                                + "," +
132                                cy
133                                + ")(" +
134                                (e.target.getLocation().getX() / r.getMaxX()) * 100
135                                + "," +
136                                (e.target.getLocation().getY() / r.getMaxY()) * 100
137                                + ")\n";
138                    }
139                }
140                output.write(edges);
141                output.write("\n" +
142                        "\\end{picture}\n" +
143                        "\\end{figure}\n" +
144                        "\\end{document}\n"
145                );
146    
147                output.flush();
148    
149            } catch (IOException e) {
150                e.printStackTrace();
151            }
152        }
153    
154        public String getDescription() {
155            return "exports latex";
156        }
157    
158        public String checkParameters() {
159            return null;
160        }
161    }