Class RtfGenerator

java.lang.Object
org.fife.ui.rsyntaxtextarea.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.
Version:
1.1
  • Field Details

    • mainBG

      private Color mainBG
    • fontList

      private List<Font> fontList
    • colorList

      private List<Color> colorList
    • document

      private 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:
  • Constructor Details

    • RtfGenerator

      public RtfGenerator(Color mainBG)
      Constructor.
      Parameters:
      mainBG - The main background color to use.
  • Method Details

    • appendNewline

      public void appendNewline()
      Adds a newline to the RTF document.
      See Also:
    • 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:
    • 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:
    • 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:
    • 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:
    • escapeAndAdd

      private void escapeAndAdd(StringBuilder sb, 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:
      sb - The buffer to append to.
      text - The text to append (with tab chars substituted).
    • 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(List<Color> list, 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 String getColorTableRtf()
    • getFontIndex

      private static int getFontIndex(List<Font> list, 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 String getFontTableRtf()
    • getMonospacedFontFamily

      private static 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 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.