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 java.util.List;
021: import java.util.ArrayList;
022: import java.util.Arrays;
023:
024: import org.apache.poi.util.LittleEndian;
025:
026: import org.apache.poi.hwpf.usermodel.ParagraphProperties;
027:
028: public class ParagraphSprmCompressor {
029: public ParagraphSprmCompressor() {
030: }
031:
032: public static byte[] compressParagraphProperty(
033: ParagraphProperties newPAP, ParagraphProperties oldPAP) {
034: ArrayList sprmList = new ArrayList();
035: int size = 0;
036:
037: if (newPAP.getJc() != oldPAP.getJc()) {
038: size += SprmUtils.addSprm((short) 0x2403, newPAP.getJc(),
039: null, sprmList);
040: }
041: if (newPAP.getFKeep() != oldPAP.getFKeep()) {
042: size += SprmUtils.addSprm((short) 0x2405,
043: newPAP.getFKeep(), null, sprmList);
044: }
045: if (newPAP.getFKeepFollow() != oldPAP.getFKeepFollow()) {
046: size += SprmUtils.addSprm((short) 0x2406, newPAP
047: .getFKeepFollow(), null, sprmList);
048: }
049: if (newPAP.getFPageBreakBefore() != oldPAP
050: .getFPageBreakBefore()) {
051: size += SprmUtils.addSprm((short) 0x2407, newPAP
052: .getFPageBreakBefore(), null, sprmList);
053: }
054: if (newPAP.getBrcl() != oldPAP.getBrcl()) {
055: size += SprmUtils.addSprm((short) 0x2408, newPAP.getBrcl(),
056: null, sprmList);
057: }
058: if (newPAP.getBrcp() != oldPAP.getBrcp()) {
059: size += SprmUtils.addSprm((short) 0x2409, newPAP.getBrcp(),
060: null, sprmList);
061: }
062: if (newPAP.getIlvl() != oldPAP.getIlvl()) {
063: size += SprmUtils.addSprm((short) 0x260A, newPAP.getIlvl(),
064: null, sprmList);
065: }
066: if (newPAP.getIlfo() != oldPAP.getIlfo()) {
067: size += SprmUtils.addSprm((short) 0x460b, newPAP.getIlfo(),
068: null, sprmList);
069: }
070: if (newPAP.getFNoLnn() != oldPAP.getFNoLnn()) {
071: size += SprmUtils.addSprm((short) 0x240C, newPAP
072: .getFNoLnn(), null, sprmList);
073: }
074: if (newPAP.getFSideBySide() != oldPAP.getFSideBySide()) {
075: size += SprmUtils.addSprm((short) 0x2404, newPAP
076: .getFSideBySide(), null, sprmList);
077: }
078: if (newPAP.getFNoAutoHyph() != oldPAP.getFNoAutoHyph()) {
079: size += SprmUtils.addSprm((short) 0x242A, newPAP
080: .getFNoAutoHyph(), null, sprmList);
081: }
082: if (newPAP.getFWidowControl() != oldPAP.getFWidowControl()) {
083: size += SprmUtils.addSprm((short) 0x2431, newPAP
084: .getFWidowControl(), null, sprmList);
085: }
086: if (newPAP.getItbdMac() != oldPAP.getItbdMac()
087: || !Arrays.equals(newPAP.getRgdxaTab(), oldPAP
088: .getRgdxaTab())
089: || !Arrays.equals(newPAP.getRgtbd(), oldPAP.getRgtbd())) {
090: /** @todo revisit this */
091: // byte[] oldTabArray = oldPAP.getRgdxaTab();
092: // byte[] newTabArray = newPAP.getRgdxaTab();
093: // byte[] newTabDescriptors = newPAP.getRgtbd();
094: // byte[] varParam = new byte[2 + oldTabArray.length + newTabArray.length +
095: // newTabDescriptors.length];
096: // varParam[0] = (byte)(oldTabArray.length/2);
097: // int offset = 1;
098: // System.arraycopy(oldTabArray, 0, varParam, offset, oldTabArray.length);
099: // offset += oldTabArray.length;
100: // varParam[offset] = (byte)(newTabArray.length/2);
101: // offset += 1;
102: // System.arraycopy(newTabArray, 0, varParam, offset, newTabArray.length);
103: // offset += newTabArray.length;
104: // System.arraycopy(newTabDescriptors, 0, varParam, offset, newTabDescriptors.length);
105: //
106: // size += SprmUtils.addSprm((short)0xC60D, 0, varParam, sprmList);
107: }
108: if (newPAP.getDxaRight() != oldPAP.getDxaRight()) {
109: size += SprmUtils.addSprm((short) 0x840E, newPAP
110: .getDxaRight(), null, sprmList);
111: }
112: if (newPAP.getDxaLeft() != oldPAP.getDxaLeft()) {
113: size += SprmUtils.addSprm((short) 0x840F, newPAP
114: .getDxaLeft(), null, sprmList);
115: }
116: if (newPAP.getDxaLeft1() != oldPAP.getDxaLeft1()) {
117: size += SprmUtils.addSprm((short) 0x8411, newPAP
118: .getDxaLeft1(), null, sprmList);
119: }
120: if (!newPAP.getLspd().equals(oldPAP.getLspd())) {
121: byte[] buf = new byte[4];
122: newPAP.getLspd().serialize(buf, 0);
123:
124: size += SprmUtils.addSprm((short) 0x6412, LittleEndian
125: .getInt(buf), null, sprmList);
126: }
127: if (newPAP.getDyaBefore() != oldPAP.getDyaBefore()) {
128: size += SprmUtils.addSprm((short) 0xA413, newPAP
129: .getDyaBefore(), null, sprmList);
130: }
131: if (newPAP.getDyaAfter() != oldPAP.getDyaAfter()) {
132: size += SprmUtils.addSprm((short) 0xA414, newPAP
133: .getDyaAfter(), null, sprmList);
134: }
135: if (newPAP.getDyaBefore() != oldPAP.getDyaBefore()) {
136: size += SprmUtils.addSprm((short) 0x2404, newPAP
137: .getDyaBefore(), null, sprmList);
138: }
139: if (newPAP.getFKinsoku() != oldPAP.getFKinsoku()) {
140: size += SprmUtils.addSprm((short) 0x2433, newPAP
141: .getDyaBefore(), null, sprmList);
142: }
143: if (newPAP.getFWordWrap() != oldPAP.getFWordWrap()) {
144: size += SprmUtils.addSprm((short) 0x2434, newPAP
145: .getFWordWrap(), null, sprmList);
146: }
147: if (newPAP.getFOverflowPunct() != oldPAP.getFOverflowPunct()) {
148: size += SprmUtils.addSprm((short) 0x2435, newPAP
149: .getFOverflowPunct(), null, sprmList);
150: }
151: if (newPAP.getFTopLinePunct() != oldPAP.getFTopLinePunct()) {
152: size += SprmUtils.addSprm((short) 0x2436, newPAP
153: .getFTopLinePunct(), null, sprmList);
154: }
155: if (newPAP.getFAutoSpaceDE() != oldPAP.getFAutoSpaceDE()) {
156: size += SprmUtils.addSprm((short) 0x2437, newPAP
157: .getFAutoSpaceDE(), null, sprmList);
158: }
159: if (newPAP.getFAutoSpaceDN() != oldPAP.getFAutoSpaceDN()) {
160: size += SprmUtils.addSprm((short) 0x2438, newPAP
161: .getFAutoSpaceDN(), null, sprmList);
162: }
163: if (newPAP.getWAlignFont() != oldPAP.getWAlignFont()) {
164: size += SprmUtils.addSprm((short) 0x4439, newPAP
165: .getWAlignFont(), null, sprmList);
166: }
167: if (newPAP.isFBackward() != oldPAP.isFBackward()
168: || newPAP.isFVertical() != oldPAP.isFVertical()
169: || newPAP.isFRotateFont() != oldPAP.isFRotateFont()) {
170: int val = 0;
171: if (newPAP.isFBackward()) {
172: val |= 0x2;
173: }
174: if (newPAP.isFVertical()) {
175: val |= 0x1;
176: }
177: if (newPAP.isFRotateFont()) {
178: val |= 0x4;
179: }
180: size += SprmUtils.addSprm((short) 0x443A, val, null,
181: sprmList);
182: }
183: if (!Arrays.equals(newPAP.getAnld(), oldPAP.getAnld())) {
184: size += SprmUtils.addSprm((short) 0xC63E, 0, newPAP
185: .getAnld(), sprmList);
186: }
187: if (newPAP.getFInTable() != oldPAP.getFInTable()) {
188: size += SprmUtils.addSprm((short) 0x2416, newPAP
189: .getFInTable(), null, sprmList);
190: }
191: if (newPAP.getFTtp() != oldPAP.getFTtp()) {
192: size += SprmUtils.addSprm((short) 0x2417, newPAP.getFTtp(),
193: null, sprmList);
194: }
195: if (newPAP.getWr() != oldPAP.getWr()) {
196: size += SprmUtils.addSprm((short) 0x2423, newPAP.getWr(),
197: null, sprmList);
198: }
199: if (newPAP.getFLocked() != oldPAP.getFLocked()) {
200: size += SprmUtils.addSprm((short) 0x2430, newPAP
201: .getFLocked(), null, sprmList);
202: }
203: if (newPAP.getDxaAbs() != oldPAP.getDxaAbs()) {
204: size += SprmUtils.addSprm((short) 0x8418, newPAP
205: .getDxaAbs(), null, sprmList);
206: }
207: if (newPAP.getDyaAbs() != oldPAP.getDyaAbs()) {
208: size += SprmUtils.addSprm((short) 0x8419, newPAP
209: .getDyaAbs(), null, sprmList);
210: }
211: if (newPAP.getDxaWidth() != oldPAP.getDxaWidth()) {
212: size += SprmUtils.addSprm((short) 0x841A, newPAP
213: .getDxaWidth(), null, sprmList);
214: }
215: if (!newPAP.getBrcTop().equals(oldPAP.getBrcTop())) {
216: int brc = newPAP.getBrcTop().toInt();
217: size += SprmUtils.addSprm((short) 0x6424, brc, null,
218: sprmList);
219: }
220: if (!newPAP.getBrcLeft().equals(oldPAP.getBrcLeft())) {
221: int brc = newPAP.getBrcLeft().toInt();
222: size += SprmUtils.addSprm((short) 0x6425, brc, null,
223: sprmList);
224: }
225: if (!newPAP.getBrcBottom().equals(oldPAP.getBrcBottom())) {
226: int brc = newPAP.getBrcBottom().toInt();
227: size += SprmUtils.addSprm((short) 0x6426, brc, null,
228: sprmList);
229: }
230: if (!newPAP.getBrcRight().equals(oldPAP.getBrcRight())) {
231: int brc = newPAP.getBrcRight().toInt();
232: size += SprmUtils.addSprm((short) 0x6427, brc, null,
233: sprmList);
234: }
235: if (newPAP.getBrcBar().equals(oldPAP.getBrcBar())) {
236: int brc = newPAP.getBrcBar().toInt();
237: size += SprmUtils.addSprm((short) 0x6428, brc, null,
238: sprmList);
239: }
240: if (newPAP.getDxaFromText() != oldPAP.getDxaFromText()) {
241: size += SprmUtils.addSprm((short) 0x842F, newPAP
242: .getDxaFromText(), null, sprmList);
243: }
244: if (newPAP.getDyaFromText() != oldPAP.getDyaFromText()) {
245: size += SprmUtils.addSprm((short) 0x842E, newPAP
246: .getDyaFromText(), null, sprmList);
247: }
248: if (newPAP.getDyaHeight() != oldPAP.getDyaHeight()
249: || newPAP.getFMinHeight() != oldPAP.getFMinHeight()) {
250: short val = (short) newPAP.getDyaHeight();
251: if (newPAP.getFMinHeight() > 0) {
252: val |= 0x8000;
253: }
254: size += SprmUtils.addSprm((short) 0x442B, val, null,
255: sprmList);
256: }
257: if (newPAP.getShd() != null
258: && !newPAP.getShd().equals(oldPAP.getShd())) {
259: size += SprmUtils.addSprm((short) 0x442D, newPAP.getShd()
260: .toShort(), null, sprmList);
261: }
262: if (newPAP.getDcs() != null
263: && !newPAP.getDcs().equals(oldPAP.getDcs())) {
264: size += SprmUtils.addSprm((short) 0x442C, newPAP.getDcs()
265: .toShort(), null, sprmList);
266: }
267: if (newPAP.getLvl() != oldPAP.getLvl()) {
268: size += SprmUtils.addSprm((short) 0x2640, newPAP.getLvl(),
269: null, sprmList);
270: }
271: if (newPAP.getFNumRMIns() != oldPAP.getFNumRMIns()) {
272: size += SprmUtils.addSprm((short) 0x2443, newPAP
273: .getFNumRMIns(), null, sprmList);
274: }
275: if (newPAP.getFPropRMark() != oldPAP.getFPropRMark()
276: || newPAP.getIbstPropRMark() != oldPAP
277: .getIbstPropRMark()
278: || !newPAP.getDttmPropRMark().equals(
279: oldPAP.getDttmPropRMark())) {
280: byte[] buf = new byte[7];
281: buf[0] = (byte) newPAP.getFPropRMark();
282: LittleEndian.putShort(buf, 1, (short) newPAP
283: .getIbstPropRMark());
284: newPAP.getDttmPropRMark().serialize(buf, 3);
285: size += SprmUtils.addSprm((short) 0xC63F, 0, buf, sprmList);
286: }
287: if (!Arrays.equals(newPAP.getNumrm(), oldPAP.getNumrm())) {
288: size += SprmUtils.addSprm((short) 0xC645, 0, newPAP
289: .getNumrm(), sprmList);
290: }
291:
292: if (newPAP.getTableLevel() != oldPAP.getTableLevel()) {
293: size += SprmUtils.addSprm((short) 0x6649, newPAP
294: .getTableLevel(), null, sprmList);
295: }
296:
297: if (newPAP.getEmbeddedCellMark() != oldPAP
298: .getEmbeddedCellMark()) {
299: size += SprmUtils.addSprm((short) 0x244b, newPAP
300: .getEmbeddedCellMark(), null, sprmList);
301: }
302:
303: if (newPAP.getFTtpEmbedded() != oldPAP.getFTtpEmbedded()) {
304: size += SprmUtils.addSprm((short) 0x244c, newPAP
305: .getFTtpEmbedded(), null, sprmList);
306: }
307:
308: return SprmUtils.getGrpprl(sprmList, size);
309:
310: }
311: }
|