summaryrefslogtreecommitdiff
path: root/xmerge/java/org/openoffice/xmerge/converter/xml/sxc/pexcel/records/ExtendedFormat.java
blob: dc4780de9efe666e8523b5705c8f162ef5b90b16 (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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
/************************************************************************
 *
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * Copyright 2000, 2010 Oracle and/or its affiliates.
 *
 * OpenOffice.org - a multi-platform office productivity suite
 *
 * This file is part of OpenOffice.org.
 *
 * OpenOffice.org is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 3
 * only, as published by the Free Software Foundation.
 *
 * OpenOffice.org is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License version 3 for more details
 * (a copy is included in the LICENSE file that accompanied this code).
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3 along with OpenOffice.org.  If not, see
 * <http://www.openoffice.org/license.html>
 * for a copy of the LGPLv3 License.
 *
 ************************************************************************/

package org.openoffice.xmerge.converter.xml.sxc.pexcel.records;

import java.io.DataInputStream;
import java.io.OutputStream;
import java.io.InputStream;
import java.io.IOException;
import java.awt.Color;

import org.openoffice.xmerge.util.Debug;
import org.openoffice.xmerge.util.EndianConverter;
import org.openoffice.xmerge.util.ColourConverter;
import org.openoffice.xmerge.converter.xml.sxc.Format;
import org.openoffice.xmerge.converter.xml.sxc.pexcel.PocketExcelConstants;

/**
 * Represents a BIFF Record descibing extended formatting information
 *
 */
public class ExtendedFormat implements BIFFRecord,
org.openoffice.xmerge.converter.xml.OfficeConstants {

    private byte[] ixfnt        = new byte[2];  // Index to Font Record
    private byte[] ixnf         = new byte[2];
    private byte[] fattributes  = new byte[4];
    private byte[] fBaseAttr    = new byte[2];  // base attribute flags
    private byte[] fTextAttr    = new byte[2];  // text attribute flags
    private byte[] icvFore      = new byte[2];  // Background colour of the cell
    private byte[] icvFill      = new byte[2];
    private byte bRight;                        // Right border Style
    private byte bTop;                          // Top border style
    private byte bLeft;                         // Left border style
    private byte bBottom;                       // Bottom border style
    private byte backstyle;
    private byte borderstyle;

    public static final int TOP_BORDER      = 0x01;
    public static final int LEFT_BORDER     = 0x02;
    public static final int BOTTOM_BORDER   = 0x04;
    public static final int RIGHT_BORDER    = 0x08;

    // Horizontal Alignment Styles
    public static final int NORMAL_ALIGN    = 0x00;
    public static final int LEFT_ALIGN      = 0x01;
    public static final int CENTER_ALIGN    = 0x02;
    public static final int RIGHT_ALIGN     = 0x03;

    // Vertical Alignment Styles
    public static final int TOP_ALIGN       = 0x10;
    public static final int MIDDLE_ALIGN    = 0x20;
    public static final int BOTTOM_ALIGN    = 0x30;

    public static final int WORD_WRAP       = 0x08;

    /**
      * Constructs an <code>ExtendedFormat</code> from the
      * <code>InputStream</code>
      *
      * @param  is <code>InputStream</code> to read from
      */
    public ExtendedFormat(InputStream is) throws IOException {
        read(is);
    }

    /**
      * Constructs a pocket Excel Document using defualt values and sets the
     * font index using the specified attribute
      *
      * @param  ixfnt   index of the font this format should use
      */
    public ExtendedFormat(int ixfnt, Format fmt) {

        this.ixfnt          = EndianConverter.writeShort((short)ixfnt);
        String category = fmt.getCategory();
        if(category.equalsIgnoreCase(CELLTYPE_CURRENCY)) {
            this.ixnf           = EndianConverter.writeShort((short) 0);
        } else if(category.equalsIgnoreCase(CELLTYPE_DATE)) {
            this.ixnf           = EndianConverter.writeShort((short) 0x12);
        } else if(category.equalsIgnoreCase(CELLTYPE_TIME)) {
            this.ixnf           = EndianConverter.writeShort((short) 0x1E);
        } else {
            this.ixnf           = EndianConverter.writeShort((short) 0);
        }
        this.fattributes    = new byte[] {(byte)0xFF,(byte)0xFF,(byte)0xFF,(byte)0xFF};
        this.fBaseAttr      = new byte[] {(byte)0x02,(byte)0x00};

        this.fTextAttr      = new byte[] {(byte)0x00, (byte)0x00};

        int align = fmt.getAlign();

        // Horizontal alignment
        if(align==Format.CENTER_ALIGN) {
            fTextAttr[0] |= CENTER_ALIGN;
        } else if(align==Format.LEFT_ALIGN) {
            fTextAttr[0] |= LEFT_ALIGN;
        } else if(align==Format.RIGHT_ALIGN) {
            fTextAttr[0] |= RIGHT_ALIGN;
        } else {
            fTextAttr[0] |= NORMAL_ALIGN;
        }

        int vertAlign = fmt.getVertAlign();

        // Vertical alignment
        if(vertAlign==Format.TOP_ALIGN) {
            fTextAttr[0] |= TOP_ALIGN;
        } else if(vertAlign==Format.BOTTOM_ALIGN) {
            fTextAttr[0] |= BOTTOM_ALIGN;
        } else if(vertAlign==Format.MIDDLE_ALIGN) {
            fTextAttr[0] |= MIDDLE_ALIGN;
        } else {
            fTextAttr[0] |= BOTTOM_ALIGN;
        }

        if(fmt.getAttribute(Format.WORD_WRAP)) {
            fTextAttr[0] |= WORD_WRAP;
        }

        if(fmt.getAttribute(Format.LEFT_BORDER)) {
            fTextAttr[1] |= LEFT_BORDER;
        }
        if(fmt.getAttribute(Format.RIGHT_BORDER)) {
            fTextAttr[1] |= RIGHT_BORDER;
        }
        if(fmt.getAttribute(Format.TOP_BORDER)) {
            fTextAttr[1] |= TOP_BORDER;
        }
        if(fmt.getAttribute(Format.BOTTOM_BORDER)) {
            fTextAttr[1] |= BOTTOM_BORDER;
        }

        Color background = fmt.getBackground();
        if( background != null ) {
            ColourConverter cc = new ColourConverter(PocketExcelConstants.cLookup);
            icvFill = EndianConverter.writeShort(cc.convertFromRGB(background));
        } else {
            icvFill     = new byte[] {(byte)0xFF,(byte)0x00};
        }

        icvFore     = new byte[] {(byte)0xFF,(byte)0x00};

        bRight      = (byte) 0xFF;
        bTop        = (byte) 0xFF;
        bLeft       = (byte) 0xFF;
        bBottom     = (byte) 0xFF;
        backstyle   = (byte) 0x00;
        borderstyle = (byte) 0x00;

    }

    /**
     * Get the font index this format uses
     *
     * @return the font index
     */
    public int getFontIndex() {
        return EndianConverter.readShort(ixfnt);
    }

    /**
     * Get the font index this format uses
     *
     * @return the font index
     */
    public int getFormatIndex() {
        return EndianConverter.readShort(ixnf);
    }

    /**
     * Get the font index this format uses
     *
     * @return the font index
     */
    public int getTextAttr() {
        return EndianConverter.readShort(fTextAttr);
    }

    /**
     * Get the background color this format uses
     *
     * @return the background color
     */
    public Color getBackground() {
        short rgb = EndianConverter.readShort(icvFill);
        Color c = null;
        if(rgb!=0xFF) {
            ColourConverter cc = new ColourConverter(PocketExcelConstants.cLookup);
            c = cc.convertToRGB(rgb);
        }
        return c;
    }

    /**
     * Get the Vertical alignment for this Format
     *
     * @return the alignment
     */
    public int getVertAlign() {

        int mask = MIDDLE_ALIGN | BOTTOM_ALIGN | TOP_ALIGN;
        int masked = fTextAttr[0] & mask;

        if(masked == MIDDLE_ALIGN)
            return Format.MIDDLE_ALIGN;

        if(masked == BOTTOM_ALIGN)
            return Format.BOTTOM_ALIGN;

        if(masked == TOP_ALIGN)
            return Format.TOP_ALIGN;

        return Format.BOTTOM_ALIGN;
    }

    /**
     * Get the alignment for this Format
     *
     * @return the alignment
     */
    public int getAlign() {

        int mask = LEFT_ALIGN | CENTER_ALIGN | RIGHT_ALIGN;
        int masked = fTextAttr[0] & mask;

        if(masked == MIDDLE_ALIGN)
            return Format.LEFT_ALIGN;

        if(masked == CENTER_ALIGN)
            return Format.CENTER_ALIGN;

        if(masked == RIGHT_ALIGN)
            return Format.RIGHT_ALIGN;

        return Format.LEFT_ALIGN;
    }

    /**
     * Is the word wrap set
     *
     * @return true if it is selected
     */
    public boolean isWordWrap() {
        return (!((fTextAttr[0] & WORD_WRAP) == 0));
    }
    /**
     * Get the border style
     *
     * @param side the side to test
     * @return true if it is selected
     */
    public boolean isBorder(int side) {
        return (!((fTextAttr[1] & side) == 0));
    }

    /**
     * Compare two ExtendedFormat to see if the font index is the same
     *
     * @param the ExtendedFormat to be used in the comaprison
     * @return boolean if the two are the same otherwise false
     */
    public boolean compareTo(ExtendedFormat rhs) {

        if(EndianConverter.readShort(icvFill) !=
        EndianConverter.readShort(rhs.icvFill))
            return false;

        if(this.getTextAttr() != rhs.getTextAttr())
            return false;

        if(this.getVertAlign() != rhs.getVertAlign())
            return false;

        if(this.getAlign() != rhs.getAlign())
            return false;

        if (this.getFontIndex() != rhs.getFontIndex())
            return false;

        if (this.getFormatIndex() != rhs.getFormatIndex())
            return false;

        return true;
    }

    /**
     * Get the hex code for this particular <code>BIFFRecord</code>
     *
     * @return the hex code for <code>ExtendedFormat</code>
     */
    public short getBiffType() {
        return PocketExcelConstants.EXTENDED_FORMAT;
    }

    /**
      * Reads the extended format from the <code>Inputstream</code>
      *
      * @param  input the <code>Inputstream</code>to read
      * @return toal number of bytes read
      */
    public int read(InputStream input) throws IOException {

        int numOfBytesRead  = input.read(ixfnt);
        numOfBytesRead  += input.read(ixnf);
        numOfBytesRead  += input.read(fattributes);
        numOfBytesRead  += input.read(fBaseAttr);
        numOfBytesRead  += input.read(fTextAttr);
        numOfBytesRead  += input.read(icvFore);
        numOfBytesRead  += input.read(icvFill);
        bRight      = (byte) input.read();
        bTop        = (byte) input.read();
        bLeft       = (byte) input.read();
        bBottom     = (byte) input.read();
        backstyle   = (byte) input.read();
        borderstyle = (byte) input.read();
        numOfBytesRead += 6;

        Debug.log(Debug.TRACE,"\tixfnt : "+ EndianConverter.readShort(ixfnt) +
                            " ixnf : " + EndianConverter.readShort(ixnf) +
                            " fattributes : " + EndianConverter.readInt(fattributes) +
                            " fBaseAttr : " + EndianConverter.readShort(fBaseAttr) +
                            "\n\tfTextAttr : " + EndianConverter.readShort(fTextAttr) +
                            " icvFore : " + EndianConverter.readShort(icvFore) +
                            " icvFill : " + EndianConverter.readShort(icvFill) +
                            " bRight : " + bRight +
                            "\n\tbTop : " + bTop +
                            " bLeft : " + bLeft +
                            " bBottom : " + bBottom +
                            " backstyle : " + backstyle +
                            " borderstyle : " + borderstyle);
        return numOfBytesRead;
    }

    /**
      * Writes the ExtendedFormat to the <code>Outputstream<code>
      *
      * @param  output the <code>Outputstream</code>to write to
      */
    public void write(OutputStream output) throws IOException {

        output.write(getBiffType());
        output.write(ixfnt);
        output.write(ixnf);
        output.write(fattributes);
        output.write(fBaseAttr);
        output.write(fTextAttr);
        output.write(icvFore);
        output.write(icvFill);
        output.write(bRight);
        output.write(bTop);
        output.write(bLeft);
        output.write(bBottom);
        output.write(backstyle);
        output.write(borderstyle);

        Debug.log(Debug.TRACE,"Writing ExtendedFormat record");

    }

}