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

Quick Search    Search Deep

Source code: jpicedt/graphic/io/parser/pstricks/PsRPutExpression.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.graphic.io.parser.pstricks;
32  
33  import jpicedt.graphic.io.parser.*;
34  import jpicedt.graphic.PicPoint;
35  import jpicedt.graphic.model.PicText;
36  
37  /**
38   * \\rput[refpoint]{rotation}(x,y){stuff} <br>
39   * \\rput*[refpoint]{rotation}(x,y){stuff} <br>
40   * where : <br>
41   * <ul>
42   * <li>refpoint = B|b|t for vertical and l|r for horizontal (B = baseline), used only for psframebox and rel.
43   * <li>rotation = any angle in degree or U,L,D,R,N,W,S or E.
44   * <li>stuff = whatever ! (this allows in particular to rotate things)
45   * </ul>
46   * Depending on the "stuff" found, instanciates :
47   * - PicText : PsBoxExpression (psframebox, pscirclebox,...)
48   * - PicPut  : Non-recognized argument
49   * [pending] instanciate other elements for rotation.
50   *
51   */
52  public class PsRPutExpression extends SequenceExpression  implements ExpressionConstants {
53  
54    private LaTeXParser.Pool pool;
55    private String vertAlign = PicText.CENTER_V;
56    private String horAlign = PicText.CENTER_H; // used only if a PicText object is indeed instanciated.
57    private double rotation = 0;
58    private PicPoint rPutPoint = new PicPoint();
59    private StringBuffer hrArgument = new StringBuffer(); // used by HRArgument
60    public final static String RECTANGLE_BOX = "\\psframebox";
61    public final static String CIRCLE_BOX = "\\pscirclebox";
62    public final static String OVAL_BOX = "\\psovalbox";
63  
64    public PsRPutExpression(LaTeXParser.Pool pl){
65  
66      super(true); // throw exception if stop before end
67      pool = pl;
68  
69      add(new LiteralExpression("\\rput")); // instanciation is done later on, depending on stuff found
70      add(new OptionalExpression(new LiteralExpression("*"))); // not used so far
71  
72      // refpoint -> optional alignment : [vh] where v = b|t|B|"" and h = r|l|"" ("" default to center)
73      SequenceExpression alignmentExp = new SequenceExpression(true); // throw IncompleteSequence Exception
74      alignmentExp.add(new LiteralExpression("["));
75      alignmentExp.add(new Alignment()); // swallows "]" and can return an empty String as in "[]"
76      alignmentExp.add(new WhiteSpaces());
77      add(new OptionalExpression(alignmentExp));
78  
79      // rotation : {angle}
80      SequenceExpression angleExp = new SequenceExpression(true); // throw IncompleteSequence Exception
81      angleExp.add(new LiteralExpression("{"));
82      angleExp.add(new WordExpression("}", true){ // swallow "}"
83                     public void action(ParserEvent e){
84                       if (DEBUG) System.out.println(e);
85                 // [pending]
86                 String s = (String)e.getValue();
87                 try {
88                   rotation = ((Double)e.getValue()).doubleValue();
89                   return;
90                 } catch (NumberFormatException nex){}
91                 if (s.equals("U")) rotation = 0;
92                 else if (s.equals("L")) rotation = 90;
93                 else if (s.equals("R")) rotation = -90;
94                 else if (s.equals("D")) rotation = 180;
95                 // [pending] other shortcuts
96                     }});
97      angleExp.add(new WhiteSpaces());
98      add(new OptionalExpression(angleExp));
99  
100     // (x,y) :
101     add(new PicPointExpression("(",",",")"){
102           public void action(ParserEvent e){
103             if (DEBUG) System.out.println(e);
104             // same as LatexPutExpression except that we use pstXunit...
105             rPutPoint = ((PicPoint)e.getValue()).toMm(pool.pstXunit,pool.pstYunit); // global var
106           }});
107     add(new WhiteSpaces());
108 
109     // {stuff} :
110     AlternateExpression rputArgs = new AlternateExpression();
111     rputArgs.add(new PsFrameBox(RECTANGLE_BOX)); // \\psframebox
112     rputArgs.add(new PsFrameBox(CIRCLE_BOX)); // \\pscirclebox
113     rputArgs.add(new PsFrameBox(OVAL_BOX)); // \\psovalbox
114     // [pending] circlebox and ovalbox
115     // [pending] add PsPolygon, ... to handle rotation
116     rputArgs.add(new HRArgument()); // pure HR argument (must always be added last)
117     add(new EnclosingExpression("{", rputArgs, "}"));
118 
119   }
120 
121   /** called when this SequenceExpression was successfully parsed -> reinit locals for next time */
122   public void action(ParserEvent e){
123     if (DEBUG) System.out.println(e);
124     vertAlign = PicText.CENTER_V;
125     horAlign = PicText.CENTER_H; // used only if a PicText object is indeed instanciated.
126     rotation = 0;
127     rPutPoint = new PicPoint();
128     hrArgument = new StringBuffer(); // used by HRArgument
129   }
130     
131   /**
132    * [hv] alignment string for \\rput, provided that stuff is an HR-argument, or a box.
133    * Otherwise I noticed it'd no effect in PsTricks. [pending] am I right ?
134    * Note : order is non-significant here, i.e. [hv] or [vh]
135    */
136   class Alignment extends WordExpression {
137 
138     public Alignment(){
139       super("]",true);
140     }
141 
142     public void action(ParserEvent e){
143       if (DEBUG) System.out.println(e);
144       String s = (String)e.getValue();
145       vertAlign = PicText.CENTER_V; // default
146       if (s.indexOf("t")!=-1) vertAlign = PicText.TOP;
147       else if (s.indexOf("b")!=-1) vertAlign = PicText.BOTTOM;
148       else if (s.indexOf("B")!=-1) vertAlign = PicText.BASELINE;
149       // else default to CENTER_V
150 
151       horAlign = PicText.CENTER_H; // default
152       if (s.indexOf("l")!=-1) horAlign = PicText.LEFT;
153       else if (s.indexOf("r")!=-1) horAlign = PicText.RIGHT;
154       // else default to CENTER_H
155     }
156   }
157 
158   /**
159    * \\psframebox[param]{text}}
160    */
161   class PsFrameBox extends SequenceExpression {
162 
163     private String type;
164     
165     /** @param type RECTANGLE_BOX, ... */
166     public PsFrameBox(String type){
167       super(true); // throw IncompleteSequence Exception
168       this.type = type;
169       // command :
170       this.add(new PSTInstanciationExpression(type, new PicText(),pool));
171       this.add(new OptionalExpression(new StarExpression(pool))); // "*"
172       this.add(new WhiteSpaces());
173       // optional [param]
174       this.add(new OptionalExpression(new PSTParametersExpression(pool,false))); // push in object's attributeSet, not in pool
175       // {text}
176       this.add(new EnclosedText());
177     }
178     public void action(ParserEvent e){
179       if (DEBUG) System.out.println(e);
180       ((PicText)(pool.currentObj)).setPoint(PicText.P_ANCHOR,rPutPoint);
181       ((PicText)(pool.currentObj)).setHorAlign(horAlign);
182       ((PicText)(pool.currentObj)).setVertAlign(vertAlign);
183       if (type==RECTANGLE_BOX) ((PicText)(pool.currentObj)).setFrameType(PicText.RECTANGLE);
184       else if (type==CIRCLE_BOX) ((PicText)(pool.currentObj)).setFrameType(PicText.CIRCLE);
185       if (type==OVAL_BOX) ((PicText)(pool.currentObj)).setFrameType(PicText.OVAL);
186     }
187   }
188 
189   /**
190    * handles {text} content by setting PicText's text content
191    */
192   class EnclosedText extends EnclosingExpression {
193 
194     public EnclosedText(){
195       super("{", null, "}");
196     }
197     public void action(ParserEvent e){
198       if (DEBUG) System.out.println(e);
199       ((PicText)(pool.currentObj)).setText((String)e.getValue());
200     }
201   }
202 
203   /**
204    * swallow as many chars as possible and push them in "hrArgument" buffer
205    */
206   class HRArgumentSucker extends WildCharExpression {
207 
208     public HRArgumentSucker(){ // no jokes, but it really sucks !
209       super(ANY_CHAR);
210     }
211         public void action(ParserEvent e){
212       if (DEBUG) System.out.println(e);
213       hrArgument.append(((Character)e.getValue()));
214     }
215   }
216   
217   /**
218    * handles content of "{stuff}" when no other expression matches, by instanciating a PicText
219    * with "stuff" as the PicText string
220    */
221   class HRArgument extends RepeatExpression {
222     
223     HRArgument(){
224       super(null,0,AT_LEAST);
225       setPattern(new HRArgumentSucker());
226     }
227     
228     public void action(ParserEvent e){
229       if (DEBUG) System.out.println(e);
230       pool.currentObj = new PicText(rPutPoint, hrArgument.toString(), PicText.NO_FRAME, horAlign, vertAlign, pool.pstSet);
231       pool.currentObj.setAttributeSet(pool.pstSet);
232       pool.currentGroup.addChild(pool.currentObj);
233     }
234   }
235 
236 }