Source code: jpicedt/graphic/io/parser/pstricks/PSTLengthParameter.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.io.formatter.*;
35 import jpicedt.graphic.model.*;
36
37 /**
38 * Every statement involving length units (as set by PsTricks's "runit" register),
39 * e.g. "linewidth=13mm" or "doublesep=5.6" (default to current "runit")
40 * Numerical parsed value is pushed (after conversion to mm) in currentObj.attributeSet or in pool.ltx.attributeSet, depending on
41 * "pushInPool" flag.
42 *
43 * This expression contains a StatementExpression with parameters "DOUBLE" and "POSITIVE".
44 * So far, this expression parses "cm", "mm" and "pt".
45 *
46 * This expression MUST NOT be used to parse expressions that modify one of "xunit", "yunit" or "runit", since
47 * it relies on the value of these registers to compute the actual length value in case no unit is present !!!
48 */
49 public class PSTLengthParameter extends SequenceExpression implements PicObjectConstants {
50
51 private LaTeXParser.Pool pool;
52 private PicAttributeName attribute;
53 private boolean pushInObject;
54 private double val; // temporary stores parsed value
55
56 /**
57 * @param pl parser's pool
58 * @param tag LHS tag (e.g. "linewidth" or "doublesep") for the StatementExpression
59 * @param attribute name of attribute to modify (must be a predefined PicAttributeName of type "Double" or at least "Number")
60 * @param pushInPool if TRUE, we push the parsed value in the pool's attributeSet, otherwise in currentObj's attributeSet
61 * (this allows us to insert this expression, either in PSTParameterExpression, or in PSSetExpression)
62 */
63 public PSTLengthParameter(LaTeXParser.Pool pl, String tag, PicAttributeName attributeName, boolean pushInPool){
64
65 super(false);
66 pool = pl;
67 attribute = attributeName;
68 pushInObject = !pushInPool;
69
70 add(new StatementExpression(tag,"=",null,DOUBLE,POSITIVE){ // no postfix, but possibly whitespaces before
71 public void action(ParserEvent e){
72 if(DEBUG) System.out.println(e);
73 val = pool.pstRunit * ((Number)e.getValue()).doubleValue(); // suppose there's no unit
74 if (pushInObject) pool.currentObj.setAttribute(attribute, new Double(val));
75 else pool.pstSet.setAttribute(attribute, new Double(val));}});
76 // now, if there's a specified unit, we rescale to parsed value (i.e. dividing by pstRunit), then we compute the actual length value depending on the unit found
77 add(new OptionalExpression(new AlternateExpression(
78 new LiteralExpression("cm"){
79 public void action(ParserEvent e){
80 if (DEBUG) System.out.println(e);
81 val = val * 10.0 / pool.pstRunit;
82 if (pushInObject) pool.currentObj.setAttribute(attribute, new Double(val));
83 else pool.pstSet.setAttribute(attribute, new Double(val));}},
84 new LiteralExpression("mm"){
85 public void action(ParserEvent e){
86 if (DEBUG) System.out.println(e);
87 val = val / pool.pstRunit;
88 if (pushInObject) pool.currentObj.setAttribute(attribute, new Double(val));
89 else pool.pstSet.setAttribute(attribute, new Double(val));}},
90 new LiteralExpression("pt"){
91 public void action(ParserEvent e){
92 if (DEBUG) System.out.println(e);
93 val = val * PS_POINT / pool.pstRunit;
94 if (pushInObject) pool.currentObj.setAttribute(attribute, new Double(val));
95 else pool.pstSet.setAttribute(attribute, new Double(val));}})));
96 }
97 } // class