Class RPrintUtilities


  • public abstract class RPrintUtilities
    extends java.lang.Object
    A collection of static methods useful for printing text from Swing text components.
    Version:
    1.0
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      private static class  RPrintUtilities.RPrintTabExpander
      A tab expander for the document currently being printed with the font being used for the printing.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static char[] BREAK_CHARS  
      private static int currentDocLineNumber  
      private static java.awt.FontMetrics fm
      The metrics of the font currently being used to print.
      private static int numDocLines  
      private static javax.swing.text.Element rootElement  
      private static int tabSizeInSpaces
      The length of a tab, in spaces.
      private static int xOffset
      The x-offset (for the page margin) when printing.
    • Constructor Summary

      Constructors 
      Constructor Description
      RPrintUtilities()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      private static int getLineBreakPoint​(java.lang.String line, int maxCharsPerLine)
      Returns the position closest to, but before, position maxCharsPerLine in line of one of the chars in breakChars, or simply returns maxCharsPerLine-1 if none of the breakChars comes before that position.
      static int printDocumentMonospaced​(java.awt.Graphics g, javax.swing.text.Document doc, int fontSize, int pageIndex, java.awt.print.PageFormat pageFormat, int tabSize)
      Prints a Document using a monospaced font, and does no word wrapping (ie, words will wrap mid-word to the next line).
      static int printDocumentMonospacedWordWrap​(java.awt.Graphics g, javax.swing.text.Document doc, int fontSize, int pageIndex, java.awt.print.PageFormat pageFormat, int tabSize)
      Prints a Document using a monospaced font, word wrapping on the characters ' ', '\t', '\n', ',', '.', and ';'.
      static int printDocumentWordWrap​(java.awt.Graphics g, javax.swing.text.JTextComponent textComponent, java.awt.Font font, int pageIndex, java.awt.print.PageFormat pageFormat, int tabSize)
      Prints a Document using the specified font, word wrapping on the characters ' ', '\t', '\n', ',', '.', and ';'.
      private static javax.swing.text.Segment removeEndingWhitespace​(javax.swing.text.Segment segment)
      Removes any spaces or tabs from the end of the segment.
      • Methods inherited from class java.lang.Object

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

      • currentDocLineNumber

        private static int currentDocLineNumber
      • numDocLines

        private static int numDocLines
      • rootElement

        private static javax.swing.text.Element rootElement
      • BREAK_CHARS

        private static final char[] BREAK_CHARS
      • xOffset

        private static int xOffset
        The x-offset (for the page margin) when printing.
      • tabSizeInSpaces

        private static int tabSizeInSpaces
        The length of a tab, in spaces.
      • fm

        private static java.awt.FontMetrics fm
        The metrics of the font currently being used to print.
    • Constructor Detail

      • RPrintUtilities

        public RPrintUtilities()
    • Method Detail

      • getLineBreakPoint

        private static int getLineBreakPoint​(java.lang.String line,
                                             int maxCharsPerLine)
        Returns the position closest to, but before, position maxCharsPerLine in line of one of the chars in breakChars, or simply returns maxCharsPerLine-1 if none of the breakChars comes before that position. This position represents the logical line break for this java.lang.String if it is being printed in a monospaced font when lines can only be maxCharsPerLine characters long.
        Parameters:
        line - The text being printed.
        maxCharsPerLine - Only up-to this many characters from line can be printed on one line.
        Returns:
        The logical position at which to stop printing line to simulate word wrap.
      • printDocumentMonospaced

        public static int printDocumentMonospaced​(java.awt.Graphics g,
                                                  javax.swing.text.Document doc,
                                                  int fontSize,
                                                  int pageIndex,
                                                  java.awt.print.PageFormat pageFormat,
                                                  int tabSize)
        Prints a Document using a monospaced font, and does no word wrapping (ie, words will wrap mid-word to the next line). This method is expected to be called from Printable 'print(Graphics g)' functions.
        Parameters:
        g - The graphics context to write to.
        doc - The javax.swing.text.Document to print.
        fontSize - the point size to use for the monospaced font.
        pageIndex - The page number to print.
        pageFormat - The format to print the page with.
        tabSize - The number of spaces to expand tabs to.
        Returns:
        One of the constants from Printable.
        See Also:
        printDocumentMonospacedWordWrap(java.awt.Graphics, javax.swing.text.Document, int, int, java.awt.print.PageFormat, int)
      • printDocumentMonospacedWordWrap

        public static int printDocumentMonospacedWordWrap​(java.awt.Graphics g,
                                                          javax.swing.text.Document doc,
                                                          int fontSize,
                                                          int pageIndex,
                                                          java.awt.print.PageFormat pageFormat,
                                                          int tabSize)
        Prints a Document using a monospaced font, word wrapping on the characters ' ', '\t', '\n', ',', '.', and ';'. This method is expected to be called from Printable 'print(Graphics g)' functions.
        Parameters:
        g - The graphics context to write to.
        doc - The javax.swing.text.Document to print.
        fontSize - the point size to use for the monospaced font.
        pageIndex - The page number to print.
        pageFormat - The format to print the page with.
        tabSize - The number of spaces to expand tabs to.
        Returns:
        One of the constants from Printable.
        See Also:
        printDocumentMonospaced(java.awt.Graphics, javax.swing.text.Document, int, int, java.awt.print.PageFormat, int)
      • printDocumentWordWrap

        public static int printDocumentWordWrap​(java.awt.Graphics g,
                                                javax.swing.text.JTextComponent textComponent,
                                                java.awt.Font font,
                                                int pageIndex,
                                                java.awt.print.PageFormat pageFormat,
                                                int tabSize)
        Prints a Document using the specified font, word wrapping on the characters ' ', '\t', '\n', ',', '.', and ';'. This method is expected to be called from Printable 'print(Graphics g)' functions.
        Parameters:
        g - The graphics context to write to.
        textComponent - The javax.swing.text.JTextComponent whose text you're printing.
        font - The font to use for printing. If null, then textComponent's font is used.
        pageIndex - The page number to print.
        pageFormat - The format to print the page with.
        tabSize - The number of spaces to convert tabs to.
        Returns:
        One of the constants from Printable.
      • removeEndingWhitespace

        private static javax.swing.text.Segment removeEndingWhitespace​(javax.swing.text.Segment segment)
        Removes any spaces or tabs from the end of the segment.
        Parameters:
        segment - The segment from which to remove tailing whitespace.
        Returns:
        segment with trailing whitespace removed.