001: /* ====================================================================
002: Licensed to the Apache Software Foundation (ASF) under one or more
003: contributor license agreements. See the NOTICE file distributed with
004: this work for additional information regarding copyright ownership.
005: The ASF licenses this file to You under the Apache License, Version 2.0
006: (the "License"); you may not use this file except in compliance with
007: the License. You may obtain a copy of the License at
008:
009: http://www.apache.org/licenses/LICENSE-2.0
010:
011: Unless required by applicable law or agreed to in writing, software
012: distributed under the License is distributed on an "AS IS" BASIS,
013: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: See the License for the specific language governing permissions and
015: limitations under the License.
016: ==================================================================== */
017:
018: package org.apache.poi.hwpf.model;
019:
020: import java.io.IOException;
021: import org.apache.poi.hwpf.model.io.HWPFFileSystem;
022: import org.apache.poi.hwpf.model.io.HWPFOutputStream;
023: import org.apache.poi.util.LittleEndian;
024:
025: /**
026: * FontTable or in MS terminology sttbfffn is a common data structure written in all
027: * Word files. The sttbfffn is an sttbf where each string is an FFN structure instead
028: * of pascal-style strings. An sttbf is a string Table stored in file. Thus sttbffn
029: * is like an Sttbf with an array of FFN structures that stores the font name strings
030: *
031: * @author Praveen Mathew
032: */
033: public class FontTable {
034: private short _stringCount;// how many strings are included in the string table
035: private short _extraDataSz;// size in bytes of the extra data
036:
037: // added extra facilitator members
038: private int lcbSttbfffn;// count of bytes in sttbfffn
039: private int fcSttbfffn;// table stream offset for sttbfffn
040:
041: // FFN structure containing strings of font names
042: private Ffn[] _fontNames = null;
043:
044: public FontTable(byte[] buf, int offset, int lcbSttbfffn) {
045: this .lcbSttbfffn = lcbSttbfffn;
046: this .fcSttbfffn = offset;
047:
048: _stringCount = LittleEndian.getShort(buf, offset);
049: offset += LittleEndian.SHORT_SIZE;
050: _extraDataSz = LittleEndian.getShort(buf, offset);
051: offset += LittleEndian.SHORT_SIZE;
052:
053: _fontNames = new Ffn[_stringCount]; //Ffn corresponds to a Pascal style String in STTBF.
054:
055: for (int i = 0; i < _stringCount; i++) {
056: _fontNames[i] = new Ffn(buf, offset);
057: offset += _fontNames[i].getSize();
058: }
059: }
060:
061: public short getStringCount() {
062: return _stringCount;
063: }
064:
065: public short getExtraDataSz() {
066: return _extraDataSz;
067: }
068:
069: public Ffn[] getFontNames() {
070: return _fontNames;
071: }
072:
073: public int getSize() {
074: return lcbSttbfffn;
075: }
076:
077: public String getMainFont(int chpFtc) {
078: if (chpFtc >= _stringCount) {
079: System.out.println("Mismatch in chpFtc with stringCount");
080: return null;
081: }
082:
083: return _fontNames[chpFtc].getMainFontName();
084: }
085:
086: public String getAltFont(int chpFtc) {
087: if (chpFtc >= _stringCount) {
088: System.out.println("Mismatch in chpFtc with stringCount");
089: return null;
090: }
091:
092: return _fontNames[chpFtc].getAltFontName();
093: }
094:
095: public void setStringCount(short stringCount) {
096: this ._stringCount = stringCount;
097: }
098:
099: public void writeTo(HWPFFileSystem sys) throws IOException {
100: HWPFOutputStream tableStream = sys.getStream("1Table");
101:
102: byte[] buf = new byte[LittleEndian.SHORT_SIZE];
103: LittleEndian.putShort(buf, _stringCount);
104: tableStream.write(buf);
105: LittleEndian.putShort(buf, _extraDataSz);
106: tableStream.write(buf);
107:
108: for (int i = 0; i < _fontNames.length; i++) {
109: tableStream.write(_fontNames[i].toByteArray());
110: }
111:
112: }
113:
114: public boolean equals(Object o) {
115: boolean retVal = true;
116:
117: if (((FontTable) o).getStringCount() == _stringCount) {
118: if (((FontTable) o).getExtraDataSz() == _extraDataSz) {
119: Ffn[] fontNamesNew = ((FontTable) o).getFontNames();
120: for (int i = 0; i < _stringCount; i++) {
121: if (!(_fontNames[i].equals(fontNamesNew[i])))
122: retVal = false;
123: }
124: } else
125: retVal = false;
126: } else
127: retVal = false;
128:
129: return retVal;
130: }
131:
132: }
|