Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: jpicedt/format/pstricks/parser/PsPolygonExpression.java


1   /*  jPicEdt version 1.3.2, a picture editor for LaTeX.
2       Copyright (C) 1999-2002  Sylvain Reynal
3   
4       This program is free software; you can redistribute it and/or modify
5       it under the terms of the GNU General Public License as published by
6       the Free Software Foundation; either version 2 of the License, or
7       (at your option) any later version.
8   
9       This program is distributed in the hope that it will be useful,
10      but WITHOUT ANY WARRANTY; without even the implied warranty of
11      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12      GNU General Public License for more details.
13  
14      You should have received a copy of the GNU General Public License
15      along with this program; if not, write to the Free Software
16      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17  
18      Sylvain Reynal
19      Département de Physique
20      Ecole Nationale Supérieure de l'Electronique et de ses Applications (ENSEA)
21      6, avenue du Ponceau
22      95014 CERGY CEDEX
23      FRANCE
24  
25      Tel : 00 +33 130 736 245
26      Fax : 00 +33 130 736 667
27      e-mail : reynal@ensea.fr
28      jPicEdt web page : http://www.jpicedt.org
29  */
30  
31  package jpicedt.format.pstricks.parser;
32  
33  import jpicedt.graphic.io.parser.*;
34  import jpicedt.graphic.model.*;
35  import jpicedt.graphic.PicPoint;
36  
37  /**
38   * Parses \\pspolygon, \\psdots and \\psline commands : <ul>
39   * <li>\\pspolygon[param](2,4)(3,15) // NO ARROW ALLOWED HERE !<br>
40   * <li>\\pspolygon*[param](5,1)(5,8)...(xN,yN)<br>
41   * <li>\\psline[param]{arrows}(5,1)(5,8)...(xN,yN)<br>
42   * <li>\\psline*[param]{arrows}(5,1)(5,8)...(xN,yN)<br>
43   * <li>\\psdots[param]{arrows}(5,1)(5,8)...(xN,yN)<br>
44   * <li>\\psdots*[param]{arrows}(5,1)(5,8)...(xN,yN) (same as above, '*' being unused)<br>
45   * </ul> 
46   * Note : PsPolygon -> close path ; PsLine -> open path ; PsDots -> dots only<br>
47   * @author $Author: reynal $
48   * @version $Id: PsPolygonExpression.java,v 1.2 2002/08/05 16:44:10 reynal Exp $ 
49   */
50  public class PsPolygonExpression extends SequenceExpression  implements PicObjectConstants {
51  
52    private Pool pool;
53    private String type;
54    public static final String POLYGON = "\\pspolygon";
55    public static final String LINE = "\\psline";
56    public static final String DOTS = "\\psdots";
57  
58    /**
59     * Uses the given type as the default tag
60     */
61    public PsPolygonExpression(Pool pl, String type){
62      this(pl,type,null);
63    }
64    
65    /**
66     * @param shape POLYGON, LINE or DOTS
67     * @param tag if null, default to shape
68     */
69    public PsPolygonExpression(Pool pl, String shape, String tag){
70  
71      super(true); // throw IncompleteSequence Exception
72      pool = pl;
73      this.type = shape;
74      if (tag==null) tag = shape;
75  
76      PicPolygon prototype = new PicPolygon();
77      if (type==POLYGON) prototype.setClosed(true);
78      else  prototype.setClosed(false);
79  
80      add(new PSTInstanciationExpression(tag, prototype,pool));
81      add(WHITE_SPACES_OR_EOL);
82      add(new OptionalExpression(new StarExpression(pool)));
83      add(WHITE_SPACES_OR_EOL);
84      // add optional param here
85      add(new OptionalExpression(new EnclosingExpression("[",new PSTParametersExpression(pool,Pool.CURRENT_OBJ_ATTRIBUTES),"]"))); // push in object's attributeSet
86      // add arrows if type==LINE
87      if (type==LINE) add(new OptionalExpression(new PSTArrowExpression(pool)));
88      // multi-points :
89      add(WHITE_SPACES_OR_EOL);
90      int minNbPts = (type==DOTS ? 1 : 2);
91      add(new RepeatExpression(new SequenceExpression(
92                                   WHITE_SPACES_OR_EOL,
93                                   new PicPointExpression("(",",",")"){
94                                     public void action(ParserEvent e){
95                                       if (DEBUG) System.out.println(e);
96                                       PicPoint pt = (PicPoint)e.getValue();
97                                       ((PicPolygon)(pool.currentObj)).addPoint(pt.toMm(pool.getDouble(PstricksParser.KEY_X_UNIT),pool.getDouble(PstricksParser.KEY_Y_UNIT)));
98                         if (type==DOTS) pool.currentObj.setAttribute(POLYGON_STYLE,POLYGON_DOTS);
99                         else pool.currentObj.setAttribute(POLYGON_STYLE,POLYGON_SOLID);
100                                    }
101                                  }),minNbPts, AT_LEAST));
102   }
103 
104   public String toString(){
105     return "[PsPolygonExpression."+type+"]";
106   }
107 }