Class RtfGenerator
java.lang.Object
org.fife.ui.rsyntaxtextarea.RtfGenerator
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
- It assumes that tokens changing foreground color often is fairly common.
- It assumes that background highlighting is fairly uncommon.
- Version:
- 1.1
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final intThe default font size for RTF.private StringBuilderprivate booleanprivate intprivate intprivate intprivate booleanprivate booleanprivate Colorprivate intJava2D 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 -
Method Summary
Modifier and TypeMethodDescriptionvoidAdds a newline to the RTF document.voidappendToDoc(String text, Font f, Color fg, Color bg) Appends styled text to the RTF document being generated.voidappendToDoc(String text, Font f, Color fg, Color bg, boolean underline) Appends styled text to the RTF document being generated.voidappendToDoc(String text, Font f, Color fg, Color bg, boolean underline, boolean setFG) Appends styled text to the RTF document being generated.voidappendToDocNoFG(String text, Font f, Color bg, boolean underline) Appends styled text to the RTF document being generated.private voidescapeAndAdd(StringBuilder sb, String text) Appends some text to a buffer, with special care taken for special characters as defined by the RTF spec.private intfixFontSize(float pointSize) Returns a font point size, adjusted for the current screen resolution.private static intgetColorIndex(List<Color> list, Color item) Returns the index of the specified item in a list.private Stringprivate static intgetFontIndex(List<Font> list, Font font) Returns the index of the specified font in a list of fonts.private Stringprivate static StringReturns a good "default" monospaced font to use when Java's logical font "Monospaced" is found.getRtf()Returns the RTF document created by this generator.voidreset()Resets this generator.
-
Field Details
-
mainBG
-
fontList
-
colorList
-
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 screenResJava2D 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_SIZEThe default font size for RTF. This is point size, in half points.- See Also:
-
-
Constructor Details
-
RtfGenerator
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
Appends styled text to the RTF document being generated.- Parameters:
text- The text to append.f- The font of the text. If this isnull, the default font is used.fg- The foreground of the text. If this isnull, the default foreground color is used.bg- The background color of the text. If this isnull, the default background color is used.- See Also:
-
appendToDocNoFG
Appends styled text to the RTF document being generated.- Parameters:
text- The text to append.f- The font of the text. If this isnull, the default font is used.bg- The background color of the text. If this isnull, the default background color is used.underline- Whether the text should be underlined.- See Also:
-
appendToDoc
Appends styled text to the RTF document being generated.- Parameters:
text- The text to append.f- The font of the text. If this isnull, the default font is used.fg- The foreground of the text. If this isnull, the default foreground color is used.bg- The background color of the text. If this isnull, the default background color is used.underline- Whether the text should be underlined.- See Also:
-
appendToDoc
Appends styled text to the RTF document being generated.- Parameters:
text- The text to append.f- The font of the text. If this isnull, the default font is used.fg- The foreground of the text. If this isnull, the default foreground color is used.bg- The background color of the text. If this isnull, the default background color is used.underline- Whether the text should be underlined.setFG- Whether the foreground specified byfgshould be honored (if it is non-null).- See Also:
-
escapeAndAdd
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).
- All tab characters are replaced with the string
"
-
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 fromgetSize2D().- 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
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
-
getFontIndex
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
-
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
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.
-