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 org.apache.poi.util.BitField;
021: import org.apache.poi.util.BitFieldFactory;
022: import org.apache.poi.util.LittleEndian;
023: import java.util.Arrays;
024:
025: /**
026: * FFN - Font Family Name. FFN is a data structure that stores the names of the Main
027: * Font and that of Alternate font as an array of characters. It has also a header
028: * that stores info about the whole structure and the fonts
029: *
030: * @author Praveen Mathew
031: */
032: public class Ffn {
033: private int _cbFfnM1;//total length of FFN - 1.
034: private byte _info;
035: private static BitField _prq = BitFieldFactory.getInstance(0x0003);// pitch request
036: private static BitField _fTrueType = BitFieldFactory
037: .getInstance(0x0004);// when 1, font is a TrueType font
038: private static BitField _ff = BitFieldFactory.getInstance(0x0070);
039: private short _wWeight;// base weight of font
040: private byte _chs;// character set identifier
041: private byte _ixchSzAlt; // index into ffn.szFfn to the name of
042: // the alternate font
043: private byte[] _panose = new byte[10];//????
044: private byte[] _fontSig = new byte[24];//????
045:
046: // zero terminated string that records name of font, cuurently not
047: // supporting Extended chars
048: private char[] _xszFfn;
049:
050: // extra facilitator members
051: private int _xszFfnLength;
052:
053: public Ffn(byte[] buf, int offset) {
054: int offsetTmp = offset;
055:
056: _cbFfnM1 = LittleEndian.getUnsignedByte(buf, offset);
057: offset += LittleEndian.BYTE_SIZE;
058: _info = buf[offset];
059: offset += LittleEndian.BYTE_SIZE;
060: _wWeight = LittleEndian.getShort(buf, offset);
061: offset += LittleEndian.SHORT_SIZE;
062: _chs = buf[offset];
063: offset += LittleEndian.BYTE_SIZE;
064: _ixchSzAlt = buf[offset];
065: offset += LittleEndian.BYTE_SIZE;
066:
067: // read panose and fs so we can write them back out.
068: System.arraycopy(buf, offset, _panose, 0, _panose.length);
069: offset += _panose.length;
070: System.arraycopy(buf, offset, _fontSig, 0, _fontSig.length);
071: offset += _fontSig.length;
072:
073: offsetTmp = offset - offsetTmp;
074: _xszFfnLength = (this .getSize() - offsetTmp) / 2;
075: _xszFfn = new char[_xszFfnLength];
076:
077: for (int i = 0; i < _xszFfnLength; i++) {
078: _xszFfn[i] = (char) LittleEndian.getShort(buf, offset);
079: offset += LittleEndian.SHORT_SIZE;
080: }
081:
082: }
083:
084: public int get_cbFfnM1() {
085: return _cbFfnM1;
086: }
087:
088: public short getWeight() {
089: return _wWeight;
090: }
091:
092: public byte getChs() {
093: return _chs;
094: }
095:
096: public byte[] getPanose() {
097: return _panose;
098: }
099:
100: public byte[] getFontSig() {
101: return _fontSig;
102: }
103:
104: public int getSize() {
105: return (_cbFfnM1 + 1);
106: }
107:
108: public String getMainFontName() {
109: int index = 0;
110: for (; index < _xszFfnLength; index++) {
111: if (_xszFfn[index] == '\0') {
112: break;
113: }
114: }
115: return new String(_xszFfn, 0, index);
116: }
117:
118: public String getAltFontName() {
119: int index = _ixchSzAlt;
120: for (; index < _xszFfnLength; index++) {
121: if (_xszFfn[index] == '\0') {
122: break;
123: }
124: }
125: return new String(_xszFfn, _ixchSzAlt, index);
126:
127: }
128:
129: public void set_cbFfnM1(int _cbFfnM1) {
130: this ._cbFfnM1 = _cbFfnM1;
131: }
132:
133: // changed protected to public
134: public byte[] toByteArray() {
135: int offset = 0;
136: byte[] buf = new byte[this .getSize()];
137:
138: buf[offset] = (byte) _cbFfnM1;
139: offset += LittleEndian.BYTE_SIZE;
140: buf[offset] = _info;
141: offset += LittleEndian.BYTE_SIZE;
142: LittleEndian.putShort(buf, offset, _wWeight);
143: offset += LittleEndian.SHORT_SIZE;
144: buf[offset] = _chs;
145: offset += LittleEndian.BYTE_SIZE;
146: buf[offset] = _ixchSzAlt;
147: offset += LittleEndian.BYTE_SIZE;
148:
149: System.arraycopy(_panose, 0, buf, offset, _panose.length);
150: offset += _panose.length;
151: System.arraycopy(_fontSig, 0, buf, offset, _fontSig.length);
152: offset += _fontSig.length;
153:
154: for (int i = 0; i < _xszFfn.length; i++) {
155: LittleEndian.putShort(buf, offset, (short) _xszFfn[i]);
156: offset += LittleEndian.SHORT_SIZE;
157: }
158:
159: return buf;
160:
161: }
162:
163: public boolean equals(Object o) {
164: boolean retVal = true;
165:
166: if (((Ffn) o).get_cbFfnM1() == _cbFfnM1) {
167: if (((Ffn) o)._info == _info) {
168: if (((Ffn) o)._wWeight == _wWeight) {
169: if (((Ffn) o)._chs == _chs) {
170: if (((Ffn) o)._ixchSzAlt == _ixchSzAlt) {
171: if (Arrays.equals(((Ffn) o)._panose,
172: _panose)) {
173: if (Arrays.equals(((Ffn) o)._fontSig,
174: _fontSig)) {
175: if (!(Arrays.equals(
176: ((Ffn) o)._xszFfn, _xszFfn)))
177: retVal = false;
178: } else
179: retVal = false;
180: } else
181: retVal = false;
182: } else
183: retVal = false;
184: } else
185: retVal = false;
186: } else
187: retVal = false;
188: } else
189: retVal = false;
190: } else
191: retVal = false;
192:
193: return retVal;
194: }
195:
196: }
|