aboutsummaryrefslogtreecommitdiffstats
path: root/poi-scratchpad/src/main/java/org/apache/poi/hwpf/usermodel/Paragraph.java
blob: 285d73402cfc652a1dda36d2010f4fd7c328919a (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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
/* ====================================================================
   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.usermodel;

import java.util.NoSuchElementException;

import org.apache.logging.log4j.Logger;
import org.apache.poi.common.Duplicatable;
import org.apache.poi.hwpf.HWPFDocumentCore;
import org.apache.poi.hwpf.model.LFO;
import org.apache.poi.hwpf.model.ListLevel;
import org.apache.poi.hwpf.model.ListTables;
import org.apache.poi.hwpf.model.PAPX;
import org.apache.poi.hwpf.model.StyleSheet;
import org.apache.poi.hwpf.sprm.ParagraphSprmUncompressor;
import org.apache.poi.hwpf.sprm.SprmBuffer;
import org.apache.poi.hwpf.sprm.TableSprmCompressor;
import org.apache.poi.logging.PoiLogManager;
import org.apache.poi.util.Internal;

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

public class Paragraph extends Range implements Duplicatable {
    private static final Logger LOGGER = PoiLogManager.getLogger(Paragraph.class);

    public static final short SPRM_JC = 0x2403;
    public static final short SPRM_FSIDEBYSIDE = 0x2404;
    public static final short SPRM_FKEEP = 0x2405;
    public static final short SPRM_FKEEPFOLLOW = 0x2406;
    public static final short SPRM_FPAGEBREAKBEFORE = 0x2407;
    public static final short SPRM_BRCL = 0x2408;
    public static final short SPRM_BRCP = 0x2409;
    public static final short SPRM_ILVL = 0x260A;
    public static final short SPRM_ILFO = 0x460B;
    public static final short SPRM_FNOLINENUMB = 0x240C;
    public static final short SPRM_CHGTABSPAPX = (short)0xC60D;
    public static final short SPRM_DXARIGHT = (short)0x840E;
    public static final short SPRM_DXALEFT = (short)0x840F;
    public static final short SPRM_DXALEFT1 = (short)0x8411;
    public static final short SPRM_DYALINE = 0x6412;
    public static final short SPRM_DYABEFORE = (short)0xA413;
    public static final short SPRM_DYAAFTER = (short)0xA414;
    public static final short SPRM_CHGTABS = (short)0xC615;
    public static final short SPRM_FINTABLE = 0x2416;
    public static final short SPRM_FTTP = 0x2417;
    public static final short SPRM_DXAABS = (short)0x8418;
    public static final short SPRM_DYAABS = (short)0x8419;
    public static final short SPRM_DXAWIDTH = (short)0x841A;
    public static final short SPRM_PC = 0x261B;
    public static final short SPRM_WR = 0x2423;
    public static final short SPRM_BRCTOP = 0x6424;
    public static final short SPRM_BRCLEFT = 0x6425;
    public static final short SPRM_BRCBOTTOM = 0x6426;
    public static final short SPRM_BRCRIGHT = 0x6427;
    public static final short SPRM_BRCBAR = 0x6629;
    public static final short SPRM_FNOAUTOHYPH = 0x242A;
    public static final short SPRM_WHEIGHTABS = 0x442B;
    public static final short SPRM_DCS = 0x442C;
    public static final short SPRM_SHD80 = 0x442D;
    public static final short SPRM_SHD = (short)0xC64D;
    public static final short SPRM_DYAFROMTEXT = (short)0x842E;
    public static final short SPRM_DXAFROMTEXT = (short)0x842F;
    public static final short SPRM_FLOCKED = 0x2430;
    public static final short SPRM_FWIDOWCONTROL = 0x2431;
    public static final short SPRM_RULER = (short)0xC632;
    public static final short SPRM_FKINSOKU = 0x2433;
    public static final short SPRM_FWORDWRAP = 0x2434;
    public static final short SPRM_FOVERFLOWPUNCT = 0x2435;
    public static final short SPRM_FTOPLINEPUNCT = 0x2436;
    public static final short SPRM_AUTOSPACEDE = 0x2437;
    public static final short SPRM_AUTOSPACEDN = 0x2438;
    public static final short SPRM_WALIGNFONT = 0x4439;
    public static final short SPRM_FRAMETEXTFLOW = 0x443A;
    public static final short SPRM_ANLD = (short)0xC63E;
    public static final short SPRM_PROPRMARK = (short)0xC63F;
    public static final short SPRM_OUTLVL = 0x2640;
    public static final short SPRM_FBIDI = 0x2441;
    public static final short SPRM_FNUMRMLNS = 0x2443;
    public static final short SPRM_CRLF = 0x2444;
    public static final short SPRM_NUMRM = (short)0xC645;
    public static final short SPRM_USEPGSUSETTINGS = 0x2447;
    public static final short SPRM_FADJUSTRIGHT = 0x2448;

    @Internal
    public static Paragraph newParagraph( Range parent, PAPX papx )
    {
        HWPFDocumentCore doc = parent._doc;
        ListTables listTables = doc.getListTables();
        StyleSheet styleSheet = doc.getStyleSheet();

        ParagraphProperties properties = new ParagraphProperties();
        properties.setIstd( papx.getIstd() );

        properties = newParagraph_applyStyleProperties( styleSheet, papx,
                properties );
        properties = ParagraphSprmUncompressor.uncompressPAP( properties,
                papx.getGrpprl(), 2 );

        if ( properties.getIlfo() != 0 && listTables != null )
        {
            LFO lfo = null;
            try
            {
                lfo = listTables.getLfo( properties.getIlfo() );
            }
            catch ( NoSuchElementException exc )
            {
                LOGGER.atWarn().log("Paragraph refers to LFO #{} that does not exists", box(properties.getIlfo()));
            }
            if ( lfo != null )
            {
                final ListLevel listLevel = listTables.getLevel( lfo.getLsid(),
                        properties.getIlvl() );

                if ( listLevel != null && listLevel.getGrpprlPapx() != null )
                {
                    properties = ParagraphSprmUncompressor.uncompressPAP(
                            properties, listLevel.getGrpprlPapx(), 0 );
                    // reapply style and local PAPX properties
                    properties = newParagraph_applyStyleProperties( styleSheet,
                            papx, properties );
                    properties = ParagraphSprmUncompressor.uncompressPAP(
                            properties, papx.getGrpprl(), 2 );
                }
            }
        }

        if ( properties.getIlfo() > 0 )
            return new ListEntry( papx, properties, parent );

        return new Paragraph( papx, properties, parent );
    }

    protected static ParagraphProperties newParagraph_applyStyleProperties(
            StyleSheet styleSheet, PAPX papx, ParagraphProperties properties )
    {
        if ( styleSheet == null )
            return properties;

        int style = papx.getIstd();
        byte[] grpprl = styleSheet.getPAPX( style );
        return ParagraphSprmUncompressor.uncompressPAP( properties, grpprl, 2 );
    }

  protected short _istd;
  protected ParagraphProperties _props;
  protected SprmBuffer _papx;

    @Internal
    Paragraph( PAPX papx, ParagraphProperties properties, Range parent )
    {
        super( Math.max( parent._start, papx.getStart() ), Math.min(
                parent._end, papx.getEnd() ), parent );
        _props = properties;
        _papx = papx.getSprmBuf();
        _istd = papx.getIstd();
    }

    Paragraph(Paragraph other) {
        super(other);
        _istd = other._istd;
        _props = (other._props == null) ? null : other._props.copy();
        _papx = (other._papx == null) ? null : other._papx.copy();
    }

  /**
   * Returns the index of the style which applies to this
   *  Paragraph. Details of the style can be looked up
   *  from the {@link StyleSheet}, via
   *  {@link StyleSheet#getStyleDescription(int)}
   */
  public short getStyleIndex()
  {
     return _istd;
  }

  public boolean isInTable()
  {
    return _props.getFInTable();
  }

  /**
   * @return {@code true}, if table trailer paragraph (last in table row),
   *         {@code false} otherwise
   */
  public boolean isTableRowEnd()
  {
    return _props.getFTtp() || _props.getFTtpEmbedded();
  }

  public int getTableLevel()
  {
    return _props.getItap();
  }

    /**
     * @return {@code true}, if the end of paragraph mark is really an end of
     *         cell mark for a nested table cell, {@code false} otherwise
     */
    public boolean isEmbeddedCellMark()
    {
        return _props.getFInnerTableCell();
    }

  public int getJustification()
  {
    return _props.getJc();
  }

  public void setJustification(byte jc)
  {
    _props.setJc(jc);
    _papx.updateSprm(SPRM_JC, jc);
  }

  public boolean keepOnPage()
  {
    return _props.getFKeep();
  }

  public void setKeepOnPage(boolean fKeep)
  {
    _props.setFKeep(fKeep);
    _papx.updateSprm(SPRM_FKEEP, fKeep);
  }

  public boolean keepWithNext()
  {
    return _props.getFKeepFollow();
  }

  public void setKeepWithNext(boolean fKeepFollow)
  {
    _props.setFKeepFollow(fKeepFollow);
    _papx.updateSprm(SPRM_FKEEPFOLLOW, fKeepFollow);
  }

  public boolean pageBreakBefore()
  {
    return _props.getFPageBreakBefore();
  }

  public void setPageBreakBefore(boolean fPageBreak)
  {
    _props.setFPageBreakBefore(fPageBreak);
    _papx.updateSprm(SPRM_FPAGEBREAKBEFORE, fPageBreak);
  }

  public boolean isLineNotNumbered()
  {
    return _props.getFNoLnn();
  }

  public void setLineNotNumbered(boolean fNoLnn)
  {
    _props.setFNoLnn(fNoLnn);
    _papx.updateSprm(SPRM_FNOLINENUMB, fNoLnn);
  }

  public boolean isSideBySide()
  {
    return _props.getFSideBySide();
  }

  public void setSideBySide(boolean fSideBySide)
  {
    _props.setFSideBySide(fSideBySide);
    _papx.updateSprm(SPRM_FSIDEBYSIDE, fSideBySide);
  }

  public boolean isAutoHyphenated()
  {
    return !_props.getFNoAutoHyph();
  }

  public void setAutoHyphenated(boolean autoHyph)
  {
    _props.setFNoAutoHyph(!autoHyph);
    _papx.updateSprm(SPRM_FNOAUTOHYPH, !autoHyph);
  }

  public boolean isWidowControlled()
  {
    return _props.getFWidowControl();
  }

  public void setWidowControl(boolean widowControl)
  {
    _props.setFWidowControl(widowControl);
    _papx.updateSprm(SPRM_FWIDOWCONTROL, widowControl);
  }

  public int getIndentFromRight()
  {
    return _props.getDxaRight();
  }

  public void setIndentFromRight(int dxaRight)
  {
    _props.setDxaRight(dxaRight);
    _papx.updateSprm(SPRM_DXARIGHT, (short)dxaRight);
  }

  public int getIndentFromLeft()
  {
    return _props.getDxaLeft();
  }

  public void setIndentFromLeft(int dxaLeft)
  {
    _props.setDxaLeft(dxaLeft);
    _papx.updateSprm(SPRM_DXALEFT, (short)dxaLeft);
  }

  public int getFirstLineIndent()
  {
    return _props.getDxaLeft1();
  }

  public void setFirstLineIndent(int first)
  {
    _props.setDxaLeft1(first);
    _papx.updateSprm(SPRM_DXALEFT1, (short)first);
  }

  public LineSpacingDescriptor getLineSpacing()
  {
    return _props.getLspd();
  }

  public void setLineSpacing(LineSpacingDescriptor lspd)
  {
    _props.setLspd(lspd);
    _papx.updateSprm(SPRM_DYALINE, lspd.toInt());
  }

  public int getSpacingBefore()
  {
    return _props.getDyaBefore();
  }

  public void setSpacingBefore(int before)
  {
    _props.setDyaBefore(before);
    _papx.updateSprm(SPRM_DYABEFORE, (short)before);
  }

  public int getSpacingAfter()
  {
    return _props.getDyaAfter();
  }

  public void setSpacingAfter(int after)
  {
    _props.setDyaAfter(after);
    _papx.updateSprm(SPRM_DYAAFTER, (short)after);
  }

  public boolean isKinsoku()
  {
    return _props.getFKinsoku();
  }

  public void setKinsoku(boolean kinsoku)
  {
    _props.setFKinsoku(kinsoku);
    _papx.updateSprm(SPRM_FKINSOKU, kinsoku);
  }

  public boolean isWordWrapped()
  {
    return _props.getFWordWrap();
  }

  public void setWordWrapped(boolean wrap)
  {
    _props.setFWordWrap(wrap);
    _papx.updateSprm(SPRM_FWORDWRAP, wrap);
  }

  public int getFontAlignment()
  {
    return _props.getWAlignFont();
  }

  public void setFontAlignment(int align)
  {
    _props.setWAlignFont(align);
    _papx.updateSprm(SPRM_WALIGNFONT, (short)align);
  }

  public boolean isVertical()
  {
    return _props.isFVertical();
  }

  public void setVertical(boolean vertical)
  {
    _props.setFVertical(vertical);
    _papx.updateSprm(SPRM_FRAMETEXTFLOW, getFrameTextFlow());
  }

  public boolean isBackward()
  {
    return _props.isFBackward();
  }

  public void setBackward(boolean bward)
  {
    _props.setFBackward(bward);
    _papx.updateSprm(SPRM_FRAMETEXTFLOW, getFrameTextFlow());
  }

  public BorderCode getTopBorder()
  {
    return _props.getBrcTop();
  }

  public void setTopBorder(BorderCode top)
  {
    _props.setBrcTop(top);
    _papx.updateSprm(SPRM_BRCTOP, top.toInt());
  }

  public BorderCode getLeftBorder()
  {
    return _props.getBrcLeft();
  }

  public void setLeftBorder(BorderCode left)
  {
    _props.setBrcLeft(left);
    _papx.updateSprm(SPRM_BRCLEFT, left.toInt());
  }

  public BorderCode getBottomBorder()
  {
    return _props.getBrcBottom();
  }

  public void setBottomBorder(BorderCode bottom)
  {
    _props.setBrcBottom(bottom);
    _papx.updateSprm(SPRM_BRCBOTTOM, bottom.toInt());
  }

  public BorderCode getRightBorder()
  {
    return _props.getBrcRight();
  }

  public void setRightBorder(BorderCode right)
  {
    _props.setBrcRight(right);
    _papx.updateSprm(SPRM_BRCRIGHT, right.toInt());
  }

  public BorderCode getBarBorder()
  {
    return _props.getBrcBar();
  }

  public void setBarBorder(BorderCode bar)
  {
    _props.setBrcBar(bar);
    _papx.updateSprm(SPRM_BRCBAR, bar.toInt());
  }

  public ShadingDescriptor getShading()
  {
    return _props.getShd();
  }

  public void setShading(ShadingDescriptor shd)
  {
    _props.setShd(shd);
    //TODO: remove old one
    _papx.addSprm( SPRM_SHD, shd.serialize() );
  }

  public DropCapSpecifier getDropCap()
  {
    return _props.getDcs();
  }

  public void setDropCap(DropCapSpecifier dcs)
  {
    _props.setDcs(dcs);
    _papx.updateSprm(SPRM_DCS, dcs.toShort());
  }

  /**
   * Returns the ilfo, an index to the document's hpllfo, which
   *  describes the automatic number formatting of the paragraph.
   * A value of zero means it isn't numbered.
   */
  public int getIlfo()
  {
     return _props.getIlfo();
  }

  /**
   * Returns the multi-level indent for the paragraph. Will be
   *  zero for non-list paragraphs, and the first level of any
   *  list. Subsequent levels in hold values 1-8.
   */
  public int getIlvl()
  {
     return _props.getIlvl();
  }

  /**
   * Returns the heading level (1-8), or 9 if the paragraph
   *  isn't in a heading style.
   */
  public int getLvl()
  {
     return _props.getLvl();
  }

  void setTableRowEnd(TableProperties props)
  {
    setTableRowEnd(true);
    byte[] grpprl = TableSprmCompressor.compressTableProperty(props);
    _papx.append(grpprl);
  }

  private void setTableRowEnd(boolean val)
  {
    _props.setFTtp(val);
    _papx.updateSprm(SPRM_FTTP, val);
  }

    /**
     * Returns number of tabs stops defined for paragraph. Must be >= 0 and <= 64.
     *
     * @return number of tabs stops defined for paragraph. Must be >= 0 and <= 64
     */
    public int getTabStopsNumber()
    {
        return _props.getItbdMac();
    }

    /**
     * Returns array of positions of itbdMac tab stops
     *
     * @return array of positions of itbdMac tab stops
     */
    public int[] getTabStopsPositions()
    {
        return _props.getRgdxaTab();
    }

    public HWPFList getList()
    {
        if ( getIlfo() == 0x000 || getIlfo() == 0xF801 )
        {
            throw new IllegalStateException( "Paragraph not in list" );
        }
        return new HWPFList( getDocument().getStyleSheet(),
                getDocument().getListTables(), getIlfo() );
    }

    public boolean isInList()
    {
        return getIlfo() != 0x000 && getIlfo() != 0xF801;
    }

    /**
     * Clone the ParagraphProperties object associated with this
     *  Paragraph, so that you can apply the same properties to
     *  another Paragraph.
     */
    public ParagraphProperties cloneProperties() {
        return _props.copy();
    }

    @Override
    public Paragraph copy() {
        return new Paragraph(this);
    }

    private short getFrameTextFlow()
    {
        short retVal = 0;
        if (_props.isFVertical())
        {
            retVal |= 1;
        }
        if (_props.isFBackward())
        {
            retVal |= 2;
        }
        if (_props.isFRotateFont())
        {
            retVal |= 4;
        }
        return retVal;
    }

    @Override
    public String toString()
    {
        return "Paragraph [" + getStartOffset() + "; " + getEndOffset() + ")";
    }

    @Internal
    public ParagraphProperties getProps() {
        return _props;
    }
}