KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > fo > FOText


1 /*
2  * $Id: FOText.java,v 1.24.2.8 2003/03/02 13:04:15 pietsch Exp $
3  * ============================================================================
4  * The Apache Software License, Version 1.1
5  * ============================================================================
6  *
7  * Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without modifica-
10  * tion, are permitted provided that the following conditions are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright notice,
16  * this list of conditions and the following disclaimer in the documentation
17  * and/or other materials provided with the distribution.
18  *
19  * 3. The end-user documentation included with the redistribution, if any, must
20  * include the following acknowledgment: "This product includes software
21  * developed by the Apache Software Foundation (http://www.apache.org/)."
22  * Alternately, this acknowledgment may appear in the software itself, if
23  * and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. The names "FOP" and "Apache Software Foundation" must not be used to
26  * endorse or promote products derived from this software without prior
27  * written permission. For written permission, please contact
28  * apache@apache.org.
29  *
30  * 5. Products derived from this software may not be called "Apache", nor may
31  * "Apache" appear in their name, without prior written permission of the
32  * Apache Software Foundation.
33  *
34  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
35  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
36  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
37  * APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
38  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
39  * DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
40  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
41  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
42  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
43  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44  * ============================================================================
45  *
46  * This software consists of voluntary contributions made by many individuals
47  * on behalf of the Apache Software Foundation and was originally created by
48  * James Tauber <jtauber@jtauber.com>. For more information on the Apache
49  * Software Foundation, please see <http://www.apache.org/>.
50  */

51 package org.apache.fop.fo;
52
53 // FOP
54
import org.apache.fop.layout.Area;
55 import org.apache.fop.layout.BlockArea;
56 import org.apache.fop.layout.FontState;
57 import org.apache.fop.layout.*;
58 import org.apache.fop.datatypes.*;
59 import org.apache.fop.fo.properties.*;
60 import org.apache.fop.apps.FOPException;
61
62 /**
63  * a text node in the formatting object tree
64  *
65  */

