Class RichText
- All Implemented Interfaces:
AttributedCharSequence
,ToRichText
,CharSequence
,Iterable<Run>
Sequences of characters that share the same formatting attributes form a
Run
. Use RichTextBuilder
create RichText
instances.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final char
A character used as a delimiter or marker to insert where subsequent text segment should not be joined into a single one. -
Method Summary
Modifier and TypeMethodDescriptionvoid
appendTo
(RichTextBuilder builder) Append this object's rich text representation to a buffer.Gat styled copy of this instance.attributedCharAt
(int index) Returns theAttributedCharacter
value at the specified index.char
charAt
(int index) boolean
Test if CharSequence is contained.static RichText
Returns the empty String as RichText.static BiPredicate
<@Nullable RichText, @Nullable RichText> Returns a BiPredicate that checks whether two RichText objects are equal based on the provided ComparisonSettings.boolean
equals
(@Nullable RichText other, BiPredicate<? super Run, ? super Run> runEquals) Compare using a user supplied predicate for comparing Run instances.boolean
boolean
equalsText
(@Nullable CharSequence other) Textual compare.boolean
equalsTextAndFont
(@Nullable RichText other) Check if texts and style are equal, ignoring other attributes.boolean
equalsTextIgnoreCase
(@Nullable CharSequence other) Textual compare ignoring case.double
getBaseline
(Font defaultFont) Calculates the baseline value based on the maximum descent of fonts within the runs of text.int
hashCode()
int
indexOf
(char ch, int off) Find character.int
indexOf
(int ch) Find character.int
Return the index of the needle in this RichText instance.int
indexOf
(CharSequence s, int fromIndex) Return the index of the needle in this RichText instance.boolean
isBlank()
Test if blank.iterator()
static RichText
Join RichText instances together.static RichText
Join RichText instances together.static RichText
join
(CharSequence delimiter, RichText... elements) Join RichText instances together.static RichText
join
(CharSequence delimiter, Iterable<RichText> elements) Join RichText instances together.static RichTextJoiner
Create aRichTextJoiner
.static RichTextJoiner
Create aRichTextJoiner
.static RichTextJoiner
Create aRichTextJoiner
.static RichTextJoiner
Create aRichTextJoiner
.int
length()
Length of text in characters.lines()
Get stream of lines contained in this instance.static RichTextMatcher
Create aRichTextMatcher
.static RichText
newline()
Returns RichText containing a single newline character.replaceAll
(String regex, RichText replacement) Replaces each substring of this RichText that matches the given regular expression with the specified RichText replacement.replaceAll
(String regex, String replacement) Replaces every occurrence of the specified regular expression in the text with the given replacement string.replaceFirst
(String regex, RichText replacement) Replaces the first substring of this RichText that matches the given regular expression with the specified replacement RichText.replaceFirst
(String regex, String replacement) Replaces the first substring of this RichText object that matches the given regular expression with the specified replacement string.runAt
(int pos) Get run at position.runs()
Get the runs of this instance.Get a stream containing the runs of this instance.static RichText
space()
Returns RichText containing a single space character.RichText[]
Splits this RichText into an array of RichText objects based on the provided regular expression.RichText[]
Splits the current RichText object into an array of RichText objects based on the given regular expression and limit.RichText[]
Splits the current RichText into an array of RichText instances based on the provided pattern.RichText[]
Splits the current text into an array of RichText objects based on the given pattern and limit.boolean
Test whether this instance starts with the givenCharSequence
.stream()
A stream of the Runs this text consists of.strip()
Remove leading and trailing whitespace, as defined by Unicode, removed.Remove leading whitespace, as defined by Unicode, removed.Remove trailing whitespace, as defined by Unicode, removed.stylesAt
(int pos) Get active styles at position.subSequence
(int start) Get a subrange of this instance.subSequence
(int start, int end) Get a subrange of this instance.static RichText
tab()
Returns RichText containing a single tabulator.static boolean
textAndFontEquals
(@Nullable RichText a, @Nullable RichText b) Check if texts and style are equal, ignoring other attributes.Get RichText representation of this object (similar totoString()
).toString()
trim()
Remove leading and trailing whitespace, defined as "any character whose codepoint is less than or equal to'U+0020'
(the space character)".static RichText
valueOf
(char c) Convert char to RichText.static RichText
Convert char to RichText.static RichText
valueOf
(char c, Collection<Style> styles) Convert char to RichText.static RichText
Get RichText containing an objects string representation.static RichText
Get styled RichText containing an object's string representation.static RichText
valueOf
(Object obj, Collection<Style> styles) Get styled RichText containing an object's string representation.static RichText
Convert String to RichText.Wrap RichText in style.Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface com.dua3.utility.text.AttributedCharSequence
attributedChars
Methods inherited from interface java.lang.CharSequence
chars, codePoints, isEmpty
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
Field Details
-
SPLIT_MARKER
public static final char SPLIT_MARKERA character used as a delimiter or marker to insert where subsequent text segment should not be joined into a single one.When two texts both marked to have italic style are joined, the
RichTextBuilder
class will not detect the attribute change and for example when converted to HTML, the result of the joined texts will not be "<i>some text </i><i>other text</i>" but instead "<i>some text other text<i>".For attributes that merely control text appearance, this is usually the desired result. But to prevent two paragraphs to be joined into a single one, at least one character must come between both texts so that the change can be detected. A SPLIT_MARKER can thus be inserted to make sure the paragraphs are not joined.
The
HtmlConverter
class for example automatically removes SPLIT_MARKER from the output.- See Also:
-
-
Method Details
-
emptyText
Returns the empty String as RichText.- Returns:
- the empty text
-
space
Returns RichText containing a single space character.- Returns:
- RichText.valueOf(" ")
-
tab
Returns RichText containing a single tabulator.- Returns:
- RichText.valueOf(" \ t ")
-
newline
Returns RichText containing a single newline character.- Returns:
- RichText.valueOf(" \ n ")
-
valueOf
Get RichText containing an objects string representation.- Parameters:
obj
- the object to convert to RichText- Returns:
- RichText.valueOf(String.valueOf ( obj))
-
valueOf
Convert String to RichText.- Parameters:
s
- the String to convert- Returns:
- RichText representation of s
-
valueOf
Convert char to RichText.- Parameters:
c
- the character- Returns:
- RichText containing only the character c
-
valueOf
Get styled RichText containing an object's string representation.- Parameters:
obj
- the object to convert to RichTextstyles
- the styles to apply- Returns:
- RichText.valueOf(String.valueOf ( obj))
-
valueOf
Get styled RichText containing an object's string representation.- Parameters:
obj
- the object to convert to RichTextstyles
- the styles to apply- Returns:
- RichText representation of s
-
valueOf
Convert char to RichText.- Parameters:
c
- the characterstyles
- the styles to apply- Returns:
- RichText containing only the character c
-
valueOf
Convert char to RichText.- Parameters:
c
- the characterstyles
- the styles to apply- Returns:
- RichText containing only the character c
-
textAndFontEquals
Check if texts and style are equal, ignoring other attributes.- Parameters:
a
- textb
- text- Returns:
- true, if a and b consist of the same characters with the same styling
-
join
Join RichText instances together.- Parameters:
delimiter
- the delimiterelements
- the elements to join- Returns:
- RichText containing the joined elements
-
join
Join RichText instances together.- Parameters:
delimiter
- the delimiterelements
- the elements to join- Returns:
- RichText containing the joined elements
-
join
Join RichText instances together.- Parameters:
delimiter
- the delimiterelements
- the elements to join- Returns:
- RichText containing the joined elements
-
join
Join RichText instances together.- Parameters:
delimiter
- the delimiterelements
- the elements to join- Returns:
- RichText containing the joined elements
-
matcher
Create aRichTextMatcher
.- Parameters:
pattern
- the patterntext
- the text- Returns:
- a matcher
-
joiner
Create aRichTextJoiner
.- Parameters:
delimiter
- the delimiter to use- Returns:
- the joiner
-
joiner
Create aRichTextJoiner
.- Parameters:
delimiter
- the delimiter to useprefix
- the prefixsuffix
- the suffix- Returns:
- the joiner
-
joiner
Create aRichTextJoiner
.- Parameters:
delimiter
- the delimiter to use- Returns:
- the joiner
-
joiner
Create aRichTextJoiner
.- Parameters:
delimiter
- the delimiter to useprefix
- the prefixsuffix
- the suffix- Returns:
- the joiner
-
equals
-
equals
Compare using a user supplied predicate for comparing Run instances.- Parameters:
other
- the object to compare torunEquals
- the BiPredicate used for comparing- Returns:
- result true, if obj is an instance of RichText and all runs compare as equal to this instance's runs using the supplied predicate
-
equalsTextAndFont
Check if texts and style are equal, ignoring other attributes.- Parameters:
other
- the text to compare with- Returns:
- true, if other consist of the same characters with the same styling as this instance
-
equalsText
Textual compare.- Parameters:
other
- theCharSequence
to compare to- Returns:
- true, if this instance contains the same sequence of characters as
other
-
equalsTextIgnoreCase
Textual compare ignoring case.- Parameters:
other
- theCharSequence
to compare to- Returns:
- true, if this instance contains the same sequence of characters as
other
-
hashCode
public int hashCode() -
isBlank
public boolean isBlank()Test if blank.- Returns:
true
if this instance is empty or contains only whitespace characters,false
otherwise
-
iterator
-
length
public int length()Length of text in characters.- Specified by:
length
in interfaceCharSequence
- Returns:
- the text length
-
stream
A stream of the Runs this text consists of.- Returns:
- stream of Runs
-
toString
- Specified by:
toString
in interfaceCharSequence
- Overrides:
toString
in classObject
-
appendTo
Description copied from interface:ToRichText
Append this object's rich text representation to a buffer.- Specified by:
appendTo
in interfaceToRichText
- Parameters:
builder
- the builder
-
toRichText
Description copied from interface:ToRichText
Get RichText representation of this object (similar totoString()
).- Specified by:
toRichText
in interfaceToRichText
- Returns:
- RichText presentation of the object
-
lines
Get stream of lines contained in this instance.- Returns:
- stream of this text's lines
-
subSequence
Get a subrange of this instance.- Specified by:
subSequence
in interfaceAttributedCharSequence
- Specified by:
subSequence
in interfaceCharSequence
- Parameters:
start
- begin index (inclusive)end
- end index (exclusive)- Returns:
- RichText instance of the subrange
-
subSequence
Get a subrange of this instance.- Parameters:
start
- start index (inclusive)- Returns:
- RichText instance of the subrange from beginIndex to the end
-
charAt
public char charAt(int index) - Specified by:
charAt
in interfaceCharSequence
-
attributedCharAt
Description copied from interface:AttributedCharSequence
Returns theAttributedCharacter
value at the specified index.- Specified by:
attributedCharAt
in interfaceAttributedCharSequence
- Parameters:
index
- the index of theAttributedCharacter
value to be returned- Returns:
- the specified
AttributedCharacter
value
-
trim
Remove leading and trailing whitespace, defined as "any character whose codepoint is less than or equal to'U+0020'
(the space character)".- Returns:
- copy of this instance with leading and trailing whitespace according to above criteria removed
- See Also:
-
strip
Remove leading and trailing whitespace, as defined by Unicode, removed.- Returns:
- copy of this instance with leading and trailing whitespace (according to Unicode) removed
- See Also:
-
stripLeading
Remove leading whitespace, as defined by Unicode, removed.- Returns:
- copy of this instance with leading whitespace (according to Unicode) removed
- See Also:
-
stripTrailing
Remove trailing whitespace, as defined by Unicode, removed.- Returns:
- copy of this instance with trailing whitespace (according to Unicode) removed
- See Also:
-
wrap
Wrap RichText in style.- Parameters:
style
- the style- Returns:
- copy of this RichText instance with style applied
-
split
Splits this RichText into an array of RichText objects based on the provided regular expression.- Parameters:
regex
- the regular expression to use as a delimiter for splitting this RichText- Returns:
- an array of RichText objects computed by splitting this RichText around matches of the given regular expression
- See Also:
-
split
Splits the current RichText into an array of RichText instances based on the provided pattern.- Parameters:
pattern
- the regular expression pattern used to split the RichText. It determines the boundaries for splitting.- Returns:
- an array of RichText objects resulting from splitting the current RichText based on the given pattern.
- See Also:
-
split
Splits the current RichText object into an array of RichText objects based on the given regular expression and limit.The method splits the text into segments using the provided regular expression as the delimiter. The maximum number of resulting segments can be controlled by the limit parameter.
- Parameters:
regex
- the regular expression used to determine the boundaries for splittinglimit
- the maximum number of segments to include in the result; if the limit is zero or negative, the method will include all segments, including trailing empty strings if any- Returns:
- an array of RichText objects resulting from the split operation
- See Also:
-
split
Splits the current text into an array of RichText objects based on the given pattern and limit.- Parameters:
pattern
- the regular expression pattern used to define delimiters for splitting the textlimit
- the maximum number of elements in the result array; if limit is less than or equal to zero, splits are unlimited; if limit is 0, trailing empty matches are removed from the result- Returns:
- an array of RichText objects, split from the current text based on the specified pattern and limit
- See Also:
-
replaceFirst
Replaces the first substring of this RichText that matches the given regular expression with the specified replacement RichText.- Parameters:
regex
- the regular expression to which this RichText is to be matchedreplacement
- the RichText to be used as a replacement for the first match- Returns:
- a new RichText with the first matching substring replaced by the replacement RichText
- See Also:
-
replaceFirst
Replaces the first substring of this RichText object that matches the given regular expression with the specified replacement string.- Parameters:
regex
- the regular expression to which the substring should matchreplacement
- the string to replace the first matching substring- Returns:
- a new RichText object with the first matching substring replaced
- See Also:
-
replaceAll
Replaces each substring of this RichText that matches the given regular expression with the specified RichText replacement.- Parameters:
regex
- the regular expression to which this RichText is matchedreplacement
- the RichText to be substituted for each match- Returns:
- a new RichText resulting from replacing all occurrences of the pattern with the specified replacement
- See Also:
-
replaceAll
Replaces every occurrence of the specified regular expression in the text with the given replacement string.- Parameters:
regex
- the regular expression to be matchedreplacement
- the string to replace each match- Returns:
- a new RichText object with the replacements made
- See Also:
-
indexOf
public int indexOf(int ch) Find character.- Parameters:
ch
- the character- Returns:
- the index of the first occurrence of
ch
, or -1 if not found - See Also:
-
indexOf
public int indexOf(char ch, int off) Find character.- Parameters:
ch
- the characteroff
- the starting position- Returns:
- the position where the char was found or -1 if not found
- See Also:
-
indexOf
Return the index of the needle in this RichText instance.- Parameters:
s
- the text to find- Returns:
- the first index, where s is found within this instance
- See Also:
-
indexOf
Return the index of the needle in this RichText instance.- Parameters:
s
- the text to findfromIndex
- the starting position- Returns:
- the first index, where s is found within this instance
- See Also:
-
startsWith
Test whether this instance starts with the givenCharSequence
.- Parameters:
s
- the sequence to test- Returns:
- true, if this instance starts with s
- See Also:
-
contains
Test if CharSequence is contained.- Parameters:
s
- the sequence to search for- Returns:
- true, if s is contained
- See Also:
-
apply
Gat styled copy of this instance.- Parameters:
style
- the style- Returns:
- styled copy
-
stylesAt
Get active styles at position.- Parameters:
pos
- the position (character index)- Returns:
- (unmodifiable) list of styles
-
runAt
Get run at position.- Parameters:
pos
- the position (character index)- Returns:
- the Run the character at the given position belongs to
-
runs
Get the runs of this instance.- Returns:
- unmodifiable list of runs
-
runStream
Get a stream containing the runs of this instance.- Returns:
- stream of runs
-
equalizer
Returns a BiPredicate that checks whether two RichText objects are equal based on the provided ComparisonSettings.- Parameters:
s
- the ComparisonSettings to be used for the equality check- Returns:
- the BiPredicate that checks for equality based on the ComparisonSettings
-
getBaseline
Calculates the baseline value based on the maximum descent of fonts within the runs of text.- Parameters:
defaultFont
- the default font used as a fallback when deriving fonts from font definitions- Returns:
- the maximum descent value among all derived fonts; returns 0.0 if no runs are present
-