Class RtfGenerator


  • public class RtfGenerator
    extends java.lang.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.
    Version:
    1.1
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.util.List<java.awt.Color> colorList  
      private static int DEFAULT_FONT_SIZE
      The default font size for RTF.
      private java.lang.StringBuilder document  
      private java.util.List<java.awt.Font> fontList  
      private boolean lastBold  
      private int lastFGIndex  
      private int lastFontIndex  
      private int lastFontSize  
      private boolean lastItalic  
      private boolean lastWasControlWord  
      private java.awt.Color mainBG  
      private int screenRes
      Java2D assumes a 72 dpi screen resolution, but on Windows the screen resolution is either 96 dpi or 120 dpi, depending on your font display settings.
    • Constructor Summary

      Constructors 
      Constructor Description
      RtfGenerator​(java.awt.Color mainBG)
      Constructor.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void appendNewline()
      Adds a newline to the RTF document.
      void appendToDoc​(java.lang.String text, java.awt.Font f, java.awt.Color fg, java.awt.Color bg)
      Appends styled text to the RTF document being generated.
      void appendToDoc​(java.lang.String text, java.awt.Font f, java.awt.Color fg, java.awt.Color bg, boolean underline)
      Appends styled text to the RTF document being generated.
      void appendToDoc​(java.lang.String text, java.awt.Font f, java.awt.Color fg, java.awt.Color bg, boolean underline, boolean setFG)
      Appends styled text to the RTF document being generated.
      void appendToDocNoFG​(java.lang.String text, java.awt.Font f, java.awt.Color bg, boolean underline)
      Appends styled text to the RTF document being generated.
      private void escapeAndAdd​(java.lang.StringBuilder sb, java.lang.String text)
      Appends some text to a buffer, with special care taken for special characters as defined by the RTF spec.
      private int fixFontSize​(float pointSize)
      Returns a font point size, adjusted for the current screen resolution.
      private static int getColorIndex​(java.util.List<java.awt.Color> list, java.awt.Color item)
      Returns the index of the specified item in a list.
      private java.lang.String getColorTableRtf()  
      private static int getFontIndex​(java.util.List<java.awt.Font> list, java.awt.Font font)
      Returns the index of the specified font in a list of fonts.
      private java.lang.String getFontTableRtf()  
      private static java.lang.String getMonospacedFontFamily()
      Returns a good "default" monospaced font to use when Java's logical font "Monospaced" is found.
      java.lang.String getRtf()
      Returns the RTF document created by this generator.
      void reset()
      Resets this generator.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • mainBG

        private java.awt.Color mainBG
      • fontList

        private java.util.List<java.awt.Font> fontList
      • colorList

        private java.util.List<java.awt.Color> colorList
      • document

        private java.lang.StringBuilder document
      • lastWasControlWord

        private boolean lastWasControlWord
      • lastFontIndex

        private int lastFontIndex
      • lastFGIndex

        private int lastFGIndex
      • lastBold

        private boolean lastBold
      • lastItalic

        private boolean lastItalic
      • lastFontSize

        private int lastFontSize
      • screenRes

        private int screenRes
        Java2D assumes a 72 dpi screen resolution, but on Windows the screen resolution is either 96 dpi or 120 dpi, depending on your font display settings. This is an attempt to make the RTF generated match the size of what's displayed in the RSyntaxTextArea.
      • DEFAULT_FONT_SIZE

        private static final int DEFAULT_FONT_SIZE
        The default font size for RTF. This is point size, in half points.
        See Also:
        Constant Field Values
    • Constructor Detail

      • RtfGenerator

        public RtfGenerator​(java.awt.Color mainBG)
        Constructor.
        Parameters:
        mainBG - The main background color to use.
    • Method Detail

      • appendToDoc

        public void appendToDoc​(java.lang.String text,
                                java.awt.Font f,
                                java.awt.Color fg,
                                java.awt.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​(java.lang.String text,
                                    java.awt.Font f,
                                    java.awt.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​(java.lang.String text,
                                java.awt.Font f,
                                java.awt.Color fg,
                                java.awt.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​(java.lang.String text,
                                java.awt.Font f,
                                java.awt.Color fg,
                                java.awt.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()
      • escapeAndAdd

        private void escapeAndAdd​(java.lang.StringBuilder sb,
                                  java.lang.String text)
        Appends some text to a buffer, with special care taken for special characters as defined by the RTF spec.
        • All tab characters are replaced with the string "\tab"
        • '\', '{' and '}' are changed to "\\", "\{" and "\}"
        Parameters:
        text - The text to append (with tab chars substituted).
        sb - The buffer to append to.
      • fixFontSize

        private int fixFontSize​(float pointSize)
        Returns a font point size, adjusted for the current screen resolution.

        Java2D assumes 72 dpi. On systems with larger dpi (Windows, GTK, etc.), font rendering will appear too small if we simply return a Java "Font" object's getSize() value. We need to adjust it for the screen resolution.

        Parameters:
        pointSize - A Java Font's point size, as returned from getSize2D().
        Returns:
        The font point size, adjusted for the current screen resolution. This will allow other applications to render fonts the same size as they appear in the Java application.
      • getColorIndex

        private static int getColorIndex​(java.util.List<java.awt.Color> list,
                                         java.awt.Color item)
        Returns the index of the specified item in a list. If the item is not in the list, it is added, and its new index is returned.
        Parameters:
        list - The list (possibly) containing the item.
        item - The item to get the index of.
        Returns:
        The index of the item.
      • getColorTableRtf

        private java.lang.String getColorTableRtf()
      • getFontIndex

        private static int getFontIndex​(java.util.List<java.awt.Font> list,
                                        java.awt.Font font)
        Returns the index of the specified font in a list of fonts. This method only checks for a font by its family name; its attributes such as bold and italic are ignored.

        If the font is not in the list, it is added, and its new index is returned.

        Parameters:
        list - The list (possibly) containing the font.
        font - The font to get the index of.
        Returns:
        The index of the font.
      • getFontTableRtf

        private java.lang.String getFontTableRtf()
      • getMonospacedFontFamily

        private static java.lang.String getMonospacedFontFamily()
        Returns a good "default" monospaced font to use when Java's logical font "Monospaced" is found.
        Returns:
        The monospaced font family to use.
      • getRtf

        public java.lang.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.