KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > hwpf > model > FontTable


1
2 /* ====================================================================
3    Copyright 2002-2004 Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16 ==================================================================== */

17         
18
19 package org.apache.poi.hwpf.model;
20
21 import java.io.IOException JavaDoc;
22 import org.apache.poi.hwpf.model.io.HWPFFileSystem;
23 import org.apache.poi.hwpf.model.io.HWPFOutputStream;
24 import org.apache.poi.util.LittleEndian;
25
26 /**
27  * FontTable or in MS terminology sttbfffn is a common data structure written in all
28  * Word files. The sttbfffn is an sttbf where each string is an FFN structure instead
29  * of pascal-style strings. An sttbf is a string Table stored in file. Thus sttbffn
30  * is like an Sttbf with an array of FFN structures that stores the font name strings
31  *
32  * @author Praveen Mathew
33  */

34 public class FontTable
35 {
36   private short _stringCount;// how many strings are included in the string table
37
private short _extraDataSz;// size in bytes of the extra data
38

39   // added extra facilitator members
40
private int lcbSttbfffn;// count of bytes in sttbfffn
41
private int fcSttbfffn;// table stream offset for sttbfffn
42

43   // FFN structure containing strings of font names
44
private Ffn[] _fontNames = null;
45
46
47   public FontTable(byte[] buf, int offset, int lcbSttbfffn)
48   {
49     this.lcbSttbfffn = lcbSttbfffn;
50     this.fcSttbfffn = offset;
51
52     _stringCount = LittleEndian.getShort(buf, offset);
53     offset += LittleEndian.SHORT_SIZE;
54     _extraDataSz = LittleEndian.getShort(buf, offset);
55     offset += LittleEndian.SHORT_SIZE;
56
57     _fontNames = new Ffn[_stringCount]; //Ffn corresponds to a Pascal style String in STTBF.
58

59     for(int i = 0;i<_stringCount; i++)
60     {
61       _fontNames[i] = new Ffn(buf,offset);
62       offset += _fontNames[i].getSize();
63     }
64   }
65
66   public short getStringCount()
67   {
68     return _stringCount;
69   }
70
71   public short getExtraDataSz()
72   {
73     return _extraDataSz;
74   }
75
76   public Ffn[] getFontNames()
77   {
78     return _fontNames;
79   }
80
81   public int getSize()
82   {
83     return lcbSttbfffn;
84   }
85
86   public String JavaDoc getMainFont(int chpFtc )
87   {
88     if(chpFtc >= _stringCount)
89     {
90       System.out.println("Mismatch in chpFtc with stringCount");
91       return null;
92     }
93
94     return _fontNames[chpFtc].getMainFontName();
95   }
96
97   public String JavaDoc getAltFont(int chpFtc )
98   {
99     if(chpFtc >= _stringCount)
100     {
101       System.out.println("Mismatch in chpFtc with stringCount");
102       return null;
103     }
104
105     return _fontNames[chpFtc].getAltFontName();
106   }
107
108   public void setStringCount(short stringCount)
109   {
110     this._stringCount = stringCount;
111   }
112
113   public void writeTo(HWPFFileSystem sys)
114       throws IOException JavaDoc
115   {
116       HWPFOutputStream tableStream = sys.getStream("1Table");
117
118       byte[] buf = new byte[LittleEndian.SHORT_SIZE];
119       LittleEndian.putShort(buf, _stringCount);
120       tableStream.write(buf);
121       LittleEndian.putShort(buf, _extraDataSz);
122       tableStream.write(buf);
123
124       for(int i = 0; i < _fontNames.length; i++)
125       {
126           tableStream.write(_fontNames[i].toByteArray());
127       }
128
129   }
130
131   public boolean equals(Object JavaDoc o)
132   {
133     boolean retVal = true;
134
135     if(((FontTable)o).getStringCount() == _stringCount)
136     {
137       if(((FontTable)o).getExtraDataSz() == _extraDataSz)
138       {
139         Ffn[] fontNamesNew = ((FontTable)o).getFontNames();
140         for(int i = 0;i<_stringCount; i++)
141         {
142           if(!(_fontNames[i].equals(fontNamesNew[i])))
143             retVal = false;
144         }
145       }
146       else
147         retVal = false;
148     }
149     else
150         retVal = false;
151
152
153       return retVal;
154   }
155
156
157
158 }
159
160
161
Popular Tags