KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openlaszlo > iv > flash > api > text > TextRecord


1 /*
2  * $Id: TextRecord.java,v 1.3 2002/04/15 00:49:45 skavish Exp $
3  *
4  * ==========================================================================
5  *
6  * The JGenerator Software License, Version 1.0
7  *
8  * Copyright (c) 2000 Dmitry Skavish (skavish@usa.net). All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution, if
22  * any, must include the following acknowlegement:
23  * "This product includes software developed by Dmitry Skavish
24  * (skavish@usa.net, http://www.flashgap.com/)."
25  * Alternately, this acknowlegement may appear in the software itself,
26  * if and wherever such third-party acknowlegements normally appear.
27  *
28  * 4. The name "The JGenerator" must not be used to endorse or promote
29  * products derived from this software without prior written permission.
30  * For written permission, please contact skavish@usa.net.
31  *
32  * 5. Products derived from this software may not be called "The JGenerator"
33  * nor may "The JGenerator" appear in their names without prior written
34  * permission of Dmitry Skavish.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL DMITRY SKAVISH OR THE OTHER
40  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  *
49  */

50
51 package org.openlaszlo.iv.flash.api.text;
52
53 import org.openlaszlo.iv.flash.parser.*;
54 import org.openlaszlo.iv.flash.util.*;
55 import org.openlaszlo.iv.flash.api.*;
56 import java.io.*;
57 import java.util.*;
58
59 /**
60  * TextRecord.
61  * <p>
62  * Defines a text of one style.
63  *
64  * @author Dmitry Skavish
65  * @see TextStyleChangeRecord
66  */

