KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > hwpf > sprm > CharacterSprmUncompressor


1
2 /* ====================================================================
3    Copyright 2002-2004 Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16 ==================================================================== */

17         
18
19 package org.apache.poi.hwpf.sprm;
20
21 import org.apache.poi.hwpf.usermodel.CharacterProperties;
22 import org.apache.poi.hwpf.usermodel.DateAndTime;
23 import org.apache.poi.hwpf.usermodel.BorderCode;
24 import org.apache.poi.hwpf.usermodel.ShadingDescriptor;
25 import org.apache.poi.hwpf.model.StyleSheet;
26 import org.apache.poi.util.LittleEndian;
27
28 public class CharacterSprmUncompressor
29 {
30   public CharacterSprmUncompressor()
31   {
32   }
33
34   public static CharacterProperties uncompressCHP(CharacterProperties parent,
35                                                   byte[] grpprl,
36                                                   int offset)
37   {
38     CharacterProperties newProperties = null;
39     try
40     {
41       newProperties = (CharacterProperties) parent.clone();
42     }
43     catch (CloneNotSupportedException JavaDoc cnse)
44     {
45       throw new RuntimeException JavaDoc("There is no way this exception should happen!!");
46     }
47     SprmIterator sprmIt = new SprmIterator(grpprl, offset);
48
49     while (sprmIt.hasNext())
50     {
51       SprmOperation sprm = (SprmOperation)sprmIt.next();
52       unCompressCHPOperation(parent, newProperties, sprm);
53     }
54
55     return newProperties;
56   }
57
58   /**
59    * Used in decompression of a chpx. This performs an operation defined by
60    * a single sprm.
61    *
62    * @param oldCHP The base CharacterProperties.
63    * @param newCHP The current CharacterProperties.
64    * @param operand The operand defined by the sprm (See Word file format spec)
65    * @param param The parameter defined by the sprm (See Word file format spec)
66    * @param varParam The variable length parameter defined by the sprm. (See
67    * Word file format spec)
68    * @param grpprl The entire chpx that this operation is a part of.
69    * @param offset The offset in the grpprl of the next sprm
70    * @param styleSheet The StyleSheet for this document.
71    */

72   static void unCompressCHPOperation (CharacterProperties oldCHP,
73                                       CharacterProperties newCHP,
74                                       SprmOperation sprm)
75   {
76
77     switch (sprm.getOperation())
78     {
79       case 0:
80         newCHP.setFRMarkDel (getFlag (sprm.getOperand()));
81         break;
82       case 0x1:
83         newCHP.setFRMark (getFlag (sprm.getOperand()));
84         break;
85       case 0x2:
86         newCHP.setFFldVanish (getFlag (sprm.getOperand()));
87         break;
88       case 0x3:
89         newCHP.setFcPic (sprm.getOperand());
90         newCHP.setFSpec (true);
91         break;
92       case 0x4:
93         newCHP.setIbstRMark ((short) sprm.getOperand());
94         break;
95       case 0x5:
96         newCHP.setDttmRMark (new DateAndTime(sprm.getGrpprl(), sprm.getGrpprlOffset()));
97         break;
98       case 0x6:
99         newCHP.setFData (getFlag (sprm.getOperand()));
100         break;
101       case 0x7:
102         //don't care about this
103
break;
104       case 0x8:
105         //short chsDiff = (short)((param & 0xff0000) >>> 16);
106
int operand =sprm.getOperand();
107         short chsDiff = (short) (operand & 0x0000ff);
108         newCHP.setFChsDiff (getFlag (chsDiff));
109         newCHP.setChse ((short) (operand & 0xffff00));
110         break;
111       case 0x9:
112         newCHP.setFSpec (true);
113         newCHP.setFtcSym ((short) LittleEndian.getShort (sprm.getGrpprl(), sprm.getGrpprlOffset()));
114         newCHP.setXchSym ((short) LittleEndian.getShort (sprm.getGrpprl(), sprm.getGrpprlOffset() + 2));
115         break;
116       case 0xa:
117         newCHP.setFOle2 (getFlag (sprm.getOperand()));
118         break;
119       case 0xb:
120
121         // Obsolete
122
break;
123       case 0xc:
124         newCHP.setIcoHighlight ((byte) sprm.getOperand());
125         newCHP.setFHighlight (getFlag (sprm.getOperand()));
126         break;
127       case 0xd:
128
129         // undocumented
130
break;
131       case 0xe:
132         newCHP.setFcObj (sprm.getOperand());
133         break;
134       case 0xf:
135
136         // undocumented
137
break;
138       case 0x10:
139
140         // undocumented
141
break;
142
143         // undocumented till 0x30
144

145       case 0x11:
146         break;
147       case 0x12:
148         break;
149       case 0x13:
150         break;
151       case 0x14:
152         break;
153       case 0x15:
154         break;
155       case 0x16:
156         break;
157       case 0x17:
158         break;
159       case 0x18:
160         break;
161       case 0x19:
162         break;
163       case 0x1a:
164         break;
165       case 0x1b:
166         break;
167       case 0x1c:
168         break;
169       case 0x1d:
170         break;
171       case 0x1e:
172         break;
173       case 0x1f:
174         break;
175       case 0x20:
176         break;
177       case 0x21:
178         break;
179       case 0x22:
180         break;
181       case 0x23:
182         break;
183       case 0x24:
184         break;
185       case 0x25:
186         break;
187       case 0x26:
188         break;
189       case 0x27:
190         break;
191       case 0x28:
192         break;
193       case 0x29:
194         break;
195       case 0x2a:
196         break;
197       case 0x2b:
198         break;
199       case 0x2c:
200         break;
201       case 0x2d:
202         break;
203       case 0x2e:
204         break;
205       case 0x2f:
206         break;
207       case 0x30:
208         newCHP.setIstd (sprm.getOperand());
209         break;
210       case 0x31:
211
212         //permutation vector for fast saves, who cares!
213
break;
214       case 0x32:
215         newCHP.setFBold (false);
216         newCHP.setFItalic (false);
217         newCHP.setFOutline (false);
218         newCHP.setFStrike (false);
219         newCHP.setFShadow (false);
220         newCHP.setFSmallCaps (false);
221         newCHP.setFCaps (false);
222         newCHP.setFVanish (false);
223         newCHP.setKul ((byte) 0);
224         newCHP.setIco ((byte) 0);
225         break;
226       case 0x33:
227         try
228         {
229           // preserve the fSpec setting from the original CHP
230
boolean fSpec = newCHP.isFSpec ();
231           newCHP = (CharacterProperties) oldCHP.clone ();
232           newCHP.setFSpec (fSpec);
233
234         }
235         catch (CloneNotSupportedException JavaDoc e)
236         {
237           //do nothing
238
}
239         return;
240       case 0x34:
241
242         // undocumented
243
break;
244       case 0x35:
245         newCHP.setFBold (getCHPFlag ((byte) sprm.getOperand(), oldCHP.isFBold ()));
246         break;
247       case 0x36:
248         newCHP.setFItalic (getCHPFlag ((byte) sprm.getOperand(), oldCHP.isFItalic ()));
249         break;
250       case 0x37:
251         newCHP.setFStrike (getCHPFlag ((byte) sprm.getOperand(), oldCHP.isFStrike ()));
252         break;
253       case 0x38:
254         newCHP.setFOutline (getCHPFlag ((byte) sprm.getOperand(), oldCHP.isFOutline ()));
255         break;
256       case 0x39:
257         newCHP.setFShadow (getCHPFlag ((byte) sprm.getOperand(), oldCHP.isFShadow ()));
258         break;
259       case 0x3a:
260         newCHP.setFSmallCaps (getCHPFlag ((byte) sprm.getOperand(), oldCHP.isFSmallCaps ()));
261         break;
262       case 0x3b:
263         newCHP.setFCaps (getCHPFlag ((byte) sprm.getOperand(), oldCHP.isFCaps ()));
264         break;
265       case 0x3c:
266         newCHP.setFVanish (getCHPFlag ((byte) sprm.getOperand(), oldCHP.isFVanish ()));
267         break;
268       case 0x3d:
269         newCHP.setFtcAscii ((short) sprm.getOperand());
270         break;
271       case 0x3e:
272         newCHP.setKul ((byte) sprm.getOperand());
273         break;
274       case 0x3f:
275         operand = sprm.getOperand();
276         int hps = operand & 0xff;
277         if (hps != 0)
278         {
279           newCHP.setHps (hps);
280         }
281
282         //byte cInc = (byte)(((byte)(param & 0xfe00) >>> 4) >> 1);
283
byte cInc = (byte) ((operand & 0xff00) >>> 8);
284         cInc = (byte) (cInc >>> 1);
285         if (cInc != 0)
286         {
287           newCHP.setHps (Math.max (newCHP.getHps () + (cInc * 2), 2));
288         }
289
290         //byte hpsPos = (byte)((param & 0xff0000) >>> 8);
291
byte hpsPos = (byte) ((operand & 0xff0000) >>> 16);
292         if (hpsPos != 0x80)
293         {
294           newCHP.setHpsPos (hpsPos);
295         }
296         boolean fAdjust = (operand & 0x0100) > 0;
297         if (fAdjust && hpsPos != 128 && hpsPos != 0 && oldCHP.getHpsPos () == 0)
298         {
299           newCHP.setHps (Math.max (newCHP.getHps () + ( -2), 2));
300         }
301         if (fAdjust && hpsPos == 0 && oldCHP.getHpsPos () != 0)
302         {
303           newCHP.setHps (Math.max (newCHP.getHps () + 2, 2));
304         }
305         break;
306       case 0x40:
307         newCHP.setDxaSpace (sprm.getOperand());
308         break;
309       case 0x41:
310         newCHP.setLidDefault ((short) sprm.getOperand());
311         break;
312       case 0x42:
313         newCHP.setIco ((byte) sprm.getOperand());
314         break;
315       case 0x43:
316         newCHP.setHps (sprm.getOperand());
317         break;
318       case 0x44:
319         byte hpsLvl = (byte) sprm.getOperand();
320         newCHP.setHps (Math.max (newCHP.getHps () + (hpsLvl * 2), 2));
321         break;
322       case 0x45:
323         newCHP.setHpsPos ((short) sprm.getOperand());
324         break;
325       case 0x46:
326         if (sprm.getOperand() != 0)
327         {
328           if (oldCHP.getHpsPos () == 0)
329           {
330             newCHP.setHps (Math.max (newCHP.getHps () + ( -2), 2));
331           }
332         }
333         else
334         {
335           if (oldCHP.getHpsPos () != 0)
336           {
337             newCHP.setHps (Math.max (newCHP.getHps () + 2, 2));
338           }
339         }
340         break;
341       case 0x47:
342         /*CharacterProperties genCHP = new CharacterProperties ();
343         genCHP.setFtcAscii (4);
344         genCHP = (CharacterProperties) unCompressProperty (varParam, genCHP,
345           styleSheet);
346         CharacterProperties styleCHP = styleSheet.getStyleDescription (oldCHP.
347           getBaseIstd ()).getCHP ();
348         if (genCHP.isFBold () == newCHP.isFBold ())
349         {
350           newCHP.setFBold (styleCHP.isFBold ());
351         }
352         if (genCHP.isFItalic () == newCHP.isFItalic ())
353         {
354           newCHP.setFItalic (styleCHP.isFItalic ());
355         }
356         if (genCHP.isFSmallCaps () == newCHP.isFSmallCaps ())
357         {
358           newCHP.setFSmallCaps (styleCHP.isFSmallCaps ());
359         }
360         if (genCHP.isFVanish () == newCHP.isFVanish ())
361         {
362           newCHP.setFVanish (styleCHP.isFVanish ());
363         }
364         if (genCHP.isFStrike () == newCHP.isFStrike ())
365         {
366           newCHP.setFStrike (styleCHP.isFStrike ());
367         }
368         if (genCHP.isFCaps () == newCHP.isFCaps ())
369         {
370           newCHP.setFCaps (styleCHP.isFCaps ());
371         }
372         if (genCHP.getFtcAscii () == newCHP.getFtcAscii ())
373         {
374           newCHP.setFtcAscii (styleCHP.getFtcAscii ());
375         }
376         if (genCHP.getFtcFE () == newCHP.getFtcFE ())
377         {
378           newCHP.setFtcFE (styleCHP.getFtcFE ());
379         }
380         if (genCHP.getFtcOther () == newCHP.getFtcOther ())
381         {
382           newCHP.setFtcOther (styleCHP.getFtcOther ());
383         }
384         if (genCHP.getHps () == newCHP.getHps ())
385         {
386           newCHP.setHps (styleCHP.getHps ());
387         }
388         if (genCHP.getHpsPos () == newCHP.getHpsPos ())
389         {
390           newCHP.setHpsPos (styleCHP.getHpsPos ());
391         }
392         if (genCHP.getKul () == newCHP.getKul ())
393         {
394           newCHP.setKul (styleCHP.getKul ());
395         }
396         if (genCHP.getDxaSpace () == newCHP.getDxaSpace ())
397         {
398           newCHP.setDxaSpace (styleCHP.getDxaSpace ());
399         }
400         if (genCHP.getIco () == newCHP.getIco ())
401         {
402           newCHP.setIco (styleCHP.getIco ());
403         }
404         if (genCHP.getLidDefault () == newCHP.getLidDefault ())
405         {
406           newCHP.setLidDefault (styleCHP.getLidDefault ());
407         }
408         if (genCHP.getLidFE () == newCHP.getLidFE ())
409         {
410           newCHP.setLidFE (styleCHP.getLidFE ());
411         }*/

412         break;
413       case 0x48:
414         newCHP.setIss ((byte) sprm.getOperand());
415         break;
416       case 0x49:
417         newCHP.setHps (LittleEndian.getShort (sprm.getGrpprl(), sprm.getGrpprlOffset()));
418         break;
419       case 0x4a:
420         int increment = LittleEndian.getShort (sprm.getGrpprl(), sprm.getGrpprlOffset());
421         newCHP.setHps (Math.max (newCHP.getHps () + increment, 8));
422         break;
423       case 0x4b:
424         newCHP.setHpsKern (sprm.getOperand());
425         break;
426       case 0x4c:
427 // unCompressCHPOperation (oldCHP, newCHP, 0x47, param, varParam,
428
// styleSheet, opSize);
429
break;
430       case 0x4d:
431         float percentage = (float) sprm.getOperand() / 100.0f;
432         int add = (int) ((float) percentage * (float) newCHP.getHps ());
433         newCHP.setHps (newCHP.getHps () + add);
434         break;
435       case 0x4e:
436         newCHP.setYsr ((byte) sprm.getOperand());
437         break;
438       case 0x4f:
439         newCHP.setFtcAscii ((short) sprm.getOperand());
440         break;
441       case 0x50:
442         newCHP.setFtcFE ((short) sprm.getOperand());
443         break;
444       case 0x51:
445         newCHP.setFtcOther ((short) sprm.getOperand());
446         break;
447       case 0x52:
448
449         // undocumented
450
break;
451       case 0x53:
452         newCHP.setFDStrike (getFlag (sprm.getOperand()));
453         break;
454       case 0x54:
455         newCHP.setFImprint (getFlag (sprm.getOperand()));
456         break;
457       case 0x55:
458         newCHP.setFSpec (getFlag (sprm.getOperand()));
459         break;
460       case 0x56:
461         newCHP.setFObj (getFlag (sprm.getOperand()));
462         break;
463       case 0x57:
464         byte[] buf = sprm.getGrpprl();
465         int offset = sprm.getGrpprlOffset();
466         newCHP.setFPropMark (buf[offset]);
467         newCHP.setIbstPropRMark ((short) LittleEndian.getShort (buf, offset + 1));
468         newCHP.setDttmPropRMark (new DateAndTime(buf, offset +3));
469         break;
470       case 0x58:
471         newCHP.setFEmboss (getFlag (sprm.getOperand()));
472         break;
473       case 0x59:
474         newCHP.setSfxtText ((byte) sprm.getOperand());
475         break;
476
477         // undocumented till 0x61
478
case 0x5a:
479         break;
480       case 0x5b:
481         break;
482       case 0x5c:
483         break;
484       case 0x5d:
485         break;
486       case 0x5e:
487         break;
488       case 0x5f:
489         break;
490       case 0x60:
491         break;
492       case 0x61:
493         break;
494       case 0x62:
495         byte[] xstDispFldRMark = new byte[32];
496         buf = sprm.getGrpprl();
497         offset = sprm.getGrpprlOffset();
498         newCHP.setFDispFldRMark (buf[offset]);
499         newCHP.setIbstDispFldRMark ((short) LittleEndian.getShort (buf, offset + 1));
500         newCHP.setDttmDispFldRMark (new DateAndTime(buf, offset + 3));
501         System.arraycopy (buf, offset + 7, xstDispFldRMark, 0, 32);
502         newCHP.setXstDispFldRMark (xstDispFldRMark);
503         break;
504       case 0x63:
505         newCHP.setIbstRMarkDel ((short) sprm.getOperand());
506         break;
507       case 0x64:
508         newCHP.setDttmRMarkDel (new DateAndTime(sprm.getGrpprl(), sprm.getGrpprlOffset()));
509         break;
510       case 0x65:
511         newCHP.setBrc (new BorderCode(sprm.getGrpprl(), sprm.getGrpprlOffset()));
512         break;
513       case 0x66:
514         newCHP.setShd (new ShadingDescriptor(sprm.getGrpprl(), sprm.getGrpprlOffset()));
515         break;
516       case 0x67:
517
518         // Obsolete
519
break;
520       case 0x68:
521         break;
522
523         // undocumented till 0x6c
524

525       case 0x69:
526         break;
527       case 0x6a:
528         break;
529       case 0x6b:
530         break;
531       case 0x6c:
532         break;
533       case 0x6d:
534         newCHP.setLidDefault ((short) sprm.getOperand());
535         break;
536       case 0x6e:
537         newCHP.setLidFE ((short) sprm.getOperand());
538         break;
539       case 0x6f:
540         newCHP.setIdctHint ((byte) sprm.getOperand());
541         break;
542       case 0x70:
543         newCHP.setIco24 (sprm.getOperand());
544         break;
545     }
546   }
547
548   /**
549    * Converts an int into a boolean. If the int is non-zero, it returns true.
550    * Otherwise it returns false.
551    *
552    * @param x The int to convert.
553    *
554    * @return A boolean whose value depends on x.
555    */

556   public static boolean getFlag (int x)
557   {
558     if (x != 0)
559     {
560       return true;
561     }
562     else
563     {
564       return false;
565     }
566   }
567
568   private static boolean getCHPFlag (byte x, boolean oldVal)
569   {
570     /*
571          switch(x)
572          {
573      case 0:
574        return false;
575      case 1:
576        return true;
577      case (byte)0x80:
578        return oldVal;
579      case (byte)0x81:
580        return !oldVal;
581      default:
582        return false;
583          }
584      */

585     if (x == 0)
586     {
587       return false;
588     }
589     else if (x == 1)
590     {
591       return true;
592     }
593     else if ((x & 0x81) == 0x80)
594     {
595       return oldVal;
596     }
597     else if ((x & 0x81) == 0x81)
598     {
599       return!oldVal;
600     }
601     else
602     {
603       return false;
604     }
605   }
606
607 }
608
Popular Tags