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

Quick Search    Search Deep

Source code: jpicedt/format/latex/PicPolygonFormatter.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   * An interface that specifies common formatting behaviours for Drawable objects
42   */
43  public class PicPolygonFormatter implements Formatter, PicObjectConstants,LatexConstants {
44  
45    /** the Element this formater acts upon */
46    protected PicPolygon element;
47    protected LatexFormatter factory;
48  
49    /**
50     *
51     */
52    public PicPolygonFormatter(PicPolygon element, LatexFormatter factory){
53      this.element = element;
54      this.factory=factory;;
55    }
56  
57    /**
58     * <P>Return a string representation of a PicPolygon in the LaTeX format.</p>
59     * <p>Syntax : <br>
60     * %Polygon [arrows] (x1,y1)(x2,y2)...(xn,yn) blacken|shade|whiten<br>
61     * ... (LaTeX or eepic commands)<br>
62     * %End Polygon<br>
63     * where [arrows]= "0 0", "0 1", "1 0" or "1 1".</p>
64     *
65     * <p>Filling is only relevant if using eepic, but we must store these informations even though they may not be used
66     * in this format.</p>
67     *
68     * @return The LaTeX string corresponding to this object
69     * @param The zero-point of the LaTeX picture in pixel-coordinates
70     * @since PicEdt 1.0
71     */
72    public String format(){
73  
74      StringBuffer buf = new StringBuffer(100);
75      int lastPt = element.getLastPointIndex(); // e.g. 1 if polygon has 2 pts
76      if (lastPt==0) return ""; // a point
77  
78      factory.appendThicknessString(buf,element);
79  
80      // picedt comment-like starting string
81      appendPicedtStartingString(buf);
82  
83      // LaTeX commands
84      double dash = element.getAttribute(LINE_STYLE)==DASHED ? ((Double)element.getAttribute(DASH_OPAQUE)).doubleValue() : 0; // [pending] add support for black/white dash
85      if (lastPt==1){ // line
86        buf.append(factory.lineToLatexString(
87              element.getPointX(0),element.getPointY(0),
88                     element.getPointX(1),element.getPointY(1),
89                     (Arrow)element.getAttribute(LEFT_ARROW), 
90              (Arrow)element.getAttribute(RIGHT_ARROW), 
91                     dash));
92        buf.append(CR_LF);
93      }
94      else { 
95        // first segment, possibly with arrows on point "0" if not closed
96        buf.append(factory.lineToLatexString(
97              element.getPointX(0),element.getPointY(0),
98                     element.getPointX(1),element.getPointY(1),
99                     element.isClosed() ? Arrow.NONE : (Arrow)element.getAttribute(LEFT_ARROW), // left only
100             Arrow.NONE, // no right arrow except if it's a line, since it's the only segment
101                    dash));
102       buf.append(CR_LF);
103       // inner segments, no arrow ; only if lastPt > 2
104       for (int i=1; i<=lastPt-2; i++){
105         buf.append(factory.lineToLatexString(
106               element.getPointX(i),element.getPointY(i),
107                      element.getPointX(i+1),element.getPointY(i+1),
108                      Arrow.NONE,Arrow.NONE,
109                      dash));
110         buf.append(CR_LF);
111       }
112       // last segment, possibly with arrows on the last point
113       if (lastPt > 1){
114         buf.append(factory.lineToLatexString(
115               element.getPointX(lastPt-1),element.getPointY(lastPt-1),
116                      element.getPointX(lastPt),element.getPointY(lastPt),
117                      Arrow.NONE, 
118               element.isClosed() ? Arrow.NONE : (Arrow)element.getAttribute(RIGHT_ARROW),
119                      dash));
120         buf.append(CR_LF);
121       }
122       // add one segment to close path if applicable
123       if (element.isClosed() && lastPt > 1){
124         buf.append(factory.lineToLatexString(
125               element.getPointX(0),element.getPointY(0),
126                      element.getPointX(lastPt),element.getPointY(lastPt),
127                      Arrow.NONE,Arrow.NONE,
128                      dash));
129         buf.append(CR_LF);
130       }
131     }
132 
133     // picedt ending string
134     buf.append("%End Polygon");
135     buf.append(CR_LF);buf.append(CR_LF);
136     return buf.toString();
137   }
138 
139 
140   /**
141    * Create the PicEdt starting string (i.e. %Polygon...) valid for both the LaTeX and the eepic format, and
142    * append it to the given StringBuffer. Appended string is CR-terminated.
143    */
144   protected void appendPicedtStartingString(StringBuffer buf){
145 
146     buf.append("%Polygon ");
147     // Arrows : 0 or 1 repeated twice
148     buf.append(factory.toPicedtString((Arrow)element.getAttribute(LEFT_ARROW)));
149     buf.append(" ");
150     buf.append(factory.toPicedtString((Arrow)element.getAttribute(RIGHT_ARROW)));
151     // PicPolygon points
152     for (int i=element.getFirstPointIndex(); i<=element.getLastPointIndex(); i++){
153       buf.append(element.getPoint(i,null));
154     }
155     // closed state
156     //  if (element.isClosed() && !element.getAttribute(FILL_STYLE)==SOLID))  buf.append(" closed ");
157     if (element.isClosed())  buf.append(" closed");
158     factory.appendPicedtParameterString(buf,element);
159     buf.append(CR_LF);
160   }
161 
162 }