Class RtfGenerator


  • public class RtfGenerator
    extends Object
    Generates RTF text via a simple Java API.

    The following RTF features are supported:

    • Fonts
    • Font sizes
    • Foreground and background colors
    • Bold, italic, and underline
    The RTF generated isn't really "optimized," but it will do, especially for small amounts of text, such as what's common when copy-and-pasting. It tries to be sufficient for the use case of copying syntax highlighted code:
    • It assumes that tokens changing foreground color often is fairly common.
    • It assumes that background highlighting is fairly uncommon.
    • Constructor Detail

      • RtfGenerator

        public RtfGenerator()
        Constructor.
    • Method Detail

      • appendToDoc

        public void appendToDoc​(String text,
                                Font f,
                                Color fg,
                                Color bg)
        Appends styled text to the RTF document being generated.
        Parameters:
        text - The text to append.
        f - The font of the text. If this is null, the default font is used.
        fg - The foreground of the text. If this is null, the default foreground color is used.
        bg - The background color of the text. If this is null, the default background color is used.
        See Also:
        appendNewline()
      • appendToDocNoFG

        public void appendToDocNoFG​(String text,
                                    Font f,
                                    Color bg,
                                    boolean underline)
        Appends styled text to the RTF document being generated.
        Parameters:
        text - The text to append.
        f - The font of the text. If this is null, the default font is used.
        bg - The background color of the text. If this is null, the default background color is used.
        underline - Whether the text should be underlined.
        See Also:
        appendNewline()
      • appendToDoc

        public void appendToDoc​(String text,
                                Font f,
                                Color fg,
                                Color bg,
                                boolean underline)
        Appends styled text to the RTF document being generated.
        Parameters:
        text - The text to append.
        f - The font of the text. If this is null, the default font is used.
        fg - The foreground of the text. If this is null, the default foreground color is used.
        bg - The background color of the text. If this is null, the default background color is used.
        underline - Whether the text should be underlined.
        See Also:
        appendNewline()
      • appendToDoc

        public void appendToDoc​(String text,
                                Font f,
                                Color fg,
                                Color bg,
                                boolean underline,
                                boolean setFG)
        Appends styled text to the RTF document being generated.
        Parameters:
        text - The text to append.
        f - The font of the text. If this is null, the default font is used.
        fg - The foreground of the text. If this is null, the default foreground color is used.
        bg - The background color of the text. If this is null, the default background color is used.
        underline - Whether the text should be underlined.
        setFG - Whether the foreground specified by fg should be honored (if it is non-null).
        See Also:
        appendNewline()
      • getRtf

        public String getRtf()
        Returns the RTF document created by this generator.
        Returns:
        The RTF document, as a String.
      • reset

        public void reset()
        Resets this generator. All document information and content is cleared.