support for drawing ship diagrams
[fleet.git] / src / edu / berkeley / fleet / doc / Doc.java
1 package edu.berkeley.fleet.doc;
2
3 import java.io.*;
4 import java.util.*;
5
6
7 public class Doc {
8     
9     public static void print() throws Exception {
10         if (!new File(".tmp").exists())
11             new File(".tmp").mkdirs();
12         PrintWriter pw = new PrintWriter(new FileOutputStream(".tmp/FleetTwo.Manual.tex"));
13         BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("doc/archman.tex")));
14         for(String s = br.readLine(); s!=null; s = br.readLine())
15             pw.println(s);
16         for(String f : new File("ships").list()) {
17             print(pw, new ShipDescription(f, new BufferedReader(new InputStreamReader(new FileInputStream(new File("ships/"+f))))));
18         }
19         pw.println("\\end{document}");
20         pw.close();
21     }
22
23     private static void print(PrintWriter pw, ShipDescription sd) throws Exception {
24         pw.println("\\pagebreak");
25         pw.println("\\section*{The {\\tt "+sd.getName()+"} Ship}");
26         String tex = sd.getSection("tex");
27         /*
28         for(PumpDescription bbd : sd) {
29             pw.println("{\\bf "+(bbd.isInbox() ? "Input: " : "Output: ")+"{\\tt "+bbd.getName()+"}}\n\n");
30         }
31         */
32         int boxGap = 5;
33         int boxHeight = 25;
34         int boxWidth = 75;
35
36         int leftSize = 0;
37         int rightSize = 0;
38         for(PumpDescription bbd : sd)
39             if (bbd.isLeft()) leftSize += (boxHeight+boxGap);
40             else              rightSize += (boxHeight+boxGap);
41
42         int totalHeight = Math.max(leftSize, rightSize);
43         int shipWidth = (int)(boxWidth * 1.5);
44         int totalWidth = boxGap*2 + boxWidth*2 + shipWidth;
45
46         pw.println("");
47         pw.println("\\begin{center}");
48         pw.println("\\begin{empfile}["+sd.getName()+"]");
49         pw.println("\\begin{emp}["+sd.getName()+"]("+(totalWidth+10)+","+(totalHeight+10)+")");
50         pw.println("  beginfig(1)");
51         pw.println("      pickup pencircle scaled 1pt;");
52         pw.println("      draw "+
53                    "("+((totalWidth-shipWidth)/2)+","+0+")--"+
54                    "("+((totalWidth-shipWidth)/2)+","+totalHeight+")--"+
55                    "("+(totalWidth-((totalWidth-shipWidth)/2))+","+totalHeight+")--"+
56                    "("+(totalWidth-((totalWidth-shipWidth)/2))+","+0+")--"+
57                    "("+((totalWidth-shipWidth)/2)+","+0+");");
58         int left = 0;
59         int right = 0;
60         for(PumpDescription bbd : sd) {
61             int ypos = (totalHeight - (boxGap/2) - (bbd.isLeft() ? left : right));
62             int half = (totalWidth-shipWidth)/2;
63             int p1 = bbd.isLeft() ? (half-5) : ((totalWidth-half)+5);
64             int p3 = bbd.isLeft() ? (p1 - boxWidth) : (p1 + boxWidth);
65             if (bbd.isInbox()) {
66                 int p1x = p1;
67                 p1 = p3;
68                 p3 = p1x;
69             }
70             boolean goo = ((bbd.isLeft() && bbd.isInbox()) || (!bbd.isLeft() && bbd.isOutbox()));
71             int p2 = goo ? (p3 - (boxHeight/2)) : (p3 + (boxHeight/2));
72             if (bbd.isLeft()) left += (boxHeight+boxGap);
73             else              right += (boxHeight+boxGap);
74             if (goo) {
75                 pw.println("      label.rt(btex \\tt "+bbd.getName()+" etex, ("+(p1+3)+","+(ypos-boxHeight/2)+"));");
76             } else {
77                 pw.println("      label.lft(btex \\tt "+bbd.getName()+" etex, ("+(p1-3)+","+(ypos-boxHeight/2)+"));");
78             }
79             pw.println("      draw "+
80                        "  ("+p1+","+ypos+")--"+
81                        "  ("+p2+","+ypos+")--"+
82                        "  ("+p3+","+(ypos-(boxHeight/2))+")--"+
83                        "  ("+p2+","+(ypos-boxHeight)+")--"+
84                        "  ("+p1+","+(ypos-boxHeight)+")--"+
85                        "  ("+p1+","+ypos+");");
86             if (bbd.isLeft()) leftSize += boxHeight;
87             else              rightSize += boxHeight;
88         }
89         pw.println("  endfig;");
90         pw.println("\\end{emp}");
91         pw.println("\\end{empfile}");
92         pw.println("\\end{center}");
93         pw.println("");
94
95         if (tex!=null)
96             pw.println(tex);
97     }
98
99 }