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.ArrayList;
021: import java.util.List;
022: import java.util.Arrays;
023:
024: import org.apache.poi.hwpf.usermodel.CharacterProperties;
025: import org.apache.poi.util.LittleEndian;
026:
027: public class CharacterSprmCompressor {
028: public CharacterSprmCompressor() {
029: }
030:
031: public static byte[] compressCharacterProperty(
032: CharacterProperties newCHP, CharacterProperties oldCHP) {
033: ArrayList sprmList = new ArrayList();
034: int size = 0;
035:
036: if (newCHP.isFRMarkDel() != oldCHP.isFRMarkDel()) {
037: int value = 0;
038: if (newCHP.isFRMarkDel()) {
039: value = 0x01;
040: }
041: size += SprmUtils.addSprm((short) 0x0800, value, null,
042: sprmList);
043: }
044: if (newCHP.isFRMark() != oldCHP.isFRMark()) {
045: int value = 0;
046: if (newCHP.isFRMark()) {
047: value = 0x01;
048: }
049: size += SprmUtils.addSprm((short) 0x0801, value, null,
050: sprmList);
051: }
052: if (newCHP.isFFldVanish() != oldCHP.isFFldVanish()) {
053: int value = 0;
054: if (newCHP.isFFldVanish()) {
055: value = 0x01;
056: }
057: size += SprmUtils.addSprm((short) 0x0802, value, null,
058: sprmList);
059: }
060: if (newCHP.isFSpec() != oldCHP.isFSpec()
061: || newCHP.getFcPic() != oldCHP.getFcPic()) {
062: size += SprmUtils.addSprm((short) 0x6a03,
063: newCHP.getFcPic(), null, sprmList);
064: }
065: if (newCHP.getIbstRMark() != oldCHP.getIbstRMark()) {
066: size += SprmUtils.addSprm((short) 0x4804, newCHP
067: .getIbstRMark(), null, sprmList);
068: }
069: if (!newCHP.getDttmRMark().equals(oldCHP.getDttmRMark())) {
070: byte[] buf = new byte[4];
071: newCHP.getDttmRMark().serialize(buf, 0);
072:
073: size += SprmUtils.addSprm((short) 0x6805, LittleEndian
074: .getInt(buf), null, sprmList);
075: }
076: if (newCHP.isFData() != oldCHP.isFData()) {
077: int value = 0;
078: if (newCHP.isFData()) {
079: value = 0x01;
080: }
081: size += SprmUtils.addSprm((short) 0x0806, value, null,
082: sprmList);
083: }
084: if (newCHP.isFSpec() && newCHP.getFtcSym() != 0) {
085: byte[] varParam = new byte[4];
086: LittleEndian.putShort(varParam, 0, (short) newCHP
087: .getFtcSym());
088: LittleEndian.putShort(varParam, 2, (short) newCHP
089: .getXchSym());
090:
091: size += SprmUtils.addSprm((short) 0x6a09, 0, varParam,
092: sprmList);
093: }
094: if (newCHP.isFOle2() != newCHP.isFOle2()) {
095: int value = 0;
096: if (newCHP.isFOle2()) {
097: value = 0x01;
098: }
099: size += SprmUtils.addSprm((short) 0x080a, value, null,
100: sprmList);
101: }
102: if (newCHP.getIcoHighlight() != oldCHP.getIcoHighlight()) {
103: size += SprmUtils.addSprm((short) 0x2a0c, newCHP
104: .getIcoHighlight(), null, sprmList);
105: }
106: if (newCHP.getFcObj() != oldCHP.getFcObj()) {
107: size += SprmUtils.addSprm((short) 0x680e,
108: newCHP.getFcObj(), null, sprmList);
109: }
110: if (newCHP.getIstd() != oldCHP.getIstd()) {
111: size += SprmUtils.addSprm((short) 0x4a30, newCHP.getIstd(),
112: null, sprmList);
113: }
114: if (newCHP.isFBold() != oldCHP.isFBold()) {
115: int value = 0;
116: if (newCHP.isFBold()) {
117: value = 0x01;
118: }
119: size += SprmUtils.addSprm((short) 0x0835, value, null,
120: sprmList);
121: }
122: if (newCHP.isFItalic() != oldCHP.isFItalic()) {
123: int value = 0;
124: if (newCHP.isFItalic()) {
125: value = 0x01;
126: }
127: size += SprmUtils.addSprm((short) 0x0836, value, null,
128: sprmList);
129: }
130: if (newCHP.isFStrike() != oldCHP.isFStrike()) {
131: int value = 0;
132: if (newCHP.isFStrike()) {
133: value = 0x01;
134: }
135: size += SprmUtils.addSprm((short) 0x0837, value, null,
136: sprmList);
137: }
138: if (newCHP.isFOutline() != oldCHP.isFOutline()) {
139: int value = 0;
140: if (newCHP.isFOutline()) {
141: value = 0x01;
142: }
143: size += SprmUtils.addSprm((short) 0x0838, value, null,
144: sprmList);
145: }
146: if (newCHP.isFShadow() != oldCHP.isFShadow()) {
147: int value = 0;
148: if (newCHP.isFShadow()) {
149: value = 0x01;
150: }
151: size += SprmUtils.addSprm((short) 0x0839, value, null,
152: sprmList);
153: }
154: if (newCHP.isFSmallCaps() != oldCHP.isFSmallCaps()) {
155: int value = 0;
156: if (newCHP.isFSmallCaps()) {
157: value = 0x01;
158: }
159: size += SprmUtils.addSprm((short) 0x083a, value, null,
160: sprmList);
161: }
162: if (newCHP.isFCaps() != oldCHP.isFCaps()) {
163: int value = 0;
164: if (newCHP.isFCaps()) {
165: value = 0x01;
166: }
167: size += SprmUtils.addSprm((short) 0x083b, value, null,
168: sprmList);
169: }
170: if (newCHP.isFVanish() != oldCHP.isFVanish()) {
171: int value = 0;
172: if (newCHP.isFVanish()) {
173: value = 0x01;
174: }
175: size += SprmUtils.addSprm((short) 0x083c, value, null,
176: sprmList);
177: }
178: if (newCHP.getKul() != oldCHP.getKul()) {
179: size += SprmUtils.addSprm((short) 0x2a3e, newCHP.getKul(),
180: null, sprmList);
181: }
182: if (newCHP.getDxaSpace() != oldCHP.getDxaSpace()) {
183: size += SprmUtils.addSprm((short) 0x8840, newCHP
184: .getDxaSpace(), null, sprmList);
185: }
186: if (newCHP.getIco() != oldCHP.getIco()) {
187: size += SprmUtils.addSprm((short) 0x2a42, newCHP.getIco(),
188: null, sprmList);
189: }
190: if (newCHP.getHps() != oldCHP.getHps()) {
191: size += SprmUtils.addSprm((short) 0x4a43, newCHP.getHps(),
192: null, sprmList);
193: }
194: if (newCHP.getHpsPos() != oldCHP.getHpsPos()) {
195: size += SprmUtils.addSprm((short) 0x4845, newCHP
196: .getHpsPos(), null, sprmList);
197: }
198: if (newCHP.getHpsKern() != oldCHP.getHpsKern()) {
199: size += SprmUtils.addSprm((short) 0x484b, newCHP
200: .getHpsKern(), null, sprmList);
201: }
202: if (newCHP.getYsr() != oldCHP.getYsr()) {
203: size += SprmUtils.addSprm((short) 0x484e, newCHP.getYsr(),
204: null, sprmList);
205: }
206: if (newCHP.getFtcAscii() != oldCHP.getFtcAscii()) {
207: size += SprmUtils.addSprm((short) 0x4a4f, newCHP
208: .getFtcAscii(), null, sprmList);
209: }
210: if (newCHP.getFtcFE() != oldCHP.getFtcFE()) {
211: size += SprmUtils.addSprm((short) 0x4a50,
212: newCHP.getFtcFE(), null, sprmList);
213: }
214: if (newCHP.getFtcOther() != oldCHP.getFtcOther()) {
215: size += SprmUtils.addSprm((short) 0x4a51, newCHP
216: .getFtcOther(), null, sprmList);
217: }
218:
219: if (newCHP.isFDStrike() != oldCHP.isFDStrike()) {
220: int value = 0;
221: if (newCHP.isFDStrike()) {
222: value = 0x01;
223: }
224: size += SprmUtils.addSprm((short) 0x2a53, value, null,
225: sprmList);
226: }
227: if (newCHP.isFImprint() != oldCHP.isFImprint()) {
228: int value = 0;
229: if (newCHP.isFImprint()) {
230: value = 0x01;
231: }
232: size += SprmUtils.addSprm((short) 0x0854, value, null,
233: sprmList);
234: }
235: if (newCHP.isFSpec() != oldCHP.isFSpec()) {
236: int value = 0;
237: if (newCHP.isFSpec()) {
238: value = 0x01;
239: }
240: size += SprmUtils.addSprm((short) 0x0855, value, null,
241: sprmList);
242: }
243: if (newCHP.isFObj() != oldCHP.isFObj()) {
244: int value = 0;
245: if (newCHP.isFObj()) {
246: value = 0x01;
247: }
248: size += SprmUtils.addSprm((short) 0x0856, value, null,
249: sprmList);
250: }
251: if (newCHP.isFEmboss() != oldCHP.isFEmboss()) {
252: int value = 0;
253: if (newCHP.isFEmboss()) {
254: value = 0x01;
255: }
256: size += SprmUtils.addSprm((short) 0x0858, value, null,
257: sprmList);
258: }
259: if (newCHP.getSfxtText() != oldCHP.getSfxtText()) {
260: size += SprmUtils.addSprm((short) 0x2859, newCHP
261: .getSfxtText(), null, sprmList);
262: }
263: if (newCHP.getIco24() != oldCHP.getIco24()) {
264: if (newCHP.getIco24() != -1) // don't add a sprm if we're looking at an ico = Auto
265: size += SprmUtils.addSprm((short) 0x6870, newCHP
266: .getIco24(), null, sprmList);
267: }
268:
269: return SprmUtils.getGrpprl(sprmList, size);
270: }
271:
272: }
|