Class RichText

java.lang.Object
com.codename1.wordprocessing.RichText

public final class RichText extends Object

Immutable styled text: a sequence of runs, each a string with one TextStyle, and inline objects -- fields, note references, pictures and embedded components -- each of which takes exactly one character, OBJECT.

Adjacent text runs with equal styles are merged and empty runs are dropped, so two texts that read the same and look the same are equals. An object run is never merged.

  • Field Details

    • OBJECT

      public static final char OBJECT
      The character an inline object occupies, U+FFFC OBJECT REPLACEMENT CHARACTER.
      See Also:
    • EMPTY

      public static final RichText EMPTY
      The empty text.
  • Method Details

    • of

      public static RichText of(String s)

      Plain text in the default style.

      Parameters
      • s: the characters
      Returns

      the text

    • of

      public static RichText of(String s, TextStyle style)

      Text in a single style.

      Parameters
      • s: the characters

      • style: their style

      Returns

      the text

    • of

      public static RichText of(Inline object, TextStyle style)

      A text holding one inline object.

      Parameters
      • object: the object

      • style: the style of its character

      Returns

      the text

    • length

      public int length()

      The number of characters.

      Returns

      the length

    • isEmpty

      public boolean isEmpty()

      Whether the text has no characters.

      Returns

      true when empty

    • runCount

      public int runCount()

      The number of runs, inline objects included.

      Returns

      the run count

    • runText

      public String runText(int index)

      The characters of one run; OBJECT for an inline object.

      Parameters
      • index: the run, from zero
      Returns

      the run's text

    • runStyle

      public TextStyle runStyle(int index)

      The style of one run.

      Parameters
      • index: the run, from zero
      Returns

      the run's style

    • runObject

      public Inline runObject(int index)

      The inline object of one run.

      Parameters
      • index: the run, from zero
      Returns

      the object, or null for a text run

    • objectAt

      public Inline objectAt(int offset)

      The inline object at a character offset.

      Parameters
      • offset: the character offset
      Returns

      the object, or null when the character is not an object

    • hasObjects

      public boolean hasObjects()

      Whether the text holds any inline object.

      Returns

      true when it does

    • plainText

      public String plainText()

      The characters without formatting. Every inline object is one OBJECT character.

      Returns

      the plain text

    • displayText

      public String displayText()

      The text as a reader sees it: a field shows its result, a note reference its mark, and other objects nothing.

      Returns

      the readable text

    • styleAt

      public TextStyle styleAt(int offset)

      The style at a character offset. At the end of the text, and for an empty text, this is the style of the last run, so text typed there continues it.

      Parameters
      • offset: the character offset
      Returns

      the style in effect

    • visit

      public void visit(RichText.Visitor visitor)

      Calls the visitor once per run, in order.

      Parameters
      • visitor: the receiver
    • substring

      public RichText substring(int start, int end)

      The characters in [start, end) with their styles and objects.

      Parameters
      • start: the first offset, inclusive

      • end: the last offset, exclusive

      Returns

      the slice

    • insert

      public RichText insert(int offset, RichText insert)

      This text with insert placed at an offset.

      Parameters
      • offset: where to insert

      • insert: the text to insert

      Returns

      the new text

    • delete

      public RichText delete(int start, int end)

      This text without the characters in [start, end).

      Parameters
      • start: the first offset, inclusive

      • end: the last offset, exclusive

      Returns

      the new text

    • append

      public RichText append(RichText other)

      This text followed by another.

      Parameters
      • other: the text to append
      Returns

      the new text

    • restyle

      public RichText restyle(int start, int end, RichText.StyleMapper mapper)

      Restyles the characters in [start, end).

      Parameters
      • start: the first offset, inclusive

      • end: the last offset, exclusive

      • mapper: computes each run's new style from its old one

      Returns

      the new text

    • withObject

      public RichText withObject(int offset, Inline object)

      This text with the object at offset replaced.

      Parameters
      • offset: the object's character

      • object: the replacement

      Returns

      the new text

    • styles

      public List<TextStyle> styles()

      The distinct styles used, in order of first use.

      Returns

      an unmodifiable list

    • equals

      public boolean equals(Object o)

      Two texts are equal when they have the same runs and objects.

      Parameters
      • o: the other object
      Returns

      true when equal

      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()

      A hash consistent with equals.

      Returns

      the hash

      Overrides:
      hashCode in class Object
    • toString

      public String toString()

      The plain text, for debugging.

      Returns

      the characters

      Overrides:
      toString in class Object