aboutsummaryrefslogtreecommitdiffstats
path: root/poi-scratchpad/src/main/java/org/apache/poi/hwpf/sprm/ParagraphSprmUncompressor.java
blob: da16b69ce791eeb5fb6340e8fc33e04a6157f8b3 (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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
/* ====================================================================
   Licensed to the Apache Software Foundation (ASF) under one or more
   contributor license agreements.  See the NOTICE file distributed with
   this work for additional information regarding copyright ownership.
   The ASF licenses this file to You under the Apache License, Version 2.0
   (the "License"); you may not use this file except in compliance with
   the License.  You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
==================================================================== */

package org.apache.poi.hwpf.sprm;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.logging.log4j.Logger;
import org.apache.poi.logging.PoiLogManager;
import org.apache.poi.hwpf.model.TabDescriptor;
import org.apache.poi.hwpf.model.types.TBDAbstractType;
import org.apache.poi.hwpf.usermodel.BorderCode;
import org.apache.poi.hwpf.usermodel.DateAndTime;
import org.apache.poi.hwpf.usermodel.DropCapSpecifier;
import org.apache.poi.hwpf.usermodel.LineSpacingDescriptor;
import org.apache.poi.hwpf.usermodel.ParagraphProperties;
import org.apache.poi.hwpf.usermodel.ShadingDescriptor;
import org.apache.poi.hwpf.usermodel.ShadingDescriptor80;
import org.apache.poi.util.Internal;
import org.apache.poi.util.LittleEndian;
import org.apache.poi.util.LittleEndianConsts;

import static org.apache.logging.log4j.util.Unbox.box;

@Internal
public final class ParagraphSprmUncompressor extends SprmUncompressor {

    private static final Logger LOG = PoiLogManager.getLogger(ParagraphSprmUncompressor.class);

    public ParagraphSprmUncompressor() {
    }

    public static ParagraphProperties uncompressPAP(ParagraphProperties parent,
                                                    byte[] grpprl,
                                                    int offset) {
        ParagraphProperties newProperties = parent.copy();
        SprmIterator sprmIt = new SprmIterator(grpprl, offset);

        while (sprmIt.hasNext()) {
            SprmOperation sprm = sprmIt.next();

            // PAPXs can contain table sprms if the paragraph marks the end of a
            // table row
            if (sprm.getType() == SprmOperation.TYPE_PAP) {
                try {
                    unCompressPAPOperation(newProperties, sprm);
                } catch (Exception exc) {
                    LOG.atError().withThrowable(exc).log("Unable to apply SPRM operation '{}'", box(sprm.getOperation()));
                }
            }
        }

        return newProperties;
    }

    /**
     * Performs an operation on a ParagraphProperties object. Used to uncompress
     * from a papx.
     *
     * @param newPAP The ParagraphProperties object to perform the operation on.
     * @param sprm   sn SPRM operation.
     */
    static void unCompressPAPOperation(ParagraphProperties newPAP, SprmOperation sprm) {
        switch (sprm.getOperation()) {
            case 0:
                newPAP.setIstd(sprm.getOperand());
                break;
            case 0x1:

                // Used only for piece table grpprl's not for PAPX
//        int istdFirst = LittleEndian.getShort (varParam, 2);
//        int istdLast = LittleEndian.getShort (varParam, 4);
//        if ((newPAP.getIstd () > istdFirst) || (newPAP.getIstd () <= istdLast))
//        {
//          permuteIstd (newPAP, varParam, opSize);
//        }
                break;
            case 0x2:
                if (newPAP.getIstd() <= 9 || newPAP.getIstd() >= 1) {
                    byte paramTmp = (byte) sprm.getOperand();
                    newPAP.setIstd(newPAP.getIstd() + paramTmp);
                    newPAP.setLvl((byte) (newPAP.getLvl() + paramTmp));

                    if (((paramTmp >> 7) & 0x01) == 1) {
                        newPAP.setIstd(Math.max(newPAP.getIstd(), 1));
                    } else {
                        newPAP.setIstd(Math.min(newPAP.getIstd(), 9));
                    }

                }
                break;
            case 0x3:
                // Physical justification of the paragraph
                newPAP.setJc((byte) sprm.getOperand());
                break;
            case 0x4:
                newPAP.setFSideBySide(sprm.getOperand() != 0);
                break;
            case 0x5:
                newPAP.setFKeep(sprm.getOperand() != 0);
                break;
            case 0x6:
                newPAP.setFKeepFollow(sprm.getOperand() != 0);
                break;
            case 0x7:
                newPAP.setFPageBreakBefore(sprm.getOperand() != 0);
                break;
            case 0x8:
                newPAP.setBrcl((byte) sprm.getOperand());
                break;
            case 0x9:
                newPAP.setBrcp((byte) sprm.getOperand());
                break;
            case 0xa:
                newPAP.setIlvl((byte) sprm.getOperand());
                break;
            case 0xb:
                /* sprmPIlfo -- 0x460B */
                newPAP.setIlfo(sprm.getOperandShortSigned());
                break;
            case 0xc:
                newPAP.setFNoLnn(sprm.getOperand() != 0);
                break;
            case 0xd:
                // handle tabs . variable parameter. separate processing needed
                handleTabs(newPAP, sprm);
                break;
            case 0xe:
                newPAP.setDxaRight(sprm.getOperand());
                break;
            case 0xf:
                newPAP.setDxaLeft(sprm.getOperand());
                break;
            case 0x10:

                // sprmPNest is only stored in grpprls linked to a piece table.
                newPAP.setDxaLeft(newPAP.getDxaLeft() + sprm.getOperand());
                newPAP.setDxaLeft(Math.max(0, newPAP.getDxaLeft()));
                break;
            case 0x11:
                newPAP.setDxaLeft1(sprm.getOperand());
                break;
            case 0x12:
                newPAP.setLspd(new LineSpacingDescriptor(sprm.getGrpprl(), sprm.getGrpprlOffset()));
                break;
            case 0x13:
                newPAP.setDyaBefore(sprm.getOperand());
                break;
            case 0x14:
                newPAP.setDyaAfter(sprm.getOperand());
                break;
            case 0x15:
                // fast saved only
                //applySprmPChgTabs (newPAP, varParam, opSize);
                break;
            case 0x16:
                // sprmPFInTable -- 0x2416
                newPAP.setFInTable(sprm.getOperand() != 0);
                break;
            case 0x17:
                newPAP.setFTtp(sprm.getOperand() != 0);
                break;
            case 0x18:
                newPAP.setDxaAbs(sprm.getOperand());
                break;
            case 0x19:
                newPAP.setDyaAbs(sprm.getOperand());
                break;
            case 0x1a:
                newPAP.setDxaWidth(sprm.getOperand());
                break;
            case 0x1b:
                byte param = (byte) sprm.getOperand();
                // TODO: handle paragraph postioning
                byte pcVert = (byte) ((param & 0x0c) >> 2);
                byte pcHorz = (byte) (param & 0x03);
                if (pcVert != 3) {
                    newPAP.setPcVert(pcVert);
                }
                if (pcHorz != 3) {
                    newPAP.setPcHorz(pcHorz);
                }
                break;

            // BrcXXX1 is older Version. Brc is used
            // case 0x1c:
            // newPAP.setBrcTop1((short)param);
            // break;
            // case 0x1d:
            // newPAP.setBrcLeft1((short)param);
            // break;
            // case 0x1e:
            // newPAP.setBrcBottom1((short)param);
            // break;
            // case 0x1f:
            // newPAP.setBrcRight1((short)param);
            // break;
            // case 0x20:
            // newPAP.setBrcBetween1((short)param);
            // break;
            // case 0x21:
            // newPAP.setBrcBar1((byte)param);
            // break;

            case 0x22:
                newPAP.setDxaFromText(sprm.getOperand());
                break;
            case 0x23:
                newPAP.setWr((byte) sprm.getOperand());
                break;
            case 0x24:
                newPAP.setBrcTop(new BorderCode(sprm.getGrpprl(), sprm.getGrpprlOffset()));
                break;
            case 0x25:
                newPAP.setBrcLeft(new BorderCode(sprm.getGrpprl(), sprm.getGrpprlOffset()));
                break;
            case 0x26:
                newPAP.setBrcBottom(new BorderCode(sprm.getGrpprl(), sprm.getGrpprlOffset()));
                break;
            case 0x27:
                newPAP.setBrcRight(new BorderCode(sprm.getGrpprl(), sprm.getGrpprlOffset()));
                break;
            case 0x28:
                newPAP.setBrcBetween(new BorderCode(sprm.getGrpprl(), sprm.getGrpprlOffset()));
                break;
            case 0x29:
                newPAP.setBrcBar(new BorderCode(sprm.getGrpprl(), sprm.getGrpprlOffset()));
                break;
            case 0x2a:
                newPAP.setFNoAutoHyph(sprm.getOperand() != 0);
                break;
            case 0x2b:
                newPAP.setDyaHeight(sprm.getOperand());
                break;
            case 0x2c:
                newPAP.setDcs(new DropCapSpecifier((short) sprm.getOperand()));
                break;
            case 0x2d:
                newPAP.setShd(new ShadingDescriptor80((short) sprm.getOperand())
                        .toShadingDescriptor());
                break;
            case 0x2e:
                newPAP.setDyaFromText(sprm.getOperand());
                break;
            case 0x2f:
                newPAP.setDxaFromText(sprm.getOperand());
                break;
            case 0x30:
                newPAP.setFLocked(sprm.getOperand() != 0);
                break;
            case 0x31:
                newPAP.setFWidowControl(sprm.getOperand() != 0);
                break;
            case 0x33:
                newPAP.setFKinsoku(sprm.getOperand() != 0);
                break;
            case 0x34:
                newPAP.setFWordWrap(sprm.getOperand() != 0);
                break;
            case 0x35:
                newPAP.setFOverflowPunct(sprm.getOperand() != 0);
                break;
            case 0x36:
                newPAP.setFTopLinePunct(sprm.getOperand() != 0);
                break;
            case 0x37:
                newPAP.setFAutoSpaceDE(sprm.getOperand() != 0);
                break;
            case 0x38:
                newPAP.setFAutoSpaceDN(sprm.getOperand() != 0);
                break;
            case 0x39:
                newPAP.setWAlignFont(sprm.getOperand());
                break;
            case 0x3a:
                newPAP.setFontAlign((short) sprm.getOperand());
                break;
            case 0x3b:
                //obsolete
                break;
            case 0x3e: {
                // TODO: REMOVEME
                byte[] buf = Arrays.copyOfRange(sprm.getGrpprl(), sprm.getGrpprlOffset(), sprm.getGrpprlOffset() + (sprm.size() - 3));
                newPAP.setAnld(buf);
                break;
            }
            case 0x3f:
                //don't really need this. spec is confusing regarding this
                //sprm
                byte[] varParam = sprm.getGrpprl();
                int offset = sprm.getGrpprlOffset();
                newPAP.setFPropRMark(varParam[offset] != 0);
                newPAP.setIbstPropRMark(LittleEndian.getShort(varParam, offset + 1));
                newPAP.setDttmPropRMark(new DateAndTime(varParam, offset + 3));
                break;
            case 0x40:
                // This condition commented out, as Word seems to set outline levels even for
                //  paragraph with other styles than Heading 1..9, even though specification
                //  does not say so. See bug 49820 for discussion.
                //if (newPAP.getIstd () < 1 && newPAP.getIstd () > 9)
            {
                newPAP.setLvl((byte) sprm.getOperand());
            }
            break;
            case 0x41:
                // sprmPFBiDi
                newPAP.setFBiDi(sprm.getOperand() != 0);
                break;
            case 0x43:

                //pap.fNumRMIns
                newPAP.setFNumRMIns(sprm.getOperand() != 0);
                break;
            case 0x44:

                //undocumented
                break;
            case 0x45:
                if (sprm.getSizeCode() == 6) {
                    byte[] buf = new byte[sprm.size() - 3];
                    System.arraycopy(buf, 0, sprm.getGrpprl(), sprm.getGrpprlOffset(), buf.length);
                    newPAP.setNumrm(buf);
                } else {
                    /**@todo handle large PAPX from data stream*/
                }
                break;

            case 0x47:
                newPAP.setFUsePgsuSettings(sprm.getOperand() != 0);
                break;
            case 0x48:
                newPAP.setFAdjustRight(sprm.getOperand() != 0);
                break;
            case 0x49:
                // sprmPItap -- 0x6649
                newPAP.setItap(sprm.getOperand());
                break;
            case 0x4a:
                // sprmPDtap -- 0x664a
                newPAP.setItap((byte) (newPAP.getItap() + sprm.getOperand()));
                break;
            case 0x4b:
                // sprmPFInnerTableCell -- 0x244b
                newPAP.setFInnerTableCell(sprm.getOperand() != 0);
                break;
            case 0x4c:
                // sprmPFInnerTtp -- 0x244c
                newPAP.setFTtpEmbedded(sprm.getOperand() != 0);
                break;
            case 0x4d:
                // sprmPShd -- 0xc64d
                ShadingDescriptor shadingDescriptor = new ShadingDescriptor(
                        sprm.getGrpprl(), 3);
                newPAP.setShading(shadingDescriptor);
                break;
            case 0x5d:
                // sprmPDxaRight -- 0x845d
                newPAP.setDxaRight(sprm.getOperand());
                break;
            case 0x5e:
                // sprmPDxaLeft -- 0x845e
                newPAP.setDxaLeft(sprm.getOperand());
                break;
            case 0x60:
                // sprmPDxaLeft1 -- 0x8460
                newPAP.setDxaLeft1(sprm.getOperand());
                break;
            case 0x61:
                // sprmPJc
                newPAP.setJustificationLogical((byte) sprm.getOperand());
                break;
            case 0x67:
                // sprmPRsid -- 0x6467
                newPAP.setRsid(sprm.getOperand());
                break;
            default:
                LOG.atDebug().log("Unknown PAP sprm ignored: {}", sprm);
                break;
        }
    }

    private static void handleTabs(ParagraphProperties pap, SprmOperation sprm) {
        byte[] grpprl = sprm.getGrpprl();
        int offset = sprm.getGrpprlOffset();
        int delSize = grpprl[offset++];
        int[] tabPositions = pap.getRgdxaTab();
        TabDescriptor[] tabDescriptors = pap.getRgtbd();

        Map<Integer, TabDescriptor> tabMap = new HashMap<>();
        for (int x = 0; x < tabPositions.length; x++) {
            tabMap.put(tabPositions[x], tabDescriptors[x]);
        }

        for (int x = 0; x < delSize; x++) {
            tabMap.remove((int) LittleEndian.getShort(grpprl, offset));
            offset += LittleEndianConsts.SHORT_SIZE;
        }

        int addSize = grpprl[offset++];
        int start = offset;
        for (int x = 0; x < addSize; x++) {
            Integer key = (int) LittleEndian.getShort(grpprl, offset);
            TabDescriptor val = new TabDescriptor(grpprl, start + ((TBDAbstractType.getSize() * addSize) + x));
            tabMap.put(key, val);
            offset += LittleEndianConsts.SHORT_SIZE;
        }

        tabPositions = new int[tabMap.size()];
        tabDescriptors = new TabDescriptor[tabPositions.length];

        List<Integer> list = new ArrayList<>(tabMap.keySet());
        Collections.sort(list);

        for (int x = 0; x < tabPositions.length; x++) {
            Integer key = list.get(x);
            tabPositions[x] = key;
            if (tabMap.containsKey(key))
                tabDescriptors[x] = tabMap.get(key);
            else
                tabDescriptors[x] = new TabDescriptor();
        }

        pap.setRgdxaTab(tabPositions);
        pap.setRgtbd(tabDescriptors);
    }
}