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/PsUnitLengthExpression.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  
35  /**
36   * To be used by PSTParamExpression.<p>
37   * "unit=1mm,xunit=1mm,yunit=1mm,runit=1mm" (or pt or cm), where setting unit sets xunit, yunit and runit simult.
38   * Default PsTricks's unit is 1cm ! (whereas jPicEdt default unit is 1mm) hence no unit leads
39   * to pstXunit = 0.1, etc...
40   * <p>
41   * Allowed syntaxs are : "0.11" (i.e. cm), "0.11mm", "0.11cm", "0.11pt" 
42   * <p>
43   * According to PsTricks's doc, whitespaces are allowed ONLY after the comma (see PsTricks doc. page 6),
44   * though here we use a StatementExpression which swallows them, which makes it less stringent.
45   * <p>
46   * Once parsed, we set "psXunit", "psRunit"  and "psYunit" in the pool
47   * <p>
48   */
49  public class PsUnitLengthExpression extends SequenceExpression  implements ExpressionConstants {
50  
51    private LaTeXParser.Pool pool;
52    public static final String XUNIT="xunit";
53    public static final String YUNIT="yunit";
54    public static final String RUNIT="runit";
55    public static final String UNIT="unit";
56    private String type;
57  
58    /**
59     * unit actually sets the following three parameters : xunit, yunit, and runit.
60     * @param type XUNIT, YUNIT,...
61     */
62    public PsUnitLengthExpression(LaTeXParser.Pool pl, String type){
63  
64      super(false);
65      this.pool = pl;
66      if (type!=XUNIT && type!=YUNIT && type!= RUNIT && type!=UNIT) throw new IllegalArgumentException(type);
67      this.type = type;
68  
69      add(new StatementExpression(type,"=",null,DOUBLE,POSITIVE){ // no postfix, but possibly whitespaces before
70                    public void action(ParserEvent e){
71                      if(DEBUG) System.out.println(e);
72                setUnit(10.0 * ((Number)e.getValue()).doubleValue());// suppose it's in cm (the default) and scale to mm
73                    }});
74      add(new OptionalExpression(new AlternateExpression(
75                                             new LiteralExpression("cm"), // do nothing !
76                                             new LiteralExpression("mm"){
77                                               public void action(ParserEvent e){
78                                                 if (DEBUG) System.out.println(e);
79                               scaleUnit(0.1);}},
80                                             new LiteralExpression("pt"){
81                                               public void action(ParserEvent e){
82                                                 if (DEBUG) System.out.println(e);
83                                                 scaleUnit(2.56/72.0);}}))); // rescale from pt to mm
84    }
85    
86    private void setUnit(double value){
87      if (type==XUNIT) pool.pstXunit = value;
88      else if (type==YUNIT) pool.pstYunit = value;
89      else if (type==RUNIT) pool.pstRunit = value;
90      else if (type==UNIT) {
91        pool.pstXunit = value;
92        pool.pstYunit = value;
93        pool.pstRunit = value;
94      }
95    }
96  
97    private void scaleUnit(double scale){
98      if (type==XUNIT) pool.pstXunit *= scale;
99      else if (type==YUNIT) pool.pstYunit *= scale;
100     else if (type==RUNIT) pool.pstRunit *= scale;
101     else if (type==UNIT) {
102       pool.pstXunit *= scale;
103       pool.pstYunit *= scale;
104       pool.pstRunit *= scale;
105     }
106   }
107 
108 } // PsUnitLengthExpression