1 JXG.PsTricks = new function() {
  2     this.psTricksString = "";
  3 };
  4 
  5 JXG.PsTricks.convertBoardToPsTricks = function(board) {
  6     var p = new JXG.Coords(JXG.COORDS_BY_SCREEN, [board.canvasWidth, board.canvasHeight], board);
  7     var q = new JXG.Coords(JXG.COORDS_BY_SCREEN, [0, 0], board);
  8     this.psTricksString = '\\begin{pspicture*}('+q.usrCoords[1]+','+p.usrCoords[2]+')('+p.usrCoords[1]+','+q.usrCoords[2]+')\n';
  9 
 10     // Arcs (hier nur Sektoren)
 11     for(var el in board.objects) {
 12         var pEl = board.objects[el];
 13         if(pEl.type == JXG.OBJECT_TYPE_ARC) {
 14             if(pEl.visProp['visible']) {
 15                 this.addSector(pEl);
 16             }
 17         }
 18     }    
 19     // Polygone
 20     for(var el in board.objects) {
 21         var pEl = board.objects[el];
 22         if(pEl.type == JXG.OBJECT_TYPE_POLYGON) {
 23             if(pEl.visProp['visible']) {
 24                 this.addPolygon(pEl);
 25             }
 26         }
 27     }
 28     // Winkel
 29     for(var el in board.objects) {
 30         var pEl = board.objects[el];
 31         if(pEl.type == JXG.OBJECT_TYPE_ANGLE) {
 32             if(pEl.visProp['visible']) {
 33                 this.addAngle(pEl);
 34             }
 35         }
 36     }
 37     // Kreise
 38     for(var el in board.objects) {
 39         var pEl = board.objects[el];
 40         if(pEl.type == JXG.OBJECT_TYPE_CIRCLE) {
 41             if(pEl.visProp['visible']) {
 42                 this.addCircle(pEl);
 43             }
 44         }
 45     }
 46     // Arcs
 47     for(var el in board.objects) {
 48         var pEl = board.objects[el];
 49         if(pEl.type == JXG.OBJECT_TYPE_ARC) {
 50             if(pEl.visProp['visible']) {
 51                 this.addArc(pEl);
 52             }
 53         }
 54     }
 55     // Linien
 56     for(var el in board.objects) {
 57         var pEl = board.objects[el];
 58         if(pEl.type == JXG.OBJECT_TYPE_LINE) {
 59             if(pEl.visProp['visible']) {
 60                 this.addLine(pEl);
 61             }
 62         }
 63     }
 64     // Punkte
 65     for(var el in board.objects) {
 66         var pEl = board.objects[el];
 67         if(pEl.type == JXG.OBJECT_TYPE_POINT) {
 68             if(pEl.visProp['visible']) {
 69                 this.addPoint(pEl);
 70             }
 71         }
 72     }    
 73     this.psTricksString += '\\end{pspicture*}';
 74 };
 75 
 76 JXG.PsTricks.givePsTricksToDiv = function(divId, board) {
 77     this.convertBoardToPsTricks(board);
 78     document.getElementById(divId).innerHTML = this.psTricksString;
 79 };
 80 
 81 JXG.PsTricks.addPoint = function(el) {
 82     this.psTricksString += "\\psdot";
 83     this.psTricksString += "[linecolor=" + this.parseColor(el.visProp['strokeColor']) + ",";
 84     this.psTricksString += "dotstyle=";
 85     if(el.visProp['face'] == 'cross') { // x
 86         this.psTricksString += "x, dotsize=";
 87         if(el.visProp['size'] == 2) {
 88             this.psTricksString += "2pt 2";
 89         }
 90         else if(el.visProp['size'] == 3) {
 91             this.psTricksString += "5pt 2";
 92         }
 93         else if(el.visProp['size'] >= 4) {
 94             this.psTricksString += "5pt 3";
 95         }        
 96     }
 97     else if(el.visProp['face'] == 'circle') { // circle
 98         this.psTricksString += "*, dotsize=";
 99         if(el.visProp['size'] == 1) {
100             this.psTricksString += "2pt 2";
101         }
102         else if(el.visProp['size'] == 2) {
103             this.psTricksString += "4pt 2";
104         }
105         else if(el.visProp['size'] == 3) {
106             this.psTricksString += "6pt 2";
107         }  
108         else if(el.visProp['size'] >= 4) { // TODO
109             this.psTricksString += "6pt 3";
110         }          
111     }
112     else if(el.visProp['face'] == 'square') { // rectangle
113         this.psTricksString += "square*, dotsize=";
114         if(el.visProp['size'] == 2) {
115             this.psTricksString += "2pt 2";
116         }
117         else if(el.visProp['size'] == 3) {
118             this.psTricksString += "5pt 2";
119         }
120         else if(el.visProp['size'] >= 4) { // TODO
121             this.psTricksString += "5pt 3";
122         }           
123     }
124     else if(el.visProp['face'] == 'plus') { // +
125         this.psTricksString += "+, dotsize=";
126         if(el.visProp['size'] == 2) {
127             this.psTricksString += "2pt 2";
128         }
129         else if(el.visProp['size'] == 3) {
130             this.psTricksString += "5pt 2";
131         }
132         else if(el.visProp['size'] >= 4) { // TODO
133             this.psTricksString += "5pt 3";
134         }            
135     }
136     this.psTricksString += "]";
137     this.psTricksString += "("+el.coords.usrCoords[1]+","+el.coords.usrCoords[2]+")\n";
138     
139     // Label
140     this.psTricksString += "\\rput("+(el.coords.usrCoords[1]+15/ el.board.stretchY)+","+(el.coords.usrCoords[2]+15/ el.board.stretchY)+"){\\small $"+el.name+"$}\n";
141 };
142 
143 JXG.PsTricks.addLine = function(el) {
144     var screenCoords1 = new JXG.Coords(JXG.COORDS_BY_USER, el.point1.coords.usrCoords, el.board);
145     var screenCoords2 = new JXG.Coords(JXG.COORDS_BY_USER, el.point2.coords.usrCoords, el.board);
146     if(el.visProp['straightFirst'] || el.visProp['straightLast']) {
147        el.board.renderer.calcStraight(el,screenCoords1,screenCoords2); 
148     } 
149     this.psTricksString += "\\psline";
150     this.psTricksString += "[linecolor=" + this.parseColor(el.visProp['strokeColor']) + ", linewidth=" +el.visProp['strokeWidth']+"px";
151     this.psTricksString += "]";
152     if(el.visProp['firstArrow']) {
153         if(el.visProp['lastArrow']) {
154             this.psTricksString += "{<->}";
155         }
156         else {
157             this.psTricksString += "{<-}";
158         }
159     }
160     else {
161         if(el.visProp['lastArrow']) {
162             this.psTricksString += "{->}";
163         }
164     }
165     this.psTricksString += "("+screenCoords1.usrCoords[1]+","+screenCoords1.usrCoords[2]+")("+screenCoords2.usrCoords[1]+","+screenCoords2.usrCoords[2]+")\n";
166 };
167 
168 JXG.PsTricks.addCircle = function(el) {
169     var radius = el.Radius();
170     this.psTricksString += "\\pscircle";
171     this.psTricksString += "[linecolor=" + this.parseColor(el.visProp['strokeColor']) +", linewidth=" +el.visProp['strokeWidth']+"px";
172     if(el.visProp['fillColor'] != 'none' && el.visProp['fillOpacity'] != 0) {
173         this.psTricksString += ", fillstyle=solid, fillcolor="+this.parseColor(el.visProp['fillColor'])+", opacity="+el.visProp['fillOpacity'].toFixed(5);
174     }
175     this.psTricksString += "]";
176     this.psTricksString += "("+el.midpoint.coords.usrCoords[1]+","+el.midpoint.coords.usrCoords[2]+"){"+radius+"}\n";
177 };
178 
179 JXG.PsTricks.addPolygon = function(el) {
180     this.psTricksString += "\\pspolygon";
181     this.psTricksString += "[linestyle=none, fillstyle=solid, fillcolor="+this.parseColor(el.visProp['fillColor'])+", opacity="+el.visProp['fillOpacity'].toFixed(5)+"]";
182     for(var i=0; i < el.vertices.length; i++) {
183         this.psTricksString += "("+el.vertices[i].coords.usrCoords[1]+","+el.vertices[i].coords.usrCoords[2]+")";
184     }
185     this.psTricksString += "\n";
186 };
187 
188 JXG.PsTricks.addArc = function(el) {
189     var radius = el.Radius();  
190     var p = {};
191     p.coords = new JXG.Coords(JXG.COORDS_BY_USER, 
192                           [el.board.canvasWidth/(el.board.stretchY), el.midpoint.coords.usrCoords[2]],
193                           el.board);
194     var angle2 = JXG.Math.Geometry.trueAngle(p, el.midpoint, el.point2).toFixed(4);
195     var angle1 = JXG.Math.Geometry.trueAngle(p, el.midpoint, el.point3).toFixed(4);
196     
197     this.psTricksString += "\\psarc";
198     this.psTricksString += "[linecolor=" + this.parseColor(el.visProp['strokeColor']) + ", linewidth=" +el.visProp['strokeWidth']+"px";
199     this.psTricksString += "]";
200     if(el.visProp['lastArrow']) {
201         if(el.visProp['firstArrow']) {
202             this.psTricksString += "{<->}";
203         }
204         else {
205             this.psTricksString += "{<-}";
206         }
207     }
208     else {
209         if(el.visProp['firstArrow']) {
210             this.psTricksString += "{->}";
211         }
212     }    
213     this.psTricksString += "("+el.midpoint.coords.usrCoords[1]+","+el.midpoint.coords.usrCoords[2]+"){"+radius+"}{"+angle2+"}{"+angle1+"}\n";
214 };
215 
216 JXG.PsTricks.addSector = function(el) {
217     var radius = el.Radius();  
218     var p = {};
219     p.coords = new JXG.Coords(JXG.COORDS_BY_USER, 
220                           [el.board.canvasWidth/(el.board.stretchY), el.midpoint.coords.usrCoords[2]],
221                           el.board);
222     var angle2 = JXG.Math.Geometry.trueAngle(p, el.midpoint, el.point2).toFixed(4);
223     var angle1 = JXG.Math.Geometry.trueAngle(p, el.midpoint, el.point3).toFixed(4);
224 
225     if(el.visProp['fillColor'] != 'none' && el.visProp['fillOpacity'] != 0) {
226         this.psTricksString += "\\pswedge";
227         this.psTricksString += "[linestyle=none, fillstyle=solid, fillcolor="+this.parseColor(el.visProp['fillColor'])+", opacity="+el.visProp['fillOpacity'].toFixed(5)+"]";
228         this.psTricksString += "("+el.midpoint.coords.usrCoords[1]+","+el.midpoint.coords.usrCoords[2]+"){"+radius+"}{"+angle2+"}{"+angle1+"}\n";    
229     }
230 };
231 
232 JXG.PsTricks.addAngle = function(el) {
233     var radius = el.radius;
234     var p = {};
235     p.coords = new JXG.Coords(JXG.COORDS_BY_USER, 
236                           [el.board.canvasWidth/(el.board.stretchY), el.point2.coords.usrCoords[2]],
237                           el.board);
238     var angle2 = JXG.Math.Geometry.trueAngle(p, el.point2, el.point1).toFixed(4);
239     var angle1 = JXG.Math.Geometry.trueAngle(p, el.point2, el.point3).toFixed(4);
240 
241     if(el.visProp['fillColor'] != 'none' && el.visProp['fillOpacity'] != 0) {
242         this.psTricksString += "\\pswedge";
243         this.psTricksString += "[linestyle=none, fillstyle=solid, fillcolor="+this.parseColor(el.visProp['fillColor'])+", opacity="+el.visProp['fillOpacity'].toFixed(5)+"]";
244         this.psTricksString += "("+el.point2.coords.usrCoords[1]+","+el.point2.coords.usrCoords[2]+"){"+radius+"}{"+angle2+"}{"+angle1+"}\n";    
245     }
246     this.psTricksString += "\\psarc";
247     this.psTricksString += "[linecolor=" + this.parseColor(el.visProp['strokeColor']) + ", linewidth=" +el.visProp['strokeWidth']+"px";
248     this.psTricksString += "]"; 
249     this.psTricksString += "("+el.point2.coords.usrCoords[1]+","+el.point2.coords.usrCoords[2]+"){"+radius+"}{"+angle2+"}{"+angle1+"}\n";
250 };
251 
252 JXG.PsTricks.parseColor = function(color) {
253     var arr = JXG.rgbParser(color);
254     return "{[rgb]{"+arr[0]/255+","+arr[1]/255+","+arr[2]/255+"}}";
255 };
256