numDocLines = rootElement.getElementCount(); // The number of lines in our document.
currentDocLineNumber = 0; // The line number of the document we're currently on.
int startingOffset = 0; // Used when a line is so long it has to be wrapped.
while (currentDocLineNumber<numDocLines) {
Segment currentLineSeg = new Segment();
// Get the current line (as an Element), and its starting and ending offset in doc.
Element currentLine = rootElement.getElement(currentDocLineNumber);
int currentLineStart = currentLine.getStartOffset();
int currentLineEnd = currentLine.getEndOffset();
// Put the chars of this line in currentLineSeg, but only starting at our desired offset
// (because this line may be the second part of a wrapped line, so we'd start after the part
// that has already been printed).
try {
doc.getText(currentLineStart+startingOffset, currentLineEnd-(currentLineStart+startingOffset),
currentLineSeg);
} catch (BadLocationException ble) {
System.err.println("BadLocationException in print (where there shouldn't be one!): " + ble);
return Printable.NO_SUCH_PAGE;
}
// Remove any spaces and/or tabs from the end of the segment (would cause problems if you left 'em).
currentLineSeg = removeEndingWhitespace(currentLineSeg);
// Figger out how long the line is, in pixels.
int currentLineLengthInPixels = Utilities.getTabbedTextWidth(currentLineSeg, fm, 0, tabExpander, 0);
//System.err.println("'" + currentLineSeg + "' - " + currentLineLengthInPixels + "/" + LINE_LENGTH_IN_PIXELS);
// If it'll fit by itself on a printed line, great.
if (currentLineLengthInPixels <= LINE_LENGTH_IN_PIXELS) {
currentDocLineNumber += 1; // We (will) have printed one more line from the document.
startingOffset = 0; // Start at the first character in the new document line.
}
// If it won't fit on a printed line by itself (i.e., it needs to be wrapped)...
else {
// Loop while the current line is too long to fit on a printed line.
int currentPos = -1;
while (currentLineLengthInPixels > LINE_LENGTH_IN_PIXELS) {
//System.err.println("'" + currentLineSeg + "' - " + currentLineLengthInPixels + "/" + LINE_LENGTH_IN_PIXELS);
// Remove any spaces and/or tabs from the end of the segment (would cause problems if you left 'em).
currentLineSeg = removeEndingWhitespace(currentLineSeg);
// currentPos will be the last position in the current text of a "line break character."
currentPos = -1;
String currentLineString = currentLineSeg.toString();
for (int i=0; i<breakChars.length; i++) {
// "+1" below so we include the character on the line.
int pos = currentLineString.lastIndexOf(breakChars[i]) + 1;
//if (pos>-1 && pos>currentPos)
// currentPos = pos;