Source code: jpicedt/format/pstricks/parser/PSTDashExpression.java
1 /*
2 * jPicEdt version 1.3.2, a picture editor for LaTeX.
3 * Copyright (C) 1999-2002 Sylvain Reynal
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 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
15 * Sylvain Reynal
16 * Département de Physique
17 * Ecole Nationale Supérieure de l'Electronique et de ses Applications (ENSEA)
18 * 6, avenue du Ponceau
19 * 95014 CERGY CEDEX
20 * FRANCE
21 * Tel : 00 +33 130 736 245
22 * Fax : 00 +33 130 736 667
23 * e-mail : reynal@ensea.fr
24 * jPicEdt web page : http://www.jpicedt.org
25 */
26 package jpicedt.format.pstricks.parser;
27
28 import jpicedt.graphic.io.parser.*;
29 import jpicedt.graphic.io.formatter.*;
30 import jpicedt.graphic.model.*;
31
32 /**
33 * Parses statements similar to "dash=1pt 3mm" (first opaque, then transparent).
34 * @author $Author: reynal $
35 * @version $Id: PSTDashExpression.java,v 1.2 2002/08/05 16:44:10 reynal Exp $
36 */
37 public class PSTDashExpression extends SequenceExpression implements PicObjectConstants {
38
39 private Pool pool;
40 private Pool.Key setKey;
41 private double val;// temporary stores parsed value
42
43
44 /**
45 * @param pl parser's pool
46 * @param attributeSetKey used to fetch the attribute set in which parsed parameters are stored.
47 */
48 public PSTDashExpression(Pool pl, Pool.Key attributeSetKey) {
49
50 super(false);
51 pool = pl;
52 this.setKey = attributeSetKey;
53
54 // "dash=3pt" ... (opaque dash)
55 add(
56 new StatementExpression("dash", "=", null, DOUBLE, POSITIVE) {// no postfix, but possibly whitespaces before
57 public void action(ParserEvent e) {
58 if (DEBUG) System.out.println(e);
59 val = pool.getDouble(PstricksParser.KEY_R_UNIT) * ((Number)e.getValue()).doubleValue();// suppose there's no unit
60 pool.setAttribute(setKey,DASH_OPAQUE, new Double(val));
61 }
62 });
63 // 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
64 add(new OptionalExpression(
65 new AlternateExpression(
66 new LiteralExpression("cm") {
67 public void action(ParserEvent e) {
68 if (DEBUG)
69 System.out.println(e);
70 val = val * 10.0 / pool.getDouble(PstricksParser.KEY_R_UNIT);
71 pool.setAttribute(setKey,DASH_OPAQUE, new Double(val));
72 }
73 },
74 new LiteralExpression("mm") {
75 public void action(ParserEvent e) {
76 if (DEBUG)
77 System.out.println(e);
78 val = val / pool.getDouble(PstricksParser.KEY_R_UNIT);
79 pool.setAttribute(setKey,DASH_OPAQUE, new Double(val));
80 }
81 },
82 new LiteralExpression("pt") {
83 public void action(ParserEvent e) {
84 if (DEBUG)
85 System.out.println(e);
86 val = val * PS_POINT / pool.getDouble(PstricksParser.KEY_R_UNIT);
87 pool.setAttribute(setKey,DASH_OPAQUE, new Double(val));
88 }
89 })));
90 // ... " 5mm" (transparent dash)
91 add(WHITE_SPACES_OR_EOL);
92 add(
93 new NumericalExpression(DOUBLE, POSITIVE, null, false) {
94 public void action(ParserEvent e) {
95 if (DEBUG)
96 System.out.println(e);
97 val = pool.getDouble(PstricksParser.KEY_R_UNIT) * ((Number)e.getValue()).doubleValue();// suppose there's no unit
98 pool.setAttribute(setKey,DASH_TRANSPARENT, new Double(val));
99 }
100 });
101 add(new OptionalExpression(new AlternateExpression(
102 new LiteralExpression("cm") {
103 public void action(ParserEvent e) {
104 if (DEBUG)
105 System.out.println(e);
106 val = val * 10.0 / pool.getDouble(PstricksParser.KEY_R_UNIT);
107 pool.setAttribute(setKey,DASH_TRANSPARENT, new Double(val));
108 }
109 },
110 new LiteralExpression("mm") {
111 public void action(ParserEvent e) {
112 if (DEBUG)
113 System.out.println(e);
114 val = val / pool.getDouble(PstricksParser.KEY_R_UNIT);
115 pool.setAttribute(setKey,DASH_TRANSPARENT, new Double(val));
116 }
117 },
118 new LiteralExpression("pt") {
119 public void action(ParserEvent e) {
120 if (DEBUG)
121 System.out.println(e);
122 val = val * PS_POINT / pool.getDouble(PstricksParser.KEY_R_UNIT);
123 pool.setAttribute(setKey,DASH_TRANSPARENT, new Double(val));
124 }
125 })));
126
127 }
128 }
129