aboutsummaryrefslogtreecommitdiffstats
path: root/poi-ooxml/src/main/java/org/apache/poi/xddf/usermodel/text/XDDFTextParagraph.java
blob: 2fad9ddbbe5ef1d65a0ff7821997a3cc4f7d8cfb (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
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
/* ====================================================================
   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.xddf.usermodel.text;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;
import java.util.Spliterator;
import java.util.function.Function;
import java.util.function.Predicate;

import org.apache.commons.collections4.iterators.IteratorIterable;
import org.apache.commons.collections4.iterators.ReverseListIterator;
import org.apache.poi.ooxml.util.POIXMLUnits;
import org.apache.poi.util.Beta;
import org.apache.poi.util.Internal;
import org.apache.poi.util.LocaleUtil;
import org.apache.poi.util.Units;
import org.apache.poi.xddf.usermodel.XDDFColor;
import org.apache.xmlbeans.QNameSet;
import org.apache.xmlbeans.XmlObject;
import org.openxmlformats.schemas.drawingml.x2006.main.CTRegularTextRun;
import org.openxmlformats.schemas.drawingml.x2006.main.CTTextCharacterProperties;
import org.openxmlformats.schemas.drawingml.x2006.main.CTTextField;
import org.openxmlformats.schemas.drawingml.x2006.main.CTTextLineBreak;
import org.openxmlformats.schemas.drawingml.x2006.main.CTTextParagraph;
import org.openxmlformats.schemas.drawingml.x2006.main.CTTextParagraphProperties;
import org.openxmlformats.schemas.drawingml.x2006.main.CTTextSpacing;

/**
 * Represents a paragraph of text within the containing text body. The paragraph
 * is the highest level text separation mechanism.
 */
@Beta
public class XDDFTextParagraph implements Iterable<XDDFTextRun> {
    private XDDFTextBody _parent;
    private XDDFParagraphProperties _properties;
    private final CTTextParagraph _p;
    private final ArrayList<XDDFTextRun> _runs;

    @Internal
    protected XDDFTextParagraph(CTTextParagraph paragraph, XDDFTextBody parent) {
        this._p = paragraph;
        this._parent = parent;

        final int count = paragraph.sizeOfBrArray() + paragraph.sizeOfFldArray() + paragraph.sizeOfRArray();
        this._runs = new ArrayList<>(count);

        for (XmlObject xo : paragraph.selectChildren(QNameSet.ALL)) {
            if (xo instanceof CTTextLineBreak) {
                _runs.add(new XDDFTextRun((CTTextLineBreak) xo, this));
            } else if (xo instanceof CTTextField) {
                _runs.add(new XDDFTextRun((CTTextField) xo, this));
            } else if (xo instanceof CTRegularTextRun) {
                _runs.add(new XDDFTextRun((CTRegularTextRun) xo, this));
            }
        }

        addDefaultRunProperties();
        addAfterLastRunProperties();
    }

    public void setText(String text) {
        // keep the properties of current last run
        XmlObject existing = null;
        if (!_runs.isEmpty()) {
            existing = _runs.get(_runs.size() - 1).getProperties().copy();
        }
        // remove all runs
        for (int i = _p.sizeOfBrArray() - 1; i >= 0; i--) {
            _p.removeBr(i);
        }
        for (int i = _p.sizeOfFldArray() - 1; i >= 0; i--) {
            _p.removeFld(i);
        }
        for (int i = _p.sizeOfRArray() - 1; i >= 0; i--) {
            _p.removeR(i);
        }
        _runs.clear();
        XDDFTextRun run = appendRegularRun(text);
        if (existing != null) {
            run.getProperties().set(existing);
        }
    }

    public String getText() {
        StringBuilder out = new StringBuilder();
        for (XDDFTextRun r : _runs) {
            out.append(r.getText());
        }
        return out.toString();
    }

    public XDDFTextBody getParentBody() {
        return _parent;
    }

    public List<XDDFTextRun> getTextRuns() {
        return _runs;
    }

    @Override
    public Iterator<XDDFTextRun> iterator() {
        return _runs.iterator();
    }

    /**
     * @since POI 5.2.0
     */
    @Override
    public Spliterator<XDDFTextRun> spliterator() {
        return _runs.spliterator();
    }

    /**
     * Append a line break.
     *
     * @return text run representing this line break ('\n').
     */
    public XDDFTextRun appendLineBreak() {
        CTTextLineBreak br = _p.addNewBr();
        // by default, line break has the font properties of the last text run
        for (XDDFTextRun tr : new IteratorIterable<>(new ReverseListIterator<>(_runs))) {
            CTTextCharacterProperties prevProps = tr.getProperties();
            // let's find one which is not undefined
            if (prevProps != null) {
                br.setRPr((CTTextCharacterProperties) prevProps.copy());
                break;
            }
        }
        XDDFTextRun run = new XDDFTextRun(br, this);
        _runs.add(run);
        return run;
    }

    /**
     * Append a new text field.
     *
     * @return the new text field.
     */
    public XDDFTextRun appendField(String id, String type, String text) {
        CTTextField f = _p.addNewFld();
        f.setId(id);
        f.setType(type);
        f.setT(text);
        CTTextCharacterProperties rPr = f.addNewRPr();
        rPr.setLang(LocaleUtil.getUserLocale().toLanguageTag());
        XDDFTextRun run = new XDDFTextRun(f, this);
        _runs.add(run);
        return run;
    }

    /**
     * Append a new run of text.
     *
     * @return the new run of text.
     */
    public XDDFTextRun appendRegularRun(String text) {
        CTRegularTextRun r = _p.addNewR();
        r.setT(text);
        CTTextCharacterProperties rPr = r.addNewRPr();
        rPr.setLang(LocaleUtil.getUserLocale().toLanguageTag());
        XDDFTextRun run = new XDDFTextRun(r, this);
        _runs.add(run);
        return run;
    }

    /**
     * Returns the paragraph indentation level.
     *
     * @return indentation level of the paragraph.
     */
    public int getIndentationLevel() {
        if (_p.isSetPPr()) {
            return getProperties().getLevel();
        } else {
            return 0;
        }
    }

    /**
     * Specifies the paragraph indentation level, between 1 and 9.
     *
     * @param level
     *            new indentation level for the paragraph.
     *            Use <code>null</code> to unset the indentation level.
     */
    public void setIndentationLevel(Integer level) {
        if (_p.isSetPPr()) {
            getProperties().setLevel(level);
        }
    }

    /**
     * Returns the alignment that is applied to the paragraph.
     *
     * If this attribute is omitted, then a value of left is implied.
     *
     * @return alignment that is applied to the paragraph
     */
    public TextAlignment getTextAlignment() {
        return findDefinedParagraphProperty(
                CTTextParagraphProperties::isSetAlgn,
                CTTextParagraphProperties::getAlgn)
            .map(TextAlignment::valueOf).orElse(null);
    }

    /**
     * Specifies the alignment that is to be applied to the paragraph. Possible
     * values for this include left, right, centered, justified and distributed,
     *
     * @param align
     *            text alignment
     */
    public void setTextAlignment(TextAlignment align) {
        if (align != null || _p.isSetPPr()) {
            getOrCreateProperties().setTextAlignment(align);
        }
    }

    /**
     * Returns where vertically on a line of text the actual words are
     * positioned. This deals with vertical placement of the characters with
     * respect to the baselines.
     *
     * If this attribute is omitted, then a value of baseline is implied.
     *
     * @return alignment that is applied to the paragraph
     */
    public FontAlignment getFontAlignment() {
        return findDefinedParagraphProperty(
                CTTextParagraphProperties::isSetFontAlgn,
                CTTextParagraphProperties::getFontAlgn)
            .map(FontAlignment::valueOf).orElse(null);
    }

    /**
     * Determines where vertically on a line of text the actual words are
     * positioned. This deals with vertical placement of the characters with
     * respect to the baselines. For instance having text anchored to the top
     * baseline, anchored to the bottom baseline, centered in between, etc.
     *
     * @param align
     *            text font alignment
     */
    public void setFontAlignment(FontAlignment align) {
        if (align != null || _p.isSetPPr()) {
            getOrCreateProperties().setFontAlignment(align);
        }
    }

    /**
     *
     * @return the indentation, in points, applied to the first line of text in
     *         the paragraph.
     */
    public Double getIndentation() {
        return findDefinedParagraphProperty(
                CTTextParagraphProperties::isSetIndent,
                CTTextParagraphProperties::getIndent)
            .map(Units::toPoints).orElse(null);
    }

    /**
     * Specifies the indentation size that will be applied to the first line of
     * text in the paragraph.
     *
     * @param points
     *            the indentation in points. The value <code>null</code> unsets
     *            the indentation for this paragraph.
     *            <dl>
     *            <dt>Minimum inclusive =</dt>
     *            <dd>-4032</dd>
     *            <dt>Maximum inclusive =</dt>
     *            <dd>4032</dd>
     *            </dl>
     */
    public void setIndentation(Double points) {
        if (points != null || _p.isSetPPr()) {
            getOrCreateProperties().setIndentation(points);
        }
    }

    /**
     *
     * @return the left margin, in points, of the paragraph.
     */
    public Double getMarginLeft() {
        return findDefinedParagraphProperty(
                CTTextParagraphProperties::isSetMarL,
                CTTextParagraphProperties::getMarL)
            .map(Units::toPoints).orElse(null);
    }

    /**
     * Specifies the left margin of the paragraph. This is specified in addition
     * to the text body inset and applies only to this text paragraph. That is
     * the text body inset and the LeftMargin attributes are additive with
     * respect to the text position.
     *
     * @param points
     *            the margin in points. The value <code>null</code> unsets the
     *            left margin for this paragraph.
     *            <dl>
     *            <dt>Minimum inclusive =</dt>
     *            <dd>0</dd>
     *            <dt>Maximum inclusive =</dt>
     *            <dd>4032</dd>
     *            </dl>
     */
    public void setMarginLeft(Double points) {
        if (points != null || _p.isSetPPr()) {
            getOrCreateProperties().setMarginLeft(points);
        }
    }

    /**
     *
     * @return the right margin, in points, of the paragraph.
     */
    public Double getMarginRight() {
        return findDefinedParagraphProperty(
                CTTextParagraphProperties::isSetMarR,
                CTTextParagraphProperties::getMarR)
            .map(Units::toPoints).orElse(null);
    }

    /**
     * Specifies the right margin of the paragraph. This is specified in
     * addition to the text body inset and applies only to this text paragraph.
     * That is the text body inset and the RightMargin attributes are additive
     * with respect to the text position.
     *
     * @param points
     *            the margin in points. The value <code>null</code> unsets the
     *            right margin for this paragraph.
     *            <dl>
     *            <dt>Minimum inclusive =</dt>
     *            <dd>0</dd>
     *            <dt>Maximum inclusive =</dt>
     *            <dd>4032</dd>
     *            </dl>
     */
    public void setMarginRight(Double points) {
        if (points != null || _p.isSetPPr()) {
            getOrCreateProperties().setMarginRight(points);
        }
    }

    /**
     *
     * @return the default size for a tab character within this paragraph in
     *         points.
     */
    public Double getDefaultTabSize() {
        return findDefinedParagraphProperty(
                CTTextParagraphProperties::isSetDefTabSz,
                CTTextParagraphProperties::xgetDefTabSz)
            .map(POIXMLUnits::parseLength).map(Units::toPoints).orElse(null);
    }

    /**
     * Specifies the default size for a tab character within this paragraph.
     *
     * @param points
     *            the default tab size in points. The value <code>null</code>
     *            unsets the default tab size for this paragraph.
     */
    public void setDefaultTabSize(Double points) {
        if (points != null || _p.isSetPPr()) {
            getOrCreateProperties().setDefaultTabSize(points);
        }
    }

    /**
     * Returns the vertical line spacing that is to be used within a paragraph.
     * This may be specified in two different ways, percentage spacing or font
     * points spacing:
     * <p>
     * If line spacing is a percentage of normal line height, result is instance
     * of XDDFSpacingPercent. If line spacing is expressed in points, result is
     * instance of XDDFSpacingPoints.
     * </p>
     *
     * @return the vertical line spacing.
     */
    public XDDFSpacing getLineSpacing() {
        return findDefinedParagraphProperty(
                CTTextParagraphProperties::isSetLnSpc,
                CTTextParagraphProperties::getLnSpc)
            .map(this::extractSpacing).orElse(null);

    }

    /**
     * This element specifies the vertical line spacing that is to be used
     * within a paragraph. This may be specified in two different ways,
     * percentage spacing or font points spacing:
     * <p>
     * If spacing is instance of XDDFSpacingPercent, then line spacing is a
     * percentage of normal line height. If spacing is instance of
     * XDDFSpacingPoints, then line spacing is expressed in points.
     * </p>
     * Examples:
     *
     * <pre>
     * <code>
     *      // spacing will be 120% of the size of the largest text on each line
     *      paragraph.setLineSpacing(new XDDFSpacingPercent(120));
     *
     *      // spacing will be 200% of the size of the largest text on each line
     *      paragraph.setLineSpacing(new XDDFSpacingPercent(200));
     *
     *      // spacing will be 48 points
     *      paragraph.setLineSpacing(new XDDFSpacingPoints(48.0));
     * </code>
     * </pre>
     *
     * @param linespacing
     *            the vertical line spacing
     */
    public void setLineSpacing(XDDFSpacing linespacing) {
        if (linespacing != null || _p.isSetPPr()) {
            getOrCreateProperties().setLineSpacing(linespacing);
        }
    }

    /**
     * The amount of vertical white space before the paragraph. This may be
     * specified in two different ways, percentage spacing or font points
     * spacing:
     * <p>
     * If spacing is a percentage of normal line height, result is instance of
     * XDDFSpacingPercent. If spacing is expressed in points, result is instance
     * of XDDFSpacingPoints.
     * </p>
     *
     * @return the vertical white space before the paragraph.
     */
    public XDDFSpacing getSpaceBefore() {
        return findDefinedParagraphProperty(
                CTTextParagraphProperties::isSetSpcBef,
                CTTextParagraphProperties::getSpcBef)
            .map(this::extractSpacing).orElse(null);
    }

    /**
     * Set the amount of vertical white space that will be present before the
     * paragraph. This may be specified in two different ways, percentage
     * spacing or font points spacing:
     * <p>
     * If spacing is instance of XDDFSpacingPercent, then spacing is a
     * percentage of normal line height. If spacing is instance of
     * XDDFSpacingPoints, then spacing is expressed in points.
     * </p>
     * Examples:
     *
     * <pre>
     * <code>
     *      // The paragraph will be formatted to have a spacing before the paragraph text.
     *      // The spacing will be 200% of the size of the largest text on each line
     *      paragraph.setSpaceBefore(new XDDFSpacingPercent(200));
     *
     *      // The spacing will be a size of 48 points
     *      paragraph.setSpaceBefore(new XDDFSpacingPoints(48.0));
     * </code>
     * </pre>
     *
     * @param spaceBefore
     *            the vertical white space before the paragraph.
     */
    public void setSpaceBefore(XDDFSpacing spaceBefore) {
        if (spaceBefore != null || _p.isSetPPr()) {
            getOrCreateProperties().setSpaceBefore(spaceBefore);
        }
    }

    /**
     * The amount of vertical white space after the paragraph. This may be
     * specified in two different ways, percentage spacing or font points
     * spacing:
     * <p>
     * If spacing is a percentage of normal line height, result is instance of
     * XDDFSpacingPercent. If spacing is expressed in points, result is instance
     * of XDDFSpacingPoints.
     * </p>
     *
     * @return the vertical white space after the paragraph.
     */
    public XDDFSpacing getSpaceAfter() {
        return findDefinedParagraphProperty(
                CTTextParagraphProperties::isSetSpcAft,
                CTTextParagraphProperties::getSpcAft)
            .map(this::extractSpacing).orElse(null);
    }

    /**
     * Set the amount of vertical white space that will be present after the
     * paragraph. This may be specified in two different ways, percentage
     * spacing or font points spacing:
     * <p>
     * If spacing is instance of XDDFSpacingPercent, then spacing is a
     * percentage of normal line height. If spacing is instance of
     * XDDFSpacingPoints, then spacing is expressed in points.
     * </p>
     * Examples:
     *
     * <pre>
     * <code>
     *      // The paragraph will be formatted to have a spacing after the paragraph text.
     *      // The spacing will be 200% of the size of the largest text on each line
     *      paragraph.setSpaceAfter(new XDDFSpacingPercent(200));
     *
     *      // The spacing will be a size of 48 points
     *      paragraph.setSpaceAfter(new XDDFSpacingPoints(48.0));
     * </code>
     * </pre>
     *
     * @param spaceAfter
     *            the vertical white space after the paragraph.
     */
    public void setSpaceAfter(XDDFSpacing spaceAfter) {
        if (spaceAfter != null || _p.isSetPPr()) {
            getOrCreateProperties().setSpaceAfter(spaceAfter);
        }
    }

    /**
     *
     * @return the color of bullet characters within a given paragraph. A
     *         <code>null</code> value means to use the text font color.
     */
    public XDDFColor getBulletColor() {
        return findDefinedParagraphProperty(props -> props.isSetBuClr() || props.isSetBuClrTx(),
            props -> new XDDFParagraphBulletProperties(props).getBulletColor()).orElse(null);
    }

    /**
     * Set the color to be used on bullet characters within a given paragraph.
     *
     * @param color
     *            the bullet color
     */
    public void setBulletColor(XDDFColor color) {
        if (color != null || _p.isSetPPr()) {
            getOrCreateBulletProperties().setBulletColor(color);
        }
    }

    /**
     * Specifies the color to be used on bullet characters has to follow text
     * color within a given paragraph.
     */
    public void setBulletColorFollowText() {
        getOrCreateBulletProperties().setBulletColorFollowText();
    }

    /**
     *
     * @return the font of bullet characters within a given paragraph. A
     *         <code>null</code> value means to use the text font.
     */
    public XDDFFont getBulletFont() {
        return findDefinedParagraphProperty(props -> props.isSetBuFont() || props.isSetBuFontTx(),
            props -> new XDDFParagraphBulletProperties(props).getBulletFont()).orElse(null);
    }

    /**
     * Set the font to be used on bullet characters within a given paragraph.
     *
     * @param font
     *            the bullet font
     */
    public void setBulletFont(XDDFFont font) {
        if (font != null || _p.isSetPPr()) {
            getOrCreateBulletProperties().setBulletFont(font);
        }
    }

    /**
     * Specifies the font to be used on bullet characters has to follow text
     * font within a given paragraph.
     */
    public void setBulletFontFollowText() {
        getOrCreateBulletProperties().setBulletFontFollowText();
    }

    /**
     * Returns the bullet size that is to be used within a paragraph. This may
     * be specified in three different ways, follows text size, percentage size
     * and font points size:
     * <p>
     * If returned value is instance of XDDFBulletSizeFollowText, then bullet
     * size is text size; If returned value is instance of
     * XDDFBulletSizePercent, then bullet size is a percentage of the font size;
     * If returned value is instance of XDDFBulletSizePoints, then bullet size
     * is specified in points.
     * </p>
     *
     * @return the bullet size
     */
    public XDDFBulletSize getBulletSize() {
        return findDefinedParagraphProperty(
            props -> props.isSetBuSzPct() || props.isSetBuSzPts() || props.isSetBuSzTx(),
            props -> new XDDFParagraphBulletProperties(props).getBulletSize()).orElse(null);
    }

    /**
     * Sets the bullet size that is to be used within a paragraph. This may be
     * specified in three different ways, follows text size, percentage size and
     * font points size:
     * <p>
     * If given value is instance of XDDFBulletSizeFollowText, then bullet size
     * is text size; If given value is instance of XDDFBulletSizePercent, then
     * bullet size is a percentage of the font size; If given value is instance
     * of XDDFBulletSizePoints, then bullet size is specified in points.
     * </p>
     *
     * @param size
     *            the bullet size specification
     */
    public void setBulletSize(XDDFBulletSize size) {
        if (size != null || _p.isSetPPr()) {
            getOrCreateBulletProperties().setBulletSize(size);
        }
    }

    public XDDFBulletStyle getBulletStyle() {
        return findDefinedParagraphProperty(
            props -> props.isSetBuAutoNum() || props.isSetBuBlip() || props.isSetBuChar() || props.isSetBuNone(),
            props -> new XDDFParagraphBulletProperties(props).getBulletStyle()).orElse(null);
    }

    public void setBulletStyle(XDDFBulletStyle style) {
        if (style != null || _p.isSetPPr()) {
            getOrCreateBulletProperties().setBulletStyle(style);
        }
    }

    public boolean hasEastAsianLineBreak() {
        return findDefinedParagraphProperty(
                CTTextParagraphProperties::isSetEaLnBrk,
                CTTextParagraphProperties::getEaLnBrk)
            .orElse(false);
    }

    public void setEastAsianLineBreak(Boolean value) {
        if (value != null || _p.isSetPPr()) {
            getOrCreateProperties().setEastAsianLineBreak(value);
        }
    }

    public boolean hasLatinLineBreak() {
        return findDefinedParagraphProperty(
                CTTextParagraphProperties::isSetLatinLnBrk,
                CTTextParagraphProperties::getLatinLnBrk)
            .orElse(false);
    }

    public void setLatinLineBreak(Boolean value) {
        if (value != null || _p.isSetPPr()) {
            getOrCreateProperties().setLatinLineBreak(value);
        }
    }

    public boolean hasHangingPunctuation() {
        return findDefinedParagraphProperty(
                CTTextParagraphProperties::isSetHangingPunct,
                CTTextParagraphProperties::getHangingPunct)
            .orElse(false);
    }

    public void setHangingPunctuation(Boolean value) {
        if (value != null || _p.isSetPPr()) {
            getOrCreateProperties().setHangingPunctuation(value);
        }
    }

    public boolean isRightToLeft() {
        return findDefinedParagraphProperty(
                CTTextParagraphProperties::isSetRtl,
                CTTextParagraphProperties::getRtl)
            .orElse(false);
    }

    public void setRightToLeft(Boolean value) {
        if (value != null || _p.isSetPPr()) {
            getOrCreateProperties().setRightToLeft(value);
        }
    }

    public XDDFTabStop addTabStop() {
        return getOrCreateProperties().addTabStop();
    }

    public XDDFTabStop insertTabStop(int index) {
        return getOrCreateProperties().insertTabStop(index);
    }

    public void removeTabStop(int index) {
        if (_p.isSetPPr()) {
            getProperties().removeTabStop(index);
        }
    }

    public XDDFTabStop getTabStop(int index) {
        if (_p.isSetPPr()) {
            return getProperties().getTabStop(index);
        } else {
            return null;
        }
    }

    public List<XDDFTabStop> getTabStops() {
        if (_p.isSetPPr()) {
            return getProperties().getTabStops();
        } else {
            return Collections.emptyList();
        }
    }

    public int countTabStops() {
        if (_p.isSetPPr()) {
            return getProperties().countTabStops();
        } else {
            return 0;
        }
    }

    public XDDFParagraphBulletProperties getOrCreateBulletProperties() {
        return getOrCreateProperties().getBulletProperties();
    }

    public XDDFParagraphBulletProperties getBulletProperties() {
        if (_p.isSetPPr()) {
            return getProperties().getBulletProperties();
        } else {
            return null;
        }
    }

    /**
     * @since 4.0.1
     */
    public XDDFRunProperties addDefaultRunProperties() {
        return getOrCreateProperties().addDefaultRunProperties();
    }

    public XDDFRunProperties getDefaultRunProperties() {
        if (_p.isSetPPr()) {
            return getProperties().getDefaultRunProperties();
        } else {
            return null;
        }
    }

    public void setDefaultRunProperties(XDDFRunProperties properties) {
        if (properties != null || _p.isSetPPr()) {
            getOrCreateProperties().setDefaultRunProperties(properties);
        }
    }

    public XDDFRunProperties addAfterLastRunProperties() {
        if (!_p.isSetEndParaRPr()) {
            _p.addNewEndParaRPr();
        }
        return getAfterLastRunProperties();
    }

    public XDDFRunProperties getAfterLastRunProperties() {
        if (_p.isSetEndParaRPr()) {
            return new XDDFRunProperties(_p.getEndParaRPr());
        } else {
            return null;
        }
    }

    public void setAfterLastRunProperties(XDDFRunProperties properties) {
        if (properties == null) {
            if (_p.isSetEndParaRPr()) {
                _p.unsetEndParaRPr();
            }
        } else {
            _p.setEndParaRPr(properties.getXmlObject());
        }
    }

    private XDDFSpacing extractSpacing(CTTextSpacing spacing) {
        if (spacing.isSetSpcPct()) {
            double scale = 1 - _parent.getBodyProperties().getAutoFit().getLineSpaceReduction() / 100_000.0;
            return new XDDFSpacingPercent(spacing, spacing.getSpcPct(), scale);
        } else if (spacing.isSetSpcPts()) {
            return new XDDFSpacingPoints(spacing, spacing.getSpcPts());
        }
        return null;
    }

    private XDDFParagraphProperties getProperties() {
        if (_properties == null) {
            _properties = new XDDFParagraphProperties(_p.getPPr());
        }
        return _properties;
    }

    private XDDFParagraphProperties getOrCreateProperties() {
        if (!_p.isSetPPr()) {
            _properties = new XDDFParagraphProperties(_p.addNewPPr());
        }
        return getProperties();
    }

    protected <R> Optional<R> findDefinedParagraphProperty(Predicate<CTTextParagraphProperties> isSet,
        Function<CTTextParagraphProperties, R> getter) {
        if (_p.isSetPPr()) {
            int level = _p.getPPr().isSetLvl() ? 1 + _p.getPPr().getLvl() : 0;
            return findDefinedParagraphProperty(isSet, getter, level);
        } else {
            return _parent.findDefinedParagraphProperty(isSet, getter, 0);
        }
    }

    private <R> Optional<R> findDefinedParagraphProperty(Predicate<CTTextParagraphProperties> isSet,
        Function<CTTextParagraphProperties, R> getter, int level) {
        final CTTextParagraphProperties props = _p.getPPr();
        if (props != null && isSet.test(props)) {
            return Optional.ofNullable(getter.apply(props));
        } else {
            return _parent.findDefinedParagraphProperty(isSet, getter, level);
        }
    }

    protected <R> Optional<R> findDefinedRunProperty(Predicate<CTTextCharacterProperties> isSet,
        Function<CTTextCharacterProperties, R> getter) {
        if (_p.isSetPPr()) {
            int level = _p.getPPr().isSetLvl() ? 1 + _p.getPPr().getLvl() : 0;
            return findDefinedRunProperty(isSet, getter, level);
        } else {
            return _parent.findDefinedRunProperty(isSet, getter, 0);
        }
    }

    private <R> Optional<R> findDefinedRunProperty(Predicate<CTTextCharacterProperties> isSet,
        Function<CTTextCharacterProperties, R> getter, int level) {
        final CTTextCharacterProperties props = _p.getPPr().isSetDefRPr() ? _p.getPPr().getDefRPr() : null;
        if (props != null && isSet.test(props)) {
            return Optional.ofNullable(getter.apply(props));
        } else {
            return _parent.findDefinedRunProperty(isSet, getter, level);
        }
    }
}