📄 rprintutilities.java
字号:
/*
* 11/14/2003
*
* RPrintUtilities.java - A collection of static methods useful for printing
* text from Swing text components.
* Copyright (C) 2003 Robert Futrell
* email@address.com
* www.website.com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
package org.fife.print;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.Element;
import javax.swing.text.Segment;
import javax.swing.text.JTextComponent;
import javax.swing.text.TabExpander;
import javax.swing.text.Utilities;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.print.*;
/**
* A collection of static methods useful for printing text from Swing text components.
*
* @author Robert Futrell
* @version 1.0
*/
public abstract class RPrintUtilities {
private static int currentDocLineNumber; // The line number in the document we are currently on.
private static int numDocLines; // The number of lines in the current document.
private static Element rootElement; // The first Element (line) in the current document.
// The characters at which to break a line if implementing word wrap.
private static final char [] breakChars = { ' ', '\t', ',', '.', ';', '?', '!' };
// These variables are 'global' because RPrintTabExpander uses them.
private static int xOffset; // The x-offset (for the page margin) when printing.
private static int tabSizeInSpaces; // The length of a tab, in spaces.
private static FontMetrics fontMetrics; // The metrics of the font currently being used to print.
/*****************************************************************************/
/**
* Returns the position closest to, but before, position <code>maxCharsPerLine</code> in
* <code>line</code> of one of the chars in <code>breakChars</code>, or simply returns
* <code>maxCharsPerLine-1</code> if none of the <code>breakChars</code> comes before
* that position. This position represents the logical line break for this <code>java.lang.String</code>
* if it is being printed in a monospaced font when lines can only be <code>maxCharsPerLine</code>
* characters long.
*
* @param line The text being printed.
* @param maxCharsPerLine Only up-to this many characters from <code>line</code> can be printed
* on one line.
* @return The logical position at which to stop printing <code>line</code> to simulate
* word wrap.
*/
private static int getLineBreakPoint(String line, final int maxCharsPerLine) {
int breakPoint = -1;
for (int i=0; i<breakChars.length; i++) {
int breakCharPos = line.lastIndexOf(breakChars[i], maxCharsPerLine-1);
if (breakCharPos > breakPoint)
breakPoint = breakCharPos;
}
return (breakPoint==-1 ? maxCharsPerLine-1 : breakPoint);
}
/*****************************************************************************/
/**
* 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;
fontMetrics = g.getFontMetrics();
// Create our tab expander.
//RPrintTabExpander tabExpander = new RPrintTabExpander();
// Get width and height of characters in this monospaced font.
int fontWidth = fontMetrics.charWidth('w'); // Any character will do here, since font is monospaced.
int fontHeight = fontMetrics.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() + fontMetrics.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);
try {
curLineString = doc.getText(currentLine.getStartOffset(), currentLine.getEndOffset()-currentLine.getStartOffset());
} catch (BadLocationException ble) {
System.err.println("EXCEPTION - " + ble);
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.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 += " ";
curLineString = curLineString.replaceFirst("\t", replacementString); // Note that "\t" is actually a regex for this method.
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;
}
/*****************************************************************************/
/**
* Prints a <code>Document</code> 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.
*
* @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 #printDocumentMonospaced
*/
public static int printDocumentMonospacedWordWrap(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;
fontMetrics = g.getFontMetrics();
// Create our tab expander.
//RPrintTabExpander tabExpander = new RPrintTabExpander();
// Get width and height of characters in this monospaced font.
int fontWidth = fontMetrics.charWidth('w'); // Any character will do here, since font is monospaced.
int fontHeight = fontMetrics.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() + fontMetrics.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);
try {
curLineString = doc.getText(currentLine.getStartOffset(), currentLine.getEndOffset()-currentLine.getStartOffset());
} catch (BadLocationException ble) {
System.err.println("EXCEPTION - " + ble);
return Printable.NO_SUCH_PAGE;
}
// Remove newlines, because they end up as boxes if you don't; this is a monospaced font.
curLineString = curLineString.replaceAll("\n", "");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -