JAL-3032 adds Java 8 functionality (2/2)
[jalview.git] / src2 / fr / orsay / lri / varna / models / export / XFIGExport.java
1 /*
2  VARNA is a tool for the automated drawing, visualization and annotation of the secondary structure of RNA, designed as a companion software for web servers and databases.
3  Copyright (C) 2008  Kevin Darty, Alain Denise and Yann Ponty.
4  electronic mail : Yann.Ponty@lri.fr
5  paper mail : LRI, bat 490 Université Paris-Sud 91405 Orsay Cedex France
6
7  This file is part of VARNA version 3.1.
8  VARNA version 3.1 is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License
9  as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
10
11  VARNA version 3.1 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
12  without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13  See the GNU General Public License for more details.
14
15  You should have received a copy of the GNU General Public License along with VARNA version 3.1.
16  If not, see http://www.gnu.org/licenses.
17  */
18 package fr.orsay.lri.varna.models.export;
19
20 import java.awt.Color;
21 import java.awt.geom.Point2D;
22 import java.awt.geom.Rectangle2D;
23 import java.awt.geom.Point2D.Double;
24 import java.util.Hashtable;
25
26 import fr.orsay.lri.varna.models.rna.ModeleBP;
27
28 public class XFIGExport extends SecStrDrawingProducer {
29
30         private int _font = SecStrDrawingProducer.FONT_TIMES_ROMAN;
31         @SuppressWarnings("unused")
32         private StringBuffer buf = new StringBuffer();
33
34         private Hashtable<Color, Integer> _definedCols = new Hashtable<Color, Integer>();
35
36         // From XFig 3.2 file format, indexed RGB colors are in the range [32,543]
37         private int _nextColCode = 32;
38         private final static int UPPER_BOUND_COLOR_CODE = 543;
39
40         public XFIGExport()
41         {
42                 super();
43                 super.setScale(20.0);
44         }
45         
46         
47         private String ensureColorDefinition(Color col) {
48                 if (!_definedCols.containsKey(col)) {
49                         if (_nextColCode < UPPER_BOUND_COLOR_CODE) {
50                                 int curColorCode = _nextColCode;
51                                 _definedCols.put(col, curColorCode);
52                                 _nextColCode++;
53
54                                 String RGBR = Integer.toHexString(col.getRed());
55                                 if (RGBR.length() < 2) {
56                                         RGBR = "0" + RGBR;
57                                 }
58                                 String RGBG = Integer.toHexString(col.getGreen());
59                                 if (RGBG.length() < 2) {
60                                         RGBG = "0" + RGBG;
61                                 }
62                                 String RGBB = Integer.toHexString(col.getBlue());
63                                 if (RGBB.length() < 2) {
64                                         RGBB = "0" + RGBB;
65                                 }
66                                 String RGBHex = "#" + RGBR + RGBG + RGBB;
67                                 RGBHex = RGBHex.toUpperCase();
68                                 return "0 " + curColorCode + " " + RGBHex + "\n";
69                         }
70                 }
71                 return "";
72         }
73
74         private int getColorCode(Color col) {
75                 if (_definedCols.containsKey(col)) {
76                         return _definedCols.get(col);
77                 }
78                 return 0;
79         }
80
81         private int getCurColorCode() {
82                 if (_definedCols.containsKey(_curColor)) {
83                         return _definedCols.get(_curColor);
84                 }
85                 return 0;
86         }
87
88         private String XFIGHeader() {
89                 return "#FIG 3.2\n" + "Landscape\n" + "Center\n" + "Inches\n"
90                                 + "Letter  \n" + "100.00\n" + "Single\n" + "-2\n" + "1200 2\n";
91         }
92
93         public String drawCircleS(Point2D.Double p, double radius, double thickness) {
94                 return ("1 3 0 " + (long) thickness + " " + getCurColorCode()
95                                 + " 7 50 -1 -1 0.000 1 0.0000 " + (long) p.x + " "
96                                 + (long) -p.y + " " + (long) radius + " " + (long) radius + " 1 1 1 1\n");
97         }
98
99         public String drawLineS(Point2D.Double p0, Point2D.Double p1,
100                         double thickness) {
101                 return ("2 1 0 " + (long) thickness + " " + getCurColorCode()
102                                 + " 7 60 -1 -1 0.000 0 0 -1 0 0 2\n" + " " + (long) p0.x + " "
103                                 + (long) -p0.y + " " + (long) p1.x + " " + (long) -p1.y + "\n");
104         }
105
106         public String drawRectangleS(Point2D.Double p, Point2D.Double dims,
107                         double thickness) {
108                 return ("2 2 0 " + (long) thickness + " " + getCurColorCode()
109                                 + " 7 50 -1 -1 0.000 0 0 -1 0 0 5\n" + "\t " + (long) (p.x)
110                                 + " " + (long) (-p.y) + " " + (long) (p.x + dims.x) + " "
111                                 + (long) (-p.y) + " " + (long) (p.x + dims.x) + " "
112                                 + (long) -(p.y + dims.y) + " " + (long) (p.x) + " "
113                                 + (long) -(p.y + dims.y) + " " + (long) (p.x) + " "
114                                 + (long) -(p.y) + "\n");
115         }
116
117         public String drawTextS(Point2D.Double p, String txt) {
118                 return ("4 1 " + getCurColorCode() + " 40 -1 " + _font + " "
119                                 + (long) _fontsize + " 0.0000 6 " + (long) 4 * _fontsize + " "
120                                 + (long) (2 * _fontsize) + " " + (long) (p.x) + " "
121                                 + (long) -(p.y - 6 * _fontsize) + " " + txt + "\\001\n");
122         }
123
124         public String fillCircleS(Point2D.Double p, double radius,
125                         double thickness, Color col) {
126                 String coldef = ensureColorDefinition(col);
127                 return (coldef + "1 3 0 " + (long) thickness + " 0 "
128                                 + getColorCode(col) + " 50 0 20 0.000 1 0.0000 " + (long) p.x
129                                 + " " + (long) -p.y + " " + (long) radius + " " + (long) radius + " 1 1 1 1\n");
130         }
131
132         public String setFontS(int font, double size) {
133                 _font = font;
134                 _fontsize = 1.2 * size;
135                 return "";
136         }
137
138         public String setColorS(Color col) {
139                 super.setColorS(col);
140                 return (ensureColorDefinition(col));
141         }
142
143         public String footerS() {
144                 return "";
145         }
146
147         public String headerS(Rectangle2D.Double bb) {
148                 return XFIGHeader();
149         }
150
151         @Override
152         public String drawArcS(Point2D.Double origine, double width, double height,
153                         double startAngle, double endAngle) {
154                 double p1x = origine.x;
155                 double p1y = -origine.y;
156                 double p2x = origine.x + width / 2.0;
157                 double p2y = -origine.y - height / 2.0;
158                 double p3x = origine.x + width;
159                 double p3y = p1y;
160                 double cx = (p1x + p3x) / 2.0;
161                 double cy = p3y + height / 2.0;
162                 return ("5 1 0 1 " + getCurColorCode() + " 7 50 0 -1 4.000 0 0 0 0 "
163                                 + cx + " " + cy + " " + (int) p1x + " " + (int) p1y + " "
164                                 + (int) p2x + " " + (int) p2y + " " + (int) p3x + " "
165                                 + (int) p3y + "\n");
166
167         }
168
169         @Override
170         public String drawPolygonS(Double[] points, double thickness) {
171                 if (points.length > 0) {
172                         String result = "2 3 0 1 " + getCurColorCode()
173                                         + " 7 40 0 -1 4.000 0 0 0 0 0 " + (points.length + 1)
174                                         + "\n";
175                         for (int i = 0; i < points.length; i++) {
176                                 result += (int) Math.round(points[i].x) + " "
177                                                 + (int) Math.round(-points[i].y) + " ";
178                         }
179                         result += (int) Math.round(points[0].x) + " "
180                                         + (int) Math.round(-points[0].y) + " ";
181                         result += "\n";
182                         return result;
183                 } else {
184                         return "";
185                 }
186         }
187
188         @Override
189         public String fillPolygonS(Double[] points,  Color col) {
190                 if (points.length > 0) {
191                         String coldef = ensureColorDefinition(col);
192                         String result = "2 3 0 1 0 " + getColorCode(col)
193                                         + " 35 0 0 4.000 0 0 0 0 0 " + (points.length + 1) + "\n";
194                         for (int i = 0; i < points.length; i++) {
195                                 result += (int) Math.round(points[i].x) + " "
196                                                 + (int) Math.round(-points[i].y) + " ";
197                         }
198                         result += (int) Math.round(points[0].x) + " "
199                                         + (int) Math.round(-points[0].y) + " ";
200                         result += "\n";
201                         return coldef + result;
202                 } else {
203                         return "";
204                 }
205         }
206
207         @Override
208         public String drawBaseStartS(int index) {
209                 return "";
210         }
211
212         @Override
213         public String drawBaseEndS(int index) {
214                 return "";
215         }
216
217         @Override
218         public String drawBasePairStartS(int i, int j, ModeleBP bps) {
219                 return "";
220         }
221
222         @Override
223         public String drawBasePairEndS(int index) {
224                 return "";
225         }
226
227         @Override
228         public String drawBackboneStartS(int i, int j) {
229                 return "";
230         }
231
232         @Override
233         public String drawBackboneEndS(int index) {
234                 return "";
235         }
236
237 }