Bug fixed in preprocessor for new primitives.
[Faustine.git] / interpretor / preprocessor / faust-0.9.47mr3 / compiler / draw / device / SVGDev.cpp
1 /************************************************************************
2 ************************************************************************
3 FAUST compiler
4 Copyright (C) 2003-2004 GRAME, Centre National de Creation Musicale
5 ---------------------------------------------------------------------
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 ************************************************************************
20 ************************************************************************/
21
22
23
24 // SVGDev.cpp
25
26 #include "SVGDev.h"
27 #include "stdio.h"
28 #include <iostream>
29 using namespace std;
30
31 extern bool gShadowBlur;
32
33 static char* xmlcode(const char* name, char* name2)
34 {
35 int i,j;
36
37 // SUBSTITUTION DES CARACTeRES INTERDITS EN XML
38
39 for (i=0, j=0; (name[i] != 0) && (j < 250); i++) {
40 switch (name[i]) {
41 case '<' : name2[j++] = '&'; name2[j++] = 'l'; name2[j++] = 't'; name2[j++] = ';'; break;
42 case '>' : name2[j++] = '&'; name2[j++] = 'g'; name2[j++] = 't'; name2[j++] = ';'; break;
43 case '\'' : name2[j++] = '&'; name2[j++] = 'a'; name2[j++] = 'p'; name2[j++] = 'o'; name2[j++] = 's'; name2[j++] = ';'; break;
44 case '"' : name2[j++] = '&'; name2[j++] = 'q'; name2[j++] = 'u'; name2[j++] = 'o'; name2[j++] = 't'; name2[j++] = ';'; break;
45 case '&' : name2[j++] = '&'; name2[j++] = 'a'; name2[j++] = 'm'; name2[j++] = 'p'; name2[j++] = ';'; break;
46 default : name2[j++] = name[i];
47 }
48 }
49 name2[j] = 0;
50
51 return name2;
52 }
53
54 SVGDev::SVGDev(const char* ficName,double largeur, double hauteur)
55 {
56 double gScale = 0.5;
57 if ((fic_repr = fopen(ficName,"w+")) == NULL) {
58 cout<<"Impossible de creer ou d'ouvrir "<<ficName<<endl;
59 }
60
61 // representation file:
62 fprintf(fic_repr,"<?xml version=\"1.0\"?>\n");
63 // + DTD ...
64 // viewBox:
65 fprintf(fic_repr,"<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" viewBox=\"0 0 %f %f\" width=\"%fmm\" height=\"%fmm\" version=\"1.1\">\n", largeur, hauteur, largeur*gScale, hauteur*gScale);
66
67 if (gShadowBlur) {
68 fprintf(fic_repr,
69 "<defs>\n"
70 " <filter id=\"filter\" filterRes=\"18\" x=\"0\" y=\"0\">\n"
71 " <feGaussianBlur in=\"SourceGraphic\" stdDeviation=\"1.55\" result=\"blur\"/>\n"
72 " <feOffset in=\"blur\" dx=\"3\" dy=\"3\"/>\n"
73 " </filter>\n"
74 "</defs>\n"
75 );
76 }
77
78 }
79
80 SVGDev::~SVGDev()
81 {
82 fprintf(fic_repr,"</svg>\n");
83 fclose(fic_repr);
84 }
85
86 void SVGDev::rect(double x,double y,double l,double h, const char* color, const char* link)
87 {
88 char buf[512];
89 if (link != 0 && link[0]!=0) {
90 // open the optional link tag
91 fprintf(fic_repr,"<a xlink:href=\"%s\">\n", xmlcode(link, buf));
92 }
93 // draw the shadow
94 if (gShadowBlur) {
95 fprintf(fic_repr,"<rect x=\"%f\" y=\"%f\" width=\"%f\" height=\"%f\" rx=\"0.1\" ry=\"0.1\" style=\"stroke:none;fill:#aaaaaa;;filter:url(#filter);\"/>\n",x+1,y+1,l,h);
96 } else {
97 fprintf(fic_repr,"<rect x=\"%f\" y=\"%f\" width=\"%f\" height=\"%f\" rx=\"0\" ry=\"0\" style=\"stroke:none;fill:#cccccc;\"/>\n",x+1,y+1,l,h);
98 }
99
100 // draw the rectangle
101 fprintf(fic_repr,"<rect x=\"%f\" y=\"%f\" width=\"%f\" height=\"%f\" rx=\"0\" ry=\"0\" style=\"stroke:none;fill:%s;\"/>\n", x, y, l, h, color);
102 if (link != 0 && link[0]!=0) {
103 // close the optional link tag
104 fprintf(fic_repr,"</a>\n");
105 }
106
107 }
108
109 //<polygon fill="lightsteelblue" stroke="midnightblue" stroke-width="5"
110 // points="350,180 380,180 380,160 410,160 410,180 440,180 440,140 470,140 470,180
111 // 500,180 500,120 530,120 530,180" />
112
113 void SVGDev::triangle(double x,double y,double l,double h, const char* color, const char* link, bool leftright)
114 {
115 char buf[512];
116 if (link != 0 && link[0]!=0) {
117 // open the optional link tag
118 fprintf(fic_repr,"<a xlink:href=\"%s\">\n", xmlcode(link, buf));
119 }
120 // draw triangle+circle
121 float r = 1.5; // circle radius
122 float x0, x1, x2;
123 if (leftright) {
124 x0 = x;
125 x1 = x+l-2*r;
126 x2 = x+l-r;
127 } else {
128 x0 = x+l;
129 x1 = x+2*r;
130 x2 = x+r;
131 }
132 fprintf(fic_repr,"<polygon fill=\"%s\" stroke=\"black\" stroke-width=\".25\" points=\"%f,%f %f,%f %f,%f\"/>\n", color, x0,y, x1,y+h/2.0, x0,y+h);
133 fprintf(fic_repr,"<circle fill=\"%s\" stroke=\"black\" stroke-width=\".25\" cx=\"%f\" cy=\"%f\" r=\"%f\"/>\n", color, x2, y+h/2.0, r);
134 }
135
136 void SVGDev::rond(double x,double y,double rayon)
137 {
138 fprintf(fic_repr,"<circle cx=\"%f\" cy=\"%f\" r=\"%f\"/>\n",x,y,rayon);
139 }
140
141 void SVGDev::fleche(double x,double y,double rotation,int sens)
142 {
143 double dx = 3;
144 double dy = 1;
145
146 if(sens == 1)
147 {
148 fprintf(fic_repr,"<line x1=\"%f\" y1=\"%f\" x2=\"%f\" y2=\"%f\" transform=\"rotate(%f,%f,%f)\" style=\"stroke: black; stroke-width:0.25;\"/>\n",x-dx,y-dy,x,y,rotation,x,y);
149 fprintf(fic_repr,"<line x1=\"%f\" y1=\"%f\" x2=\"%f\" y2=\"%f\" transform=\"rotate(%f,%f,%f)\" style=\"stroke: black; stroke-width:0.25;\"/>\n",x-dx,y+dy,x,y,rotation,x,y);
150 }
151 else //for recursion
152 {
153 fprintf(fic_repr,"<line x1=\"%f\" y1=\"%f\" x2=\"%f\" y2=\"%f\" transform=\"rotate(%f,%f,%f)\" style=\"stroke: black; stroke-width:0.25;\"/>\n",x+dx,y-dy,x,y,rotation,x,y);
154 fprintf(fic_repr,"<line x1=\"%f\" y1=\"%f\" x2=\"%f\" y2=\"%f\" transform=\"rotate(%f,%f,%f)\" style=\"stroke: black; stroke-width:0.25;\"/>\n",x+dx,y+dy,x,y,rotation,x,y);
155 }
156 }
157
158 void SVGDev::carre(double x,double y,double cote)
159 {
160 fprintf(fic_repr,"<rect x=\"%f\" y=\"%f\" width=\"%f\" height=\"%f\" style=\"stroke: black;stroke-width:0.5;fill:none;\"/>\n",x-0.5*cote,y-cote,cote,cote);
161 }
162
163 void SVGDev::trait(double x1,double y1,double x2,double y2)
164 {
165 fprintf(fic_repr,"<line x1=\"%f\" y1=\"%f\" x2=\"%f\" y2=\"%f\" style=\"stroke:black; stroke-linecap:round; stroke-width:0.25;\"/>\n",x1,y1,x2,y2);
166 }
167
168 void SVGDev::dasharray(double x1,double y1,double x2,double y2)
169 {
170 fprintf(fic_repr,"<line x1=\"%f\" y1=\"%f\" x2=\"%f\" y2=\"%f\" style=\"stroke: black; stroke-linecap:round; stroke-width:0.25; stroke-dasharray:3,3;\"/>\n",x1,y1,x2,y2);
171 }
172
173 void SVGDev::text(double x,double y,const char* name, const char* link)
174 {
175 char buf[512];
176 if (link != 0 && link[0]!=0) {
177 // open the optional link tag
178 fprintf(fic_repr,"<a xlink:href=\"%s\">\n", xmlcode(link, buf));
179 }
180 char name2[256];
181 fprintf(fic_repr,"<text x=\"%f\" y=\"%f\" font-family=\"Arial\" font-size=\"7\" text-anchor=\"middle\" fill=\"#FFFFFF\">%s</text>\n",x,y+2,xmlcode(name,name2));
182 if (link != 0 && link[0]!=0) {
183 // close the optional link tag
184 fprintf(fic_repr,"</a>\n");
185 }
186 }
187
188 void SVGDev::label(double x,double y,const char* name)
189 {
190 char name2[256];
191 fprintf(fic_repr,"<text x=\"%f\" y=\"%f\" font-family=\"Arial\" font-size=\"7\">%s</text>\n",x,y+2,xmlcode(name,name2));
192 }
193
194 void SVGDev::markSens(double x,double y,int sens)
195 {
196 int offset = (sens == 1) ? 2 : -2;
197 fprintf(fic_repr,"<circle cx=\"%f\" cy=\"%f\" r=\"1\"/>\n", x+offset, y+offset);
198 }
199
200 void SVGDev::Error(const char* message, const char* reason,int nb_error,double x,double y,double largeur)
201 {
202 fprintf(fic_repr,"<text x=\"%f\" y=\"%f\" textLength=\"%f\" lengthAdjust=\"spacingAndGlyphs\" style=\"stroke: red; stroke-width:0.3; fill:red; text-anchor:middle;\">%d : %s</text>\n",x,y-7,largeur,nb_error,message);
203 fprintf(fic_repr,"<text x=\"%f\" y=\"%f\" textLength=\"%f\" lengthAdjust=\"spacingAndGlyphs\" style=\"stroke: red; stroke-width:0.3; fill:none; text-anchor:middle;\">%s</text>\n",x,y+7,largeur,reason);
204 }
205
206
207