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.sprm;
019:
020: import org.apache.poi.hwpf.usermodel.TableProperties;
021: import org.apache.poi.util.LittleEndian;
022: import org.apache.poi.hwpf.usermodel.TableCellDescriptor;
023: import org.apache.poi.hwpf.usermodel.ShadingDescriptor;
024: import org.apache.poi.hwpf.usermodel.BorderCode;
025:
026: import java.util.ArrayList;
027: import java.util.Arrays;
028:
029: public class TableSprmCompressor {
030: public TableSprmCompressor() {
031: }
032:
033: public static byte[] compressTableProperty(TableProperties newTAP) {
034: int size = 0;
035: ArrayList sprmList = new ArrayList();
036:
037: if (newTAP.getJc() != 0) {
038: size += SprmUtils.addSprm((short) 0x5400, newTAP.getJc(),
039: null, sprmList);
040: }
041: if (newTAP.getFCantSplit()) {
042: size += SprmUtils
043: .addSprm((short) 0x3403, 1, null, sprmList);
044: }
045: if (newTAP.getFTableHeader()) {
046: size += SprmUtils
047: .addSprm((short) 0x3404, 1, null, sprmList);
048: }
049: byte[] brcBuf = new byte[6 * BorderCode.SIZE];
050: int offset = 0;
051: newTAP.getBrcTop().serialize(brcBuf, offset);
052: offset += BorderCode.SIZE;
053: newTAP.getBrcLeft().serialize(brcBuf, offset);
054: offset += BorderCode.SIZE;
055: newTAP.getBrcBottom().serialize(brcBuf, offset);
056: offset += BorderCode.SIZE;
057: newTAP.getBrcRight().serialize(brcBuf, offset);
058: offset += BorderCode.SIZE;
059: newTAP.getBrcHorizontal().serialize(brcBuf, offset);
060: offset += BorderCode.SIZE;
061: newTAP.getBrcVertical().serialize(brcBuf, offset);
062: byte[] compare = new byte[6 * BorderCode.SIZE];
063: if (!Arrays.equals(brcBuf, compare)) {
064: size += SprmUtils.addSprm((short) 0xD605, 0, brcBuf,
065: sprmList);
066: }
067: if (newTAP.getDyaRowHeight() != 0) {
068: size += SprmUtils.addSprm((short) 0x9407, newTAP
069: .getDyaRowHeight(), null, sprmList);
070: }
071: if (newTAP.getItcMac() > 0) {
072: int itcMac = newTAP.getItcMac();
073: byte[] buf = new byte[1
074: + (LittleEndian.SHORT_SIZE * (itcMac + 1))
075: + (TableCellDescriptor.SIZE * itcMac)];
076: buf[0] = (byte) itcMac;
077:
078: short[] dxaCenters = newTAP.getRgdxaCenter();
079: for (int x = 0; x < dxaCenters.length; x++) {
080: LittleEndian.putShort(buf,
081: 1 + (x * LittleEndian.SHORT_SIZE),
082: dxaCenters[x]);
083: }
084:
085: TableCellDescriptor[] cellDescriptors = newTAP.getRgtc();
086: for (int x = 0; x < cellDescriptors.length; x++) {
087: cellDescriptors[x].serialize(buf, 1
088: + ((itcMac + 1) * LittleEndian.SHORT_SIZE)
089: + (x * TableCellDescriptor.SIZE));
090: }
091: size += SprmUtils.addSpecialSprm((short) 0xD608, buf,
092: sprmList);
093:
094: // buf = new byte[(itcMac * ShadingDescriptor.SIZE) + 1];
095: // buf[0] = (byte)itcMac;
096: // ShadingDescriptor[] shds = newTAP.getRgshd();
097: // for (int x = 0; x < itcMac; x++)
098: // {
099: // shds[x].serialize(buf, 1 + (x * ShadingDescriptor.SIZE));
100: // }
101: // size += SprmUtils.addSpecialSprm((short)0xD609, buf, sprmList);
102: }
103: if (newTAP.getTlp() != 0) {
104: size += SprmUtils.addSprm((short) 0x740a, newTAP.getTlp(),
105: null, sprmList);
106: }
107:
108: return SprmUtils.getGrpprl(sprmList, size);
109: }
110: }
|