~ubuntu-branches/debian/squeeze/latexdraw/squeeze

« back to all changes in this revision

Viewing changes to latexDraw/parsers/pstricks/LaTeXDrawPSTricksParserActions.java

  • Committer: Bazaar Package Importer
  • Author(s): Stuart Prescott
  • Date: 2009-05-03 23:49:35 UTC
  • Revision ID: james.westby@ubuntu.com-20090503234935-cls7n48x018g0vk2
Tags: upstream-2.0.2+1
ImportĀ upstreamĀ versionĀ 2.0.2+1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * LaTeXDrawPSTricksParserActions.java
 
3
 */
 
4
package latexDraw.parsers.pstricks;
 
5
 
 
6
import java.awt.geom.Arc2D;
 
7
import java.awt.geom.Point2D;
 
8
import java.util.Vector;
 
9
 
 
10
import latexDraw.figures.*;
 
11
import latexDraw.figures.properties.Arrowable;
 
12
import latexDraw.parsers.InvalidFormatCommandException;
 
13
import latexDraw.parsers.NotFullyManagedException;
 
14
import latexDraw.parsers.pstricks.PSTricksParameters.PositionParameters;
 
15
import latexDraw.psTricks.PSTricksConstants;
 
16
import latexDraw.util.LaTeXDrawPoint2D;
 
17
 
 
18
 
 
19
/** 
 
20
 * This class defines actions to do on a given command for the LaTeXDraw project.<br>
 
21
 *<br>
 
22
 * This file is part of LaTeXDraw<br>
 
23
 * Copyright (c) 2005-2008 Arnaud BLOUIN<br>
 
24
 *<br>
 
25
 *  LaTeXDraw is free software; you can redistribute it and/or modify
 
26
 *  it under the terms of the GNU General Public License as published by
 
27
 *  the Free Software Foundation; either version 2 of the License, or
 
28
 *  any later version.<br>
 
29
 *<br>
 
30
 *  LaTeXDraw is distributed without any warranty; without even the 
 
31
 *  implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
 
32
 *  PURPOSE. See the GNU General Public License for more details.<br>
 
33
 *<br>
 
34
 * 
 
35
 * 09/06/06<br>
 
36
 * @author Arnaud BLOUIN<br>
 
37
 * @version 0.5<br>
 
38
 */
 
39
public class LaTeXDrawPSTricksParserActions extends PSTricksParserActionsManager
 