66 public class FOText extends FONode {
67
68     private char[] ca;
69
70     private FontState fs;
71     private float red;
72     private float green;
73     private float blue;
74     private int wrapOption;
75     private int whiteSpaceCollapse;
76     private int verticalAlign;
77
78     // Textdecoration
79
private TextState ts;
80
81     public FOText(StringBuffer JavaDoc b, FObj parent) {
82         super(parent);
83         this.ca = new char[b.length()];
84         b.getChars(0,b.length(),ca,0);
85     }
86
87     public void setTextState(TextState ts) {
88         this.ts = ts;
89     }
90
91     public boolean willCreateArea() {
92         this.whiteSpaceCollapse =
93             this.parent.properties.get("white-space-collapse").getEnum();
94         if (this.whiteSpaceCollapse == WhiteSpaceCollapse.FALSE
95                 && ca.length > 0) {
96             return true;
97         }
98
99         for (int i = 0; i < ca.length; i++) {
100             char ch = ca[i];
101             if (!((ch == ' ') || (ch == '\n') || (ch == '\r')
102                     || (ch == '\t'))) { // whitespace
103
return true;
104             }
105         }
106         return false;
107     }
108
109     public boolean mayPrecedeMarker() {
110         for (int i = 0; i < ca.length; i++) {
111             char ch = ca[i];
112             if ((ch != ' ') || (ch != '\n') || (ch != '\r')
113                     || (ch != '\t')) { // whitespace
114
return true;
115             }
116         }
117         return false;
118     }
119   
120     public int layout(Area area) throws FOPException {
121         if (!(area instanceof BlockArea)) {
122             log.error("text outside block area"
123                                    + new String JavaDoc(ca, 0, ca.length));
124             return Status.OK;
125         }
126         if (this.marker == START) {
127             String JavaDoc fontFamily =
128                 this.parent.properties.get("font-family").getString();
129             String JavaDoc fontStyle =
130                 this.parent.properties.get("font-style").getString();
131             String JavaDoc fontWeight =
132                 this.parent.properties.get("font-weight").getString();
133             int fontSize =
134                 this.parent.properties.get("font-size").getLength().mvalue();
135             // font-variant support
136
// added by Eric SCHAEFFER
137
int fontVariant =
138                 this.parent.properties.get("font-variant").getEnum();
139
140             int letterSpacing =
141                 this.parent.properties.get("letter-spacing").getLength().mvalue();
142             this.fs = new FontState(area.getFontInfo(), fontFamily,
143                                     fontStyle, fontWeight, fontSize,
144                                     fontVariant, letterSpacing);
145
146             ColorType c = this.parent.properties.get("color").getColorType();
147             this.red = c.red();
148             this.green = c.green();
149             this.blue = c.blue();
150
151             this.verticalAlign =
152                 this.parent.properties.get("vertical-align").getEnum();
153
154             this.wrapOption =
155                 this.parent.properties.get("wrap-option").getEnum();
156             this.whiteSpaceCollapse =
157                 this.parent.properties.get("white-space-collapse").getEnum();
158             this.marker = 0;
159         }
160         int orig_start = this.marker;
161         this.marker = addText((BlockArea)area, fs, red, green, blue,
162                               wrapOption, this.getLinkSet(),
163                               whiteSpaceCollapse, ca, this.marker, ca.length,
164                               ts, verticalAlign);
165         if (this.marker == -1) {
166
167
168             // commented out by Hani Elabed, 11/28/2000
169
// if this object has been laid out
170
// successfully, leave it alone....
171
// Now, to prevent the array index out of
172
// bound of LineArea.addText(), I have added
173
// the following test at the beginning of that method.
174
// if( start == -1 ) return -1;
175
// see LineArea.addText()
176

177             // this.marker = 0;
178
return Status.OK;
179         } else if (this.marker != orig_start) {
180             return Status.AREA_FULL_SOME;
181         } else {
182             return Status.AREA_FULL_NONE;
183         }
184     }
185
186     // font-variant support : addText is a wrapper for addRealText
187
// added by Eric SCHAEFFER
188
public static int addText(BlockArea ba, FontState fontState, float red,
189                               float green, float blue, int wrapOption,
190                               LinkSet ls, int whiteSpaceCollapse,
191                               char data[], int start, int end,
192                               TextState textState, int vAlign) {
193         if (fontState.getFontVariant() == FontVariant.SMALL_CAPS) {
194             FontState smallCapsFontState;
195             try {
196                 int smallCapsFontHeight =
197                     (int)(((double)fontState.getFontSize()) * 0.8d);
198                 smallCapsFontState = new FontState(fontState.getFontInfo(),
199                                                    fontState.getFontFamily(),
200                                                    fontState.getFontStyle(),
201                                                    fontState.getFontWeight(),
202                                                    smallCapsFontHeight,
203                                                    FontVariant.NORMAL);
204             } catch (FOPException ex) {
205                 smallCapsFontState = fontState;
206                 //log.error("Error creating small-caps FontState: "
207
// + ex.getMessage());
208
}
209
210             // parse text for upper/lower case and call addRealText
211
char c;
212             char newdata[] = new char[end];
213             boolean isLowerCase;
214             int caseStart;
215             FontState fontStateToUse;
216             for (int i = start; i < end; ) {
217                 caseStart = i;
218                 c = data[i];
219                 isLowerCase = (java.lang.Character.isLetter(c)
220                                && java.lang.Character.isLowerCase(c));
221                 while (isLowerCase
222                         == (java.lang.Character.isLetter(c)
223                             && java.lang.Character.isLowerCase(c))) {
224                     if (isLowerCase) {
225                         newdata[i] = java.lang.Character.toUpperCase(c);
226                     } else {
227                         newdata[i] = c;
228                     }
229                     i++;
230                     if (i == end)
231                         break;
232                     c = data[i];
233                 }
234                 if (isLowerCase) {
235                     fontStateToUse = smallCapsFontState;
236                 } else {
237                     fontStateToUse = fontState;
238                 }
239                 int index = addRealText(ba, fontStateToUse, red, green, blue,
240                                         wrapOption, ls, whiteSpaceCollapse,
241                                         newdata, caseStart, i, textState,
242                                         vAlign);
243                 if (index != -1) {
244                     return index;
245                 }
246             }
247
248             return -1;
249         }
250
251         // font-variant normal
252
return addRealText(ba, fontState, red, green, blue, wrapOption, ls,
253                            whiteSpaceCollapse, data, start, end, textState,
254                            vAlign);
255     }
256
257     protected static int addRealText(BlockArea ba, FontState fontState,
258                                      float red, float green, float blue,
259                                      int wrapOption, LinkSet ls,
260                                      int whiteSpaceCollapse, char data[],
261                                      int start, int end, TextState textState,
262                                      int vAlign) {
263         LineArea la = ba.getCurrentLineArea();
264         if (la == null) {
265             return start;
266         }
267
268         la.changeFont(fontState);
269         la.changeColor(red, green, blue);
270         la.changeWrapOption(wrapOption);
271         la.changeWhiteSpaceCollapse(whiteSpaceCollapse);
272         la.changeVerticalAlign(vAlign);
273         // la.changeHyphenation(language, country, hyphenate,
274
// hyphenationChar, hyphenationPushCharacterCount,
275
// hyphenationRemainCharacterCount);
276
ba.setupLinkSet(ls);
277
278         start = la.addText(data, start, end, ls, textState);
279
280         while ( start != -1) {
281             la = ba.createNextLineArea();
282             if (la == null) {
283                 return start;
284             }
285             la.changeFont(fontState);
286             la.changeColor(red, green, blue);
287             la.changeWrapOption(wrapOption);
288             la.changeWhiteSpaceCollapse(whiteSpaceCollapse);
289             // la.changeHyphenation(language, country, hyphenate,
290
// hyphenationChar, hyphenationPushCharacterCount,
291
// hyphenationRemainCharacterCount);
292
ba.setupLinkSet(ls);
293
294             start = la.addText(data, start, end, ls, textState);
295         }
296         return -1;
297     }
298
299
300 }
301
Popular Tags