Bug fixed in preprocessor for new primitives.
[Faustine.git] / interpretor / preprocessor / faust-0.9.47mr3 / compiler / draw / device / PSDev.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 // PSDev.cpp
25
26 #include "PSDev.h"
27 #include "string.h"
28 #include "math.h"
29 #include "compatibility.hh"
30 #include <iostream>
31
32 using namespace std;
33
34 static int gFileNum = 0;
35
36 static char * addFileNum(const char* fname)
37 {
38 char f[256];
39 char s[256];
40 int i;
41
42 // remove suffixes (.xxx.yyy)
43 for (i=0; (fname[i] != 0) && (fname[i] != '.'); i++) {
44 f[i] = fname[i];
45 }
46 f[i] = 0;
47
48 // add number and .ps suffix
49 snprintf(s, 255, "%s-%d.ps", f, ++gFileNum);
50 //cerr << "file name " << s << endl;
51 return strdup(s);
52 }
53
54 PSDev::PSDev(const char* ficName, double largeur, double hauteur)
55 {
56 if ((fic_repr = fopen(addFileNum(ficName),"w+")) == NULL) {
57 //if ((fic_repr = fopen(ficName,"w+")) == NULL) {
58 cout<<"Impossible de creer ou d'ouvrir "<<ficName<<endl;
59 }
60
61 if(largeur<hauteur)
62 largeur=hauteur;
63
64 fprintf(fic_repr,"%%!PS-Adobe-3.0 \n");
65 //fprintf(fic_repr,"%%%%Pages: (atend) \n");
66 fprintf(fic_repr,"%%%%BoundingBox: 0 0 450 %d\n",(int)floor((hauteur*450/largeur)+1));
67
68 fprintf(fic_repr,"/unit {%f mul} def\n\n",450/largeur);
69 fprintf(fic_repr,"0 %f unit translate\n",hauteur);
70 fprintf(fic_repr,"1 -1 scale\n\n"); // postscript's origin = lower left corner (SVG:upper left)
71 fprintf(fic_repr,"0.6 unit setlinewidth\n"); // lines' width
72
73 fprintf(fic_repr,"/Times-Roman findfont %% Get the basic font for text\n");
74 //fprintf(fic_repr,"15 unit scalefont %% Scale the font to 15 units\n");
75 fprintf(fic_repr,"10 unit scalefont %% Scale the font to 10 units\n");
76 fprintf(fic_repr,"setfont %% Make it the current font\n\n");
77 }
78
79 PSDev::~PSDev()
80 {
81 fprintf(fic_repr,"showpage\n"); //eject the page
82 fclose(fic_repr);
83 }
84
85 void PSDev::rect(double x,double y,double l,double h, const char*, const char*)
86 {
87 fprintf(fic_repr,"gsave\n");
88 fprintf(fic_repr,"newpath\n");
89 //fprintf(fic_repr,"1.5 unit setlinewidth\n");
90 fprintf(fic_repr,"1.0 unit setlinewidth\n");
91 fprintf(fic_repr,"%f unit %f unit moveto\n",x,y);
92 fprintf(fic_repr,"0 unit %f unit rlineto\n",h);
93 fprintf(fic_repr,"%f unit 0 unit rlineto\n",l);
94 fprintf(fic_repr,"0 unit %f unit rlineto\n",-h);
95 fprintf(fic_repr,"closepath\n");
96 fprintf(fic_repr,"stroke\n");
97 fprintf(fic_repr,"grestore\n");
98 }
99
100 void PSDev::triangle(double x,double y,double l,double h, const char*, const char*, bool leftright)
101 {
102 fprintf(fic_repr,"gsave\n");
103 fprintf(fic_repr,"newpath\n");
104 //fprintf(fic_repr,"1.5 unit setlinewidth\n");
105 fprintf(fic_repr,"1.0 unit setlinewidth\n");
106 fprintf(fic_repr,"%f unit %f unit moveto\n",x,y);
107 fprintf(fic_repr,"0 unit %f unit rlineto\n",h);
108 fprintf(fic_repr,"%f unit 0 unit rlineto\n",l);
109 fprintf(fic_repr,"0 unit %f unit rlineto\n",-h);
110 fprintf(fic_repr,"closepath\n");
111 fprintf(fic_repr,"stroke\n");
112 fprintf(fic_repr,"grestore\n");
113 }
114
115 void PSDev::rond(double x,double y,double rayon)
116 {
117 fprintf(fic_repr,"gsave\n");
118 fprintf(fic_repr,"newpath\n");
119 fprintf(fic_repr,"%f unit %f unit %f unit 0 360 arc\n",x,y,rayon);
120 fprintf(fic_repr,"fill\n");
121 fprintf(fic_repr,"grestore\n");
122 }
123
124 void PSDev::fleche(double x,double y,double rotation,int sens)
125 {
126 if(sens == 1)
127 {
128 fprintf(fic_repr,"gsave\n");
129 fprintf(fic_repr,"newpath\n");
130 fprintf(fic_repr,"0.3 setgray\n");
131 fprintf(fic_repr,"%f unit %f unit moveto\n",x,y);
132 fprintf(fic_repr,"%f rotate\n",rotation);
133 fprintf(fic_repr,"%f unit %f unit rlineto\n",(double)-4,(double)-2);
134 fprintf(fic_repr,"%f rotate\n",(double)-rotation);
135 fprintf(fic_repr,"%f unit %f unit moveto\n",x,y);
136 fprintf(fic_repr,"%f rotate\n",rotation);
137 fprintf(fic_repr,"%f unit %f unit rlineto\n",(double)-4,(double)+2);
138 fprintf(fic_repr,"closepath\n");
139 fprintf(fic_repr,"stroke\n");
140 fprintf(fic_repr,"grestore\n");
141 }
142 else //for the recursion
143 {
144 fprintf(fic_repr,"gsave\n");
145 fprintf(fic_repr,"newpath\n");
146 fprintf(fic_repr,"0.3 setgray\n");
147 fprintf(fic_repr,"%f unit %f unit moveto\n",x,y);
148 fprintf(fic_repr,"%f rotate\n",rotation);
149 fprintf(fic_repr,"%f unit %f unit rlineto\n",(double)4,(double)-2);
150 fprintf(fic_repr,"%f rotate\n",(double)-rotation);
151 fprintf(fic_repr,"%f unit %f unit moveto\n",x,y);
152 fprintf(fic_repr,"%f rotate\n",rotation);
153 fprintf(fic_repr,"%f unit %f unit rlineto\n",(double)4,(double)+2);
154 fprintf(fic_repr,"closepath\n");
155 fprintf(fic_repr,"stroke\n");
156 fprintf(fic_repr,"grestore\n");
157 }
158 }
159
160 void PSDev::carre(double x,double y,double cote)
161 {
162 fprintf(fic_repr,"gsave\n");
163 fprintf(fic_repr,"newpath\n");
164 fprintf(fic_repr,"0.3 setgray\n");
165 fprintf(fic_repr,"%f unit %f unit moveto\n",x-cote/2,y);
166 fprintf(fic_repr,"0 unit %f unit rlineto\n",-cote);
167 fprintf(fic_repr,"%f unit 0 unit rlineto\n",cote);
168 fprintf(fic_repr,"0 unit %f unit rlineto\n",cote);
169 fprintf(fic_repr,"closepath\n");
170 fprintf(fic_repr,"stroke\n");
171 fprintf(fic_repr,"grestore\n");
172 }
173
174 void PSDev::trait(double x1,double y1,double x2,double y2)
175 {
176 fprintf(fic_repr,"gsave\n");
177 fprintf(fic_repr,"0.3 setgray\n");
178 fprintf(fic_repr,"newpath\n");
179 fprintf(fic_repr,"%f unit %f unit moveto\n",x1,y1);
180 fprintf(fic_repr,"%f unit %f unit lineto\n",x2,y2);
181 fprintf(fic_repr,"stroke\n");
182 fprintf(fic_repr,"grestore\n");
183 }
184
185 void PSDev::dasharray(double x1,double y1,double x2,double y2)
186 {
187 fprintf(fic_repr,"gsave\n");
188 fprintf(fic_repr,"newpath\n");
189 fprintf(fic_repr,"0.6 setgray\n");
190 fprintf(fic_repr,"0.8 unit setlinewidth\n");
191 fprintf(fic_repr,"%f unit %f unit moveto\n",x1,y1);
192 fprintf(fic_repr,"%f unit %f unit lineto\n",x2,y2);
193 fprintf(fic_repr,"stroke\n");
194 fprintf(fic_repr,"grestore\n");
195 }
196
197 void PSDev::text(double x,double y,const char* nom, const char* link)
198 {
199 fprintf(fic_repr,"newpath\n");
200 //fprintf(fic_repr,"%f unit %f unit moveto\n",(x-4)-(strlen(nom)-1)*3.8,y+2);
201 fprintf(fic_repr,"%f unit %f unit moveto\n",(x-0)-(strlen(nom)-1)*3.8,y+2);
202 fprintf(fic_repr,"gsave\n");
203 fprintf(fic_repr,"1 -1 scale\n\n");
204 fprintf(fic_repr,"(%s) show\n",nom);
205 fprintf(fic_repr,"grestore\n");
206 }
207
208 void PSDev::label(double x,double y,const char* label)
209 {
210 fprintf(fic_repr,"gsave\n");
211 fprintf(fic_repr,"/Times-Roman findfont %% Get the basic font for text\n");
212 fprintf(fic_repr,"7 unit scalefont %% Scale the font to 10 points\n");
213 fprintf(fic_repr,"setfont %% Make it the current font\n\n");
214 fprintf(fic_repr,"newpath\n");
215 fprintf(fic_repr,"%f unit %f unit moveto\n",(x+2),y+1.2);
216 fprintf(fic_repr,"1 -1 scale\n");
217 fprintf(fic_repr,"(%s) show\n",label);
218 fprintf(fic_repr,"grestore\n");
219 }
220
221 void PSDev::markSens(double x,double y,int sens)
222 {
223 if (sens==1)
224 {
225 fprintf(fic_repr,"newpath\n");
226 fprintf(fic_repr,"%f unit %f unit moveto\n",x,y+4);
227 fprintf(fic_repr,"%f unit %f unit rlineto\n",(double)4,(double)-4);
228 fprintf(fic_repr,"closepath\n");
229 }
230 else //for the recursion
231 {
232 fprintf(fic_repr,"newpath\n");
233 fprintf(fic_repr,"%f unit %f unit moveto\n",x,y-4);
234 fprintf(fic_repr,"%f unit %f unit rlineto\n",(double)-4,(double)4);
235 fprintf(fic_repr,"closepath\n");
236 }
237 fprintf(fic_repr,"stroke\n");
238 }
239
240 void PSDev::Error(const char* message, const char* reason,int nb_error,double x,double y,double largeur)
241 {
242 fprintf(fic_repr,"gsave\n");
243 fprintf(fic_repr,"/Times-Roman findfont %% Get the basic font for text\n");
244 fprintf(fic_repr,"17 unit scalefont %% Scale the font to 10 points\n");
245 fprintf(fic_repr,"setfont %% Make it the current font\n\n");
246 fprintf(fic_repr,"newpath\n");
247 fprintf(fic_repr,"%f unit %f unit moveto\n",(x-8)-(strlen(message)-1)*3.8,y-10);
248 fprintf(fic_repr,"1 -1 scale\n");
249 fprintf(fic_repr,"(%s) show\n",message);
250 fprintf(fic_repr,"1 -1 scale\n");
251 fprintf(fic_repr,"%f unit %f unit moveto\n",(x-8)-(strlen(reason)-1)*3.8,y+10);
252 fprintf(fic_repr,"1 -1 scale\n");
253 fprintf(fic_repr,"(%s) show\n",reason);
254 fprintf(fic_repr,"grestore\n");
255
256 }
257
258