-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathParagraphBuilder.java
More file actions
957 lines (895 loc) · 35.3 KB
/
Copy pathParagraphBuilder.java
File metadata and controls
957 lines (895 loc) · 35.3 KB
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
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
package com.demcha.compose.document.dsl;
import com.demcha.compose.document.emoji.EmojiLibrary;
import com.demcha.compose.document.image.DocumentImageData;
import com.demcha.compose.document.node.DocumentBookmarkOptions;
import com.demcha.compose.document.node.DocumentLinkOptions;
import com.demcha.compose.document.node.DocumentLinkTarget;
import com.demcha.compose.document.node.ExternalLinkTarget;
import com.demcha.compose.document.node.InternalLinkTarget;
import com.demcha.compose.document.node.InlineImageAlignment;
import com.demcha.compose.document.node.InlineShapeRun;
import com.demcha.compose.document.node.InlineSvgRun;
import com.demcha.compose.document.node.InlineImageRun;
import com.demcha.compose.document.node.ShapeLayer;
import com.demcha.compose.document.node.InlineRun;
import com.demcha.compose.document.node.InlineTextRun;
import com.demcha.compose.document.node.ParagraphNode;
import com.demcha.compose.document.node.TextAlign;
import com.demcha.compose.document.node.TextVerticalAlign;
import com.demcha.compose.document.style.DocumentColor;
import com.demcha.compose.document.style.DocumentInsets;
import com.demcha.compose.document.style.DocumentStroke;
import com.demcha.compose.document.style.DocumentTextAutoSize;
import com.demcha.compose.document.style.DocumentTextIndent;
import com.demcha.compose.document.style.DocumentTextStyle;
import com.demcha.compose.document.style.ShapeOutline;
import com.demcha.compose.document.svg.SvgIcon;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.function.Consumer;
/**
* Builder for semantic paragraph nodes and inline text runs.
* @since 1.0.0
*/
public final class ParagraphBuilder {
private String name = "";
private String text = "";
private final List<InlineRun> inlineRuns = new ArrayList<>();
private DocumentTextStyle textStyle = DocumentTextStyle.DEFAULT;
private TextAlign align = TextAlign.LEFT;
private double lineSpacing = 0.0;
private String bulletOffset = "";
private DocumentTextIndent indentStrategy = DocumentTextIndent.NONE;
private DocumentLinkTarget linkTarget;
private String anchor;
private DocumentBookmarkOptions bookmarkOptions;
private DocumentInsets padding = DocumentInsets.zero();
private DocumentInsets margin = DocumentInsets.zero();
private DocumentTextAutoSize autoSize;
private TextVerticalAlign verticalAlign = TextVerticalAlign.DEFAULT;
/**
* Creates a paragraph builder.
*/
public ParagraphBuilder() {
}
/**
* Sets the paragraph node name.
*
* @param name name used in snapshots and layout graph paths
* @return this builder
*/
public ParagraphBuilder name(String name) {
this.name = name == null ? "" : name;
return this;
}
/**
* Sets plain paragraph text and clears inline runs.
*
* @param text paragraph text
* @return this builder
*/
public ParagraphBuilder text(String text) {
this.text = text == null ? "" : text;
this.inlineRuns.clear();
return this;
}
/**
* Sets paragraph text style with the public canonical style value.
*
* @param textStyle paragraph text style
* @return this builder
*/
public ParagraphBuilder textStyle(DocumentTextStyle textStyle) {
this.textStyle = textStyle == null ? DocumentTextStyle.DEFAULT : textStyle;
return this;
}
/**
* Sets horizontal text alignment.
*
* @param align text alignment
* @return this builder
*/
public ParagraphBuilder align(TextAlign align) {
this.align = align == null ? TextAlign.LEFT : align;
return this;
}
/**
* Sets the vertical seating of the text within its line box.
*
* <p>{@link TextVerticalAlign#TOP}, {@code CENTER} and {@code BOTTOM} seat the
* line by its cap band (cap top to the box top, cap band centred, or baseline
* to the box bottom); pair them with a vertically-centred layer placement
* ({@code .center(...)} / {@code .centerLeft(...)}) to seat a single line
* inside a taller {@code ShapeContainer} / {@code LayerStack} layer.</p>
*
* <p>Tuned for a single line of text. A multi-line paragraph seats each line
* independently, and a line that mixes inline images with text is seated by
* the text cap height.</p>
*
* @param verticalAlign vertical text alignment, or {@code null} for
* {@link TextVerticalAlign#DEFAULT}
* @return this builder
* @since 1.7.0
*/
public ParagraphBuilder verticalAlign(TextVerticalAlign verticalAlign) {
this.verticalAlign = verticalAlign == null ? TextVerticalAlign.DEFAULT : verticalAlign;
return this;
}
/**
* Sets spacing between wrapped lines.
*
* @param lineSpacing line spacing in points
* @return this builder
*/
public ParagraphBuilder lineSpacing(double lineSpacing) {
this.lineSpacing = lineSpacing;
return this;
}
/**
* Sets the first-line prefix used by list-like paragraphs.
*
* @param bulletOffset first-line prefix
* @return this builder
*/
public ParagraphBuilder bulletOffset(String bulletOffset) {
this.bulletOffset = bulletOffset == null ? "" : bulletOffset;
return this;
}
/**
* Sets paragraph indentation behavior with the public canonical value.
*
* @param indentStrategy indent strategy
* @return this builder
*/
public ParagraphBuilder indentStrategy(DocumentTextIndent indentStrategy) {
this.indentStrategy = indentStrategy == null ? DocumentTextIndent.NONE : indentStrategy;
return this;
}
/**
* Attaches paragraph-level external link metadata.
*
* @param linkOptions link metadata
* @return this builder
*/
public ParagraphBuilder link(DocumentLinkOptions linkOptions) {
this.linkTarget = linkOptions == null ? null : new ExternalLinkTarget(linkOptions);
return this;
}
/**
* Attaches a paragraph-level link target (external URI or internal anchor).
*
* @param linkTarget link target, or {@code null} to clear
* @return this builder
* @since 1.9.0
*/
public ParagraphBuilder linkTarget(DocumentLinkTarget linkTarget) {
this.linkTarget = linkTarget;
return this;
}
/**
* Attaches a paragraph-level internal link to a named {@code anchor(...)}
* elsewhere in the document.
*
* @param anchor target anchor name
* @return this builder
* @since 1.9.0
*/
public ParagraphBuilder linkTo(String anchor) {
this.linkTarget = new InternalLinkTarget(anchor);
return this;
}
/**
* Declares a named in-document navigation anchor at this paragraph's
* top-left.
*
* @param anchor anchor name, or {@code null}/blank to clear
* @return this builder
* @since 1.9.0
*/
public ParagraphBuilder anchor(String anchor) {
this.anchor = anchor == null || anchor.isBlank() ? null : anchor.trim();
return this;
}
/**
* Adds a plain inline text run.
*
* @param text inline text
* @return this builder
*/
public ParagraphBuilder inlineText(String text) {
return inlineText(text, (DocumentTextStyle) null, null);
}
/**
* Adds an inline text run with a public canonical style value.
*
* @param text inline text
* @param textStyle inline text style
* @return this builder
*/
public ParagraphBuilder inlineText(String text, DocumentTextStyle textStyle) {
return inlineText(text, textStyle, null);
}
/**
* Adds an inline link run.
*
* @param text visible link text
* @param linkOptions link metadata
* @return this builder
*/
public ParagraphBuilder inlineLink(String text, DocumentLinkOptions linkOptions) {
return inlineText(text, (DocumentTextStyle) null, linkOptions);
}
/**
* Adds an inline internal-link run that jumps to a named {@code anchor(...)}
* elsewhere in the document.
*
* @param text visible link text
* @param anchor target anchor name
* @return this builder
* @since 1.9.0
*/
public ParagraphBuilder inlineLinkTo(String text, String anchor) {
this.inlineRuns.add(new InlineTextRun(text, null, new InternalLinkTarget(anchor)));
this.text = "";
return this;
}
/**
* Adds a styled inline text run with optional link metadata.
*
* @param text inline text
* @param textStyle inline text style
* @param linkOptions optional link metadata
* @return this builder
*/
public ParagraphBuilder inlineText(String text, DocumentTextStyle textStyle, DocumentLinkOptions linkOptions) {
this.inlineRuns.add(new InlineTextRun(text, textStyle, linkOptions));
this.text = "";
return this;
}
/**
* Adds an inline image run measured on the same baseline as the
* surrounding text. Width and height are explicit and uniform across
* backends; vertical alignment defaults to {@link InlineImageAlignment#CENTER}.
*
* @param imageData image payload
* @param width target width in points
* @param height target height in points
* @return this builder
*/
public ParagraphBuilder inlineImage(DocumentImageData imageData, double width, double height) {
return inlineImage(imageData, width, height, InlineImageAlignment.CENTER, 0.0, null);
}
/**
* Adds an inline image run with explicit vertical alignment.
*
* @param imageData image payload
* @param width target width in points
* @param height target height in points
* @param alignment vertical alignment relative to surrounding text
* @return this builder
*/
public ParagraphBuilder inlineImage(DocumentImageData imageData,
double width,
double height,
InlineImageAlignment alignment) {
return inlineImage(imageData, width, height, alignment, 0.0, null);
}
/**
* Adds an inline image run with optional link metadata. The link
* annotation covers only the image rectangle — wrap an adjacent text
* run in a matching {@code inlineLink} call to extend the hit area.
*
* @param imageData image payload
* @param width target width in points
* @param height target height in points
* @param alignment vertical alignment relative to surrounding text
* @param baselineOffset extra vertical shift in points; positive values move
* the image up
* @param linkOptions optional inline link metadata
* @return this builder
*/
public ParagraphBuilder inlineImage(DocumentImageData imageData,
double width,
double height,
InlineImageAlignment alignment,
double baselineOffset,
DocumentLinkOptions linkOptions) {
this.inlineRuns.add(new InlineImageRun(
imageData,
width,
height,
alignment == null ? InlineImageAlignment.CENTER : alignment,
baselineOffset,
linkOptions));
this.text = "";
return this;
}
/**
* Adds an inline image run that jumps to a named {@code anchor(...)} elsewhere
* in the document, with default {@link InlineImageAlignment#CENTER} alignment
* and zero offset.
*
* @param imageData image payload
* @param width target width in points
* @param height target height in points
* @param anchor target anchor name
* @return this builder
* @since 1.9.0
*/
public ParagraphBuilder inlineImageLinkTo(DocumentImageData imageData, double width, double height, String anchor) {
return inlineImageLinkTo(imageData, width, height, InlineImageAlignment.CENTER, 0.0, anchor);
}
/**
* Adds a fully-specified inline image run that jumps to a named
* {@code anchor(...)} elsewhere in the document.
*
* @param imageData image payload
* @param width target width in points
* @param height target height in points
* @param alignment vertical alignment relative to surrounding text
* @param baselineOffset extra vertical shift in points; positive moves up
* @param anchor target anchor name
* @return this builder
* @since 1.9.0
*/
public ParagraphBuilder inlineImageLinkTo(DocumentImageData imageData,
double width,
double height,
InlineImageAlignment alignment,
double baselineOffset,
String anchor) {
this.inlineRuns.add(new InlineImageRun(
imageData,
width,
height,
alignment == null ? InlineImageAlignment.CENTER : alignment,
baselineOffset,
new InternalLinkTarget(anchor)));
this.text = "";
return this;
}
/**
* Adds an inline filled circle ("dot") measured on the same baseline as the
* surrounding text — the building block for skill rating dots, custom
* bullets and status indicators that should not depend on font glyph
* coverage.
*
* @param diameter circle diameter in points
* @param fill fill color
* @return this builder
*/
public ParagraphBuilder dot(double diameter, DocumentColor fill) {
return shape(ShapeOutline.circle(diameter), fill, null, InlineImageAlignment.CENTER, 0.0, null);
}
/**
* Adds an inline circle with an explicit fill and/or outline stroke — for
* example a filled dot ({@code ●}) or an outlined one ({@code ○}).
*
* @param diameter circle diameter in points
* @param fill optional fill color
* @param stroke optional outline stroke
* @return this builder
*/
public ParagraphBuilder dot(double diameter, DocumentColor fill, DocumentStroke stroke) {
return shape(ShapeOutline.circle(diameter), fill, stroke, InlineImageAlignment.CENTER, 0.0, null);
}
/**
* Adds an inline ellipse measured on the surrounding text baseline.
*
* @param width target width in points
* @param height target height in points ({@code width == height} renders a circle)
* @param fill optional fill color
* @param stroke optional outline stroke
* @return this builder
*/
public ParagraphBuilder ellipse(double width, double height, DocumentColor fill, DocumentStroke stroke) {
return shape(new ShapeOutline.Ellipse(width, height), fill, stroke, InlineImageAlignment.CENTER, 0.0, null);
}
/**
* Adds an inline diamond (rhombus) sized {@code size × size}.
*
* @param size figure width and height in points
* @param fill fill color
* @return this builder
*/
public ParagraphBuilder diamond(double size, DocumentColor fill) {
return shape(ShapeOutline.diamond(size, size), fill, null, InlineImageAlignment.CENTER, 0.0, null);
}
/**
* Adds an inline upward-pointing triangle sized {@code size × size}.
*
* @param size figure width and height in points
* @param fill fill color
* @return this builder
*/
public ParagraphBuilder triangle(double size, DocumentColor fill) {
return shape(ShapeOutline.triangle(size, size), fill, null, InlineImageAlignment.CENTER, 0.0, null);
}
/**
* Adds an inline five-pointed star sized {@code size × size}.
*
* @param size figure width and height in points
* @param fill fill color
* @return this builder
*/
public ParagraphBuilder star(double size, DocumentColor fill) {
return shape(ShapeOutline.star(size, size), fill, null, InlineImageAlignment.CENTER, 0.0, null);
}
/**
* Adds an inline block arrow sized {@code size × size} pointing in
* {@code direction} — a directional marker between text or a list bullet.
*
* @param size figure width and height in points
* @param direction the way the arrow points
* @param fill fill color
* @return this builder
*/
public ParagraphBuilder arrow(double size, ShapeOutline.Direction direction, DocumentColor fill) {
return shape(ShapeOutline.arrow(size, size, direction), fill, null, InlineImageAlignment.CENTER, 0.0, null);
}
/**
* Adds an inline arrow of the given {@link ShapeOutline.ArrowStyle} — the
* swappable-design overload (block arrow, triangular arrowhead, …).
*
* @param size figure width and height in points
* @param direction the way the arrow points
* @param style the arrow design
* @param fill fill color
* @return this builder
* @since 1.7.0
*/
public ParagraphBuilder arrow(double size,
ShapeOutline.Direction direction,
ShapeOutline.ArrowStyle style,
DocumentColor fill) {
return shape(ShapeOutline.arrow(size, size, direction, style), fill, null,
InlineImageAlignment.CENTER, 0.0, null);
}
/**
* Adds an inline chevron sized {@code size × size} pointing in
* {@code direction} — a lighter directional separator for step lists.
*
* @param size figure width and height in points
* @param direction the way the chevron points
* @param fill fill color
* @return this builder
*/
public ParagraphBuilder chevron(double size, ShapeOutline.Direction direction, DocumentColor fill) {
return shape(ShapeOutline.chevron(size, size, direction), fill, null, InlineImageAlignment.CENTER, 0.0, null);
}
/**
* Adds an inline shape of any {@link ShapeOutline} kind with a filled
* interior, default {@link InlineImageAlignment#CENTER} alignment and zero
* offset.
*
* @param outline figure geometry; supplies the run's size
* @param fill fill color
* @return this builder
*/
public ParagraphBuilder shape(ShapeOutline outline, DocumentColor fill) {
return shape(outline, fill, null, InlineImageAlignment.CENTER, 0.0, null);
}
/**
* Adds an inline shape of any {@link ShapeOutline} kind, measured on the
* surrounding text baseline. At least one of {@code fill} or {@code stroke}
* must be present; vertical alignment defaults to
* {@link InlineImageAlignment#CENTER} when {@code null}. The figure is drawn
* from geometry, so it never depends on font glyph coverage.
*
* @param outline figure geometry; supplies the run's size
* @param fill optional fill color
* @param stroke optional outline stroke
* @param alignment vertical alignment relative to surrounding text
* @param baselineOffset extra vertical shift in points; positive moves up
* @param linkOptions optional inline link metadata
* @return this builder
*/
public ParagraphBuilder shape(ShapeOutline outline,
DocumentColor fill,
DocumentStroke stroke,
InlineImageAlignment alignment,
double baselineOffset,
DocumentLinkOptions linkOptions) {
this.inlineRuns.add(new InlineShapeRun(
outline,
fill,
stroke,
alignment == null ? InlineImageAlignment.CENTER : alignment,
baselineOffset,
linkOptions));
this.text = "";
return this;
}
/**
* Adds an inline SVG-icon run sized to {@code size} points tall, with
* default {@link InlineImageAlignment#CENTER} alignment and zero offset.
*
* <p>The icon is drawn as crisp vector layers on the text baseline, carrying
* its own colours — so it renders independently of the active font's glyph
* coverage. This is the inline path for vector colour emoji (e.g. Twemoji
* SVG) and small vector marks. The icon keeps its aspect ratio: the width is
* {@code size * icon.aspectRatio()}.</p>
*
* @param icon parsed vector icon; must not be {@code null}
* @param size target height in points (the icon's vertical extent on the line)
* @return this builder
* @since 1.9.0
*/
public ParagraphBuilder inlineSvgIcon(SvgIcon icon, double size) {
return inlineSvgIcon(icon, size, InlineImageAlignment.CENTER, 0.0, null);
}
/**
* Adds an inline SVG-icon run with explicit vertical alignment.
*
* @param icon parsed vector icon; must not be {@code null}
* @param size target height in points
* @param alignment vertical alignment relative to the surrounding text
* @return this builder
* @since 1.9.0
*/
public ParagraphBuilder inlineSvgIcon(SvgIcon icon, double size, InlineImageAlignment alignment) {
return inlineSvgIcon(icon, size, alignment, 0.0, null);
}
/**
* Adds a fully-specified, optionally clickable inline SVG-icon run, measured
* on the surrounding text baseline. The figure is drawn from vector geometry,
* so it never depends on font glyph coverage.
*
* @param icon parsed vector icon; must not be {@code null}
* @param size target height in points
* @param alignment vertical alignment relative to surrounding text
* @param baselineOffset extra vertical shift in points; positive moves up
* @param linkOptions optional inline link metadata
* @return this builder
* @since 1.9.0
*/
public ParagraphBuilder inlineSvgIcon(SvgIcon icon,
double size,
InlineImageAlignment alignment,
double baselineOffset,
DocumentLinkOptions linkOptions) {
Objects.requireNonNull(icon, "icon");
this.inlineRuns.add(new InlineSvgRun(
icon,
size * icon.aspectRatio(),
size,
alignment == null ? InlineImageAlignment.CENTER : alignment,
baselineOffset,
linkOptions));
this.text = "";
return this;
}
/**
* Adds a colour emoji resolved from its GitHub-style shortcode (e.g.
* {@code ":star:"}) as an inline vector glyph sized to {@code size} points
* tall, with default {@link InlineImageAlignment#CENTER} alignment.
*
* <p>Resolution is lenient: when the shortcode is unknown, or no emoji set is
* on the classpath (the {@code graph-compose-emoji} artifact), the literal
* shortcode is added as inline text — the way GitHub renders an unrecognised
* {@code :code:}. Resolution uses {@link EmojiLibrary#getDefault()}.</p>
*
* @param shortcode emoji shortcode, with or without surrounding colons
* @param size target height in points
* @return this builder
* @since 1.9.0
*/
public ParagraphBuilder inlineEmoji(String shortcode, double size) {
return inlineEmoji(shortcode, size, InlineImageAlignment.CENTER, 0.0, null);
}
/**
* Adds a colour emoji (see {@link #inlineEmoji(String, double)}) with explicit
* vertical alignment, baseline offset and optional link metadata.
*
* @param shortcode emoji shortcode, with or without surrounding colons
* @param size target height in points
* @param alignment vertical alignment relative to the surrounding text
* @param baselineOffset extra vertical shift in points; positive moves up
* @param linkOptions optional link metadata (ignored on the text fallback)
* @return this builder
* @since 1.9.0
*/
public ParagraphBuilder inlineEmoji(String shortcode,
double size,
InlineImageAlignment alignment,
double baselineOffset,
DocumentLinkOptions linkOptions) {
SvgIcon icon = EmojiLibrary.getDefault().find(shortcode).orElse(null);
if (icon != null) {
return inlineSvgIcon(icon, size, alignment, baselineOffset, linkOptions);
}
return inlineText(shortcode);
}
/**
* Adds an inline filled shape that jumps to a named {@code anchor(...)}
* elsewhere in the document, with default {@link InlineImageAlignment#CENTER}
* alignment and zero offset.
*
* @param outline figure geometry; supplies the run's size
* @param fill fill color
* @param anchor target anchor name
* @return this builder
* @since 1.9.0
*/
public ParagraphBuilder shapeLinkTo(ShapeOutline outline, DocumentColor fill, String anchor) {
return shapeLinkTo(outline, fill, null, InlineImageAlignment.CENTER, 0.0, anchor);
}
/**
* Adds a fully-specified inline shape that jumps to a named {@code anchor(...)}
* elsewhere in the document. At least one of {@code fill} or {@code stroke}
* must be present.
*
* @param outline figure geometry; supplies the run's size
* @param fill optional fill color
* @param stroke optional outline stroke
* @param alignment vertical alignment relative to surrounding text
* @param baselineOffset extra vertical shift in points; positive moves up
* @param anchor target anchor name
* @return this builder
* @since 1.9.0
*/
public ParagraphBuilder shapeLinkTo(ShapeOutline outline,
DocumentColor fill,
DocumentStroke stroke,
InlineImageAlignment alignment,
double baselineOffset,
String anchor) {
this.inlineRuns.add(new InlineShapeRun(
List.of(new ShapeLayer(outline, fill, stroke)),
alignment == null ? InlineImageAlignment.CENTER : alignment,
baselineOffset,
new InternalLinkTarget(anchor)));
this.text = "";
return this;
}
/**
* Adds an inline checkbox — a rounded square frame with an optional centred
* checkmark inside (the checked state), each in its own colour — for todo /
* checklist markers between text.
*
* @param size box width and height in points
* @param checked whether the checkmark is shown
* @param boxColor frame stroke color
* @param checkColor checkmark fill color
* @return this builder
*/
public ParagraphBuilder checkbox(double size, boolean checked, DocumentColor boxColor, DocumentColor checkColor) {
this.inlineRuns.add(InlineShapeRun.checkbox(size, checked, boxColor, checkColor));
this.text = "";
return this;
}
/**
* Adds an inline checkbox using one colour for both the frame and the
* checkmark.
*
* @param size box width and height in points
* @param checked whether the checkmark is shown
* @param color frame and checkmark color
* @return this builder
*/
public ParagraphBuilder checkbox(double size, boolean checked, DocumentColor color) {
return checkbox(size, checked, color, color);
}
/**
* Adds an inline checkbox whose checked-state tick uses the given
* {@link ShapeOutline.CheckmarkStyle} — the "pick your tick" overload.
*
* @param size box width and height in points
* @param checked whether the checkmark is shown
* @param markStyle design of the checked-state tick
* @param boxColor frame stroke color
* @param checkColor checkmark fill color
* @return this builder
* @since 1.7.0
*/
public ParagraphBuilder checkbox(double size,
boolean checked,
ShapeOutline.CheckmarkStyle markStyle,
DocumentColor boxColor,
DocumentColor checkColor) {
this.inlineRuns.add(InlineShapeRun.checkbox(size, checked, markStyle, boxColor, checkColor));
this.text = "";
return this;
}
/**
* Adds an inline checkbox whose checked-state mark is an arbitrary
* {@link ShapeOutline} — the power-user overload. Size the mark to fit the
* frame (≈ {@code 0.6 × size}); it is drawn centred in the box.
*
* @param size box width and height in points
* @param checked whether the mark is shown
* @param mark checked-state mark geometry, already sized; must be non-null
* when {@code checked} is {@code true}
* @param boxColor frame stroke color
* @param checkColor mark fill color
* @return this builder
* @since 1.7.0
*/
public ParagraphBuilder checkbox(double size,
boolean checked,
ShapeOutline mark,
DocumentColor boxColor,
DocumentColor checkColor) {
this.inlineRuns.add(InlineShapeRun.checkbox(size, checked, mark, boxColor, checkColor));
this.text = "";
return this;
}
/**
* Replaces inline runs with the contents of a {@link RichText} builder.
*
* @param rich rich-text builder; must not be {@code null}
* @return this builder
*/
public ParagraphBuilder rich(RichText rich) {
Objects.requireNonNull(rich, "rich");
return inlineRunsMixed(rich.runs());
}
/**
* Replaces inline runs by configuring a fresh {@link RichText} builder.
*
* <p>Convenient for inline declaration:
* {@code paragraph.rich(t -> t.text("Status: ").bold("Pending"))}.</p>
*
* @param spec callback that configures the rich-text builder
* @return this builder
*/
public ParagraphBuilder rich(Consumer<RichText> spec) {
Objects.requireNonNull(spec, "spec");
RichText builder = RichText.empty();
spec.accept(builder);
return inlineRunsMixed(builder.runs());
}
/**
* Replaces inline runs with text-only runs. Equivalent to
* {@link #inlineRunsMixed(List)} when the supplied list is text-only.
*
* @param inlineTextRuns inline text runs in source order
* @return this builder
*/
public ParagraphBuilder inlineRuns(List<InlineTextRun> inlineTextRuns) {
this.inlineRuns.clear();
if (inlineTextRuns != null) {
for (InlineTextRun run : inlineTextRuns) {
if (run != null) {
this.inlineRuns.add(run);
}
}
}
if (!this.inlineRuns.isEmpty()) {
this.text = "";
}
return this;
}
/**
* Replaces inline runs with a mixed list of text and image runs.
*
* @param runs inline runs in source order; may mix text and image
* @return this builder
*/
public ParagraphBuilder inlineRunsMixed(List<? extends InlineRun> runs) {
this.inlineRuns.clear();
if (runs != null) {
for (InlineRun run : runs) {
if (run != null) {
this.inlineRuns.add(run);
}
}
}
if (!this.inlineRuns.isEmpty()) {
this.text = "";
}
return this;
}
/**
* Attaches paragraph-level bookmark metadata.
*
* @param bookmarkOptions bookmark metadata
* @return this builder
*/
public ParagraphBuilder bookmark(DocumentBookmarkOptions bookmarkOptions) {
this.bookmarkOptions = bookmarkOptions;
return this;
}
/**
* Sets paragraph padding with the public canonical spacing value.
*
* @param padding padding in points
* @return this builder
*/
public ParagraphBuilder padding(DocumentInsets padding) {
this.padding = padding == null ? DocumentInsets.zero() : padding;
return this;
}
/**
* Sets paragraph padding from explicit side values.
*
* @param top top padding
* @param right right padding
* @param bottom bottom padding
* @param left left padding
* @return this builder
*/
public ParagraphBuilder padding(float top, float right, float bottom, float left) {
return padding(new DocumentInsets(top, right, bottom, left));
}
/**
* Sets paragraph margin with the public canonical spacing value.
*
* @param margin margin in points
* @return this builder
*/
public ParagraphBuilder margin(DocumentInsets margin) {
this.margin = margin == null ? DocumentInsets.zero() : margin;
return this;
}
/**
* Sets paragraph margin from explicit side values.
*
* @param top top margin
* @param right right margin
* @param bottom bottom margin
* @param left left margin
* @return this builder
*/
public ParagraphBuilder margin(float top, float right, float bottom, float left) {
return margin(new DocumentInsets(top, right, bottom, left));
}
/**
* Enables auto-size paragraph rendering. The layout pipeline searches the
* inclusive font-size range {@code [minSize, maxSize]} (in points) and
* picks the largest size that still renders the paragraph on a single line
* inside the resolved inner width. Pass {@code null} to clear.
*
* @param autoSize auto-size hint, or {@code null} to disable
* @return this builder
*/
public ParagraphBuilder autoSize(DocumentTextAutoSize autoSize) {
this.autoSize = autoSize;
return this;
}
/**
* Convenience overload of {@link #autoSize(DocumentTextAutoSize)} taking
* explicit max and min font sizes with the default search step.
*
* @param maxSize upper bound for the resolved font size in points
* @param minSize lower bound for the resolved font size in points
* @return this builder
*/
public ParagraphBuilder autoSize(double maxSize, double minSize) {
return autoSize(DocumentTextAutoSize.between(maxSize, minSize));
}
/**
* Convenience overload of {@link #autoSize(DocumentTextAutoSize)} that uses
* {@link DocumentTextAutoSize#DEFAULT_MIN_SIZE} as the lower bound.
*
* @param maxSize upper bound for the resolved font size in points
* @return this builder
*/
public ParagraphBuilder autoSize(double maxSize) {
return autoSize(DocumentTextAutoSize.upTo(maxSize));
}
/**
* Builds the semantic paragraph node.
*
* @return paragraph node
*/
public ParagraphNode build() {
return new ParagraphNode(
name,
text,
List.copyOf(inlineRuns),
textStyle,
align,
lineSpacing,
bulletOffset,
indentStrategy,
linkTarget,
bookmarkOptions,
padding,
margin,
autoSize,
verticalAlign,
anchor);
}
}