JAL-3026 srcjar files for VARNA and log4j
[jalview.git] / srcjar / fr / orsay / lri / varna / models / export / SVGExport.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 public class SVGExport extends SecStrDrawingProducer {
28
29         private double _fontsize = 10.0;
30         private Rectangle2D.Double _bb = new Rectangle2D.Double(0, 0, 10, 10);
31         double _thickness = 2.0;
32         
33         
34         public SVGExport()
35         {
36                 super();
37                 super.setScale(0.5);
38         }
39
40         private String getRGBString(Color col) {
41                 int rpc = (int) ((((double) col.getRed()) / 255.0) * 100);
42                 int gpc = (int) ((((double) col.getGreen()) / 255.0) * 100);
43                 int bpc = (int) ((((double) col.getBlue()) / 255.0) * 100);
44                 return "rgb(" + rpc + "%, " + gpc + "%, " + bpc + "%)";
45         }
46
47         public String drawCircleS(Point2D.Double base, double radius,
48                         double thickness) {
49                 _thickness = thickness;
50                 return "<circle cx=\"" + base.x + "\" cy=\"" + (_bb.height - base.y)
51                                 + "\" r=\"" + radius + "\" stroke=\"" + getRGBString(_curColor)
52                                 + "\" stroke-width=\"" + thickness + "\" fill=\"none\"/>\n";
53         }
54
55         public String drawLineS(Point2D.Double orig, Point2D.Double dest,
56                         double thickness) {
57                 _thickness = thickness;
58                 return "<line x1=\"" + orig.x + "\" y1=\"" + (_bb.height - orig.y)
59                                 + "\" x2=\"" + dest.x + "\" y2=\"" + (_bb.height - dest.y)
60                                 + "\" stroke=\"" + getRGBString(_curColor)
61                                 + "\" stroke-width=\"" + thickness + "\" />\n";
62         }
63
64         public String drawRectangleS(Point2D.Double orig, Point2D.Double dims,
65                         double thickness) {
66                 _thickness = thickness;
67                 return "<rect x=\""+ orig.x+"\" y=\""+(_bb.height - orig.y - dims.y)+"\" width=\""+dims.x
68                   +"\" height=\""+dims.y+
69                   "\" fill=\"none\" style=\"stroke-width:"+thickness+";stroke:"+getRGBString(_curColor)+"\"/>";
70         }
71
72         public String drawTextS(Point2D.Double base, String txt) {
73                 //System.out.println(txt);
74                 return "<text x=\""
75                                 + (base.x)
76                                 + "\" y=\""
77                                 + (_bb.height - base.y + 0.4 * _fontsize)
78                                 + "\" text-anchor=\"middle\" font-family=\"Verdana\" font-size=\""
79                                 + _fontsize + "\" fill=\"" + getRGBString(_curColor) + "\" >"
80                                 + txt + "</text>\n";
81         }
82
83         public String fillCircleS(Point2D.Double base, double radius,
84                         double thickness, Color col) {
85                 _thickness = thickness;
86
87                 return "<circle cx=\"" + base.x + "\" cy=\"" + (_bb.height - base.y)
88                                 + "\" r=\"" + radius + "\" stroke=\"none\" stroke-width=\""
89                                 + thickness + "\" fill=\"" + getRGBString(col) + "\"/>\n";
90         }
91
92         public String footerS() {
93                 return "</svg>\n";
94         }
95
96         public String headerS(Rectangle2D.Double bb) {
97                 _bb = bb;
98                 return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
99                                 + "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" \n"
100                                 + "\"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n"
101                                 + "\n"
102                                 + "<svg width=\"100%\" height=\"100%\" version=\"1.1\"\n"
103                                 + "xmlns=\"http://www.w3.org/2000/svg\">\n";
104         }
105
106         public String setFontS(int font, double size) {
107                 _fontsize = 0.5 * size;
108                 return "";
109         }
110
111         
112         private Point2D.Double polarToCartesian(Point2D.Double center, double radiusX, double radiusY, double angleInDegrees) {
113                   double angleInRadians = (angleInDegrees) * Math.PI / 180.0;
114
115                   return new Point2D.Double(center.x + (radiusX * Math.cos(angleInRadians)),
116                                   _bb.height - (center.y + (radiusY * Math.sin(angleInRadians))));
117                 }
118
119         
120         public String drawArcS(Point2D.Double o, double width, double height,
121                         double startAngle, double endAngle) {
122                 double rx = width / 2.0;
123                 double ry = height / 2.0;
124                 
125                 Point2D.Double ps = polarToCartesian(o,rx,ry,startAngle);
126                 Point2D.Double pe = polarToCartesian(o,rx,ry,endAngle);
127                 
128                 String d = "<path d=\"M " + ps.x + "," +  ps.y + " A " + rx + "," + ry
129                                 + " 0 1,1 " +  pe.x + "," + pe.y + "\" style=\"fill:none; stroke:"
130                                 + getRGBString(_curColor) + "; stroke-width:" + _thickness
131                                 + "\"/>\n";             
132                 return d;
133         }
134
135         public String drawPolygonS(Double[] points, double thickness) {
136                 String result = "<path d=\"";
137                 for (int i = 0; i < points.length; i++) {
138                         if (i == 0) {
139                                 result += "M " + points[i].x + " " + (_bb.height - points[i].y)
140                                                 + " ";
141                         } else {
142                                 result += "L " + points[i].x + " " + (_bb.height - points[i].y)
143                                                 + " ";
144                         }
145                 }
146                 result += "z\" style=\"fill:none; stroke:" + getRGBString(_curColor)
147                                 + "; stroke-width:" + thickness + ";\"/>\n";
148                 return result;
149         }
150
151         @Override
152         public String fillPolygonS(Double[] points, Color col) {
153                 String result = "<path d=\"";
154                 for (int i = 0; i < points.length; i++) {
155                         if (i == 0) {
156                                 result += "M " + points[i].x + " " + (_bb.height - points[i].y)
157                                                 + " ";
158                         } else {
159                                 result += "L " + points[i].x + " " + (_bb.height - points[i].y)
160                                                 + " ";
161                         }
162                 }
163                 result += "z\" fill=\""+getRGBString(col)+"\" style=\"stroke:none;\"/>\n";
164                 return result;
165         }
166         
167         @Override
168         public String drawBaseStartS(int index) {
169                 return "";
170         }
171
172         @Override
173         public String drawBaseEndS(int index) {
174                 return "";
175         }
176
177         @Override
178         public String drawBasePairStartS(int i, int j, ModeleBP bps) {
179                 return "";
180         }
181
182         @Override
183         public String drawBasePairEndS(int index) {
184                 return "";
185         }
186
187         @Override
188         public String drawBackboneStartS(int i, int j) {
189                 return "";
190         }
191
192         @Override
193         public String drawBackboneEndS(int index) {
194                 return "";
195         }
196
197 }