Source code: jpicedt/format/latex/PicBezierQuadFormatter.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.latex;
32
33 import jpicedt.graphic.*;
34 import jpicedt.graphic.model.*;
35 import jpicedt.graphic.io.formatter.*;
36 import jpicedt.graphic.PicPoint;
37
38 import java.awt.*;
39
40 /**
41 * A formater for PicBezierQuad objects
42 */
43 public class PicBezierQuadFormatter implements Formatter, LatexConstants,PicObjectConstants {
44
45 /** the Element this formater acts upon */
46 private PicBezierQuad element;
47 private LatexFormatter factory;
48
49 /**
50 *
51 */
52 public PicBezierQuadFormatter(PicBezierQuad element, LatexFormatter factory){
53 this.element = element;
54 this.factory=factory;;
55 }
56
57 /**
58 * latex or eepic formating algorithm
59 * support quad and cubic splines
60 *
61 * @return The LaTeX string corresponding to this object
62 * Returned string syntax :
63 * %Bezier ...
64 * ... (LaTeX commands)
65 * %End Bezier
66 *
67 * with the following detailed syntax for the first line (called the "comment string") :
68 * - Bezier 0 0(x0,y0)(xctrl1,yctrl1)(x1,y1) for a "naked" curve
69 * - Bezier 0 1(x0,y0)(xctrl1,yctrl1)(x1,y1) " " with an arrow on the first point
70 * - Bezier 1 0(x0,y0)(xctrl1,yctrl1)(x1,y1) " " with an arrow on the second point
71 * - Bezier 1 1(x0,y0)(xctrl1,yctrl1)(x1,y1) " " with two arrows
72 *
73 * @param zeroPoint (0,0) point of the LaTeX picture in pixel-coordinates
74 *
75 * @since jPicEdt 1.1, modified jPicEdt 1.3.2 to handles cubic splines
76 */
77 public String format(){
78
79 StringBuffer buf = new StringBuffer(100); // 100 char as initial capacity seems a good guess
80
81 factory.appendThicknessString(buf,element);
82
83 appendPicedtStartingString(buf);
84 PicPoint ptBuf = new PicPoint();
85
86 buf.append("\\qbezier"); // quad splines !
87 buf.append(element.getPoint(PicBezierQuad.P_1,ptBuf));
88 buf.append(element.getPoint(PicBezierQuad.P_CTRL,ptBuf));
89 buf.append(element.getPoint(PicBezierQuad.P_2,ptBuf));
90 buf.append(CR_LF);
91
92 // arrows
93 PicPoint loc,dir;
94 if (((Arrow)element.getAttribute(LEFT_ARROW))!=Arrow.NONE){
95
96 loc = element.getPoint(PicBezierQuad.P_1,null);
97 dir = PEToolKit.getDirector(element.getPoint(PicBezierQuad.P_CTRL,null),
98 element.getPoint(PicBezierQuad.P_1,null));
99 buf.append(factory.arrowToLatexString(loc, dir));
100 buf.append(CR_LF);
101 }
102
103 if (((Arrow)element.getAttribute(RIGHT_ARROW))!=Arrow.NONE){
104
105 loc = element.getPoint(PicBezierQuad.P_2,null);
106 dir = PEToolKit.getDirector(element.getPoint(PicBezierQuad.P_CTRL,null),
107 element.getPoint(PicBezierQuad.P_2,null));
108 buf.append(factory.arrowToLatexString(loc, dir));
109 buf.append(CR_LF);
110 }
111
112 buf.append("%End Bezier");
113 buf.append(CR_LF);
114 buf.append(CR_LF);
115 return buf.toString();
116 }
117
118 /**
119 * Create the PicEdt starting string (i.e. %Bezier...) valid for both the LaTeX and the eepic format, and
120 * append it to the given StringBuffer. The appended string is CR-terminated.
121 */
122 protected void appendPicedtStartingString(StringBuffer buf){
123
124 buf.append("%Bezier ");
125 buf.append(factory.toPicedtString((Arrow)element.getAttribute(LEFT_ARROW)));
126 buf.append(" ");
127 buf.append(factory.toPicedtString((Arrow)element.getAttribute(RIGHT_ARROW)));
128 buf.append(element.getPoint(PicBezierQuad.P_1,null));
129 buf.append(element.getPoint(PicBezierQuad.P_CTRL,null));
130 buf.append(element.getPoint(PicBezierQuad.P_2,null));
131 buf.append(CR_LF);
132 }
133
134 } // class