40
{
 
41
        /** The parsed figures. */
 
42
        protected Vector<Figure> figures;
 
43
        
 
44
        /** The figures contained in the pscustom command. Cleared at the end of each <code>pscustom</code> command.*/
 
45
        public Vector<Figure> psCustFigures;
 
46
        
 
47
 
 
48
        
 
49
        /**
 
50
         * The constructor by default.
 
51
         */
 
52
        public LaTeXDrawPSTricksParserActions()
 
53
        {
 
54
                super();
 
55
                psCustFigures   = new Vector<Figure>();
 
56
                figures                 = new Vector<Figure>();
 
57
        }
 
58
        
 
59
        
 
60
        
 
61
 
 
62
        @Override
 
63
        public void actionsOnText(Vector<PSTricksParameters> params) throws InvalidFormatCommandException
 
64
        {
 
65
                if(params.isEmpty()) return;
 
66
                
 
67
                if(params.lastElement().textParsed.length()!=0)
 
68
                {
 
69
                        PSTricksParameters last = params.lastElement();
 
70
                        
 
71
                        PositionParameters pp = null;
 
72
                        int i = params.size()-1;
 
73
                        
 
74
                        while(pp==null && i>=0)
 
75
                        {
 
76
                                pp = params.elementAt(i).getPosition();
 
77
                                i--;
 
78
                        }
 
79
                        
 
80
                        if(pp==null)
 
81
                                pp = new PositionParameters();
 
82
                        
 
83
                        Text t = new Text(true);
 
84
                        Point2D.Double position = moveCoordinate(params, new Point2D.Double(0,0));
 
85
                        double angle = Math.toRadians(PSTricksParameters.getWholeRotationAngle(params));
 
86
                        
 
87
                        if(position==null) throw new InvalidFormatCommandException("text", -1); //$NON-NLS-1$
 
88
                        
 
89
                        if(last.textSize!=null && last.textSize.length()!=0)
 
90
                                t.setSizeByCommand(last.textSize);
 
91
                        
 
92
                        if((last.textSeries!=null && last.textSeries.equals("b")) || last.textBold) //$NON-NLS-1$
 
93
                                 t.setIsBold(true);
 
94
                        else t.setIsBold(false);
 
95
                        
 
96
                        if((last.textShape!=null && (last.textShape.equals("sl") || //$NON-NLS-1$
 
97
                           last.textShape.equals("it")) || last.textItalic)) //$NON-NLS-1$
 
98
                                 t.setIsItalic(true);
 
99
                        else t.setIsItalic(false);
 
100
                        
 
101
                        t.setRotationAngle(-angle);
 
102
                        t.setText(last.textParsed);
 
103
                        t.setLinesColor(last.textColor);
 
104
                        
 
105
                        if(last.textFamily!=null && last.textFamily.length()!=0)
 
106
                                t.setTextFontByFamily(last.textFamily);
 
107
                        
 
108
                        t.updateFontsBorders();
 
109
                        
 
110
                        LaTeXDrawPoint2D pos = new LaTeXDrawPoint2D(position.x*Figure.PPC-t.getWidth()/2., 
 
111
                                        (position.y*Figure.PPC-t.getFontMetrics().getHeight()/4.)*-1);
 
112
                        
 
113
                        if(pp.refPoint!=PositionParameters.REF_POINT_DEFAULT_CENTER)
 
114
                        {
 
115
                                double addX=0, addY=0;
 
116
                                Point2D.Double gc = new Point2D.Double((pos.x+t.getWidth())/2.,(pos.y+t.getHeight())/2.);
 
117
                                
 
118
                                if(pp.refPoint==PositionParameters.REF_POINT_BASE)
 
119
                                        addY=-t.getFontMetrics().getDescent();
 
120
                                else if(pp.refPoint==PositionParameters.REF_POINT_TOP)
 
121
                                        addY=t.getFontMetrics().getAscent()/2.;
 
122
                                else if(pp.refPoint==PositionParameters.REF_POINT_BOTTOM)
 
123
                                        addY=-t.getFontMetrics().getDescent()*2;
 
124
                                else if(pp.refPoint==PositionParameters.REF_POINT_LEFT)
 
125
                                        addX=t.getWidth()/2.;
 
126
                                else if(pp.refPoint==PositionParameters.REF_POINT_RIGHT)
 
127
                                        addX=-t.getWidth()/2.;
 
128
                                else if(pp.refPoint==PositionParameters.REF_POINT_RIGHT+PositionParameters.REF_POINT_BASE)
 
129
                                {
 
130
                                        addY=-t.getFontMetrics().getDescent();
 
131
                                        addX=-t.getWidth()/2.;
 
132
                                }else if(pp.refPoint==PositionParameters.REF_POINT_RIGHT+PositionParameters.REF_POINT_BOTTOM)
 
133
                                {
 
134
                                        addY=-t.getFontMetrics().getDescent()*2;
 
135
                                        addX=-t.getWidth()/2.;
 
136
                                }else if(pp.refPoint==PositionParameters.REF_POINT_RIGHT+PositionParameters.REF_POINT_TOP)
 
137
                                {
 
138
                                        addY=t.getFontMetrics().getAscent()/2.;
 
139
                                        addX=-t.getWidth()/2.;
 
140
                                }else if(pp.refPoint==PositionParameters.REF_POINT_LEFT+PositionParameters.REF_POINT_BASE)
 
141
                                {
 
142
                                        addY=-t.getFontMetrics().getDescent();
 
143
                                        addX=t.getWidth()/2.;
 
144
                                }else if(pp.refPoint==PositionParameters.REF_POINT_LEFT+PositionParameters.REF_POINT_BOTTOM)
 
145
                                {
 
146
                                        addY=-t.getFontMetrics().getDescent()*2;
 
147
                                        addX=t.getWidth()/2.;
 
148
                                }else if(pp.refPoint==PositionParameters.REF_POINT_LEFT+PositionParameters.REF_POINT_TOP)
 
149
                                {
 
150
                                        addY=t.getFontMetrics().getAscent()/2.;
 
151
                                        addX=t.getWidth()/2.;
 
152
                                }
 
153
                                
 
154
                                addX+=pp.labelSep*Figure.PPC;
 
155
                                
 
156
                                Point2D.Double pt = rotatePoint(new Point2D.Double(gc.x+addX, gc.y-addY), gc, angle);
 
157
                                pos.setLocation(pos.x+(pt.x-gc.x), pos.y-(pt.y-gc.y));
 
158
                        }
 
159
                        
 
160
                        t.setPosition(pos);
 
161
                        t.updateFontsBorders();
 
162
                        
 
163
                        if(last.psCustomP.fromPsCustom)
 
164
                                psCustFigures.add(t);
 
165
                        else
 
166
                                figures.add(t);
 
167
                }
 
168
        }
 
169
 
 
170
 
 
171
 
 
172
 
 
173
        /**
 
174
         * @return the figures
 
175
         */
 
176
        public Vector<Figure> getFigures()
 
177
        {
 
178
                return figures;
 
179
        }
 
180
 
 
181
 
 
182
        
 
183
        /**
 
184
         * Allows to set the parameters of a figure <code>f</code>.
 
185
         */
 
186
        public void setFigureParameters(PSTricksParameters p, Figure f, boolean hasStar)
 
187
        {
 
188
                if(f==null) return ;
 
189
                
 
190
                if(f.canHaveShadow())
 
191
                {
 
192
                        f.setHasShadow(p.isShadow);
 
193
                        f.setShadowAngle(Math.toRadians(p.shadowAngle));
 
194
                        f.setShadowSize(Math.abs(p.shadowSize)*Figure.PPC);
 
195
                        f.setShadowColor(p.shadowCol);
 
196
                }
 
197
                
 
198
                if(f.canBeHatched())
 
199
                {
 
200
                        f.setHatchingSep(p.hatchSep*Figure.PPC);
 
201
                        f.setGradientAngle(Math.toRadians(p.gradAngle));
 
202
                        f.setGradientEndColor(p.gradEnd);
 
203
                        f.setGradientMidPoint(p.gradMidPoint);
 
204
                        f.setGradientStartColor(p.gradBegin);
 
205
                }
 
206
        
 
207
                f.setLinesColor(p.lineColor);
 
208
                f.setBordersPosition(p.borderPos);
 
209
                f.setDoubleColor(p.dbleColor);
 
210
                f.setDoubleSep(Math.abs(p.dbleSep*Figure.PPC));
 
211
                f.setHasDoubleBoundary(p.dbleLine);
 
212
                f.setHatchingColor(p.hatchCol);
 
213
                if(p.fillStyle.equals(PSTricksConstants.TOKEN_FILL_SOLID))
 
214
                         f.setHatchingStyle(PSTricksConstants.TOKEN_FILL_NONE);
 
215
                else f.setHatchingStyle(
 
216
                                p.fillStyle.endsWith("*") ? p.fillStyle.substring(0, p.fillStyle.length()-1) : p.fillStyle);//$NON-NLS-1$
 
217
                f.setHatchingWidth((float)Math.abs(p.hatchWidth*Figure.PPC));
 
218
                f.setDotSep((float)  p.dotStep*Figure.PPC);
 
219
                f.setHatchingAngle(Math.toRadians(p.hatchAngle));
 
220
                f.setBlackDashLength((float)p.dashBlack*Figure.PPC);
 
221
                f.setWhiteDashLength((float)p.dashWhite*Figure.PPC);
 
222
                f.setInteriorColor(p.fillColor);
 
223
                f.setIsFilled(p.fillStyle.equals(PSTricksConstants.TOKEN_FILL_SOLID) ||
 
224
                                p.fillStyle.equals(PSTricksConstants.TOKEN_FILL_CROSSHATCH_F) ||
 
225
                                p.fillStyle.equals(PSTricksConstants.TOKEN_FILL_HLINES_F) ||
 
226
                                p.fillStyle.equals(PSTricksConstants.TOKEN_FILL_VLINES_F));
 
227
                f.setLineStyle(p.lineStyle);
 
228
                f.setThickness((float)(Math.abs(p.lineWidth*Figure.PPC)));
 
229
                
 
230
                if(hasStar)
 
231
                {
 
232
                        f.setIsFilled(true);
 
233
                        f.setInteriorColor(f.getLinesColor());
 
234
                        f.setHasDoubleBoundary(false);  
 
235
                        f.setLineStyle(PSTricksConstants.LINE_NONE_STYLE);
 
236
                        f.setHatchingStyle(PSTricksConstants.TOKEN_FILL_NONE);
 
237
                        f.setBordersPosition(PSTricksConstants.BORDERS_INSIDE);
 
238
                }
 
239
        }
 
240
        
 
241
        
 
242
 
 
243
 
 
244
        
 
245
        
 
246
        @Override
 
247
        public int actionsOnFrameEllipse(int line, boolean hasStar, Vector<PSTricksParameters> ps, String params, 
 
248
                                boolean isFrame) throws InvalidFormatCommandException
 
249
        {
 
250
                try
 
251
                {
 
252
                        if(ps.isEmpty()) return ERR_END_CMD_INDEX;
 
253
                        
 
254
                        Point2D.Double p3;
 
255
                        PSTricksParameters p = ps.lastElement();
 
256
                        Point2D.Double p1 = new Point2D.Double(p.origin.x, p.origin.y);
 
257
                        Point2D.Double p2 = new Point2D.Double();
 
258
                        PSTricksParameters newP = new PSTricksParameters(p);
 
259
                        double angle = Math.toRadians(-PSTricksParameters.getWholeRotationAngle(ps));
 
260
                        int end = parseParametersFrameEllipse(line, params, p1, p2, newP);
 
261
                        double xunit = newP.unit==PSTricksConstants.DEFAULT_UNIT?  newP.xUnit : newP.unit;
 
262
                        double yunit = newP.unit==PSTricksConstants.DEFAULT_UNIT? newP.yUnit : newP.unit ;
 
263
                        Figure f;
 
264
                        p1.setLocation(p1.x+p.origin.x, p1.y+p.origin.y);
 
265
                        
 
266
                        if(isFrame)
 
267
                        {
 
268
                                p2.setLocation(p2.x+p.origin.x, p2.y+p.origin.y);
 
269
                                if(ps.size()>1)
 
270
                                {
 
271
                                        p1 = moveCoordinate(ps, p1);
 
272
                                        p2 = moveCoordinate(ps, p2);
 
273
                                        p3 = new Point2D.Double((p1.x+p2.x)/2.,(p1.y+p2.y)/2.);
 
274
                                        p1 = rotatePoint(p1, p3, angle);
 
275
                                        p2 = rotatePoint(p2, p3, angle);
 
276
                                }
 
277
                                
 
278
                                p1.setLocation(p1.x*Figure.PPC*xunit, p1.y*Figure.PPC*-1*yunit);
 
279
                                p2.setLocation(p2.x*Figure.PPC*xunit, p2.y*Figure.PPC*-1*yunit);
 
280
                                
 
281
                                LaTeXDrawPoint2D pt1 = new LaTeXDrawPoint2D(p1.x, p2.y);
 
282
                                LaTeXDrawPoint2D pt2 = new LaTeXDrawPoint2D(p2.x, p2.y);
 
283
                                LaTeXDrawPoint2D pt3 = new LaTeXDrawPoint2D(p1.x, p1.y);
 
284
                                LaTeXDrawPoint2D pt4 = new LaTeXDrawPoint2D(p2.x, p1.y);
 
285
                                
 
286
                                f = new LaTeXDrawRectangle(pt1, pt2, pt3, pt4, true);
 
287
                                ((LaTeXDrawRectangle)f).setIsRound(newP.frameArc>0);
 
288
                                if(newP.frameArc>0) ((LaTeXDrawRectangle)f).setFrameArc(newP.frameArc);
 
289
                        }
 
290
                        else 
 
291
                        {
 
292
                                p1 = moveCoordinate(ps, p1);
 
293
                                p1.setLocation(p1.x*Figure.PPC*xunit, p1.y*Figure.PPC*-1*yunit);
 
294
                                p2.setLocation(p2.x*Figure.PPC*xunit, p2.y*Figure.PPC*-1*yunit);
 
295
                                
 
296
                                LaTeXDrawPoint2D pt1 = new LaTeXDrawPoint2D(p1.x-p2.x, p1.y+p2.y);
 
297
                                LaTeXDrawPoint2D pt2 = new LaTeXDrawPoint2D(p1.x+p2.x, p1.y+p2.y);
 
298
                                LaTeXDrawPoint2D pt3 = new LaTeXDrawPoint2D(p1.x-p2.x, p1.y-p2.y);
 
299
                                LaTeXDrawPoint2D pt4 = new LaTeXDrawPoint2D(p1.x+p2.x, p1.y-p2.y);
 
300
                                
 
301
                                f = new Ellipse(pt1, pt2, pt3, pt4, true);
 
302
                        }
 
303
                        
 
304
                        setFigureParameters(newP, f, hasStar);
 
305
                        f.setRotationAngle(angle);
 
306
                        
 
307
                        if(p.psCustomP.fromPsCustom)
 
308
                                psCustFigures.add(f);
 
309
                        else
 
310
                                figures.add(f);
 
311
                        
 
312
                        return end;
 
313
                        
 
314
                }catch(Exception e) 
 
315
                { 
 
316
                        if(isFrame)
 
317
                                throw new InvalidFormatCommandException("psframe", line);  //$NON-NLS-1$
 
318
                        throw new InvalidFormatCommandException("psellipse", line);  //$NON-NLS-1$
 
319
                }
 
320
        }
 
321
 
 
322
 
 
323
 
 
324
 
 
325
        @Override
 
326
        public int actionsOnCircle(int line, boolean hasStar, Vector<PSTricksParameters> ps, String params) 
 
327
                                throws InvalidFormatCommandException
 
328
        {
 
329
                try
 
330
                {
 
331
                        PSTricksParameters p = ps.lastElement();
 
332
                        Point2D.Double center = new Point2D.Double(p.origin.x, p.origin.y);
 
333
                        double[] radius = {1};
 
334
                        PSTricksParameters newP = new PSTricksParameters(p);
 
335
                        Figure c;
 
336
                        double angle = Math.toRadians(-PSTricksParameters.getWholeRotationAngle(ps));
 
337
                        int end = parseParametersCircle(line, params, center, radius, newP);
 
338
                        LaTeXDrawPoint2D pt1, pt2, pt3, pt4;
 
339
                        center = moveCoordinate(ps, center);
 
340
                        
 
341
                        pt1 = new LaTeXDrawPoint2D((p.origin.x+center.x-radius[0])*Figure.PPC*newP.unit, 
 
342
                                                                                (p.origin.y+center.y+radius[0])*Figure.PPC*-1*newP.unit);
 
343
                        pt2 = new LaTeXDrawPoint2D((p.origin.x+center.x+radius[0])*Figure.PPC*newP.unit, 
 
344
                                                                                (p.origin.y+center.y+radius[0])*Figure.PPC*-1*newP.unit);
 
345
                        pt3 = new LaTeXDrawPoint2D((p.origin.x+center.x-radius[0])*Figure.PPC*newP.unit, 
 
346
                                                                                (p.origin.y+center.y-radius[0])*Figure.PPC*-1*newP.unit);
 
347
                        pt4 = new LaTeXDrawPoint2D((p.origin.x+center.x+radius[0])*Figure.PPC*newP.unit, 
 
348
                                                                                (p.origin.y+center.y-radius[0])*Figure.PPC*-1*newP.unit);
 
349
                        
 
350
                        c = new Circle(pt1, pt2, pt3, pt4, true);
 
351
                        setFigureParameters(newP, c, hasStar);
 
352
                        c.setRotationAngle(angle);
 
353
                        
 
354
                        if(p.psCustomP.fromPsCustom)
 
355
                                psCustFigures.add(c);
 
356
                        else
 
357
                                figures.add(c);
 
358
                        
 
359
                        return end;
 
360
                        
 
361
                }catch(Exception e) { throw new InvalidFormatCommandException("pscircle", line); } //$NON-NLS-1$
 
362
        }
 
363
 
 
364
 
 
365
 
 
366
        
 
367
        
 
368
 
 
369
        @Override
 
370
        public int actionsOnDot(int line, Vector<PSTricksParameters> ps, String params,
 
371
                        boolean noCoordinatePossible) throws InvalidFormatCommandException
 
372
        {
 
373
                try
 
374
                {
 
375
                        if(ps==null || ps.isEmpty()) return ERR_END_CMD_INDEX;
 
376
                        // It can have several points defined
 
377
                        Vector<Point2D.Double> center = new Vector<Point2D.Double>();
 
378
                        PSTricksParameters last = ps.lastElement();
 
379
                        PSTricksParameters newP = new PSTricksParameters(last);
 
380
                        double angle = Math.toRadians(-PSTricksParameters.getWholeRotationAngle(ps));
 
381
                        int end = parseParametersDot(line, params, center, newP, noCoordinatePossible);
 
382
                        double xunit = newP.unit==PSTricksConstants.DEFAULT_UNIT?  newP.xUnit : newP.unit;
 
383
                        double yunit = newP.unit==PSTricksConstants.DEFAULT_UNIT? newP.yUnit : newP.unit ;
 
384
                        newP.arrowDotSD = newP.arrowDotSD+newP.arrowDotSN<0 ? Math.abs(newP.arrowDotSD) : newP.arrowDotSD;
 
385
                        newP.arrowDotSN = newP.arrowDotSD+newP.arrowDotSN<0 ? Math.abs(newP.arrowDotSN) : newP.arrowDotSN;
 
386
                        newP.dotScale1 = Math.abs(newP.dotScale1);
 
387
                                
 
388
                        for(Point2D.Double pt: center)
 
389
                        {
 
390
                                pt = moveCoordinate(ps, pt);
 
391
                                Dot d = new Dot(new LaTeXDrawPoint2D(pt.x*Figure.PPC*xunit, pt.y*Figure.PPC*-1*yunit), 
 
392
                                                                        true);
 
393
                                setFigureParameters(newP, d, false);
 
394
                                
 
395
                                d.setCurrentStyle(newP.dotStyle);
 
396
                                d.setWidth((float)((newP.arrowDotSD+newP.arrowDotSN*newP.lineWidth)*newP.dotScale1)*Figure.PPC);
 
397
                                d.setRotationAngle(Math.toRadians(-newP.dotAngle)+angle);
 
398
                                d.setInteriorColor(newP.fillColor);
 
399
                                
 
400
                                if(!last.psCustomP.fromPsCustom)
 
401
                                        figures.add(d);
 
402
                        }
 
403
                        
 
404
                        return end;
 
405
                        
 
406
                }catch(Exception e) { throw new InvalidFormatCommandException("psdot", line); }//$NON-NLS-1$
 
407
        }
 
408
 
 
409
 
 
410
        
 
411
        
 
412
 
 
413
 
 
414
        @Override
 
415
        public int actionsOnLine(int line, boolean hasStar, Vector<PSTricksParameters> ps, 
 
416
                        String params) throws InvalidFormatCommandException
 
417
        {
 
418
                try
 
419
                {
 
420
                        if(ps==null || ps.isEmpty()) return ERR_END_CMD_INDEX;
 
421
                        
 
422
                        Vector<Point2D.Double> pts = new Vector<Point2D.Double>();
 
423
                        PSTricksParameters last = ps.lastElement();
 
424
                        PSTricksParameters newP = new PSTricksParameters(last);
 
425
                        Figure f;
 
426
                        int end = parseParametersLine(line, params, pts, newP);
 
427
                        double xunit = newP.unit==PSTricksConstants.DEFAULT_UNIT?  newP.xUnit : newP.unit;
 
428
                        double yunit = newP.unit==PSTricksConstants.DEFAULT_UNIT? newP.yUnit : newP.unit ;
 
429
                        
 
430
                        if(pts.isEmpty()) return end;
 
431
                        
 
432
                        if(ps.size()>1)
 
433
                                for(Point2D.Double pt : pts)
 
434
                                        pt.setLocation(moveCoordinate(ps, pt));
 
435
                        
 
436
                        if(pts.size()>2)//joined lines
 
437
                                f = new JoinedLines(new LaTeXDrawPoint2D(), new LaTeXDrawPoint2D(), true);
 
438
                        else
 
439
                                f = new Line(new LaTeXDrawPoint2D(), new LaTeXDrawPoint2D(), true);
 
440
                                
 
441
                        
 
442
                        Point2D.Double ptOld= null;
 
443
                        
 
444
                        for(Point2D.Double pt : pts)
 
445
                                if(!pt.equals(ptOld))
 
446
                                {
 
447
                                        ((LaTeXDrawPolygon)f).addPoint(new LaTeXDrawPoint2D(pt.x*Figure.PPC*xunit,
 
448
                                                                                                        pt.y*Figure.PPC*-1*yunit));
 
449
                                        ptOld = pt;
 
450
                                }
 
451
 
 
452
                        if(pts.size()>2)//joined lines
 
453
                        {
 
454
                                ((JoinedLines)f).removePointAt(0);
 
455
                                ((JoinedLines)f).removePointAt(0);
 
456
                                ((JoinedLines)f).setArrow2Style(ArrowHead.invertArrowStyle(newP.arrowStyle[1]));
 
457
                                
 
458
                                if(((JoinedLines)f).getNbPoints()<2)
 
459
                                        return end;
 
460
                        }
 
461
                        else
 
462
                        {
 
463
                                ((Line)f).removePointAt(0);
 
464
                                ((Line)f).removePointAt(0);
 
465
                                ((Line)f).setArrow2Style(newP.arrowStyle[1]);
 
466
                        }
 
467
                        
 
468
                        ((Arrowable)f).setArrow1Style(newP.arrowStyle[0]);
 
469
                        setArrowParameters(((Arrowable)f).getArrowHead1(), newP);
 
470
                        setArrowParameters(((Arrowable)f).getArrowHead2(), newP);
 
471
                        setFigureParameters(newP, f, hasStar);
 
472
                        
 
473
                        if(last.psCustomP.fromPsCustom)
 
474
                                psCustFigures.add(f);
 
475
                        else
 
476
                                figures.add(f);
 
477
                        
 
478
                        return end;
 
479
                        
 
480
                }catch(Exception e) { e.printStackTrace(); throw  new InvalidFormatCommandException("psline", line); }//$NON-NLS-1$
 
481
        }
 
482
 
 
483
        
 
484
        
 
485
        
 
486
        /**
 
487
         * Sets the parameters of an arrowhead.
 
488
         * @param ah The arrowhead.
 
489
         * @param param The parameters.
 
490
         * @since 0.2.1
 
491
         */
 
492
        protected void setArrowParameters(ArrowHead ah, PSTricksParameters param)
 
493
        {
 
494
                ah.setArrowInset(param.arrowInset);
 
495
                ah.setArrowLength(param.arrowLgth);
 
496
                ah.setArrowSizeDim(param.arrowSizeD*Figure.PPC);
 
497
                ah.setArrowSizeNum(param.arrowSizeN);
 
498
                ah.setBracketNum(param.arrowBrLgth);
 
499
                ah.setDotSizeDim((param.arrowDotSD+param.arrowDotSN<0 ? Math.abs(param.arrowDotSD) : param.arrowDotSD)*Figure.PPC);
 
500
                ah.setDotSizeNum(param.arrowDotSD+param.arrowDotSN<0 ? Math.abs(param.arrowDotSN) : param.arrowDotSN);
 
501
                ah.setRBracketNum(param.arrowrBrLgth);
 
502
                ah.setTBarSizeDim(param.arrowTBarSD+param.arrowTBarSD<0 ? Math.abs(param.arrowTBarSD) : param.arrowTBarSD*Figure.PPC);
 
503
                ah.setTBarSizeNum(param.arrowTBarSD+param.arrowTBarSD<0 ? Math.abs(param.arrowTBarSN) : param.arrowTBarSN);
 
504
 
 
505
        }
 
506
 
 
507
 
 
508
 
 
509
        @Override
 
510
        public int actionsOnPolygon(int line, boolean hasStar, Vector<PSTricksParameters> ps, 
 
511
                        String params) throws InvalidFormatCommandException
 
512
        {
 
513
                try
 
514
                {
 
515
                        if(ps==null || ps.isEmpty()) return ERR_END_CMD_INDEX;
 
516
                        
 
517
                        Vector<Point2D.Double> pts = new Vector<Point2D.Double>();
 
518
                        PSTricksParameters last = ps.lastElement();
 
519
                        PSTricksParameters newP = new PSTricksParameters(last);
 
520
                        int end = parseParametersPolygon(line, params, pts, newP);
 
521
                        double xunit = newP.unit!=PSTricksConstants.DEFAULT_UNIT? newP.unit : newP.xUnit;
 
522
                        double yunit = newP.unit!=PSTricksConstants.DEFAULT_UNIT? newP.unit : newP.yUnit;
 
523
 
 
524
                        if(pts.size()<2) return end;
 
525
                        
 
526
                        LaTeXDrawPolygon pol = new LaTeXDrawPolygon(new LaTeXDrawPoint2D(), new LaTeXDrawPoint2D(), true);
 
527
                        Point2D.Double ptOld= null;
 
528
                        
 
529
                        if(pts.size()==2)
 
530
                                pts.add(new Point2D.Double(newP.origin.x, newP.origin.y));
 
531
                        
 
532
                        for(Point2D.Double pt : pts)
 
533
                        {
 
534
                                pt.setLocation(moveCoordinate(ps, pt));
 
535
                                if(!pt.equals(ptOld))
 
536
                                {
 
537
                                        pol.addPoint(new LaTeXDrawPoint2D(pt.x*Figure.PPC*xunit, pt.y*Figure.PPC*-1*yunit));
 
538
                                        ptOld = pt;
 
539
                                }
 
540
                        }
 
541
                        
 
542
                        pol.removePointAt(0);
 
543
                        pol.removePointAt(0);
 
544
                        
 
545
                        if(pol.getNbPoints()<3)
 
546
                                return end;
 
547
                        
 
548
                        setFigureParameters(newP, pol, hasStar);
 
549
                        
 
550
                        pol.updateBorders();
 
551
                        pol.updateGravityCenter();
 
552
                        
 
553
                        if(last.psCustomP.fromPsCustom)
 
554
                                psCustFigures.add(pol);
 
555
                        else
 
556
                                figures.add(pol);
 
557
                        
 
558
                        return end;
 
559
                        
 
560
                }catch(Exception e) { throw new InvalidFormatCommandException("pspolygon", line); }//$NON-NLS-1$
 
561
        }
 
562
 
 
563
 
 
564
 
 
565
 
 
566
        
 
567
        
 
568
        
 
569
        @Override
 
570
        public int actionsOnDiamondTriangle(int line, boolean hasStar, Vector<PSTricksParameters> ps,
 
571
                        String params, boolean isDiamondNotTriangle) throws InvalidFormatCommandException
 
572
        {
 
573
                try
 
574
                {
 
575
                        if(ps==null || ps.isEmpty()) return ERR_END_CMD_INDEX;
 
576
                        
 
577
                        PSTricksParameters p = ps.lastElement();
 
578
                        Point2D.Double p1 = new Point2D.Double(p.origin.x, p.origin.y);
 
579
                        Point2D.Double p2 = new Point2D.Double();
 
580
                        PSTricksParameters newP = new PSTricksParameters(p);
 
581
                        Figure f;
 
582
                        double angle = Math.toRadians(-PSTricksParameters.getWholeRotationAngle(ps));
 
583
                        int end = parseParametersFrameEllipse(line, params, p1, p2, newP);
 
584
                        double xunit = newP.unit!=PSTricksConstants.DEFAULT_UNIT? newP.unit : newP.xUnit;
 
585
                        double yunit = newP.unit!=PSTricksConstants.DEFAULT_UNIT? newP.unit : newP.yUnit;
 
586
                        p1.setLocation(p1.x+p.origin.x, p1.y+p.origin.y);
 
587
                        double moduloA;
 
588
                        if(ps.size()>1) moduloA = ps.elementAt(ps.size()-2).degrees;
 
589
                        else moduloA = 360;
 
590
 
 
591
                        if(isDiamondNotTriangle)
 
592
                        {
 
593
                                if(ps.size()>1)
 
594
                                        p1 = moveCoordinate(ps, p1);
 
595
                                
 
596
                                LaTeXDrawPoint2D pt1 = new LaTeXDrawPoint2D((p1.x-p2.x)*Figure.PPC*xunit, 
 
597
                                                                                                                        (p1.y-p2.y)*Figure.PPC*-1*yunit);
 
598
                                LaTeXDrawPoint2D pt2 = new LaTeXDrawPoint2D((p1.x+p2.x)*Figure.PPC*xunit, 
 
599
                                                                                                                        (p1.y-p2.y)*Figure.PPC*-1*yunit);
 
600
                                LaTeXDrawPoint2D pt3 = new LaTeXDrawPoint2D((p1.x-p2.x)*Figure.PPC*xunit, 
 
601
                                                                                                                        (p1.y+p2.y)*Figure.PPC*-1*yunit);
 
602
                                LaTeXDrawPoint2D pt4 = new LaTeXDrawPoint2D((p1.x+p2.x)*Figure.PPC*xunit, 
 
603
                                                                                                                        (p1.y+p2.y)*Figure.PPC*-1*yunit);
 
604
                                
 
605
                                f = new Rhombus(pt1, pt2, pt3, pt4, true);
 
606
                        }
 
607
                        else 
 
608
                        {
 
609
                                Point2D.Double p3 = new Point2D.Double();
 
610
                                if(ps.size()>1)
 
611
                                {
 
612
                                        p1 = moveCoordinate(ps, p1);
 
613
                                        p3 = new Point2D.Double(p1.x,p1.y-p2.y/2.);
 
614
                                        p1 = rotatePoint(p1, p3, -angle);
 
615
                                }
 
616
                        
 
617
                                LaTeXDrawPoint2D pt1 = new LaTeXDrawPoint2D((p1.x-p2.x/2.)*Figure.PPC*xunit, 
 
618
                                                                                                                        (p1.y+p2.y)*Figure.PPC*-1*yunit);
 
619
                                LaTeXDrawPoint2D pt2 = new LaTeXDrawPoint2D((p1.x+p2.x/2.)*Figure.PPC*xunit, 
 
620
                                                                                                                        (p1.y+p2.y)*Figure.PPC*-1*yunit);
 
621
                                LaTeXDrawPoint2D pt3 = new LaTeXDrawPoint2D((p1.x-p2.x/2.)*Figure.PPC*xunit, 
 
622
                                                                                                                        (p1.y)*Figure.PPC*-1*yunit);
 
623
                                LaTeXDrawPoint2D pt4 = new LaTeXDrawPoint2D((p1.x+p2.x/2.)*Figure.PPC*xunit, 
 
624
                                                                                                                        (p1.y)*Figure.PPC*-1*yunit);
 
625
                        
 
626
                                f = new Triangle(pt1, pt2, pt3, pt4, true);
 
627
                                
 
628
                                if((newP.gangle%moduloA)!=0)
 
629
                                {
 
630
                                        LaTeXDrawPoint2D gc = f.getGravityCenter();
 
631
                                        LaTeXDrawPoint2D base = new LaTeXDrawPoint2D((pt1.x+pt2.x)/2., pt3.y);
 
632
                                        LaTeXDrawPoint2D newGc = Figure.rotatePoint(gc, base, Math.toRadians(-(360/moduloA)*newP.gangle));
 
633
                                        f.shift(gc, newGc);
 
634
                                }
 
635
                        }
 
636
                        
 
637
                        setFigureParameters(newP, f, hasStar);
 
638
                        
 
639
                        if(isDiamondNotTriangle)
 
640
                        {
 
641
                                Rhombus r = ((Rhombus)f);
 
642
                                r.setShadowAngle(r.getShadowAngle()-Math.toRadians(newP.gangle%moduloA));
 
643
                                r.setHatchingAngle(r.getHatchingAngle()-Math.toRadians(newP.gangle%moduloA));
 
644
                                r.setGradientAngle(r.getGradientAngle()-Math.toRadians(newP.gangle%moduloA));
 
645
                                
 
646
                                if((newP.gangle%moduloA)!=0)
 
647
                                        r.setRotationAngle(Math.toRadians(-(360/moduloA)*newP.gangle)+angle);
 
648
                                else r.setRotationAngle(angle);
 
649
                        }
 
650
                        else
 
651
                        {
 
652
                                Triangle t = ((Triangle)f);
 
653
                                boolean reverse = t.getPoint(0).y>t.getPoint(2).y;
 
654
                                
 
655
                                t.setRotationAngle(angle); // fixes #1556340
 
656
                                if(reverse)
 
657
                                {
 
658
                                        t.setShadowAngle(t.getShadowAngle()+Math.PI);
 
659
                                        t.setGradientAngle(t.getGradientAngle()+Math.PI);
 
660
                                }
 
661
                        }
 
662
                        
 
663
                        if(p.psCustomP.fromPsCustom)
 
664
                                psCustFigures.add(f);
 
665
                        else
 
666
                                figures.add(f);
 
667
                        
 
668
                        return end;
 
669
                        
 
670
                }catch(Exception e) 
 
671
                { 
 
672
                        if(isDiamondNotTriangle)
 
673
                                throw new InvalidFormatCommandException("psdiamond", line); //$NON-NLS-1$
 
674
                        throw new InvalidFormatCommandException("triangle", line); //$NON-NLS-1$
 
675
                }
 
676
        }
 
677
 
 
678
        
 
679
        
 
680
        
 
681
        
 
682
         @Override
 
683
        public int actionsOnBezierCurve(int line, boolean hasStar, Vector<PSTricksParameters> ps, 
 
684
                        String params) throws InvalidFormatCommandException
 
685
                {
 
686
                        try
 
687
                        {
 
688
                                if(ps==null || ps.isEmpty()) return ERR_END_CMD_INDEX;
 
689
                                
 
690
                                Vector<Point2D.Double> pts = new Vector<Point2D.Double>();
 
691
                                PSTricksParameters last = ps.lastElement();
 
692
                                PSTricksParameters newP = new PSTricksParameters(last);
 
693
                                LaTeXDrawPoint2D p1;
 
694
                                int ended = parseParametersBezierCurve(line, params, pts, newP), i, size, j;
 
695
                                double xunit = newP.unit!=PSTricksConstants.DEFAULT_UNIT? newP.unit : newP.xUnit;
 
696
                                double yunit = newP.unit!=PSTricksConstants.DEFAULT_UNIT? newP.unit : newP.yUnit;
 
697
                        
 
698
                                if(pts.size()<3) return ended;
 
699
                                
 
700
                                for(Point2D.Double pt : pts)
 
701
                                        pt.setLocation(moveCoordinate(ps, pt));
 
702
                                
 
703
                                if(pts.size()%3==0) 
 
704
                                        p1 = new LaTeXDrawPoint2D(newP.origin.x*Figure.PPC*xunit, newP.origin.y*-1*Figure.PPC*yunit);
 
705
                                else 
 
706
                                {
 
707
                                        p1 = new LaTeXDrawPoint2D(pts.firstElement().x*Figure.PPC*xunit, pts.firstElement().y*-1*Figure.PPC*yunit);
 
708
                                        pts.removeElementAt(0);
 
709
                                }
 
710
                        
 
711
                                Point2D.Double pt2;
 
712
                                BezierCurve f = new BezierCurve(p1, 
 
713
                                                                                                new LaTeXDrawPoint2D(pts.elementAt(2).x*Figure.PPC*xunit, 
 
714
                                                                                                                                         pts.elementAt(2).y*-1*Figure.PPC*yunit), true);
 
715
                                
 
716
                                f.setXCoordFirstCtrl(pts.firstElement().x*Figure.PPC*xunit, 0);
 
717
                                f.setYCoordFirstCtrl(pts.firstElement().y*-1*Figure.PPC*yunit, 0);
 
718
                                f.setXCoordFirstCtrl(pts.elementAt(1).x*Figure.PPC*xunit, 1);
 
719
                                f.setYCoordFirstCtrl(pts.elementAt(1).y*-1*Figure.PPC*yunit, 1);
 
720
                                
 
721
                                i = 4;
 
722
                                j = 2;
 
723
                                size = pts.size();
 
724
                
 
725
                                while(i<size)
 
726
                                {
 
727
                                        pt2 = pts.elementAt(i);
 
728
                                        ++i;
 
729
                                        
 
730
                                        f.addPoint(new LaTeXDrawPoint2D(pts.elementAt(i).x*Figure.PPC*xunit, 
 
731
                                                                                                        pts.elementAt(i).y*-1*Figure.PPC*yunit));
 
732
                                        
 
733
                                        f.setXCoordFirstCtrl(pt2.x*Figure.PPC*xunit, j);
 
734
                                        f.setYCoordFirstCtrl(pt2.y*-1*Figure.PPC*yunit, j);
 
735
                                        j++;
 
736
                                        i+=2;
 
737
                                }
 
738
                                
 
739
                                setFigureParameters(newP, f, hasStar);
 
740
                                
 
741
                                f.updateSecondControlPoints();
 
742
                                f.updateShape();
 
743
                                f.updateBorders();
 
744
                                f.setShowPoints(newP.showPoints);
 
745
                                f.setOpen(true);
 
746
                                f.replaceLastPointByClosing();
 
747
                                
 
748
                                ((Arrowable)f).setArrow1Style(newP.arrowStyle[0]);
 
749
                                ((Arrowable)f).setArrow2Style(ArrowHead.invertArrowStyle(newP.arrowStyle[1]));
 
750
                                setArrowParameters(((Arrowable)f).getArrowHead1(), newP);
 
751
                                setArrowParameters(((Arrowable)f).getArrowHead2(), newP);
 
752
                                
 
753
                                if(f.getNbPoints()>1)
 
754
                                        if(last.psCustomP.fromPsCustom)
 
755
                                                psCustFigures.add(f);
 
756
                                        else
 
757
                                                figures.add(f);
 
758
                                
 
759
                                return ended;
 
760
                                
 
761
                        }catch(Exception e) { throw new InvalidFormatCommandException("psbezier", line); } //$NON-NLS-1$
 
762
                }
 
763
 
 
764
 
 
765
 
 
766
         
 
767
 
 
768
        @Override
 
769
        public int actionsOnArcWedge(int line, boolean hasStar, Vector<PSTricksParameters> ps, String params, 
 
770
                        boolean isArc) throws InvalidFormatCommandException
 
771
        {
 
772
                try
 
773
                {
 
774
                        PSTricksParameters p = ps.lastElement();
 
775
                        Point2D.Double center = (Point2D.Double)p.origin.clone();
 
776
                        PSTricksParameters newP = new PSTricksParameters(p);
 
777
                        double[] angle1 = {0}, angle2 = {0}, radius = {0};
 
778
                        double angle = Math.toRadians(-PSTricksParameters.getWholeRotationAngle(ps));
 
779
                        
 
780
                        int end = parseParametersArcWedge(line, params, radius, center, angle1, angle2, newP, isArc);
 
781
                
 
782
                        center = moveCoordinate(ps, center);
 
783
                        
 
784
                        Arc f = new Arc(new LaTeXDrawPoint2D((center.x-radius[0])*Figure.PPC*newP.xUnit, 
 
785
                                        (center.y+radius[0])*Figure.PPC*-1*newP.xUnit), 
 
786
                                        new LaTeXDrawPoint2D((center.x+radius[0])*Figure.PPC*newP.xUnit, 
 
787
                                        (center.y-radius[0])*Figure.PPC*-1*newP.xUnit), isArc ? Arc2D.OPEN : Arc2D.PIE, true);
 
788
                        
 
789
                        f.setStartAngle(Math.toRadians(angle1[0]));
 
790
                        f.setEndAngle(Math.toRadians(angle2[0]));
 
791
                        f.setShowPoints(newP.showPoints);
 
792
                        f.setArrow1Style(newP.arrowStyle[0]);
 
793
                        
 
794
                        String currentArrowRStyle = newP.arrowStyle[1];
 
795
                        if(currentArrowRStyle.equals(PSTricksConstants.DLARROW_STYLE))
 
796
                                currentArrowRStyle = PSTricksConstants.DRARROW_STYLE;
 
797
                        else if(currentArrowRStyle.equals(PSTricksConstants.DRARROW_STYLE))
 
798
                                currentArrowRStyle = PSTricksConstants.DLARROW_STYLE;
 
799
                        else if(currentArrowRStyle.equals(PSTricksConstants.RARROW_STYLE))
 
800
                                currentArrowRStyle = PSTricksConstants.LARROW_STYLE;
 
801
                        else if(currentArrowRStyle.equals(PSTricksConstants.LARROW_STYLE))
 
802
                                currentArrowRStyle = PSTricksConstants.RARROW_STYLE;
 
803
                        else if(currentArrowRStyle.equals(PSTricksConstants.DLARROW_STYLE))
 
804
                                currentArrowRStyle = PSTricksConstants.DRARROW_STYLE;
 
805
                        else if(currentArrowRStyle.equals(PSTricksConstants.LRBRACKET_STYLE))
 
806
                                currentArrowRStyle = PSTricksConstants.RRBRACKET_STYLE;
 
807
                        else if(currentArrowRStyle.equals(PSTricksConstants.RRBRACKET_STYLE))
 
808
                                currentArrowRStyle = PSTricksConstants.LRBRACKET_STYLE;
 
809
                        else if(currentArrowRStyle.equals(PSTricksConstants.RSBRACKET_STYLE))
 
810
                                currentArrowRStyle = PSTricksConstants.LSBRACKET_STYLE;
 
811
                        else if(currentArrowRStyle.equals(PSTricksConstants.LSBRACKET_STYLE))
 
812
                                currentArrowRStyle = PSTricksConstants.RSBRACKET_STYLE;
 
813
                        
 
814
                        f.setArrow2Style(currentArrowRStyle);
 
815
                        
 
816
                        setArrowParameters(f.getArrowHead1(), newP);
 
817
                        setArrowParameters(f.getArrowHead2(), newP);
 
818
                        setFigureParameters(newP, f, hasStar);
 
819
                        f.setRotationAngle(angle);
 
820
                        
 
821
                        if(p.psCustomP.fromPsCustom)
 
822
                                psCustFigures.add(f);
 
823
                        else
 
824
                                figures.add(f);
 
825
                        
 
826
                        return end;
 
827
                        
 
828
                }catch(Exception e) 
 
829
                { 
 
830
                        if(isArc)
 
831
                                throw new InvalidFormatCommandException("psarc", line);  //$NON-NLS-1$
 
832
                        throw new InvalidFormatCommandException("pswedge", line);  //$NON-NLS-1$
 
833
                }
 
834
        }
 
835
 
 
836
 
 
837
 
 
838
        
 
839
        @Override
 
840
        public int actionsOnGridAxes(int line, Vector<PSTricksParameters> ps, Point2D.Double pictureSWPt, 
 
841
                        Point2D.Double pictureNEPt, String params, boolean isGrid) throws InvalidFormatCommandException
 
842
                {
 
843
                        try
 
844
                        {
 
845
                                if(ps==null || ps.isEmpty()) return ERR_END_CMD_INDEX;
 
846
                                
 
847
                                Vector<Point2D.Double> pts = new Vector<Point2D.Double>();
 
848
                                PSTricksParameters last = ps.lastElement();
 
849
                                PSTricksParameters newP = new PSTricksParameters(last);
 
850
                                int gridEndX, gridEndY, gridStartX, gridStartY, originX, originY;
 
851
                                double angle = Math.toRadians(-PSTricksParameters.getWholeRotationAngle(ps));
 
852
                                int end = parseParametersGridAxes(line, params, pts, newP);
 
853
                                double xunit = newP.unit!=PSTricksConstants.DEFAULT_UNIT? newP.unit : newP.xUnit;
 
854
                                double yunit = newP.unit!=PSTricksConstants.DEFAULT_UNIT? newP.unit : newP.yUnit;
 
855
                                newP.origin.setLocation(moveCoordinate(ps, newP.origin));
 
856
                                boolean isGridXLabelReverse = false;
 
857
                                boolean isGridYLabelReverse = false;
 
858
                                
 
859
                                if(pts.isEmpty())
 
860
                                {
 
861
                                        if(pictureNEPt.x>=0)
 
862
                                                 gridEndX = (pictureNEPt.x-((int)pictureNEPt.x))>0.5 ? (int)pictureNEPt.x+1 : (int)pictureNEPt.x;
 
863
                                        else gridEndX = (((int)pictureNEPt.x)-pictureNEPt.x)>0.5 ? (int)pictureNEPt.x-1 : (int)pictureNEPt.x;
 
864
                                        if(pictureNEPt.y>=0)
 
865
                                                 gridEndY = (pictureNEPt.y-((int)pictureNEPt.y))>0.5 ? (int)pictureNEPt.y+1 : (int)pictureNEPt.y;
 
866
                                        else gridEndY = (((int)pictureNEPt.y)-pictureNEPt.y)>0.5 ? (int)pictureNEPt.y-1 : (int)pictureNEPt.y;
 
867
                                
 
868
                                        if(pictureSWPt.x>=0)
 
869
                                                 gridStartX = originX = (pictureSWPt.x-((int)pictureSWPt.x))>0.5 ? (int)pictureSWPt.x+1 : (int)pictureSWPt.x;
 
870
                                        else gridStartX = originX = (((int)pictureSWPt.x)-pictureSWPt.x)>0.5 ? (int)pictureSWPt.x-1 : (int)pictureSWPt.x;
 
871
                                        
 
872
                                        if(pictureSWPt.y>=0)
 
873
                                                 gridStartY = originY = (pictureSWPt.y-((int)pictureSWPt.y))>0.5 ? (int)pictureSWPt.y+1 : (int)pictureSWPt.y;
 
874
                                        else gridStartY = originY = (((int)pictureSWPt.y)-pictureSWPt.y)>0.5 ? (int)pictureSWPt.y-1 : (int)pictureSWPt.y;
 
875
                                }
 
876
                                else
 
877
                                        if(pts.size()==1)
 
878
                                        {
 
879
                                                gridStartX = gridStartY = originX = originY = 0;
 
880
                                                gridEndX = (int)pts.firstElement().x;
 
881
                                                gridEndY = (int)pts.firstElement().y;
 
882
                                        }
 
883
                                        else
 
884
                                                if(pts.size()==2)
 
885
                                                {
 
886
                                                        originX = gridStartX = (int)pts.firstElement().x;
 
887
                                                        originY = gridStartY = (int)pts.firstElement().y;
 
888
                                                        gridEndX = (int)pts.elementAt(1).x;
 
889
                                                        gridEndY = (int)pts.elementAt(1).y;
 
890
                                                }
 
891
                                                else
 
892
                                                {
 
893
                                                        originX = (int)pts.firstElement().x;
 
894
                                                        originY = (int)pts.firstElement().y;
 
895
                                                        gridStartX = (int)pts.elementAt(1).x;
 
896
                                                        gridStartY = (int)pts.elementAt(1).y;
 
897
                                                        gridEndX = (int)pts.elementAt(2).x;
 
898
                                                        gridEndY = (int)pts.elementAt(2).y;
 
899
                                                }
 
900
                                
 
901
                                if(gridStartX>=gridEndX)
 
902
                                {
 
903
                                        int tmp = gridEndX;
 
904
                                        gridEndX = gridStartX;
 
905
                                        gridStartX = tmp;
 
906
                                        isGridXLabelReverse = true;
 
907
                                }
 
908
                                
 
909
                                if(gridStartY>=gridEndY)
 
910
                                {
 
911
                                        int tmp = gridEndY;
 
912
                                        gridEndY = gridStartY;
 
913
                                        gridStartY = tmp;
 
914
                                        isGridYLabelReverse = true;
 
915
                                }
 
916
                                
 
917
                                LaTeXDrawPoint2D position = new LaTeXDrawPoint2D(newP.origin.x*Figure.PPC,newP.origin.y*Figure.PPC*-1);
 
918
                                GridShape f=null;
 
919
                                
 
920
                                if(angle!=0)
 
921
                                {
 
922
                                        Point2D.Double tmp1 = new Point2D.Double(
 
923
                                                        position.x+((gridEndX+gridStartX)/2.)*Figure.PPC*xunit, 
 
924
                                                        position.y-((gridEndY+gridStartY)/2.)*Figure.PPC*yunit);
 
925
                                        Point2D.Double tmp = rotatePoint(tmp1, position, angle);
 
926
                                        position.setLocation(position.x+(tmp.x-tmp1.x), position.y+(tmp.y-tmp1.y));
 
927
                                }
 
928
                                
 
929
                                if(isGrid)
 
930
                                {
 
931
                                        f = new Grid(position, true);
 
932
                                        Grid g = (Grid)f;
 
933
                                        g.setUnit(xunit);
 
934
                                        g.setGridDots(newP.gridDots);
 
935
                                        g.setGridLabelsColor(newP.labelsGridCol);
 
936
                                        g.setGridLabelsSize((int)((newP.gridLabel*PSTricksConstants.CM_VAL_PT)/0.6));
 
937
                                        g.setGridWidth((float)Math.abs(newP.gridWidth*Figure.PPC));
 
938
                                        g.setSubGridColor(newP.subGridCol);
 
939
                                        g.setSubGridDiv(newP.subGridDiv);
 
940
                                        g.setSubGridDots(newP.subGridDots);
 
941
                                        g.setSubGridWidth((float)Math.abs(newP.subGridWidth*Figure.PPC));
 
942
                                        g.setOriginX(originX);
 
943
                                        g.setOriginY(originY);
 
944
                                        g.setLinesColor(newP.gridColor);
 
945
                                }
 
946
                                else
 
947
                                {
 
948
                                        f = new Axe(position, true);
 
949
                                        Axe a = (Axe)f;
 
950
                                        a.setOriginX((int)newP.ox);
 
951
                                        a.setOriginY((int)newP.oy);
 
952
                                        a.setLabelsDisplayedToken(newP.labels);
 
953
                                        a.setTicksDisplayedToken(newP.ticks);
 
954
                                        a.setTicksStyleToken(newP.ticksStyle);
 
955
                                        a.setTicksSize(newP.ticksSize*Figure.PPC);
 
956
                                        a.setIncrementX(newP.dxIncrement);
 
957
                                        a.setIncrementY(newP.dyIncrement);
 
958
                                        a.setDistLabelsX(newP.dxLabelDist);
 
959
                                        a.setDistLabelsY(newP.dyLabelDist);
 
960
                                        a.setShowOrigin(newP.showOrigin);
 
961
                                        a.setAxesStyleToken(newP.axesStyle);
 
962
                                        setFigureParameters(newP, a, false);
 
963
                                        
 
964
                                        String arrowHead2Style = newP.arrowStyle[1];
 
965
                                        
 
966
                                        if(arrowHead2Style.equals(PSTricksConstants.DLARROW_STYLE))
 
967
                                                arrowHead2Style = PSTricksConstants.DRARROW_STYLE;
 
968
                                        else if(arrowHead2Style.equals(PSTricksConstants.DRARROW_STYLE))
 
969
                                                arrowHead2Style = PSTricksConstants.DLARROW_STYLE;
 
970
                                        else if(arrowHead2Style.equals(PSTricksConstants.RARROW_STYLE))
 
971
                                                arrowHead2Style = PSTricksConstants.LARROW_STYLE;
 
972
                                        else if(arrowHead2Style.equals(PSTricksConstants.LARROW_STYLE))
 
973
                                                arrowHead2Style = PSTricksConstants.RARROW_STYLE;
 
974
                                        else if(arrowHead2Style.equals(PSTricksConstants.DLARROW_STYLE))
 
975
                                                arrowHead2Style = PSTricksConstants.DRARROW_STYLE;
 
976
                                        else if(arrowHead2Style.equals(PSTricksConstants.LRBRACKET_STYLE))
 
977
                                                arrowHead2Style = PSTricksConstants.RRBRACKET_STYLE;
 
978
                                        else if(arrowHead2Style.equals(PSTricksConstants.RRBRACKET_STYLE))
 
979
                                                arrowHead2Style = PSTricksConstants.LRBRACKET_STYLE;
 
980
                                        else if(arrowHead2Style.equals(PSTricksConstants.RSBRACKET_STYLE))
 
981
                                                arrowHead2Style = PSTricksConstants.LSBRACKET_STYLE;
 
982
                                        else if(arrowHead2Style.equals(PSTricksConstants.LSBRACKET_STYLE))
 
983
                                                arrowHead2Style = PSTricksConstants.RSBRACKET_STYLE;
 
984
                                        
 
985
                                        ((Arrowable)a).setArrow1Style(newP.arrowStyle[0]);
 
986
                                        ((Arrowable)a).setArrow2Style(arrowHead2Style);
 
987
                                        setArrowParameters(((Arrowable)a).getArrowHead1(), newP);
 
988
                                        setArrowParameters(((Arrowable)a).getArrowHead2(), newP);
 
989
                                }
 
990
                                
 
991
                                f.setGridEndX(gridEndX);
 
992
                                f.setGridEndY(gridEndY);
 
993
                                f.setGridStartX(gridStartX);
 
994
                                f.setGridStartY(gridStartY);
 
995
                                f.updateFonts();
 
996
                                f.updateBorders(null);
 
997
                                f.updateBorders();
 
998
                                f.setRotationAngle(angle);
 
999
                                f.setXLabelSouth(!isGridYLabelReverse);
 
1000
                                f.setYLabelWest(!isGridXLabelReverse);
 
1001
                                
 
1002
                                if(!last.psCustomP.fromPsCustom)
 
1003
                                        figures.add(f);
 
1004
                        
 
1005
                                return end;
 
1006
                                
 
1007
                        }catch(Exception e) { throw new InvalidFormatCommandException("psgrid or psaxes", line); } //$NON-NLS-1$
 
1008
                }
 
1009
 
 
1010
 
 
1011
 
 
1012
 
 
1013
        @Override
 
1014
        public int actionsOnPicture(int line, Vector<PSTricksParameters> ps, 
 
1015
                                                                String params) throws InvalidFormatCommandException
 
1016
        {
 
1017
                try
 
1018
                {
 
1019
                        String[] path = {""};//$NON-NLS-1$
 
1020
                        int end = parseParametersPicture(line, params, path);
 
1021
//                      LaTeXDrawPoint2D position = new LaTeXDrawPoint2D();
 
1022
//                      BatchConvertFrame bcf = new BatchConvertFrame();
 
1023
//                      Image image = bcf.getImage(new File(path[0]));
 
1024
//                      BufferedImage bufferedI = new BufferedImage(image.getWidth(null), 
 
1025
//                                                                                      image.getHeight(null), BufferedImage.TYPE_INT_RGB );
 
1026
//                      Graphics g = bufferedI.createGraphics();
 
1027
//                      g.drawImage(image,0,0,null);
 
1028
//                      g.dispose();
 
1029
//                      
 
1030
//                      Picture p = new Picture(true, position, bufferedI, path[0]);
 
1031
//                      
 
1032
//                      figures.add(p);
 
1033
                        
 
1034
                        return end;
 
1035
                        
 
1036
                }catch(Exception e)
 
1037
                {
 
1038
                        e.printStackTrace();
 
1039
                        throw new InvalidFormatCommandException("includegraphics", line); //$NON-NLS-1$
 
1040
                }
 
1041
        }
 
1042
 
 
1043
 
 
1044
 
 
1045
 
 
1046
        @Override
 
1047
        public int actionsOnFramedBox(int nLine, boolean hasStar, Vector<PSTricksParameters> ps, 
 
1048
                        String cmd, String params, int type) throws InvalidFormatCommandException
 
1049
        {
 
1050
                int i = ps.size()-1;
 
1051
                PSTricksParameters pp = null;
 
1052
                
 
1053
                while(pp==null && i>=0)
 
1054
                        if(ps.elementAt(i).getPosition()!=null)
 
1055
                                pp = ps.elementAt(i);
 
1056
                        else i--;
 
1057
                        
 
1058
                if(pp==null)
 
1059
                        pp = ps.firstElement();
 
1060
                
 
1061
                FramedBox fb = new FramedBox(pp.psBoxText);
 
1062
                fb.setStart(pp.textForFrame.length());
 
1063
                pp.psBox.add(fb);
 
1064
                PSTricksParameters newP = new PSTricksParameters(ps.lastElement());
 
1065
                int end = -1;
 
1066
                int[] j = {0};
 
1067
                try 
 
1068
                { 
 
1069
                        goToNextChar(params, j); 
 
1070
                        if(params.charAt(j[0])=='*')
 
1071
                        {
 
1072
                                j[0]++;
 
1073
                                goToNextChar(params, j); 
 
1074
                        }
 
1075
                } 
 
1076
                catch(Exception e1) { throw new InvalidFormatCommandException(nLine); }
 
1077
                
 
1078
                if(params.charAt(j[0])=='[')
 
1079
                {
 
1080
                        switch(type)
 
1081
                        {
 
1082
                                case 0: 
 
1083
                                        fb.setBoxType(FramedBox.BOX_RECTANGLE); 
 
1084
                                        end = parseParametersFrameEllipse(nLine, params, null, null, newP);
 
1085
                                        if(newP.frameArc>0) 
 
1086
                                        {
 
1087
                                                ((LaTeXDrawRectangle)fb.getBox()).setFrameArc(newP.frameArc);
 
1088
                                                ((LaTeXDrawRectangle)fb.getBox()).setIsRound(true);
 
1089
                                        }
 
1090
                                        break;
 
1091
                                case 1: 
 
1092
                                        fb.setBoxType(FramedBox.BOX_CIRCLE); 
 
1093
                                        end = parseParametersCircle(nLine, params, null, null, newP);
 
1094
                                        break;
 
1095
                                case 2: 
 
1096
                                        fb.setBoxType(FramedBox.BOX_TRIANGLE); 
 
1097
                                        end = parseParametersPolygon(nLine, params, null, newP);
 
1098
                                        break;
 
1099
                                case 3: 
 
1100
                                        fb.setBoxType(FramedBox.BOX_DIAMOND); 
 
1101
                                        end = parseParametersPolygon(nLine, params, null, newP);
 
1102
                                        break;
 
1103
                                case 4: 
 
1104
                                        fb.setBoxType(FramedBox.BOX_ELLIPSE);
 
1105
                                        end = parseParametersFrameEllipse(nLine, params, null, null, newP);
 
1106
                                        break;
 
1107
                                case 5: 
 
1108
                                        fb.setBoxType(FramedBox.BOX_RECTANGLE); 
 
1109
                                        end = parseParametersFrameEllipse(nLine, params, null, null, newP);
 
1110
                                        if(newP.frameArc>0) 
 
1111
                                        {
 
1112
                                                ((LaTeXDrawRectangle)fb.getBox()).setFrameArc(newP.frameArc);
 
1113
                                                ((LaTeXDrawRectangle)fb.getBox()).setIsRound(true);
 
1114
                                        }
 
1115
                                        break;
 
1116
                                case 6: 
 
1117
                                        fb.setBoxType(FramedBox.BOX_RECTANGLE); 
 
1118
                                        end = parseParametersFrameEllipse(nLine, params, null, null, newP);
 
1119
                                        if(newP.frameArc>0) 
 
1120
                                        {
 
1121
                                                ((LaTeXDrawRectangle)fb.getBox()).setFrameArc(newP.frameArc);
 
1122
                                                ((LaTeXDrawRectangle)fb.getBox()).setIsRound(true);
 
1123
                                        }
 
1124
                                        break;
 
1125
                        }
 
1126
                        j[0] = end;
 
1127
                }
 
1128
                
 
1129
                try
 
1130
                {
 
1131
                        goToNextChar(params, j);
 
1132
                } catch(Exception e1) { throw new InvalidFormatCommandException(nLine); }
 
1133
                
 
1134
                switch(type)
 
1135
                {
 
1136
                        case 0: 
 
1137
                                fb.setBoxType(FramedBox.BOX_RECTANGLE); 
 
1138
                                break;
 
1139
                        case 1: 
 
1140
                                fb.setBoxType(FramedBox.BOX_CIRCLE); 
 
1141
                                break;
 
1142
                        case 2: 
 
1143
                                fb.setBoxType(FramedBox.BOX_TRIANGLE); 
 
1144
                                break;
 
1145
                        case 3: 
 
1146
                                fb.setBoxType(FramedBox.BOX_DIAMOND); 
 
1147
                                break;
 
1148
                        case 4: 
 
1149
                                fb.setBoxType(FramedBox.BOX_ELLIPSE);
 
1150
                                break;
 
1151
                        case 5: 
 
1152
                                fb.setBoxType(FramedBox.BOX_RECTANGLE); 
 
1153
                                break;
 
1154
                        case 6: 
 
1155
                                fb.setBoxType(FramedBox.BOX_RECTANGLE); 
 
1156
                                break;
 
1157
                }
 
1158
 
 
1159
                
 
1160
                if(params.charAt(j[0])!='{')//psframebox3, psframebox[framesep=0.3]3 for instance.
 
1161
                {
 
1162
                        PSTricksParameters p;
 
1163
                        boolean again = true;
 
1164
                        int k = ps.size()-1;
 
1165
                        
 
1166
                        while(again && k>=0)
 
1167
                        {
 
1168
                                p = ps.elementAt(k);
 
1169
                                if(p.getPosition()!=null)
 
1170
                                {
 
1171
                                        again = false;
 
1172
                                        p.textForFrame+=params.charAt(j[0]);
 
1173
                                }
 
1174
                                else 
 
1175
                                {
 
1176
                                        p.textForFrame+=params.charAt(j[0]);
 
1177
                                        k--;
 
1178
                                }
 
1179
                        }
 
1180
                        
 
1181
                        k = ps.size()-1;
 
1182
                        p=null;
 
1183
                        
 
1184
                        while(p==null && k>=0)
 
1185
                                if(ps.elementAt(k).getPosition()!=null)
 
1186
                                        p = ps.elementAt(k);
 
1187
                                else k--;
 
1188
                        
 
1189
                        if(p==null)
 
1190
                                p = ps.firstElement();
 
1191
                        
 
1192
                        p.psBox.lastElement().setEnd(p.textForFrame.length());
 
1193
                        p.psBox.add(0, p.psBox.remove(p.psBox.size()-1));
 
1194
                        
 
1195
                        end=j[0]+1;
 
1196
                }
 
1197
                
 
1198
                fb.setFrameSep(newP.frameSep*Figure.PPC);
 
1199
                fb.setBoxSep(newP.boxSep);
 
1200
                
 
1201
                setFigureParameters(newP, fb.getBox(), hasStar);
 
1202
                
 
1203
                if(hasStar)
 
1204
                {
 
1205
                        fb.getBox().setIsFilled(true);
 
1206
                        fb.getBox().setInteriorColor(newP.fillColor);
 
1207
                        fb.getBox().setLinesColor(newP.fillColor);
 
1208
                }
 
1209
                
 
1210
                switch(type)
 
1211
                {
 
1212
                        case 5: 
 
1213
                                fb.getBox().setHasDoubleBoundary(true);
 
1214
                                break;
 
1215
                        case 6: 
 
1216
                                fb.getBox().setHasShadow(true);
 
1217
                                break;
 
1218
                }
 
1219
                
 
1220
                if(end==-1)
 
1221
                        end = j[0];
 
1222
                
 
1223
                return end;
 
1224
        }
 
1225
 
 
1226
 
 
1227
 
 
1228
 
 
1229
        
 
1230
        @Override
 
1231
        public void actionsOnTerminatedFramedBoxes(Vector<PSTricksParameters> params) throws InvalidFormatCommandException
 
1232
        {
 
1233
                PSTricksParameters pp = params.lastElement();
 
1234
                
 
1235
                String str = pp.psBoxText.getText();
 
1236
                
 
1237
                if(str.length()==0)
 
1238
                        str = " ";//$NON-NLS-1$
 
1239
                
 
1240
                pp.textParsed = pp.textForFrame;
 
1241
                actionsOnText(params);
 
1242
                
 
1243
                if(pp.psBox.isEmpty())
 
1244
                        return;
 
1245
                
 
1246
                Text txt = (Text)figures.remove(figures.size()-1);
 
1247
                boolean simpleBox = pp.psBox.size()==1 && pp.psBox.firstElement().getBoxedText().equals(txt.getText());
 
1248
                
 
1249
                if(simpleBox)
 
1250
                {
 
1251
                        FramedBox fb = pp.psBox.firstElement();
 
1252
                        fb.setText(txt);
 
1253
                        fb.setStart(-1);
 
1254
                        fb.setEnd(-1);
 
1255
                        fb.getBox().setBordersPosition(PSTricksConstants.BORDERS_OUTSIDE);
 
1256
                        txt.setSimpleBox(fb);
 
1257
                }
 
1258
                else
 
1259
                        for(FramedBox fb : pp.psBox)
 
1260
                        {
 
1261
                                fb.setText(txt);
 
1262
                                txt.addBox(fb);
 
1263
                                fb.getBox().setBordersPosition(PSTricksConstants.BORDERS_OUTSIDE);
 
1264
                        }
 
1265
                
 
1266
                txt.setHasFramedBox(!pp.psBox.isEmpty());
 
1267
                txt.setHasSimpleFramedBox(simpleBox);
 
1268
                txt.updateFramedBoxes();
 
1269
                txt.updateFontsBorders();
 
1270
                
 
1271
                if(!pp.psCustomP.fromPsCustom)
 
1272
                        figures.add(txt);
 
1273
                
 
1274
                if(simpleBox)
 
1275
                {
 
1276
                        if(txt.getSimpleBox().getBoxType()==FramedBox.BOX_TRIANGLE)
 
1277
                        {
 
1278
                                double height = (txt.getSimpleBox().getBox().getTheSEBoundPoint().y-
 
1279
                                                txt.getSimpleBox().getBox().getTheNWBoundPoint().y)/4.;
 
1280
                                LaTeXDrawPoint2D pos = txt.getPosition();
 
1281
                                txt.setPosition(new LaTeXDrawPoint2D(pos.x, pos.y+height));
 
1282
                                txt.getSimpleBox().getBox().updateShape();
 
1283
                        }
 
1284
                }
 
1285
                else
 
1286
                {
 
1287
                        FramedBox fb, max=null;
 
1288
                        double xMin=Double.MAX_VALUE;
 
1289
                        int i=0, size = txt.getMultipleBox().size();
 
1290
                        
 
1291
                        while(i<size)
 
1292
                        {
 
1293
                                fb = txt.getMultipleBox().elementAt(i);
 
1294
 
 
1295
                                if(fb.isBoxSep() && fb.getBoxType()==FramedBox.BOX_TRIANGLE &&
 
1296
                                        fb.getBox().getTheNWBoundPoint().x<xMin)
 
1297
                                {
 
1298
                                        xMin = fb.getBox().getTheNWBoundPoint().x;
 
1299
                                        max = fb;
 
1300
                                }
 
1301
                                else i++;
 
1302
                        }
 
1303
 
 
1304
                        if(max!=null)
 
1305
                        {
 
1306
                                double height = (max.getBox().getTheSEBoundPoint().y-max.getBox().getTheNWBoundPoint().y)/4.;
 
1307
                                                                        
 
1308
                                LaTeXDrawPoint2D pos = txt.getPosition();
 
1309
                                txt.setPosition(new LaTeXDrawPoint2D(pos.x, pos.y+height));
 
1310
                                max.getBox().updateShape();
 
1311
                        }
 
1312
                        
 
1313
                }
 
1314
                
 
1315
                txt.updateFramedBoxes();
 
1316
                txt.updateFontsBorders();
 
1317
        }
 
1318
 
 
1319
 
 
1320
        
 
1321
        
 
1322
        
 
1323
        /**
 
1324
         * Optimises the figures; for instance, frame are transformed in square.
 
1325
         * @param vFigures The figures to optimise.
 
1326
         * @since 1.9
 
1327
         */
 
1328
        public static void optimizeCode(Vector<Figure> vFigures)
 
1329
        {
 
1330
                Vector<Arc> vArcs       = new Vector<Arc>();
 
1331
                Vector<Line> vLines = new Vector<Line>();
 
1332
                Vector<BezierCurve> vCurves = new Vector<BezierCurve>();
 
1333
                int sizevl, i, size, j;
 
1334
                
 
1335
                for(Figure f : vFigures)
 
1336
                        if(f instanceof Line)
 
1337
                                vLines.add((Line)f);
 
1338
                        else
 
1339
                                if(f instanceof Arc)
 
1340
                                        vArcs.add((Arc)f);
 
1341
                                else
 
1342
                                        if(f instanceof BezierCurve)
 
1343
                                                vCurves.add((BezierCurve)f);
 
1344
 
 
1345
                sizevl = vLines.size();
 
1346
                
 
1347
                for(Arc a : vArcs)// We check if we can convert arc+line in chord.
 
1348
                        if(a.getType()==Arc2D.OPEN)
 
1349
                        {
 
1350
                                boolean again = true;
 
1351
                                i=0;
 
1352
                                LaTeXDrawPoint2D startP, endP, p1, p2;
 
1353
                                Line l=null;
 
1354
                                
 
1355
                                while(again && i<sizevl)
 
1356
                                {
 
1357
                                        l               = vLines.elementAt(i);
 
1358
                                        startP  = a.getStartPoint(true);
 
1359
                                        endP    = a.getEndPoint(true);
 
1360
                                        p1              = l.getPt1();
 
1361
                                        p2              = l.getPt2();
 
1362
                                        startP.y*=-1;
 
1363
                                        endP.y*=-1;
 
1364
                                        
 
1365
                                        if((startP.equals(p1, 1) && endP.equals(p2, 1)) || 
 
1366
                                                (startP.equals(p2, 1) && endP.equals(p1, 1)))
 
1367
                                                again = false;
 
1368
                                        else 
 
1369
                                                i++;
 
1370
                                }
 
1371
                                
 
1372
                                if(!again && a.isParametersEquals(l, false, true))
 
1373
                                {
 
1374
                                        vFigures.remove(l);
 
1375
                                        a.setType(Arc2D.CHORD);
 
1376
                                }
 
1377
                        }
 
1378
                
 
1379
                if(vCurves.size()>1)// We try to concat the BĆ©zier curves.
 
1380
                {
 
1381
                        if(vCurves.size()<200)// We must set a limit otherwise it may have a freeze.
 
1382
                        {
 
1383
                                BezierCurve b1, b2;
 
1384
                        
 
1385
                                for(j=0; j<vCurves.size(); j++)
 
1386
                                {
 
1387
                                        i = j+1;
 
1388
                                        b1 = vCurves.elementAt(j);
 
1389
                                        
 
1390
                                        while(i<vCurves.size())
 
1391
                                        {
 
1392
                                                b2 = vCurves.elementAt(i);
 
1393
                                        
 
1394
                                                if(b1.isJoined(b2)==1 && b1.isParametersEquals(b2, true, true))
 
1395
                                                {
 
1396
                                                        vCurves.remove(b2);
 
1397
                                                        vFigures.remove(b2);
 
1398
                                                        b1.join(b2);
 
1399
                                                        i=j+1;
 
1400
                                                }
 
1401
                                                else
 
1402
                                                        i++;
 
1403
                                        }
 
1404
                                } //for
 
1405
                        }
 
1406
                        
 
1407
                        for(BezierCurve bc : vCurves)
 
1408
                                bc.replaceLastPointByClosing();
 
1409
                }//if
 
1410
                
 
1411
                i = 0;
 
1412
                while(i<vCurves.size()) // We check if the it is possible to close a curve.
 
1413
                {
 
1414
                        BezierCurve bc = vCurves.elementAt(i);
 
1415
                        bc.replaceLastPointByClosing();
 
1416
                        
 
1417
                        if(bc.isOpen())
 
1418
                        {
 
1419
                                boolean again = true;
 
1420
                                size = vLines.size();
 
1421
 
 
1422
                                for(int k=0; k<size && again; k++)//We try to close it with a line.
 
1423
                                        if(bc.isLineClosingCurve(vLines.elementAt(k)) && 
 
1424
                                                bc.isParametersEquals(vLines.elementAt(k), true, false))
 
1425
                                        {
 
1426
                                                bc.setOpen(false);
 
1427
                                                bc.setCloseType(BezierCurve.CLOSE_TYPE_LINE);
 
1428
                                                again = false;
 
1429
                                                vFigures.remove(vLines.elementAt(k));
 
1430
                                        }
 
1431
                                
 
1432
                                if(again)//We try to close it with a two-points curve.
 
1433
                                {
 
1434
                                        j = 0;
 
1435
                                        
 
1436
                                        while(j<i && again)
 
1437
                                        {
 
1438
                                                BezierCurve bc2 = vCurves.elementAt(j);
 
1439
                                                
 
1440
                                                if(bc2.getNbPoints()==2 && bc.isOpen() && bc2.isCurveClosingCurve(bc)==1 && 
 
1441
                                                        bc.isParametersEquals(bc2, true, false))
 
1442
                                                {
 
1443
                                                        bc.setOpen(false);
 
1444
                                                        bc.setCloseType(BezierCurve.CLOSE_TYPE_CURVE);
 
1445
                                                        again = false;
 
1446
                                                        vCurves.remove(j);
 
1447
                                                        vFigures.remove(bc2);
 
1448
                                                        i--;
 
1449
                                                }
 
1450
                                                else j++;
 
1451
                                        }
 
1452
                                        
 
1453
                                        j = i+1;
 
1454
                                        while(j<vCurves.size() && again)
 
1455
                                        {
 
1456
                                                BezierCurve bc2 = vCurves.elementAt(j);
 
1457
                                                
 
1458
                                                if(bc2.getNbPoints()==2 && bc.isOpen() && bc.isCurveClosingCurve(bc2)==1 && 
 
1459
                                                        bc.isParametersEquals(bc2, true, false))
 
1460
                                                {
 
1461
                                                        bc.setOpen(false);
 
1462
                                                        bc.setCloseType(BezierCurve.CLOSE_TYPE_CURVE);
 
1463
                                                        again = false;
 
1464
                                                        vCurves.remove(j);
 
1465
                                                        vFigures.remove(bc2);
 
1466
                                                }
 
1467
                                                else j++;
 
1468
                                        }
 
1469
                                }
 
1470
                        }//if(bc.isOpen())
 
1471
                        i++;
 
1472
                }
 
1473
                
 
1474
                i=0;
 
1475
                size = vFigures.size();
 
1476
                while(i<size)
 
1477
                {
 
1478
                        Figure f = vFigures.elementAt(i);
 
1479
                        
 
1480
                        if(f instanceof LaTeXDrawRectangle && !(f instanceof Square))
 
1481
                        {// We check if we can convert frame in square.
 
1482
                                LaTeXDrawRectangle frame = (LaTeXDrawRectangle)f;
 
1483
                                
 
1484
                                if(frame.getHeight()==frame.getWidth())
 
1485
                                {
 
1486
                                        Square square = new Square(frame,true);
 
1487
                                        vFigures.remove(i);
 
1488
                                        vFigures.add(i, square);
 
1489
                                }
 
1490
                        }
 
1491
                        i++;
 
1492
                }
 
1493
        }
 
1494
 
 
1495
 
 
1496
 
 
1497
 
 
1498
        @Override
 
1499
        public void actionsOnterminatedPsCustom(PSTricksParameters param) throws NotFullyManagedException
 
1500
        {
 
1501
                Figure f;
 
1502
                int i=0;
 
1503
                
 
1504
                while(!psCustFigures.isEmpty())
 
1505
                {
 
1506
                        f = psCustFigures.remove(0);
 
1507
                        
 
1508
                        if(!(f instanceof AkinPoints) || ((AkinPoints)f).getNbPoints()>1)
 
1509
                        {
 
1510
                                if(f instanceof AkinPoints && ((AkinPoints)f).getType()==AkinPoints.TYPE_CURVES)
 
1511
                                {// We must transform the read points.
 
1512
                                        AkinPoints ak = (AkinPoints)f;
 
1513
                                        int j, size = ak.getNbPoints();
 
1514
                                        LaTeXDrawPoint2D prev, curr = ak.getPoint(0);
 
1515
                                        
 
1516
                                        for(j=1; j<size; j++)
 
1517
                                        {
 
1518
                                                prev = curr;
 
1519
                                                curr = ak.getPoint(j);
 
1520
                                                curr.setLocation(2*curr.x-prev.x, 2*curr.y-prev.y);
 
1521
                                        }
 
1522
                                        
 
1523
                                        ((AkinPoints)f).updateBorders();
 
1524
                                }
 
1525
                                
 
1526
                                figures.add(f);
 
1527
                                i++;
 
1528
                        }
 
1529
                }
 
1530
        }
 
1531
 
 
1532
 
 
1533
 
 
1534
 
 
1535
        @Override
 
1536
        public void actionsOnNewPath(int line)
 
1537
        {
 
1538
                psCustFigures.clear();
 
1539
        }
 
1540
 
 
1541
 
 
1542
 
 
1543
 
 
1544
        @Override
 
1545
        public int parseOpenShadowCommand(PSTricksParameters param, int line, String txt) throws InvalidFormatCommandException
 
1546
        {
 
1547
                PSTricksParameters pstp = new PSTricksParameters(param);
 
1548
                int end =  super.parseOpenShadowCommand(pstp, line, txt);
 
1549
                
 
1550
                for(Figure f : psCustFigures)
 
1551
                {
 
1552
                        f.setHasShadow(true);
 
1553
                        f.setShadowAngle(Math.toRadians(pstp.shadowAngle));
 
1554
                        f.setShadowColor(pstp.shadowCol);
 
1555
                        f.setShadowSize(Math.abs(pstp.shadowSize)*Figure.PPC);
 
1556
                }
 
1557
                
 
1558
                if(psCustFigures.size()>0 && psCustFigures.lastElement() instanceof AkinPoints)
 
1559
                {
 
1560
                        double xunit = param.unit!=PSTricksConstants.DEFAULT_UNIT? param.unit : param.xUnit;
 
1561
                        double yunit = param.unit!=PSTricksConstants.DEFAULT_UNIT? param.unit : param.yUnit;
 
1562
                        
 
1563
                        AkinPoints ak = new AkinPoints(new LaTeXDrawPoint2D(
 
1564
                                        param.psCustomP.lastPosition.x*Figure.PPC*xunit, 
 
1565
                                        param.psCustomP.lastPosition.y*Figure.PPC*yunit*-1), true);
 
1566
                        ak.setInterval(1);
 
1567
                        ak.setType(AkinPoints.TYPE_LINES);
 
1568
                        psCustFigures.add(ak);
 
1569
                }
 
1570
                
 
1571
                return end;
 
1572
        }
 
1573
 
 
1574
 
 
1575
 
 
1576
 
 
1577
        @Override
 
1578
        public int actionsOnCurveTo(int line, Vector<PSTricksParameters> params, String txt)
 
1579
                                                                throws InvalidFormatCommandException
 
1580
        {
 
1581
                Point2D.Double p1 = new Point2D.Double();
 
1582
                Point2D.Double p2 = new Point2D.Double();
 
1583
                Point2D.Double p3 = new Point2D.Double();
 
1584
                int end = parseParametersCurveTo(line, txt, p1, p2, p3);
 
1585
                PSTricksParameters last = params.lastElement();
 
1586
                AkinPoints ak ;
 
1587
                double xunit = last.unit!=PSTricksConstants.DEFAULT_UNIT? last.unit : last.xUnit;
 
1588
                double yunit = last.unit!=PSTricksConstants.DEFAULT_UNIT? last.unit : last.yUnit;
 
1589
                
 
1590
                if(psCustFigures.isEmpty() || !(psCustFigures.lastElement() instanceof AkinPoints))
 
1591
                {
 
1592
                        ak = new AkinPoints(new LaTeXDrawPoint2D(last.psCustomP.lastPosition.x*Figure.PPC*xunit, 
 
1593
                                        last.psCustomP.lastPosition.y*Figure.PPC*-1*yunit), true);
 
1594
                        setFigureParameters(params.lastElement(), ak, false);
 
1595
                        ak.setInterval(1);
 
1596
                        psCustFigures.add(ak);
 
1597
                }
 
1598
                else ak = (AkinPoints)psCustFigures.lastElement();
 
1599
                
 
1600
                ak.setType(AkinPoints.TYPE_CURVES);
 
1601
                ak.addPoint(new LaTeXDrawPoint2D(p3.x*Figure.PPC*xunit, p3.y*Figure.PPC*-1*yunit));
 
1602
                last.psCustomP.lastPosition.setLocation(p3);
 
1603
 
 
1604
                return end;
 
1605
        }
 
1606
 
 
1607
 
 
1608
 
 
1609
 
 
1610
        @Override
 
1611
        public int actionsOnLineTo(int line, Vector<PSTricksParameters> params, String txt) throws InvalidFormatCommandException
 
1612
        {
 
1613
                Point2D.Double p1 = new Point2D.Double();
 
1614
                int end = parseParametersLineMoveTo(line, txt, p1);
 
1615
                PSTricksParameters last = params.lastElement();
 
1616
                AkinPoints ak ;
 
1617
                double xunit = last.unit!=PSTricksConstants.DEFAULT_UNIT? last.unit : last.xUnit;
 
1618
                double yunit = last.unit!=PSTricksConstants.DEFAULT_UNIT? last.unit : last.yUnit;
 
1619
                
 
1620
                if(psCustFigures.isEmpty() || !(psCustFigures.lastElement() instanceof AkinPoints))
 
1621
                {
 
1622
                        ak = new AkinPoints(new LaTeXDrawPoint2D(last.psCustomP.lastPosition.x*Figure.PPC*xunit, 
 
1623
                                                                last.psCustomP.lastPosition.y*Figure.PPC*-1*yunit), true);
 
1624
                        setFigureParameters(params.lastElement(), ak, false);
 
1625
                        ak.setInterval(1);
 
1626
                        ak.setType(AkinPoints.TYPE_LINES);
 
1627
                        psCustFigures.add(ak);
 
1628
                }
 
1629
                else ak = (AkinPoints)psCustFigures.lastElement();
 
1630
                
 
1631
                ak.addPoint(new LaTeXDrawPoint2D(p1.x*Figure.PPC*xunit, p1.y*Figure.PPC*-1*yunit));
 
1632
                last.psCustomP.lastPosition.setLocation(p1);
 
1633
                
 
1634
                return end;
 
1635
        }
 
1636
 
 
1637
 
 
1638
 
 
1639
 
 
1640
        @Override
 
1641
        public int actionsOnMoveTo(int line, Vector<PSTricksParameters> params, String txt) throws InvalidFormatCommandException
 
1642
        {
 
1643
                Point2D.Double p1 = new Point2D.Double();
 
1644
                int end = parseParametersLineMoveTo(line, txt, p1);
 
1645
                PSTricksParameters last = params.lastElement();
 
1646
                last.psCustomP.lastPosition.setLocation(p1);
 
1647
                
 
1648
                return end;
 
1649
        }
 
1650
 
 
1651
 
 
1652
 
 
1653
 
 
1654
        @Override
 
1655
        public void actionsOnClosePathCommand(int line)
 
1656
        {
 
1657
                if(!psCustFigures.isEmpty())
 
1658
                {
 
1659
                        Figure f = psCustFigures.lastElement();
 
1660
                        
 
1661
                        if(f instanceof AkinPoints)
 
1662
                                ((AkinPoints)f).setOpen(false);
 
1663
                }
 
1664
        }
 
1665
        
 
1666
}
 
1667
 
 
1668