Java Swing Font Size printDocumentMonospaced(Graphics g, Document doc, int fontSize, int pageIndex, PageFormat pageFormat, int tabSize)

Here you can find the source of printDocumentMonospaced(Graphics g, Document doc, int fontSize, int pageIndex, PageFormat pageFormat, int tabSize)

Description

Prints a Document using a monospaced font, and does no word wrapping (ie, words will wrap mid-word to the next line).

License

BSD License

Parameter

Parameter Description
g The graphics context to write to.
doc The <code>javax.swing.text.Document</code> 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.

Declaration

public static int printDocumentMonospaced(Graphics g, Document doc,
        int fontSize, int pageIndex, PageFormat pageFormat, int tabSize) 

Method Source Code

//package com.java2s;
/*/*from  w  ww.j  av  a2 s  . com*/
 * 11/14/2003
 *
 * RPrintUtilities.java - A collection of static methods useful for printing
 * text from Swing text components.
 * 
 * This library is distributed under a modified BSD license.  See the included
 * RSyntaxTextArea.License.txt file for details.
 */

import javax.swing.text.*;
import java.awt.*;
import java.awt.print.PageFormat;
import java.awt.print.Printable;

public class Main {
    private static int currentDocLineNumber;
    private static int numDocLines;
    private static Element rootElement;
    private static int xOffset;
    private static int tabSizeInSpaces;
    private static FontMetrics fm;

    /**
     * Prints a <code>Document</code> 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.
     *
     * @param g          The graphics context to write to.
     * @param doc        The <code>javax.swing.text.Document</code> to print.
     * @param fontSize   the point size to use for the monospaced font.
     * @param pageIndex  The page number to print.
     * @param pageFormat The format to print the page with.
     * @param tabSize    The number of spaces to expand tabs to.
     * @see #printDocumentMonospacedWordWrap
     */
    public static int printDocumentMonospaced(Graphics g, Document doc,
            int fontSize, int pageIndex, PageFormat pageFormat, int tabSize) {

        g.setColor(Color.BLACK);
        g.setFont(new Font("Monospaced", Font.PLAIN, fontSize));

        // Initialize our static variables (these are used by our tab expander below).
        tabSizeInSpaces = tabSize;
        fm = g.getFontMetrics();

        // Create our tab expander.
        //RPrintTabExpander tabExpander = new RPrintTabExpander();

        // Get width and height of characters in this monospaced font.
        int fontWidth = fm.charWidth('w'); // Any character will do as font is monospaced.
        int fontHeight = fm.getHeight();

        int MAX_CHARS_PER_LINE = (int) pageFormat.getImageableWidth()
                / fontWidth;
        int MAX_LINES_PER_PAGE = (int) pageFormat.getImageableHeight()
                / fontHeight;

        final int STARTING_LINE_NUMBER = MAX_LINES_PER_PAGE * pageIndex;

        // The (x,y) coordinate to print at (in pixels, not characters).
        // Since y is the baseline of where we'll start printing (not the top-left
        // corner), we offset it by the font's ascent ( + 1 just for good measure).
        xOffset = (int) pageFormat.getImageableX();
        int y = (int) pageFormat.getImageableY() + fm.getAscent() + 1;

        // A counter to keep track of the number of lines that WOULD HAVE been
        // printed if we were printing all lines.
        int numPrintedLines = 0;

        // Keep going while there are more lines in the document.
        currentDocLineNumber = 0; // The line number of the document we're currently on.
        rootElement = doc.getDefaultRootElement(); // To shorten accesses in our loop.
        numDocLines = rootElement.getElementCount(); // The number of lines in our document.
        while (currentDocLineNumber < numDocLines) {

            // Get the line we are going to print.
            String curLineString;
            Element currentLine = rootElement
                    .getElement(currentDocLineNumber);
            int startOffs = currentLine.getStartOffset();
            try {
                curLineString = doc.getText(startOffs,
                        currentLine.getEndOffset() - startOffs);
            } catch (BadLocationException ble) { // Never happens
                ble.printStackTrace();
                return Printable.NO_SUCH_PAGE;
            }

            // Get rid of newlines, because they end up as boxes if you don't; this is a monospaced font.
            curLineString = curLineString.replaceAll("\n", "");

            // Replace tabs with how many spaces they should be.
            if (tabSizeInSpaces == 0) {
                curLineString = curLineString.replaceAll("\t", "");
            } else {
                int tabIndex = curLineString.indexOf('\t');
                while (tabIndex > -1) {
                    int spacesNeeded = tabSizeInSpaces
                            - (tabIndex % tabSizeInSpaces);
                    String replacementString = "";
                    for (int i = 0; i < spacesNeeded; i++)
                        replacementString += ' ';
                    // Note that "\t" is actually a regex for this method.
                    curLineString = curLineString.replaceFirst("\t",
                            replacementString);
                    tabIndex = curLineString.indexOf('\t');
                }
            }

            // If this document line is too long to fit on one printed line on the page,
            // break it up into multpile lines.
            while (curLineString.length() > MAX_CHARS_PER_LINE) {

                numPrintedLines++;
                if (numPrintedLines > STARTING_LINE_NUMBER) {
                    g.drawString(
                            curLineString.substring(0, MAX_CHARS_PER_LINE),
                            xOffset, y);
                    y += fontHeight;
                    if (numPrintedLines == STARTING_LINE_NUMBER
                            + MAX_LINES_PER_PAGE)
                        return Printable.PAGE_EXISTS;
                }

                curLineString = curLineString.substring(MAX_CHARS_PER_LINE,
                        curLineString.length());

            }

            currentDocLineNumber += 1; // We have printed one more line from the document.

            numPrintedLines++;
            if (numPrintedLines > STARTING_LINE_NUMBER) {
                g.drawString(curLineString, xOffset, y);
                y += fontHeight;
                if (numPrintedLines == STARTING_LINE_NUMBER
                        + MAX_LINES_PER_PAGE)
                    return Printable.PAGE_EXISTS;
            }

        }

        // Now, the whole document has been "printed."  Decide if this page had any text on it or not.
        if (numPrintedLines > STARTING_LINE_NUMBER)
            return Printable.PAGE_EXISTS;
        return Printable.NO_SUCH_PAGE;

    }
}

Related

  1. getDefaultFontSize()
  2. getFontRelativeSize(int size)
  3. getFontSizeFactor()
  4. getSizeOfTextInComponent(String text, Font font, JComponent component)
  5. increaseFontSize(JComponent component, int offset)
  6. printDocumentMonospacedWordWrap(Graphics g, Document doc, int fontSize, int pageIndex, PageFormat pageFormat, int tabSize)
  7. resetUIFont(String name, int style, int size)
  8. resetUIFontSize(int size)
  9. ScaleLabelFontsizeAndDimension(JLabel label, int fontSize)