JAL-3032 adds Java 8 functionality (2/2)
[jalview.git] / src2 / fr / orsay / lri / varna / models / export / PSExport.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
25 import fr.orsay.lri.varna.models.rna.ModeleBP;
26
27 /**
28  * @author ponty
29  * 
30  */
31 public class PSExport extends SecStrDrawingProducer {
32
33         public PSExport()
34         {
35                 super();
36                 super.setScale(0.4);
37         }
38         
39         private String PSMacros() {
40                 String setFontSize =
41                 // Params [fontsize|...]
42                 "/setbasefont \n" + "{ /Helvetica-Bold findfont\n" + // =>
43                                 // [font|scale|...]
44                                 "  exch scalefont\n" + // => [scaled_font|...]
45                                 "  setfont \n" + // => [...]
46                                 "  } def\n\n";
47
48                 String writeTextCentered =
49                 // Params [txt|size|...]
50                 "/txtcenter \n" + "{ dup \n" + // => [txt|txt|size|...]
51                                 "  stringwidth pop\n" + // => [wtxt|txt|size|...]
52                                 "  2 div neg \n" + // => [-wtxt/2|txt|size|...]
53                                 "  3 -1 roll \n" + // => [size|-wtxt/2|txt...]
54                                 "  2 div neg\n" + // => [-size/2|-wtxt/2|txt|...]
55                                 "  rmoveto\n" + // => [txt|...]
56                                 "  show\n" + // => [...]
57                                 "  } def\n\n";
58
59                 String drawEllipse = "/ellipse {\n" + "  /endangle exch def\n"
60                                 + "  /startangle exch def\n" + "  /yrad exch def\n"
61                                 + "  /xrad exch def\n" + "  /y exch def\n" + "  /x exch def\n"
62                                 + "  /savematrix matrix currentmatrix def\n"
63                                 + "  x y translate\n" + "  xrad yrad scale\n"
64                                 + "  0 0 1 startangle endangle arc\n"
65                                 + "  savematrix setmatrix\n" + "  } def\n\n";
66                 return setFontSize + writeTextCentered + drawEllipse;
67         }
68
69         private String EPSHeader(double minX, double maxX, double minY, double maxY) {
70                 String bbox = PSBBox(minX, minY, maxX, maxY);
71                 String init = "%!PS-Adobe-3.0\n" + "%%Pages: 1\n" + bbox
72                                 + "%%EndComments\n" + "%%Page: 1 1\n";
73                 String macros = PSMacros();
74                 return init + macros;
75         }
76
77         private String EPSFooter() {
78                 return "showpage\n" + "%%EndPage: 1\n" + "%%EOF";
79         }
80
81         private String PSNewPath() {
82                 return ("newpath\n");
83         }
84
85         private String PSMoveTo(double x, double y) {
86                 return ("" + x + " " + y + " moveto\n");
87         }
88
89         private String PSLineTo(double dx, double dy) {
90                 return ("" + dx + " " + dy + " lineto\n");
91         }
92
93         private String PSRLineTo(double dx, double dy) {
94                 return ("" + dx + " " + dy + " rlineto\n");
95         }
96
97         private String PSSetLineWidth(double thickness) {
98                 thickness /= 2;
99                 return ("" + thickness + " setlinewidth\n");
100         }
101
102         private String PSStroke() {
103                 return ("stroke\n");
104         }
105
106         private String PSArc(double x, double y, double radiusX, double radiusY,
107                         double angleFrom, double angleTo) {
108
109                 double centerX = x;
110                 double centerY = y;
111
112                 // return (centerX + " " + centerY + " "+ radiusX/2.0+" " + angleFrom +
113                 // " " + angleTo + "  arc\n");
114
115                 return (centerX + " " + centerY + " " + radiusX / 2.0 + " " + radiusY
116                                 / 2.0 + " " + angleTo + " " + angleFrom + " ellipse\n");
117
118         }
119
120         private String PSArc(double x, double y, double radius, double angleFrom,
121                         double angleTo) {
122
123                 return ("" + x + " " + y + " " + radius + " " + angleFrom + " "
124                                 + angleTo + "  arc\n");
125         }
126
127         private String PSBBox(double minX, double maxX, double minY, double maxY) {
128                 String norm = ("%%BoundingBox: " + (long) Math.floor(minX) + " "
129                                 + (long) Math.floor(minY) + " " + (long) Math.ceil(maxX) + " "
130                                 + (long) Math.ceil(maxY) + "\n");
131                 String high = ("%%HighResBoundingBox: " + (long) Math.floor(minX) + " "
132                                 + (long) Math.floor(minY) + " " + (long) Math.ceil(maxX) + " "
133                                 + (long) Math.ceil(maxY) + "\n");
134                 return norm + high;
135         }
136
137         private String PSText(String txt) {
138                 return ("(" + txt + ") ");
139         }
140
141         @SuppressWarnings("unused")
142         private String PSShow() {
143                 return ("show\n");
144         }
145
146         private String PSClosePath() {
147                 return ("closepath\n");
148         }
149
150         private String PSFill() {
151                 return ("fill\n");
152         }
153
154         private String PSSetColor(Color col) {
155                 return ("" + (((double) col.getRed()) / 255.0) + " "
156                                 + (((double) col.getGreen()) / 255.0) + " "
157                                 + (((double) col.getBlue()) / 255.0) + " setrgbcolor\n");
158         }
159
160         private String fontName(int font) {
161                 switch (font) {
162                 case (FONT_TIMES_ROMAN):
163                         return "/Times-Roman";
164                 case (FONT_TIMES_BOLD):
165                         return "/Times-Bold";
166                 case (FONT_TIMES_ITALIC):
167                         return "/Times-Italic";
168                 case (FONT_TIMES_BOLD_ITALIC):
169                         return "/Times-BoldItalic";
170                 case (FONT_HELVETICA):
171                         return "/Helvetica";
172                 case (FONT_HELVETICA_BOLD):
173                         return "/Helvetica-Bold";
174                 case (FONT_HELVETICA_OBLIQUE):
175                         return "/Helvetica-Oblique";
176                 case (FONT_HELVETICA_BOLD_OBLIQUE):
177                         return "/Helvetica-BoldOblique";
178                 case (FONT_COURIER):
179                         return "/Courier";
180                 case (FONT_COURIER_BOLD):
181                         return "/Courier-Bold";
182                 case (FONT_COURIER_OBLIQUE):
183                         return "/Courier-Oblique";
184                 case (FONT_COURIER_BOLD_OBLIQUE):
185                         return "/Courier-BoldOblique";
186                 }
187                 return "/Helvetica";
188         }
189
190         private String PSSetFont(int font, double size) {
191                 return (fontName(font) + " findfont " + size + " scalefont setfont\n");
192         }
193
194         public String setFontS(int font, double size) {
195                 _fontsize = (long) (0.4 * size);
196                 return PSSetFont(font, _fontsize);
197         }
198
199         public String setColorS(Color col) {
200                 super.setColorS(col);
201                 String result = PSSetColor(col);
202                 return result;
203         }
204
205         public String drawLineS(Point2D.Double p0, Point2D.Double p1,
206                         double thickness) {
207                 String tmp = "";
208                 tmp += PSMoveTo(p0.x, p0.y);
209                 tmp += PSLineTo(p1.x, p1.y);
210                 tmp += PSSetLineWidth(thickness);
211                 tmp += PSStroke();
212                 return tmp;
213         }
214
215         public String drawTextS(Point2D.Double p, String txt) {
216                 String tmp = "";
217                 tmp += PSMoveTo(p.x, p.y);
218                 tmp += ("" + (_fontsize / 2.0 + 1) + " \n");
219                 tmp += PSText(txt);
220                 tmp += (" txtcenter\n");
221                 return tmp;
222         }
223
224         public String drawRectangleS(Point2D.Double orig, Point2D.Double dims,
225                         double thickness) {
226                 String tmp = PSNewPath();
227                 tmp += PSMoveTo(orig.x, orig.y);
228                 tmp += PSRLineTo(0, dims.y);
229                 tmp += PSRLineTo(dims.x, 0);
230                 tmp += PSRLineTo(0, -dims.y);
231                 tmp += PSClosePath();
232                 tmp += PSSetLineWidth(thickness);
233                 tmp += PSStroke();
234                 return tmp;
235         }
236
237         public String drawCircleS(Point2D.Double p, double radius, double thickness) {
238                 String tmp = PSNewPath();
239                 tmp += PSArc(p.x, p.y, radius, 0, 360);
240                 tmp += PSSetLineWidth(thickness);
241                 tmp += PSStroke();
242                 return tmp;
243         }
244
245         public String fillCircleS(Point2D.Double p, double radius,
246                         double thickness, Color color) {
247                 String tmp = PSNewPath();
248                 tmp += PSArc(p.x, p.y, radius, 0, 360);
249                 tmp += PSSetLineWidth(thickness);
250                 tmp += PSSetColor(color);
251                 tmp += PSFill();
252                 return tmp;
253         }
254
255         public String footerS() {
256                 return EPSFooter();
257         }
258
259         public String headerS(Rectangle2D.Double bb) {
260                 return EPSHeader(bb.x, bb.y, bb.x + bb.width, bb.y + bb.height);
261         }
262
263         @Override
264         public String drawArcS(Point2D.Double origine, double width, double height,
265                         double startAngle, double endAngle) {
266                 return PSArc(origine.x, origine.y, width, height, startAngle, endAngle)
267                                 + PSStroke();
268         }
269
270         @Override
271         public String drawPolygonS(Double[] points, double thickness) {
272                 String tmp = PSNewPath();
273                 tmp += PSSetLineWidth(thickness);
274                 for (int i = 0; i < points.length; i++) {
275                         if (i == 0) {
276                                 tmp += PSMoveTo(points[i].x, points[i].y);
277                         } else {
278                                 tmp += PSLineTo(points[i].x, points[i].y);
279                         }
280                 }
281                 tmp += PSClosePath();
282                 tmp += PSStroke();
283                 return tmp;
284         }
285
286         @Override
287         public String fillPolygonS(Double[] points, Color color) {
288                 Color bck = _curColor;
289                 String tmp = PSNewPath();
290                 for (int i = 0; i < points.length; i++) {
291                         if (i == 0) {
292                                 tmp += PSMoveTo(points[i].x, points[i].y);
293                         } else {
294                                 tmp += PSLineTo(points[i].x, points[i].y);
295                         }
296                 }
297                 tmp += PSClosePath();
298                 tmp += PSSetColor(color);
299                 tmp += PSFill();
300                 tmp += PSSetColor(bck);
301                 return tmp;
302         }
303
304         @Override
305         public String drawBaseStartS(int index) {
306                 return "";
307         }
308
309         @Override
310         public String drawBaseEndS(int index) {
311                 return "";
312         }
313
314         @Override
315         public String drawBasePairStartS(int i, int j, ModeleBP bps) {
316                 return "";
317         }
318
319         @Override
320         public String drawBasePairEndS(int index) {
321                 return "";
322         }
323
324         @Override
325         public String drawBackboneStartS(int i, int j) {
326                 return "";
327         }
328
329         @Override
330         public String drawBackboneEndS(int index) {
331                 return "";
332         }
333 }