67 public final class TextRecord extends FlashItem {
68
69     private int size; // size of the text (number of characters)
70
private char[] text; // the text itself ('size' characters)
71
private int[] indexes; // indexes of the text in font's codetable
72
private int[] advances; // advance values of the text in TWIXELS!
73

74     public TextRecord() {}
75
76     /**
77      * Creates text record of zero size and of specified capacity
78      *
79      * @param maxSize capacity of created text record
80      */

81     public TextRecord( int maxSize ) {
82         text = new char[ maxSize ];
83         indexes = new int[ maxSize ];
84         advances = new int[ maxSize ];
85         size = 0;
86     }
87
88 // public void setText( char[] text ) { this.text = text; }
89
// public void setIndexes( int[] indexes ) { this.indexes = indexes; }
90
// public void setAdvances( int[] advances ) { this.advances = advances; }
91

92     public char[] getText() { return text; }
93     public int[] getIndexes() { return indexes; }
94     public int[] getAdvances() { return advances; }
95
96     /**
97      * Returns character at specified index
98      *
99      * @param i index of the character to be returned
100      * @return character at specified index
101      */

102     public char getChar( int i ) {
103         return text[i];
104     }
105
106     /**
107      * Returns character's index at specified index
108      *
109      * @param i index of the character's index to be returned
110      * @return character's index at specified index
111      */

112     public int getIndex( int i ) {
113         return indexes[i];
114     }
115
116     /**
117      * Returns advance value at specified index
118      *
119      * @param i index of the advance value to be returned
120      * @return advance value at specified index
121      */

122     public int getAdvance( int i ) {
123         return advances[i];
124     }
125
126     /**
127      * Sets new character at specified index
128      *
129      * @param i index of the character to be set
130      * @param ch character to be set
131      */

132     public void setChar( int i, char ch ) {
133         text[i] = ch;
134     }
135
136     /**
137      * Sets new character's index at specified index
138      *
139      * @param i index of the character's index to be set
140      * @param index character's index to be set
141      */

142     public void setIndex( int i, int index ) {
143         indexes[i] = index;
144     }
145
146     /**
147      * Sets new advance value at specified index
148      *
149      * @param i index of the advance value to be set
150      * @param ch advance value to be set
151      */

152     public void setAdvance( int i, int advance ) {
153         advances[i] = advance;
154     }
155
156     /**
157      * Returns size of the text
158      *
159      * @return size of the text
160      */

161     public int getSize() {
162         return size;
163     }
164
165     /**
166      * Sets new size of the text
167      *
168      * @param size new size of the text
169      */

170     public void setSize( int size ) {
171         this.size = size;
172     }
173
174     /**
175      * Returns width of the text in twixels
176      *
177      * @return width of the text in twixels
178      */

179     public int getWidth() {
180         int width = 0;
181         for( int i=0; i<size; i++ ) {
182             width += advances[i];
183         }
184         return width;
185     }
186
187     /**
188      * Updates indexes of this record from specified font
189      *
190      * @param font font to update from
191      */

192     public void updateIndexes( Font font ) {
193         for( int i=0; i<size; i++ ) {
194             char ch = text[i];
195             //System.out.print( "text["+i+"]='"+ch+"'" );
196
int idx = font.getIndex(ch);
197             //System.out.println( ", new index="+idx );
198
if( idx == -1 ) idx = 0; // just for safety
199
indexes[i] = idx;
200         }
201     }
202
203     /**
204      * Adds new index and advance value.
205      * <P>
206      * Does not check for overflow.
207      *
208      * @param index new index to be added
209      * @param advance new advance value to be added
210      */

211     public void add( int index, int advance ) {
212         indexes[size] = index;
213         advances[size] = advance;
214         size++;
215     }
216
217     /**
218      * Adds new character, index and advance value.
219      * <P>
220      * Does not check for overflow.
221      *
222      * @param ch new character to be added
223      * @param index new index to be added
224      * @param advance new advance value to be added
225      */

226     public void add( char ch, int index, int advance ) {
227         text[size] = ch;
228         add( index, advance );
229     }
230
231     /**
232      * Trims this record from the end
233      * <P>
234      * Removes all spaces from the end
235      *
236      * @return width in twixels of all removed spaces
237      */

238     public int trimEnd() {
239         int i;
240         int w = 0;
241         for( i=size; --i>=0; ) {
242             if( !Character.isWhitespace(text[i]) ) break;
243             w += advances[i];
244         }
245         size = i+1;
246         return w;
247     }
248
249     /**
250      * Trims this record from the start
251      * <P>
252      * Removes all spaces from the start
253      *
254      * @return width in twixels of all removed spaces
255      */

256     public int trimStart() {
257         int i;
258         int w = 0;
259         for( i=0; i<size; i++ ) {
260             if( !Character.isWhitespace(text[i]) ) break;
261             w += advances[i];
262         }
263         if( i != 0 ) {
264             size -= i;
265             System.arraycopy( text, i, text, 0, size );
266             System.arraycopy( advances, i, advances, 0, size );
267             System.arraycopy( indexes, i, indexes, 0, size );
268         }
269         return w;
270     }
271
272     /**
273      * Returns maximum index
274      *
275      * @return maximum index
276      */

277     public int getMaxIndex() {
278         return Util.getMax(indexes, size);
279     }
280
281     /**
282      * Returns maximum advance value
283      *
284      * @return maximum advance value
285      */

286     public int getMaxAdvance() {
287         return Util.getMax(advances, size);
288     }
289
290     /**
291      * Writes this text record to flash buffer.
292      * <P>
293      * Expects two-element integer array in userdata of flashbuffer.
294      * First element is number of glyph bits and second - number of
295      * advance value bits.
296      *
297      * @param fob flashbuffer to write to
298      */

299     public void write( FlashOutput fob ) {
300         int[] nbits = (int[]) fob.getUserData();
301         int nGlyphBits = nbits[0];
302         int nAdvanceBits = nbits[1];
303         fob.writeByte(size);
304         for( int k=0; k<size; k++ ) {
305             fob.writeBits(indexes[k], nGlyphBits);
306             fob.writeBits(advances[k], nAdvanceBits);
307         }
308         fob.flushBits();
309     }
310
311     public void printContent( PrintStream out, String JavaDoc indent ) {
312         out.println( indent+"TextRecord: nGlyphs="+size );
313         for( int k=0; k<size; k++ ) {
314             out.println( indent+" char["+k+"]='"+text[k]+"', advance["+k+"]="+advances[k] );
315         }
316     }
317
318     protected FlashItem copyInto( FlashItem item, ScriptCopier copier ) {
319         char[] txt = new char[text.length];
320         System.arraycopy(text, 0, txt, 0, text.length);
321         int[] ind = new int[indexes.length];
322         System.arraycopy(indexes, 0, ind, 0, indexes.length);
323         int[] adv = new int[advances.length];
324         System.arraycopy(advances, 0, adv, 0, advances.length);
325         ((TextRecord)item).size = size;
326         ((TextRecord)item).text = txt;
327         ((TextRecord)item).indexes = ind;
328         ((TextRecord)item).advances = adv;
329         return item;
330     }
331
332     public FlashItem getCopy( ScriptCopier copier ) {
333         return copyInto( new TextRecord(), copier );
334     }
335 }
336
337
Popular Tags