aboutsummaryrefslogtreecommitdiffstats
path: root/poi-scratchpad/src/main/java/org/apache/poi/hwpf/model/Ffn.java
blob: 3f925f862651b9a9bad73d256c6e5ea57fafed0a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/* ====================================================================
   Licensed to the Apache Software Foundation (ASF) under one or more
   contributor license agreements.  See the NOTICE file distributed with
   this work for additional information regarding copyright ownership.
   The ASF licenses this file to You under the Apache License, Version 2.0
   (the "License"); you may not use this file except in compliance with
   the License.  You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
==================================================================== */

package org.apache.poi.hwpf.model;

import java.util.Arrays;

import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.util.BitField;
import org.apache.poi.util.BitFieldFactory;
import org.apache.poi.util.IOUtils;
import org.apache.poi.util.Internal;
import org.apache.poi.util.LittleEndian;
import org.apache.poi.util.LittleEndianConsts;

/**
 * FFN - Font Family Name. FFN is a data structure that stores the names of the Main
 * Font and that of Alternate font as an array of characters. It has also a header
 * that stores info about the whole structure and the fonts
 */
@Internal
public final class Ffn {

    private int _cbFfnM1;//total length of FFN - 1.
    private byte _info;
    private static BitField _prq = BitFieldFactory.getInstance(0x0003);// pitch request
    private static BitField _fTrueType = BitFieldFactory.getInstance(0x0004);// when 1, font is a TrueType font
    private static BitField _ff = BitFieldFactory.getInstance(0x0070);
    private short _wWeight;// base weight of font
    private byte _chs;// character set identifier
    private byte _ixchSzAlt;  // index into ffn.szFfn to the name of
    // the alternate font
    private byte[] _panose = new byte[10];//????
    private byte[] _fontSig = new byte[24];//????

    // zero terminated string that records name of font, cuurently not
    // supporting Extended chars
    private char[] _xszFfn;

    // extra facilitator members
    private int _xszFfnLength;

    public Ffn(byte[] buf, int offset) {
        int offsetTmp = offset;

        _cbFfnM1 = LittleEndian.getUByte(buf, offset);
        offset += LittleEndianConsts.BYTE_SIZE;
        _info = buf[offset];
        offset += LittleEndianConsts.BYTE_SIZE;
        _wWeight = LittleEndian.getShort(buf, offset);
        offset += LittleEndianConsts.SHORT_SIZE;
        _chs = buf[offset];
        offset += LittleEndianConsts.BYTE_SIZE;
        _ixchSzAlt = buf[offset];
        offset += LittleEndianConsts.BYTE_SIZE;

        // read panose and fs so we can write them back out.
        System.arraycopy(buf, offset, _panose, 0, _panose.length);
        offset += _panose.length;
        System.arraycopy(buf, offset, _fontSig, 0, _fontSig.length);
        offset += _fontSig.length;

        offsetTmp = offset - offsetTmp;
        _xszFfnLength = (this.getSize() - offsetTmp) / 2;

        if (_xszFfnLength < 0) {
            throw new IllegalArgumentException("Had invalid computed size: " + _xszFfnLength + " with size " + getSize() + " and offsetTmp: " + offsetTmp);
        }

        _xszFfn = new char[_xszFfnLength];

        for (int i = 0; i < _xszFfnLength; i++) {
            _xszFfn[i] = (char) LittleEndian.getShort(buf, offset);
            offset += LittleEndianConsts.SHORT_SIZE;
        }


    }

    public int get_cbFfnM1() {
        return _cbFfnM1;
    }

    public short getWeight() {
        return _wWeight;
    }

    public byte getChs() {
        return _chs;
    }

    public byte[] getPanose() {
        return _panose;
    }

    public byte[] getFontSig() {
        return _fontSig;
    }

    public int getSize() {
        return (_cbFfnM1 + 1);
    }

    public String getMainFontName() {
        int index = 0;
        for (; index < _xszFfnLength; index++) {
            if (_xszFfn[index] == '\0') {
                break;
            }
        }
        return new String(_xszFfn, 0, index);
    }

    public String getAltFontName() {
        int index = _ixchSzAlt;
        for (; index < _xszFfnLength; index++) {
            if (_xszFfn[index] == '\0') {
                break;
            }
        }
        return new String(_xszFfn, _ixchSzAlt, index);

    }

    public void set_cbFfnM1(int _cbFfnM1) {
        this._cbFfnM1 = _cbFfnM1;
    }

    // changed protected to public
    public byte[] toByteArray() {
        int offset = 0;
        byte[] buf = IOUtils.safelyAllocate(this.getSize(), HWPFDocument.getMaxRecordLength());

        buf[offset] = (byte) _cbFfnM1;
        offset += LittleEndianConsts.BYTE_SIZE;
        buf[offset] = _info;
        offset += LittleEndianConsts.BYTE_SIZE;
        LittleEndian.putShort(buf, offset, _wWeight);
        offset += LittleEndianConsts.SHORT_SIZE;
        buf[offset] = _chs;
        offset += LittleEndianConsts.BYTE_SIZE;
        buf[offset] = _ixchSzAlt;
        offset += LittleEndianConsts.BYTE_SIZE;

        System.arraycopy(_panose, 0, buf, offset, _panose.length);
        offset += _panose.length;
        System.arraycopy(_fontSig, 0, buf, offset, _fontSig.length);
        offset += _fontSig.length;

        for (char c : _xszFfn) {
            LittleEndian.putShort(buf, offset, (short) c);
            offset += LittleEndianConsts.SHORT_SIZE;
        }

        return buf;

    }

    @Override
    public boolean equals(Object other) {
        if (!(other instanceof Ffn)) return false;
        Ffn o = (Ffn) other;

        return (
                o._cbFfnM1 == this._cbFfnM1
                        && o._info == this._info
                        && o._wWeight == _wWeight
                        && o._chs == _chs
                        && o._ixchSzAlt == _ixchSzAlt
                        && Arrays.equals(o._panose, _panose)
                        && Arrays.equals(o._fontSig, _fontSig)
                        && Arrays.equals(o._xszFfn, _xszFfn)
        );
    }


    @Override
    public int hashCode() {
        assert false : "hashCode not designed";
        return 42; // any arbitrary constant will do
    }
}